/* Handle parameterized types (templates) for GNU C++. Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 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 GNU CC. GNU CC 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. GNU CC 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 GNU CC; 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 "obstack.h" #include "tree.h" #include "flags.h" #include "cp-tree.h" #include "decl.h" #include "parse.h" #include "lex.h" #include "output.h" #include "except.h" #include "toplev.h" #include "rtl.h" #include "ggc.h" #include "timevar.h" /* The type of functions taking a tree, and some additional data, and returning an int. */ typedef int (*tree_fn_t) PARAMS ((tree, void*)); extern struct obstack permanent_obstack; /* 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 a SRCLOC indicating where the instantiate request occurred; the TREE_VALUE is a either a DECL (for a function or static data member), or a TYPE (for a class) indicating what we are hoping to instantiate. */ static tree pending_templates; static tree *template_tail = &pending_templates; static tree maybe_templates; static tree *maybe_template_tail = &maybe_templates; int processing_template_parmlist; static int template_header_count; static tree saved_trees; static varray_type inline_parm_levels; static size_t inline_parm_levels_used; static tree current_tinst_level; /* 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 obstack_chunk_alloc xmalloc #define obstack_chunk_free free #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 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 int resolve_overloaded_unification PARAMS ((tree, tree, tree, tree, unification_kind_t, int)); static int try_one_overload PARAMS ((tree, tree, tree, tree, tree, unification_kind_t, int)); static int unify PARAMS ((tree, tree, tree, tree, int)); static void add_pending_template PARAMS ((tree)); static int push_tinst_level PARAMS ((tree)); static void reopen_tinst_level PARAMS ((tree)); static tree classtype_mangled_name PARAMS ((tree)); static char *mangle_class_name_for_template PARAMS ((const char *, tree, tree)); static tree tsubst_initializer_list PARAMS ((tree, tree)); static int list_eq PARAMS ((tree, tree)); static tree get_class_bindings PARAMS ((tree, tree, tree)); static tree coerce_template_parms PARAMS ((tree, tree, tree, int, int)); static void tsubst_enum PARAMS ((tree, tree, tree)); static tree add_to_template_args PARAMS ((tree, tree)); static tree add_outermost_template_args PARAMS ((tree, tree)); static int maybe_adjust_types_for_deduction PARAMS ((unification_kind_t, tree*, tree*)); static int type_unification_real PARAMS ((tree, tree, tree, tree, int, unification_kind_t, int, int)); static void note_template_header PARAMS ((int)); static tree maybe_fold_nontype_arg PARAMS ((tree)); static tree convert_nontype_argument PARAMS ((tree, tree)); static tree convert_template_argument PARAMS ((tree, tree, tree, int, int , tree)); static tree get_bindings_overload PARAMS ((tree, tree, tree)); static int for_each_template_parm PARAMS ((tree, tree_fn_t, void*)); static tree build_template_parm_index PARAMS ((int, int, int, tree, tree)); static int inline_needs_template_parms PARAMS ((tree)); static void push_inline_template_parms_recursive PARAMS ((tree, int)); static tree retrieve_specialization PARAMS ((tree, tree)); static tree retrieve_local_specialization PARAMS ((tree)); static tree register_specialization PARAMS ((tree, tree, tree)); static void register_local_specialization PARAMS ((tree, tree)); static int unregister_specialization PARAMS ((tree, tree)); static tree reduce_template_parm_level PARAMS ((tree, tree, int)); static tree build_template_decl PARAMS ((tree, tree)); static int mark_template_parm PARAMS ((tree, void *)); static tree tsubst_friend_function PARAMS ((tree, tree)); static tree tsubst_friend_class PARAMS ((tree, tree)); static tree get_bindings_real PARAMS ((tree, tree, tree, int, int, int)); static int template_decl_level PARAMS ((tree)); static tree maybe_get_template_decl_from_type_decl PARAMS ((tree)); static int check_cv_quals_for_unify PARAMS ((int, tree, tree)); static tree tsubst_template_arg_vector PARAMS ((tree, tree, int)); static tree tsubst_template_parms PARAMS ((tree, tree, int)); static void regenerate_decl_from_template PARAMS ((tree, tree)); static tree most_specialized PARAMS ((tree, tree, tree)); static tree most_specialized_class PARAMS ((tree, tree)); static void set_mangled_name_for_template_decl PARAMS ((tree)); static int template_class_depth_real PARAMS ((tree, int)); static tree tsubst_aggr_type PARAMS ((tree, tree, int, tree, int)); static tree tsubst_decl PARAMS ((tree, tree, tree)); static tree tsubst_arg_types PARAMS ((tree, tree, int, tree)); static tree tsubst_function_type PARAMS ((tree, tree, int, tree)); static void check_specialization_scope PARAMS ((void)); static tree process_partial_specialization PARAMS ((tree)); static void set_current_access_from_decl PARAMS ((tree)); static void check_default_tmpl_args PARAMS ((tree, tree, int, int)); static tree tsubst_call_declarator_parms PARAMS ((tree, tree, int, tree)); static tree get_template_base_recursive PARAMS ((tree, tree, tree, tree, tree, int)); static tree get_template_base PARAMS ((tree, tree, tree, tree)); static int verify_class_unification PARAMS ((tree, tree, tree)); static tree try_class_unification PARAMS ((tree, tree, tree, tree)); static int coerce_template_template_parms PARAMS ((tree, tree, int, tree, tree)); static tree determine_specialization PARAMS ((tree, tree, tree *, int)); static int template_args_equal PARAMS ((tree, tree)); static void tsubst_default_arguments PARAMS ((tree)); static tree for_each_template_parm_r PARAMS ((tree *, int *, void *)); static tree instantiate_clone PARAMS ((tree, tree)); static tree copy_default_args_to_explicit_spec_1 PARAMS ((tree, tree)); static void copy_default_args_to_explicit_spec PARAMS ((tree)); static int invalid_nontype_parm_type_p PARAMS ((tree, int)); /* Called once to initialize pt.c. */ void init_pt () { ggc_add_tree_root (&pending_templates, 1); ggc_add_tree_root (&maybe_templates, 1); ggc_add_tree_root (&saved_trees, 1); ggc_add_tree_root (¤t_tinst_level, 1); } /* Do any processing required when DECL (a member template declaration using TEMPLATE_PARAMETERS as its innermost parameter list) 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 (decl) tree decl; { if (decl == NULL_TREE || decl == void_type_node) return NULL_TREE; else if (decl == error_mark_node) /* By returning NULL_TREE, the parser will just ignore this declaration. We have already issued the error. */ return NULL_TREE; else if (TREE_CODE (decl) == TREE_LIST) { /* Assume that the class is the only declspec. */ decl = TREE_VALUE (decl); if (IS_AGGR_TYPE (decl) && CLASSTYPE_TEMPLATE_INFO (decl) && ! CLASSTYPE_TEMPLATE_SPECIALIZATION (decl)) { tree tmpl = CLASSTYPE_TI_TEMPLATE (decl); check_member_template (tmpl); return tmpl; } return NULL_TREE; } else if (TREE_CODE (decl) == FIELD_DECL) cp_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 cp_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 (type, count_specializations) 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 (type) 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 (decl) 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 (parmlist, levels) 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; pushlevel (0); 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; DECL_INITIAL (decl) = DECL_INITIAL (parm); SET_DECL_TEMPLATE_PARM_P (decl); pushdecl (decl); } break; default: my_friendly_abort (0); } } } /* Restore the template parameter context for a member template or a friend template defined in a class definition. */ void maybe_begin_member_template_processing (decl) 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 () { 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 non-zero 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 (t) 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 non-zero 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 (t) 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 (args, extra_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 (args, extra_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 (args, n) 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 () { /* 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); ++processing_template_decl; ++processing_template_parmlist; note_template_header (0); } /* This routine is called when a specialization is declared. If it is illegal to declare a specialization here, an error is reported. */ static void check_specialization_scope () { 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) cp_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) cp_error ("enclosing class templates are not explicitly specialized"); } /* We've just seen template <>. */ void begin_specialization () { begin_scope (sk_template_spec); note_template_header (1); check_specialization_scope (); } /* Called at then end of processing a declaration preceded by template<>. */ void end_specialization () { finish_scope (); reset_specialization (); } /* Any template <>'s that we have seen thus far are not referring to a function specialization. */ void reset_specialization () { processing_specialization = 0; template_header_count = 0; } /* We've just seen a template header. If SPECIALIZATION is non-zero, it was of the form template <>. */ static void note_template_header (specialization) int specialization; { processing_specialization = specialization; template_header_count++; } /* We're beginning an explicit instantiation. */ void begin_explicit_instantiation () { ++processing_explicit_instantiation; } void end_explicit_instantiation () { my_friendly_assert(processing_explicit_instantiation > 0, 0); --processing_explicit_instantiation; } /* 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 (type) tree type; { if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type)) { if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type)) { if (current_namespace != decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type))) { cp_pedwarn ("specializing `%#T' in different namespace", type); cp_pedwarn_at (" from definition of `%#D'", 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)) cp_error ("specialization of `%T' after instantiation", type); } else if (processing_specialization) cp_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 (tmpl, args) 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 (tmpl) tree tmpl; { return (tree) htab_find (local_specializations, tmpl); } /* Returns non-zero iff DECL is a specialization of TMPL. */ int is_specialization_of (decl, tmpl) 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; } /* 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 (spec, tmpl, args) 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)) { cp_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)) { duplicate_decls (spec, fn); return fn; } } } } DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); return spec; } /* Unregister the specialization SPEC as a specialization of TMPL. Returns nonzero if the SPEC was listed as a specialization of TMPL. */ static int unregister_specialization (spec, tmpl) tree spec; tree tmpl; { tree* s; for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl); *s != NULL_TREE; s = &TREE_CHAIN (*s)) if (TREE_VALUE (*s) == spec) { *s = TREE_CHAIN (*s); return 1; } return 0; } /* Like register_specialization, but for local declarations. We are registering SPEC, an instantiation of TMPL. */ static void register_local_specialization (spec, tmpl) tree spec; tree tmpl; { void **slot; slot = htab_find_slot (local_specializations, tmpl, INSERT); *slot = spec; } /* Print the list of candidate FNS in an error message. */ void print_candidates (fns) 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 non-zero 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 (template_id, decl, targs_out, need_member_template) 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 = TREE_VALUE (fns); if (!is_overloaded_fn (fns)) { cp_error ("`%D' is not a function template", fns); return error_mark_node; } for (; fns; fns = OVL_NEXT (fns)) { tree tmpl; tree fn = OVL_CURRENT (fns); if (TREE_CODE (fn) == TEMPLATE_DECL) /* DECL might be a specialization of FN. */ tmpl = fn; else if (need_member_template) /* FN is an ordinary member function, and we need a specialization of a member template. */ continue; else if (TREE_CODE (fn) != FUNCTION_DECL) /* We can get IDENTIFIER_NODEs here in certain erroneous cases. */ continue; else if (!DECL_FUNCTION_MEMBER_P (fn)) /* This is just an ordinary non-member function. Nothing can be a specialization of that. */ continue; 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 (!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); continue; } /* See whether this function might be a specialization of this template. */ targs = get_bindings (tmpl, 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, tmpl, templates); } 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 illegal programs legal, 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 (spec_types, tmpl_types) 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 (decl) 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; /* 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); } } /* 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 (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_cplus_method_type (object_type, TREE_TYPE (old_type), new_spec_types); } else new_type = build_function_type (TREE_TYPE (old_type), new_spec_types); new_type = 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 illegal; 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 (declarator, decl, template_count, flags) 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(); */ cp_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: cp_error ("template parameter list used in explicit instantiation"); /* Fall through. */ case tsk_expl_inst: if (have_def) cp_error ("definition provided for explicit instantiation"); explicit_instantiation = 1; break; case tsk_excessive_parms: cp_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) { cp_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 legal C++. */ if (pedantic) cp_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)) cp_error ("partial specialization `%D' of function template", declarator); else cp_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: my_friendly_abort (20000309); } if (specialization || member_specialization) { tree t = TYPE_ARG_TYPES (TREE_TYPE (decl)); for (; t; t = TREE_CHAIN (t)) if (TREE_PURPOSE (t)) { cp_pedwarn ("default argument specified in explicit specialization"); break; } if (current_lang_name == lang_name_c) cp_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 = IDENTIFIER_NAMESPACE_VALUE (dname); else fns = dname; 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 illegal, but the error will already have been flagged by check_specialization_scope. */ return error_mark_node; else { /* It's not legal 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 illegal) we can get here. The error will be issued later. */ ; } return decl; } else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR) { /* A friend declaration. We can't do much, because we don't know what this resolves to, yet. */ my_friendly_assert (is_friend != 0, 0); my_friendly_assert (!explicit_instantiation, 0); SET_DECL_IMPLICIT_INSTANTIATION (decl); 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 (name == constructor_name (ctype) || name == constructor_name_full (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]. */ cp_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 = 2; 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) { cp_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); } /* 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; return tmpl; } /* 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); last_function_parms = TREE_CHAIN (last_function_parms); } /* 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); /* Mangle the function name appropriately. Note that we do not mangle specializations of non-template member functions of template classes, e.g. with template struct S { void f(); } and given the specialization template <> void S::f() {} we do not mangle S::f() here. That's because it's just an ordinary member function and doesn't need special treatment. We do this here so that the ordinary, non-template, name-mangling algorithm will not be used later. */ if ((is_member_template (tmpl) || ctype == NULL_TREE) && name_mangling_version >= 1) set_mangled_name_for_template_decl (decl); 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; } /* TYPE is being declared. Verify that the use of template headers and such is reasonable. Issue error messages if not. */ void maybe_check_template_type (type) tree type; { if (template_header_count) { /* We are in the scope of some `template <...>' header. */ int context_depth = template_class_depth_real (TYPE_CONTEXT (type), /*count_specializations=*/1); if (template_header_count <= context_depth) /* This is OK; the template headers are for the context. We are actually too lenient here; like check_explicit_specialization we should consider the number of template types included in the actual declaration. For example, template struct S { template template struct I {}; }; is illegal, but: template struct S { template struct I; }; template template ::I {}; is not. */ ; else if (template_header_count > context_depth + 1) /* There are two many template parameter lists. */ cp_error ("too many template parameter lists in declaration of `%T'", type); } } /* 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 (parms1, parms2) 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 (decl) 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 (index, level, orig_level, decl, type) 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; 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 (index, type, levels) 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 decl = build_decl (TREE_CODE (TEMPLATE_PARM_DECL (index)), DECL_NAME (TEMPLATE_PARM_DECL (index)), type); tree 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; DECL_ARTIFICIAL (decl) = 1; SET_DECL_TEMPLATE_PARM_P (decl); /* 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 (list, next) tree list, 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_TREE); /* [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) = 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)); 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 (parms) tree parms; { int nparms; tree parm; 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 = TREE_CHAIN (parm), nparms++) TREE_VEC_ELT (saved_parmlist, nparms) = parm; --processing_template_parmlist; return saved_parmlist; } /* end_template_decl is called after a template declaration is seen. */ void end_template_decl () { 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 () { 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 (decl, parms) 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)) { if (CAN_HAVE_FULL_LANG_DECL_P (decl)) DECL_VIRTUAL_CONTEXT (tmpl) = DECL_VIRTUAL_CONTEXT (decl); DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_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 non-zero 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 non-zero 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 (t, data) 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 (decl) 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 illegal; 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 ((PTR) tpd.parms, 0, sizeof (int) * ntparms); tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs); memset ((PTR) 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); } 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) { cp_error ("template parameters not used in partial specialization:"); did_error_intro = 1; } cp_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))))) cp_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]) cp_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 = (int*) 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 = (int*) 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 ((PTR) tpd2.parms, 0, sizeof (int) * nargs); for_each_template_parm (type, &mark_template_parm, &tpd2); 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]) { cp_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 non-zero if DECL is the thing declared by a primary template. IS_PARTIAL is non-zero if DECL is a partial specialization. */ static void check_default_tmpl_args (decl, parms, is_primary, is_partial) 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_CONTEXT (decl) && same_type_p (DECL_CONTEXT (decl), current_class_type)) || (DECL_FRIEND_CONTEXT (decl) && same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type))) /* 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) { cp_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 argument for template parameter in function template `%D'"; else if (is_partial) msg = "default argument in partial specialization `%D'"; 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 legal 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) { cp_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'"; } } /* 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 non-zero, DECL is a friend declaration. */ tree push_template_decl_real (decl, is_friend) 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) cp_error ("template with C linkage"); else if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl))) cp_error ("template class without a name"); else if ((DECL_IMPLICIT_TYPEDEF_P (decl) && CLASS_TYPE_P (TREE_TYPE (decl))) || (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)) || TREE_CODE (decl) == FUNCTION_DECL) /* OK */; else cp_error ("template declaration of `%#D'", decl); } /* 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 || 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 legal. */ 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 { cp_error ("`%D' does not declare a template type", decl); return decl; } } else if (! DECL_TEMPLATE_INFO (decl)) { cp_error ("template definition of non-template `%#D'", decl); return decl; } else tmpl = DECL_TI_TEMPLATE (decl); if (is_member_template (tmpl) && DECL_FUNCTION_TEMPLATE_P (tmpl) && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl) && DECL_TEMPLATE_SPECIALIZATION (decl)) { 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) { cp_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) cp_error ("got %d template parameters for `%#D'", TREE_VEC_LENGTH (a), decl); else cp_error ("got %d template parameters for `%#T'", TREE_VEC_LENGTH (a), current); cp_error (" but %d required", TREE_VEC_LENGTH (t)); } /* 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; 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) 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 (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 (type, parms) tree type; tree parms; { tree tmpl; tree tmpl_parms; int i; if (!TYPE_TEMPLATE_INFO (type)) { cp_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); cp_error ("used %d template parameter%s instead of %d", TREE_VEC_LENGTH (tmpl_parms), TREE_VEC_LENGTH (tmpl_parms) == 1 ? "" : "s", 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); cp_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. */ cp_error ("redefinition of default argument for `%#D'", parm); cp_error_at (" 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; } } /* 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. By the time this function is called, neither TYPE nor EXPR may make use of template parameters. */ static tree convert_nontype_argument (type, expr) tree type; tree expr; { tree 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) expr = decl_constant_value (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_PTRMEM_P (expr_type) || TYPE_PTRMEMFUNC_P (expr_type)) { if (TREE_CODE (expr) != PTRMEM_CST) goto bad_argument; } else if (TYPE_PTR_P (expr_type) || TYPE_PTRMEM_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: cp_error ("`%E' is not a valid template argument", expr); if (TYPE_PTR_P (expr_type)) { if (TREE_CODE (TREE_TYPE (expr_type)) == FUNCTION_TYPE) cp_error ("it must be the address of a function with external linkage"); else cp_error ("it must be the address of an object with external linkage"); } else if (TYPE_PTRMEM_P (expr_type) || TYPE_PTRMEMFUNC_P (expr_type)) cp_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) { cp_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 (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)) { cp_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_PTRMEM_P (expr_type) || TYPE_PTRMEMFUNC_P (expr_type) /* The next two are g++ extensions. */ || TREE_CODE (expr_type) == REAL_TYPE || TREE_CODE (expr_type) == COMPLEX_TYPE) { if (! TREE_CONSTANT (expr)) { non_constant: cp_error ("non-constant `%E' cannot be used as template argument", expr); return NULL_TREE; } } else { cp_error ("object `%E' cannot be used as 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 REAL_TYPE: case COMPLEX_TYPE: /* These are g++ extensions. */ if (TREE_CODE (expr_type) != TREE_CODE (type)) return error_mark_node; expr = digest_init (type, expr, (tree*) 0); if (TREE_CODE (expr) != REAL_CST) goto non_constant; return expr; case POINTER_TYPE: { tree type_pointed_to = TREE_TYPE (type); if (TYPE_PTRMEM_P (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; } else 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, itf_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 underling object. */ if (TREE_CODE (expr_type) == REFERENCE_TYPE) { 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, itf_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; } mark_addressable (expr); return build1 (ADDR_EXPR, type, 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, itf_none); if (expr == error_mark_node) return error_mark_node; my_friendly_assert (same_type_p (type, TREE_TYPE (expr)), 0); return expr; } break; default: /* All non-type parameters must have one of these types. */ my_friendly_abort (0); 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