/* Handle parameterized types (templates) for GNU C++. Copyright (C) 1992, 93, 94, 95, 96, 1997 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 "defaults.h" #include "except.h" /* The type of functions taking a tree, and some additional data, and returning an int. */ typedef int (*tree_fn_t) PROTO((tree, void*)); extern struct obstack permanent_obstack; extern int lineno; extern char *input_filename; struct pending_inline *pending_template_expansions; tree current_template_parms; HOST_WIDE_INT processing_template_decl; tree pending_templates; static tree *template_tail = &pending_templates; tree maybe_templates; static tree *maybe_template_tail = &maybe_templates; int minimal_parse_mode; int processing_specialization; int processing_explicit_instantiation; static int template_header_count; static tree saved_trees; #define obstack_chunk_alloc xmalloc #define obstack_chunk_free free static int unify PROTO((tree, tree, int, tree, tree, int, int*)); static void add_pending_template PROTO((tree)); static int push_tinst_level PROTO((tree)); static tree classtype_mangled_name PROTO((tree)); static char *mangle_class_name_for_template PROTO((char *, tree, tree, tree)); static tree tsubst_expr_values PROTO((tree, tree)); static int comp_template_args PROTO((tree, tree)); static int list_eq PROTO((tree, tree)); static tree get_class_bindings PROTO((tree, tree, tree, tree)); static tree coerce_template_parms PROTO((tree, tree, tree, int, int, int)); static tree tsubst_enum PROTO((tree, tree, tree *)); static tree add_to_template_args PROTO((tree, tree)); static int type_unification_real PROTO((tree, tree, tree, tree, int, int, int, int*)); static tree complete_template_args PROTO((tree, tree, int)); static void note_template_header PROTO((int)); static tree maybe_fold_nontype_arg PROTO((tree)); static tree convert_nontype_argument PROTO((tree, tree)); static tree get_bindings_overload PROTO((tree, tree, tree)); static int for_each_template_parm PROTO((tree, tree_fn_t, void*)); /* 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 (template_parameters, decl) tree template_parameters; tree decl; { if (template_parameters) end_template_decl (); else end_specialization (); if (decl == NULL_TREE || decl == void_type_node) 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 (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. Also, both A::B and A::B have depth one. */ int template_class_depth (type) tree type; { int depth; for (depth = 0; type && TREE_CODE (type) != FUNCTION_DECL; type = TYPE_CONTEXT (type)) if (CLASSTYPE_TEMPLATE_INFO (type) && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)) && uses_template_parms (CLASSTYPE_TI_ARGS (type))) ++depth; return depth; } /* Return the original template for this decl, disregarding any specializations. */ static tree original_template (decl) tree decl; { while (DECL_TEMPLATE_INFO (decl)) decl = DECL_TI_TEMPLATE (decl); return decl; } /* 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 (list_length (DECL_TEMPLATE_PARMS (original_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 (build_int_2 (0, 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 (TREE_CODE_CLASS (TREE_CODE (parm)) == 'd', 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. */ tree decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm)); DECL_INITIAL (decl) = DECL_INITIAL (parm); 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; if (! inline_needs_template_parms (decl)) return; parms = DECL_TEMPLATE_PARMS (original_template (decl)); levels = list_length (parms) - processing_template_decl; if (DECL_TEMPLATE_SPECIALIZATION (decl)) { --levels; parms = TREE_CHAIN (parms); } push_inline_template_parms_recursive (parms, levels); } /* Undo the effects of begin_member_template_processing. */ void maybe_end_member_template_processing (decl) tree decl; { if (! processing_template_decl) return; while (current_template_parms && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)) { --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. */ int is_member_template (t) tree t; { if (TREE_CODE (t) != FUNCTION_DECL && !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 (hack_decl_function_context (t)) return 0; if ((DECL_FUNCTION_MEMBER_P (t) && !DECL_TEMPLATE_SPECIALIZATION (t)) || (TREE_CODE (t) == TEMPLATE_DECL && DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t)))) { tree tmpl; if (DECL_FUNCTION_TEMPLATE_P (t)) tmpl = t; else if (DECL_TEMPLATE_INFO (t) && DECL_FUNCTION_TEMPLATE_P (DECL_TI_TEMPLATE (t))) tmpl = DECL_TI_TEMPLATE (t); else tmpl = NULL_TREE; if (tmpl /* If there are more levels of template parameters than there are template classes surrounding the declaration, then we have a member template. */ && (list_length (DECL_TEMPLATE_PARMS (tmpl)) > template_class_depth (DECL_CLASS_CONTEXT (t)))) return 1; } return 0; } /* Return a new template argument vector which contains all of ARGS for all outer templates TYPE is contained in, but has as its innermost set of arguments the EXTRA_ARGS. If UNBOUND_ONLY, we are only interested in unbound template arguments, not arguments from enclosing templates that have been instantiated already. */ static tree complete_template_args (tmpl, extra_args, unbound_only) tree tmpl, extra_args; int unbound_only; { /* depth is the number of levels of enclosing args we're adding. */ int depth, i; tree args, new_args, spec_args = NULL_TREE; my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0); my_friendly_assert (TREE_CODE (extra_args) == TREE_VEC, 0); if (DECL_TEMPLATE_INFO (tmpl) && !unbound_only) { /* A specialization of a member template of a template class shows up as a TEMPLATE_DECL with DECL_TEMPLATE_SPECIALIZATION set. DECL_TI_ARGS is the specialization args, and DECL_TI_TEMPLATE is the template being specialized. */ if (DECL_TEMPLATE_SPECIALIZATION (tmpl)) { spec_args = DECL_TI_ARGS (tmpl); tmpl = DECL_TI_TEMPLATE (tmpl); } if (DECL_TEMPLATE_INFO (tmpl)) { /* A partial instantiation of a member template shows up as a TEMPLATE_DECL with DECL_TEMPLATE_INFO. DECL_TI_ARGS is all the bound template arguments. */ args = DECL_TI_ARGS (tmpl); if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC) depth = 1; else depth = TREE_VEC_LENGTH (args); } else /* If we are a specialization, we might have no previously bound template args. */ depth = 0; new_args = make_tree_vec (depth + 1 + (!!spec_args)); if (depth == 1) TREE_VEC_ELT (new_args, 0) = args; else for (i = 0; i < depth; ++i) TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i); } else { tree type; int skip; /* For unbound args, we have to do more work. We are getting bindings for the innermost args from extra_args, so we start from our context and work out until we've seen all the args. We need to do it this way to handle partial specialization. */ depth = list_length (DECL_TEMPLATE_PARMS (tmpl)) - 1; if (depth == 0) return extra_args; new_args = make_tree_vec (depth + 1); /* If this isn't a member template, extra_args is for the innermost template class, so skip over it. */ skip = (! is_member_template (tmpl)); type = DECL_REAL_CONTEXT (tmpl); for (i = depth; i; type = TYPE_CONTEXT (type)) if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))) { if (skip) skip = 0; else { --i; TREE_VEC_ELT (new_args, i) = CLASSTYPE_TI_ARGS (type); } } } TREE_VEC_ELT (new_args, depth) = extra_args; if (spec_args) TREE_VEC_ELT (new_args, depth + 1) = spec_args; return new_args; } /* 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; if (TREE_CODE (TREE_VEC_ELT (args, 0)) != TREE_VEC) { new_args = make_tree_vec (2); TREE_VEC_ELT (new_args, 0) = args; } else { int i; new_args = make_tree_vec (TREE_VEC_LENGTH (args) + 1); for (i = 0; i < TREE_VEC_LENGTH (args); ++i) TREE_VEC_ELT (new_args, i) = TREE_VEC_ELT (args, i); } TREE_VEC_ELT (new_args, TREE_VEC_LENGTH (new_args) - 1) = extra_args; 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 () { pushlevel (0); declare_pseudo_global_level (); ++processing_template_decl; note_template_header (0); } /* We've just seen template <>. */ void begin_specialization () { note_template_header (1); } /* Called at then end of processing a declaration preceeded by template<>. */ void end_specialization () { 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; } /* 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); 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; } /* Register the specialization SPEC as a specialization of TMPL with the indicated ARGS. */ static void 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) != TEMPLATE_DECL && list_length (DECL_TEMPLATE_PARMS (tmpl)) > 1) /* Avoid registering function declarations as specializations of member templates, as would otherwise happen with out-of-class specializations of member templates. */ return; for (s = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); s != NULL_TREE; s = TREE_CHAIN (s)) if (comp_template_args (TREE_PURPOSE (s), args)) { tree fn = TREE_VALUE (s); 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; } 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 of class type, and then specialized it later. */ TREE_VALUE (s) = spec; return; } } else if (DECL_TEMPLATE_SPECIALIZATION (fn)) { if (DECL_INITIAL (fn)) cp_error ("duplicate specialization of %D", fn); TREE_VALUE (s) = spec; return; } } } DECL_TEMPLATE_SPECIALIZATIONS (tmpl) = perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl)); } /* Print the list of candidate FNS in an error message. */ static void print_candidates (fns) tree fns; { tree fn; char* str = "candidates are:"; for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn)) { cp_error_at ("%s %+#D", str, TREE_VALUE (fn)); 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. If NEED_MEMBER_TEMPLATE is true the function is 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, unless COMPLAIN is 0. The DECL may be NULL_TREE if none is available. */ tree determine_specialization (template_id, decl, targs_out, need_member_template, complain) tree template_id; tree decl; tree* targs_out; int need_member_template; int complain; { tree fns = TREE_OPERAND (template_id, 0); tree targs_in = TREE_OPERAND (template_id, 1); tree templates = NULL_TREE; tree fn; int overloaded; int i; *targs_out = NULL_TREE; if (is_overloaded_fn (fns)) fn = get_first_fn (fns); else fn = NULL_TREE; overloaded = really_overloaded_fn (fns); for (; fn != NULL_TREE; fn = overloaded ? DECL_CHAIN (fn) : NULL_TREE) { tree tmpl; if (!need_member_template && TREE_CODE (fn) == FUNCTION_DECL && DECL_FUNCTION_MEMBER_P (fn) && DECL_USE_TEMPLATE (fn) && DECL_TI_TEMPLATE (fn)) /* We can get here when processing something like: template class X { void f(); } template <> void X::f() {} We're specializing a member function, but not a member template. */ tmpl = DECL_TI_TEMPLATE (fn); else if (TREE_CODE (fn) != TEMPLATE_DECL || (need_member_template && !is_member_template (fn))) continue; else tmpl = fn; if (list_length (targs_in) > DECL_NTPARMS (tmpl)) continue; if (decl == NULL_TREE) { tree targs = make_scratch_vec (DECL_NTPARMS (tmpl)); /* We allow incomplete unification here, because we are going to check all the functions. */ i = type_unification (DECL_INNERMOST_TEMPLATE_PARMS (tmpl), targs, NULL_TREE, NULL_TREE, targs_in, 1, 1); if (i == 0) /* Unification was successful. */ templates = scratch_tree_cons (targs, tmpl, templates); } else templates = scratch_tree_cons (NULL_TREE, tmpl, templates); } if (decl != NULL_TREE) { tree tmpl = most_specialized (templates, decl, targs_in); if (tmpl == error_mark_node) goto ambiguous; else if (tmpl == NULL_TREE) goto no_match; *targs_out = get_bindings (tmpl, decl, targs_in); return tmpl; } if (templates == NULL_TREE) { no_match: if (complain) cp_error ("`%D' does not match any template declaration", template_id); return NULL_TREE; } else if (TREE_CHAIN (templates) != NULL_TREE) { ambiguous: if (complain) { cp_error ("ambiguous template specialization `%D'", template_id); print_candidates (templates); } return NULL_TREE; } /* We have one, and exactly one, match. */ *targs_out = TREE_PURPOSE (templates); return TREE_VALUE (templates); } /* 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. FLAGS is a bitmask consisting of the following flags: 1: We are being called by finish_struct. (We are unable to determine what template is specialized by an in-class declaration until the class definition is complete, so finish_struct_methods calls this function again later to finish the job.) 2: The function has a definition. 4: The function is a friend. 8: The function is known to be a specialization of a member template. 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 finish_member = flags & 1; int have_def = flags & 2; int is_friend = flags & 4; int specialization = 0; int explicit_instantiation = 0; int member_specialization = flags & 8; tree ctype = DECL_CLASS_CONTEXT (decl); tree dname = DECL_NAME (decl); if (!finish_member) { if (processing_specialization) { /* The last template header was of the form template <>. */ if (template_header_count > template_count) { /* There were more template headers than qualifying template classes. */ if (template_header_count - template_count > 1) /* There shouldn't be that many template parameter lists. There can be at most one parameter list for every qualifying class, plus one for the function itself. */ cp_error ("too many template parameter lists in declaration of `%D'", decl); SET_DECL_TEMPLATE_SPECIALIZATION (decl); if (ctype) member_specialization = 1; else specialization = 1; } else if (template_header_count == template_count) { /* The counts are equal. So, this might be a specialization, but it is not a specialization of a member template. It might be something like template struct S { void f(int i); }; template <> void S::f(int i) {} */ specialization = 1; SET_DECL_TEMPLATE_SPECIALIZATION (decl); } else { /* This cannot be an explicit specialization. There are not enough headers for all of the qualifying classes. For example, we might have: template <> void S::T::f(); But, we're missing another template <>. */ cp_error("too few template parameter lists in declaration of `%D'", decl); return decl; } } else if (processing_explicit_instantiation) { if (template_header_count) cp_error ("template parameter list used in explicit instantiation"); if (have_def) cp_error ("definition provided for explicit instantiation"); explicit_instantiation = 1; } else if (ctype != NULL_TREE && !TYPE_BEING_DEFINED (ctype) && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype)) { /* This case catches outdated code that looks like this: template struct S { void f(); }; void S::f() {} // Missing template <> We disable this check when the type is being defined to avoid complaining about default compiler-generated constructors, destructors, and assignment operators. Since the type is an instantiation, not a specialization, these are the only functions that can be defined before the class is complete. */ /* If they said template void S::f() {} that's bogus. */ if (template_header_count) { cp_error ("template parameters specified in specialization"); return decl; } if (pedantic) cp_pedwarn ("explicit specialization not preceded by `template <>'"); specialization = 1; SET_DECL_TEMPLATE_SPECIALIZATION (decl); } else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR) { /* This case handles bogus declarations like template <> template void f(); */ cp_error ("template-id `%D' in declaration of primary template", declarator); return decl; } } 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 (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 (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; } if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype)) { /* Since finish_struct_1 has not been called yet, we can't call lookup_fnfields. We note that this template is a specialization, and proceed, letting finish_struct fix this up later. */ tree ti = perm_tree_cons (NULL_TREE, TREE_OPERAND (declarator, 1), NULL_TREE); TI_PENDING_SPECIALIZATION_FLAG (ti) = 1; DECL_TEMPLATE_INFO (decl) = ti; /* This should not be an instantiation; explicit instantiation directives can only occur at the top level. */ my_friendly_assert (!explicit_instantiation, 0); 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; 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 decl; } fns = TREE_VEC_ELT(CLASSTYPE_METHOD_VEC (ctype), is_constructor ? 0 : 1); } else fns = lookup_fnfields (TYPE_BINFO (ctype), name, 1); if (fns == NULL_TREE) { cp_error ("no member function `%s' declared in `%T'", IDENTIFIER_POINTER (name), ctype); return decl; } 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 the instantiation is for a member template, or just a member function of a template class. In particular, even in if the instantiation is for a member template, the template arguments could be deduced from the declaration. */ tmpl = determine_specialization (declarator, decl, &targs, member_specialization, 1); if (tmpl) { if (explicit_instantiation) { decl = instantiate_template (tmpl, targs); if (!DECL_TEMPLATE_SPECIALIZATION (decl)) /* There doesn't seem to be anything in the draft to prevent a specialization from being explicitly instantiated. We're careful not to destroy the information indicating that this is a specialization here. */ SET_DECL_EXPLICIT_INSTANTIATION (decl); return decl; } else if (DECL_STATIC_FUNCTION_P (tmpl) && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl)) { revert_static_member_fn (&decl, 0, 0); last_function_parms = TREE_CHAIN (last_function_parms); } /* 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. */ if ((is_member_template (tmpl) || ctype == NULL_TREE) && name_mangling_version >= 1) { tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (tmpl)); if (ctype && TREE_CODE (TREE_TYPE (tmpl)) == FUNCTION_TYPE) arg_types = hash_tree_chain (build_pointer_type (ctype), arg_types); DECL_ASSEMBLER_NAME (decl) = build_template_decl_overload (DECL_NAME (decl), arg_types, TREE_TYPE (TREE_TYPE (tmpl)), DECL_INNERMOST_TEMPLATE_PARMS (tmpl), targs, ctype != NULL_TREE); } 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); DECL_TEMPLATE_INFO (decl) = perm_tree_cons (tmpl, targs, NULL_TREE); return decl; } /* If DECL_TI_TEMPLATE (decl), the decl is an instantiation of a specialization of a member template. (In other words, there was a member template, in a class template. That member template was specialized. We then instantiated the class, so there is now an instance of that specialization.) According to the CD2, 14.7.3.13 [tmpl.expl.spec] A specialization of a member function template or member class template of a non-specialized class template is itself a template. So, we just leave the template info alone in this case. */ if (!(DECL_TEMPLATE_INFO (decl) && DECL_TI_TEMPLATE (decl))) DECL_TEMPLATE_INFO (decl) = perm_tree_cons (tmpl, targs, NULL_TREE); register_specialization (decl, tmpl, targs); return decl; } } 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 (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 (!comptypes (TREE_TYPE (parm1), TREE_TYPE (parm2), 1)) return 0; } } if ((p1 != NULL_TREE) != (p2 != NULL_TREE)) /* One set of parameters has more parameters lists than the other. */ return 0; return 1; } /* 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; /* 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) idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p)); else if (TREE_CODE (p) == TEMPLATE_DECL) idx = TEMPLATE_TYPE_IDX (TREE_TYPE (DECL_TEMPLATE_RESULT (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); /* A template parameter is not modifiable. */ TREE_READONLY (parm) = 1; if (IS_AGGR_TYPE (TREE_TYPE (parm)) && TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM) { cp_error ("`%#T' is not a valid type for a template constant parameter", TREE_TYPE (parm)); if (DECL_NAME (parm) == NULL_TREE) error (" a template type parameter must begin with `class' or `typename'"); TREE_TYPE (parm) = void_type_node; } else if (pedantic && (TREE_CODE (TREE_TYPE (parm)) == REAL_TYPE || TREE_CODE (TREE_TYPE (parm)) == COMPLEX_TYPE)) cp_pedwarn ("`%T' is not a valid type for a template constant parameter", TREE_TYPE (parm)); if (TREE_PERMANENT (parm) == 0) { parm = copy_node (parm); TREE_PERMANENT (parm) = 1; } 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_lang_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_lang_type (TEMPLATE_TYPE_PARM); /* parm is either IDENTIFIER_NODE or NULL_TREE */ decl = build_decl (TYPE_DECL, parm, t); } CLASSTYPE_GOT_SEMICOLON (t) = 1; 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)); } SET_DECL_ARTIFICIAL (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 (build_int_2 (0, 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; 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. */ poplevel (0, 0, 0); --processing_template_decl; current_template_parms = TREE_CHAIN (current_template_parms); (void) get_pending_sizes (); /* Why? */ } /* Generate a valid set of template args from current_template_parms. */ tree current_template_args () { tree header = current_template_parms; int length = list_length (header); tree args = make_tree_vec (length); int l = length; while (header) { tree a = copy_node (TREE_VALUE (header)); int i = TREE_VEC_LENGTH (a); TREE_TYPE (a) = NULL_TREE; while (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; } TREE_VEC_ELT (args, --l) = a; header = TREE_CHAIN (header); } 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)) { DECL_CLASS_CONTEXT (tmpl) = DECL_CLASS_CONTEXT (decl); DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl); } return tmpl; } struct template_parm_data { int level; int* 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; /* Return zero so that for_each_template_parm will continue the traversal of the tree; we want to mark *every* template parm. */ return 0; } /* 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. */ tree push_template_decl (decl) tree decl; { tree tmpl; tree args; tree info; tree ctx; int primary; int 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 (DECL_REAL_CONTEXT (decl)) /* In the case of a virtual function, we want the class in which it is defined. */ ctx = DECL_REAL_CONTEXT (decl); else /* Otherwise, if we're currently definining some class, the DECL is assumed to be a member of the class. */ ctx = current_class_type; /* For determining whether this is a primary template or not, we're really interested in the lexical context, not the true context. */ if (is_friend) info = DECL_CLASS_CONTEXT (decl); else info = ctx; if (info && TREE_CODE (info) == FUNCTION_DECL) primary = 0; else if (! info || (TYPE_BEING_DEFINED (info) && template_header_count && ! processing_specialization) || (template_header_count > template_class_depth (info))) primary = 1; else primary = 0; if (primary) { if (current_lang_name == lang_name_c) cp_error ("template with C linkage"); if (TREE_CODE (decl) == TYPE_DECL && ANON_AGGRNAME_P (DECL_NAME (decl))) cp_error ("template class without a name"); } /* Partial specialization. */ if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl) && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl))) { tree type = TREE_TYPE (decl); tree maintmpl = CLASSTYPE_TI_TEMPLATE (type); tree mainargs = CLASSTYPE_TI_ARGS (type); tree spec = DECL_TEMPLATE_SPECIALIZATIONS (maintmpl); /* 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. */ int i; struct template_parm_data tpd; int ntparms = TREE_VEC_LENGTH (TREE_VALUE (current_template_parms)); int did_error_intro = 0; tpd.level = TREE_INT_CST_HIGH (TREE_PURPOSE (current_template_parms)); tpd.parms = alloca (sizeof (int) * ntparms); for (i = 0; i < ntparms; ++i) tpd.parms[i] = 0; for (i = 0; i < TREE_VEC_LENGTH (mainargs); ++i) for_each_template_parm (TREE_VEC_ELT (mainargs, 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 (TREE_VALUE (current_template_parms), i))); } for (; spec; spec = TREE_CHAIN (spec)) { /* purpose: args to main template value: spec template */ if (comp_template_args (TREE_PURPOSE (spec), mainargs)) return decl; } DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type) = perm_tree_cons (mainargs, TREE_VALUE (current_template_parms), DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)); TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type; return 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); else { tmpl = build_template_decl (decl, current_template_parms); 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 t; tree a; if (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx)) cp_error ("must specialize `%#T' before defining member `%#D'", ctx, decl); if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) tmpl = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (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)) { if (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) = perm_tree_cons (tmpl, args, NULL_TREE); register_specialization (new_tmpl, tmpl, args); return decl; } a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1); t = DECL_INNERMOST_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)); if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) { cp_error ("got %d template parameters for `%#D'", TREE_VEC_LENGTH (a), decl); cp_error (" but %d required", TREE_VEC_LENGTH (t)); } if (TREE_VEC_LENGTH (args) > 1) /* Get the template parameters for the enclosing template class. */ a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 2); else a = NULL_TREE; } else a = TREE_VEC_ELT (args, TREE_VEC_LENGTH (args) - 1); t = NULL_TREE; if (CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx)) { /* When processing an inline member template of a specialized class, there is no CLASSTYPE_TI_SPEC_INFO. */ if (CLASSTYPE_TI_SPEC_INFO (ctx)) t = TREE_VALUE (CLASSTYPE_TI_SPEC_INFO (ctx)); } else if (CLASSTYPE_TEMPLATE_INFO (ctx)) t = DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (ctx)); /* There should be template arguments if and only if there is a template class. */ my_friendly_assert((a != NULL_TREE) == (t != NULL_TREE), 0); if (t != NULL_TREE && TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a)) { cp_error ("got %d template parameters for `%#D'", TREE_VEC_LENGTH (a), decl); cp_error (" but `%#T' has %d", ctx, TREE_VEC_LENGTH (t)); } } /* Get the innermost set of template arguments. */ args = innermost_args (args, 0); DECL_TEMPLATE_RESULT (tmpl) = decl; TREE_TYPE (tmpl) = TREE_TYPE (decl); if (! ctx && primary) /* The check of PRIMARY ensures 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. */ tmpl = pushdecl_top_level (tmpl); if (primary) TREE_TYPE (DECL_INNERMOST_TEMPLATE_PARMS (tmpl)) = tmpl; info = perm_tree_cons (tmpl, args, NULL_TREE); if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl)) { CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (tmpl)) = info; if (!ctx || TREE_CODE (ctx) != FUNCTION_DECL) DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl)); } else if (! DECL_LANG_SPECIFIC (decl)) cp_error ("template declaration of `%#D'", decl); else DECL_TEMPLATE_INFO (decl) = info; return DECL_TEMPLATE_RESULT (tmpl); } /* Called when a class template TYPE is redeclared, e.g.: template struct S; template struct S {}; */ void redeclare_class_template (type) tree type; { tree tmpl = CLASSTYPE_TI_TEMPLATE (type); tree tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl); tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms); int i; 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; 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); 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; } } /* 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 unsuccesful, 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. */ if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr)) 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 (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)) { cp_error ("non-constant `%E' cannot be used as template argument", expr); return NULL_TREE; } } else if (TYPE_PTR_P (expr_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 (e) != ADDR_EXPR) { bad_argument: cp_error ("`%E' is not a valid template argument", expr); error ("it must be %s%s with external linkage", TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE ? "a pointer to " : "", TREE_CODE (TREE_TYPE (TREE_TYPE (expr))) == FUNCTION_TYPE ? "a function" : "an object"); 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", referent); error ("because it is the address of an object with static linkage"); 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 (!TREE_PUBLIC (referent)) { cp_error ("address of non-extern `%E' cannot be used as template argument", referent); return error_mark_node; } } else if (TREE_CODE (expr) == VAR_DECL) { if (!TREE_PUBLIC (expr)) goto bad_argument; } 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. */ return digest_init (type, expr, (tree*) 0); case REAL_TYPE: case COMPLEX_TYPE: /* These are g++ extensions. */ if (TREE_CODE (expr_type) != TREE_CODE (type)) return error_mark_node; return digest_init (type, expr, (tree*) 0); case POINTER_TYPE: { tree type_pointed_to = TREE_TYPE (type); if (TYPE_PTRMEM_P (type)) /* For a non-type template-parameter of type pointer to data member, qualification conversions (_conv.qual_) are applied. */ return perform_qualification_conversions (type, expr); 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, 0); if (fn == error_mark_node) return error_mark_node; if (!TREE_PUBLIC (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 (comptypes (type, TREE_TYPE (expr), 1), 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 (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 fns = expr; tree fn; fn = instantiate_type (type_referred_to, fns, 0); if (!TREE_PUBLIC (fn)) { if (really_overloaded_fn (fns)) /* 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; } if (fn == error_mark_node) return error_mark_node; my_friendly_assert (comptypes (type, TREE_TYPE (fn), 1), 0); return 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 (!comptypes (TYPE_MAIN_VARIANT (expr_type), TYPE_MAIN_VARIANT (type), 1) || (TYPE_READONLY (expr_type) > TYPE_READONLY (type_referred_to)) || (TYPE_VOLATILE (expr_type) > TYPE_VOLATILE (type_referred_to)) || !real_lvalue_p (expr)) return error_mark_node; else return expr; } } break; case RECORD_TYPE: { tree fns; tree fn; my_friendly_assert (TYPE_PTRMEMFUNC_P (type), 0); /* 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) == CONSTRUCTOR) { /* A ptr-to-member constant. */ if (!comptypes (type, expr_type, 1)) return error_mark_node; else return expr; } if (TREE_CODE (expr) != ADDR_EXPR) return error_mark_node; fns = TREE_OPERAND (expr, 0); fn = instantiate_type (TREE_TYPE (TREE_TYPE (type)), fns, 0); if (fn == error_mark_node) return error_mark_node; expr = build_unary_op (ADDR_EXPR, fn, 0); my_friendly_assert (comptypes (type, TREE_TYPE (expr), 1), 0); return expr; } break; default: /* All non-type parameters must have one of these types. */ my_friendly_abort (0); break; } return error_mark_node; } /* Convert all template arguments to their appropriate types, and return a vector containing the resulting values. If any error occurs, return error_mark_node, and, if COMPLAIN is non-zero, issue an error message. Some error messages are issued even if COMPLAIN is zero; for instance, if a template argument is composed from a local class. If REQUIRE_ALL_ARGUMENTS is non-zero, all arguments must be provided in ARGLIST, or else trailing parameters must have default values. If REQUIRE_ALL_ARGUMENTS is zero, we will attempt argument deduction for any unspecified trailing arguments. If IS_TMPL_PARM is non-zero, we will coercing parameters of template template arguments. In this case, ARGLIST is a chain of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL or PARM_DECL. */ static tree coerce_template_parms (parms, arglist, in_decl, complain, require_all_arguments, is_tmpl_parm) tree parms, arglist; tree in_decl; int complain; int require_all_arguments; int is_tmpl_parm; { int nparms, nargs, i, lost = 0; tree vec = NULL_TREE; if (arglist == NULL_TREE) nargs = 0; else if (TREE_CODE (arglist) == TREE_VEC) nargs = TREE_VEC_LENGTH (arglist); else nargs = list_length (arglist); nparms = TREE_VEC_LENGTH (parms); if (nargs > nparms || (nargs < nparms && require_all_arguments && TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE)) { if (complain) { error ("incorrect number of parameters (%d, should be %d)", nargs, nparms); if (in_decl) cp_error_at ("in template expansion for decl `%D'", in_decl); } return error_mark_node; } if (arglist && TREE_CODE (arglist) == TREE_VEC && nargs == nparms) vec = copy_node (arglist); else { vec = make_tree_vec (nparms); for (i = 0; i < nparms; i++) { tree arg; tree parm = TREE_VEC_ELT (parms, i); if (arglist) { arg = arglist; arglist = TREE_CHAIN (arglist); if (arg == error_mark_node) lost++; else arg = TREE_VALUE (arg); } else if (is_tmpl_parm && i < nargs) { arg = TREE_VEC_ELT (arglist, i); if (arg == error_mark_node) lost++; } else if (TREE_PURPOSE (parm) == NULL_TREE) { my_friendly_assert (!require_all_arguments, 0); break; } else if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL) arg = tsubst (TREE_PURPOSE (parm), vec, in_decl); else arg = tsubst_expr (TREE_PURPOSE (parm), vec, in_decl); TREE_VEC_ELT (vec, i) = arg; } } for (i = 0; i < nparms; i++) { tree arg = TREE_VEC_ELT (vec, i); tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i)); tree val = 0; int is_type, requires_type, is_tmpl_type, requires_tmpl_type; if (is_tmpl_parm && i < nargs) { /* In case we are checking arguments inside a template template parameter, ARG that does not come from default argument is also a TREE_LIST node. Note that ARG can also be a TREE_LIST in other cases such as overloaded functions. */ if (arg != NULL_TREE && arg != error_mark_node) arg = TREE_VALUE (arg); } if (arg == NULL_TREE) /* We're out of arguments. */ { my_friendly_assert (!require_all_arguments, 0); break; } if (arg == error_mark_node) { cp_error ("template argument %d is invalid", i + 1); lost++; continue; } if (TREE_CODE (arg) == TREE_LIST && TREE_TYPE (arg) != NULL_TREE && TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE) { /* The template argument was the name of some member function. That's usually illegal, but static members are OK. In any case, grab the underlying fields/functions and issue an error later if required. */ arg = TREE_VALUE (arg); TREE_TYPE (arg) = unknown_type_node; } requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL; requires_type = TREE_CODE (parm) == TYPE_DECL || requires_tmpl_type; /* Check if it is a class template. If REQUIRES_TMPL_TYPE is true, we also accept implicitly created TYPE_DECL as a valid argument. */ is_tmpl_type = (TREE_CODE (arg) == TEMPLATE_DECL && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL) || (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM && !CLASSTYPE_TEMPLATE_INFO (arg)) || (TREE_CODE (arg) == RECORD_TYPE && CLASSTYPE_TEMPLATE_INFO (arg) && TREE_CODE (TYPE_NAME (arg)) == TYPE_DECL && DECL_ARTIFICIAL (TYPE_NAME (arg)) && requires_tmpl_type); if (is_tmpl_type && TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM) arg = TYPE_STUB_DECL (arg); else if (is_tmpl_type && TREE_CODE (arg) == RECORD_TYPE) arg = CLASSTYPE_TI_TEMPLATE (arg); if (is_tmpl_parm && i < nargs) is_type = TREE_CODE (arg) == TYPE_DECL || is_tmpl_type; else is_type = TREE_CODE_CLASS (TREE_CODE (arg)) == 't' || is_tmpl_type; if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM) { cp_pedwarn ("to refer to a type member of a template parameter,"); cp_pedwarn (" use `typename %E'", arg); arg = make_typename_type (TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1)); is_type = 1; } if (is_type != requires_type) { if (in_decl) { if (complain) { cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", i + 1, in_decl); if (is_type) cp_error (" expected a constant of type `%T', got `%T'", TREE_TYPE (parm), (is_tmpl_type ? DECL_NAME (arg) : arg)); else cp_error (" expected a type, got `%E'", arg); } } lost++; TREE_VEC_ELT (vec, i) = error_mark_node; continue; } if (is_tmpl_type ^ requires_tmpl_type) { if (in_decl) { cp_error ("type/value mismatch at argument %d in template parameter list for `%D'", i + 1, in_decl); if (is_tmpl_type) cp_error (" expected a type, got `%T'", DECL_NAME (arg)); else cp_error (" expected a class template, got `%T'", arg); } lost++; TREE_VEC_ELT (vec, i) = error_mark_node; continue; } if (is_tmpl_parm) { if (requires_tmpl_type) { cp_error ("nested template template parameter not implemented"); lost++; TREE_VEC_ELT (vec, i) = error_mark_node; } continue; } if (is_type) { if (requires_tmpl_type) { tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm); tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg); /* The parameter and argument roles have to be switched here in order to handle default arguments properly. For example, template