#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"
#include "toplev.h"
#include "rtl.h"
#include "varray.h"
#ifdef OBJCPLUS
#include "objc-act.h"
#endif
typedef int (*tree_fn_t) PROTO((tree, void*));
extern struct obstack permanent_obstack;
extern int lineno;
extern char *input_filename;
tree current_template_parms;
HOST_WIDE_INT processing_template_decl;
static tree pending_templates;
static tree *template_tail = &pending_templates;
static tree maybe_templates;
static tree *maybe_template_tail = &maybe_templates;
int minimal_parse_mode;
int processing_specialization;
int processing_explicit_instantiation;
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;
#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 GTB_VIA_VIRTUAL 1
#define GTB_IGNORE_TYPE 2
static int resolve_overloaded_unification PROTO((tree, tree, tree, tree,
unification_kind_t, int));
static int try_one_overload PROTO((tree, tree, tree, tree, tree,
unification_kind_t, int));
static int unify PROTO((tree, tree, tree, tree, 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));
static tree tsubst_expr_values PROTO((tree, tree));
static int list_eq PROTO((tree, tree));
static tree get_class_bindings PROTO((tree, tree, tree));
static tree coerce_template_parms PROTO((tree, tree, tree, int, int));
static void tsubst_enum PROTO((tree, tree, tree));
static tree add_to_template_args PROTO((tree, tree));
static tree add_outermost_template_args PROTO((tree, tree));
static void maybe_adjust_types_for_deduction PROTO((unification_kind_t, tree*,
tree*));
static int type_unification_real PROTO((tree, tree, tree, tree,
int, unification_kind_t, 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 convert_template_argument PROTO ((tree, tree, tree, int,
int , tree));
static tree get_bindings_overload PROTO((tree, tree, tree));
static int for_each_template_parm PROTO((tree, tree_fn_t, void*));
static tree build_template_parm_index PROTO((int, int, int, tree, tree));
static int inline_needs_template_parms PROTO((tree));
static void push_inline_template_parms_recursive PROTO((tree, int));
static tree retrieve_specialization PROTO((tree, tree));
static tree register_specialization PROTO((tree, tree, tree));
static int unregister_specialization PROTO((tree, tree));
static tree reduce_template_parm_level PROTO((tree, tree, int));
static tree build_template_decl PROTO((tree, tree));
static int mark_template_parm PROTO((tree, void *));
static tree tsubst_friend_function PROTO((tree, tree));
static tree tsubst_friend_class PROTO((tree, tree));
static tree get_bindings_real PROTO((tree, tree, tree, int));
static int template_decl_level PROTO((tree));
static tree maybe_get_template_decl_from_type_decl PROTO((tree));
static int check_cv_quals_for_unify PROTO((int, tree, tree));
static tree tsubst_template_arg_vector PROTO((tree, tree, int));
static tree tsubst_template_parms PROTO((tree, tree, int));
static void regenerate_decl_from_template PROTO((tree, tree));
static tree most_specialized PROTO((tree, tree, tree));
static tree most_specialized_class PROTO((tree, tree));
static tree most_general_template PROTO((tree));
static void set_mangled_name_for_template_decl PROTO((tree));
static int template_class_depth_real PROTO((tree, int));
static tree tsubst_aggr_type PROTO((tree, tree, int, tree, int));
static tree tsubst_decl PROTO((tree, tree, tree, tree));
static tree tsubst_arg_types PROTO((tree, tree, int, tree));
static tree tsubst_function_type PROTO((tree, tree, int, tree));
static void check_specialization_scope PROTO((void));
static tree process_partial_specialization PROTO((tree));
static void set_current_access_from_decl PROTO((tree));
static void check_default_tmpl_args PROTO((tree, tree, int, int));
static tree tsubst_call_declarator_parms PROTO((tree, tree, int, tree));
static tree get_template_base_recursive PROTO((tree, tree,
tree, tree, tree, int));
static tree get_template_base PROTO((tree, tree, tree, tree));
static tree try_class_unification PROTO((tree, tree, tree, tree));
static int coerce_template_template_parms PROTO((tree, tree, int,
tree, tree));
static tree determine_specialization PROTO((tree, tree, tree *, int));
static int template_args_equal PROTO((tree, tree));
static void print_template_context PROTO((int));
#define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE) \
(NODE != NULL_TREE \
&& TREE_CODE (NODE) == TREE_VEC \
&& TREE_VEC_LENGTH (NODE) > 0 \
&& TREE_VEC_ELT (NODE, 0) != NULL_TREE \
&& TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
#define TMPL_ARGS_DEPTH(NODE) \
(TMPL_ARGS_HAVE_MULTIPLE_LEVELS (NODE) ? TREE_VEC_LENGTH (NODE) : 1)
#define TMPL_ARGS_LEVEL(ARGS, LEVEL) \
(TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS) \
? TREE_VEC_ELT ((ARGS), (LEVEL) - 1) : ARGS)
#define SET_TMPL_ARGS_LEVEL(ARGS, LEVEL, VAL) \
(TREE_VEC_ELT ((ARGS), (LEVEL) - 1) = (VAL))
#define TMPL_ARG(ARGS, LEVEL, IDX) \
(TREE_VEC_ELT (TMPL_ARGS_LEVEL (ARGS, LEVEL), IDX))
#define SET_TMPL_ARG(ARGS, LEVEL, IDX, VAL) \
(TREE_VEC_ELT (TREE_VEC_ELT ((ARGS), (LEVEL) - 1), (IDX)) = (VAL))
#define NUM_TMPL_ARGS(NODE) \
((NODE) == NULL_TREE ? 0 \
: (TREE_CODE (NODE) == TREE_VEC \
? TREE_VEC_LENGTH (NODE) : list_length (NODE)))
#define TMPL_PARMS_DEPTH(NODE) \
(TREE_INT_CST_HIGH (TREE_PURPOSE (NODE)))
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)
return NULL_TREE;
else if (TREE_CODE (decl) == TREE_LIST)
{
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;
}
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)
? DECL_REAL_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;
}
int
template_class_depth (type)
tree type;
{
return template_class_depth_real (type, 0);
}
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)));
}
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:
{
tree decl = build_decl (CONST_DECL, DECL_NAME (parm),
TREE_TYPE (parm));
SET_DECL_ARTIFICIAL (decl);
DECL_INITIAL (decl) = DECL_INITIAL (parm);
DECL_TEMPLATE_PARM_P (decl) = 1;
pushdecl (decl);
}
break;
default:
my_friendly_abort (0);
}
}
}
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);
}
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;
}
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);
}
}
int
is_member_template (t)
tree t;
{
if (!DECL_FUNCTION_TEMPLATE_P (t))
return 0;
if (hack_decl_function_context (t))
return 0;
return (DECL_FUNCTION_MEMBER_P (DECL_TEMPLATE_RESULT (t))
&& (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
template_class_depth (DECL_CLASS_CONTEXT (t))));
}
#if 0
int
is_member_template_class (t)
tree t;
{
if (!DECL_CLASS_TEMPLATE_P (t))
return 0;
if (!DECL_CLASS_SCOPE_P (t))
return 0;
return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (t)) >
template_class_depth (DECL_CONTEXT (t)));
}
#endif
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_temp_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;
}
static tree
add_outermost_template_args (args, extra_args)
tree args;
tree extra_args;
{
tree new_args;
my_friendly_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args),
0);
if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
return extra_args;
TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
new_args = add_to_template_args (args, extra_args);
TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
return new_args;
}
void
begin_template_parm_list ()
{
pushlevel (0);
declare_pseudo_global_level ();
++processing_template_decl;
++processing_template_parmlist;
note_template_header (0);
}
static void
check_specialization_scope ()
{
tree scope = current_scope ();
if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
cp_error ("explicit specialization in non-namespace scope `%D'",
scope);
if (current_template_parms)
cp_error ("enclosing class templates are not explicitly specialized");
}
void
begin_specialization ()
{
note_template_header (1);
check_specialization_scope ();
}
void
end_specialization ()
{
reset_specialization ();
}
void
reset_specialization ()
{
processing_specialization = 0;
template_header_count = 0;
}
static void
note_template_header (specialization)
int specialization;
{
processing_specialization = specialization;
template_header_count++;
}
void
begin_explicit_instantiation ()
{
++processing_explicit_instantiation;
}
void
end_explicit_instantiation ()
{
my_friendly_assert(processing_explicit_instantiation > 0, 0);
--processing_explicit_instantiation;
}
void
maybe_process_partial_specialization (type)
tree type;
{
if (IS_AGGR_TYPE (type) && CLASSTYPE_USE_TEMPLATE (type))
{
if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
&& TYPE_SIZE (type) == NULL_TREE)
{
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);
}
static tree
retrieve_specialization (tmpl, args)
tree tmpl;
tree args;
{
tree s;
my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 0);
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;
}
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_p (TYPE_MAIN_VARIANT (t),
TYPE_MAIN_VARIANT (TREE_TYPE (tmpl))))
return 1;
}
return 0;
}
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)))
return spec;
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))
{
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 spec;
}
else
{
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)
= perm_tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
return spec;
}
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;
}
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 = " ";
}
}
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 fn;
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;
if (TREE_CODE (fns) == TREE_LIST)
fns = TREE_VALUE (fns);
for (; fns; fns = OVL_NEXT (fns))
{
tree tmpl;
fn = OVL_CURRENT (fns);
if (TREE_CODE (fn) == TEMPLATE_DECL)
tmpl = fn;
else if (need_member_template)
continue;
else if (TREE_CODE (fn) != FUNCTION_DECL)
continue;
else if (!DECL_FUNCTION_MEMBER_P (fn))
continue;
else
{
tree decl_arg_types;
if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
TREE_TYPE (TREE_TYPE (fn))))
continue;
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))
candidates = tree_cons (NULL_TREE, fn, candidates);
continue;
}
targs = get_bindings (tmpl, decl, explicit_targs);
if (!targs)
continue;
templates = scratch_tree_cons (targs, tmpl, templates);
}
if (templates && TREE_CHAIN (templates))
{
tree tmpl = most_specialized (templates, decl, explicit_targs);
if (tmpl && tmpl != error_mark_node)
{
targs = get_bindings (tmpl, decl, explicit_targs);
templates = scratch_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;
}
if (candidates)
{
*targs_out = copy_node (DECL_TI_ARGS (TREE_VALUE (candidates)));
return DECL_TI_TEMPLATE (TREE_VALUE (candidates));
}
targs = DECL_TI_ARGS (DECL_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);
}
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);
if (processing_specialization)
{
if (template_header_count > template_count)
{
if (template_header_count - template_count > 1)
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)
{
specialization = 1;
SET_DECL_TEMPLATE_SPECIALIZATION (decl);
}
else
{
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)
&& !is_friend)
{
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)
{
if (is_friend)
specialization = 1;
else
{
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 (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;
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)
return error_mark_node;
else
{
;
}
return decl;
}
else if (TREE_CODE (TREE_OPERAND (declarator, 0)) == LOOKUP_EXPR)
{
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))
{
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))
{
cp_error ("specialization of implicitly-declared special member function");
return error_mark_node;
}
name = is_constructor ? ctor_identifier : dtor_identifier;
}
if (!IDENTIFIER_TYPENAME_P (name))
{
idx = lookup_fnfields_1 (ctype, name);
if (idx >= 0)
fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (ctype), idx);
}
else
{
tree methods;
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)))
break;
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;
}
tmpl = determine_specialization (declarator, decl,
&targs,
member_specialization);
if (!tmpl || tmpl == error_mark_node)
return error_mark_node;
else
{
tree gen_tmpl = most_general_template (tmpl);
if (explicit_instantiation)
{
int arg_depth = TMPL_ARGS_DEPTH (targs);
int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
if (arg_depth > parm_depth)
{
int i;
tree new_targs;
new_targs = make_temp_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;
}
decl = instantiate_template (tmpl, targs);
return decl;
}
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);
}
DECL_TEMPLATE_INFO (decl)
= perm_tree_cons (tmpl, targs, NULL_TREE);
if ((is_member_template (tmpl) || ctype == NULL_TREE)
&& name_mangling_version >= 1)
set_mangled_name_for_template_decl (decl);
if (is_friend && !have_def)
SET_DECL_IMPLICIT_INSTANTIATION (decl);
decl = register_specialization (decl, gen_tmpl, targs);
}
}
return decl;
}
void
maybe_check_template_type (type)
tree type;
{
if (template_header_count)
{
int context_depth
= template_class_depth_real (TYPE_CONTEXT (type),
1);
if (template_header_count <= context_depth)
;
else if (template_header_count > context_depth + 1)
cp_error ("too many template parameter lists in declaration of `%T'", type);
}
}
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))
return 0;
return 1;
}
void
check_template_shadow (decl)
tree decl;
{
tree olddecl;
if (!current_template_parms)
return;
if (TREE_CODE (decl) == OVERLOAD)
decl = OVL_CURRENT (decl);
olddecl = IDENTIFIER_VALUE (DECL_NAME (decl));
if (!olddecl)
return;
if (TREE_CODE_CLASS (TREE_CODE (olddecl)) != 'd'
|| !DECL_TEMPLATE_PARM_P (olddecl))
return;
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);
}
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;
}
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_TEMPLATE_PARMS (decl)
= DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
}
return TEMPLATE_PARM_DESCENDANTS (index);
}
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);
parm = grokdeclarator (TREE_VALUE (parm), TREE_PURPOSE (parm),
PARM, 0, NULL_TREE);
TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
TREE_READONLY (parm) = 1;
if (IS_AGGR_TYPE (TREE_TYPE (parm))
&& TREE_CODE (TREE_TYPE (parm)) != TEMPLATE_TYPE_PARM
&& TREE_CODE (TREE_TYPE (parm)) != TYPENAME_TYPE)
{
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);
TREE_TYPE (parm) = t;
TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
decl = parm;
}
else
{
t = make_lang_type (TEMPLATE_TYPE_PARM);
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));
}
SET_DECL_ARTIFICIAL (decl);
DECL_TEMPLATE_PARM_P (decl) = 1;
pushdecl (decl);
parm = build_tree_list (defval, parm);
return chainon (list, parm);
}
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;
--processing_template_parmlist;
return saved_parmlist;
}
void
end_template_decl ()
{
reset_specialization ();
if (! processing_template_decl)
return;
poplevel (0, 0, 0);
--processing_template_decl;
current_template_parms = TREE_CHAIN (current_template_parms);
(void) get_pending_sizes ();
}
tree
current_template_args ()
{
tree header;
tree args = NULL_TREE;
int length = TMPL_PARMS_DEPTH (current_template_parms);
int l = length;
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);
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;
}
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);
DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
}
return tmpl;
}
struct template_parm_data
{
int level;
int current_arg;
int* parms;
int* arg_uses_template_parms;
};
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 0;
}
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_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;
tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
tpd.parms = alloca (sizeof (int) * ntparms);
bzero ((PTR) tpd.parms, sizeof (int) * ntparms);
tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
bzero ((PTR) tpd.arg_uses_template_parms, 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)
{
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)));
}
if (comp_template_args (inner_args,
innermost_args (CLASSTYPE_TI_ARGS (TREE_TYPE
(maintmpl)))))
cp_error ("partial specialization `%T' does not specialize any template arguments", type);
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 (
TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
&& TREE_CODE (arg) != TEMPLATE_DECL
&& TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
{
if (tpd.arg_uses_template_parms[i])
cp_error ("template argument `%E' involves template parameter(s)", arg);
else
{
tree type =
TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (main_inner_parms,
i)));
if (!tpd2.parms)
{
tpd2.arg_uses_template_parms
= (int*) alloca (sizeof (int) * nargs);
tpd2.parms = (int*) alloca (sizeof (int) * nargs);
tpd2.level =
TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
}
tpd2.current_arg = i;
tpd2.arg_uses_template_parms[i] = 0;
bzero ((PTR) tpd2.parms, sizeof (int) * nargs);
for_each_template_parm (type,
&mark_template_parm,
&tpd2);
if (tpd2.arg_uses_template_parms [i])
{
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))
return decl;
DECL_TEMPLATE_SPECIALIZATIONS (maintmpl) = CLASSTYPE_TI_SPEC_INFO (type)
= perm_tree_cons (inner_args, inner_parms,
DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
return decl;
}
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;
if (current_class_type
&& !TYPE_BEING_DEFINED (current_class_type)
&& DECL_LANG_SPECIFIC (decl)
&& DECL_CLASS_CONTEXT (decl) == current_class_type
&& (!DECL_FUNCTION_MEMBER_P (decl) || DECL_DEFINED_IN_CLASS_P (decl)))
return;
if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
;
else
parms = TREE_CHAIN (parms);
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))
last_level_to_check = template_class_depth (current_class_type) + 1;
else
last_level_to_check = 0;
for (; parms && TMPL_PARMS_DEPTH (parms) >= last_level_to_check;
parms = TREE_CHAIN (parms))
{
tree inner_parms = TREE_VALUE (parms);
int i, 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;
}
TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
}
if (msg)
msg = "default argument for template parameter for class enclosing `%D'";
}
}
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;
is_partial = (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (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)
ctx = DECL_CONTEXT (decl);
else if (DECL_REAL_CONTEXT (decl)
&& TREE_CODE (DECL_REAL_CONTEXT (decl)) != NAMESPACE_DECL)
ctx = DECL_REAL_CONTEXT (decl);
else
ctx = current_class_type;
if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
ctx = NULL_TREE;
if (!DECL_CONTEXT (decl))
DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
if (is_friend)
info = current_class_type;
else
info = ctx;
if (info && TREE_CODE (info) == FUNCTION_DECL)
primary = 0;
else if (processing_template_decl > 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");
if (TREE_CODE (decl) == TYPE_DECL
&& TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
cp_error ("template declaration of `%#T'", TREE_TYPE (decl));
}
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);
else
{
tmpl = build_template_decl (decl, current_template_parms);
if (DECL_LANG_SPECIFIC (decl)
&& DECL_TEMPLATE_SPECIALIZATION (decl))
{
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 (CLASSTYPE_TEMPLATE_INSTANTIATION (ctx))
cp_error ("must specialize `%#T' before defining member `%#D'",
ctx, decl);
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;
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;
}
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));
}
if (current == decl)
current = ctx;
else
current = TYPE_CONTEXT (current);
}
}
DECL_TEMPLATE_RESULT (tmpl) = decl;
TREE_TYPE (tmpl) = TREE_TYPE (decl);
if (! ctx
&& !(is_friend && template_class_depth (current_class_type) > 0))
tmpl = pushdecl_namespace_level (tmpl);
if (primary)
DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
info = perm_tree_cons (tmpl, args, NULL_TREE);
if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (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))
cp_error ("template declaration of `%#D'", decl);
else
DECL_TEMPLATE_INFO (decl) = info;
return DECL_TEMPLATE_RESULT (tmpl);
}
tree
push_template_decl (decl)
tree decl;
{
return push_template_decl_real (decl, 0);
}
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))
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)
{
cp_error ("redefinition of default argument for `%#D'", parm);
cp_error_at (" original definition appeared here", tmpl_parm);
return;
}
if (parm_default != NULL_TREE)
TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
}
}
static tree
convert_nontype_argument (type, expr)
tree type;
tree expr;
{
tree expr_type = TREE_TYPE (expr);
if (INTEGRAL_TYPE_P (expr_type) && TREE_READONLY_DECL_P (expr))
expr = decl_constant_value (expr);
if (is_overloaded_fn (expr))
;
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
|| expr_type == unknown_type_node)
{
tree referent;
tree e = expr;
STRIP_NOPS (e);
if (TREE_CODE (type) == REFERENCE_TYPE
|| TREE_CODE (expr_type) == ARRAY_TYPE)
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",
referent);
error ("because it is the address of an object with static linkage");
return NULL_TREE;
}
if (is_overloaded_fn (referent))
;
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 (INTEGRAL_TYPE_P (expr_type)
|| TYPE_PTRMEM_P (expr_type)
|| TYPE_PTRMEMFUNC_P (expr_type)
|| 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:
if (!INTEGRAL_TYPE_P (expr_type))
return error_mark_node;
expr = digest_init (type, expr, (tree*) 0);
if (TREE_CODE (expr) != INTEGER_CST)
goto non_constant;
return expr;
case REAL_TYPE:
case COMPLEX_TYPE:
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;
e = perform_qualification_conversions (type, expr);
if (TREE_CODE (e) == NOP_EXPR)
e = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
return e;
}
else if (TREE_CODE (type_pointed_to) == FUNCTION_TYPE)
{
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 (same_type_p (type, TREE_TYPE (expr)),
0);
return expr;
}
else
{
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)
{
tree fns = expr;
tree fn;
fn = instantiate_type (type_referred_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;
}
my_friendly_assert (same_type_p (type_referred_to,
TREE_TYPE (fn)),
0);
return fn;
}
else
{
if ((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;
else
return expr;
}
}
break;
case RECORD_TYPE:
{
if (!TYPE_PTRMEMFUNC_P (type))
return error_mark_node;
if (!TYPE_PTRMEMFUNC_P (expr_type) &&
expr_type != unknown_type_node)
return error_mark_node;
if (TREE_CODE (expr) == PTRMEM_CST)
{
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, 0);
if (expr == error_mark_node)
return error_mark_node;
my_friendly_assert (same_type_p (type, TREE_TYPE (expr)),
0);
return expr;
}
break;
default:
my_friendly_abort (0);
break;
}
return error_mark_node;
}
static int
coerce_template_template_parms (parm_parms, arg_parms, complain,
in_decl, outer_args)
tree parm_parms, arg_parms;
int complain;
tree in_decl, outer_args;
{
int nparms, nargs, i;
tree parm, arg;
my_friendly_assert (TREE_CODE (parm_parms) == TREE_VEC, 0);
my_friendly_assert (TREE_CODE (arg_parms) == TREE_VEC, 0);
nparms = TREE_VEC_LENGTH (parm_parms);
nargs = TREE_VEC_LENGTH (arg_parms);
if (nargs < nparms
|| (nargs > nparms
&& TREE_PURPOSE (TREE_VEC_ELT (arg_parms, nparms)) == NULL_TREE))
return 0;
for (i = 0; i < nparms; ++i)
{
parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
if (arg == NULL_TREE || arg == error_mark_node
|| parm == NULL_TREE || parm == error_mark_node)
return 0;
if (TREE_CODE (arg) != TREE_CODE (parm))
return 0;
switch (TREE_CODE (parm))
{
case TYPE_DECL:
break;
case TEMPLATE_DECL:
{
tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
if (!coerce_template_template_parms (parmparm, argparm,
complain, in_decl,
outer_args))
return 0;
}
break;
case PARM_DECL:
if (!same_type_p (tsubst (TREE_TYPE (parm), outer_args,
complain, in_decl),
TREE_TYPE (arg)))
return 0;
break;
default:
my_friendly_abort (0);
}
}
return 1;
}
static tree
convert_template_argument (parm, arg, args, complain, i, in_decl)
tree parm;
tree arg;
tree args;
int complain;
int i;
tree in_decl;
{
tree val;
tree inner_args;
int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
inner_args = innermost_args (args);
if (TREE_CODE (arg) == TREE_LIST
&& TREE_TYPE (arg) != NULL_TREE
&& TREE_CODE (TREE_TYPE (arg)) == OFFSET_TYPE)
{
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);
is_tmpl_type
= ((TREE_CODE (arg) == TEMPLATE_DECL
&& TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
|| (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
&& !TEMPLATE_TEMPLATE_PARM_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
&& is_base_of_enclosing_class (arg, current_class_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);
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);
}
}
return error_mark_node;
}
if (is_tmpl_type ^ requires_tmpl_type)
{
if (in_decl && complain)
{
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);
}
return error_mark_node;
}
if (is_type)
{
if (requires_tmpl_type)
{
tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
if (coerce_template_template_parms (parmparm, argparm, complain,
in_decl, inner_args))
{
val = arg;
if (val != error_mark_node
&& DECL_TEMPLATE_TEMPLATE_PARM_P (val))
val = TREE_TYPE (val);
}
else
{
if (in_decl && complain)
{
cp_error ("type/value mismatch at argument %d in template parameter list for `%D'",
i + 1, in_decl);
cp_error (" expected a template of type `%D', got `%D'", parm, arg);
}
val = error_mark_node;
}
}
else
{
val = groktypename (arg);
if (! processing_template_decl)
{
tree t = no_linkage_check (val);
if (t)
{
if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
cp_pedwarn
("template-argument `%T' uses anonymous type", val);
else
cp_error
("template-argument `%T' uses local type `%T'",
val, t);
return error_mark_node;
}
}
}
}
else
{
tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
if (processing_template_decl)
arg = maybe_fold_nontype_arg (arg);
if (!uses_template_parms (arg) && !uses_template_parms (t))
val = convert_nontype_argument (t, arg);
else
val = arg;
if (val == NULL_TREE)
val = error_mark_node;
else if (val == error_mark_node && complain)
cp_error ("could not convert template argument `%E' to `%T'",
arg, t);
}
return val;
}
static tree
coerce_template_parms (parms, args, in_decl,
complain,
require_all_arguments)
tree parms, args;
tree in_decl;
int complain;
int require_all_arguments;
{
int nparms, nargs, i, lost = 0;
tree inner_args;
tree new_args;
tree new_inner_args;
inner_args = innermost_args (args);
nargs = NUM_TMPL_ARGS (inner_args);
nparms = TREE_VEC_LENGTH (parms);
if (nargs > nparms
|| (nargs < nparms
&& require_all_arguments
&& TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
{
if (complain)
{
cp_error ("wrong number of template arguments (%d, should be %d)",
nargs, nparms);
if (in_decl)
cp_error_at ("provided for `%D'", in_decl);
}
return error_mark_node;
}
new_inner_args = make_temp_vec (nparms);
new_args = add_outermost_template_args (args, new_inner_args);
for (i = 0; i < nparms; i++)
{
tree arg;
tree parm;
parm = TREE_VEC_ELT (parms, i);
if (inner_args && TREE_CODE (inner_args) == TREE_LIST)
{
arg = TREE_VALUE (inner_args);
inner_args = TREE_CHAIN (inner_args);
}
else if (i < nargs)
arg = TREE_VEC_ELT (inner_args, i);
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), new_args, complain, in_decl);
else
arg = tsubst_expr (TREE_PURPOSE (parm), new_args, complain,
in_decl);
if (arg == NULL_TREE)
{
my_friendly_assert (!require_all_arguments, 0);
break;
}
else if (arg == error_mark_node)
{
cp_error ("template argument %d is invalid", i + 1);
arg = error_mark_node;
}
else
arg = convert_template_argument (TREE_VALUE (parm),
arg, new_args, complain, i,
in_decl);
if (arg == error_mark_node)
lost++;
TREE_VEC_ELT (new_inner_args, i) = arg;
}
if (lost)
return error_mark_node;
return new_inner_args;
}
static int
template_args_equal (ot, nt)
tree ot, nt;
{
if (nt == ot)
return 1;
if (TREE_CODE (nt) != TREE_CODE (ot))
return 0;
if (TREE_CODE (nt) == TREE_VEC)
return comp_template_args (ot, nt);
else if (TREE_CODE_CLASS (TREE_CODE (ot)) == 't')
return same_type_p (ot, nt);
else
return (cp_tree_equal (ot, nt) > 0);
}
int
comp_template_args (oldargs, newargs)
tree oldargs, newargs;
{
int i;
if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
return 0;
for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
{
tree nt = TREE_VEC_ELT (newargs, i);
tree ot = TREE_VEC_ELT (oldargs, i);
if (! template_args_equal (ot, nt))
return 0;
}
return 1;
}
static char *
mangle_class_name_for_template (name, parms, arglist)
char *name;
tree parms, arglist;
{
static struct obstack scratch_obstack;
static char *scratch_firstobj;
int i, nparms;
if (!scratch_firstobj)
gcc_obstack_init (&scratch_obstack);
else
obstack_free (&scratch_obstack, scratch_firstobj);
scratch_firstobj = obstack_alloc (&scratch_obstack, 1);
#define ccat(c) obstack_1grow (&scratch_obstack, (c));
#define cat(s) obstack_grow (&scratch_obstack, (s), strlen (s))
cat (name);
ccat ('<');
nparms = TREE_VEC_LENGTH (parms);
arglist = innermost_args (arglist);
my_friendly_assert (nparms == TREE_VEC_LENGTH (arglist), 268);
for (i = 0; i < nparms; i++)
{
tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
tree arg = TREE_VEC_ELT (arglist, i);
if (i)
ccat (',');
if (TREE_CODE (parm) == TYPE_DECL)
{
cat (type_as_string_real (arg, 0, 1));
continue;
}
else if (TREE_CODE (parm) == TEMPLATE_DECL)
{
if (TREE_CODE (arg) == TEMPLATE_DECL)
{
tree context = DECL_CONTEXT (arg);
if (context)
{
my_friendly_assert (TREE_CODE (context) == NAMESPACE_DECL, 980422);
cat(decl_as_string (DECL_CONTEXT (arg), 0));
cat("::");
}
cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
}
else
cat (type_as_string_real (arg, 0, 1));
continue;
}
else
my_friendly_assert (TREE_CODE (parm) == PARM_DECL, 269);
if (TREE_CODE (arg) == TREE_LIST)
{
my_friendly_assert (TREE_PURPOSE (arg) == NULL_TREE, 270);
arg = TREE_VALUE (arg);
}
cat (expr_as_string (arg, 0));
}
{
char *bufp = obstack_next_free (&scratch_obstack);
int offset = 0;
while (bufp[offset - 1] == ' ')
offset--;
obstack_blank_fast (&scratch_obstack, offset);
if (bufp[offset - 1] == '>')
ccat (' ');
}
ccat ('>');
ccat ('\0');
return (char *) obstack_base (&scratch_obstack);
}
static tree
classtype_mangled_name (t)
tree t;
{
if (CLASSTYPE_TEMPLATE_INFO (t)
&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
{
tree tmpl = most_general_template (CLASSTYPE_TI_TEMPLATE (t));
if (PRIMARY_TEMPLATE_P (tmpl))
{
tree name = DECL_NAME (tmpl);
char *mangled_name = mangle_class_name_for_template
(IDENTIFIER_POINTER (name),
DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
CLASSTYPE_TI_ARGS (t));
tree id = get_identifier (mangled_name);
IDENTIFIER_TEMPLATE (id) = name;
return id;
}
}
return TYPE_IDENTIFIER (t);
}
static void
add_pending_template (d)
tree d;
{
tree ti;
if (TREE_CODE_CLASS (TREE_CODE (d)) == 't')
ti = CLASSTYPE_TEMPLATE_INFO (d);
else
ti = DECL_TEMPLATE_INFO (d);
if (TI_PENDING_TEMPLATE_FLAG (ti))
return;
*template_tail = perm_tree_cons
(build_srcloc_here (), d, NULL_TREE);
template_tail = &TREE_CHAIN (*template_tail);
TI_PENDING_TEMPLATE_FLAG (ti) = 1;
}
tree
lookup_template_function (fns, arglist)
tree fns, arglist;
{
tree type;
if (fns == NULL_TREE)
{
cp_error ("non-template used as template");
return error_mark_node;
}
type = TREE_TYPE (fns);
if (TREE_CODE (fns) == OVERLOAD || !type)
type = unknown_type_node;
if (processing_template_decl)
return build_min (TEMPLATE_ID_EXPR, type, fns, arglist);
else
return build (TEMPLATE_ID_EXPR, type, fns, arglist);
}
static tree
maybe_get_template_decl_from_type_decl (decl)
tree decl;
{
return (decl != NULL_TREE
&& TREE_CODE (decl) == TYPE_DECL
&& DECL_ARTIFICIAL (decl)
&& CLASS_TYPE_P (TREE_TYPE (decl))
&& CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
}
tree
lookup_template_class (d1, arglist, in_decl, context, entering_scope)
tree d1, arglist;
tree in_decl;
tree context;
int entering_scope;
{
tree template = NULL_TREE, parmlist;
tree t;
if (TREE_CODE (d1) == IDENTIFIER_NODE)
{
if (IDENTIFIER_VALUE (d1)
&& DECL_TEMPLATE_TEMPLATE_PARM_P (IDENTIFIER_VALUE (d1)))
template = IDENTIFIER_VALUE (d1);
else
{
if (context)
push_decl_namespace (context);
if (current_class_type != NULL_TREE)
template =
maybe_get_template_decl_from_type_decl
(IDENTIFIER_CLASS_VALUE (d1));
if (template == NULL_TREE)
template = lookup_name_nonclass (d1);
if (context)
pop_decl_namespace ();
}
if (template)
context = DECL_CONTEXT (template);
}
else if (TREE_CODE (d1) == TYPE_DECL && IS_AGGR_TYPE (TREE_TYPE (d1)))
{
tree type = TREE_TYPE (d1);
if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
type = TREE_TYPE (type);
if (CLASSTYPE_TEMPLATE_INFO (type))
{
template = CLASSTYPE_TI_TEMPLATE (type);
d1 = DECL_NAME (template);
}
}
else if (TREE_CODE (d1) == ENUMERAL_TYPE
|| (TREE_CODE_CLASS (TREE_CODE (d1)) == 't'
&& IS_AGGR_TYPE (d1)))
{
template = TYPE_TI_TEMPLATE (d1);
d1 = DECL_NAME (template);
}
else if (TREE_CODE (d1) == TEMPLATE_DECL
&& TREE_CODE (DECL_RESULT (d1)) == TYPE_DECL)
{
template = d1;
d1 = DECL_NAME (template);
context = DECL_CONTEXT (template);
}
else
my_friendly_abort (272);
if (! template)
{
cp_error ("`%T' is not a template", d1);
return error_mark_node;
}
if (context == NULL_TREE)
context = global_namespace;
if (TREE_CODE (template) != TEMPLATE_DECL)
{
cp_error ("non-template type `%T' used as a template", d1);
if (in_decl)
cp_error_at ("for template declaration `%D'", in_decl);
return error_mark_node;
}
if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
{
tree parm = copy_template_template_parm (TREE_TYPE (template));
tree template2 = TYPE_STUB_DECL (parm);
tree arglist2;
parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
arglist2 = coerce_template_parms (parmlist, arglist, template, 1, 1);
if (arglist2 == error_mark_node)
return error_mark_node;
arglist2 = copy_to_permanent (arglist2);
TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm)
= perm_tree_cons (template2, arglist2, NULL_TREE);
TYPE_SIZE (parm) = 0;
return parm;
}
else
{
tree template_type = TREE_TYPE (template);
tree gen_tmpl;
tree type_decl;
tree found = NULL_TREE;
int arg_depth;
int parm_depth;
int is_partial_instantiation;
gen_tmpl = most_general_template (template);
parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
parm_depth = TMPL_PARMS_DEPTH (parmlist);
arg_depth = TMPL_ARGS_DEPTH (arglist);
push_momentary ();
if (arg_depth == 1 && parm_depth > 1)
{
arglist
= add_outermost_template_args (TYPE_TI_ARGS (TREE_TYPE (template)),
arglist);
arg_depth = TMPL_ARGS_DEPTH (arglist);
}
my_friendly_assert (parm_depth == arg_depth, 0);
template = gen_tmpl;
if (parm_depth > 1)
{
int i;
int saved_depth = TMPL_ARGS_DEPTH (arglist);
tree bound_args = make_temp_vec (parm_depth);
for (i = saved_depth,
t = DECL_TEMPLATE_PARMS (template);
i > 0 && t != NULL_TREE;
--i, t = TREE_CHAIN (t))
{
tree a = coerce_template_parms (TREE_VALUE (t),
arglist, template, 1, 1);
SET_TMPL_ARGS_LEVEL (bound_args, i, a);
TREE_VEC_LENGTH (arglist)--;
}
TREE_VEC_LENGTH (arglist) = saved_depth;
arglist = bound_args;
}
else
arglist
= coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parmlist),
innermost_args (arglist),
template, 1, 1);
if (arglist == error_mark_node)
return error_mark_node;
if (comp_template_args (TYPE_TI_ARGS (template_type),
arglist))
{
found = template_type;
if (!entering_scope && PRIMARY_TEMPLATE_P (template))
{
tree ctx;
for (ctx = current_class_type;
ctx;
ctx = (TREE_CODE_CLASS (TREE_CODE (ctx)) == 't')
? TYPE_CONTEXT (ctx) : DECL_CONTEXT (ctx))
if (same_type_p (ctx, template_type))
break;
if (!ctx)
found = NULL_TREE;
}
}
if (!found)
{
for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
found; found = TREE_CHAIN (found))
if (comp_template_args (TREE_PURPOSE (found), arglist))
break;
if (found)
found = TREE_VALUE (found);
}
if (found)
{
pop_momentary ();
return found;
}
push_obstacks (&permanent_obstack, &permanent_obstack);
is_partial_instantiation = uses_template_parms (arglist);
if (TREE_CODE (template_type) == ENUMERAL_TYPE)
{
if (!is_partial_instantiation)
t = start_enum (TYPE_IDENTIFIER (template_type));
else
t = make_node (ENUMERAL_TYPE);
}
else
{
t = make_lang_type (TREE_CODE (template_type));
CLASSTYPE_DECLARED_CLASS (t)
= CLASSTYPE_DECLARED_CLASS (template_type);
CLASSTYPE_GOT_SEMICOLON (t) = 1;
SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
}
if (!TYPE_NAME (t))
{
TYPE_CONTEXT (t) = FROB_CONTEXT (context);
type_decl = build_decl (TYPE_DECL, DECL_NAME (template), t);
SET_DECL_ARTIFICIAL (type_decl);
DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
DECL_SOURCE_FILE (type_decl)
= DECL_SOURCE_FILE (TYPE_STUB_DECL (template_type));
DECL_SOURCE_LINE (type_decl)
= DECL_SOURCE_LINE (TYPE_STUB_DECL (template_type));
TYPE_STUB_DECL (t) = TYPE_NAME (t) = type_decl;
}
else
type_decl = TYPE_NAME (t);
if (parm_depth == 1 || is_partial_instantiation
|| !PRIMARY_TEMPLATE_P (template))
found = template;
else
{
for (found = DECL_TEMPLATE_INSTANTIATIONS (template);
found; found = TREE_CHAIN (found))
{
int success;
tree tmpl = CLASSTYPE_TI_TEMPLATE (TREE_VALUE (found));
if (CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_VALUE (found))
|| !uses_template_parms (TREE_VALUE (found)))
continue;
TREE_VEC_LENGTH (arglist)--;
TREE_VEC_LENGTH (TREE_PURPOSE (found)) --;
success = comp_template_args (TREE_PURPOSE (found), arglist);
TREE_VEC_LENGTH (arglist)++;
TREE_VEC_LENGTH (TREE_PURPOSE (found))++;
if (success)
{
found = tmpl;
break;
}
}
if (!found)
my_friendly_abort (0);
}
arglist = copy_to_permanent (arglist);
SET_TYPE_TEMPLATE_INFO (t,
tree_cons (found, arglist, NULL_TREE));
DECL_TEMPLATE_INSTANTIATIONS (template)
= tree_cons (arglist, t,
DECL_TEMPLATE_INSTANTIATIONS (template));
if (TREE_CODE (t) == ENUMERAL_TYPE
&& !is_partial_instantiation)
tsubst_enum (template_type, t, arglist);
pop_obstacks ();
pop_momentary ();
if (TREE_CODE (t) != ENUMERAL_TYPE)
DECL_NAME (type_decl) = classtype_mangled_name (t);
DECL_ASSEMBLER_NAME (type_decl) = DECL_NAME (type_decl);
if (!is_partial_instantiation)
{
DECL_ASSEMBLER_NAME (type_decl)
= get_identifier (build_overload_name (t, 1, 1));
if (TREE_CODE (t) != ENUMERAL_TYPE
&& flag_external_templates
&& CLASSTYPE_INTERFACE_KNOWN (TREE_TYPE (template))
&& ! CLASSTYPE_INTERFACE_ONLY (TREE_TYPE (template)))
add_pending_template (t);
}
else
DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
return t;
}
}
static int
for_each_template_parm (t, fn, data)
tree t;
tree_fn_t fn;
void* data;
{
if (!t)
return 0;
if (TREE_CODE_CLASS (TREE_CODE (t)) == 't'
&& for_each_template_parm (TYPE_CONTEXT (t), fn, data))
return 1;
switch (TREE_CODE (t))
{
case INDIRECT_REF:
case COMPONENT_REF:
return for_each_template_parm (TREE_TYPE (t), fn, data);
case ARRAY_REF:
case OFFSET_REF:
return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
|| for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
case IDENTIFIER_NODE:
if (!IDENTIFIER_TEMPLATE (t))
return 0;
my_friendly_abort (42);
case TREE_VEC:
{
int i = TREE_VEC_LENGTH (t);
while (i--)
if (for_each_template_parm (TREE_VEC_ELT (t, i), fn, data))
return 1;
return 0;
}
case TREE_LIST:
if (for_each_template_parm (TREE_PURPOSE (t), fn, data)
|| for_each_template_parm (TREE_VALUE (t), fn, data))
return 1;
return for_each_template_parm (TREE_CHAIN (t), fn, data);
case OVERLOAD:
if (for_each_template_parm (OVL_FUNCTION (t), fn, data))
return 1;
return for_each_template_parm (OVL_CHAIN (t), fn, data);
case POINTER_TYPE:
case REFERENCE_TYPE:
return for_each_template_parm (TREE_TYPE (t), fn, data);
case RECORD_TYPE:
if (TYPE_PTRMEMFUNC_FLAG (t))
return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE (t),
fn, data);
case UNION_TYPE:
case ENUMERAL_TYPE:
if (! TYPE_TEMPLATE_INFO (t))
return 0;
return for_each_template_parm (TREE_VALUE
(TYPE_TEMPLATE_INFO (t)),
fn, data);
case METHOD_TYPE:
if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data))
return 1;
case FUNCTION_TYPE:
{
tree parm;
for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
if (for_each_template_parm (TREE_VALUE (parm), fn, data))
return 1;
}
return for_each_template_parm (TREE_TYPE (t), fn, data);
case ARRAY_TYPE:
if (for_each_template_parm (TYPE_DOMAIN (t), fn, data))
return 1;
return for_each_template_parm (TREE_TYPE (t), fn, data);
case OFFSET_TYPE:
if (for_each_template_parm (TYPE_OFFSET_BASETYPE (t), fn, data))
return 1;
return for_each_template_parm (TREE_TYPE (t), fn, data);
case TYPE_DECL:
return for_each_template_parm (TREE_TYPE (t), fn, data);
case TEMPLATE_DECL:
if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
return for_each_template_parm (TREE_TYPE (t), fn, data);
return 0;
case CONST_DECL:
if (for_each_template_parm (DECL_INITIAL (t), fn, data))
return 1;
goto check_type_and_context;
case FUNCTION_DECL:
case VAR_DECL:
if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t)
&& for_each_template_parm (DECL_TI_ARGS (t), fn, data))
return 1;
case PARM_DECL:
check_type_and_context:
if (for_each_template_parm (TREE_TYPE (t), fn, data))
return 1;
if (DECL_CONTEXT (t)
&& for_each_template_parm (DECL_CONTEXT (t), fn, data))
return 1;
return 0;
case CALL_EXPR:
return (for_each_template_parm (TREE_OPERAND (t, 0), fn, data)
|| for_each_template_parm (TREE_OPERAND (t, 1), fn, data));
case ADDR_EXPR:
return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
case TEMPLATE_TEMPLATE_PARM:
if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t)
&& for_each_template_parm (TYPE_TI_ARGS (t), fn, data))
return 1;
case TEMPLATE_TYPE_PARM:
case TEMPLATE_PARM_INDEX:
if (fn)
return (*fn)(t, data);
else
return 1;
case INTEGER_TYPE:
if (for_each_template_parm (TYPE_MIN_VALUE (t), fn, data))
return 1;
return for_each_template_parm (TYPE_MAX_VALUE (t), fn, data);
case REAL_TYPE:
case COMPLEX_TYPE:
case VECTOR_TYPE:
case VOID_TYPE:
case BOOLEAN_TYPE:
case NAMESPACE_DECL:
return 0;
case INTEGER_CST:
case REAL_CST:
case STRING_CST:
return 0;
case ERROR_MARK:
my_friendly_assert (t == error_mark_node, 274);
return 0;
case LOOKUP_EXPR:
case TYPENAME_TYPE:
return 1;
case PTRMEM_CST:
return for_each_template_parm (TREE_TYPE (t), fn, data);
case SCOPE_REF:
return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
case CONSTRUCTOR:
if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t)))
return for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
(TREE_TYPE (t)), fn, data);
return for_each_template_parm (TREE_OPERAND (t, 1), fn, data);
case MODOP_EXPR:
case CAST_EXPR:
case REINTERPRET_CAST_EXPR:
case CONST_CAST_EXPR:
case STATIC_CAST_EXPR:
case DYNAMIC_CAST_EXPR:
case ARROW_EXPR:
case DOTSTAR_EXPR:
case TYPEID_EXPR:
return 1;
case SIZEOF_EXPR:
case ALIGNOF_EXPR:
return for_each_template_parm (TREE_OPERAND (t, 0), fn, data);
default:
switch (TREE_CODE_CLASS (TREE_CODE (t)))
{
case '1':
case '2':
case 'e':
case '<':
{
int i;
for (i = first_rtl_op (TREE_CODE (t)); --i >= 0;)
if (for_each_template_parm (TREE_OPERAND (t, i), fn, data))
return 1;
return 0;
}
default:
break;
}
sorry ("testing %s for template parms",
tree_code_name [(int) TREE_CODE (t)]);
my_friendly_abort (82);
return 0;
}
}
int
uses_template_parms (t)
tree t;
{
return for_each_template_parm (t, 0, 0);
}
static struct tinst_level *current_tinst_level;
static struct tinst_level *free_tinst_level;
static int tinst_depth;
extern int max_tinst_depth;
#ifdef GATHER_STATISTICS
int depth_reached;
#endif
int tinst_level_tick;
int last_template_error_tick;
static void
print_template_context (err)
int err;
{
struct tinst_level *p = current_tinst_level;
int line = lineno;
char *file = input_filename;
if (err && p)
{
if (current_function_decl != p->decl
&& current_function_decl != NULL_TREE)
;
else
{
if (current_function_decl == p->decl)
;
else
fprintf (stderr, "%s: In instantiation of `%s':\n",
file, decl_as_string (p->decl, 0));
line = p->line;
file = p->file;
p = p->next;
}
}
for (; p; p = p->next)
{
fprintf (stderr, "%s:%d: instantiated from `%s'\n", file, line,
decl_as_string (p->decl, 0));
line = p->line;
file = p->file;
}
fprintf (stderr, "%s:%d: instantiated from here\n", file, line);
}
void
maybe_print_template_context ()
{
if (last_template_error_tick == tinst_level_tick
|| current_tinst_level == 0)
return;
last_template_error_tick = tinst_level_tick;
print_template_context (1);
}
static int
push_tinst_level (d)
tree d;
{
struct tinst_level *new;
if (tinst_depth >= max_tinst_depth)
{
if (uses_template_parms (d))
return 0;
last_template_error_tick = tinst_level_tick;
error ("template instantiation depth exceeds maximum of %d",
max_tinst_depth);
error (" (use -ftemplate-depth-NN to increase the maximum)");
cp_error (" instantiating `%D'", d);
print_template_context (0);
return 0;
}
if (free_tinst_level)
{
new = free_tinst_level;
free_tinst_level = new->next;
}
else
new = (struct tinst_level *) xmalloc (sizeof (struct tinst_level));
new->decl = d;
new->line = lineno;
new->file = input_filename;
new->next = current_tinst_level;
current_tinst_level = new;
++tinst_depth;
#ifdef GATHER_STATISTICS
if (tinst_depth > depth_reached)
depth_reached = tinst_depth;
#endif
++tinst_level_tick;
return 1;
}
void
pop_tinst_level ()
{
struct tinst_level *old = current_tinst_level;
lineno = old->line;
input_filename = old->file;
extract_interface_info ();
current_tinst_level = old->next;
old->next = free_tinst_level;
free_tinst_level = old;
--tinst_depth;
++tinst_level_tick;
}
struct tinst_level *
tinst_for_decl ()
{
struct tinst_level *p = current_tinst_level;
if (p)
for (; p->next ; p = p->next )
;
return p;
}
static tree
tsubst_friend_function (decl, args)
tree decl;
tree args;
{
tree new_friend;
int line = lineno;
char *file = input_filename;
lineno = DECL_SOURCE_LINE (decl);
input_filename = DECL_SOURCE_FILE (decl);
if (TREE_CODE (decl) == FUNCTION_DECL
&& DECL_TEMPLATE_INSTANTIATION (decl)
&& TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
{
tree template_id;
tree new_args;
tree tmpl;
template_id
= lookup_template_function (tsubst_expr (DECL_TI_TEMPLATE (decl),
args, 1,
NULL_TREE),
tsubst (DECL_TI_ARGS (decl),
args, 1,
NULL_TREE));
new_friend = tsubst (decl, args, 1, NULL_TREE);
tmpl = determine_specialization (template_id, new_friend,
&new_args,
0);
new_friend = instantiate_template (tmpl, new_args);
goto done;
}
new_friend = tsubst (decl, args, 1, NULL_TREE);
DECL_USE_TEMPLATE (new_friend) = 0;
if (TREE_CODE (decl) == TEMPLATE_DECL)
DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
if (TREE_CODE (new_friend) != TEMPLATE_DECL)
{
set_mangled_name_for_decl (new_friend);
DECL_RTL (new_friend) = 0;
make_decl_rtl (new_friend, NULL_PTR, 1);
}
if (DECL_NAMESPACE_SCOPE_P (new_friend))
{
tree old_decl;
tree new_friend_template_info;
tree new_friend_result_template_info;
int new_friend_is_defn;
new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
if (TREE_CODE (new_friend) == TEMPLATE_DECL)
{
DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
new_friend_is_defn
= DECL_INITIAL (DECL_RESULT (new_friend)) != NULL_TREE;
new_friend_result_template_info
= DECL_TEMPLATE_INFO (DECL_RESULT (new_friend));
}
else
{
new_friend_is_defn = DECL_INITIAL (new_friend) != NULL_TREE;
new_friend_result_template_info = NULL_TREE;
}
old_decl = pushdecl_namespace_level (new_friend);
if (old_decl != new_friend)
{
if (!new_friend_is_defn)
;
else
{
DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
if (TREE_CODE (old_decl) != TEMPLATE_DECL)
;
else
{
tree t;
tree new_friend_args;
DECL_TEMPLATE_INFO (DECL_RESULT (old_decl))
= new_friend_result_template_info;
new_friend_args = TI_ARGS (new_friend_template_info);
for (t = DECL_TEMPLATE_SPECIALIZATIONS (old_decl);
t != NULL_TREE;
t = TREE_CHAIN (t))
{
tree spec = TREE_VALUE (t);
DECL_TI_ARGS (spec)
= add_outermost_template_args (new_friend_args,
DECL_TI_ARGS (spec));
DECL_TI_ARGS (spec)
= copy_to_permanent (DECL_TI_ARGS (spec));
}
t = most_general_template (old_decl);
if (t != old_decl)
{
DECL_TEMPLATE_SPECIALIZATIONS (t)
= chainon (DECL_TEMPLATE_SPECIALIZATIONS (t),
DECL_TEMPLATE_SPECIALIZATIONS (old_decl));
DECL_TEMPLATE_SPECIALIZATIONS (old_decl) = NULL_TREE;
}
}
}
new_friend = old_decl;
}
}
else if (TYPE_SIZE (DECL_CONTEXT (new_friend)))
{
tree fn = check_classfn (DECL_CONTEXT (new_friend),
new_friend);
if (fn)
new_friend = fn;
}
done:
lineno = line;
input_filename = file;
return new_friend;
}
static tree
tsubst_friend_class (friend_tmpl, args)
tree friend_tmpl;
tree args;
{
tree friend_type;
tree tmpl;
tmpl = lookup_name (DECL_NAME (friend_tmpl), 0);
if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
{
tmpl = lookup_name (DECL_NAME (friend_tmpl), 1);
tmpl = maybe_get_template_decl_from_type_decl (tmpl);
}
if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
{
tree parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
args, 1);
redeclare_class_template (TREE_TYPE (tmpl), parms);
friend_type = TREE_TYPE (tmpl);
}
else
{
tmpl = tsubst (friend_tmpl, args, 1, NULL_TREE);
DECL_USE_TEMPLATE (tmpl) = 0;
DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
}
return friend_type;
}
tree
instantiate_class_template (type)
tree type;
{
tree template, args, pattern, t;
tree typedecl;
if (type == error_mark_node)
return error_mark_node;
if (TYPE_BEING_DEFINED (type) || TYPE_SIZE (type))
return type;
push_momentary ();
template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
my_friendly_assert (TREE_CODE (template) == TEMPLATE_DECL, 279);
args = CLASSTYPE_TI_ARGS (type);
PARTIAL_INSTANTIATION_P (type) = uses_template_parms (args);
if (pedantic && PARTIAL_INSTANTIATION_P (type))
goto end;
if (PARTIAL_INSTANTIATION_P (type))
t = NULL_TREE;
else
{
t = most_specialized_class (template, args);
if (t == error_mark_node)
{
const char *str = "candidates are:";
cp_error ("ambiguous class template instantiation for `%#T'", type);
for (t = DECL_TEMPLATE_SPECIALIZATIONS (template); t;
t = TREE_CHAIN (t))
{
if (get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
args))
{
cp_error_at ("%s %+#T", str, TREE_TYPE (t));
str = " ";
}
}
TYPE_BEING_DEFINED (type) = 1;
type = error_mark_node;
goto end;
}
}
if (t)
pattern = TREE_TYPE (t);
else
pattern = TREE_TYPE (template);
if (TYPE_SIZE (pattern) == NULL_TREE)
goto end;
if (PARTIAL_INSTANTIATION_P (type))
{
TYPE_BINFO_BASETYPES (type) = TYPE_BINFO_BASETYPES (pattern);
TYPE_FIELDS (type) = TYPE_FIELDS (pattern);
TYPE_METHODS (type) = TYPE_METHODS (pattern);
CLASSTYPE_TAGS (type) = CLASSTYPE_TAGS (pattern);
TYPE_SIZE (type) = integer_zero_node;
goto end;
}
if (! push_tinst_level (type))
goto end;
TYPE_BEING_DEFINED (type) = 1;
maybe_push_to_top_level (uses_template_parms (type));
if (t)
{
tree inner_args
= get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t),
args);
if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
{
args = copy_node (args);
SET_TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args), inner_args);
}
else
args = inner_args;
}
if (flag_external_templates)
{
if (flag_alt_external_templates)
{
CLASSTYPE_INTERFACE_ONLY (type) = interface_only;
SET_CLASSTYPE_INTERFACE_UNKNOWN_X (type, interface_unknown);
CLASSTYPE_VTABLE_NEEDS_WRITING (type)
= (! CLASSTYPE_INTERFACE_ONLY (type)
&& CLASSTYPE_INTERFACE_KNOWN (type));
}
else
{
CLASSTYPE_INTERFACE_ONLY (type) = CLASSTYPE_INTERFACE_ONLY (pattern);
SET_CLASSTYPE_INTERFACE_UNKNOWN_X
(type, CLASSTYPE_INTERFACE_UNKNOWN (pattern));
CLASSTYPE_VTABLE_NEEDS_WRITING (type)
= (! CLASSTYPE_INTERFACE_ONLY (type)
&& CLASSTYPE_INTERFACE_KNOWN (type));
}
}
else
{
SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
CLASSTYPE_VTABLE_NEEDS_WRITING (type) = 1;
}
TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
TYPE_HAS_DESTRUCTOR (type) = TYPE_HAS_DESTRUCTOR (pattern);
TYPE_OVERLOADS_CALL_EXPR (type) = TYPE_OVERLOADS_CALL_EXPR (pattern);
TYPE_OVERLOADS_ARRAY_REF (type) = TYPE_OVERLOADS_ARRAY_REF (pattern);
TYPE_OVERLOADS_ARROW (type) = TYPE_OVERLOADS_ARROW (pattern);
TYPE_GETS_NEW (type) = TYPE_GETS_NEW (pattern);
TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (pattern);
TYPE_VEC_DELETE_TAKES_SIZE (type) = TYPE_VEC_DELETE_TAKES_SIZE (pattern);
TYPE_HAS_ASSIGN_REF (type) = TYPE_HAS_ASSIGN_REF (pattern);
TYPE_HAS_CONST_ASSIGN_REF (type) = TYPE_HAS_CONST_ASSIGN_REF (pattern);
TYPE_HAS_ABSTRACT_ASSIGN_REF (type) = TYPE_HAS_ABSTRACT_ASSIGN_REF (pattern);
TYPE_HAS_INIT_REF (type) = TYPE_HAS_INIT_REF (pattern);
TYPE_HAS_CONST_INIT_REF (type) = TYPE_HAS_CONST_INIT_REF (pattern);
TYPE_HAS_DEFAULT_CONSTRUCTOR (type) = TYPE_HAS_DEFAULT_CONSTRUCTOR (pattern);
TYPE_HAS_CONVERSION (type) = TYPE_HAS_CONVERSION (pattern);
TYPE_USES_COMPLEX_INHERITANCE (type)
= TYPE_USES_COMPLEX_INHERITANCE (pattern);
TYPE_USES_MULTIPLE_INHERITANCE (type)
= TYPE_USES_MULTIPLE_INHERITANCE (pattern);
TYPE_USES_VIRTUAL_BASECLASSES (type)
= TYPE_USES_VIRTUAL_BASECLASSES (pattern);
TYPE_PACKED (type) = TYPE_PACKED (pattern);
TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern);
if (ANON_UNION_TYPE_P (pattern))
SET_ANON_UNION_TYPE_P (type);
args = copy_to_permanent (args);
if (TYPE_BINFO_BASETYPES (pattern))
{
tree base_list = NULL_TREE;
tree pbases = TYPE_BINFO_BASETYPES (pattern);
int i;
for (i = 0; i < TREE_VEC_LENGTH (pbases); ++i)
{
tree base;
tree access;
tree pbase;
pbase = TREE_VEC_ELT (pbases, i);
base = tsubst (BINFO_TYPE (pbase), args,
1, NULL_TREE);
if (base == error_mark_node)
continue;
if (TREE_VIA_VIRTUAL (pbase))
{
if (TREE_VIA_PUBLIC (pbase))
access = access_public_virtual_node;
else if (TREE_VIA_PROTECTED (pbase))
access = access_protected_virtual_node;
else
access = access_private_virtual_node;
}
else
{
if (TREE_VIA_PUBLIC (pbase))
access = access_public_node;
else if (TREE_VIA_PROTECTED (pbase))
access = access_protected_node;
else
access = access_private_node;
}
base_list = tree_cons (access, base, base_list);
}
base_list = nreverse (base_list);
xref_basetypes (TREE_CODE (pattern) == RECORD_TYPE
? (CLASSTYPE_DECLARED_CLASS (pattern)
? class_type_node : record_type_node)
: union_type_node,
DECL_NAME (TYPE_NAME (pattern)),
type,
base_list);
}
pushclass (type, 1);
for (t = CLASSTYPE_TAGS (pattern); t; t = TREE_CHAIN (t))
{
tree tag = TREE_VALUE (t);
tree name = TYPE_IDENTIFIER (tag);
tree newtag;
newtag = tsubst (tag, args, 1, NULL_TREE);
if (TREE_CODE (newtag) != ENUMERAL_TYPE)
{
if (TYPE_LANG_SPECIFIC (tag) && CLASSTYPE_IS_TEMPLATE (tag))
CLASSTYPE_USE_TEMPLATE (newtag) = 0;
if (name)
SET_IDENTIFIER_TYPE_VALUE (name, newtag);
pushtag (name, newtag, 0);
}
}
for (t = TYPE_FIELDS (pattern); t; t = TREE_CHAIN (t))
if (TREE_CODE (t) != CONST_DECL)
{
tree r;
lineno = DECL_SOURCE_LINE (t);
input_filename = DECL_SOURCE_FILE (t);
r = tsubst (t, args, 1, NULL_TREE);
if (TREE_CODE (r) == VAR_DECL)
{
tree init;
if (DECL_DEFINED_IN_CLASS_P (r))
init = tsubst_expr (DECL_INITIAL (t), args,
1, NULL_TREE);
else
init = NULL_TREE;
finish_static_data_member_decl (r, init,
NULL_TREE,
0,
0);
if (DECL_DEFINED_IN_CLASS_P (r))
check_static_variable_definition (r, TREE_TYPE (r));
}
if (!TREE_CHAIN (r))
{
set_current_access_from_decl (r);
finish_member_declaration (r);
}
}
for (t = TYPE_METHODS (pattern); t; t = TREE_CHAIN (t))
{
tree r = tsubst (t, args, 1, NULL_TREE);
set_current_access_from_decl (r);
finish_member_declaration (r);
}
typedecl = TYPE_MAIN_DECL (type);
for (t = DECL_FRIENDLIST (TYPE_MAIN_DECL (pattern));
t != NULL_TREE;
t = TREE_CHAIN (t))
{
tree friends;
for (friends = TREE_VALUE (t);
friends != NULL_TREE;
friends = TREE_CHAIN (friends))
if (TREE_PURPOSE (friends) == error_mark_node)
add_friend (type,
tsubst_friend_function (TREE_VALUE (friends),
args));
else
add_friends (type,
tsubst_copy (TREE_PURPOSE (t), args,
1, NULL_TREE),
tsubst (TREE_PURPOSE (friends), args,
1, NULL_TREE));
}
for (t = CLASSTYPE_FRIEND_CLASSES (pattern);
t != NULL_TREE;
t = TREE_CHAIN (t))
{
tree friend_type = TREE_VALUE (t);
tree new_friend_type;
if (TREE_CODE (friend_type) == TEMPLATE_DECL)
new_friend_type = tsubst_friend_class (friend_type, args);
else if (uses_template_parms (friend_type))
new_friend_type = tsubst (friend_type, args, 1,
NULL_TREE);
else
new_friend_type =
xref_tag_from_type (friend_type, NULL_TREE, 1);
if (TREE_CODE (friend_type) == TEMPLATE_DECL)
++processing_template_decl;
make_friend_class (type, new_friend_type);
if (TREE_CODE (friend_type) == TEMPLATE_DECL)
--processing_template_decl;
}
if (!processing_template_decl)
{
t = tsubst (DECL_TEMPLATE_INJECT (template), args,
1, NULL_TREE);
for (; t; t = TREE_CHAIN (t))
{
tree d = TREE_VALUE (t);
if (TREE_CODE (d) == TYPE_DECL)
;
else
pushdecl (d);
}
}
for (t = TYPE_FIELDS (type); t; t = TREE_CHAIN (t))
if (TREE_CODE (t) == FIELD_DECL)
{
TREE_TYPE (t) = complete_type (TREE_TYPE (t));
require_complete_type (t);
}
lineno = DECL_SOURCE_LINE (typedecl);
input_filename = DECL_SOURCE_FILE (typedecl);
unreverse_member_declarations (type);
finish_struct_1 (type, 0);
CLASSTYPE_GOT_SEMICOLON (type) = 1;
TYPE_BEING_DEFINED (type) = 0;
repo_template_used (type);
popclass ();
pop_from_top_level ();
pop_tinst_level ();
end:
pop_momentary ();
return type;
}
static int
list_eq (t1, t2)
tree t1, t2;
{
if (t1 == NULL_TREE)
return t2 == NULL_TREE;
if (t2 == NULL_TREE)
return 0;
if (TYPE_MAIN_VARIANT (TREE_VALUE (t1))
!= TYPE_MAIN_VARIANT (TREE_VALUE (t2)))
return 0;
return list_eq (TREE_CHAIN (t1), TREE_CHAIN (t2));
}
static tree
maybe_fold_nontype_arg (arg)
tree arg;
{
if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't'
&& !uses_template_parms (arg))
{
int saved_processing_template_decl = processing_template_decl;
processing_template_decl = 0;
arg = fold (build_expr_from_tree (arg));
processing_template_decl = saved_processing_template_decl;
}
return arg;
}
tree
innermost_args (args)
tree args;
{
return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
}
static tree
tsubst_template_arg_vector (t, args, complain)
tree t;
tree args;
int complain;
{
int len = TREE_VEC_LENGTH (t), need_new = 0, i;
tree *elts = (tree *) alloca (len * sizeof (tree));
bzero ((char *) elts, len * sizeof (tree));
for (i = 0; i < len; i++)
{
if (TREE_VEC_ELT (t, i) != NULL_TREE
&& TREE_CODE (TREE_VEC_ELT (t, i)) == TREE_VEC)
elts[i] = tsubst_template_arg_vector (TREE_VEC_ELT (t, i),
args, complain);
else
elts[i] = maybe_fold_nontype_arg
(tsubst_expr (TREE_VEC_ELT (t, i), args, complain,
NULL_TREE));
if (elts[i] != TREE_VEC_ELT (t, i))
need_new = 1;
}
if (!need_new)
return t;
t = make_temp_vec (len);
for (i = 0; i < len; i++)
TREE_VEC_ELT (t, i) = elts[i];
return t;
}
static tree
tsubst_template_parms (parms, args, complain)
tree parms;
tree args;
int complain;
{
tree r;
tree* new_parms = &r;
for (new_parms = &r;
TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
new_parms = &(TREE_CHAIN (*new_parms)),
parms = TREE_CHAIN (parms))
{
tree new_vec =
make_tree_vec (TREE_VEC_LENGTH (TREE_VALUE (parms)));
int i;
for (i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
{
tree default_value =
TREE_PURPOSE (TREE_VEC_ELT (TREE_VALUE (parms), i));
tree parm_decl =
TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), i));
TREE_VEC_ELT (new_vec, i)
= build_tree_list (tsubst (default_value, args, complain,
NULL_TREE),
tsubst (parm_decl, args, complain,
NULL_TREE));
}
*new_parms =
tree_cons (build_int_2 (0, (TMPL_PARMS_DEPTH (parms)
- TMPL_ARGS_DEPTH (args))),
new_vec, NULL_TREE);
}
return r;
}
static tree
tsubst_aggr_type (t, args, complain, in_decl, entering_scope)
tree t;
tree args;
int complain;
tree in_decl;
int entering_scope;
{
if (t == NULL_TREE)
return NULL_TREE;
switch (TREE_CODE (t))
{
case RECORD_TYPE:
if (TYPE_PTRMEMFUNC_P (t))
{
tree r = build_ptrmemfunc_type
(tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl));
return cp_build_qualified_type (r, TYPE_QUALS (t));
}
case ENUMERAL_TYPE:
case UNION_TYPE:
if (TYPE_TEMPLATE_INFO (t))
{
tree argvec;
tree context;
tree r;
if (TYPE_CONTEXT (t) != NULL_TREE)
context = tsubst_aggr_type (TYPE_CONTEXT (t), args,
complain,
in_decl, 1);
else
context = NULL_TREE;
push_momentary ();
argvec = tsubst_template_arg_vector (TYPE_TI_ARGS (t), args,
complain);
r = lookup_template_class (t, argvec, in_decl, context,
entering_scope);
pop_momentary ();
return cp_build_qualified_type (r, TYPE_QUALS (t));
}
else
return t;
default:
return tsubst (t, args, complain, in_decl);
}
}
static tree
tsubst_decl (t, args, type, in_decl)
tree t;
tree args;
tree type;
tree in_decl;
{
int saved_lineno;
char* saved_filename;
tree r = NULL_TREE;
saved_lineno = lineno;
saved_filename = input_filename;
lineno = DECL_SOURCE_LINE (t);
input_filename = DECL_SOURCE_FILE (t);
switch (TREE_CODE (t))
{
case TEMPLATE_DECL:
{
tree decl = DECL_TEMPLATE_RESULT (t);
tree spec;
int is_template_template_parm = DECL_TEMPLATE_TEMPLATE_PARM_P (t);
if (!is_template_template_parm)
{
tree tmpl_args = DECL_CLASS_TEMPLATE_P (t)
? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
: DECL_TI_ARGS (DECL_RESULT (t));
tree full_args;
push_momentary ();
full_args = tsubst_template_arg_vector (tmpl_args, args,
1);
my_friendly_assert (full_args != tmpl_args, 0);
spec = retrieve_specialization (t, full_args);
pop_momentary ();
if (spec != NULL_TREE)
{
r = spec;
break;
}
}
r = copy_node (t);
copy_lang_decl (r);
my_friendly_assert (DECL_LANG_SPECIFIC (r) != 0, 0);
TREE_CHAIN (r) = NULL_TREE;
if (is_template_template_parm)
{
tree new_decl = tsubst (decl, args, 1, in_decl);
DECL_RESULT (r) = new_decl;
TREE_TYPE (r) = TREE_TYPE (new_decl);
break;
}
DECL_CONTEXT (r)
= tsubst_aggr_type (DECL_CONTEXT (t), args, 1,
in_decl, 1);
DECL_CLASS_CONTEXT (r)
= tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args,
1, in_decl,
1);
DECL_TEMPLATE_INFO (r) = build_tree_list (t, args);
if (TREE_CODE (decl) == TYPE_DECL)
{
tree new_type = tsubst (TREE_TYPE (t), args,
1, in_decl);
TREE_TYPE (r) = new_type;
CLASSTYPE_TI_TEMPLATE (new_type) = r;
DECL_RESULT (r) = TYPE_MAIN_DECL (new_type);
DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
}
else
{
tree new_decl = tsubst (decl, args, 1, in_decl);
DECL_RESULT (r) = new_decl;
DECL_TI_TEMPLATE (new_decl) = r;
TREE_TYPE (r) = TREE_TYPE (new_decl);
DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
}
SET_DECL_IMPLICIT_INSTANTIATION (r);
DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
DECL_TEMPLATE_PARMS (r)
= tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
1);
if (PRIMARY_TEMPLATE_P (t))
DECL_PRIMARY_TEMPLATE (r) = r;
if (TREE_CODE (decl) == TYPE_DECL)
break;
for (spec = DECL_TEMPLATE_SPECIALIZATIONS (t);
spec != NULL_TREE;
spec = TREE_CHAIN (spec))
{
tree fn = TREE_VALUE (spec);
tree spec_args;
tree new_fn;
if (!DECL_TEMPLATE_SPECIALIZATION (fn))
continue;
if (TREE_CODE (fn) != TEMPLATE_DECL)
continue;
spec_args = tsubst (DECL_TI_ARGS (fn), args,
1, in_decl);
new_fn = tsubst (DECL_RESULT (most_general_template (fn)),
spec_args, 1, in_decl);
DECL_TI_TEMPLATE (new_fn) = fn;
register_specialization (new_fn, r,
innermost_args (spec_args));
}
register_specialization (r, t,
DECL_TI_ARGS (DECL_RESULT (r)));
}
break;
case FUNCTION_DECL:
{
tree ctx;
tree argvec = NULL_TREE;
tree *friends;
tree gen_tmpl;
int member;
int args_depth;
int parms_depth;
my_friendly_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
{
tree spec;
push_momentary ();
gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
argvec
= tsubst_template_arg_vector (DECL_TI_ARGS
(DECL_TEMPLATE_RESULT (gen_tmpl)),
args, 1);
spec = retrieve_specialization (gen_tmpl, argvec);
if (spec)
{
r = spec;
pop_momentary ();
break;
}
argvec = copy_to_permanent (argvec);
pop_momentary ();
args_depth = TMPL_ARGS_DEPTH (args);
parms_depth =
TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
if (args_depth > parms_depth
&& !DECL_TEMPLATE_SPECIALIZATION (t))
{
my_friendly_assert (DECL_FRIEND_P (t), 0);
if (parms_depth > 1)
{
int i;
args = make_temp_vec (parms_depth);
for (i = 0; i < parms_depth; ++i)
TREE_VEC_ELT (args, i) =
TREE_VEC_ELT (args, i + (args_depth - parms_depth));
}
else
args = TREE_VEC_ELT (args, args_depth - parms_depth);
}
}
else
{
my_friendly_assert ((TREE_CODE (DECL_TI_TEMPLATE (t))
== LOOKUP_EXPR)
|| (TREE_CODE (DECL_TI_TEMPLATE (t))
== IDENTIFIER_NODE), 0);
gen_tmpl = NULL_TREE;
}
if (DECL_CLASS_SCOPE_P (t))
{
if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
member = 2;
else
member = 1;
ctx = tsubst_aggr_type (DECL_CLASS_CONTEXT (t), args,
1, t,
1);
}
else
{
member = 0;
ctx = NULL_TREE;
}
type = tsubst (type, args, 1, in_decl);
r = copy_node (t);
copy_lang_decl (r);
DECL_USE_TEMPLATE (r) = 0;
TREE_TYPE (r) = type;
DECL_CONTEXT (r)
= tsubst_aggr_type (DECL_CONTEXT (t), args, 1, t,
1);
DECL_CLASS_CONTEXT (r) = ctx;
if (member && IDENTIFIER_TYPENAME_P (DECL_NAME (r)))
DECL_NAME (r) = build_typename_overload (TREE_TYPE (type));
DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
1, t);
DECL_MAIN_VARIANT (r) = r;
DECL_RESULT (r) = NULL_TREE;
TREE_STATIC (r) = 0;
TREE_PUBLIC (r) = TREE_PUBLIC (t);
DECL_EXTERNAL (r) = 1;
DECL_INTERFACE_KNOWN (r) = 0;
DECL_DEFER_OUTPUT (r) = 0;
TREE_CHAIN (r) = NULL_TREE;
DECL_PENDING_INLINE_INFO (r) = 0;
TREE_USED (r) = 0;
if (gen_tmpl)
{
argvec = copy_to_permanent (argvec);
DECL_TEMPLATE_INFO (r)
= perm_tree_cons (gen_tmpl, argvec, NULL_TREE);
SET_DECL_IMPLICIT_INSTANTIATION (r);
register_specialization (r, gen_tmpl, argvec);
if (DECL_DESTRUCTOR_P (t))
DECL_ASSEMBLER_NAME (r) = build_destructor_name (ctx);
else
{
tree tmpl = DECL_TI_TEMPLATE (t);
if (name_mangling_version < 1
|| tmpl == NULL_TREE
|| (member && !is_member_template (tmpl)
&& !DECL_TEMPLATE_INFO (tmpl)))
set_mangled_name_for_decl (r);
else
set_mangled_name_for_template_decl (r);
}
DECL_RTL (r) = 0;
make_decl_rtl (r, NULL_PTR, 1);
if (member
&& ! uses_template_parms (r)
&& (IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r))
== NULL_TREE))
SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (r), r);
}
for (friends = &DECL_BEFRIENDING_CLASSES (r);
*friends;
friends = &TREE_CHAIN (*friends))
{
*friends = copy_node (*friends);
TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
args, 1,
in_decl);
}
if (DECL_CONSTRUCTOR_P (r))
{
maybe_retrofit_in_chrg (r);
grok_ctor_properties (ctx, r);
}
if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
grok_op_properties (r, DECL_VIRTUAL_P (r), DECL_FRIEND_P (r));
}
break;
case PARM_DECL:
{
r = copy_node (t);
TREE_TYPE (r) = type;
c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
DECL_INITIAL (r) = TREE_TYPE (r);
else
DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
1, in_decl);
DECL_CONTEXT (r) = NULL_TREE;
#ifdef PROMOTE_PROTOTYPES
if ((TREE_CODE (type) == INTEGER_TYPE
|| TREE_CODE (type) == ENUMERAL_TYPE)
&& TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
DECL_ARG_TYPE (r) = integer_type_node;
#endif
if (TREE_CHAIN (t))
TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
1, TREE_CHAIN (t));
}
break;
case FIELD_DECL:
{
r = copy_node (t);
copy_lang_decl (r);
TREE_TYPE (r) = type;
c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
1, in_decl);
TREE_CHAIN (r) = NULL_TREE;
if (TREE_CODE (type) == VOID_TYPE)
cp_error_at ("instantiation of `%D' as type void", r);
}
break;
case USING_DECL:
{
r = copy_node (t);
DECL_INITIAL (r)
= tsubst_copy (DECL_INITIAL (t), args, 1, in_decl);
TREE_CHAIN (r) = NULL_TREE;
}
break;
case VAR_DECL:
{
tree argvec;
tree gen_tmpl;
tree spec;
tree tmpl;
tree ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
1,
in_decl, 1);
my_friendly_assert (DECL_LANG_SPECIFIC (t)
&& DECL_TEMPLATE_INFO (t) != NULL_TREE, 0);
tmpl = DECL_TI_TEMPLATE (t);
gen_tmpl = most_general_template (tmpl);
argvec = tsubst (DECL_TI_ARGS (t), args, 1, in_decl);
spec = retrieve_specialization (gen_tmpl, argvec);
if (spec)
{
r = spec;
break;
}
r = copy_node (t);
TREE_TYPE (r) = type;
c_apply_type_quals_to_decl (CP_TYPE_QUALS (type), r);
DECL_CONTEXT (r) = ctx;
DECL_INITIAL (r) = NULL_TREE;
DECL_RTL (r) = 0;
DECL_SIZE (r) = 0;
copy_lang_decl (r);
DECL_CLASS_CONTEXT (r) = DECL_CONTEXT (r);
DECL_EXTERNAL (r) = 1;
DECL_TEMPLATE_INFO (r) = perm_tree_cons (tmpl, argvec, NULL_TREE);
SET_DECL_IMPLICIT_INSTANTIATION (r);
register_specialization (r, gen_tmpl, argvec);
TREE_CHAIN (r) = NULL_TREE;
if (TREE_CODE (type) == VOID_TYPE)
cp_error_at ("instantiation of `%D' as type void", r);
}
break;
case TYPE_DECL:
if (t == TYPE_NAME (TREE_TYPE (t)))
r = TYPE_NAME (type);
else
{
r = copy_node (t);
TREE_TYPE (r) = type;
DECL_CONTEXT (r) = current_class_type;
TREE_CHAIN (r) = NULL_TREE;
}
break;
default:
my_friendly_abort (0);
}
lineno = saved_lineno;
input_filename = saved_filename;
return r;
}
static tree
tsubst_arg_types (arg_types, args, complain, in_decl)
tree arg_types;
tree args;
int complain;
tree in_decl;
{
tree remaining_arg_types;
tree type;
if (!arg_types || arg_types == void_list_node)
return arg_types;
remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
args, complain, in_decl);
if (remaining_arg_types == error_mark_node)
return error_mark_node;
type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
if (type == error_mark_node)
return error_mark_node;
type = TYPE_MAIN_VARIANT (type_decays_to (type));
return hash_tree_cons (TREE_PURPOSE (arg_types), type,
remaining_arg_types);
}
static tree
tsubst_function_type (t, args, complain, in_decl)
tree t;
tree args;
int complain;
tree in_decl;
{
tree return_type;
tree arg_types;
tree fntype;
my_friendly_assert (TYPE_CONTEXT (t) == NULL_TREE, 0);
return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (return_type == error_mark_node)
return error_mark_node;
arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args,
complain, in_decl);
if (arg_types == error_mark_node)
return error_mark_node;
if (TREE_CODE (t) == FUNCTION_TYPE)
fntype = build_function_type (return_type, arg_types);
else
{
tree r = TREE_TYPE (TREE_VALUE (arg_types));
if (! IS_AGGR_TYPE (r))
{
if (complain)
cp_error ("creating pointer to member function of non-class type `%T'",
r);
return error_mark_node;
}
fntype = build_cplus_method_type (r, return_type, TREE_CHAIN
(arg_types));
}
fntype = build_qualified_type (fntype, TYPE_QUALS (t));
fntype = build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
return fntype;
}
static tree
tsubst_call_declarator_parms (parms, args, complain, in_decl)
tree parms;
tree args;
int complain;
tree in_decl;
{
tree new_parms;
tree type;
tree defarg;
if (!parms || parms == void_list_node)
return parms;
new_parms = tsubst_call_declarator_parms (TREE_CHAIN (parms),
args, complain, in_decl);
type = tsubst (TREE_VALUE (parms), args, complain, in_decl);
defarg = tsubst_expr (TREE_PURPOSE (parms), args, complain, in_decl);
new_parms = tree_cons (defarg, type, new_parms);
TREE_PARMLIST (new_parms) = 1;
return new_parms;
}
tree
tsubst (t, args, complain, in_decl)
tree t, args;
int complain;
tree in_decl;
{
tree type, r;
if (t == NULL_TREE || t == error_mark_node
|| t == integer_type_node
|| t == void_type_node
|| t == char_type_node
|| TREE_CODE (t) == NAMESPACE_DECL)
return t;
if (TREE_CODE (t) == IDENTIFIER_NODE)
type = IDENTIFIER_TYPE_VALUE (t);
else
type = TREE_TYPE (t);
if (type == unknown_type_node)
my_friendly_abort (42);
if (type && TREE_CODE (t) != FUNCTION_DECL
&& TREE_CODE (t) != TYPENAME_TYPE
&& TREE_CODE (t) != TEMPLATE_DECL
&& TREE_CODE (t) != IDENTIFIER_NODE
&& TREE_CODE (t) != FUNCTION_TYPE
&& TREE_CODE (t) != METHOD_TYPE)
type = tsubst (type, args, complain, in_decl);
if (type == error_mark_node)
return error_mark_node;
if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
return tsubst_decl (t, args, type, in_decl);
switch (TREE_CODE (t))
{
case RECORD_TYPE:
case UNION_TYPE:
case ENUMERAL_TYPE:
return tsubst_aggr_type (t, args, complain, in_decl,
0);
case ERROR_MARK:
case IDENTIFIER_NODE:
case OP_IDENTIFIER:
case VOID_TYPE:
case REAL_TYPE:
case COMPLEX_TYPE:
case BOOLEAN_TYPE:
case INTEGER_CST:
case REAL_CST:
case STRING_CST:
return t;
case INTEGER_TYPE:
if (t == integer_type_node)
return t;
if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
&& TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
return t;
{
tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
max = tsubst_expr (omax, args, complain, in_decl);
if (max == error_mark_node)
return error_mark_node;
max = maybe_fold_nontype_arg (max);
if (!processing_template_decl && TREE_READONLY_DECL_P (max))
max = decl_constant_value (max);
if (processing_template_decl
|| TREE_CODE (max) != INTEGER_CST)
{
tree itype = make_node (INTEGER_TYPE);
TYPE_MIN_VALUE (itype) = size_zero_node;
TYPE_MAX_VALUE (itype) = build_min (MINUS_EXPR, sizetype, max,
integer_one_node);
return itype;
}
if (integer_zerop (omax))
{
if (pedantic)
pedwarn ("creating array with size zero");
}
else if (integer_zerop (max) || INT_CST_LT (max, integer_zero_node))
{
if (complain)
cp_error ("creating array with size `%E'", max);
return error_mark_node;
}
max = fold (build_binary_op (MINUS_EXPR, max, integer_one_node));
if (!TREE_PERMANENT (max) && !allocation_temporary_p ())
max = copy_to_permanent (max);
return build_index_type (max);
}
case TEMPLATE_TYPE_PARM:
case TEMPLATE_TEMPLATE_PARM:
case TEMPLATE_PARM_INDEX:
{
int idx;
int level;
int levels;
r = NULL_TREE;
if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
|| TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
{
idx = TEMPLATE_TYPE_IDX (t);
level = TEMPLATE_TYPE_LEVEL (t);
}
else
{
idx = TEMPLATE_PARM_IDX (t);
level = TEMPLATE_PARM_LEVEL (t);
}
if (TREE_VEC_LENGTH (args) > 0)
{
tree arg = NULL_TREE;
levels = TMPL_ARGS_DEPTH (args);
if (level <= levels)
arg = TMPL_ARG (args, level, idx);
if (arg == error_mark_node)
return error_mark_node;
else if (arg != NULL_TREE)
{
if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
{
my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (arg))
== 't', 0);
return cp_build_qualified_type
(arg, CP_TYPE_QUALS (arg) | CP_TYPE_QUALS (t));
}
else if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
{
if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
{
tree argvec = tsubst (TYPE_TI_ARGS (t),
args, complain, in_decl);
if (argvec == error_mark_node)
return error_mark_node;
if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
arg = TYPE_NAME (arg);
r = lookup_template_class (DECL_NAME (arg),
argvec, in_decl,
DECL_CONTEXT (arg),
0);
return cp_build_qualified_type (r, TYPE_QUALS (t));
}
else
return arg;
}
else
return arg;
}
}
else
my_friendly_abort (981018);
if (level == 1)
return t;
switch (TREE_CODE (t))
{
case TEMPLATE_TYPE_PARM:
case TEMPLATE_TEMPLATE_PARM:
r = copy_node (t);
TEMPLATE_TYPE_PARM_INDEX (r)
= reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
r, levels);
TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
TYPE_MAIN_VARIANT (r) = r;
TYPE_POINTER_TO (r) = NULL_TREE;
TYPE_REFERENCE_TO (r) = NULL_TREE;
if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
&& TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t))
{
tree argvec = tsubst (TYPE_TI_ARGS (t), args,
complain, in_decl);
if (argvec == error_mark_node)
return error_mark_node;
TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
= perm_tree_cons (TYPE_NAME (t), argvec, NULL_TREE);
}
break;
case TEMPLATE_PARM_INDEX:
r = reduce_template_parm_level (t, type, levels);
break;
default:
my_friendly_abort (0);
}
return r;
}
case TREE_LIST:
{
tree purpose, value, chain, result;
if (t == void_list_node)
return t;
purpose = TREE_PURPOSE (t);
if (purpose)
{
purpose = tsubst (purpose, args, complain, in_decl);
if (purpose == error_mark_node)
return error_mark_node;
}
value = TREE_VALUE (t);
if (value)
{
value = tsubst (value, args, complain, in_decl);
if (value == error_mark_node)
return error_mark_node;
}
chain = TREE_CHAIN (t);
if (chain && chain != void_type_node)
{
chain = tsubst (chain, args, complain, in_decl);
if (chain == error_mark_node)
return error_mark_node;
}
if (purpose == TREE_PURPOSE (t)
&& value == TREE_VALUE (t)
&& chain == TREE_CHAIN (t))
return t;
result = hash_tree_cons (purpose, value, chain);
TREE_PARMLIST (result) = TREE_PARMLIST (t);
return result;
}
case TREE_VEC:
if (type != NULL_TREE)
{
t = copy_node (t);
type = BINFO_TYPE (TYPE_BINFO (type));
TREE_TYPE (t) = complete_type (type);
if (IS_AGGR_TYPE (type))
{
BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (type);
BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (type);
if (TYPE_BINFO_BASETYPES (type) != NULL_TREE)
BINFO_BASETYPES (t) = copy_node (TYPE_BINFO_BASETYPES (type));
}
return t;
}
return tsubst_template_arg_vector (t, args, complain);
case POINTER_TYPE:
case REFERENCE_TYPE:
{
enum tree_code code;
if (type == TREE_TYPE (t))
return t;
code = TREE_CODE (t);
if (TREE_CODE (type) == REFERENCE_TYPE
|| (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
{
static int last_line = 0;
static char* last_file = 0;
if (complain && (last_line != lineno ||
last_file != input_filename))
{
if (TREE_CODE (type) == VOID_TYPE)
cp_error ("forming reference to void");
else
cp_error ("forming %s to reference type `%T'",
(code == POINTER_TYPE) ? "pointer" : "reference",
type);
last_line = lineno;
last_file = input_filename;
}
return error_mark_node;
}
else if (code == POINTER_TYPE)
r = build_pointer_type (type);
else
r = build_reference_type (type);
r = cp_build_qualified_type (r, TYPE_QUALS (t));
layout_type (r);
return r;
}
case OFFSET_TYPE:
{
r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
if (r == error_mark_node || !IS_AGGR_TYPE (r))
{
if (complain)
cp_error ("creating pointer to member of non-class type `%T'",
r);
return error_mark_node;
}
return build_offset_type (r, type);
}
case FUNCTION_TYPE:
case METHOD_TYPE:
{
tree fntype;
tree raises;
fntype = tsubst_function_type (t, args, complain, in_decl);
if (fntype == error_mark_node)
return error_mark_node;
raises = TYPE_RAISES_EXCEPTIONS (t);
if (raises)
{
raises = tsubst (raises, args, complain, in_decl);
if (raises == error_mark_node)
return raises;
fntype = build_exception_variant (fntype, raises);
}
return fntype;
}
case ARRAY_TYPE:
{
tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
if (domain == error_mark_node)
return error_mark_node;
if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
return t;
if (TREE_CODE (type) == VOID_TYPE
|| TREE_CODE (type) == FUNCTION_TYPE
|| TREE_CODE (type) == REFERENCE_TYPE)
{
if (complain)
cp_error ("creating array of `%T'", type);
return error_mark_node;
}
r = build_cplus_array_type (type, domain);
return r;
}
case PLUS_EXPR:
case MINUS_EXPR:
{
tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
in_decl);
tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain,
in_decl);
if (e1 == error_mark_node || e2 == error_mark_node)
return error_mark_node;
return fold (build (TREE_CODE (t), TREE_TYPE (t), e1, e2));
}
case NEGATE_EXPR:
case NOP_EXPR:
{
tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
in_decl);
if (e == error_mark_node)
return error_mark_node;
return fold (build (TREE_CODE (t), TREE_TYPE (t), e));
}
case TYPENAME_TYPE:
{
tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
in_decl, 1);
tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
complain, in_decl);
if (ctx == error_mark_node || f == error_mark_node)
return error_mark_node;
if (!IS_AGGR_TYPE (ctx))
{
if (complain)
cp_error ("`%T' is not a class, struct, or union type",
ctx);
return error_mark_node;
}
else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
{
ctx = complete_type (ctx);
if (!TYPE_SIZE (ctx))
{
if (complain)
incomplete_type_error (NULL_TREE, ctx);
return error_mark_node;
}
}
f = make_typename_type (ctx, f);
if (f == error_mark_node)
return f;
return cp_build_qualified_type (f,
CP_TYPE_QUALS (f)
| CP_TYPE_QUALS (t));
}
case INDIRECT_REF:
{
tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
in_decl);
if (e == error_mark_node)
return error_mark_node;
return make_pointer_declarator (type, e);
}
case ADDR_EXPR:
{
tree e = tsubst (TREE_OPERAND (t, 0), args, complain,
in_decl);
if (e == error_mark_node)
return error_mark_node;
return make_reference_declarator (type, e);
}
case ARRAY_REF:
{
tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
in_decl);
tree e2 = tsubst_expr (TREE_OPERAND (t, 1), args, complain,
in_decl);
if (e1 == error_mark_node || e2 == error_mark_node)
return error_mark_node;
return build_parse_node (ARRAY_REF, e1, e2, tsubst_expr);
}
case CALL_EXPR:
{
tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
in_decl);
tree e2 = tsubst_call_declarator_parms (TREE_OPERAND (t, 1), args,
complain, in_decl);
tree e3 = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (e1 == error_mark_node || e2 == error_mark_node
|| e3 == error_mark_node)
return error_mark_node;
return make_call_declarator (e1, e2, TREE_OPERAND (t, 2), e3);
}
case SCOPE_REF:
{
tree e1 = tsubst (TREE_OPERAND (t, 0), args, complain,
in_decl);
tree e2 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
if (e1 == error_mark_node || e2 == error_mark_node)
return error_mark_node;
return build_parse_node (TREE_CODE (t), e1, e2);
}
case TYPEOF_TYPE:
{
tree e1 = tsubst_expr (TYPE_FIELDS (t), args, complain,
in_decl);
if (e1 == error_mark_node)
return error_mark_node;
return TREE_TYPE (e1);
}
default:
sorry ("use of `%s' in template",
tree_code_name [(int) TREE_CODE (t)]);
return error_mark_node;
}
}
void
do_pushlevel ()
{
emit_line_note (input_filename, lineno);
pushlevel (0);
clear_last_expr ();
push_momentary ();
expand_start_bindings (0);
}
tree
do_poplevel ()
{
tree t;
int saved_warn_unused = 0;
if (processing_template_decl)
{
saved_warn_unused = warn_unused;
warn_unused = 0;
}
expand_end_bindings (getdecls (), kept_level_p (), 0);
if (processing_template_decl)
warn_unused = saved_warn_unused;
t = poplevel (kept_level_p (), 1, 0);
pop_momentary ();
return t;
}
tree
tsubst_copy (t, args, complain, in_decl)
tree t, args;
int complain;
tree in_decl;
{
enum tree_code code;
tree r;
if (t == NULL_TREE || t == error_mark_node)
return t;
code = TREE_CODE (t);
switch (code)
{
case PARM_DECL:
return do_identifier (DECL_NAME (t), 0, NULL_TREE);
case CONST_DECL:
{
tree enum_type;
tree v;
if (!DECL_CONTEXT (t))
return t;
enum_type
= tsubst_aggr_type (TREE_TYPE (t), args, complain, in_decl,
0);
for (v = TYPE_VALUES (enum_type);
v != NULL_TREE;
v = TREE_CHAIN (v))
if (TREE_PURPOSE (v) == DECL_NAME (t))
return TREE_VALUE (v);
my_friendly_abort (0);
}
return t;
case FIELD_DECL:
if (DECL_CONTEXT (t))
{
tree ctx;
ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
1);
if (ctx != DECL_CONTEXT (t))
return lookup_field (ctx, DECL_NAME (t), 0, 0);
}
return t;
case VAR_DECL:
case FUNCTION_DECL:
if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
t = tsubst (t, args, complain, in_decl);
mark_used (t);
return t;
case TEMPLATE_DECL:
if (is_member_template (t))
return tsubst (t, args, complain, in_decl);
else
return t;
case LOOKUP_EXPR:
{
tree id = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
if (id != TREE_OPERAND (t, 0))
{
r = build_nt (LOOKUP_EXPR, id);
LOOKUP_EXPR_GLOBAL (r) = LOOKUP_EXPR_GLOBAL (t);
t = r;
}
return t;
}
case CAST_EXPR:
case REINTERPRET_CAST_EXPR:
case CONST_CAST_EXPR:
case STATIC_CAST_EXPR:
case DYNAMIC_CAST_EXPR:
case NOP_EXPR:
return build1
(code, tsubst (TREE_TYPE (t), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
case INDIRECT_REF:
case PREDECREMENT_EXPR:
case PREINCREMENT_EXPR:
case POSTDECREMENT_EXPR:
case POSTINCREMENT_EXPR:
case NEGATE_EXPR:
case TRUTH_NOT_EXPR:
case BIT_NOT_EXPR:
case ADDR_EXPR:
case CONVERT_EXPR:
case SIZEOF_EXPR:
case ALIGNOF_EXPR:
case ARROW_EXPR:
case THROW_EXPR:
case TYPEID_EXPR:
return build1
(code, tsubst (TREE_TYPE (t), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
case PLUS_EXPR:
case MINUS_EXPR:
case MULT_EXPR:
case TRUNC_DIV_EXPR:
case CEIL_DIV_EXPR:
case FLOOR_DIV_EXPR:
case ROUND_DIV_EXPR:
case EXACT_DIV_EXPR:
case BIT_AND_EXPR:
case BIT_ANDTC_EXPR:
case BIT_IOR_EXPR:
case BIT_XOR_EXPR:
case TRUNC_MOD_EXPR:
case FLOOR_MOD_EXPR:
case TRUTH_ANDIF_EXPR:
case TRUTH_ORIF_EXPR:
case TRUTH_AND_EXPR:
case TRUTH_OR_EXPR:
case RSHIFT_EXPR:
case LSHIFT_EXPR:
case RROTATE_EXPR:
case LROTATE_EXPR:
case EQ_EXPR:
case NE_EXPR:
case MAX_EXPR:
case MIN_EXPR:
case LE_EXPR:
case GE_EXPR:
case LT_EXPR:
case GT_EXPR:
case COMPONENT_REF:
case ARRAY_REF:
case COMPOUND_EXPR:
case SCOPE_REF:
case DOTSTAR_EXPR:
case MEMBER_REF:
return build_nt
(code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
case CALL_EXPR:
{
tree fn = TREE_OPERAND (t, 0);
if (is_overloaded_fn (fn))
fn = tsubst_copy (get_first_fn (fn), args, complain, in_decl);
else
fn = tsubst_copy (fn, args, complain, in_decl);
return build_nt
(code, fn, tsubst_copy (TREE_OPERAND (t, 1), args, complain,
in_decl),
NULL_TREE);
}
case METHOD_CALL_EXPR:
{
tree name = TREE_OPERAND (t, 0);
if (TREE_CODE (name) == BIT_NOT_EXPR)
{
name = tsubst_copy (TREE_OPERAND (name, 0), args,
complain, in_decl);
name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
}
else if (TREE_CODE (name) == SCOPE_REF
&& TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
{
tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
complain, in_decl);
name = TREE_OPERAND (name, 1);
name = tsubst_copy (TREE_OPERAND (name, 0), args,
complain, in_decl);
name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
name = build_nt (SCOPE_REF, base, name);
}
else
name = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
return build_nt
(code, name, tsubst_copy (TREE_OPERAND (t, 1), args,
complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl),
NULL_TREE);
}
#ifdef OBJCPLUS
case MESSAGE_SEND_EXPR:
return build_nt
(MESSAGE_SEND_EXPR,
tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
TREE_OPERAND (t, 1),
tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
case CLASS_REFERENCE_EXPR:
return build_nt
(CLASS_REFERENCE_EXPR,
tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
#endif
case BIND_EXPR:
case COND_EXPR:
case MODOP_EXPR:
{
r = build_nt
(code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
if (code == BIND_EXPR && !processing_template_decl)
{
tree rtl_expr = begin_stmt_expr ();
tree block = tsubst_expr (TREE_OPERAND (r, 1), args,
complain, in_decl);
r = finish_stmt_expr (rtl_expr, block);
}
return r;
}
case NEW_EXPR:
{
r = build_nt
(code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl));
NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
return r;
}
case DELETE_EXPR:
{
r = build_nt
(code, tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl));
DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
return r;
}
case TEMPLATE_ID_EXPR:
{
tree targs = tsubst_copy (TREE_OPERAND (t, 1), args, complain,
in_decl);
if (targs && TREE_CODE (targs) == TREE_LIST)
{
tree chain;
for (chain = targs; chain; chain = TREE_CHAIN (chain))
TREE_VALUE (chain) = maybe_fold_nontype_arg (TREE_VALUE (chain));
}
else if (targs)
{
int i;
for (i = 0; i < TREE_VEC_LENGTH (targs); ++i)
TREE_VEC_ELT (targs, i)
= maybe_fold_nontype_arg (TREE_VEC_ELT (targs, i));
}
return lookup_template_function
(tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl), targs);
}
case TREE_LIST:
{
tree purpose, value, chain;
if (t == void_list_node)
return t;
purpose = TREE_PURPOSE (t);
if (purpose)
purpose = tsubst_copy (purpose, args, complain, in_decl);
value = TREE_VALUE (t);
if (value)
value = tsubst_copy (value, args, complain, in_decl);
chain = TREE_CHAIN (t);
if (chain && chain != void_type_node)
chain = tsubst_copy (chain, args, complain, in_decl);
if (purpose == TREE_PURPOSE (t)
&& value == TREE_VALUE (t)
&& chain == TREE_CHAIN (t))
return t;
return tree_cons (purpose, value, chain);
}
case RECORD_TYPE:
case UNION_TYPE:
case ENUMERAL_TYPE:
case INTEGER_TYPE:
case TEMPLATE_TYPE_PARM:
case TEMPLATE_TEMPLATE_PARM:
case TEMPLATE_PARM_INDEX:
case POINTER_TYPE:
case REFERENCE_TYPE:
case OFFSET_TYPE:
case FUNCTION_TYPE:
case METHOD_TYPE:
case ARRAY_TYPE:
case TYPENAME_TYPE:
case TYPE_DECL:
return tsubst (t, args, complain, in_decl);
case IDENTIFIER_NODE:
if (IDENTIFIER_TYPENAME_P (t)
&& TREE_TYPE (t))
return build_typename_overload
(tsubst (TREE_TYPE (t), args, complain, in_decl));
else
return t;
case CONSTRUCTOR:
{
r = build
(CONSTRUCTOR, tsubst (TREE_TYPE (t), args, complain, in_decl),
NULL_TREE, tsubst_copy (CONSTRUCTOR_ELTS (t), args,
complain, in_decl));
TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
return r;
}
default:
return t;
}
}
tree
tsubst_expr (t, args, complain, in_decl)
tree t, args;
int complain;
tree in_decl;
{
if (t == NULL_TREE || t == error_mark_node)
return t;
if (processing_template_decl)
return tsubst_copy (t, args, complain, in_decl);
switch (TREE_CODE (t))
{
case RETURN_STMT:
lineno = TREE_COMPLEXITY (t);
finish_return_stmt (tsubst_expr (RETURN_EXPR (t),
args, complain, in_decl));
break;
case EXPR_STMT:
lineno = TREE_COMPLEXITY (t);
finish_expr_stmt (tsubst_expr (EXPR_STMT_EXPR (t),
args, complain, in_decl));
break;
case DECL_STMT:
{
int i = suspend_momentary ();
tree dcl, init;
lineno = TREE_COMPLEXITY (t);
emit_line_note (input_filename, lineno);
dcl = start_decl
(tsubst (TREE_OPERAND (t, 0), args, complain, in_decl),
tsubst (TREE_OPERAND (t, 1), args, complain, in_decl),
TREE_OPERAND (t, 2) != 0, NULL_TREE, NULL_TREE);
init = tsubst_expr (TREE_OPERAND (t, 2), args, complain, in_decl);
cp_finish_decl
(dcl, init, NULL_TREE, 1, 0);
resume_momentary (i);
return dcl;
}
case FOR_STMT:
{
tree tmp;
lineno = TREE_COMPLEXITY (t);
begin_for_stmt ();
for (tmp = FOR_INIT_STMT (t); tmp; tmp = TREE_CHAIN (tmp))
tsubst_expr (tmp, args, complain, in_decl);
finish_for_init_stmt (NULL_TREE);
finish_for_cond (tsubst_expr (FOR_COND (t), args,
complain, in_decl),
NULL_TREE);
tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
finish_for_expr (tmp, NULL_TREE);
tsubst_expr (FOR_BODY (t), args, complain, in_decl);
finish_for_stmt (tmp, NULL_TREE);
}
break;
case WHILE_STMT:
{
lineno = TREE_COMPLEXITY (t);
begin_while_stmt ();
finish_while_stmt_cond (tsubst_expr (WHILE_COND (t),
args, complain, in_decl),
NULL_TREE);
tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
finish_while_stmt (NULL_TREE);
}
break;
case DO_STMT:
{
lineno = TREE_COMPLEXITY (t);
begin_do_stmt ();
tsubst_expr (DO_BODY (t), args, complain, in_decl);
finish_do_body (NULL_TREE);
finish_do_stmt (tsubst_expr (DO_COND (t), args,
complain, in_decl),
NULL_TREE);
}
break;
case IF_STMT:
{
tree tmp;
lineno = TREE_COMPLEXITY (t);
begin_if_stmt ();
finish_if_stmt_cond (tsubst_expr (IF_COND (t),
args, complain, in_decl),
NULL_TREE);
if (tmp = THEN_CLAUSE (t), tmp)
{
tsubst_expr (tmp, args, complain, in_decl);
finish_then_clause (NULL_TREE);
}
if (tmp = ELSE_CLAUSE (t), tmp)
{
begin_else_clause ();
tsubst_expr (tmp, args, complain, in_decl);
finish_else_clause (NULL_TREE);
}
finish_if_stmt ();
}
break;
case COMPOUND_STMT:
{
tree substmt;
lineno = TREE_COMPLEXITY (t);
begin_compound_stmt (COMPOUND_STMT_NO_SCOPE (t));
for (substmt = COMPOUND_BODY (t);
substmt != NULL_TREE;
substmt = TREE_CHAIN (substmt))
tsubst_expr (substmt, args, complain, in_decl);
return finish_compound_stmt (COMPOUND_STMT_NO_SCOPE (t),
NULL_TREE);
}
break;
case BREAK_STMT:
lineno = TREE_COMPLEXITY (t);
finish_break_stmt ();
break;
case CONTINUE_STMT:
lineno = TREE_COMPLEXITY (t);
finish_continue_stmt ();
break;
case SWITCH_STMT:
{
tree val, tmp;
lineno = TREE_COMPLEXITY (t);
begin_switch_stmt ();
val = tsubst_expr (SWITCH_COND (t), args, complain, in_decl);
finish_switch_cond (val);
if (tmp = TREE_OPERAND (t, 1), tmp)
tsubst_expr (tmp, args, complain, in_decl);
finish_switch_stmt (val, NULL_TREE);
}
break;
case CASE_LABEL:
finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
tsubst_expr (CASE_HIGH (t), args, complain, in_decl));
break;
case LABEL_DECL:
t = define_label (DECL_SOURCE_FILE (t), DECL_SOURCE_LINE (t),
DECL_NAME (t));
if (t)
expand_label (t);
break;
case GOTO_STMT:
lineno = TREE_COMPLEXITY (t);
t = GOTO_DESTINATION (t);
if (TREE_CODE (t) != IDENTIFIER_NODE)
t = tsubst_expr (t, args, complain, in_decl);
finish_goto_stmt (t);
break;
case ASM_STMT:
lineno = TREE_COMPLEXITY (t);
finish_asm_stmt (tsubst_expr (ASM_CV_QUAL (t), args, complain, in_decl),
tsubst_expr (ASM_STRING (t), args, complain, in_decl),
tsubst_expr (ASM_OUTPUTS (t), args, complain, in_decl),
tsubst_expr (ASM_INPUTS (t), args, complain, in_decl),
tsubst_expr (ASM_CLOBBERS (t), args, complain,
in_decl));
break;
case TRY_BLOCK:
lineno = TREE_COMPLEXITY (t);
begin_try_block ();
tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
finish_try_block (NULL_TREE);
{
tree handler = TRY_HANDLERS (t);
for (; handler; handler = TREE_CHAIN (handler))
tsubst_expr (handler, args, complain, in_decl);
}
finish_handler_sequence (NULL_TREE);
break;
case HANDLER:
lineno = TREE_COMPLEXITY (t);
begin_handler ();
if (HANDLER_PARMS (t))
{
tree d = HANDLER_PARMS (t);
expand_start_catch_block
(tsubst (TREE_OPERAND (d, 1), args, complain, in_decl),
tsubst (TREE_OPERAND (d, 0), args, complain, in_decl));
}
else
expand_start_catch_block (NULL_TREE, NULL_TREE);
finish_handler_parms (NULL_TREE);
tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
finish_handler (NULL_TREE);
break;
case TAG_DEFN:
lineno = TREE_COMPLEXITY (t);
t = TREE_TYPE (t);
if (TREE_CODE (t) == ENUMERAL_TYPE)
tsubst (t, args, complain, NULL_TREE);
break;
default:
return build_expr_from_tree (tsubst_copy (t, args, complain, in_decl));
}
return NULL_TREE;
}
tree
instantiate_template (tmpl, targ_ptr)
tree tmpl, targ_ptr;
{
tree fndecl;
tree gen_tmpl;
tree spec;
int i, len;
struct obstack *old_fmp_obstack;
extern struct obstack *function_maybepermanent_obstack;
tree inner_args;
if (tmpl == error_mark_node)
return error_mark_node;
my_friendly_assert (TREE_CODE (tmpl) == TEMPLATE_DECL, 283);
spec = retrieve_specialization (tmpl, targ_ptr);
if (spec != NULL_TREE)
return spec;
if (DECL_TEMPLATE_INFO (tmpl))
{
targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
targ_ptr);
gen_tmpl = most_general_template (tmpl);
spec = retrieve_specialization (gen_tmpl, targ_ptr);
if (spec != NULL_TREE)
return spec;
}
else
gen_tmpl = tmpl;
push_obstacks (&permanent_obstack, &permanent_obstack);
old_fmp_obstack = function_maybepermanent_obstack;
function_maybepermanent_obstack = &permanent_obstack;
len = DECL_NTPARMS (gen_tmpl);
inner_args = innermost_args (targ_ptr);
i = len;
while (i--)
{
tree t = TREE_VEC_ELT (inner_args, i);
if (TREE_CODE_CLASS (TREE_CODE (t)) == 't')
{
tree nt = target_type (t);
if (IS_AGGR_TYPE (nt) && decl_function_context (TYPE_MAIN_DECL (nt)))
{
cp_error ("type `%T' composed from a local class is not a valid template-argument", t);
cp_error (" trying to instantiate `%D'", gen_tmpl);
fndecl = error_mark_node;
goto out;
}
}
}
targ_ptr = copy_to_permanent (targ_ptr);
fndecl = tsubst (DECL_RESULT (gen_tmpl), targ_ptr, 1, gen_tmpl);
DECL_TI_TEMPLATE (fndecl) = tmpl;
if (flag_external_templates)
add_pending_template (fndecl);
out:
function_maybepermanent_obstack = old_fmp_obstack;
pop_obstacks ();
return fndecl;
}
void
overload_template_name (type)
tree type;
{
tree id = DECL_NAME (CLASSTYPE_TI_TEMPLATE (type));
tree decl;
if (IDENTIFIER_CLASS_VALUE (id)
&& TREE_TYPE (IDENTIFIER_CLASS_VALUE (id)) == type)
return;
decl = build_decl (TYPE_DECL, id, type);
SET_DECL_ARTIFICIAL (decl);
pushdecl_class_level (decl);
}
int
fn_type_unification (fn, explicit_targs, targs, args, return_type,
strict)
tree fn, explicit_targs, targs, args, return_type;
unification_kind_t strict;
{
tree parms;
tree fntype;
my_friendly_assert (TREE_CODE (fn) == TEMPLATE_DECL, 0);
fntype = TREE_TYPE (fn);
if (explicit_targs)
{
int i;
tree converted_args;
converted_args
= (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
explicit_targs, NULL_TREE, 0,
0));
if (converted_args == error_mark_node)
return 1;
fntype = tsubst (fntype, converted_args, 0, NULL_TREE);
if (fntype == error_mark_node)
return 1;
for (i = 0; i < TREE_VEC_LENGTH (targs); i++)
TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
}
parms = TYPE_ARG_TYPES (fntype);
if (DECL_CONV_FN_P (fn))
{
parms = scratch_tree_cons (NULL_TREE, TREE_TYPE (fntype),
TREE_CHAIN (parms));
args = scratch_tree_cons (NULL_TREE, return_type, TREE_CHAIN (args));
}
return type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
targs, parms, args, 0,
strict, 1);
}
static void
maybe_adjust_types_for_deduction (strict, parm, arg)
unification_kind_t strict;
tree* parm;
tree* arg;
{
switch (strict)
{
case DEDUCE_CALL:
break;
case DEDUCE_CONV:
{
tree* temp = parm;
parm = arg;
arg = temp;
break;
}
case DEDUCE_EXACT:
return;
default:
my_friendly_abort (0);
}
if (TREE_CODE (*parm) != REFERENCE_TYPE)
{
if (TREE_CODE (*arg) == ARRAY_TYPE)
*arg = build_pointer_type (TREE_TYPE (*arg));
else if (TREE_CODE (*arg) == FUNCTION_TYPE)
*arg = build_pointer_type (*arg);
else
*arg = TYPE_MAIN_VARIANT (*arg);
}
*parm = TYPE_MAIN_VARIANT (*parm);
if (TREE_CODE (*parm) == REFERENCE_TYPE)
*parm = TREE_TYPE (*parm);
}
static int
type_unification_real (tparms, targs, parms, args, subr,
strict, allow_incomplete)
tree tparms, targs, parms, args;
int subr;
unification_kind_t strict;
int allow_incomplete;
{
tree parm, arg;
int i;
int ntparms = TREE_VEC_LENGTH (tparms);
int sub_strict;
my_friendly_assert (TREE_CODE (tparms) == TREE_VEC, 289);
my_friendly_assert (parms == NULL_TREE
|| TREE_CODE (parms) == TREE_LIST, 290);
if (args)
my_friendly_assert (TREE_CODE (args) == TREE_LIST, 291);
my_friendly_assert (ntparms > 0, 292);
switch (strict)
{
case DEDUCE_CALL:
sub_strict = UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_DERIVED;
break;
case DEDUCE_CONV:
sub_strict = UNIFY_ALLOW_LESS_CV_QUAL;
break;
case DEDUCE_EXACT:
sub_strict = UNIFY_ALLOW_NONE;
break;
default:
my_friendly_abort (0);
}
while (parms
&& parms != void_list_node
&& args
&& args != void_list_node)
{
parm = TREE_VALUE (parms);
parms = TREE_CHAIN (parms);
arg = TREE_VALUE (args);
args = TREE_CHAIN (args);
if (arg == error_mark_node)
return 1;
if (arg == unknown_type_node)
continue;
if (! uses_template_parms (parm))
{
tree type;
if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
type = TREE_TYPE (arg);
else
{
type = arg;
arg = NULL_TREE;
}
if (strict == DEDUCE_EXACT)
{
if (same_type_p (parm, type))
continue;
}
else
continue;
return 1;
}
if (TREE_CODE_CLASS (TREE_CODE (arg)) != 't')
{
my_friendly_assert (TREE_TYPE (arg) != NULL_TREE, 293);
if (type_unknown_p (arg))
{
if (resolve_overloaded_unification
(tparms, targs, parm, arg, strict, sub_strict)
!= 0)
return 1;
continue;
}
arg = TREE_TYPE (arg);
}
if (!subr)
maybe_adjust_types_for_deduction (strict, &parm, &arg);
switch (unify (tparms, targs, parm, arg, sub_strict))
{
case 0:
break;
case 1:
return 1;
}
}
if (args && args != void_list_node && parms == void_list_node)
return 1;
if (parms
&& parms != void_list_node
&& TREE_PURPOSE (parms) == NULL_TREE)
return 1;
if (!subr)
for (i = 0; i < ntparms; i++)
if (TREE_VEC_ELT (targs, i) == NULL_TREE)
{
if (!allow_incomplete)
error ("incomplete type unification");
return 2;
}
return 0;
}
static int
resolve_overloaded_unification (tparms, targs, parm, arg, strict,
sub_strict)
tree tparms, targs, parm, arg;
unification_kind_t strict;
int sub_strict;
{
tree tempargs = copy_node (targs);
int good = 0;
if (TREE_CODE (arg) == ADDR_EXPR)
arg = TREE_OPERAND (arg, 0);
if (TREE_CODE (arg) == COMPONENT_REF)
arg = TREE_OPERAND (arg, 1);
if (TREE_CODE (arg) == OFFSET_REF)
arg = TREE_OPERAND (arg, 1);
while (TREE_CODE (arg) == TREE_LIST)
arg = TREE_VALUE (arg);
if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
{
tree expl_subargs = TREE_OPERAND (arg, 1);
arg = TREE_OPERAND (arg, 0);
for (; arg; arg = OVL_NEXT (arg))
{
tree fn = OVL_CURRENT (arg);
tree subargs, elem;
if (TREE_CODE (fn) != TEMPLATE_DECL)
continue;
subargs = get_bindings_overload (fn, DECL_RESULT (fn), expl_subargs);
if (subargs)
{
elem = tsubst (TREE_TYPE (fn), subargs, 0,
NULL_TREE);
if (TREE_CODE (elem) == METHOD_TYPE)
elem = build_ptrmemfunc_type (build_pointer_type (elem));
good += try_one_overload (tparms, targs, tempargs, parm, elem,
strict, sub_strict);
}
}
}
else if (TREE_CODE (arg) == OVERLOAD)
{
for (; arg; arg = OVL_NEXT (arg))
{
tree type = TREE_TYPE (OVL_CURRENT (arg));
if (TREE_CODE (type) == METHOD_TYPE)
type = build_ptrmemfunc_type (build_pointer_type (type));
good += try_one_overload (tparms, targs, tempargs, parm,
type,
strict, sub_strict);
}
}
else
my_friendly_abort (981006);
if (good == 1)
{
int i = TREE_VEC_LENGTH (targs);
for (; i--; )
if (TREE_VEC_ELT (tempargs, i))
TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (tempargs, i);
}
if (good)
return 0;
return 1;
}
static int
try_one_overload (tparms, orig_targs, targs, parm, arg, strict,
sub_strict)
tree tparms, orig_targs, targs, parm, arg;
unification_kind_t strict;
int sub_strict;
{
int nargs;
tree tempargs;
int i;
if (uses_template_parms (arg))
return 1;
maybe_adjust_types_for_deduction (strict, &parm, &arg);
nargs = TREE_VEC_LENGTH (targs);
tempargs = make_scratch_vec (nargs);
if (unify (tparms, tempargs, parm, arg, sub_strict) != 0)
return 0;
for (i = nargs; i--; )
{
tree elt = TREE_VEC_ELT (tempargs, i);
tree oldelt = TREE_VEC_ELT (orig_targs, i);
if (elt == NULL_TREE)
continue;
else if (uses_template_parms (elt))
{
TREE_VEC_ELT (tempargs, i) = NULL_TREE;
continue;
}
else if (oldelt && ! template_args_equal (oldelt, elt))
return 0;
}
for (i = nargs; i--; )
{
tree elt = TREE_VEC_ELT (tempargs, i);
if (elt)
TREE_VEC_ELT (targs, i) = elt;
}
return 1;
}
static tree
try_class_unification (tparms, targs, parm, arg)
tree tparms;
tree targs;
tree parm;
tree arg;
{
int i;
tree copy_of_targs;
if (!CLASSTYPE_TEMPLATE_INFO (arg)
|| CLASSTYPE_TI_TEMPLATE (arg) != CLASSTYPE_TI_TEMPLATE (parm))
return NULL_TREE;
push_momentary ();
copy_of_targs = make_temp_vec (TREE_VEC_LENGTH (targs));
i = unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE);
pop_momentary ();
if (i != 0)
return NULL_TREE;
else
return arg;
}
static tree
get_template_base_recursive (tparms, targs, parm,
arg_binfo, rval, flags)
tree tparms;
tree targs;
tree arg_binfo;
tree rval;
tree parm;
int flags;
{
tree binfos;
int i, n_baselinks;
tree arg = BINFO_TYPE (arg_binfo);
if (!(flags & GTB_IGNORE_TYPE))
{
tree r = try_class_unification (tparms, targs,
parm, arg);
if (r && rval && !same_type_p (r, rval))
return error_mark_node;
else if (r)
rval = r;
}
binfos = BINFO_BASETYPES (arg_binfo);
n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
for (i = 0; i < n_baselinks; i++)
{
tree base_binfo = TREE_VEC_ELT (binfos, i);
int this_virtual;
if (BINFO_MARKED (base_binfo))
continue;
this_virtual =
(flags & GTB_VIA_VIRTUAL) || TREE_VIA_VIRTUAL (base_binfo);
if (! this_virtual)
SET_BINFO_MARKED (base_binfo);
rval = get_template_base_recursive (tparms, targs,
parm,
base_binfo,
rval,
GTB_VIA_VIRTUAL * this_virtual);
if (rval == error_mark_node)
return error_mark_node;
}
return rval;
}
static tree
get_template_base (tparms, targs, parm, arg)
tree tparms;
tree targs;
tree parm;
tree arg;
{
tree rval;
tree arg_binfo;
my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)), 92);
arg_binfo = TYPE_BINFO (complete_type (arg));
rval = get_template_base_recursive (tparms, targs,
parm, arg_binfo,
NULL_TREE,
GTB_IGNORE_TYPE);
dfs_walk (arg_binfo, dfs_unmark, markedp, 0);
return rval;
}
static int
template_decl_level (decl)
tree decl;
{
switch (TREE_CODE (decl))
{
case TYPE_DECL:
case TEMPLATE_DECL:
return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
case PARM_DECL:
return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
default:
my_friendly_abort (0);
return 0;
}
}
static int
check_cv_quals_for_unify (strict, arg, parm)
int strict;
tree arg;
tree parm;
{
return !((!(strict & UNIFY_ALLOW_MORE_CV_QUAL)
&& !at_least_as_qualified_p (arg, parm))
|| (!(strict & UNIFY_ALLOW_LESS_CV_QUAL)
&& (!at_least_as_qualified_p (parm, arg))));
}
static int
unify (tparms, targs, parm, arg, strict)
tree tparms, targs, parm, arg;
int strict;
{
int idx;
tree targ;
tree tparm;
while (TREE_CODE (parm) == NOP_EXPR)
parm = TREE_OPERAND (parm, 0);
if (arg == error_mark_node)
return 1;
if (arg == unknown_type_node)
return 0;
if (arg == parm && !uses_template_parms (parm))
return 0;
if (TREE_CODE (arg) == TREE_CODE (parm)
&& TREE_CODE_CLASS (TREE_CODE (arg)) == 't'
&& TREE_CODE (arg) != TEMPLATE_TYPE_PARM
&& !check_cv_quals_for_unify (strict, arg, parm))
return 1;
switch (TREE_CODE (parm))
{
case TYPENAME_TYPE:
return 0;
case TEMPLATE_TYPE_PARM:
case TEMPLATE_TEMPLATE_PARM:
tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
if (TEMPLATE_TYPE_LEVEL (parm)
!= template_decl_level (tparm))
return (TREE_CODE (arg) == TREE_CODE (parm)
&& same_type_p (parm, arg)) ? 0 : 1;
idx = TEMPLATE_TYPE_IDX (parm);
targ = TREE_VEC_ELT (targs, idx);
tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
&& TREE_CODE (tparm) != TYPE_DECL)
|| (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
&& TREE_CODE (tparm) != TEMPLATE_DECL))
return 1;
if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
{
if (TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (parm))
{
if (TREE_CODE (arg) != RECORD_TYPE || !CLASSTYPE_TEMPLATE_INFO (arg))
return 1;
{
tree parmtmpl = TYPE_TI_TEMPLATE (parm);
tree parmvec = TYPE_TI_ARGS (parm);
tree argvec = CLASSTYPE_TI_ARGS (arg);
tree argtmplvec
= DECL_INNERMOST_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (arg));
int i;
if (coerce_template_parms (argtmplvec, parmvec, parmtmpl, 0, 1)
== error_mark_node)
return 1;
for (i = 0; i < TREE_VEC_LENGTH (parmvec); ++i)
{
tree t = TREE_VEC_ELT (parmvec, i);
if (unify (tparms, targs, t,
TREE_VEC_ELT (argvec, i),
UNIFY_ALLOW_NONE))
return 1;
}
}
arg = CLASSTYPE_TI_TEMPLATE (arg);
}
}
else
{
if (!check_cv_quals_for_unify (strict | UNIFY_ALLOW_LESS_CV_QUAL,
arg, parm))
return 1;
arg =
cp_build_qualified_type (arg,
CP_TYPE_QUALS (arg)
& ~CP_TYPE_QUALS (parm));
}
if (targ != NULL_TREE && same_type_p (targ, arg))
return 0;
else if (targ)
return 1;
if (TREE_CODE (arg) == ARRAY_TYPE
&& !uses_template_parms (arg))
if (TYPE_DOMAIN (arg) == NULL_TREE)
{
cp_error
("pointer/reference to array of unknown bound in parm type");
return 1;
}
else if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (arg))) != INTEGER_CST)
return 1;
TREE_VEC_ELT (targs, idx) = arg;
return 0;
case TEMPLATE_PARM_INDEX:
tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
if (TEMPLATE_PARM_LEVEL (parm)
!= template_decl_level (tparm))
return (TREE_CODE (arg) == TREE_CODE (parm)
&& cp_tree_equal (parm, arg) > 0) ? 0 : 1;
idx = TEMPLATE_PARM_IDX (parm);
targ = TREE_VEC_ELT (targs, idx);
if (targ)
{
int i = (cp_tree_equal (targ, arg) > 0);
if (i == 1)
return 0;
else if (i == 0)
return 1;
else
my_friendly_abort (42);
}
if (same_type_p (TREE_TYPE (arg), TREE_TYPE (parm)))
;
else if ((strict & UNIFY_ALLOW_INTEGER)
&& (TREE_CODE (TREE_TYPE (parm)) == INTEGER_TYPE
|| TREE_CODE (TREE_TYPE (parm)) == BOOLEAN_TYPE))
;
else
return 1;
TREE_VEC_ELT (targs, idx) = copy_to_permanent (arg);
return 0;
case POINTER_TYPE:
{
int sub_strict;
if (TREE_CODE (arg) == RECORD_TYPE && TYPE_PTRMEMFUNC_FLAG (arg))
return (unify (tparms, targs, parm,
TYPE_PTRMEMFUNC_FN_TYPE (arg), strict));
if (TREE_CODE (arg) != POINTER_TYPE)
return 1;
sub_strict = strict;
if (TREE_CODE (TREE_TYPE (arg)) != RECORD_TYPE
|| TYPE_PTRMEMFUNC_FLAG (TREE_TYPE (arg)))
sub_strict &= ~UNIFY_ALLOW_DERIVED;
return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE
(arg), sub_strict);
}
case REFERENCE_TYPE:
if (TREE_CODE (arg) != REFERENCE_TYPE)
return 1;
return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
UNIFY_ALLOW_NONE);
case ARRAY_TYPE:
if (TREE_CODE (arg) != ARRAY_TYPE)
return 1;
if ((TYPE_DOMAIN (parm) == NULL_TREE)
!= (TYPE_DOMAIN (arg) == NULL_TREE))
return 1;
if (TYPE_DOMAIN (parm) != NULL_TREE
&& unify (tparms, targs, TYPE_DOMAIN (parm),
TYPE_DOMAIN (arg), UNIFY_ALLOW_NONE) != 0)
return 1;
return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
UNIFY_ALLOW_NONE);
case REAL_TYPE:
case COMPLEX_TYPE:
case VECTOR_TYPE:
case INTEGER_TYPE:
case BOOLEAN_TYPE:
case VOID_TYPE:
if (TREE_CODE (arg) != TREE_CODE (parm))
return 1;
if (TREE_CODE (parm) == INTEGER_TYPE
&& TREE_CODE (TYPE_MAX_VALUE (parm)) != INTEGER_CST)
{
if (TYPE_MIN_VALUE (parm) && TYPE_MIN_VALUE (arg)
&& unify (tparms, targs, TYPE_MIN_VALUE (parm),
TYPE_MIN_VALUE (arg), UNIFY_ALLOW_INTEGER))
return 1;
if (TYPE_MAX_VALUE (parm) && TYPE_MAX_VALUE (arg)
&& unify (tparms, targs, TYPE_MAX_VALUE (parm),
TYPE_MAX_VALUE (arg), UNIFY_ALLOW_INTEGER))
return 1;
}
else if (!same_type_p (TYPE_MAIN_VARIANT (arg),
TYPE_MAIN_VARIANT (parm)))
return 1;
return 0;
case INTEGER_CST:
while (TREE_CODE (arg) == NOP_EXPR)
arg = TREE_OPERAND (arg, 0);
if (TREE_CODE (arg) != INTEGER_CST)
return 1;
return !tree_int_cst_equal (parm, arg);
case TREE_VEC:
{
int i;
if (TREE_CODE (arg) != TREE_VEC)
return 1;
if (TREE_VEC_LENGTH (parm) != TREE_VEC_LENGTH (arg))
return 1;
for (i = TREE_VEC_LENGTH (parm) - 1; i >= 0; i--)
if (unify (tparms, targs,
TREE_VEC_ELT (parm, i), TREE_VEC_ELT (arg, i),
UNIFY_ALLOW_NONE))
return 1;
return 0;
}
case RECORD_TYPE:
case UNION_TYPE:
if (TYPE_PTRMEMFUNC_FLAG (parm))
return unify (tparms, targs, TYPE_PTRMEMFUNC_FN_TYPE (parm),
arg, strict);
if (TREE_CODE (arg) != TREE_CODE (parm))
return 1;
if (CLASSTYPE_TEMPLATE_INFO (parm))
{
tree t = NULL_TREE;
if (strict & UNIFY_ALLOW_DERIVED)
{
t = try_class_unification (tparms, targs,
parm, arg);
if (!t)
{
t = get_template_base (tparms, targs,
parm, arg);
if (! t || t == error_mark_node)
return 1;
}
}
else if (CLASSTYPE_TEMPLATE_INFO (arg)
&& (CLASSTYPE_TI_TEMPLATE (parm)
== CLASSTYPE_TI_TEMPLATE (arg)))
t = arg;
else
return 1;
return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE);
}
else if (!same_type_p (TYPE_MAIN_VARIANT (parm),
TYPE_MAIN_VARIANT (arg)))
return 1;
return 0;
case METHOD_TYPE:
case FUNCTION_TYPE:
if (TREE_CODE (arg) != TREE_CODE (parm))
return 1;
if (unify (tparms, targs, TREE_TYPE (parm),
TREE_TYPE (arg), UNIFY_ALLOW_NONE))
return 1;
return type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
TYPE_ARG_TYPES (arg), 1,
DEDUCE_EXACT, 0);
case OFFSET_TYPE:
if (TREE_CODE (arg) != OFFSET_TYPE)
return 1;
if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
TYPE_OFFSET_BASETYPE (arg), UNIFY_ALLOW_NONE))
return 1;
return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
strict);
case CONST_DECL:
if (arg != decl_constant_value (parm))
return 1;
return 0;
case TEMPLATE_DECL:
return 1;
case MINUS_EXPR:
if (TREE_CODE (TREE_OPERAND (parm, 1)) == INTEGER_CST)
{
tree t, t1, t2;
t1 = TREE_OPERAND (parm, 0);
t2 = TREE_OPERAND (parm, 1);
t = maybe_fold_nontype_arg (build (PLUS_EXPR,
integer_type_node,
arg, t2));
return unify (tparms, targs, t1, t, strict);
}
default:
if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (parm))))
return 0;
else
sorry ("use of `%s' in template type unification",
tree_code_name [(int) TREE_CODE (parm)]);
return 1;
}
}
void
mark_decl_instantiated (result, extern_p)
tree result;
int extern_p;
{
if (TREE_CODE (result) != FUNCTION_DECL)
TREE_PUBLIC (result) = 1;
if (! extern_p)
{
DECL_INTERFACE_KNOWN (result) = 1;
DECL_NOT_REALLY_EXTERN (result) = 1;
if (DECL_ARTIFICIAL (result) && flag_weak)
comdat_linkage (result);
else if (TREE_PUBLIC (result))
maybe_make_one_only (result);
}
else if (TREE_CODE (result) == FUNCTION_DECL)
mark_inline_for_output (result);
}
int
more_specialized (pat1, pat2, explicit_args)
tree pat1, pat2, explicit_args;
{
tree targs;
int winner = 0;
targs = get_bindings_overload (pat1, DECL_RESULT (pat2), explicit_args);
if (targs)
--winner;
targs = get_bindings_overload (pat2, DECL_RESULT (pat1), explicit_args);
if (targs)
++winner;
return winner;
}
int
more_specialized_class (pat1, pat2)
tree pat1, pat2;
{
tree targs;
int winner = 0;
targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
TREE_PURPOSE (pat2));
if (targs)
--winner;
targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
TREE_PURPOSE (pat1));
if (targs)
++winner;
return winner;
}
static tree
get_bindings_real (fn, decl, explicit_args, check_rettype)
tree fn, decl, explicit_args;
int check_rettype;
{
int ntparms = DECL_NTPARMS (fn);
tree targs = make_scratch_vec (ntparms);
tree decl_type;
tree decl_arg_types;
int i;
decl_type = TREE_TYPE (decl);
if (explicit_args && uses_template_parms (decl_type))
{
tree tmpl;
tree converted_args;
if (DECL_TEMPLATE_INFO (decl))
tmpl = DECL_TI_TEMPLATE (decl);
else
return NULL_TREE;
converted_args
= (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
explicit_args, NULL_TREE,
0,
0));
if (converted_args == error_mark_node)
return NULL_TREE;
decl_type = tsubst (decl_type, converted_args, 0,
NULL_TREE);
if (decl_type == error_mark_node)
return NULL_TREE;
}
decl_arg_types = TYPE_ARG_TYPES (decl_type);
if (DECL_STATIC_FUNCTION_P (fn)
&& DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
decl_arg_types = TREE_CHAIN (decl_arg_types);
i = fn_type_unification (fn, explicit_args, targs,
decl_arg_types,
TREE_TYPE (decl_type),
DEDUCE_EXACT);
if (i != 0)
return NULL_TREE;
if (check_rettype)
{
tree t = tsubst (TREE_TYPE (TREE_TYPE (fn)), targs,
0, NULL_TREE);
if (!same_type_p (t, TREE_TYPE (TREE_TYPE (decl))))
return NULL_TREE;
}
return targs;
}
tree
get_bindings (fn, decl, explicit_args)
tree fn, decl, explicit_args;
{
return get_bindings_real (fn, decl, explicit_args, 1);
}
static tree
get_bindings_overload (fn, decl, explicit_args)
tree fn, decl, explicit_args;
{
return get_bindings_real (fn, decl, explicit_args, 0);
}
static tree
get_class_bindings (tparms, parms, args)
tree tparms, parms, args;
{
int i, ntparms = TREE_VEC_LENGTH (tparms);
tree vec = make_temp_vec (ntparms);
args = innermost_args (args);
if (unify (tparms, vec, parms, args, UNIFY_ALLOW_NONE))
return NULL_TREE;
for (i = 0; i < ntparms; ++i)
if (! TREE_VEC_ELT (vec, i))
return NULL_TREE;
return vec;
}
tree
most_specialized_instantiation (instantiations, explicit_args)
tree instantiations;
tree explicit_args;
{
tree fn, champ;
int fate;
if (!instantiations)
return NULL_TREE;
champ = instantiations;
for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
{
fate = more_specialized (TREE_VALUE (champ),
TREE_VALUE (fn), explicit_args);
if (fate == 1)
;
else
{
if (fate == 0)
{
fn = TREE_CHAIN (fn);
if (! fn)
return error_mark_node;
}
champ = fn;
}
}
for (fn = instantiations; fn && fn != champ; fn = TREE_CHAIN (fn))
{
fate = more_specialized (TREE_VALUE (champ),
TREE_VALUE (fn), explicit_args);
if (fate != 1)
return error_mark_node;
}
return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
}
static tree
most_specialized (fns, decl, explicit_args)
tree fns, decl, explicit_args;
{
tree candidates = NULL_TREE;
tree fn, args;
for (fn = fns; fn; fn = TREE_CHAIN (fn))
{
tree candidate = TREE_VALUE (fn);
args = get_bindings (candidate, decl, explicit_args);
if (args)
candidates = scratch_tree_cons (NULL_TREE, candidate,
candidates);
}
return most_specialized_instantiation (candidates, explicit_args);
}
static tree
most_general_template (decl)
tree decl;
{
while (DECL_TEMPLATE_INFO (decl))
decl = DECL_TI_TEMPLATE (decl);
return decl;
}
static tree
most_specialized_class (tmpl, args)
tree tmpl;
tree args;
{
tree list = NULL_TREE;
tree t;
tree champ;
int fate;
tmpl = most_general_template (tmpl);
for (t = DECL_TEMPLATE_SPECIALIZATIONS (tmpl); t; t = TREE_CHAIN (t))
{
tree spec_args
= get_class_bindings (TREE_VALUE (t), TREE_PURPOSE (t), args);
if (spec_args)
{
list = decl_tree_cons (TREE_PURPOSE (t), TREE_VALUE (t), list);
TREE_TYPE (list) = TREE_TYPE (t);
}
}
if (! list)
return NULL_TREE;
t = list;
champ = t;
t = TREE_CHAIN (t);
for (; t; t = TREE_CHAIN (t))
{
fate = more_specialized_class (champ, t);
if (fate == 1)
;
else
{
if (fate == 0)
{
t = TREE_CHAIN (t);
if (! t)
return error_mark_node;
}
champ = t;
}
}
for (t = list; t && t != champ; t = TREE_CHAIN (t))
{
fate = more_specialized_class (champ, t);
if (fate != 1)
return error_mark_node;
}
return champ;
}
void
do_decl_instantiation (declspecs, declarator, storage)
tree declspecs, declarator, storage;
{
tree decl = grokdeclarator (declarator, declspecs, NORMAL, 0, NULL_TREE);
tree result = NULL_TREE;
int extern_p = 0;
if (! DECL_LANG_SPECIFIC (decl))
{
cp_error ("explicit instantiation of non-template `%#D'", decl);
return;
}
else if (TREE_CODE (decl) == VAR_DECL)
{
result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, 0);
if (result && TREE_CODE (result) != VAR_DECL)
{
cp_error ("no matching template for `%D' found", result);
return;
}
}
else if (TREE_CODE (decl) != FUNCTION_DECL)
{
cp_error ("explicit instantiation of `%#D'", decl);
return;
}
else
result = decl;
if (DECL_TEMPLATE_SPECIALIZATION (result))
{
cp_pedwarn ("explicit instantiation of `%#D' after", result);
cp_pedwarn_at ("explicit specialization here", result);
return;
}
else if (DECL_EXPLICIT_INSTANTIATION (result))
{
if (DECL_INTERFACE_KNOWN (result) && !extern_p && !flag_use_repository)
cp_pedwarn ("duplicate explicit instantiation of `%#D'", result);
if (DECL_INTERFACE_KNOWN (result))
return;
}
else if (!DECL_IMPLICIT_INSTANTIATION (result))
{
cp_error ("no matching template for `%D' found", result);
return;
}
else if (!DECL_TEMPLATE_INFO (result))
{
cp_pedwarn ("explicit instantiation of non-template `%#D'", result);
return;
}
#ifdef MARK_TEMPLATE_COALESCED
MARK_TEMPLATE_COALESCED (result);
#endif
if (flag_external_templates)
return;
if (storage == NULL_TREE)
;
else if (storage == ridpointers[(int) RID_EXTERN])
{
if (pedantic)
cp_pedwarn ("ANSI C++ forbids the use of `extern' on explicit instantiations");
extern_p = 1;
}
else
cp_error ("storage class `%D' applied to template instantiation",
storage);
SET_DECL_EXPLICIT_INSTANTIATION (result);
mark_decl_instantiated (result, extern_p);
repo_template_instantiated (result, extern_p);
if (! extern_p)
instantiate_decl (result);
}
void
mark_class_instantiated (t, extern_p)
tree t;
int extern_p;
{
SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
SET_CLASSTYPE_INTERFACE_KNOWN (t);
CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
CLASSTYPE_VTABLE_NEEDS_WRITING (t) = ! extern_p;
TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
if (! extern_p)
{
CLASSTYPE_DEBUG_REQUESTED (t) = 1;
rest_of_type_compilation (t, 1);
}
}
void
do_type_instantiation (t, storage)
tree t, storage;
{
int extern_p = 0;
int nomem_p = 0;
int static_p = 0;
if (TREE_CODE (t) == TYPE_DECL)
t = TREE_TYPE (t);
if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
{
cp_error ("explicit instantiation of non-template type `%T'", t);
return;
}
complete_type (t);
if (flag_external_templates)
return;
if (TYPE_SIZE (t) == NULL_TREE)
{
cp_error ("explicit instantiation of `%#T' before definition of template",
t);
return;
}
if (storage != NULL_TREE)
{
if (pedantic)
cp_pedwarn("ANSI C++ forbids the use of `%s' on explicit instantiations",
IDENTIFIER_POINTER (storage));
if (storage == ridpointers[(int) RID_INLINE])
nomem_p = 1;
else if (storage == ridpointers[(int) RID_EXTERN])
extern_p = 1;
else if (storage == ridpointers[(int) RID_STATIC])
static_p = 1;
else
{
cp_error ("storage class `%D' applied to template instantiation",
storage);
extern_p = 0;
}
}
if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
{
cp_error ("explicit instantiation of `%#T' after", t);
cp_error_at ("explicit specialization here", t);
return;
}
else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
{
if (!CLASSTYPE_INTERFACE_ONLY (t) && !extern_p && !flag_use_repository)
cp_pedwarn ("duplicate explicit instantiation of `%#T'", t);
if (!CLASSTYPE_INTERFACE_ONLY (t))
return;
}
mark_class_instantiated (t, extern_p);
repo_template_instantiated (t, extern_p);
if (nomem_p)
return;
{
tree tmp;
if (! static_p)
for (tmp = TYPE_METHODS (t); tmp; tmp = TREE_CHAIN (tmp))
if (TREE_CODE (tmp) == FUNCTION_DECL
&& DECL_TEMPLATE_INSTANTIATION (tmp))
{
mark_decl_instantiated (tmp, extern_p);
repo_template_instantiated (tmp, extern_p);
if (! extern_p)
instantiate_decl (tmp);
}
for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
{
mark_decl_instantiated (tmp, extern_p);
repo_template_instantiated (tmp, extern_p);
if (! extern_p)
instantiate_decl (tmp);
}
for (tmp = CLASSTYPE_TAGS (t); tmp; tmp = TREE_CHAIN (tmp))
if (IS_AGGR_TYPE (TREE_VALUE (tmp))
&& !uses_template_parms (CLASSTYPE_TI_ARGS (TREE_VALUE (tmp))))
do_type_instantiation (TYPE_MAIN_DECL (TREE_VALUE (tmp)), storage);
}
}
static void
regenerate_decl_from_template (decl, tmpl)
tree decl;
tree tmpl;
{
tree args;
tree code_pattern;
tree new_decl;
tree gen_tmpl;
int unregistered;
args = DECL_TI_ARGS (decl);
code_pattern = DECL_TEMPLATE_RESULT (tmpl);
gen_tmpl = most_general_template (tmpl);
unregistered = unregister_specialization (decl, gen_tmpl);
my_friendly_assert (unregistered, 0);
if (TREE_CODE (decl) == VAR_DECL)
pushclass (DECL_CONTEXT (decl), 2);
new_decl = tsubst (code_pattern, args, 1, NULL_TREE);
if (TREE_CODE (decl) == VAR_DECL)
{
DECL_INITIAL (new_decl) =
tsubst_expr (DECL_INITIAL (code_pattern), args,
1, DECL_TI_TEMPLATE (decl));
popclass ();
}
else if (TREE_CODE (decl) == FUNCTION_DECL)
{
DECL_INITIAL (new_decl) = error_mark_node;
DECL_INITIAL (decl) = NULL_TREE;
}
DECL_TI_TEMPLATE (new_decl) = DECL_TI_TEMPLATE (decl);
DECL_ASSEMBLER_NAME (new_decl) = DECL_ASSEMBLER_NAME (decl);
DECL_RTL (new_decl) = DECL_RTL (decl);
duplicate_decls (new_decl, decl);
register_specialization (decl, gen_tmpl, args);
}
tree
instantiate_decl (d)
tree d;
{
tree tmpl = DECL_TI_TEMPLATE (d);
tree args = DECL_TI_ARGS (d);
tree td;
tree code_pattern;
tree spec;
tree gen_tmpl;
int nested = in_function_p ();
int pattern_defined;
int line = lineno;
char *file = input_filename;
my_friendly_assert (TREE_CODE (d) == FUNCTION_DECL
|| TREE_CODE (d) == VAR_DECL, 0);
if (DECL_TEMPLATE_INSTANTIATED (d))
return d;
gen_tmpl = most_general_template (tmpl);
spec = retrieve_specialization (gen_tmpl, args);
if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
return spec;
if (! push_tinst_level (d))
return d;
td = tmpl;
for (td = tmpl;
DECL_TEMPLATE_INSTANTIATION (td)
|| (TREE_CODE (d) == FUNCTION_DECL
&& DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (td)
&& !DECL_INITIAL (DECL_TEMPLATE_RESULT (td)));
)
{
my_friendly_assert (!(TREE_CODE (d) == VAR_DECL
&& !DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (td))),
0);
td = DECL_TI_TEMPLATE (td);
}
code_pattern = DECL_TEMPLATE_RESULT (td);
if (TREE_CODE (d) == FUNCTION_DECL)
pattern_defined = (DECL_INITIAL (code_pattern) != NULL_TREE);
else
pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
push_to_top_level ();
lineno = DECL_SOURCE_LINE (d);
input_filename = DECL_SOURCE_FILE (d);
if (pattern_defined)
{
repo_template_used (d);
if (flag_external_templates && ! DECL_INTERFACE_KNOWN (d))
{
if (flag_alt_external_templates)
{
if (interface_unknown)
warn_if_unknown_interface (d);
}
else if (DECL_INTERFACE_KNOWN (code_pattern))
{
DECL_INTERFACE_KNOWN (d) = 1;
DECL_NOT_REALLY_EXTERN (d) = ! DECL_EXTERNAL (code_pattern);
}
else
warn_if_unknown_interface (code_pattern);
}
if (at_eof)
import_export_decl (d);
}
if (DECL_INTERFACE_KNOWN (d)
&& ! DECL_NOT_REALLY_EXTERN (d)
&& ! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d)))
goto out;
if (TREE_CODE (d) == VAR_DECL
&& TREE_READONLY (d)
&& DECL_INITIAL (d) == NULL_TREE
&& DECL_INITIAL (code_pattern) != NULL_TREE)
;
else if (! pattern_defined
|| (! (TREE_CODE (d) == FUNCTION_DECL && DECL_INLINE (d) && nested)
&& ! at_eof))
{
lineno = line;
input_filename = file;
if (at_eof && !pattern_defined
&& DECL_EXPLICIT_INSTANTIATION (d))
cp_error ("explicit instantiation of `%D' but no definition available",
d);
add_pending_template (d);
goto out;
}
DECL_TEMPLATE_INSTANTIATED (d) = 1;
#ifdef MARK_TEMPLATE_COALESCED
MARK_TEMPLATE_COALESCED (d);
#endif
regenerate_decl_from_template (d, td);
lineno = DECL_SOURCE_LINE (d);
input_filename = DECL_SOURCE_FILE (d);
if (TREE_CODE (d) == VAR_DECL)
{
DECL_IN_AGGR_P (d) = 0;
if (DECL_INTERFACE_KNOWN (d))
DECL_EXTERNAL (d) = ! DECL_NOT_REALLY_EXTERN (d);
else
{
DECL_EXTERNAL (d) = 1;
DECL_NOT_REALLY_EXTERN (d) = 1;
}
cp_finish_decl (d, DECL_INITIAL (d), NULL_TREE, 0, 0);
}
else if (TREE_CODE (d) == FUNCTION_DECL)
{
tree t = DECL_SAVED_TREE (code_pattern);
start_function (NULL_TREE, d, NULL_TREE, 1);
store_parm_decls ();
if (t && TREE_CODE (t) == RETURN_INIT)
{
store_return_init
(TREE_OPERAND (t, 0),
tsubst_expr (TREE_OPERAND (t, 1), args, 1, tmpl));
t = TREE_CHAIN (t);
}
if (t && TREE_CODE (t) == CTOR_INITIALIZER)
{
current_member_init_list
= tsubst_expr_values (TREE_OPERAND (t, 0), args);
current_base_init_list
= tsubst_expr_values (TREE_OPERAND (t, 1), args);
t = TREE_CHAIN (t);
}
setup_vtbl_ptr ();
keep_next_level ();
my_friendly_assert (TREE_CODE (t) == COMPOUND_STMT, 42);
tsubst_expr (t, args, 1, tmpl);
finish_function (lineno, 0, nested);
}
out:
lineno = line;
input_filename = file;
pop_from_top_level ();
pop_tinst_level ();
return d;
}
int
instantiate_pending_templates ()
{
tree *t;
int instantiated_something = 0;
int reconsider;
do
{
reconsider = 0;
t = &pending_templates;
while (*t)
{
tree srcloc = TREE_PURPOSE (*t);
tree instantiation = TREE_VALUE (*t);
input_filename = SRCLOC_FILE (srcloc);
lineno = SRCLOC_LINE (srcloc);
if (TREE_CODE_CLASS (TREE_CODE (instantiation)) == 't')
{
tree fn;
if (!TYPE_SIZE (instantiation))
{
instantiate_class_template (instantiation);
if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
for (fn = TYPE_METHODS (instantiation);
fn;
fn = TREE_CHAIN (fn))
if (! DECL_ARTIFICIAL (fn))
instantiate_decl (fn);
if (TYPE_SIZE (instantiation))
{
instantiated_something = 1;
reconsider = 1;
}
}
if (TYPE_SIZE (instantiation))
*t = TREE_CHAIN (*t);
else
t = &TREE_CHAIN (*t);
}
else
{
if (DECL_TEMPLATE_INSTANTIATION (instantiation)
&& !DECL_TEMPLATE_INSTANTIATED (instantiation))
{
#ifdef HAVE_COALESCED_SYMBOLS
tree name = DECL_ASSEMBLER_NAME (instantiation);
#if 0
fprintf (stderr, "## '%s' ref'd %d, intf_known %d\n",
IDENTIFIER_POINTER (name),
TREE_SYMBOL_REFERENCED (name),
DECL_INTERFACE_KNOWN (instantiation));
#endif
if (TREE_SYMBOL_REFERENCED (name)
|| DECL_INTERFACE_KNOWN (instantiation)
|| flag_instantiate_unreferenced_templates
|| ! flag_implicit_templates)
#endif
instantiation = instantiate_decl (instantiation);
if (DECL_TEMPLATE_INSTANTIATED (instantiation))
{
instantiated_something = 1;
reconsider = 1;
}
}
if (!DECL_TEMPLATE_INSTANTIATION (instantiation)
|| DECL_TEMPLATE_INSTANTIATED (instantiation))
*t = TREE_CHAIN (*t);
else
t = &TREE_CHAIN (*t);
}
}
template_tail = t;
t = &maybe_templates;
while (*t)
{
tree template;
tree fn;
tree args;
fn = TREE_VALUE (*t);
if (DECL_INITIAL (fn))
;
else
{
template = TREE_PURPOSE (*t);
args = get_bindings (template, fn, NULL_TREE);
fn = instantiate_template (template, args);
instantiate_decl (fn);
reconsider = 1;
}
*t = TREE_CHAIN (*t);
}
maybe_template_tail = t;
}
while (reconsider);
return instantiated_something;
}
static tree
tsubst_expr_values (t, argvec)
tree t, argvec;
{
tree first = NULL_TREE;
tree *p = &first;
for (; t; t = TREE_CHAIN (t))
{
tree pur = tsubst_copy (TREE_PURPOSE (t), argvec,
1, NULL_TREE);
tree val = tsubst_expr (TREE_VALUE (t), argvec, 1,
NULL_TREE);
*p = build_tree_list (pur, val);
p = &TREE_CHAIN (*p);
}
return first;
}
tree last_tree;
void
add_tree (t)
tree t;
{
last_tree = TREE_CHAIN (last_tree) = t;
}
void
begin_tree ()
{
saved_trees = tree_cons (NULL_TREE, last_tree, saved_trees);
last_tree = NULL_TREE;
}
void
end_tree ()
{
my_friendly_assert (saved_trees != NULL_TREE, 0);
last_tree = TREE_VALUE (saved_trees);
saved_trees = TREE_CHAIN (saved_trees);
}
void
add_maybe_template (d, fns)
tree d, fns;
{
tree t;
if (DECL_MAYBE_TEMPLATE (d))
return;
t = most_specialized (fns, d, NULL_TREE);
if (! t)
return;
if (t == error_mark_node)
{
cp_error ("ambiguous template instantiation for `%D'", d);
return;
}
*maybe_template_tail = perm_tree_cons (t, d, NULL_TREE);
maybe_template_tail = &TREE_CHAIN (*maybe_template_tail);
DECL_MAYBE_TEMPLATE (d) = 1;
}
static void
set_current_access_from_decl (decl)
tree decl;
{
if (TREE_PRIVATE (decl))
current_access_specifier = access_private_node;
else if (TREE_PROTECTED (decl))
current_access_specifier = access_protected_node;
else
current_access_specifier = access_public_node;
}
static void
tsubst_enum (tag, newtag, args)
tree tag;
tree newtag;
tree args;
{
tree e;
for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
{
tree value;
tree elt;
value = tsubst_expr (DECL_INITIAL (TREE_VALUE (e)),
args, 1,
NULL_TREE);
set_current_access_from_decl (TREE_VALUE (e));
elt = build_enumerator (TREE_PURPOSE (e), value, newtag);
TREE_CHAIN (elt) = TYPE_VALUES (newtag);
TYPE_VALUES (newtag) = elt;
}
finish_enum (newtag);
}
static void
set_mangled_name_for_template_decl (decl)
tree decl;
{
tree saved_namespace;
tree context = NULL_TREE;
tree fn_type;
tree ret_type;
tree parm_types;
tree tparms;
tree targs;
tree tmpl;
int parm_depth;
my_friendly_assert (TREE_CODE (decl) == FUNCTION_DECL, 0);
my_friendly_assert (DECL_TEMPLATE_INFO (decl) != NULL_TREE, 0);
targs = DECL_TI_ARGS (decl);
if (uses_template_parms (targs))
return;
tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
tparms = DECL_TEMPLATE_PARMS (tmpl);
parm_depth = TMPL_PARMS_DEPTH (tparms);
my_friendly_assert (parm_depth == TMPL_ARGS_DEPTH (targs), 0);
fn_type = TREE_TYPE (tmpl);
if (DECL_STATIC_FUNCTION_P (decl))
context = DECL_CLASS_CONTEXT (decl);
if (parm_depth == 1)
;
else
{
int i;
tree partial_args;
partial_args = make_temp_vec (TREE_VEC_LENGTH (targs));
for (i = 1; i < TMPL_ARGS_DEPTH (targs); ++i)
SET_TMPL_ARGS_LEVEL (partial_args, i,
TMPL_ARGS_LEVEL (targs, i));
SET_TMPL_ARGS_LEVEL (partial_args,
TMPL_ARGS_DEPTH (targs),
make_temp_vec (DECL_NTPARMS (tmpl)));
fn_type = tsubst (fn_type, partial_args, 1, NULL_TREE);
if (DECL_STATIC_FUNCTION_P (decl))
context = tsubst (context, partial_args, 1, NULL_TREE);
TREE_VEC_LENGTH (partial_args)--;
tparms = tsubst_template_parms (tparms, partial_args, 1);
}
tparms = INNERMOST_TEMPLATE_PARMS (tparms);
targs = innermost_args (targs);
ret_type = TREE_TYPE (fn_type);
parm_types = TYPE_ARG_TYPES (fn_type);
if (DECL_STATIC_FUNCTION_P (decl))
parm_types = hash_tree_chain (build_pointer_type (context), parm_types);
my_friendly_assert (TREE_VEC_LENGTH (tparms) == TREE_VEC_LENGTH (targs),
0);
my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd', 980702);
saved_namespace = current_namespace;
current_namespace = CP_DECL_CONTEXT (decl);
DECL_ASSEMBLER_NAME (decl)
= build_decl_overload_real (DECL_NAME (decl), parm_types, ret_type,
tparms, targs,
DECL_FUNCTION_MEMBER_P (decl)
+ DECL_CONSTRUCTOR_P (decl));
current_namespace = saved_namespace;
}