#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "obstack.h"
#include "tree.h"
#include "pointer-set.h"
#include "flags.h"
#include "c-common.h"
#include "cp-tree.h"
#include "cp-objcp-common.h"
#include "tree-inline.h"
#include "decl.h"
#include "output.h"
#include "except.h"
#include "toplev.h"
#include "rtl.h"
#include "timevar.h"
#include "tree-iterator.h"
typedef int (*tree_fn_t) (tree, void*);
static GTY(()) tree pending_templates;
static GTY(()) tree last_pending_template;
int processing_template_parmlist;
static int template_header_count;
static GTY(()) tree saved_trees;
static GTY(()) varray_type inline_parm_levels;
static size_t inline_parm_levels_used;
static GTY(()) tree current_tinst_level;
static GTY(()) tree saved_access_scope;
static tree cur_stmt_expr;
static htab_t local_specializations;
#define UNIFY_ALLOW_NONE 0
#define UNIFY_ALLOW_MORE_CV_QUAL 1
#define UNIFY_ALLOW_LESS_CV_QUAL 2
#define UNIFY_ALLOW_DERIVED 4
#define UNIFY_ALLOW_INTEGER 8
#define UNIFY_ALLOW_OUTER_LEVEL 16
#define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
#define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
#define UNIFY_ALLOW_MAX_CORRECTION 128
static void push_access_scope (tree);
static void pop_access_scope (tree);
static int resolve_overloaded_unification (tree, tree, tree, tree,
unification_kind_t, int);
static int try_one_overload (tree, tree, tree, tree, tree,
unification_kind_t, int, bool);
static int unify (tree, tree, tree, tree, int);
static void add_pending_template (tree);
static void reopen_tinst_level (tree);
static tree classtype_mangled_name (tree);
static char* mangle_class_name_for_template (const char *, tree, tree);
static tree tsubst_initializer_list (tree, tree);
static tree get_class_bindings (tree, tree, tree);
static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t, int);
static void tsubst_enum (tree, tree, tree);
static tree add_to_template_args (tree, tree);
static tree add_outermost_template_args (tree, tree);
static bool check_instantiated_args (tree, tree, tsubst_flags_t);
static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*);
static int type_unification_real (tree, tree, tree, tree,
int, unification_kind_t, int, int, int);
static void note_template_header (int);
static tree convert_nontype_argument_function (tree, tree);
static tree convert_nontype_argument (tree, tree);
static tree convert_template_argument (tree, tree, tree,
tsubst_flags_t, int, tree);
static tree get_bindings_overload (tree, tree, tree);
static int for_each_template_parm (tree, tree_fn_t, void*,
struct pointer_set_t*);
static tree build_template_parm_index (int, int, int, tree, tree);
static int inline_needs_template_parms (tree);
static void push_inline_template_parms_recursive (tree, int);
static tree retrieve_local_specialization (tree);
static void register_local_specialization (tree, tree);
static tree reduce_template_parm_level (tree, tree, int);
static int mark_template_parm (tree, void *);
static int template_parm_this_level_p (tree, void *);
static tree tsubst_friend_function (tree, tree);
static tree tsubst_friend_class (tree, tree);
static int can_complete_type_without_circularity (tree);
static tree get_bindings (tree, tree, tree);
static tree get_bindings_real (tree, tree, tree, int, int, int);
static int template_decl_level (tree);
static int check_cv_quals_for_unify (int, tree, tree);
static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
static void regenerate_decl_from_template (tree, tree);
static tree most_specialized (tree, tree, tree);
static tree most_specialized_class (tree, tree);
static int template_class_depth_real (tree, int);
static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
static tree tsubst_arg_types (tree, tree, tsubst_flags_t, tree);
static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
static void check_specialization_scope (void);
static tree process_partial_specialization (tree);
static void set_current_access_from_decl (tree);
static void check_default_tmpl_args (tree, tree, int, int);
static tree tsubst_call_declarator_parms (tree, tree, tsubst_flags_t, tree);
static tree get_template_base (tree, tree, tree, tree);
static int verify_class_unification (tree, tree, tree);
static tree try_class_unification (tree, tree, tree, tree);
static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
tree, tree);
static tree determine_specialization (tree, tree, tree *, int, int);
static int template_args_equal (tree, tree);
static void tsubst_default_arguments (tree);
static tree for_each_template_parm_r (tree *, int *, void *);
static tree copy_default_args_to_explicit_spec_1 (tree, tree);
static void copy_default_args_to_explicit_spec (tree);
static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
static int eq_local_specializations (const void *, const void *);
static bool dependent_type_p_r (tree);
static tree tsubst (tree, tree, tsubst_flags_t, tree);
static tree tsubst_expr (tree, tree, tsubst_flags_t, tree);
static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
static void
push_access_scope (tree t)
{
gcc_assert (TREE_CODE (t) == FUNCTION_DECL
|| TREE_CODE (t) == VAR_DECL);
if (DECL_FRIEND_CONTEXT (t))
push_nested_class (DECL_FRIEND_CONTEXT (t));
else if (DECL_CLASS_SCOPE_P (t))
push_nested_class (DECL_CONTEXT (t));
else
push_to_top_level ();
if (TREE_CODE (t) == FUNCTION_DECL)
{
saved_access_scope = tree_cons
(NULL_TREE, current_function_decl, saved_access_scope);
current_function_decl = t;
}
}
static void
pop_access_scope (tree t)
{
if (TREE_CODE (t) == FUNCTION_DECL)
{
current_function_decl = TREE_VALUE (saved_access_scope);
saved_access_scope = TREE_CHAIN (saved_access_scope);
}
if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
pop_nested_class ();
else
pop_from_top_level ();
}
tree
finish_member_template_decl (tree decl)
{
if (decl == error_mark_node)
return error_mark_node;
gcc_assert (DECL_P (decl));
if (TREE_CODE (decl) == TYPE_DECL)
{
tree type;
type = TREE_TYPE (decl);
if (IS_AGGR_TYPE (type)
&& CLASSTYPE_TEMPLATE_INFO (type)
&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
{
tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
check_member_template (tmpl);
return tmpl;
}
return NULL_TREE;
}
else if (TREE_CODE (decl) == FIELD_DECL)
error ("data member %qD cannot be a member template", decl);
else if (DECL_TEMPLATE_INFO (decl))
{
if (!DECL_TEMPLATE_SPECIALIZATION (decl))
{
check_member_template (DECL_TI_TEMPLATE (decl));
return DECL_TI_TEMPLATE (decl);
}
else
return decl;
}
else
error ("invalid member template declaration %qD", decl);
return error_mark_node;
}
static int
template_class_depth_real (tree type, int count_specializations)
{
int depth;
for (depth = 0;
type && TREE_CODE (type) != NAMESPACE_DECL;
type = (TREE_CODE (type) == FUNCTION_DECL)
? CP_DECL_CONTEXT (type) : TYPE_CONTEXT (type))
{
if (TREE_CODE (type) != FUNCTION_DECL)
{
if (CLASSTYPE_TEMPLATE_INFO (type)
&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
&& ((count_specializations
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
|| uses_template_parms (CLASSTYPE_TI_ARGS (type))))
++depth;
}
else
{
if (DECL_TEMPLATE_INFO (type)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (type))
&& ((count_specializations
&& DECL_TEMPLATE_SPECIALIZATION (type))
|| uses_template_parms (DECL_TI_ARGS (type))))
++depth;
}
}
return depth;
}
int
template_class_depth (tree type)
{
return template_class_depth_real (type, 0);
}
static int
inline_needs_template_parms (tree decl)
{
if (! DECL_TEMPLATE_INFO (decl))
return 0;
return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
> (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
}
static void
push_inline_template_parms_recursive (tree parmlist, int levels)
{
tree parms = TREE_VALUE (parmlist);
int i;
if (levels > 1)
push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
++processing_template_decl;
current_template_parms
= tree_cons (size_int (processing_template_decl),
parms, current_template_parms);
TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
NULL);
for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
{
tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
gcc_assert (DECL_P (parm));
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));
DECL_ARTIFICIAL (decl) = 1;
TREE_CONSTANT (decl) = 1;
TREE_INVARIANT (decl) = 1;
TREE_READONLY (decl) = 1;
DECL_INITIAL (decl) = DECL_INITIAL (parm);
SET_DECL_TEMPLATE_PARM_P (decl);
pushdecl (decl);
}
break;
default:
gcc_unreachable ();
}
}
}
void
maybe_begin_member_template_processing (tree decl)
{
tree parms;
int levels = 0;
if (inline_needs_template_parms (decl))
{
parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
if (DECL_TEMPLATE_SPECIALIZATION (decl))
{
--levels;
parms = TREE_CHAIN (parms);
}
push_inline_template_parms_recursive (parms, levels);
}
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 (void)
{
int i;
if (!inline_parm_levels_used)
return;
--inline_parm_levels_used;
for (i = 0;
i < VARRAY_INT (inline_parm_levels, inline_parm_levels_used);
++i)
{
--processing_template_decl;
current_template_parms = TREE_CHAIN (current_template_parms);
poplevel (0, 0, 0);
}
}
static tree
add_to_template_args (tree args, tree extra_args)
{
tree new_args;
int extra_depth;
int i;
int j;
extra_depth = TMPL_ARGS_DEPTH (extra_args);
new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
for (j = 1; j <= extra_depth; ++j, ++i)
SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
return new_args;
}
static tree
add_outermost_template_args (tree args, tree extra_args)
{
tree new_args;
gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
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;
}
tree
get_innermost_template_args (tree args, int n)
{
tree new_args;
int extra_levels;
int i;
gcc_assert (n >= 0);
if (n == 1)
return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
extra_levels = TMPL_ARGS_DEPTH (args) - n;
gcc_assert (extra_levels >= 0);
if (extra_levels == 0)
return args;
new_args = make_tree_vec (n);
for (i = 1; i <= n; ++i)
SET_TMPL_ARGS_LEVEL (new_args, i,
TMPL_ARGS_LEVEL (args, i + extra_levels));
return new_args;
}
void
begin_template_parm_list (void)
{
begin_scope (sk_template_parms, NULL);
++processing_template_decl;
++processing_template_parmlist;
note_template_header (0);
}
static void
check_specialization_scope (void)
{
tree scope = current_scope ();
if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
error ("explicit specialization in non-namespace scope %qD", scope);
if (current_template_parms)
error ("enclosing class templates are not explicitly specialized");
}
void
begin_specialization (void)
{
begin_scope (sk_template_spec, NULL);
note_template_header (1);
check_specialization_scope ();
}
void
end_specialization (void)
{
finish_scope ();
reset_specialization ();
}
void
reset_specialization (void)
{
processing_specialization = 0;
template_header_count = 0;
}
static void
note_template_header (int specialization)
{
processing_specialization = specialization;
template_header_count++;
}
void
begin_explicit_instantiation (void)
{
gcc_assert (!processing_explicit_instantiation);
processing_explicit_instantiation = true;
}
void
end_explicit_instantiation (void)
{
gcc_assert (processing_explicit_instantiation);
processing_explicit_instantiation = false;
}
static bool
check_specialization_namespace (tree tmpl)
{
tree tpl_ns = decl_namespace_context (tmpl);
if (is_associated_namespace (current_namespace, tpl_ns))
return true;
else
{
pedwarn ("specialization of %qD in different namespace", tmpl);
cp_pedwarn_at (" from definition of %q#D", tmpl);
return false;
}
}
void
maybe_process_partial_specialization (tree type)
{
tree context = TYPE_P (type) ? TYPE_CONTEXT (type) : NULL_TREE;
if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
{
if (CLASSTYPE_IMPLICIT_INSTANTIATION (type)
&& !COMPLETE_TYPE_P (type))
{
check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (type));
SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
if (processing_template_decl)
push_template_decl (TYPE_MAIN_DECL (type));
}
else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
error ("specialization of %qT after instantiation", type);
}
else if (CLASS_TYPE_P (type)
&& !CLASSTYPE_USE_TEMPLATE (type)
&& CLASSTYPE_TEMPLATE_INFO (type)
&& context && CLASS_TYPE_P (context)
&& CLASSTYPE_TEMPLATE_INFO (context))
{
if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
&& !COMPLETE_TYPE_P (type))
{
tree t;
if (current_namespace
!= decl_namespace_context (CLASSTYPE_TI_TEMPLATE (type)))
{
pedwarn ("specializing %q#T in different namespace", type);
cp_pedwarn_at (" from definition of %q#D",
CLASSTYPE_TI_TEMPLATE (type));
}
for (t = DECL_TEMPLATE_INSTANTIATIONS
(most_general_template (CLASSTYPE_TI_TEMPLATE (type)));
t; t = TREE_CHAIN (t))
if (TREE_VALUE (t) != type
&& TYPE_CONTEXT (TREE_VALUE (t)) == context)
error ("specialization %qT after instantiation %qT",
type, TREE_VALUE (t));
SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
CLASSTYPE_TI_ARGS (type)
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
}
}
else if (processing_specialization)
error ("explicit specialization of non-template %qT", type);
}
static inline bool
optimize_specialization_lookup_p (tree tmpl)
{
return (DECL_FUNCTION_TEMPLATE_P (tmpl)
&& DECL_CLASS_SCOPE_P (tmpl)
&& CLASS_TYPE_P (DECL_CONTEXT (tmpl))
&& !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
&& !DECL_MEMBER_TEMPLATE_P (tmpl)
&& !DECL_CONV_FN_P (tmpl)
&& !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
}
static tree
retrieve_specialization (tree tmpl, tree args,
bool class_specializations_p)
{
gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
gcc_assert (TMPL_ARGS_DEPTH (args)
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
if (optimize_specialization_lookup_p (tmpl))
{
tree class_template;
tree class_specialization;
VEC(tree) *methods;
tree fns;
int idx;
class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
class_specialization
= retrieve_specialization (class_template, args,
false);
if (!class_specialization)
return NULL_TREE;
idx = class_method_index_for_fn (class_specialization, tmpl);
if (idx == -1)
return NULL_TREE;
methods = CLASSTYPE_METHOD_VEC (class_specialization);
for (fns = VEC_index (tree, methods, idx); fns; fns = OVL_NEXT (fns))
{
tree fn = OVL_CURRENT (fns);
if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl)
return fn;
}
return NULL_TREE;
}
else
{
tree *sp;
tree *head;
if (!class_specializations_p
&& TREE_CODE (DECL_TEMPLATE_RESULT (tmpl)) == TYPE_DECL)
sp = &DECL_TEMPLATE_INSTANTIATIONS (tmpl);
else
sp = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
head = sp;
while (*sp != NULL_TREE)
{
tree spec = *sp;
if (comp_template_args (TREE_PURPOSE (spec), args))
{
if (spec != *head)
{
*sp = TREE_CHAIN (*sp);
TREE_CHAIN (spec) = *head;
*head = spec;
}
return TREE_VALUE (spec);
}
sp = &TREE_CHAIN (spec);
}
}
return NULL_TREE;
}
static tree
retrieve_local_specialization (tree tmpl)
{
tree spec = htab_find_with_hash (local_specializations, tmpl,
htab_hash_pointer (tmpl));
return spec ? TREE_PURPOSE (spec) : NULL_TREE;
}
int
is_specialization_of (tree decl, tree tmpl)
{
tree t;
if (TREE_CODE (decl) == FUNCTION_DECL)
{
for (t = decl;
t != NULL_TREE;
t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
if (t == tmpl)
return 1;
}
else
{
gcc_assert (TREE_CODE (decl) == TYPE_DECL);
for (t = TREE_TYPE (decl);
t != NULL_TREE;
t = CLASSTYPE_USE_TEMPLATE (t)
? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
return 1;
}
return 0;
}
bool
is_specialization_of_friend (tree decl, tree friend)
{
bool need_template = true;
int template_depth;
gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
|| TREE_CODE (decl) == TYPE_DECL);
if (TREE_CODE (friend) == FUNCTION_DECL
&& DECL_TEMPLATE_INFO (friend)
&& !DECL_USE_TEMPLATE (friend))
{
friend = DECL_TI_TEMPLATE (friend);
need_template = false;
}
else if (TREE_CODE (friend) == TEMPLATE_DECL
&& !PRIMARY_TEMPLATE_P (friend))
need_template = false;
if (TREE_CODE (friend) != TEMPLATE_DECL)
return false;
if (is_specialization_of (decl, friend))
return true;
template_depth = template_class_depth (DECL_CONTEXT (friend));
if (template_depth
&& DECL_CLASS_SCOPE_P (decl)
&& is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend))))
{
tree context = DECL_CONTEXT (decl);
tree args = NULL_TREE;
int current_depth = 0;
while (current_depth < template_depth)
{
if (CLASSTYPE_TEMPLATE_INFO (context))
{
if (current_depth == 0)
args = TYPE_TI_ARGS (context);
else
args = add_to_template_args (TYPE_TI_ARGS (context), args);
current_depth++;
}
context = TYPE_CONTEXT (context);
}
if (TREE_CODE (decl) == FUNCTION_DECL)
{
bool is_template;
tree friend_type;
tree decl_type;
tree friend_args_type;
tree decl_args_type;
is_template = DECL_TEMPLATE_INFO (decl)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
if (need_template ^ is_template)
return false;
else if (is_template)
{
tree friend_parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
args, tf_none);
if (!comp_template_parms
(DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
friend_parms))
return false;
decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
}
else
decl_type = TREE_TYPE (decl);
friend_type = tsubst_function_type (TREE_TYPE (friend), args,
tf_none, NULL_TREE);
if (friend_type == error_mark_node)
return false;
if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
return false;
friend_args_type = TYPE_ARG_TYPES (friend_type);
decl_args_type = TYPE_ARG_TYPES (decl_type);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend))
friend_args_type = TREE_CHAIN (friend_args_type);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
decl_args_type = TREE_CHAIN (decl_args_type);
return compparms (decl_args_type, friend_args_type);
}
else
{
bool is_template;
tree decl_type = TREE_TYPE (decl);
is_template
= CLASSTYPE_TEMPLATE_INFO (decl_type)
&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
if (need_template ^ is_template)
return false;
else if (is_template)
{
tree friend_parms;
if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
!= DECL_NAME (friend))
return false;
friend_parms
= tsubst_template_parms (DECL_TEMPLATE_PARMS (friend),
args, tf_none);
return comp_template_parms
(DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
friend_parms);
}
else
return (DECL_NAME (decl)
== DECL_NAME (friend));
}
}
return false;
}
static tree
register_specialization (tree spec, tree tmpl, tree args)
{
tree fn;
gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
if (TREE_CODE (spec) == FUNCTION_DECL
&& uses_template_parms (DECL_TI_ARGS (spec)))
return spec;
gcc_assert (TMPL_ARGS_DEPTH (args)
== TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl)));
fn = retrieve_specialization (tmpl, args,
false);
if (fn == spec)
return spec;
else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
{
if (DECL_TEMPLATE_INSTANTIATION (fn))
{
if (TREE_USED (fn)
|| DECL_EXPLICIT_INSTANTIATION (fn))
{
error ("specialization of %qD after instantiation",
fn);
return spec;
}
else
{
DECL_INITIAL (fn) = NULL_TREE;
duplicate_decls (spec, fn);
return fn;
}
}
else if (DECL_TEMPLATE_SPECIALIZATION (fn))
{
if (!duplicate_decls (spec, fn) && DECL_INITIAL (spec))
DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
return fn;
}
}
if (DECL_TEMPLATE_SPECIALIZATION (spec)
&& !check_specialization_namespace (tmpl))
DECL_CONTEXT (spec) = decl_namespace_context (tmpl);
if (!optimize_specialization_lookup_p (tmpl))
DECL_TEMPLATE_SPECIALIZATIONS (tmpl)
= tree_cons (args, spec, DECL_TEMPLATE_SPECIALIZATIONS (tmpl));
return spec;
}
bool
reregister_specialization (tree spec, tree tmpl, tree new_spec)
{
tree* s;
for (s = &DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
*s != NULL_TREE;
s = &TREE_CHAIN (*s))
if (TREE_VALUE (*s) == spec)
{
if (!new_spec)
*s = TREE_CHAIN (*s);
else
TREE_VALUE (*s) = new_spec;
return 1;
}
return 0;
}
static int
eq_local_specializations (const void *p1, const void *p2)
{
return TREE_VALUE ((tree) p1) == (tree) p2;
}
static hashval_t
hash_local_specialization (const void* p1)
{
return htab_hash_pointer (TREE_VALUE ((tree) p1));
}
static void
register_local_specialization (tree spec, tree tmpl)
{
void **slot;
slot = htab_find_slot_with_hash (local_specializations, tmpl,
htab_hash_pointer (tmpl), INSERT);
*slot = build_tree_list (spec, tmpl);
}
void
print_candidates (tree fns)
{
tree fn;
const char *str = "candidates are:";
for (fn = fns; fn != NULL_TREE; fn = TREE_CHAIN (fn))
{
tree f;
for (f = TREE_VALUE (fn); f; f = OVL_NEXT (f))
cp_error_at ("%s %+#D", str, OVL_CURRENT (f));
str = " ";
}
}
static tree
determine_specialization (tree template_id,
tree decl,
tree* targs_out,
int need_member_template,
int template_count)
{
tree fns;
tree targs;
tree explicit_targs;
tree candidates = NULL_TREE;
tree templates = NULL_TREE;
int header_count;
struct cp_binding_level *b;
*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 (BASELINK_P (fns))
fns = BASELINK_FUNCTIONS (fns);
if (!is_overloaded_fn (fns))
{
error ("%qD is not a function template", fns);
return error_mark_node;
}
header_count = 0;
for (b = current_binding_level;
b->kind == sk_template_parms;
b = b->level_chain)
++header_count;
for (; fns; fns = OVL_NEXT (fns))
{
tree fn = OVL_CURRENT (fns);
if (TREE_CODE (fn) == TEMPLATE_DECL)
{
tree decl_arg_types;
tree fn_arg_types;
decl_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);
fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
if (list_length (fn_arg_types) != list_length (decl_arg_types))
continue;
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
&& !same_type_p (TREE_VALUE (fn_arg_types),
TREE_VALUE (decl_arg_types)))
continue;
if (header_count && header_count != template_count + 1)
continue;
if (current_binding_level->kind == sk_template_parms
&& !current_binding_level->explicit_spec_p
&& (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
!= TREE_VEC_LENGTH (TREE_VALUE (current_template_parms))))
continue;
targs = get_bindings (fn, decl, explicit_targs);
if (!targs)
continue;
templates = tree_cons (targs, fn, templates);
}
else if (need_member_template)
;
else if (TREE_CODE (fn) != FUNCTION_DECL)
;
else if (!DECL_FUNCTION_MEMBER_P (fn))
;
else if (DECL_ARTIFICIAL (fn))
;
else
{
tree decl_arg_types;
if (!DECL_TEMPLATE_INFO (fn))
continue;
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);
}
}
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 = tree_cons (targs, tmpl, NULL_TREE);
}
}
if (templates == NULL_TREE && candidates == NULL_TREE)
{
cp_error_at ("template-id %qD for %q+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 %qD for %q+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_TEMPLATE_RESULT (TREE_VALUE (templates)));
if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
{
*targs_out = copy_node (targs);
SET_TMPL_ARGS_LEVEL (*targs_out,
TMPL_ARGS_DEPTH (*targs_out),
TREE_PURPOSE (templates));
}
else
*targs_out = TREE_PURPOSE (templates);
return TREE_VALUE (templates);
}
static tree
copy_default_args_to_explicit_spec_1 (tree spec_types,
tree tmpl_types)
{
tree new_spec_types;
if (!spec_types)
return NULL_TREE;
if (spec_types == void_list_node)
return void_list_node;
new_spec_types =
copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
TREE_CHAIN (tmpl_types));
return hash_tree_cons (TREE_PURPOSE (tmpl_types),
TREE_VALUE (spec_types),
new_spec_types);
}
static void
copy_default_args_to_explicit_spec (tree decl)
{
tree tmpl;
tree spec_types;
tree tmpl_types;
tree new_spec_types;
tree old_type;
tree new_type;
tree t;
tree object_type = NULL_TREE;
tree in_charge = NULL_TREE;
tree vtt = NULL_TREE;
tmpl = DECL_TI_TEMPLATE (decl);
tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
for (t = tmpl_types; t; t = TREE_CHAIN (t))
if (TREE_PURPOSE (t))
break;
if (!t)
return;
old_type = TREE_TYPE (decl);
spec_types = TYPE_ARG_TYPES (old_type);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
{
object_type = TREE_TYPE (TREE_VALUE (spec_types));
spec_types = TREE_CHAIN (spec_types);
tmpl_types = TREE_CHAIN (tmpl_types);
if (DECL_HAS_IN_CHARGE_PARM_P (decl))
{
in_charge = spec_types;
spec_types = TREE_CHAIN (spec_types);
}
if (DECL_HAS_VTT_PARM_P (decl))
{
vtt = spec_types;
spec_types = TREE_CHAIN (spec_types);
}
}
new_spec_types =
copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
if (object_type)
{
if (vtt)
new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
TREE_VALUE (vtt),
new_spec_types);
if (in_charge)
new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
TREE_VALUE (in_charge),
new_spec_types);
new_type = build_method_type_directly (object_type,
TREE_TYPE (old_type),
new_spec_types);
}
else
new_type = build_function_type (TREE_TYPE (old_type),
new_spec_types);
new_type = cp_build_type_attribute_variant (new_type,
TYPE_ATTRIBUTES (old_type));
new_type = build_exception_variant (new_type,
TYPE_RAISES_EXCEPTIONS (old_type));
TREE_TYPE (decl) = new_type;
}
tree
check_explicit_specialization (tree declarator,
tree decl,
int template_count,
int flags)
{
int have_def = flags & 2;
int is_friend = flags & 4;
int specialization = 0;
int explicit_instantiation = 0;
int member_specialization = 0;
tree ctype = DECL_CLASS_CONTEXT (decl);
tree dname = DECL_NAME (decl);
tmpl_spec_kind tsk;
if (is_friend)
{
if (!processing_specialization)
tsk = tsk_none;
else
tsk = tsk_excessive_parms;
}
else
tsk = current_tmpl_spec_kind (template_count);
switch (tsk)
{
case tsk_none:
if (processing_specialization)
{
specialization = 1;
SET_DECL_TEMPLATE_SPECIALIZATION (decl);
}
else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
{
if (is_friend)
specialization = 1;
else
{
error ("template-id %qD in declaration of primary template",
declarator);
return decl;
}
}
break;
case tsk_invalid_member_spec:
return error_mark_node;
case tsk_invalid_expl_inst:
error ("template parameter list used in explicit instantiation");
case tsk_expl_inst:
if (have_def)
error ("definition provided for explicit instantiation");
explicit_instantiation = 1;
break;
case tsk_excessive_parms:
case tsk_insufficient_parms:
if (tsk == tsk_excessive_parms)
error ("too many template parameter lists in declaration of %qD",
decl);
else if (template_header_count)
error("too few template parameter lists in declaration of %qD", decl);
else
error("explicit specialization of %qD must be introduced by "
"%<template <>%>", decl);
case tsk_expl_spec:
SET_DECL_TEMPLATE_SPECIALIZATION (decl);
if (ctype)
member_specialization = 1;
else
specialization = 1;
break;
case tsk_template:
if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
{
if (uses_template_parms (declarator))
error ("function template partial specialization %qD "
"is not allowed", declarator);
else
error ("template-id %qD in declaration of primary template",
declarator);
return decl;
}
if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
specialization = 1;
break;
default:
gcc_unreachable ();
}
if (specialization || member_specialization)
{
tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
for (; t; t = TREE_CHAIN (t))
if (TREE_PURPOSE (t))
{
pedwarn
("default argument specified in explicit specialization");
break;
}
if (current_lang_name == lang_name_c)
error ("template specialization with C linkage");
}
if (specialization || member_specialization || explicit_instantiation)
{
tree tmpl = NULL_TREE;
tree targs = NULL_TREE;
if (TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
{
tree fns;
gcc_assert (TREE_CODE (declarator) == IDENTIFIER_NODE);
if (ctype)
fns = dname;
else
{
gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
fns = namespace_binding (dname, CP_DECL_CONTEXT (decl));
if (!fns || !is_overloaded_fn (fns))
{
error ("%qD is not a template function", dname);
fns = error_mark_node;
}
}
declarator = lookup_template_function (fns, NULL_TREE);
}
if (declarator == error_mark_node)
return error_mark_node;
if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
{
if (!explicit_instantiation)
return error_mark_node;
else
{
;
}
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 (constructor_name_p (name, ctype))
{
int is_constructor = DECL_CONSTRUCTOR_P (decl);
if (is_constructor ? !TYPE_HAS_CONSTRUCTOR (ctype)
: !CLASSTYPE_DESTRUCTORS (ctype))
{
error ("specialization of implicitly-declared special member function");
return error_mark_node;
}
name = is_constructor ? ctor_identifier : dtor_identifier;
}
if (!DECL_CONV_FN_P (decl))
{
idx = lookup_fnfields_1 (ctype, name);
if (idx >= 0)
fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (ctype), idx);
}
else
{
VEC(tree) *methods;
tree ovl;
fns = NULL_TREE;
methods = CLASSTYPE_METHOD_VEC (ctype);
if (methods)
for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
VEC_iterate (tree, methods, idx, ovl);
++idx)
{
if (!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)
{
error ("no member function %qD declared in %qT", name, ctype);
return error_mark_node;
}
else
TREE_OPERAND (declarator, 0) = fns;
}
tmpl = determine_specialization (declarator, decl,
&targs,
member_specialization,
template_count);
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_tree_vec (parm_depth);
for (i = arg_depth - parm_depth; i < arg_depth; ++i)
TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
= TREE_VEC_ELT (targs, i);
targs = new_targs;
}
return instantiate_template (tmpl, targs, tf_error);
}
if (DECL_STATIC_FUNCTION_P (tmpl)
&& DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
revert_static_member_fn (decl);
if (tsk == tsk_template)
{
SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl)) = NULL_TREE;
if (have_def)
{
DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (tmpl))
= DECL_SOURCE_LOCATION (decl);
DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (tmpl))
= DECL_ARGUMENTS (decl);
}
return tmpl;
}
DECL_TEMPLATE_INFO (decl) = tree_cons (tmpl, targs, NULL_TREE);
copy_default_args_to_explicit_spec (decl);
TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
if (DECL_VISIBILITY_SPECIFIED (gen_tmpl))
{
DECL_VISIBILITY_SPECIFIED (decl) = 1;
DECL_VISIBILITY (decl) = DECL_VISIBILITY (gen_tmpl);
}
if (is_friend && !have_def)
SET_DECL_IMPLICIT_INSTANTIATION (decl);
else if (DECL_CONSTRUCTOR_P (decl) || DECL_DESTRUCTOR_P (decl))
clone_function_decl (decl, 0);
decl = register_specialization (decl, gen_tmpl, targs);
}
}
return decl;
}
int
comp_template_parms (tree parms1, tree parms2)
{
tree p1;
tree p2;
if (parms1 == parms2)
return 1;
for (p1 = parms1, p2 = parms2;
p1 != NULL_TREE && p2 != NULL_TREE;
p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
{
tree t1 = TREE_VALUE (p1);
tree t2 = TREE_VALUE (p2);
int i;
gcc_assert (TREE_CODE (t1) == TREE_VEC);
gcc_assert (TREE_CODE (t2) == TREE_VEC);
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 (tree decl)
{
tree olddecl;
if (!current_template_parms)
return;
if (TREE_CODE (decl) == OVERLOAD)
decl = OVL_CURRENT (decl);
olddecl = innermost_non_namespace_value (DECL_NAME (decl));
if (!olddecl)
return;
if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
return;
if (decl == olddecl
|| TEMPLATE_PARMS_FOR_INLINE (current_template_parms))
return;
cp_error_at ("declaration of %q#D", decl);
cp_error_at (" shadows template parm %q#D", olddecl);
}
static tree
build_template_parm_index (int index,
int level,
int orig_level,
tree decl,
tree type)
{
tree t = make_node (TEMPLATE_PARM_INDEX);
TEMPLATE_PARM_IDX (t) = index;
TEMPLATE_PARM_LEVEL (t) = level;
TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
TEMPLATE_PARM_DECL (t) = decl;
TREE_TYPE (t) = type;
TREE_CONSTANT (t) = TREE_CONSTANT (decl);
TREE_INVARIANT (t) = TREE_INVARIANT (decl);
TREE_READONLY (t) = TREE_READONLY (decl);
return t;
}
static tree
reduce_template_parm_level (tree index, tree type, int levels)
{
if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
|| (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
!= TEMPLATE_PARM_LEVEL (index) - levels))
{
tree orig_decl = TEMPLATE_PARM_DECL (index);
tree decl, t;
decl = build_decl (TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
TREE_INVARIANT (decl) = TREE_INVARIANT (orig_decl);
TREE_READONLY (decl) = TREE_READONLY (orig_decl);
DECL_ARTIFICIAL (decl) = 1;
SET_DECL_TEMPLATE_PARM_P (decl);
t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
TEMPLATE_PARM_LEVEL (index) - levels,
TEMPLATE_PARM_ORIG_LEVEL (index),
decl, type);
TEMPLATE_PARM_DESCENDANTS (index) = t;
DECL_TEMPLATE_PARMS (decl)
= DECL_TEMPLATE_PARMS (TEMPLATE_PARM_DECL (index));
}
return TEMPLATE_PARM_DESCENDANTS (index);
}
tree
process_template_parm (tree list, tree next, bool is_non_type)
{
tree parm;
tree decl = 0;
tree defval;
int idx;
parm = next;
gcc_assert (TREE_CODE (parm) == TREE_LIST);
defval = TREE_PURPOSE (parm);
if (list)
{
tree p = TREE_VALUE (tree_last (list));
if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
else
idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
++idx;
}
else
idx = 0;
if (is_non_type)
{
parm = TREE_VALUE (parm);
SET_DECL_TEMPLATE_PARM_P (parm);
TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
TREE_CONSTANT (parm) = 1;
TREE_INVARIANT (parm) = 1;
TREE_READONLY (parm) = 1;
if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
TREE_TYPE (parm) = void_type_node;
decl = build_decl (CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
TREE_CONSTANT (decl) = 1;
TREE_INVARIANT (decl) = 1;
TREE_READONLY (decl) = 1;
DECL_INITIAL (parm) = DECL_INITIAL (decl)
= build_template_parm_index (idx, processing_template_decl,
processing_template_decl,
decl, TREE_TYPE (parm));
}
else
{
tree t;
parm = TREE_VALUE (TREE_VALUE (parm));
if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
{
t = make_aggr_type (TEMPLATE_TEMPLATE_PARM);
TREE_TYPE (parm) = t;
TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
decl = parm;
}
else
{
t = make_aggr_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));
}
DECL_ARTIFICIAL (decl) = 1;
SET_DECL_TEMPLATE_PARM_P (decl);
pushdecl (decl);
parm = build_tree_list (defval, parm);
return chainon (list, parm);
}
tree
end_template_parm_list (tree parms)
{
int nparms;
tree parm, next;
tree saved_parmlist = make_tree_vec (list_length (parms));
current_template_parms
= tree_cons (size_int (processing_template_decl),
saved_parmlist, current_template_parms);
for (parm = parms, nparms = 0; parm; parm = next, nparms++)
{
next = TREE_CHAIN (parm);
TREE_VEC_ELT (saved_parmlist, nparms) = parm;
TREE_CHAIN (parm) = NULL_TREE;
}
--processing_template_parmlist;
return saved_parmlist;
}
void
end_template_decl (void)
{
reset_specialization ();
if (! processing_template_decl)
return;
finish_scope ();
--processing_template_decl;
current_template_parms = TREE_CHAIN (current_template_parms);
}
tree
current_template_args (void)
{
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 (tree decl, tree parms, bool member_template_p)
{
tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
DECL_TEMPLATE_PARMS (tmpl) = parms;
DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
if (DECL_LANG_SPECIFIC (decl))
{
DECL_STATIC_FUNCTION_P (tmpl) = DECL_STATIC_FUNCTION_P (decl);
DECL_CONSTRUCTOR_P (tmpl) = DECL_CONSTRUCTOR_P (decl);
DECL_DESTRUCTOR_P (tmpl) = DECL_DESTRUCTOR_P (decl);
DECL_NONCONVERTING_P (tmpl) = DECL_NONCONVERTING_P (decl);
DECL_ASSIGNMENT_OPERATOR_P (tmpl) = DECL_ASSIGNMENT_OPERATOR_P (decl);
if (DECL_OVERLOADED_OPERATOR_P (decl))
SET_OVERLOADED_OPERATOR_CODE (tmpl,
DECL_OVERLOADED_OPERATOR_P (decl));
}
return tmpl;
}
struct template_parm_data
{
int level;
int current_arg;
int* parms;
int* arg_uses_template_parms;
};
static int
mark_template_parm (tree t, void* data)
{
int level;
int idx;
struct template_parm_data* tpd = (struct template_parm_data*) data;
if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
{
level = TEMPLATE_PARM_LEVEL (t);
idx = TEMPLATE_PARM_IDX (t);
}
else
{
level = TEMPLATE_TYPE_LEVEL (t);
idx = TEMPLATE_TYPE_IDX (t);
}
if (level == tpd->level)
{
tpd->parms[idx] = 1;
tpd->arg_uses_template_parms[tpd->current_arg] = 1;
}
return 0;
}
static tree
process_partial_specialization (tree decl)
{
tree type = TREE_TYPE (decl);
tree maintmpl = CLASSTYPE_TI_TEMPLATE (type);
tree specargs = CLASSTYPE_TI_ARGS (type);
tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
tree inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
int nargs = TREE_VEC_LENGTH (inner_args);
int ntparms = TREE_VEC_LENGTH (inner_parms);
int i;
int did_error_intro = 0;
struct template_parm_data tpd;
struct template_parm_data tpd2;
tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
tpd.parms = alloca (sizeof (int) * ntparms);
memset (tpd.parms, 0, sizeof (int) * ntparms);
tpd.arg_uses_template_parms = alloca (sizeof (int) * nargs);
memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
for (i = 0; i < nargs; ++i)
{
tpd.current_arg = i;
for_each_template_parm (TREE_VEC_ELT (inner_args, i),
&mark_template_parm,
&tpd,
NULL);
}
for (i = 0; i < ntparms; ++i)
if (tpd.parms[i] == 0)
{
if (!did_error_intro)
{
error ("template parameters not used in partial specialization:");
did_error_intro = 1;
}
error (" %qD", TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
}
if (comp_template_args
(inner_args,
INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE
(maintmpl)))))
error ("partial specialization %qT does not specialize any template arguments", type);
gcc_assert (nargs == DECL_NTPARMS (maintmpl));
tpd2.parms = 0;
for (i = 0; i < nargs; ++i)
{
tree arg = TREE_VEC_ELT (inner_args, i);
if (
!TYPE_P (arg)
&& TREE_CODE (arg) != TEMPLATE_DECL
&& TREE_CODE (arg) != TEMPLATE_PARM_INDEX)
{
if (tpd.arg_uses_template_parms[i])
error ("template argument %qE 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
= alloca (sizeof (int) * nargs);
tpd2.parms = alloca (sizeof (int) * nargs);
tpd2.level =
TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
}
tpd2.current_arg = i;
tpd2.arg_uses_template_parms[i] = 0;
memset (tpd2.parms, 0, sizeof (int) * nargs);
for_each_template_parm (type,
&mark_template_parm,
&tpd2,
NULL);
if (tpd2.arg_uses_template_parms [i])
{
int j;
for (j = 0; j < nargs; ++j)
if (tpd2.parms[j] != 0
&& tpd.arg_uses_template_parms [j])
{
error ("type %qT of template argument %qE depends "
"on template parameter(s)",
type,
arg);
break;
}
}
}
}
}
if (retrieve_specialization (maintmpl, specargs,
true))
return decl;
DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
= tree_cons (inner_args, inner_parms,
DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
return decl;
}
static void
check_default_tmpl_args (tree decl, tree parms, int is_primary, int is_partial)
{
const char *msg;
int last_level_to_check;
tree parm_level;
if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL)
return;
if (current_class_type
&& !TYPE_BEING_DEFINED (current_class_type)
&& DECL_LANG_SPECIFIC (decl)
&& (DECL_FUNCTION_MEMBER_P (decl)
? same_type_p (DECL_CONTEXT (decl), current_class_type)
: DECL_FRIEND_CONTEXT (decl)
? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
: false)
&& (!DECL_FUNCTION_MEMBER_P (decl)
|| DECL_INITIALIZED_IN_CLASS_P (decl)))
return;
for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
{
tree inner_parms = TREE_VALUE (parm_level);
int ntparms = TREE_VEC_LENGTH (inner_parms);
int seen_def_arg_p = 0;
int i;
for (i = 0; i < ntparms; ++i)
{
tree parm = TREE_VEC_ELT (inner_parms, i);
if (TREE_PURPOSE (parm))
seen_def_arg_p = 1;
else if (seen_def_arg_p)
{
error ("no default argument for %qD", TREE_VALUE (parm));
TREE_PURPOSE (parm) = error_mark_node;
}
}
}
if (TREE_CODE (decl) != TYPE_DECL || is_partial || !is_primary)
;
else
parms = TREE_CHAIN (parms);
if (TREE_CODE (decl) == FUNCTION_DECL)
msg = "default template arguments may not be used in function templates";
else if (is_partial)
msg = "default template arguments may not be used in partial specializations";
else
msg = "default argument for template parameter for class enclosing %qD";
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 (parm_level = parms;
parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
parm_level = TREE_CHAIN (parm_level))
{
tree inner_parms = TREE_VALUE (parm_level);
int i;
int ntparms;
ntparms = TREE_VEC_LENGTH (inner_parms);
for (i = 0; i < ntparms; ++i)
if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
{
if (msg)
{
error (msg, decl);
msg = 0;
}
TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
}
if (msg)
msg = "default argument for template parameter for class enclosing %qD";
}
}
static int
template_parm_this_level_p (tree t, void* data)
{
int this_level = *(int *)data;
int level;
if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
level = TEMPLATE_PARM_LEVEL (t);
else
level = TEMPLATE_TYPE_LEVEL (t);
return level == this_level;
}
tree
push_template_decl_real (tree decl, int is_friend)
{
tree tmpl;
tree args;
tree info;
tree ctx;
int primary;
int is_partial;
int new_template_p = 0;
bool member_template_p = false;
if (decl == error_mark_node)
return decl;
is_partial = (DECL_IMPLICIT_TYPEDEF_P (decl)
&& TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)));
is_friend |= (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl));
if (is_friend)
ctx = DECL_CONTEXT (decl);
else if (CP_DECL_CONTEXT (decl)
&& TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
ctx = CP_DECL_CONTEXT (decl);
else
ctx = current_scope ();
if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
ctx = NULL_TREE;
if (!DECL_CONTEXT (decl))
DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
primary = template_parm_scope_p ();
if (primary)
{
if (DECL_CLASS_SCOPE_P (decl))
member_template_p = true;
if (current_lang_name == lang_name_c)
error ("template with C linkage");
else if (TREE_CODE (decl) == TYPE_DECL
&& ANON_AGGRNAME_P (DECL_NAME (decl)))
error ("template class without a name");
else if (TREE_CODE (decl) == FUNCTION_DECL)
{
if (DECL_DESTRUCTOR_P (decl))
{
error ("destructor %qD declared as member template", decl);
return error_mark_node;
}
if (NEW_DELETE_OPNAME_P (DECL_NAME (decl))
&& (!TYPE_ARG_TYPES (TREE_TYPE (decl))
|| TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
|| !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
|| (TREE_CHAIN (TYPE_ARG_TYPES ((TREE_TYPE (decl))))
== void_list_node)))
{
error ("invalid template declaration of %qD", decl);
return decl;
}
}
else if ((DECL_IMPLICIT_TYPEDEF_P (decl)
&& CLASS_TYPE_P (TREE_TYPE (decl)))
|| (TREE_CODE (decl) == VAR_DECL && ctx && CLASS_TYPE_P (ctx)))
;
else
{
error ("template declaration of %q#D", decl);
return error_mark_node;
}
}
check_default_tmpl_args (decl, current_template_parms,
primary, is_partial);
if (is_partial)
return process_partial_specialization (decl);
args = current_template_args ();
if (!ctx
|| TREE_CODE (ctx) == FUNCTION_DECL
|| (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
|| (is_friend && !DECL_TEMPLATE_INFO (decl)))
{
if (DECL_LANG_SPECIFIC (decl)
&& DECL_TEMPLATE_INFO (decl)
&& DECL_TI_TEMPLATE (decl))
tmpl = DECL_TI_TEMPLATE (decl);
else if (DECL_IMPLICIT_TYPEDEF_P (decl)
&& TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
&& TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
{
redeclare_class_template (TREE_TYPE (decl),
current_template_parms);
tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
}
else
{
tmpl = build_template_decl (decl, current_template_parms,
member_template_p);
new_template_p = 1;
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 (TREE_CODE (decl) == TYPE_DECL)
{
if ((IS_AGGR_TYPE_CODE (TREE_CODE (TREE_TYPE (decl)))
|| TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE)
&& TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
&& TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
else
{
error ("%qD does not declare a template type", decl);
return decl;
}
}
else if (!DECL_LANG_SPECIFIC (decl) || !DECL_TEMPLATE_INFO (decl))
{
error ("template definition of non-template %q#D", decl);
return decl;
}
else
tmpl = DECL_TI_TEMPLATE (decl);
if (DECL_FUNCTION_TEMPLATE_P (tmpl)
&& DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
&& DECL_TEMPLATE_SPECIALIZATION (decl)
&& DECL_MEMBER_TEMPLATE_P (tmpl))
{
tree new_tmpl;
args = DECL_TI_ARGS (decl);
new_tmpl
= build_template_decl (decl, current_template_parms,
member_template_p);
DECL_TEMPLATE_RESULT (new_tmpl) = decl;
TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
DECL_TI_TEMPLATE (decl) = new_tmpl;
SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
DECL_TEMPLATE_INFO (new_tmpl)
= tree_cons (tmpl, args, NULL_TREE);
register_specialization (new_tmpl,
most_general_template (tmpl),
args);
return decl;
}
parms = DECL_TEMPLATE_PARMS (tmpl);
i = TMPL_PARMS_DEPTH (parms);
if (TMPL_ARGS_DEPTH (args) != i)
{
error ("expected %d levels of template parms for %q#D, got %d",
i, decl, TMPL_ARGS_DEPTH (args));
}
else
for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
{
a = TMPL_ARGS_LEVEL (args, i);
t = INNERMOST_TEMPLATE_PARMS (parms);
if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
{
if (current == decl)
error ("got %d template parameters for %q#D",
TREE_VEC_LENGTH (a), decl);
else
error ("got %d template parameters for %q#T",
TREE_VEC_LENGTH (a), current);
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 (new_template_p && !ctx
&& !(is_friend && template_class_depth (current_class_type) > 0))
{
tmpl = pushdecl_namespace_level (tmpl);
if (tmpl == error_mark_node)
return error_mark_node;
}
if (primary)
{
DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
if (DECL_CONV_FN_P (tmpl))
{
int depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
depth))
DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
}
}
if (DECL_TEMPLATE_INFO (tmpl))
args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
info = tree_cons (tmpl, args, NULL_TREE);
if (DECL_IMPLICIT_TYPEDEF_P (decl))
{
SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
if ((!ctx || TREE_CODE (ctx) != FUNCTION_DECL)
&& TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
&& !IDENTIFIER_TEMPLATE (DECL_NAME (decl)))
DECL_NAME (decl) = classtype_mangled_name (TREE_TYPE (decl));
}
else if (DECL_LANG_SPECIFIC (decl))
DECL_TEMPLATE_INFO (decl) = info;
return DECL_TEMPLATE_RESULT (tmpl);
}
tree
push_template_decl (tree decl)
{
return push_template_decl_real (decl, 0);
}
void
redeclare_class_template (tree type, tree parms)
{
tree tmpl;
tree tmpl_parms;
int i;
if (!TYPE_TEMPLATE_INFO (type))
{
error ("%qT is not a template type", type);
return;
}
tmpl = TYPE_TI_TEMPLATE (type);
if (!PRIMARY_TEMPLATE_P (tmpl))
return;
if (!parms)
{
error ("template specifiers not specified in declaration of %qD",
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 %qD", tmpl);
error ("used %d template parameter(s) instead of %d",
TREE_VEC_LENGTH (tmpl_parms),
TREE_VEC_LENGTH (parms));
return;
}
for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
{
tree tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
tree tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
tree parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
|| (TREE_CODE (tmpl_parm) != TYPE_DECL
&& !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm))))
{
cp_error_at ("template parameter %q#D", tmpl_parm);
error ("redeclared here as %q#D", parm);
return;
}
if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
{
error ("redefinition of default argument for %q#D", parm);
error ("%J original definition appeared here", tmpl_parm);
return;
}
if (parm_default != NULL_TREE)
TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
else if (tmpl_default != NULL_TREE)
TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
}
}
tree
fold_non_dependent_expr (tree expr)
{
if (processing_template_decl
&& !type_dependent_expression_p (expr)
&& !value_dependent_expression_p (expr))
{
HOST_WIDE_INT saved_processing_template_decl;
saved_processing_template_decl = processing_template_decl;
processing_template_decl = 0;
expr = tsubst_copy_and_build (expr,
NULL_TREE,
tf_error,
NULL_TREE,
false);
processing_template_decl = saved_processing_template_decl;
}
return expr;
}
tree
fold_decl_constant_value (tree expr)
{
tree const_expr = expr;
do
{
expr = fold_non_dependent_expr (const_expr);
const_expr = integral_constant_value (expr);
}
while (expr != const_expr);
return expr;
}
static tree
convert_nontype_argument_function (tree type, tree expr)
{
tree fns = expr;
tree fn, fn_no_ptr;
fn = instantiate_type (type, fns, tf_none);
if (fn == error_mark_node)
return error_mark_node;
fn_no_ptr = fn;
if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
if (!DECL_EXTERNAL_LINKAGE_P (fn_no_ptr))
{
error ("%qE is not a valid template argument for type %qT "
"because function %qD has not external linkage",
expr, type, fn_no_ptr);
return NULL_TREE;
}
return fn;
}
static tree
convert_nontype_argument (tree type, tree expr)
{
tree expr_type;
if (TREE_CODE (expr) == STRING_CST)
{
error ("%qE is not a valid template argument for type %qT "
"because string literals can never be used in this context",
expr, type);
return NULL_TREE;
}
expr = fold_non_dependent_expr (expr);
expr_type = TREE_TYPE (expr);
if (TREE_CODE (expr) == NOP_EXPR)
{
if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
{
tree addr = TREE_OPERAND (expr, 0);
gcc_assert (TREE_CODE (expr_type) == REFERENCE_TYPE);
gcc_assert (TREE_CODE (addr) == ADDR_EXPR);
gcc_assert (TREE_CODE (TREE_TYPE (addr)) == POINTER_TYPE);
gcc_assert (same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (expr_type),
TREE_TYPE (TREE_TYPE (addr))));
expr = TREE_OPERAND (addr, 0);
expr_type = TREE_TYPE (expr);
}
else if (TYPE_PTROBV_P (type))
{
STRIP_NOPS (expr);
gcc_assert (TREE_CODE (expr) == ADDR_EXPR);
gcc_assert (TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE);
if (TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == ARRAY_TYPE)
expr = TREE_OPERAND (expr, 0);
expr_type = TREE_TYPE (expr);
}
}
if (INTEGRAL_TYPE_P (type))
{
if (!INTEGRAL_TYPE_P (expr_type))
return error_mark_node;
expr = fold_decl_constant_value (expr);
if (TREE_CODE (expr) != INTEGER_CST)
{
error ("%qE is not a valid template argument for type %qT "
"because it is a non-constant expression", expr, type);
return NULL_TREE;
}
expr = ocp_convert (type, expr, CONV_IMPLICIT, LOOKUP_PROTECT);
if (expr == error_mark_node)
return error_mark_node;
expr = fold (expr);
}
else if (TYPE_PTROBV_P (type))
{
bool constant_address_p =
(TREE_CODE (expr) == ADDR_EXPR
|| TREE_CODE (expr_type) == ARRAY_TYPE
|| (DECL_P (expr) && DECL_TEMPLATE_PARM_P (expr)));
expr = decay_conversion (expr);
if (expr == error_mark_node)
return error_mark_node;
expr = perform_qualification_conversions (type, expr);
if (expr == error_mark_node)
return error_mark_node;
if (!constant_address_p)
{
error ("%qE is not a valid template argument for type %qT "
"because it is not a constant pointer", expr, type);
return NULL_TREE;
}
}
else if (TYPE_REF_OBJ_P (type))
{
if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
expr_type))
return error_mark_node;
if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
{
error ("%qE is not a valid template argument for type %qT "
"because of conflicts in cv-qualification", expr, type);
return NULL_TREE;
}
if (!real_lvalue_p (expr))
{
error ("%qE is not a valid template argument for type %qT "
"because it is not a lvalue", expr, type);
return NULL_TREE;
}
if (!DECL_EXTERNAL_LINKAGE_P (expr))
{
error ("%qE is not a valid template argument for type %qT "
"because object %qD has not external linkage",
expr, type, expr);
return NULL_TREE;
}
expr = build_nop (type, build_address (expr));
}
else if (TYPE_PTRFN_P (type))
{
if (!type_unknown_p (expr_type))
{
expr = decay_conversion (expr);
if (expr == error_mark_node)
return error_mark_node;
}
expr = convert_nontype_argument_function (type, expr);
if (!expr || expr == error_mark_node)
return expr;
}
else if (TYPE_REFFN_P (type))
{
if (TREE_CODE (expr) == ADDR_EXPR)
{
error ("%qE is not a valid template argument for type %qT "
"because it is a pointer", expr, type);
inform ("try using %qE instead", TREE_OPERAND (expr, 0));
return NULL_TREE;
}
expr = convert_nontype_argument_function (TREE_TYPE (type), expr);
if (!expr || expr == error_mark_node)
return expr;
expr = build_nop(type, build_address (expr));
}
else if (TYPE_PTRMEMFUNC_P (type))
{
expr = instantiate_type (type, expr, tf_none);
if (expr == error_mark_node)
return error_mark_node;
if (!same_type_p (TREE_TYPE (expr), type))
{
gcc_assert (can_convert (type, TREE_TYPE (expr)));
error ("%qE is not a valid template argument for type %qT "
"because it is of type %qT", expr, type,
TREE_TYPE (expr));
inform ("standard conversions are not allowed in this context");
return NULL_TREE;
}
}
else if (TYPE_PTRMEM_P (type))
{
expr = perform_qualification_conversions (type, expr);
if (expr == error_mark_node)
return expr;
}
else
gcc_unreachable ();
gcc_assert (same_type_p (type, TREE_TYPE (expr)));
return expr;
}
static int
coerce_template_template_parms (tree parm_parms,
tree arg_parms,
tsubst_flags_t complain,
tree in_decl,
tree outer_args)
{
int nparms, nargs, i;
tree parm, arg;
gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
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 (!dependent_type_p (TREE_TYPE (arg))
&& !same_type_p
(tsubst (TREE_TYPE (parm), outer_args, complain, in_decl),
TREE_TYPE (arg)))
return 0;
break;
default:
gcc_unreachable ();
}
}
return 1;
}
static tree
convert_template_argument (tree parm,
tree arg,
tree args,
tsubst_flags_t 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_TEMPLATE_ARGS (args);
if (TREE_CODE (arg) == TREE_LIST
&& TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
{
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
|| TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
if (is_tmpl_type
&& (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
|| TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
arg = TYPE_STUB_DECL (arg);
is_type = TYPE_P (arg) || is_tmpl_type;
if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
&& TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
{
pedwarn ("to refer to a type member of a template parameter, "
"use %<typename %E%>", arg);
arg = make_typename_type (TREE_OPERAND (arg, 0),
TREE_OPERAND (arg, 1),
typename_type,
complain & tf_error);
is_type = 1;
}
if (is_type != requires_type)
{
if (in_decl)
{
if (complain & tf_error)
{
error ("type/value mismatch at argument %d in template "
"parameter list for %qD",
i + 1, in_decl);
if (is_type)
error (" expected a constant of type %qT, got %qT",
TREE_TYPE (parm),
(is_tmpl_type ? DECL_NAME (arg) : arg));
else if (requires_tmpl_type)
error (" expected a class template, got %qE", arg);
else
error (" expected a type, got %qE", arg);
}
}
return error_mark_node;
}
if (is_tmpl_type ^ requires_tmpl_type)
{
if (in_decl && (complain & tf_error))
{
error ("type/value mismatch at argument %d in template "
"parameter list for %qD",
i + 1, in_decl);
if (is_tmpl_type)
error (" expected a type, got %qT", DECL_NAME (arg));
else
error (" expected a class template, got %qT", arg);
}
return error_mark_node;
}
if (is_type)
{
if (requires_tmpl_type)
{
if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
val = TREE_TYPE (arg);
else
{
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 & tf_error))
{
error ("type/value mismatch at argument %d in "
"template parameter list for %qD",
i + 1, in_decl);
error (" expected a template of type %qD, got %qD",
parm, arg);
}
val = error_mark_node;
}
}
}
else
val = arg;
}
else
{
tree t = tsubst (TREE_TYPE (parm), args, complain, in_decl);
if (invalid_nontype_parm_type_p (t, complain))
return error_mark_node;
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 & tf_error))
error ("could not convert template argument %qE to %qT", arg, t);
}
return val;
}
static tree
coerce_template_parms (tree parms,
tree args,
tree in_decl,
tsubst_flags_t complain,
int require_all_arguments)
{
int nparms, nargs, i, lost = 0;
tree inner_args;
tree new_args;
tree new_inner_args;
inner_args = INNERMOST_TEMPLATE_ARGS (args);
nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
nparms = TREE_VEC_LENGTH (parms);
if (nargs > nparms
|| (nargs < nparms
&& require_all_arguments
&& TREE_PURPOSE (TREE_VEC_ELT (parms, nargs)) == NULL_TREE))
{
if (complain & tf_error)
{
error ("wrong number of template arguments (%d, should be %d)",
nargs, nparms);
if (in_decl)
cp_error_at ("provided for %qD", in_decl);
}
return error_mark_node;
}
new_inner_args = make_tree_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 (i < nargs)
arg = TREE_VEC_ELT (inner_args, i);
else if (require_all_arguments)
arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
complain, in_decl);
else
break;
gcc_assert (arg);
if (arg == error_mark_node)
{
if (complain & tf_error)
error ("template argument %d is invalid", i + 1);
}
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 (tree ot, tree nt)
{
if (nt == ot)
return 1;
if (TREE_CODE (nt) == TREE_VEC)
return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
else if (TYPE_P (nt))
return TYPE_P (ot) && same_type_p (ot, nt);
else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
return 0;
else
return cp_tree_equal (ot, nt);
}
int
comp_template_args (tree oldargs, tree 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 (const char* name, tree parms, tree 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_TEMPLATE_ARGS (arglist);
gcc_assert (nparms == TREE_VEC_LENGTH (arglist));
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 (arg, TFF_CHASE_TYPEDEF));
continue;
}
else if (TREE_CODE (parm) == TEMPLATE_DECL)
{
if (TREE_CODE (arg) == TEMPLATE_DECL)
{
tree context = DECL_CONTEXT (arg);
if (context)
{
gcc_assert (TREE_CODE (context) == NAMESPACE_DECL
|| CLASS_TYPE_P (context));
cat (decl_as_string (DECL_CONTEXT (arg),
TFF_PLAIN_IDENTIFIER));
cat ("::");
}
cat (IDENTIFIER_POINTER (DECL_NAME (arg)));
}
else
cat (type_as_string (arg, TFF_CHASE_TYPEDEF));
continue;
}
else
gcc_assert (TREE_CODE (parm) == PARM_DECL);
cat (expr_as_string (arg, TFF_PLAIN_IDENTIFIER));
}
{
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 (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 (tree d)
{
tree ti = (TYPE_P (d)
? CLASSTYPE_TEMPLATE_INFO (d)
: DECL_TEMPLATE_INFO (d));
tree pt;
int level;
if (TI_PENDING_TEMPLATE_FLAG (ti))
return;
level = !(current_tinst_level && TINST_DECL (current_tinst_level) == d);
if (level)
push_tinst_level (d);
pt = tree_cons (current_tinst_level, d, NULL_TREE);
if (last_pending_template)
TREE_CHAIN (last_pending_template) = pt;
else
pending_templates = pt;
last_pending_template = pt;
TI_PENDING_TEMPLATE_FLAG (ti) = 1;
if (level)
pop_tinst_level ();
}
tree
lookup_template_function (tree fns, tree arglist)
{
tree type;
if (fns == error_mark_node || arglist == error_mark_node)
return error_mark_node;
gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
gcc_assert (fns && (is_overloaded_fn (fns)
|| TREE_CODE (fns) == IDENTIFIER_NODE));
if (BASELINK_P (fns))
{
BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
unknown_type_node,
BASELINK_FUNCTIONS (fns),
arglist);
return fns;
}
type = TREE_TYPE (fns);
if (TREE_CODE (fns) == OVERLOAD || !type)
type = unknown_type_node;
return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
}
tree
maybe_get_template_decl_from_type_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 (tree d1,
tree arglist,
tree in_decl,
tree context,
int entering_scope,
tsubst_flags_t complain)
{
tree template = NULL_TREE, parmlist;
tree t;
timevar_push (TV_NAME_LOOKUP);
if (TREE_CODE (d1) == IDENTIFIER_NODE)
{
tree value = innermost_non_namespace_value (d1);
if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
template = value;
else
{
if (context)
push_decl_namespace (context);
template = lookup_name (d1, 0);
template = maybe_get_template_decl_from_type_decl (template);
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
|| (TYPE_P (d1) && IS_AGGR_TYPE (d1)))
{
template = TYPE_TI_TEMPLATE (d1);
d1 = DECL_NAME (template);
}
else if (TREE_CODE (d1) == TEMPLATE_DECL
&& TREE_CODE (DECL_TEMPLATE_RESULT (d1)) == TYPE_DECL)
{
template = d1;
d1 = DECL_NAME (template);
context = DECL_CONTEXT (template);
}
if (! template)
{
if (complain & tf_error)
error ("%qT is not a template", d1);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
}
if (TREE_CODE (template) != TEMPLATE_DECL
|| ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (template)
&& !PRIMARY_TEMPLATE_P (template)))
{
if (complain & tf_error)
{
error ("non-template type %qT used as a template", d1);
if (in_decl)
cp_error_at ("for template declaration %qD", in_decl);
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
}
complain &= ~tf_user;
if (DECL_TEMPLATE_TEMPLATE_PARM_P (template))
{
tree parm;
tree arglist2;
parmlist = DECL_INNERMOST_TEMPLATE_PARMS (template);
if (current_template_parms)
arglist = add_to_template_args (current_template_args (), arglist);
arglist2 = coerce_template_parms (parmlist, arglist, template,
complain, 1);
if (arglist2 == error_mark_node
|| (!uses_template_parms (arglist2)
&& check_instantiated_args (template, arglist2, complain)))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
parm = bind_template_template_parm (TREE_TYPE (template), arglist2);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, 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);
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);
}
gcc_assert (parm_depth == arg_depth);
template = gen_tmpl;
if (parm_depth > 1)
{
int i;
int saved_depth = TMPL_ARGS_DEPTH (arglist);
tree bound_args = make_tree_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,
complain, 1);
if (a == error_mark_node)
{
TREE_VEC_LENGTH (arglist) = saved_depth;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
}
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_TEMPLATE_ARGS (arglist),
template,
complain, 1);
if (arglist == error_mark_node)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, 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 && TREE_CODE (ctx) != NAMESPACE_DECL;
ctx = (TYPE_P (ctx)
? TYPE_CONTEXT (ctx)
: DECL_CONTEXT (ctx)))
if (TYPE_P (ctx) && same_type_p (ctx, template_type))
goto found_ctx;
found = NULL_TREE;
found_ctx:;
}
}
if (found)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
found = retrieve_specialization (template, arglist,
false);
if (found)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
is_partial_instantiation = uses_template_parms (arglist);
if (!is_partial_instantiation
&& check_instantiated_args (template,
INNERMOST_TEMPLATE_ARGS (arglist),
complain))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
if (!is_partial_instantiation
&& !PRIMARY_TEMPLATE_P (template)
&& TREE_CODE (CP_DECL_CONTEXT (template)) == NAMESPACE_DECL)
{
found = xref_tag_from_type (TREE_TYPE (template),
DECL_NAME (template),
ts_global);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, found);
}
context = tsubst (DECL_CONTEXT (template), arglist,
complain, in_decl);
if (!context)
context = global_namespace;
if (TREE_CODE (template_type) == ENUMERAL_TYPE)
{
if (!is_partial_instantiation)
{
set_current_access_from_decl (TYPE_NAME (template_type));
t = start_enum (TYPE_IDENTIFIER (template_type));
}
else
t = make_node (ENUMERAL_TYPE);
}
else
{
t = make_aggr_type (TREE_CODE (template_type));
CLASSTYPE_DECLARED_CLASS (t)
= CLASSTYPE_DECLARED_CLASS (template_type);
SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
TYPE_FOR_JAVA (t) = TYPE_FOR_JAVA (template_type);
if (context == current_function_decl)
pushtag (DECL_NAME (template), t, 0);
}
if (!TYPE_NAME (t))
{
TYPE_CONTEXT (t) = FROB_CONTEXT (context);
type_decl = create_implicit_typedef (DECL_NAME (template), t);
DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
TYPE_STUB_DECL (t) = type_decl;
DECL_SOURCE_LOCATION (type_decl)
= DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
}
else
type_decl = TYPE_NAME (t);
TREE_PRIVATE (type_decl)
= TREE_PRIVATE (TYPE_STUB_DECL (template_type));
TREE_PROTECTED (type_decl)
= TREE_PROTECTED (TYPE_STUB_DECL (template_type));
DECL_IN_SYSTEM_HEADER (type_decl)
= DECL_IN_SYSTEM_HEADER (template);
if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
{
DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
}
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)
{
TREE_VEC_LENGTH (arglist)--;
found = tsubst (template, arglist, complain, NULL_TREE);
TREE_VEC_LENGTH (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);
if (TREE_CODE (t) != ENUMERAL_TYPE)
DECL_NAME (type_decl) = classtype_mangled_name (t);
if (is_partial_instantiation)
DECL_IGNORED_P (TYPE_STUB_DECL (t)) = 1;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
timevar_pop (TV_NAME_LOOKUP);
}
struct pair_fn_data
{
tree_fn_t fn;
void *data;
struct pointer_set_t *visited;
};
static tree
for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
{
tree t = *tp;
struct pair_fn_data *pfd = (struct pair_fn_data *) d;
tree_fn_t fn = pfd->fn;
void *data = pfd->data;
if (TYPE_P (t)
&& for_each_template_parm (TYPE_CONTEXT (t), fn, data, pfd->visited))
return error_mark_node;
switch (TREE_CODE (t))
{
case RECORD_TYPE:
if (TYPE_PTRMEMFUNC_P (t))
break;
case UNION_TYPE:
case ENUMERAL_TYPE:
if (!TYPE_TEMPLATE_INFO (t))
*walk_subtrees = 0;
else if (for_each_template_parm (TREE_VALUE (TYPE_TEMPLATE_INFO (t)),
fn, data, pfd->visited))
return error_mark_node;
break;
case METHOD_TYPE:
if (for_each_template_parm (TYPE_METHOD_BASETYPE (t), fn, data,
pfd->visited))
return error_mark_node;
case FUNCTION_TYPE:
if (for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
return error_mark_node;
{
tree parm;
for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
if (for_each_template_parm (TREE_VALUE (parm), fn, data,
pfd->visited))
return error_mark_node;
*walk_subtrees = 0;
}
break;
case TYPEOF_TYPE:
if (for_each_template_parm (TYPE_FIELDS (t), fn, data,
pfd->visited))
return error_mark_node;
break;
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,
pfd->visited))
return error_mark_node;
case PARM_DECL:
case CONST_DECL:
if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t)
&& for_each_template_parm (DECL_INITIAL (t), fn, data,
pfd->visited))
return error_mark_node;
if (DECL_CONTEXT (t)
&& for_each_template_parm (DECL_CONTEXT (t), fn, data,
pfd->visited))
return error_mark_node;
break;
case BOUND_TEMPLATE_TEMPLATE_PARM:
if (for_each_template_parm (TYPE_TI_ARGS (t), fn, data, pfd->visited))
return error_mark_node;
case TEMPLATE_TEMPLATE_PARM:
case TEMPLATE_TYPE_PARM:
case TEMPLATE_PARM_INDEX:
if (fn && (*fn)(t, data))
return error_mark_node;
else if (!fn)
return error_mark_node;
break;
case TEMPLATE_DECL:
if (DECL_TEMPLATE_TEMPLATE_PARM_P (t)
&& for_each_template_parm (TREE_TYPE (t), fn, data, pfd->visited))
return error_mark_node;
*walk_subtrees = 0;
break;
case TYPENAME_TYPE:
if (!fn
|| for_each_template_parm (TYPENAME_TYPE_FULLNAME (t), fn,
data, pfd->visited))
return error_mark_node;
break;
case CONSTRUCTOR:
if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
&& for_each_template_parm (TYPE_PTRMEMFUNC_FN_TYPE
(TREE_TYPE (t)), fn, data,
pfd->visited))
return error_mark_node;
break;
case INDIRECT_REF:
case COMPONENT_REF:
if (!fn && !TREE_TYPE (t))
return error_mark_node;
break;
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:
case PSEUDO_DTOR_EXPR:
if (!fn)
return error_mark_node;
break;
case BASELINK:
*walk_subtrees = 0;
if (for_each_template_parm (BASELINK_FUNCTIONS (*tp), fn, data,
pfd->visited))
return error_mark_node;
break;
default:
break;
}
return NULL_TREE;
}
static int
for_each_template_parm (tree t, tree_fn_t fn, void* data,
struct pointer_set_t *visited)
{
struct pair_fn_data pfd;
int result;
pfd.fn = fn;
pfd.data = data;
if (visited)
pfd.visited = visited;
else
pfd.visited = pointer_set_create ();
result = walk_tree (&t,
for_each_template_parm_r,
&pfd,
pfd.visited) != NULL_TREE;
if (!visited)
{
pointer_set_destroy (pfd.visited);
pfd.visited = 0;
}
return result;
}
int
uses_template_parms (tree t)
{
bool dependent_p;
int saved_processing_template_decl;
saved_processing_template_decl = processing_template_decl;
if (!saved_processing_template_decl)
processing_template_decl = 1;
if (TYPE_P (t))
dependent_p = dependent_type_p (t);
else if (TREE_CODE (t) == TREE_VEC)
dependent_p = any_dependent_template_arguments_p (t);
else if (TREE_CODE (t) == TREE_LIST)
dependent_p = (uses_template_parms (TREE_VALUE (t))
|| uses_template_parms (TREE_CHAIN (t)));
else if (DECL_P (t)
|| EXPR_P (t)
|| TREE_CODE (t) == TEMPLATE_PARM_INDEX
|| TREE_CODE (t) == OVERLOAD
|| TREE_CODE (t) == BASELINK
|| CONSTANT_CLASS_P (t))
dependent_p = (type_dependent_expression_p (t)
|| value_dependent_expression_p (t));
else
{
gcc_assert (t == error_mark_node);
dependent_p = false;
}
processing_template_decl = saved_processing_template_decl;
return dependent_p;
}
int
uses_template_parms_level (tree t, int level)
{
return for_each_template_parm (t, template_parm_this_level_p, &level, NULL);
}
static int tinst_depth;
extern int max_tinst_depth;
#ifdef GATHER_STATISTICS
int depth_reached;
#endif
static int tinst_level_tick;
static int last_template_error_tick;
int
push_tinst_level (tree d)
{
tree 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 (use "
"-ftemplate-depth-NN to increase the maximum) instantiating %qD",
max_tinst_depth, d);
print_instantiation_context ();
return 0;
}
new = make_node (TINST_LEVEL);
TINST_DECL (new) = d;
TINST_LOCATION (new) = input_location;
TINST_IN_SYSTEM_HEADER_P (new) = in_system_header;
TREE_CHAIN (new) = 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 (void)
{
tree old = current_tinst_level;
input_location = TINST_LOCATION (old);
in_system_header = TINST_IN_SYSTEM_HEADER_P (old);
current_tinst_level = TREE_CHAIN (old);
--tinst_depth;
++tinst_level_tick;
}
static void
reopen_tinst_level (tree level)
{
tree t;
tinst_depth = 0;
for (t = level; t; t = TREE_CHAIN (t))
++tinst_depth;
current_tinst_level = level;
pop_tinst_level ();
}
static tree
tsubst_friend_function (tree decl, tree args)
{
tree new_friend;
if (TREE_CODE (decl) == FUNCTION_DECL
&& DECL_TEMPLATE_INSTANTIATION (decl)
&& TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
{
tree template_id, arglist, fns;
tree new_args;
tree tmpl;
tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
push_nested_namespace (ns);
fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
tf_error | tf_warning, NULL_TREE);
pop_nested_namespace (ns);
arglist = tsubst (DECL_TI_ARGS (decl), args,
tf_error | tf_warning, NULL_TREE);
template_id = lookup_template_function (fns, arglist);
new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
tmpl = determine_specialization (template_id, new_friend,
&new_args,
0,
TREE_VEC_LENGTH (args));
return instantiate_template (tmpl, new_args, tf_error);
}
new_friend = tsubst (decl, args, tf_error | tf_warning, NULL_TREE);
if (new_friend == error_mark_node)
return error_mark_node;
DECL_USE_TEMPLATE (new_friend) = 0;
if (TREE_CODE (decl) == TEMPLATE_DECL)
{
DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
= DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
}
if (TREE_CODE (new_friend) != TEMPLATE_DECL)
{
SET_DECL_RTL (new_friend, NULL_RTX);
SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
}
if (DECL_NAMESPACE_SCOPE_P (new_friend))
{
tree old_decl;
tree new_friend_template_info;
tree new_friend_result_template_info;
tree ns;
int new_friend_is_defn;
new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
new_friend_is_defn =
(DECL_INITIAL (DECL_TEMPLATE_RESULT
(template_for_substitution (new_friend)))
!= NULL_TREE);
if (TREE_CODE (new_friend) == TEMPLATE_DECL)
{
DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
new_friend_result_template_info
= DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
}
else
new_friend_result_template_info = NULL_TREE;
ns = decl_namespace_context (new_friend);
push_nested_namespace (ns);
old_decl = pushdecl_namespace_level (new_friend);
pop_nested_namespace (ns);
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)
reregister_specialization (new_friend,
most_general_template (old_decl),
old_decl);
else
{
tree t;
tree new_friend_args;
DECL_TEMPLATE_INFO (DECL_TEMPLATE_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));
}
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
{
tree context = DECL_CONTEXT (new_friend);
bool dependent_p;
++processing_template_decl;
dependent_p = dependent_type_p (context);
--processing_template_decl;
if (!dependent_p
&& !complete_type_or_else (context, NULL_TREE))
return error_mark_node;
if (COMPLETE_TYPE_P (context))
{
tree fn = check_classfn (context,
new_friend, NULL_TREE);
if (fn)
new_friend = fn;
}
}
return new_friend;
}
static tree
tsubst_friend_class (tree friend_tmpl, tree args)
{
tree friend_type;
tree tmpl;
tree context;
context = DECL_CONTEXT (friend_tmpl);
if (context)
{
if (TREE_CODE (context) == NAMESPACE_DECL)
push_nested_namespace (context);
else
push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
}
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))
{
if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
> TMPL_ARGS_DEPTH (args))
{
tree parms;
parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
args, tf_error | tf_warning);
redeclare_class_template (TREE_TYPE (tmpl), parms);
}
friend_type = TREE_TYPE (tmpl);
}
else
{
tmpl = tsubst (friend_tmpl, args, tf_error | tf_warning, NULL_TREE);
DECL_USE_TEMPLATE (tmpl) = 0;
DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
friend_type = TREE_TYPE (pushdecl_top_level (tmpl));
}
if (context)
{
if (TREE_CODE (context) == NAMESPACE_DECL)
pop_nested_namespace (context);
else
pop_nested_class ();
}
return friend_type;
}
static int
can_complete_type_without_circularity (tree type)
{
if (type == NULL_TREE || type == error_mark_node)
return 0;
else if (COMPLETE_TYPE_P (type))
return 1;
else if (TREE_CODE (type) == ARRAY_TYPE && TYPE_DOMAIN (type))
return can_complete_type_without_circularity (TREE_TYPE (type));
else if (CLASS_TYPE_P (type)
&& TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
return 0;
else
return 1;
}
tree
instantiate_class_template (tree type)
{
tree template, args, pattern, t, member;
tree typedecl;
tree pbinfo;
tree base_list;
if (type == error_mark_node)
return error_mark_node;
if (TYPE_BEING_DEFINED (type)
|| COMPLETE_TYPE_P (type)
|| dependent_type_p (type))
return type;
template = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
gcc_assert (TREE_CODE (template) == TEMPLATE_DECL);
args = CLASSTYPE_TI_ARGS (type);
t = most_specialized_class (template, args);
if (t == error_mark_node)
{
const char *str = "candidates are:";
error ("ambiguous class template instantiation for %q#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;
return error_mark_node;
}
if (t)
pattern = TREE_TYPE (t);
else
pattern = TREE_TYPE (template);
if (!COMPLETE_TYPE_P (pattern))
return type;
if (! push_tinst_level (type))
return type;
TYPE_BEING_DEFINED (type) = 1;
push_deferring_access_checks (dk_no_deferred);
push_to_top_level ();
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;
}
SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
typedecl = TYPE_MAIN_DECL (type);
input_location = DECL_SOURCE_LOCATION (typedecl);
in_system_header = DECL_IN_SYSTEM_HEADER (typedecl);
TYPE_HAS_CONSTRUCTOR (type) = TYPE_HAS_CONSTRUCTOR (pattern);
TYPE_HAS_NEW_OPERATOR (type) = TYPE_HAS_NEW_OPERATOR (pattern);
TYPE_HAS_ARRAY_NEW_OPERATOR (type) = TYPE_HAS_ARRAY_NEW_OPERATOR (pattern);
TYPE_GETS_DELETE (type) = TYPE_GETS_DELETE (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_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_PACKED (type) = TYPE_PACKED (pattern);
TYPE_ALIGN (type) = TYPE_ALIGN (pattern);
TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
TYPE_FOR_JAVA (type) = TYPE_FOR_JAVA (pattern);
if (ANON_AGGR_TYPE_P (pattern))
SET_ANON_AGGR_TYPE_P (type);
if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
{
CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
}
pbinfo = TYPE_BINFO (pattern);
gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
|| COMPLETE_TYPE_P (TYPE_CONTEXT (type))
|| TYPE_BEING_DEFINED (TYPE_CONTEXT (type)));
base_list = NULL_TREE;
if (BINFO_N_BASE_BINFOS (pbinfo))
{
tree pbase_binfo;
tree context = TYPE_CONTEXT (type);
tree pushed_scope;
int i;
pushed_scope = push_scope (context ? context : global_namespace);
for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
{
tree base;
tree access = BINFO_BASE_ACCESS (pbinfo, i);
base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error, NULL_TREE);
if (base == error_mark_node)
continue;
base_list = tree_cons (access, base, base_list);
if (BINFO_VIRTUAL_P (pbase_binfo))
TREE_TYPE (base_list) = integer_type_node;
}
base_list = nreverse (base_list);
if (pushed_scope)
pop_scope (pushed_scope);
}
xref_basetypes (type, base_list);
pushclass (type);
for (member = CLASSTYPE_DECL_LIST (pattern);
member; member = TREE_CHAIN (member))
{
tree t = TREE_VALUE (member);
if (TREE_PURPOSE (member))
{
if (TYPE_P (t))
{
tree tag = t;
tree name = TYPE_IDENTIFIER (tag);
tree newtag;
bool class_template_p;
class_template_p = (TREE_CODE (tag) != ENUMERAL_TYPE
&& TYPE_LANG_SPECIFIC (tag)
&& CLASSTYPE_IS_TEMPLATE (tag));
if (class_template_p)
++processing_template_decl;
newtag = tsubst (tag, args, tf_error, NULL_TREE);
if (class_template_p)
--processing_template_decl;
if (newtag == error_mark_node)
continue;
if (TREE_CODE (newtag) != ENUMERAL_TYPE)
{
if (class_template_p)
CLASSTYPE_USE_TEMPLATE (newtag) = 0;
if (name)
SET_IDENTIFIER_TYPE_VALUE (name, newtag);
pushtag (name, newtag, 0);
}
}
else if (TREE_CODE (t) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (t))
{
tree r;
if (TREE_CODE (t) == TEMPLATE_DECL)
++processing_template_decl;
r = tsubst (t, args, tf_error, NULL_TREE);
if (TREE_CODE (t) == TEMPLATE_DECL)
--processing_template_decl;
set_current_access_from_decl (r);
grok_special_member_properties (r);
finish_member_declaration (r);
}
else
{
if (TREE_CODE (t) != CONST_DECL)
{
tree r;
input_location = DECL_SOURCE_LOCATION (t);
if (TREE_CODE (t) == TEMPLATE_DECL)
++processing_template_decl;
r = tsubst (t, args, tf_error | tf_warning, NULL_TREE);
if (TREE_CODE (t) == TEMPLATE_DECL)
--processing_template_decl;
if (TREE_CODE (r) == VAR_DECL)
{
tree init;
if (DECL_INITIALIZED_IN_CLASS_P (r))
init = tsubst_expr (DECL_INITIAL (t), args,
tf_error | tf_warning, NULL_TREE);
else
init = NULL_TREE;
finish_static_data_member_decl
(r, init, NULL_TREE, 0);
if (DECL_INITIALIZED_IN_CLASS_P (r))
check_static_variable_definition (r, TREE_TYPE (r));
}
else if (TREE_CODE (r) == FIELD_DECL)
{
tree rtype = TREE_TYPE (r);
if (can_complete_type_without_circularity (rtype))
complete_type (rtype);
if (!COMPLETE_TYPE_P (rtype))
{
cxx_incomplete_type_error (r, rtype);
r = error_mark_node;
}
}
if (!(TREE_CODE (r) == TYPE_DECL
&& TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
&& DECL_ARTIFICIAL (r)))
{
set_current_access_from_decl (r);
finish_member_declaration (r);
}
}
}
}
else
{
if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t))
{
tree friend_type = t;
bool adjust_processing_template_decl = false;
if (TREE_CODE (friend_type) == TEMPLATE_DECL)
{
friend_type = tsubst_friend_class (friend_type, args);
adjust_processing_template_decl = true;
}
else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
{
friend_type = tsubst (friend_type, args,
tf_error | tf_warning, NULL_TREE);
if (TREE_CODE (friend_type) == TEMPLATE_DECL)
friend_type = TREE_TYPE (friend_type);
adjust_processing_template_decl = true;
}
else if (TREE_CODE (friend_type) == TYPENAME_TYPE)
{
friend_type = tsubst (friend_type, args,
tf_error | tf_warning, NULL_TREE);
++processing_template_decl;
if (dependent_type_p (friend_type))
adjust_processing_template_decl = true;
--processing_template_decl;
}
else if (uses_template_parms (friend_type))
friend_type = tsubst (friend_type, args,
tf_error | tf_warning, NULL_TREE);
else if (CLASSTYPE_USE_TEMPLATE (friend_type))
friend_type = friend_type;
else
{
tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
push_nested_namespace (ns);
friend_type =
xref_tag_from_type (friend_type, NULL_TREE,
ts_global);
pop_nested_namespace (ns);
}
if (adjust_processing_template_decl)
++processing_template_decl;
if (friend_type != error_mark_node)
make_friend_class (type, friend_type, false);
if (adjust_processing_template_decl)
--processing_template_decl;
}
else
{
tree r;
input_location = DECL_SOURCE_LOCATION (t);
if (TREE_CODE (t) == TEMPLATE_DECL)
{
++processing_template_decl;
push_deferring_access_checks (dk_no_check);
}
r = tsubst_friend_function (t, args);
add_friend (type, r, false);
if (TREE_CODE (t) == TEMPLATE_DECL)
{
pop_deferring_access_checks ();
--processing_template_decl;
}
}
}
}
input_location = DECL_SOURCE_LOCATION (typedecl);
unreverse_member_declarations (type);
finish_struct_1 (type);
TYPE_BEING_DEFINED (type) = 0;
if (!PRIMARY_TEMPLATE_P (template))
for (t = TYPE_METHODS (type); t; t = TREE_CHAIN (t))
if (TREE_CODE (t) == FUNCTION_DECL
&& DECL_TEMPLATE_INFO (t))
tsubst_default_arguments (t);
popclass ();
pop_from_top_level ();
pop_deferring_access_checks ();
pop_tinst_level ();
if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
return type;
}
static tree
tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
tree r;
if (!t)
r = t;
else if (TYPE_P (t))
r = tsubst (t, args, complain, in_decl);
else
{
r = tsubst_expr (t, args, complain, in_decl);
if (!uses_template_parms (r))
{
if (!TREE_TYPE (r))
{
int saved_processing_template_decl = processing_template_decl;
processing_template_decl = 0;
r = tsubst_copy_and_build (r, NULL_TREE,
tf_error, NULL_TREE,
false);
processing_template_decl = saved_processing_template_decl;
}
r = fold (r);
}
}
return r;
}
static tree
tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
int len = TREE_VEC_LENGTH (t);
int need_new = 0, i;
tree *elts = alloca (len * sizeof (tree));
for (i = 0; i < len; i++)
{
tree orig_arg = TREE_VEC_ELT (t, i);
tree new_arg;
if (TREE_CODE (orig_arg) == TREE_VEC)
new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
else
new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
if (new_arg == error_mark_node)
return error_mark_node;
elts[i] = new_arg;
if (new_arg != orig_arg)
need_new = 1;
}
if (!need_new)
return t;
t = make_tree_vec (len);
for (i = 0; i < len; i++)
TREE_VEC_ELT (t, i) = elts[i];
return t;
}
static tree
tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
{
tree r = NULL_TREE;
tree* new_parms;
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 tuple = TREE_VEC_ELT (TREE_VALUE (parms), i);
tree default_value = TREE_PURPOSE (tuple);
tree parm_decl = TREE_VALUE (tuple);
parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
default_value = tsubst_template_arg (default_value, args,
complain, NULL_TREE);
tuple = build_tree_list (default_value, parm_decl);
TREE_VEC_ELT (new_vec, i) = tuple;
}
*new_parms =
tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
- TMPL_ARGS_DEPTH (args)),
new_vec, NULL_TREE);
}
return r;
}
static tree
tsubst_aggr_type (tree t,
tree args,
tsubst_flags_t 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))
return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
case ENUMERAL_TYPE:
case UNION_TYPE:
if (TYPE_TEMPLATE_INFO (t))
{
tree argvec;
tree context;
tree r;
context = TYPE_CONTEXT (t);
if (context)
context = tsubst_aggr_type (context, args, complain,
in_decl, 1);
argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
complain, in_decl);
if (argvec == error_mark_node)
return error_mark_node;
r = lookup_template_class (t, argvec, in_decl, context,
entering_scope, complain);
return cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
}
else
return t;
default:
return tsubst (t, args, complain, in_decl);
}
}
tree
tsubst_default_argument (tree fn, tree type, tree arg)
{
tree saved_class_ptr = NULL_TREE;
tree saved_class_ref = NULL_TREE;
push_access_scope (fn);
current_function_decl = NULL_TREE;
if (cfun)
{
saved_class_ptr = current_class_ptr;
cp_function_chain->x_current_class_ptr = NULL_TREE;
saved_class_ref = current_class_ref;
cp_function_chain->x_current_class_ref = NULL_TREE;
}
push_deferring_access_checks(dk_no_deferred);
arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
tf_error | tf_warning, NULL_TREE);
pop_deferring_access_checks();
if (cfun)
{
cp_function_chain->x_current_class_ptr = saved_class_ptr;
cp_function_chain->x_current_class_ref = saved_class_ref;
}
pop_access_scope (fn);
arg = check_default_argument (type, arg);
return arg;
}
static void
tsubst_default_arguments (tree fn)
{
tree arg;
tree tmpl_args;
tmpl_args = DECL_TI_ARGS (fn);
if (uses_template_parms (tmpl_args))
return;
for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
arg;
arg = TREE_CHAIN (arg))
if (TREE_PURPOSE (arg))
TREE_PURPOSE (arg) = tsubst_default_argument (fn,
TREE_VALUE (arg),
TREE_PURPOSE (arg));
}
static tree
tsubst_decl (tree t, tree args, tsubst_flags_t complain)
{
location_t saved_loc;
tree r = NULL_TREE;
tree in_decl = t;
saved_loc = input_location;
input_location = DECL_SOURCE_LOCATION (t);
switch (TREE_CODE (t))
{
case TEMPLATE_DECL:
{
tree decl = DECL_TEMPLATE_RESULT (t);
tree spec;
tree tmpl_args;
tree full_args;
if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
{
tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (new_type == error_mark_node)
return error_mark_node;
r = copy_decl (t);
TREE_CHAIN (r) = NULL_TREE;
TREE_TYPE (r) = new_type;
DECL_TEMPLATE_RESULT (r)
= build_decl (TYPE_DECL, DECL_NAME (decl), new_type);
DECL_TEMPLATE_PARMS (r)
= tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
complain);
TYPE_NAME (new_type) = r;
break;
}
tmpl_args = DECL_CLASS_TEMPLATE_P (t)
? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
: DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
full_args = tsubst_template_args (tmpl_args, args,
complain, in_decl);
gcc_assert (full_args != tmpl_args);
spec = retrieve_specialization (t, full_args,
true);
if (spec != NULL_TREE)
{
r = spec;
break;
}
r = copy_decl (t);
gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
TREE_CHAIN (r) = NULL_TREE;
DECL_CONTEXT (r)
= tsubst_aggr_type (DECL_CONTEXT (t), args,
complain, 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, complain, in_decl);
if (new_type == error_mark_node)
return error_mark_node;
TREE_TYPE (r) = new_type;
CLASSTYPE_TI_TEMPLATE (new_type) = r;
DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
}
else
{
tree new_decl = tsubst (decl, args, complain, in_decl);
if (new_decl == error_mark_node)
return error_mark_node;
DECL_TEMPLATE_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,
complain);
if (PRIMARY_TEMPLATE_P (t))
DECL_PRIMARY_TEMPLATE (r) = r;
if (TREE_CODE (decl) != TYPE_DECL)
register_specialization (r, t,
DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)));
}
break;
case FUNCTION_DECL:
{
tree ctx;
tree argvec = NULL_TREE;
tree *friends;
tree gen_tmpl;
tree type;
int member;
int args_depth;
int parms_depth;
gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
{
tree spec;
bool dependent_p;
++processing_template_decl;
dependent_p = value_dependent_expression_p (t);
--processing_template_decl;
if (!dependent_p)
return t;
gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
argvec = tsubst_template_args (DECL_TI_ARGS
(DECL_TEMPLATE_RESULT (gen_tmpl)),
args, complain, in_decl);
spec = retrieve_specialization (gen_tmpl, argvec,
false);
if (spec)
{
r = spec;
break;
}
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))
args = get_innermost_template_args (args, parms_depth);
}
else
{
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_CONTEXT (t), args,
complain, t, 1);
}
else
{
member = 0;
ctx = DECL_CONTEXT (t);
}
type = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (type == error_mark_node)
return error_mark_node;
r = copy_decl (t);
DECL_USE_TEMPLATE (r) = 0;
TREE_TYPE (r) = type;
SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
SET_DECL_RTL (r, NULL_RTX);
DECL_INITIAL (r) = NULL_TREE;
DECL_CONTEXT (r) = ctx;
if (member && DECL_CONV_FN_P (r))
DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
complain, t);
DECL_RESULT (r) = NULL_TREE;
TREE_STATIC (r) = 0;
TREE_PUBLIC (r) = TREE_PUBLIC (t);
DECL_EXTERNAL (r) = 1;
DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
DECL_DEFER_OUTPUT (r) = 0;
TREE_CHAIN (r) = NULL_TREE;
DECL_PENDING_INLINE_INFO (r) = 0;
DECL_PENDING_INLINE_P (r) = 0;
DECL_SAVED_TREE (r) = NULL_TREE;
TREE_USED (r) = 0;
if (DECL_CLONED_FUNCTION (r))
{
DECL_CLONED_FUNCTION (r) = tsubst (DECL_CLONED_FUNCTION (t),
args, complain, t);
TREE_CHAIN (r) = TREE_CHAIN (DECL_CLONED_FUNCTION (r));
TREE_CHAIN (DECL_CLONED_FUNCTION (r)) = r;
}
if (gen_tmpl)
{
DECL_TEMPLATE_INFO (r)
= tree_cons (gen_tmpl, argvec, NULL_TREE);
SET_DECL_IMPLICIT_INSTANTIATION (r);
register_specialization (r, gen_tmpl, argvec);
if (!member
&& !PRIMARY_TEMPLATE_P (gen_tmpl)
&& !uses_template_parms (argvec))
tsubst_default_arguments (r);
}
for (friends = &DECL_BEFRIENDING_CLASSES (r);
*friends;
friends = &TREE_CHAIN (*friends))
{
*friends = copy_node (*friends);
TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
args, complain,
in_decl);
}
if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
{
maybe_retrofit_in_chrg (r);
if (DECL_CONSTRUCTOR_P (r))
grok_ctor_properties (ctx, r);
if (PRIMARY_TEMPLATE_P (gen_tmpl))
clone_function_decl (r, 0);
}
else if (IDENTIFIER_OPNAME_P (DECL_NAME (r)))
grok_op_properties (r, (complain & tf_error) != 0);
if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
SET_DECL_FRIEND_CONTEXT (r,
tsubst (DECL_FRIEND_CONTEXT (t),
args, complain, in_decl));
}
break;
case PARM_DECL:
{
tree type;
r = copy_node (t);
if (DECL_TEMPLATE_PARM_P (t))
SET_DECL_TEMPLATE_PARM_P (r);
type = tsubst (TREE_TYPE (t), args, complain, in_decl);
type = type_decays_to (type);
TREE_TYPE (r) = type;
cp_apply_type_quals_to_decl (cp_type_quals (type), r);
if (DECL_INITIAL (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,
complain, in_decl);
}
DECL_CONTEXT (r) = NULL_TREE;
if (!DECL_TEMPLATE_PARM_P (r))
DECL_ARG_TYPE (r) = type_passed_as (type);
if (TREE_CHAIN (t))
TREE_CHAIN (r) = tsubst (TREE_CHAIN (t), args,
complain, TREE_CHAIN (t));
}
break;
case FIELD_DECL:
{
tree type;
r = copy_decl (t);
type = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (type == error_mark_node)
return error_mark_node;
TREE_TYPE (r) = type;
cp_apply_type_quals_to_decl (cp_type_quals (type), r);
DECL_INITIAL (r) = tsubst_expr (DECL_INITIAL (t), args,
complain, in_decl);
TREE_CHAIN (r) = NULL_TREE;
if (VOID_TYPE_P (type))
cp_error_at ("instantiation of %qD as type %qT", r, type);
}
break;
case USING_DECL:
{
r = copy_node (t);
TREE_TYPE (r) = void_type_node;
DECL_INITIAL (r)
= tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
DECL_NAME (r)
= tsubst_copy (DECL_NAME (t), args, complain, in_decl);
TREE_CHAIN (r) = NULL_TREE;
}
break;
case TYPE_DECL:
case VAR_DECL:
{
tree argvec = NULL_TREE;
tree gen_tmpl = NULL_TREE;
tree spec;
tree tmpl = NULL_TREE;
tree ctx;
tree type = NULL_TREE;
int local_p;
if (TREE_CODE (t) == TYPE_DECL)
{
type = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
|| t == TYPE_MAIN_DECL (TREE_TYPE (t)))
{
r = TYPE_NAME (type);
break;
}
}
local_p = 0;
if (TYPE_P (CP_DECL_CONTEXT (t)))
ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
complain,
in_decl, 1);
else if (DECL_NAMESPACE_SCOPE_P (t))
ctx = DECL_CONTEXT (t);
else
{
ctx = NULL_TREE;
local_p = 1;
}
if (!local_p)
{
tmpl = DECL_TI_TEMPLATE (t);
gen_tmpl = most_general_template (tmpl);
argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
spec = retrieve_specialization (gen_tmpl, argvec,
false);
}
else
spec = retrieve_local_specialization (t);
if (spec)
{
r = spec;
break;
}
r = copy_decl (t);
if (TREE_CODE (r) == VAR_DECL)
{
type = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (type == error_mark_node)
return error_mark_node;
type = complete_type (type);
DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (t);
type = check_var_type (DECL_NAME (r), type);
}
else if (DECL_SELF_REFERENCE_P (t))
SET_DECL_SELF_REFERENCE_P (r);
TREE_TYPE (r) = type;
cp_apply_type_quals_to_decl (cp_type_quals (type), r);
DECL_CONTEXT (r) = ctx;
SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
SET_DECL_RTL (r, NULL_RTX);
DECL_INITIAL (r) = NULL_TREE;
SET_DECL_RTL (r, NULL_RTX);
DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
if (TREE_CODE (r) == VAR_DECL)
{
DECL_DEAD_FOR_LOCAL (r) = 0;
DECL_INITIALIZED_P (r) = 0;
}
if (!local_p)
{
DECL_EXTERNAL (r) = 1;
register_specialization (r, gen_tmpl, argvec);
DECL_TEMPLATE_INFO (r) = tree_cons (tmpl, argvec, NULL_TREE);
SET_DECL_IMPLICIT_INSTANTIATION (r);
}
else
register_local_specialization (r, t);
TREE_CHAIN (r) = NULL_TREE;
layout_decl (r, 0);
}
break;
default:
gcc_unreachable ();
}
input_location = saved_loc;
return r;
}
static tree
tsubst_arg_types (tree arg_types,
tree args,
tsubst_flags_t 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;
if (VOID_TYPE_P (type))
{
if (complain & tf_error)
{
error ("invalid parameter type %qT", type);
if (in_decl)
cp_error_at ("in declaration %qD", in_decl);
}
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 (tree t,
tree args,
tsubst_flags_t complain,
tree in_decl)
{
tree return_type;
tree arg_types;
tree fntype;
gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
if (return_type == error_mark_node)
return error_mark_node;
if (TREE_CODE (return_type) == ARRAY_TYPE
|| TREE_CODE (return_type) == FUNCTION_TYPE)
{
if (complain & tf_error)
{
if (TREE_CODE (return_type) == ARRAY_TYPE)
error ("function returning an array");
else
error ("function returning a function");
}
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 & tf_error)
error ("creating pointer to member function of non-class type %qT",
r);
return error_mark_node;
}
fntype = build_method_type_directly (r, return_type,
TREE_CHAIN (arg_types));
}
fntype = cp_build_qualified_type_real (fntype, TYPE_QUALS (t), complain);
fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
return fntype;
}
static tree
tsubst_exception_specification (tree fntype,
tree args,
tsubst_flags_t complain,
tree in_decl)
{
tree specs;
tree new_specs;
specs = TYPE_RAISES_EXCEPTIONS (fntype);
new_specs = NULL_TREE;
if (specs)
{
if (! TREE_VALUE (specs))
new_specs = specs;
else
while (specs)
{
tree spec;
spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
if (spec == error_mark_node)
return spec;
new_specs = add_exception_specifier (new_specs, spec, complain);
specs = TREE_CHAIN (specs);
}
}
return new_specs;
}
static tree
tsubst_call_declarator_parms (tree parms,
tree args,
tsubst_flags_t 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);
return new_parms;
}
static tree
tsubst (tree t, tree args, tsubst_flags_t 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
|| t == unknown_type_node
|| TREE_CODE (t) == NAMESPACE_DECL)
return t;
if (DECL_P (t))
return tsubst_decl (t, args, complain);
if (TREE_CODE (t) == IDENTIFIER_NODE)
type = IDENTIFIER_TYPE_VALUE (t);
else
type = TREE_TYPE (t);
gcc_assert (type != unknown_type_node);
if (type
&& TREE_CODE (t) != TYPENAME_TYPE
&& 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;
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 VOID_TYPE:
case REAL_TYPE:
case COMPLEX_TYPE:
case VECTOR_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_template_arg (omax, args, complain, in_decl);
max = fold_decl_constant_value (max);
if (integer_zerop (omax))
{
if (pedantic)
pedwarn ("creating array with size zero");
}
else if (integer_zerop (max)
|| (TREE_CODE (max) == INTEGER_CST
&& INT_CST_LT (max, integer_zero_node)))
{
if (complain & tf_error)
error ("creating array with size zero (%qE)", max);
return error_mark_node;
}
return compute_array_index_type (NULL_TREE, max);
}
case TEMPLATE_TYPE_PARM:
case TEMPLATE_TEMPLATE_PARM:
case BOUND_TEMPLATE_TEMPLATE_PARM:
case TEMPLATE_PARM_INDEX:
{
int idx;
int level;
int levels;
tree arg = NULL_TREE;
r = NULL_TREE;
gcc_assert (TREE_VEC_LENGTH (args) > 0);
if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
|| TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
|| TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
{
idx = TEMPLATE_TYPE_IDX (t);
level = TEMPLATE_TYPE_LEVEL (t);
}
else
{
idx = TEMPLATE_PARM_IDX (t);
level = TEMPLATE_PARM_LEVEL (t);
}
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)
{
gcc_assert (TYPE_P (arg));
return cp_build_qualified_type_real
(arg, cp_type_quals (arg) | cp_type_quals (t),
complain | tf_ignore_bad_quals);
}
else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
{
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 (arg,
argvec, in_decl,
DECL_CONTEXT (arg),
0,
complain);
return cp_build_qualified_type_real
(r, TYPE_QUALS (t), complain);
}
else
return arg;
}
if (level == 1)
return t;
switch (TREE_CODE (t))
{
case TEMPLATE_TYPE_PARM:
case TEMPLATE_TEMPLATE_PARM:
case BOUND_TEMPLATE_TEMPLATE_PARM:
if (cp_type_quals (t))
{
r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
r = cp_build_qualified_type_real
(r, cp_type_quals (t),
complain | (TREE_CODE (t) == TEMPLATE_TYPE_PARM
? tf_ignore_bad_quals : 0));
}
else
{
r = copy_type (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) == BOUND_TEMPLATE_TEMPLATE_PARM)
{
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)
= tree_cons (TYPE_TI_TEMPLATE (t), argvec, NULL_TREE);
}
}
break;
case TEMPLATE_PARM_INDEX:
r = reduce_template_parm_level (t, type, levels);
break;
default:
gcc_unreachable ();
}
return r;
}
case TREE_LIST:
{
tree purpose, value, chain;
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;
return hash_tree_cons (purpose, value, chain);
}
case TREE_BINFO:
gcc_unreachable ();
case TREE_VEC:
gcc_assert (!type);
return tsubst_template_args (t, args, complain, in_decl);
case POINTER_TYPE:
case REFERENCE_TYPE:
{
enum tree_code code;
if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
return t;
code = TREE_CODE (t);
if (TREE_CODE (type) == REFERENCE_TYPE
|| (code == REFERENCE_TYPE && TREE_CODE (type) == VOID_TYPE))
{
static location_t last_loc;
if (complain & tf_error
#ifdef USE_MAPPED_LOCATION
&& last_loc != input_location
#else
&& (last_loc.line != input_line
|| last_loc.file != input_filename)
#endif
)
{
if (TREE_CODE (type) == VOID_TYPE)
error ("forming reference to void");
else
error ("forming %s to reference type %qT",
(code == POINTER_TYPE) ? "pointer" : "reference",
type);
last_loc = input_location;
}
return error_mark_node;
}
else if (code == POINTER_TYPE)
{
r = build_pointer_type (type);
if (TREE_CODE (type) == METHOD_TYPE)
r = build_ptrmemfunc_type (r);
}
else
r = build_reference_type (type);
r = cp_build_qualified_type_real (r, TYPE_QUALS (t), complain);
if (r != error_mark_node)
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 & tf_error)
error ("creating pointer to member of non-class type %qT", r);
return error_mark_node;
}
if (TREE_CODE (type) == REFERENCE_TYPE)
{
if (complain & tf_error)
error ("creating pointer to member reference type %qT", type);
return error_mark_node;
}
gcc_assert (TREE_CODE (type) != METHOD_TYPE);
if (TREE_CODE (type) == FUNCTION_TYPE)
{
tree method_type;
tree this_type = cp_build_qualified_type (TYPE_MAIN_VARIANT (r),
cp_type_quals (type));
tree memptr;
method_type = build_method_type_directly (this_type,
TREE_TYPE (type),
TYPE_ARG_TYPES (type));
memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
return cp_build_qualified_type_real (memptr, cp_type_quals (t),
complain);
}
else
return cp_build_qualified_type_real (build_ptrmem_type (r, type),
TYPE_QUALS (t),
complain);
}
case FUNCTION_TYPE:
case METHOD_TYPE:
{
tree fntype;
tree specs;
fntype = tsubst_function_type (t, args, complain, in_decl);
if (fntype == error_mark_node)
return error_mark_node;
specs = tsubst_exception_specification (t, args, complain,
in_decl);
if (specs)
fntype = build_exception_variant (fntype, specs);
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 & tf_error)
error ("creating array of %qT", type);
return error_mark_node;
}
if (CLASS_TYPE_P (type) && CLASSTYPE_PURE_VIRTUALS (type))
{
if (complain & tf_error)
error ("creating array of %qT, which is an abstract class type",
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 (build2 (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 (build1 (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 & tf_error)
error ("%qT 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 (!COMPLETE_TYPE_P (ctx))
{
if (complain & tf_error)
cxx_incomplete_type_error (NULL_TREE, ctx);
return error_mark_node;
}
}
f = make_typename_type (ctx, f, typename_type,
(complain & tf_error) | tf_keep_type_decl);
if (f == error_mark_node)
return f;
if (TREE_CODE (f) == TYPE_DECL)
{
complain |= tf_ignore_bad_quals;
f = TREE_TYPE (f);
}
if (TREE_CODE (f) != TYPENAME_TYPE)
{
if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
error ("%qT resolves to %qT, which is not an enumeration type",
t, f);
else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
error ("%qT resolves to %qT, which is is not a class type",
t, f);
}
return cp_build_qualified_type_real
(f, cp_type_quals (f) | cp_type_quals (t), complain);
}
case UNBOUND_CLASS_TEMPLATE:
{
tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
in_decl, 1);
tree name = TYPE_IDENTIFIER (t);
tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
if (ctx == error_mark_node || name == error_mark_node)
return error_mark_node;
if (parm_list)
parm_list = tsubst_template_parms (parm_list, args, complain);
return make_unbound_class_template (ctx, name, parm_list, complain);
}
case INDIRECT_REF:
case ADDR_EXPR:
case CALL_EXPR:
gcc_unreachable ();
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_nt (ARRAY_REF, e1, e2, NULL_TREE, NULL_TREE);
}
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_nt (TREE_CODE (t), e1, e2);
}
case TYPEOF_TYPE:
{
tree type;
type = finish_typeof (tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
complain, in_decl));
return cp_build_qualified_type_real (type,
cp_type_quals (t)
| cp_type_quals (type),
complain);
}
default:
sorry ("use of %qs in template",
tree_code_name [(int) TREE_CODE (t)]);
return error_mark_node;
}
}
static tree
tsubst_baselink (tree baselink, tree object_type,
tree args, tsubst_flags_t complain, tree in_decl)
{
tree name;
tree qualifying_scope;
tree fns;
tree template_args = 0;
bool template_id_p = false;
qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
fns = BASELINK_FUNCTIONS (baselink);
if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
{
template_id_p = true;
template_args = TREE_OPERAND (fns, 1);
fns = TREE_OPERAND (fns, 0);
if (template_args)
template_args = tsubst_template_args (template_args, args,
complain, in_decl);
}
name = DECL_NAME (get_first_fn (fns));
baselink = lookup_fnfields (qualifying_scope, name, 1);
if (BASELINK_P (baselink))
fns = BASELINK_FUNCTIONS (baselink);
if (!template_id_p && !really_overloaded_fn (fns))
mark_used (OVL_CURRENT (fns));
if (BASELINK_P (baselink) && template_id_p)
BASELINK_FUNCTIONS (baselink)
= build_nt (TEMPLATE_ID_EXPR,
BASELINK_FUNCTIONS (baselink),
template_args);
if (!object_type)
object_type = current_class_type;
return adjust_result_of_qualified_name_lookup (baselink,
qualifying_scope,
object_type);
}
static tree
tsubst_qualified_id (tree qualified_id, tree args,
tsubst_flags_t complain, tree in_decl,
bool done, bool address_p)
{
tree expr;
tree scope;
tree name;
bool is_template;
tree template_args;
gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
name = TREE_OPERAND (qualified_id, 1);
if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
{
is_template = true;
template_args = TREE_OPERAND (name, 1);
if (template_args)
template_args = tsubst_template_args (template_args, args,
complain, in_decl);
name = TREE_OPERAND (name, 0);
}
else
{
is_template = false;
template_args = NULL_TREE;
}
scope = TREE_OPERAND (qualified_id, 0);
if (args)
{
scope = tsubst (scope, args, complain, in_decl);
expr = tsubst_copy (name, args, complain, in_decl);
}
else
expr = name;
if (dependent_type_p (scope))
return build_nt (SCOPE_REF, scope, expr);
if (!BASELINK_P (name) && !DECL_P (expr))
{
expr = lookup_qualified_name (scope, expr, 0, false);
if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
{
if (complain & tf_error)
{
error ("dependent-name %qE is parsed as a non-type, but "
"instantiation yields a type", qualified_id);
inform ("say %<typename %E%> if a type is meant", qualified_id);
}
return error_mark_node;
}
}
if (DECL_P (expr))
{
check_accessibility_of_qualified_id (expr, NULL_TREE,
scope);
mark_used (expr);
}
if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
{
if (complain & tf_error)
qualified_name_lookup_error (scope,
TREE_OPERAND (qualified_id, 1),
expr);
return error_mark_node;
}
if (is_template)
expr = lookup_template_function (expr, template_args);
if (expr == error_mark_node && complain & tf_error)
qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
expr);
else if (TYPE_P (scope))
{
expr = (adjust_result_of_qualified_name_lookup
(expr, scope, current_class_type));
expr = finish_qualified_id_expr (scope, expr, done, address_p);
}
expr = convert_from_reference (expr);
return expr;
}
static tree
tsubst_copy (tree t, tree args, tsubst_flags_t 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:
r = retrieve_local_specialization (t);
gcc_assert (r != NULL);
mark_used (r);
return r;
case CONST_DECL:
{
tree enum_type;
tree v;
if (DECL_TEMPLATE_PARM_P (t))
return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
if (DECL_NAMESPACE_SCOPE_P (t))
return t;
if (args == NULL_TREE)
return integral_constant_value (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);
gcc_unreachable ();
}
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))
{
tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
if (!r)
{
if (complain & tf_error)
error ("using invalid field %qD", t);
return error_mark_node;
}
return r;
}
}
return t;
case VAR_DECL:
case FUNCTION_DECL:
if ((DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
|| local_variable_p (t))
t = tsubst (t, args, complain, in_decl);
mark_used (t);
return t;
case BASELINK:
return tsubst_baselink (t, current_class_type, args, complain, in_decl);
case TEMPLATE_DECL:
if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
args, complain, in_decl);
else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
return tsubst (t, args, complain, in_decl);
else if (DECL_CLASS_SCOPE_P (t)
&& uses_template_parms (DECL_CONTEXT (t)))
{
tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
return lookup_field (context, DECL_NAME(t), 0, false);
}
else
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 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:
case REALPART_EXPR:
case IMAGPART_EXPR:
return build1
(code, tsubst (TREE_TYPE (t), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl));
case COMPONENT_REF:
{
tree object;
tree name;
object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
name = TREE_OPERAND (t, 1);
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 if (TREE_CODE (name) == BASELINK)
name = tsubst_baselink (name,
non_reference (TREE_TYPE (object)),
args, complain,
in_decl);
else
name = tsubst_copy (name, args, complain, in_decl);
return build_nt (COMPONENT_REF, object, name, NULL_TREE);
}
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_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 COMPOUND_EXPR:
case SCOPE_REF:
case DOTSTAR_EXPR:
case MEMBER_REF:
case PREDECREMENT_EXPR:
case PREINCREMENT_EXPR:
case POSTDECREMENT_EXPR:
case POSTINCREMENT_EXPR:
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 ARRAY_REF:
return build_nt
(ARRAY_REF,
tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl),
NULL_TREE, NULL_TREE);
case CALL_EXPR:
return build_nt (code,
tsubst_copy (TREE_OPERAND (t, 0), args,
complain, in_decl),
tsubst_copy (TREE_OPERAND (t, 1), args, complain,
in_decl),
NULL_TREE);
case COND_EXPR:
case MODOP_EXPR:
case PSEUDO_DTOR_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));
TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
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 fn = TREE_OPERAND (t, 0);
tree targs = TREE_OPERAND (t, 1);
fn = tsubst_copy (fn, args, complain, in_decl);
if (targs)
targs = tsubst_template_args (targs, args, complain, in_decl);
return lookup_template_function (fn, 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 BOUND_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 UNBOUND_CLASS_TEMPLATE:
case TYPEOF_TYPE:
case TYPE_DECL:
return tsubst (t, args, complain, in_decl);
case IDENTIFIER_NODE:
if (IDENTIFIER_TYPENAME_P (t))
{
tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
return mangle_conv_op_name_for_type (new_type);
}
else
return t;
case CONSTRUCTOR:
{
r = build_constructor
(tsubst (TREE_TYPE (t), args, complain, in_decl),
tsubst_copy (CONSTRUCTOR_ELTS (t), args, complain, in_decl));
TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
return r;
}
case VA_ARG_EXPR:
return build_x_va_arg (tsubst_copy (TREE_OPERAND (t, 0), args, complain,
in_decl),
tsubst (TREE_TYPE (t), args, complain, in_decl));
case CLEANUP_POINT_EXPR:
gcc_unreachable ();
default:
return t;
}
}
static tree
tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
tree stmt, tmp;
if (t == NULL_TREE || t == error_mark_node)
return t;
if (EXPR_HAS_LOCATION (t))
input_location = EXPR_LOCATION (t);
if (STATEMENT_CODE_P (TREE_CODE (t)))
current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
switch (TREE_CODE (t))
{
case STATEMENT_LIST:
{
tree_stmt_iterator i;
for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
tsubst_expr (tsi_stmt (i), args, complain, in_decl);
break;
}
case CTOR_INITIALIZER:
finish_mem_initializers (tsubst_initializer_list
(TREE_OPERAND (t, 0), args));
break;
case RETURN_EXPR:
finish_return_stmt (tsubst_expr (TREE_OPERAND (t, 0),
args, complain, in_decl));
break;
case EXPR_STMT:
tmp = tsubst_expr (EXPR_STMT_EXPR (t), args, complain, in_decl);
if (EXPR_STMT_STMT_EXPR_RESULT (t))
finish_stmt_expr_expr (tmp, cur_stmt_expr);
else
finish_expr_stmt (tmp);
break;
case USING_STMT:
do_using_directive (tsubst_expr (USING_STMT_NAMESPACE (t),
args, complain, in_decl));
break;
case DECL_EXPR:
{
tree decl;
tree init;
decl = DECL_EXPR_DECL (t);
if (TREE_CODE (decl) == LABEL_DECL)
finish_label_decl (DECL_NAME (decl));
else if (TREE_CODE (decl) == USING_DECL)
{
tree scope = DECL_INITIAL (decl);
tree name = DECL_NAME (decl);
tree decl;
scope = tsubst_expr (scope, args, complain, in_decl);
decl = lookup_qualified_name (scope, name,
false,
false);
if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
qualified_name_lookup_error (scope, name, decl);
else
do_local_using_decl (decl, scope, name);
}
else
{
init = DECL_INITIAL (decl);
decl = tsubst (decl, args, complain, in_decl);
if (decl != error_mark_node)
{
if (init)
DECL_INITIAL (decl) = error_mark_node;
if (TREE_CODE (decl) == VAR_DECL)
DECL_TEMPLATE_INSTANTIATED (decl) = 1;
if (TREE_CODE (decl) == VAR_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
finish_anon_union (decl);
else
{
maybe_push_decl (decl);
if (TREE_CODE (decl) == VAR_DECL
&& DECL_PRETTY_FUNCTION_P (decl))
{
const char *const name
= cxx_printable_name (current_function_decl, 2);
init = cp_fname_init (name, &TREE_TYPE (decl));
}
else
init = tsubst_expr (init, args, complain, in_decl);
cp_finish_decl (decl, init, NULL_TREE, 0);
}
}
}
return decl;
}
case FOR_STMT:
stmt = begin_for_stmt ();
tsubst_expr (FOR_INIT_STMT (t), args, complain, in_decl);
finish_for_init_stmt (stmt);
tmp = tsubst_expr (FOR_COND (t), args, complain, in_decl);
finish_for_cond (tmp, stmt);
tmp = tsubst_expr (FOR_EXPR (t), args, complain, in_decl);
finish_for_expr (tmp, stmt);
tsubst_expr (FOR_BODY (t), args, complain, in_decl);
finish_for_stmt (stmt);
break;
case WHILE_STMT:
stmt = begin_while_stmt ();
tmp = tsubst_expr (WHILE_COND (t), args, complain, in_decl);
finish_while_stmt_cond (tmp, stmt);
tsubst_expr (WHILE_BODY (t), args, complain, in_decl);
finish_while_stmt (stmt);
break;
case DO_STMT:
stmt = begin_do_stmt ();
tsubst_expr (DO_BODY (t), args, complain, in_decl);
finish_do_body (stmt);
tmp = tsubst_expr (DO_COND (t), args, complain, in_decl);
finish_do_stmt (tmp, stmt);
break;
case IF_STMT:
stmt = begin_if_stmt ();
tmp = tsubst_expr (IF_COND (t), args, complain, in_decl);
finish_if_stmt_cond (tmp, stmt);
tsubst_expr (THEN_CLAUSE (t), args, complain, in_decl);
finish_then_clause (stmt);
if (ELSE_CLAUSE (t))
{
begin_else_clause (stmt);
tsubst_expr (ELSE_CLAUSE (t), args, complain, in_decl);
finish_else_clause (stmt);
}
finish_if_stmt (stmt);
break;
case BIND_EXPR:
if (BIND_EXPR_BODY_BLOCK (t))
stmt = begin_function_body ();
else
stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
? BCS_TRY_BLOCK : 0);
tsubst_expr (BIND_EXPR_BODY (t), args, complain, in_decl);
if (BIND_EXPR_BODY_BLOCK (t))
finish_function_body (stmt);
else
finish_compound_stmt (stmt);
break;
case BREAK_STMT:
finish_break_stmt ();
break;
case CONTINUE_STMT:
finish_continue_stmt ();
break;
case SWITCH_STMT:
stmt = begin_switch_stmt ();
tmp = tsubst_expr (SWITCH_STMT_COND (t), args, complain, in_decl);
finish_switch_cond (tmp, stmt);
tsubst_expr (SWITCH_STMT_BODY (t), args, complain, in_decl);
finish_switch_stmt (stmt);
break;
case CASE_LABEL_EXPR:
finish_case_label (tsubst_expr (CASE_LOW (t), args, complain, in_decl),
tsubst_expr (CASE_HIGH (t), args, complain,
in_decl));
break;
case LABEL_EXPR:
finish_label_stmt (DECL_NAME (LABEL_EXPR_LABEL (t)));
break;
case GOTO_EXPR:
tmp = GOTO_DESTINATION (t);
if (TREE_CODE (tmp) != LABEL_DECL)
tmp = tsubst_expr (tmp, args, complain, in_decl);
else
tmp = DECL_NAME (tmp);
finish_goto_stmt (tmp);
break;
case ASM_EXPR:
tmp = finish_asm_stmt
(ASM_VOLATILE_P (t),
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),
tsubst_expr (ASM_USES (t), args, complain, in_decl));
{
tree asm_expr = tmp;
if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
asm_expr = TREE_OPERAND (asm_expr, 0);
ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
}
break;
case TRY_BLOCK:
if (CLEANUP_P (t))
{
stmt = begin_try_block ();
tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
finish_cleanup_try_block (stmt);
finish_cleanup (tsubst_expr (TRY_HANDLERS (t), args,
complain, in_decl),
stmt);
}
else
{
if (FN_TRY_BLOCK_P (t))
stmt = begin_function_try_block ();
else
stmt = begin_try_block ();
tsubst_expr (TRY_STMTS (t), args, complain, in_decl);
if (FN_TRY_BLOCK_P (t))
finish_function_try_block (stmt);
else
finish_try_block (stmt);
tsubst_expr (TRY_HANDLERS (t), args, complain, in_decl);
if (FN_TRY_BLOCK_P (t))
finish_function_handler_sequence (stmt);
else
finish_handler_sequence (stmt);
}
break;
case HANDLER:
{
tree decl;
stmt = begin_handler ();
if (HANDLER_PARMS (t))
{
decl = HANDLER_PARMS (t);
decl = tsubst (decl, args, complain, in_decl);
DECL_TEMPLATE_INSTANTIATED (decl) = 1;
}
else
decl = NULL_TREE;
finish_handler_parms (decl, stmt);
tsubst_expr (HANDLER_BODY (t), args, complain, in_decl);
finish_handler (stmt);
}
break;
case TAG_DEFN:
tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
break;
default:
gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
return tsubst_copy_and_build (t, args, complain, in_decl,
false);
}
return NULL_TREE;
}
static tree
tsubst_non_call_postfix_expression (tree t, tree args,
tsubst_flags_t complain,
tree in_decl)
{
if (TREE_CODE (t) == SCOPE_REF)
t = tsubst_qualified_id (t, args, complain, in_decl,
false, false);
else
t = tsubst_copy_and_build (t, args, complain, in_decl,
false);
return t;
}
tree
tsubst_copy_and_build (tree t,
tree args,
tsubst_flags_t complain,
tree in_decl,
bool function_p)
{
#define RECUR(NODE) \
tsubst_copy_and_build (NODE, args, complain, in_decl, false)
tree op1;
if (t == NULL_TREE || t == error_mark_node)
return t;
switch (TREE_CODE (t))
{
case USING_DECL:
t = DECL_NAME (t);
case IDENTIFIER_NODE:
{
tree decl;
cp_id_kind idk;
tree qualifying_class;
bool non_integral_constant_expression_p;
const char *error_msg;
if (IDENTIFIER_TYPENAME_P (t))
{
tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
t = mangle_conv_op_name_for_type (new_type);
}
decl = lookup_name (t, 0);
if (decl == NULL_TREE)
decl = error_mark_node;
decl = finish_id_expression (t, decl, NULL_TREE,
&idk,
&qualifying_class,
false,
false,
&non_integral_constant_expression_p,
&error_msg);
if (error_msg)
error (error_msg);
if (!function_p && TREE_CODE (decl) == IDENTIFIER_NODE)
decl = unqualified_name_lookup_error (decl);
return decl;
}
case TEMPLATE_ID_EXPR:
{
tree object;
tree template = RECUR (TREE_OPERAND (t, 0));
tree targs = TREE_OPERAND (t, 1);
if (targs)
targs = tsubst_template_args (targs, args, complain, in_decl);
if (TREE_CODE (template) == COMPONENT_REF)
{
object = TREE_OPERAND (template, 0);
template = TREE_OPERAND (template, 1);
}
else
object = NULL_TREE;
template = lookup_template_function (template, targs);
if (object)
return build3 (COMPONENT_REF, TREE_TYPE (template),
object, template, NULL_TREE);
else
return template;
}
case INDIRECT_REF:
{
tree r = RECUR (TREE_OPERAND (t, 0));
if (REFERENCE_REF_P (t))
{
if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
r = convert_from_reference (r);
}
else
r = build_x_indirect_ref (r, "unary *");
return r;
}
case NOP_EXPR:
return build_nop
(tsubst (TREE_TYPE (t), args, complain, in_decl),
RECUR (TREE_OPERAND (t, 0)));
case CAST_EXPR:
return build_functional_cast
(tsubst (TREE_TYPE (t), args, complain, in_decl),
RECUR (TREE_OPERAND (t, 0)));
case REINTERPRET_CAST_EXPR:
return build_reinterpret_cast
(tsubst (TREE_TYPE (t), args, complain, in_decl),
RECUR (TREE_OPERAND (t, 0)));
case CONST_CAST_EXPR:
return build_const_cast
(tsubst (TREE_TYPE (t), args, complain, in_decl),
RECUR (TREE_OPERAND (t, 0)));
case DYNAMIC_CAST_EXPR:
return build_dynamic_cast
(tsubst (TREE_TYPE (t), args, complain, in_decl),
RECUR (TREE_OPERAND (t, 0)));
case STATIC_CAST_EXPR:
return build_static_cast
(tsubst (TREE_TYPE (t), args, complain, in_decl),
RECUR (TREE_OPERAND (t, 0)));
case POSTDECREMENT_EXPR:
case POSTINCREMENT_EXPR:
op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
args, complain, in_decl);
return build_x_unary_op (TREE_CODE (t), op1);
case PREDECREMENT_EXPR:
case PREINCREMENT_EXPR:
case NEGATE_EXPR:
case BIT_NOT_EXPR:
case ABS_EXPR:
case TRUTH_NOT_EXPR:
case CONVERT_EXPR:
case REALPART_EXPR:
case IMAGPART_EXPR:
return build_x_unary_op (TREE_CODE (t), RECUR (TREE_OPERAND (t, 0)));
case ADDR_EXPR:
op1 = TREE_OPERAND (t, 0);
if (TREE_CODE (op1) == SCOPE_REF)
op1 = tsubst_qualified_id (op1, args, complain, in_decl,
true, true);
else
op1 = tsubst_non_call_postfix_expression (op1, args, complain,
in_decl);
if (TREE_CODE (op1) == LABEL_DECL)
return finish_label_address_expr (DECL_NAME (op1));
return build_x_unary_op (ADDR_EXPR, op1);
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_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 MEMBER_REF:
case DOTSTAR_EXPR:
return build_x_binary_op
(TREE_CODE (t),
RECUR (TREE_OPERAND (t, 0)),
RECUR (TREE_OPERAND (t, 1)),
NULL);
case SCOPE_REF:
return tsubst_qualified_id (t, args, complain, in_decl, true,
false);
case ARRAY_REF:
op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
args, complain, in_decl);
return build_x_binary_op (ARRAY_REF, op1, RECUR (TREE_OPERAND (t, 1)),
NULL);
case SIZEOF_EXPR:
case ALIGNOF_EXPR:
op1 = TREE_OPERAND (t, 0);
if (!args)
{
if (!TYPE_P (op1))
op1 = TREE_TYPE (op1);
}
else
{
++skip_evaluation;
op1 = RECUR (op1);
--skip_evaluation;
}
if (TYPE_P (op1))
return cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), true);
else
return cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t));
case MODOP_EXPR:
{
tree r = build_x_modify_expr
(RECUR (TREE_OPERAND (t, 0)),
TREE_CODE (TREE_OPERAND (t, 1)),
RECUR (TREE_OPERAND (t, 2)));
if (TREE_NO_WARNING (t))
TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
return r;
}
case ARROW_EXPR:
op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
args, complain, in_decl);
if (DECL_P (op1))
mark_used (op1);
return build_x_arrow (op1);
case NEW_EXPR:
return build_new
(RECUR (TREE_OPERAND (t, 0)),
RECUR (TREE_OPERAND (t, 1)),
RECUR (TREE_OPERAND (t, 2)),
RECUR (TREE_OPERAND (t, 3)),
NEW_EXPR_USE_GLOBAL (t));
case DELETE_EXPR:
return delete_sanity
(RECUR (TREE_OPERAND (t, 0)),
RECUR (TREE_OPERAND (t, 1)),
DELETE_EXPR_USE_VEC (t),
DELETE_EXPR_USE_GLOBAL (t));
case COMPOUND_EXPR:
return build_x_compound_expr (RECUR (TREE_OPERAND (t, 0)),
RECUR (TREE_OPERAND (t, 1)));
case CALL_EXPR:
{
tree function;
tree call_args;
bool qualified_p;
bool koenig_p;
function = TREE_OPERAND (t, 0);
koenig_p = KOENIG_LOOKUP_P (t);
if (TREE_CODE (function) == SCOPE_REF)
{
qualified_p = true;
function = tsubst_qualified_id (function, args, complain, in_decl,
false,
false);
}
else
{
qualified_p = (TREE_CODE (function) == COMPONENT_REF
&& (TREE_CODE (TREE_OPERAND (function, 1))
== SCOPE_REF));
function = tsubst_copy_and_build (function, args, complain,
in_decl,
!qualified_p);
if (BASELINK_P (function))
qualified_p = true;
}
call_args = RECUR (TREE_OPERAND (t, 1));
if (koenig_p
&& ((is_overloaded_fn (function)
&& !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
|| TREE_CODE (function) == IDENTIFIER_NODE))
function = perform_koenig_lookup (function, call_args);
if (TREE_CODE (function) == IDENTIFIER_NODE)
{
unqualified_name_lookup_error (function);
return error_mark_node;
}
if (DECL_P (function))
mark_used (function);
if (TREE_CODE (function) == OFFSET_REF)
return build_offset_ref_call_from_tree (function, call_args);
if (TREE_CODE (function) == COMPONENT_REF)
{
if (!BASELINK_P (TREE_OPERAND (function, 1)))
return finish_call_expr (function, call_args,
false,
false);
else
return (build_new_method_call
(TREE_OPERAND (function, 0),
TREE_OPERAND (function, 1),
call_args, NULL_TREE,
qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL));
}
return finish_call_expr (function, call_args,
qualified_p,
koenig_p);
}
case COND_EXPR:
return build_x_conditional_expr
(RECUR (TREE_OPERAND (t, 0)),
RECUR (TREE_OPERAND (t, 1)),
RECUR (TREE_OPERAND (t, 2)));
case PSEUDO_DTOR_EXPR:
return finish_pseudo_destructor_expr
(RECUR (TREE_OPERAND (t, 0)),
RECUR (TREE_OPERAND (t, 1)),
RECUR (TREE_OPERAND (t, 2)));
case TREE_LIST:
{
tree purpose, value, chain;
if (t == void_list_node)
return t;
purpose = TREE_PURPOSE (t);
if (purpose)
purpose = RECUR (purpose);
value = TREE_VALUE (t);
if (value)
value = RECUR (value);
chain = TREE_CHAIN (t);
if (chain && chain != void_type_node)
chain = RECUR (chain);
if (purpose == TREE_PURPOSE (t)
&& value == TREE_VALUE (t)
&& chain == TREE_CHAIN (t))
return t;
return tree_cons (purpose, value, chain);
}
case COMPONENT_REF:
{
tree object;
tree member;
object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
args, complain, in_decl);
if (DECL_P (object))
mark_used (object);
member = TREE_OPERAND (t, 1);
if (BASELINK_P (member))
member = tsubst_baselink (member,
non_reference (TREE_TYPE (object)),
args, complain, in_decl);
else
member = tsubst_copy (member, args, complain, in_decl);
if (member == error_mark_node)
return error_mark_node;
else if (!CLASS_TYPE_P (TREE_TYPE (object)))
{
if (TREE_CODE (member) == BIT_NOT_EXPR)
return finish_pseudo_destructor_expr (object,
NULL_TREE,
TREE_TYPE (object));
else if (TREE_CODE (member) == SCOPE_REF
&& (TREE_CODE (TREE_OPERAND (member, 1)) == BIT_NOT_EXPR))
return finish_pseudo_destructor_expr (object,
object,
TREE_TYPE (object));
}
else if (TREE_CODE (member) == SCOPE_REF
&& TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
{
tree tmpl;
tree args;
tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
member = lookup_qualified_name (TREE_OPERAND (member, 0), tmpl,
false,
false);
if (BASELINK_P (member))
{
BASELINK_FUNCTIONS (member)
= build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
args);
member = (adjust_result_of_qualified_name_lookup
(member, BINFO_TYPE (BASELINK_BINFO (member)),
TREE_TYPE (object)));
}
else
{
qualified_name_lookup_error (TREE_TYPE (object), tmpl,
member);
return error_mark_node;
}
}
else if (TREE_CODE (member) == SCOPE_REF
&& !CLASS_TYPE_P (TREE_OPERAND (member, 0))
&& TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
{
if (complain & tf_error)
{
if (TYPE_P (TREE_OPERAND (member, 0)))
error ("%qT is not a class or namespace",
TREE_OPERAND (member, 0));
else
error ("%qD is not a class or namespace",
TREE_OPERAND (member, 0));
}
return error_mark_node;
}
else if (TREE_CODE (member) == FIELD_DECL)
return finish_non_static_data_member (member, object, NULL_TREE);
return finish_class_member_access_expr (object, member);
}
case THROW_EXPR:
return build_throw
(RECUR (TREE_OPERAND (t, 0)));
case CONSTRUCTOR:
{
tree r;
tree elts;
tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
bool purpose_p;
if (type && TYPE_PTRMEMFUNC_P (type))
return t;
r = NULL_TREE;
purpose_p = !(type && IS_AGGR_TYPE (type));
for (elts = CONSTRUCTOR_ELTS (t);
elts;
elts = TREE_CHAIN (elts))
{
tree purpose = TREE_PURPOSE (elts);
tree value = TREE_VALUE (elts);
if (purpose && purpose_p)
purpose = RECUR (purpose);
value = RECUR (value);
r = tree_cons (purpose, value, r);
}
r = build_constructor (NULL_TREE, nreverse (r));
TREE_HAS_CONSTRUCTOR (r) = TREE_HAS_CONSTRUCTOR (t);
if (type)
return digest_init (type, r, 0);
return r;
}
case TYPEID_EXPR:
{
tree operand_0 = RECUR (TREE_OPERAND (t, 0));
if (TYPE_P (operand_0))
return get_typeid (operand_0);
return build_typeid (operand_0);
}
case VAR_DECL:
if (!args)
return t;
case PARM_DECL:
{
tree r = tsubst_copy (t, args, complain, in_decl);
if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
r = convert_from_reference (r);
return r;
}
case VA_ARG_EXPR:
return build_x_va_arg (RECUR (TREE_OPERAND (t, 0)),
tsubst_copy (TREE_TYPE (t), args, complain,
in_decl));
case OFFSETOF_EXPR:
return fold_offsetof (RECUR (TREE_OPERAND (t, 0)));
case STMT_EXPR:
{
tree old_stmt_expr = cur_stmt_expr;
tree stmt_expr = begin_stmt_expr ();
cur_stmt_expr = stmt_expr;
tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl);
stmt_expr = finish_stmt_expr (stmt_expr, false);
cur_stmt_expr = old_stmt_expr;
return stmt_expr;
}
case CONST_DECL:
t = tsubst_copy (t, args, complain, in_decl);
if (TREE_CODE (t) == CONST_DECL)
return DECL_INITIAL (t);
return t;
default:
{
tree subst
= objcp_tsubst_copy_and_build (t, args, complain,
in_decl, false);
if (subst)
return subst;
}
return tsubst_copy (t, args, complain, in_decl);
}
#undef RECUR
}
static bool
check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
{
int ix, len = DECL_NTPARMS (tmpl);
bool result = false;
bool error_p = complain & tf_error;
for (ix = 0; ix != len; ix++)
{
tree t = TREE_VEC_ELT (args, ix);
if (TYPE_P (t))
{
tree nt = no_linkage_check (t, false);
if (nt)
{
if (TYPE_ANONYMOUS_P (nt))
error ("%qT is/uses anonymous type", t);
else
error ("%qT uses local type %qT", t, nt);
result = true;
error_p = true;
}
else if (variably_modified_type_p (t, NULL_TREE))
{
if (complain & tf_error)
error ("%qT is a variably modified type", t);
result = true;
}
}
else if (TREE_TYPE (t)
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
&& !TREE_CONSTANT (t))
{
if (complain & tf_error)
error ("integral expression %qE is not constant", t);
result = true;
}
}
if (result && error_p)
error (" trying to instantiate %qD", tmpl);
return result;
}
tree
instantiate_template (tree tmpl, tree targ_ptr, tsubst_flags_t complain)
{
tree fndecl;
tree gen_tmpl;
tree spec;
if (tmpl == error_mark_node)
return error_mark_node;
gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
if (DECL_CLONED_FUNCTION_P (tmpl))
{
tree spec;
tree clone;
spec = instantiate_template (DECL_CLONED_FUNCTION (tmpl), targ_ptr,
complain);
if (spec == error_mark_node)
return error_mark_node;
FOR_EACH_CLONE (clone, spec)
if (DECL_NAME (clone) == DECL_NAME (tmpl))
return clone;
gcc_unreachable ();
return NULL_TREE;
}
spec = retrieve_specialization (tmpl, targ_ptr,
false);
if (spec != NULL_TREE)
return spec;
gen_tmpl = most_general_template (tmpl);
if (tmpl != gen_tmpl)
{
targ_ptr = add_outermost_template_args (DECL_TI_ARGS (tmpl),
targ_ptr);
spec = retrieve_specialization (gen_tmpl, targ_ptr,
false);
if (spec != NULL_TREE)
return spec;
}
if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
complain))
return error_mark_node;
push_deferring_access_checks (dk_deferred);
fndecl = tsubst (DECL_TEMPLATE_RESULT (gen_tmpl),
targ_ptr, complain, gen_tmpl);
push_access_scope (fndecl);
perform_deferred_access_checks ();
pop_access_scope (fndecl);
pop_deferring_access_checks ();
DECL_TI_TEMPLATE (fndecl) = tmpl;
if (TREE_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (TREE_CHAIN (gen_tmpl)))
clone_function_decl (fndecl, 0);
return fndecl;
}
int
fn_type_unification (tree fn,
tree explicit_targs,
tree targs,
tree args,
tree return_type,
unification_kind_t strict,
int len,
int flags)
{
tree parms;
tree fntype;
int result;
gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
fntype = TREE_TYPE (fn);
if (explicit_targs)
{
int i;
tree converted_args;
bool incomplete;
if (explicit_targs == error_mark_node)
return 1;
converted_args
= (coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
explicit_targs, NULL_TREE, tf_none,
0));
if (converted_args == error_mark_node)
return 1;
incomplete = NUM_TMPL_ARGS (explicit_targs) != NUM_TMPL_ARGS (targs);
processing_template_decl += incomplete;
fntype = tsubst (fntype, converted_args, tf_none, NULL_TREE);
processing_template_decl -= incomplete;
if (fntype == error_mark_node)
return 1;
for (i = NUM_TMPL_ARGS (converted_args); i--;)
TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (converted_args, i);
}
parms = TYPE_ARG_TYPES (fntype);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
parms = TREE_CHAIN (parms);
if (return_type)
{
parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
args = tree_cons (NULL_TREE, return_type, args);
if (len >= 0)
++len;
}
result = type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
targs, parms, args, 0,
strict, 0, len, flags);
if (result == 0)
if (tsubst (TREE_TYPE (fn), targs, tf_none, NULL_TREE)
== error_mark_node)
return 1;
return result;
}
static int
maybe_adjust_types_for_deduction (unification_kind_t strict,
tree* parm,
tree* arg)
{
int result = 0;
switch (strict)
{
case DEDUCE_CALL:
break;
case DEDUCE_CONV:
{
tree* temp = parm;
parm = arg;
arg = temp;
break;
}
case DEDUCE_EXACT:
return 0;
default:
gcc_unreachable ();
}
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);
result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
}
if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
*arg = TREE_TYPE (*arg);
return result;
}
static int
type_unification_real (tree tparms,
tree targs,
tree xparms,
tree xargs,
int subr,
unification_kind_t strict,
int is_method,
int xlen,
int flags)
{
tree parm, arg;
int i;
int ntparms = TREE_VEC_LENGTH (tparms);
int sub_strict;
int saw_undeduced = 0;
tree parms, args;
int len;
gcc_assert (TREE_CODE (tparms) == TREE_VEC);
gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
gcc_assert (!xargs || TREE_CODE (xargs) == TREE_LIST);
gcc_assert (ntparms > 0);
switch (strict)
{
case DEDUCE_CALL:
sub_strict = (UNIFY_ALLOW_OUTER_LEVEL | 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:
gcc_unreachable ();
}
if (xlen == 0)
return 0;
again:
parms = xparms;
args = xargs;
len = xlen;
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 (is_method)
{
gcc_assert (TREE_CODE (parm) == POINTER_TYPE);
gcc_assert (TREE_CODE (arg) == POINTER_TYPE);
if (cp_type_quals (parm) != cp_type_quals (arg))
return 1;
parm = TREE_TYPE (parm);
arg = TREE_TYPE (arg);
if (cp_type_quals (parm) != cp_type_quals (arg))
return 1;
parm = TYPE_MAIN_VARIANT (parm);
arg = TYPE_MAIN_VARIANT (arg);
is_method = 0;
}
if (!uses_template_parms (parm))
{
tree type;
if (!TYPE_P (arg))
type = TREE_TYPE (arg);
else
type = arg;
if (same_type_p (parm, type))
continue;
if (strict != DEDUCE_EXACT
&& can_convert_arg (parm, type, TYPE_P (arg) ? NULL_TREE : arg, flags))
continue;
return 1;
}
if (!TYPE_P (arg))
{
gcc_assert (TREE_TYPE (arg) != NULL_TREE);
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 (arg == error_mark_node)
return 1;
}
{
int arg_strict = sub_strict;
if (!subr)
arg_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
if (unify (tparms, targs, parm, arg, arg_strict))
return 1;
}
if (--len == 0)
goto done;
}
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;
done:
if (!subr)
for (i = 0; i < ntparms; i++)
if (TREE_VEC_ELT (targs, i) == NULL_TREE)
{
tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
if (TREE_CODE (tparm) == PARM_DECL
&& uses_template_parms (TREE_TYPE (tparm))
&& !saw_undeduced++)
goto again;
return 2;
}
return 0;
}
static int
resolve_overloaded_unification (tree tparms,
tree targs,
tree parm,
tree arg,
unification_kind_t strict,
int sub_strict)
{
tree tempargs = copy_node (targs);
int good = 0;
bool addr_p;
if (TREE_CODE (arg) == ADDR_EXPR)
{
arg = TREE_OPERAND (arg, 0);
addr_p = true;
}
else
addr_p = false;
if (TREE_CODE (arg) == COMPONENT_REF)
arg = TREE_OPERAND (arg, 1);
if (TREE_CODE (arg) == OFFSET_REF)
arg = TREE_OPERAND (arg, 1);
if (BASELINK_P (arg))
arg = BASELINK_FUNCTIONS (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_TEMPLATE_RESULT (fn),
expl_subargs);
if (subargs)
{
elem = tsubst (TREE_TYPE (fn), subargs, tf_none, NULL_TREE);
good += try_one_overload (tparms, targs, tempargs, parm,
elem, strict, sub_strict, addr_p);
}
}
}
else
{
gcc_assert (TREE_CODE (arg) == OVERLOAD
|| TREE_CODE (arg) == FUNCTION_DECL);
for (; arg; arg = OVL_NEXT (arg))
good += try_one_overload (tparms, targs, tempargs, parm,
TREE_TYPE (OVL_CURRENT (arg)),
strict, sub_strict, addr_p);
}
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 (tree tparms,
tree orig_targs,
tree targs,
tree parm,
tree arg,
unification_kind_t strict,
int sub_strict,
bool addr_p)
{
int nargs;
tree tempargs;
int i;
if (uses_template_parms (arg))
return 1;
if (TREE_CODE (arg) == METHOD_TYPE)
arg = build_ptrmemfunc_type (build_pointer_type (arg));
else if (addr_p)
arg = build_pointer_type (arg);
sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg);
nargs = TREE_VEC_LENGTH (targs);
tempargs = make_tree_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 int
verify_class_unification (tree targs, tree parms, tree args)
{
parms = tsubst (parms, add_outermost_template_args (args, targs),
tf_none, NULL_TREE);
if (parms == error_mark_node)
return 1;
return !comp_template_args (parms, INNERMOST_TEMPLATE_ARGS (args));
}
static tree
try_class_unification (tree tparms, tree targs, tree parm, tree arg)
{
tree copy_of_targs;
if (!CLASSTYPE_TEMPLATE_INFO (arg)
|| (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
!= most_general_template (CLASSTYPE_TI_TEMPLATE (parm))))
return NULL_TREE;
copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE))
return NULL_TREE;
return arg;
}
static tree
get_template_base (tree tparms, tree targs, tree parm, tree arg)
{
tree rval = NULL_TREE;
tree binfo;
gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (arg)));
binfo = TYPE_BINFO (complete_type (arg));
if (!binfo)
return NULL_TREE;
for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
{
tree r = try_class_unification (tparms, targs, parm, BINFO_TYPE (binfo));
if (r)
{
if (rval && !same_type_p (r, rval))
return NULL_TREE;
rval = r;
}
}
return rval;
}
static int
template_decl_level (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:
gcc_unreachable ();
}
return 0;
}
static int
check_cv_quals_for_unify (int strict, tree arg, tree parm)
{
int arg_quals = cp_type_quals (arg);
int parm_quals = cp_type_quals (parm);
if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
&& !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
{
if ((TREE_CODE (arg) == REFERENCE_TYPE
|| TREE_CODE (arg) == FUNCTION_TYPE
|| TREE_CODE (arg) == METHOD_TYPE)
&& (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
return 0;
if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
&& (parm_quals & TYPE_QUAL_RESTRICT))
return 0;
}
if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
&& (arg_quals & parm_quals) != parm_quals)
return 0;
if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
&& (parm_quals & arg_quals) != arg_quals)
return 0;
return 1;
}
static int
unify (tree tparms, tree targs, tree parm, tree arg, int strict)
{
int idx;
tree targ;
tree tparm;
int strict_in = strict;
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)
&& TYPE_P (arg)
&& TREE_CODE (arg) != ARRAY_TYPE
&& TREE_CODE (arg) != TEMPLATE_TYPE_PARM
&& !check_cv_quals_for_unify (strict_in, arg, parm))
return 1;
if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
&& TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
strict &= ~UNIFY_ALLOW_DERIVED;
strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
switch (TREE_CODE (parm))
{
case TYPENAME_TYPE:
case SCOPE_REF:
case UNBOUND_CLASS_TEMPLATE:
return 0;
case TEMPLATE_TYPE_PARM:
case TEMPLATE_TEMPLATE_PARM:
case BOUND_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) == BOUND_TEMPLATE_TEMPLATE_PARM)
{
if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
&& (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 = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
tree argtmplvec
= DECL_INNERMOST_TEMPLATE_PARMS (TYPE_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)
{
if (unify (tparms, targs,
TREE_VEC_ELT (parmvec, i),
TREE_VEC_ELT (argvec, i),
UNIFY_ALLOW_NONE))
return 1;
}
}
arg = TYPE_TI_TEMPLATE (arg);
}
if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
|| TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
{
if (targ != NULL_TREE && template_args_equal (targ, arg))
return 0;
else if (targ)
return 1;
}
else
{
if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
arg, parm))
return 1;
arg = cp_build_qualified_type_real
(arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
if (arg == error_mark_node)
return 1;
if (targ != NULL_TREE && same_type_p (targ, arg))
return 0;
else if (targ)
return 1;
if (variably_modified_type_p (arg, NULL_TREE))
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));
idx = TEMPLATE_PARM_IDX (parm);
targ = TREE_VEC_ELT (targs, idx);
if (targ)
return !cp_tree_equal (targ, arg);
tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
if (!TREE_TYPE (arg))
;
else if (same_type_p (TREE_TYPE (arg), tparm))
;
else if ((strict & UNIFY_ALLOW_INTEGER)
&& (TREE_CODE (tparm) == INTEGER_TYPE
|| TREE_CODE (tparm) == BOOLEAN_TYPE))
arg = fold (build_nop (TREE_TYPE (parm), arg));
else if (uses_template_parms (tparm))
return 0;
else
return 1;
TREE_VEC_ELT (targs, idx) = arg;
return 0;
case PTRMEM_CST:
{
if (TREE_CODE (arg) != PTRMEM_CST)
return 1;
return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
PTRMEM_CST_MEMBER (arg), strict);
}
case POINTER_TYPE:
{
if (TREE_CODE (arg) != POINTER_TYPE)
return 1;
if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
strict |= (strict_in & UNIFY_ALLOW_DERIVED);
return unify (tparms, targs, TREE_TYPE (parm),
TREE_TYPE (arg), strict);
}
case REFERENCE_TYPE:
if (TREE_CODE (arg) != REFERENCE_TYPE)
return 1;
return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
strict & UNIFY_ALLOW_MORE_CV_QUAL);
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)
{
tree parm_max;
tree arg_max;
parm_max = TYPE_MAX_VALUE (TYPE_DOMAIN (parm));
arg_max = TYPE_MAX_VALUE (TYPE_DOMAIN (arg));
if (TREE_CODE (parm_max) == MINUS_EXPR)
{
arg_max = fold (build2 (PLUS_EXPR,
integer_type_node,
arg_max,
TREE_OPERAND (parm_max, 1)));
parm_max = TREE_OPERAND (parm_max, 0);
}
if (unify (tparms, targs, parm_max, arg_max, UNIFY_ALLOW_INTEGER))
return 1;
}
return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
strict & UNIFY_ALLOW_MORE_CV_QUAL);
case REAL_TYPE:
case COMPLEX_TYPE:
case VECTOR_TYPE:
case INTEGER_TYPE:
case BOOLEAN_TYPE:
case ENUMERAL_TYPE:
case VOID_TYPE:
if (TREE_CODE (arg) != TREE_CODE (parm))
return 1;
if (!same_type_ignoring_top_level_qualifiers_p (arg, 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 = 0; i < TREE_VEC_LENGTH (parm); ++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 (TREE_CODE (arg) != TREE_CODE (parm))
return 1;
if (TYPE_PTRMEMFUNC_P (parm))
{
if (!TYPE_PTRMEMFUNC_P (arg))
return 1;
return unify (tparms, targs,
TYPE_PTRMEMFUNC_FN_TYPE (parm),
TYPE_PTRMEMFUNC_FN_TYPE (arg),
strict);
}
if (CLASSTYPE_TEMPLATE_INFO (parm))
{
tree t = NULL_TREE;
if (strict_in & UNIFY_ALLOW_DERIVED)
{
t = try_class_unification (tparms, targs,
parm, arg);
if (!t)
{
t = get_template_base (tparms, targs, parm, arg);
if (!t)
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_ignoring_top_level_qualifiers_p (parm, 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,
TREE_CODE (parm) == METHOD_TYPE, -1, LOOKUP_NORMAL);
case OFFSET_TYPE:
if (TYPE_PTRMEMFUNC_P (arg))
{
tree method_type;
tree fntype;
cp_cv_quals cv_quals;
if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
return 1;
if (unify (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
TYPE_PTRMEMFUNC_OBJECT_TYPE (arg), UNIFY_ALLOW_NONE))
return 1;
method_type = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (arg));
fntype =
build_function_type (TREE_TYPE (method_type),
TREE_CHAIN (TYPE_ARG_TYPES (method_type)));
cv_quals =
cp_type_quals(TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (method_type))));
fntype = build_qualified_type (fntype, cv_quals);
return unify (tparms, targs, TREE_TYPE (parm), fntype, strict);
}
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 (DECL_TEMPLATE_PARM_P (parm))
return unify (tparms, targs, DECL_INITIAL (parm), arg, strict);
if (arg != integral_constant_value (parm))
return 1;
return 0;
case FIELD_DECL:
case TEMPLATE_DECL:
return 1;
default:
gcc_assert (EXPR_P (parm));
if (!uses_template_parms (parm)
&& !template_args_equal (parm, arg))
return 1;
else
return 0;
}
}
static void
mark_definable (tree decl)
{
tree clone;
DECL_NOT_REALLY_EXTERN (decl) = 1;
FOR_EACH_CLONE (clone, decl)
DECL_NOT_REALLY_EXTERN (clone) = 1;
}
void
mark_decl_instantiated (tree result, int extern_p)
{
SET_DECL_EXPLICIT_INSTANTIATION (result);
if (TREE_ASM_WRITTEN (result))
return;
if (TREE_CODE (result) != FUNCTION_DECL)
TREE_PUBLIC (result) = 1;
DECL_COMDAT (result) = 0;
if (extern_p)
DECL_NOT_REALLY_EXTERN (result) = 0;
else
{
mark_definable (result);
if (DECL_ARTIFICIAL (result) && flag_weak)
comdat_linkage (result);
else if (TREE_PUBLIC (result))
maybe_make_one_only (result);
}
DECL_INTERFACE_KNOWN (result) = 1;
}
int
more_specialized_fn (tree pat1, tree pat2, int len)
{
tree decl1 = DECL_TEMPLATE_RESULT (pat1);
tree decl2 = DECL_TEMPLATE_RESULT (pat2);
tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
int better1 = 0;
int better2 = 0;
if (DECL_FUNCTION_MEMBER_P (decl1) != DECL_FUNCTION_MEMBER_P (decl2))
return 0;
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
args1 = TREE_CHAIN (args1);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
args2 = TREE_CHAIN (args2);
if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
return 0;
if (DECL_CONV_FN_P (decl1))
{
args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
len++;
}
processing_template_decl++;
while (len--)
{
tree arg1 = TREE_VALUE (args1);
tree arg2 = TREE_VALUE (args2);
int deduce1, deduce2;
int quals1 = -1;
int quals2 = -1;
if (TREE_CODE (arg1) == REFERENCE_TYPE)
{
arg1 = TREE_TYPE (arg1);
quals1 = cp_type_quals (arg1);
}
if (TREE_CODE (arg2) == REFERENCE_TYPE)
{
arg2 = TREE_TYPE (arg2);
quals2 = cp_type_quals (arg2);
}
if ((quals1 < 0) != (quals2 < 0))
{
if (quals1 >= 0)
{
switch (TREE_CODE (arg1))
{
case ARRAY_TYPE:
arg1 = TREE_TYPE (arg1);
case FUNCTION_TYPE:
arg1 = build_pointer_type (arg1);
break;
default:
break;
}
}
else
{
switch (TREE_CODE (arg2))
{
case ARRAY_TYPE:
arg2 = TREE_TYPE (arg2);
case FUNCTION_TYPE:
arg2 = build_pointer_type (arg2);
break;
default:
break;
}
}
}
arg1 = TYPE_MAIN_VARIANT (arg1);
arg2 = TYPE_MAIN_VARIANT (arg2);
deduce1 = !unify (tparms1, targs1, arg1, arg2, UNIFY_ALLOW_NONE);
deduce2 = !unify (tparms2, targs2, arg2, arg1, UNIFY_ALLOW_NONE);
if (!deduce1)
better2 = -1;
if (!deduce2)
better1 = -1;
if (better1 < 0 && better2 < 0)
break;
if (deduce1 && deduce2 && quals1 >= 0 && quals2 >= 0)
{
if ((quals1 & quals2) == quals2)
deduce1 = 0;
if ((quals1 & quals2) == quals1)
deduce2 = 0;
}
if (deduce1 && !deduce2 && !better2)
better2 = 1;
if (deduce2 && !deduce1 && !better1)
better1 = 1;
args1 = TREE_CHAIN (args1);
args2 = TREE_CHAIN (args2);
}
processing_template_decl--;
return (better1 > 0) - (better2 > 0);
}
int
more_specialized_class (tree pat1, tree pat2, tree full_args)
{
tree targs;
int winner = 0;
++processing_template_decl;
targs = get_class_bindings (TREE_VALUE (pat1), TREE_PURPOSE (pat1),
add_outermost_template_args (full_args, TREE_PURPOSE (pat2)));
if (targs)
--winner;
targs = get_class_bindings (TREE_VALUE (pat2), TREE_PURPOSE (pat2),
add_outermost_template_args (full_args, TREE_PURPOSE (pat1)));
if (targs)
++winner;
--processing_template_decl;
return winner;
}
static tree
get_bindings_real (tree fn,
tree decl,
tree explicit_args,
int check_rettype,
int deduce,
int len)
{
int ntparms = DECL_NTPARMS (fn);
tree targs = make_tree_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,
tf_none, 0));
if (converted_args == error_mark_node)
return NULL_TREE;
decl_type = tsubst (decl_type, converted_args, tf_none, NULL_TREE);
if (decl_type == error_mark_node)
return NULL_TREE;
}
decl_arg_types = TYPE_ARG_TYPES (decl_type);
if (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,
(check_rettype || DECL_CONV_FN_P (fn)
? TREE_TYPE (decl_type) : NULL_TREE),
deduce, len, LOOKUP_NORMAL);
if (i != 0)
return NULL_TREE;
return targs;
}
static tree
get_bindings (tree fn, tree decl, tree explicit_args)
{
return get_bindings_real (fn, decl, explicit_args, 1, DEDUCE_EXACT, -1);
}
static tree
get_bindings_overload (tree fn, tree decl, tree explicit_args)
{
return get_bindings_real (fn, decl, explicit_args, 0, DEDUCE_EXACT, -1);
}
static tree
get_class_bindings (tree tparms, tree parms, tree args)
{
int i, ntparms = TREE_VEC_LENGTH (tparms);
tree vec = make_tree_vec (ntparms);
if (unify (tparms, vec, parms, INNERMOST_TEMPLATE_ARGS (args),
UNIFY_ALLOW_NONE))
return NULL_TREE;
for (i = 0; i < ntparms; ++i)
if (! TREE_VEC_ELT (vec, i))
return NULL_TREE;
if (verify_class_unification (vec, parms, args))
return NULL_TREE;
return vec;
}
tree
most_specialized_instantiation (tree instantiations)
{
tree fn, champ;
if (!instantiations)
return NULL_TREE;
++processing_template_decl;
champ = instantiations;
for (fn = TREE_CHAIN (instantiations); fn; fn = TREE_CHAIN (fn))
{
int fate = 0;
if (get_bindings_real (TREE_VALUE (champ),
DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
NULL_TREE, 0, DEDUCE_EXACT, -1))
fate--;
if (get_bindings_real (TREE_VALUE (fn),
DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
NULL_TREE, 0, DEDUCE_EXACT, -1))
fate++;
if (fate != 1)
{
if (!fate)
fn = TREE_CHAIN (fn);
champ = fn;
}
}
if (champ)
for (fn = instantiations; fn != champ; fn = TREE_CHAIN (fn))
if (get_bindings_real (TREE_VALUE (champ),
DECL_TEMPLATE_RESULT (TREE_VALUE (fn)),
NULL_TREE, 0, DEDUCE_EXACT, -1)
|| !get_bindings_real (TREE_VALUE (fn),
DECL_TEMPLATE_RESULT (TREE_VALUE (champ)),
NULL_TREE, 0, DEDUCE_EXACT, -1))
{
champ = NULL_TREE;
break;
}
processing_template_decl--;
if (!champ)
return error_mark_node;
return TREE_PURPOSE (champ) ? TREE_PURPOSE (champ) : TREE_VALUE (champ);
}
static tree
most_specialized (tree fns, tree decl, tree 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 = tree_cons (NULL_TREE, candidate, candidates);
}
return most_specialized_instantiation (candidates);
}
tree
most_general_template (tree decl)
{
if (TREE_CODE (decl) == FUNCTION_DECL)
{
if (DECL_TEMPLATE_INFO (decl)) {
decl = DECL_TI_TEMPLATE (decl);
if (TREE_CODE (decl) != TEMPLATE_DECL)
return NULL_TREE;
} else
return NULL_TREE;
}
while (DECL_TEMPLATE_INFO (decl))
{
if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
break;
if (CLASS_TYPE_P (TREE_TYPE (decl))
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
break;
if (!DECL_NAMESPACE_SCOPE_P (decl)
&& DECL_CONTEXT (decl)
&& CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
break;
decl = DECL_TI_TEMPLATE (decl);
}
return decl;
}
static tree
most_specialized_class (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 = 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, args);
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, args);
if (fate != 1)
return error_mark_node;
}
return champ;
}
void
do_decl_instantiation (tree decl, tree storage)
{
tree result = NULL_TREE;
int extern_p = 0;
if (!decl)
return;
else if (! DECL_LANG_SPECIFIC (decl))
{
error ("explicit instantiation of non-template %q#D", decl);
return;
}
else if (TREE_CODE (decl) == VAR_DECL)
{
result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
if (!result || TREE_CODE (result) != VAR_DECL)
{
error ("no matching template for %qD found", decl);
return;
}
}
else if (TREE_CODE (decl) != FUNCTION_DECL)
{
error ("explicit instantiation of %q#D", decl);
return;
}
else
result = decl;
if (DECL_TEMPLATE_SPECIALIZATION (result))
{
return;
}
else if (DECL_EXPLICIT_INSTANTIATION (result))
{
if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
pedwarn ("duplicate explicit instantiation of %q#D", result);
if (extern_p)
return;
}
else if (!DECL_IMPLICIT_INSTANTIATION (result))
{
error ("no matching template for %qD found", result);
return;
}
else if (!DECL_TEMPLATE_INFO (result))
{
pedwarn ("explicit instantiation of non-template %q#D", result);
return;
}
if (storage == NULL_TREE)
;
else if (storage == ridpointers[(int) RID_EXTERN])
{
if (pedantic && !in_system_header)
pedwarn ("ISO C++ forbids the use of %<extern%> on explicit "
"instantiations");
extern_p = 1;
}
else
error ("storage class %qD applied to template instantiation", storage);
mark_decl_instantiated (result, extern_p);
if (! extern_p)
instantiate_decl (result, 1, 0);
}
void
mark_class_instantiated (tree t, int extern_p)
{
SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
SET_CLASSTYPE_INTERFACE_KNOWN (t);
CLASSTYPE_INTERFACE_ONLY (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);
}
}
static void
bt_instantiate_type_proc (binding_entry entry, void *data)
{
tree storage = *(tree *) data;
if (IS_AGGR_TYPE (entry->type)
&& !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
}
static void
instantiate_class_member (tree decl, int extern_p)
{
mark_decl_instantiated (decl, extern_p);
if (! extern_p)
instantiate_decl (decl, 1, 1);
}
void
do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
{
int extern_p = 0;
int nomem_p = 0;
int static_p = 0;
int previous_instantiation_extern_p = 0;
if (TREE_CODE (t) == TYPE_DECL)
t = TREE_TYPE (t);
if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
{
error ("explicit instantiation of non-template type %qT", t);
return;
}
complete_type (t);
if (!COMPLETE_TYPE_P (t))
{
if (complain & tf_error)
error ("explicit instantiation of %q#T before definition of template",
t);
return;
}
if (storage != NULL_TREE)
{
if (pedantic && !in_system_header)
pedwarn("ISO C++ forbids the use of %qE on explicit instantiations",
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
{
error ("storage class %qD applied to template instantiation",
storage);
extern_p = 0;
}
}
if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
{
return;
}
else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
{
previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
if (!previous_instantiation_extern_p && !extern_p
&& (complain & tf_error))
pedwarn ("duplicate explicit instantiation of %q#T", t);
if (!CLASSTYPE_INTERFACE_ONLY (t))
return;
}
mark_class_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))
instantiate_class_member (tmp, extern_p);
for (tmp = TYPE_FIELDS (t); tmp; tmp = TREE_CHAIN (tmp))
if (TREE_CODE (tmp) == VAR_DECL && DECL_TEMPLATE_INSTANTIATION (tmp))
instantiate_class_member (tmp, extern_p);
if (CLASSTYPE_NESTED_UTDS (t))
binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
bt_instantiate_type_proc, &storage);
}
}
static void
regenerate_decl_from_template (tree decl, tree tmpl)
{
tree args;
tree code_pattern;
args = DECL_TI_ARGS (decl);
code_pattern = DECL_TEMPLATE_RESULT (tmpl);
push_access_scope (decl);
if (TREE_CODE (decl) == FUNCTION_DECL)
{
tree decl_parm;
tree pattern_parm;
tree specs;
int args_depth;
int parms_depth;
args_depth = TMPL_ARGS_DEPTH (args);
parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
if (args_depth > parms_depth)
args = get_innermost_template_args (args, parms_depth);
specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
args, tf_error, NULL_TREE);
if (specs)
TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
specs);
decl_parm = skip_artificial_parms_for (decl,
DECL_ARGUMENTS (decl));
pattern_parm
= skip_artificial_parms_for (code_pattern,
DECL_ARGUMENTS (code_pattern));
while (decl_parm)
{
tree parm_type;
tree attributes;
if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
NULL_TREE);
parm_type = type_decays_to (parm_type);
if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
TREE_TYPE (decl_parm) = parm_type;
attributes = DECL_ATTRIBUTES (pattern_parm);
if (DECL_ATTRIBUTES (decl_parm) != attributes)
{
DECL_ATTRIBUTES (decl_parm) = attributes;
cplus_decl_attributes (&decl_parm, attributes, 0);
}
decl_parm = TREE_CHAIN (decl_parm);
pattern_parm = TREE_CHAIN (pattern_parm);
}
if (DECL_DECLARED_INLINE_P (code_pattern)
&& !DECL_DECLARED_INLINE_P (decl))
DECL_DECLARED_INLINE_P (decl) = 1;
if (DECL_INLINE (code_pattern) && !DECL_INLINE (decl))
DECL_INLINE (decl) = 1;
}
else if (TREE_CODE (decl) == VAR_DECL)
{
if (!DECL_INITIALIZED_IN_CLASS_P (decl)
&& DECL_INITIAL (code_pattern))
DECL_INITIAL (decl) =
tsubst_expr (DECL_INITIAL (code_pattern), args,
tf_error, DECL_TI_TEMPLATE (decl));
}
else
gcc_unreachable ();
pop_access_scope (decl);
}
tree
template_for_substitution (tree decl)
{
tree tmpl = DECL_TI_TEMPLATE (decl);
while (
DECL_TEMPLATE_INSTANTIATION (tmpl)
|| (TREE_CODE (decl) == FUNCTION_DECL
&& DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
&& !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
{
gcc_assert (TREE_CODE (decl) != VAR_DECL
|| DECL_IN_AGGR_P (DECL_TEMPLATE_RESULT (tmpl)));
tmpl = DECL_TI_TEMPLATE (tmpl);
}
return tmpl;
}
tree
instantiate_decl (tree d, int defer_ok, int undefined_ok)
{
tree tmpl = DECL_TI_TEMPLATE (d);
tree gen_args;
tree args;
tree td;
tree code_pattern;
tree spec;
tree gen_tmpl;
int pattern_defined;
int need_push;
location_t saved_loc = input_location;
gcc_assert (TREE_CODE (d) == FUNCTION_DECL
|| TREE_CODE (d) == VAR_DECL);
if (TREE_CODE (d) == VAR_DECL)
defer_ok = 0;
if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
d = DECL_CLONED_FUNCTION (d);
if (DECL_TEMPLATE_INSTANTIATED (d))
return d;
gen_tmpl = most_general_template (tmpl);
gen_args = DECL_TI_ARGS (d);
spec = retrieve_specialization (gen_tmpl, gen_args,
false);
if (spec != NULL_TREE && DECL_TEMPLATE_SPECIALIZATION (spec))
return spec;
if (! push_tinst_level (d))
return d;
timevar_push (TV_PARSE);
push_deferring_access_checks (dk_no_deferred);
td = template_for_substitution (d);
code_pattern = DECL_TEMPLATE_RESULT (td);
if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
|| DECL_TEMPLATE_SPECIALIZATION (td))
args = get_innermost_template_args
(gen_args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
else
args = gen_args;
if (TREE_CODE (d) == FUNCTION_DECL)
pattern_defined = (DECL_SAVED_TREE (code_pattern) != NULL_TREE);
else
pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
if (pattern_defined
&& !DECL_INTERFACE_KNOWN (d)
&& !DECL_NOT_REALLY_EXTERN (d))
mark_definable (d);
input_location = DECL_SOURCE_LOCATION (d);
if (! pattern_defined && DECL_EXPLICIT_INSTANTIATION (d) && undefined_ok)
{
DECL_NOT_REALLY_EXTERN (d) = 0;
SET_DECL_IMPLICIT_INSTANTIATION (d);
}
if (!defer_ok)
{
tree gen = DECL_TEMPLATE_RESULT (gen_tmpl);
tree type = TREE_TYPE (gen);
push_access_scope (d);
if (TREE_CODE (gen) == FUNCTION_DECL)
{
tsubst (DECL_ARGUMENTS (gen), gen_args, tf_error | tf_warning, d);
tsubst (TYPE_RAISES_EXCEPTIONS (type), gen_args,
tf_error | tf_warning, d);
type = TREE_TYPE (type);
}
tsubst (type, gen_args, tf_error | tf_warning, d);
pop_access_scope (d);
}
gcc_assert (!(TREE_CODE (d) == VAR_DECL
&& DECL_INITIALIZED_IN_CLASS_P (d)
&& DECL_INITIAL (d) == NULL_TREE));
if (DECL_INTERFACE_KNOWN (d)
&& DECL_REALLY_EXTERN (d)
&& ! (TREE_CODE (d) == FUNCTION_DECL
&& DECL_INLINE (d)))
goto out;
else if (! pattern_defined || defer_ok)
{
input_location = saved_loc;
if (at_eof && !pattern_defined
&& DECL_EXPLICIT_INSTANTIATION (d))
pedwarn
("explicit instantiation of %qD but no definition available", d);
add_pending_template (d);
goto out;
}
if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
{
if (pch_file)
add_pending_template (d);
if (!(TREE_CODE (d) == FUNCTION_DECL
&& flag_inline_trees
&& DECL_DECLARED_INLINE_P (d)))
goto out;
}
need_push = !cfun || !global_bindings_p ();
if (need_push)
push_to_top_level ();
DECL_TEMPLATE_INSTANTIATED (d) = 1;
regenerate_decl_from_template (d, td);
input_location = DECL_SOURCE_LOCATION (d);
if (TREE_CODE (d) == VAR_DECL)
{
SET_DECL_RTL (d, NULL_RTX);
DECL_IN_AGGR_P (d) = 0;
DECL_EXTERNAL (d) = 0;
push_nested_class (DECL_CONTEXT (d));
cp_finish_decl (d,
(!DECL_INITIALIZED_IN_CLASS_P (d)
? DECL_INITIAL (d) : NULL_TREE),
NULL_TREE, 0);
pop_nested_class ();
}
else if (TREE_CODE (d) == FUNCTION_DECL)
{
htab_t saved_local_specializations;
tree subst_decl;
tree tmpl_parm;
tree spec_parm;
saved_local_specializations = local_specializations;
local_specializations = htab_create (37,
hash_local_specialization,
eq_local_specializations,
NULL);
start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
subst_decl = DECL_TEMPLATE_RESULT (template_for_substitution (d));
tmpl_parm = DECL_ARGUMENTS (subst_decl);
spec_parm = DECL_ARGUMENTS (d);
if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
{
register_local_specialization (spec_parm, tmpl_parm);
spec_parm = skip_artificial_parms_for (d, spec_parm);
tmpl_parm = skip_artificial_parms_for (subst_decl, tmpl_parm);
}
while (tmpl_parm)
{
register_local_specialization (spec_parm, tmpl_parm);
tmpl_parm = TREE_CHAIN (tmpl_parm);
spec_parm = TREE_CHAIN (spec_parm);
}
gcc_assert (!spec_parm);
tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
tf_error | tf_warning, tmpl);
htab_delete (local_specializations);
local_specializations = saved_local_specializations;
d = finish_function (0);
expand_or_defer_fn (d);
}
TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
if (need_push)
pop_from_top_level ();
out:
input_location = saved_loc;
pop_deferring_access_checks ();
pop_tinst_level ();
timevar_pop (TV_PARSE);
return d;
}
void
instantiate_pending_templates (int retries)
{
tree *t;
tree last = NULL_TREE;
int reconsider;
location_t saved_loc = input_location;
int saved_in_system_header = in_system_header;
if (pending_templates && retries >= max_tinst_depth)
{
cp_error_at ("template instantiation depth exceeds maximum of %d"
" (use -ftemplate-depth-NN to increase the maximum)"
" instantiating %q+D, possibly from virtual table"
" generation",
max_tinst_depth, TREE_VALUE (pending_templates));
return;
}
do
{
reconsider = 0;
t = &pending_templates;
while (*t)
{
tree instantiation = TREE_VALUE (*t);
reopen_tinst_level (TREE_PURPOSE (*t));
if (TYPE_P (instantiation))
{
tree fn;
if (!COMPLETE_TYPE_P (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, 0,
0);
if (COMPLETE_TYPE_P (instantiation))
reconsider = 1;
}
if (COMPLETE_TYPE_P (instantiation))
*t = TREE_CHAIN (*t);
else
{
last = *t;
t = &TREE_CHAIN (*t);
}
}
else
{
if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
&& !DECL_TEMPLATE_INSTANTIATED (instantiation))
{
instantiation = instantiate_decl (instantiation,
0,
0);
if (DECL_TEMPLATE_INSTANTIATED (instantiation))
reconsider = 1;
}
if (DECL_TEMPLATE_SPECIALIZATION (instantiation)
|| DECL_TEMPLATE_INSTANTIATED (instantiation))
*t = TREE_CHAIN (*t);
else
{
last = *t;
t = &TREE_CHAIN (*t);
}
}
tinst_depth = 0;
current_tinst_level = NULL_TREE;
}
last_pending_template = last;
}
while (reconsider);
input_location = saved_loc;
in_system_header = saved_in_system_header;
}
static tree
tsubst_initializer_list (tree t, tree argvec)
{
tree inits = NULL_TREE;
for (; t; t = TREE_CHAIN (t))
{
tree decl;
tree init;
decl = tsubst_copy (TREE_PURPOSE (t), argvec, tf_error | tf_warning,
NULL_TREE);
decl = expand_member_init (decl);
if (decl && !DECL_P (decl))
in_base_initializer = 1;
init = tsubst_expr (TREE_VALUE (t), argvec, tf_error | tf_warning,
NULL_TREE);
in_base_initializer = 0;
if (decl)
{
init = build_tree_list (decl, init);
TREE_CHAIN (init) = inits;
inits = init;
}
}
return inits;
}
static void
set_current_access_from_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 (tree tag, tree newtag, tree args)
{
tree e;
for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
{
tree value;
tree decl;
decl = TREE_VALUE (e);
value = tsubst_expr (DECL_INITIAL (decl),
args, tf_error | tf_warning,
NULL_TREE);
set_current_access_from_decl (decl);
build_enumerator (DECL_NAME (decl), value, newtag);
}
finish_enum (newtag);
DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
= DECL_SOURCE_LOCATION (TYPE_NAME (tag));
}
tree
get_mostly_instantiated_function_type (tree decl)
{
tree fn_type;
tree tmpl;
tree targs;
tree tparms;
int parm_depth;
tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
targs = DECL_TI_ARGS (decl);
tparms = DECL_TEMPLATE_PARMS (tmpl);
parm_depth = TMPL_PARMS_DEPTH (tparms);
gcc_assert (parm_depth == TMPL_ARGS_DEPTH (targs));
fn_type = TREE_TYPE (tmpl);
if (parm_depth == 1)
;
else
{
int i, save_access_control;
tree partial_args;
partial_args = make_tree_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_tree_vec (DECL_NTPARMS (tmpl)));
save_access_control = flag_access_control;
flag_access_control = 0;
++processing_template_decl;
fn_type = tsubst (fn_type, partial_args, tf_error, NULL_TREE);
--processing_template_decl;
TREE_VEC_LENGTH (partial_args)--;
tparms = tsubst_template_parms (tparms, partial_args, tf_error);
flag_access_control = save_access_control;
}
return fn_type;
}
int
problematic_instantiation_changed (void)
{
return last_template_error_tick != tinst_level_tick;
}
void
record_last_problematic_instantiation (void)
{
last_template_error_tick = tinst_level_tick;
}
tree
current_instantiation (void)
{
return current_tinst_level;
}
static int
invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
{
if (INTEGRAL_TYPE_P (type))
return 0;
else if (POINTER_TYPE_P (type))
return 0;
else if (TYPE_PTR_TO_MEMBER_P (type))
return 0;
else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
return 0;
else if (TREE_CODE (type) == TYPENAME_TYPE)
return 0;
if (complain & tf_error)
error ("%q#T is not a valid type for a template constant parameter", type);
return 1;
}
static bool
dependent_type_p_r (tree type)
{
tree scope;
if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
|| TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
return true;
if (TREE_CODE (type) == TYPENAME_TYPE)
return true;
type = TYPE_MAIN_VARIANT (type);
if (TYPE_PTR_TO_MEMBER_P (type))
return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
|| dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
(type)));
else if (TREE_CODE (type) == POINTER_TYPE
|| TREE_CODE (type) == REFERENCE_TYPE)
return dependent_type_p (TREE_TYPE (type));
else if (TREE_CODE (type) == FUNCTION_TYPE
|| TREE_CODE (type) == METHOD_TYPE)
{
tree arg_type;
if (dependent_type_p (TREE_TYPE (type)))
return true;
for (arg_type = TYPE_ARG_TYPES (type);
arg_type;
arg_type = TREE_CHAIN (arg_type))
if (dependent_type_p (TREE_VALUE (arg_type)))
return true;
return false;
}
if (TREE_CODE (type) == ARRAY_TYPE)
{
if (TYPE_DOMAIN (type)
&& ((value_dependent_expression_p
(TYPE_MAX_VALUE (TYPE_DOMAIN (type))))
|| (type_dependent_expression_p
(TYPE_MAX_VALUE (TYPE_DOMAIN (type))))))
return true;
return dependent_type_p (TREE_TYPE (type));
}
if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
return true;
else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
&& (any_dependent_template_arguments_p
(INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
return true;
if (TREE_CODE (type) == TYPEOF_TYPE)
return true;
scope = TYPE_CONTEXT (type);
if (scope && TYPE_P (scope))
return dependent_type_p (scope);
else if (scope && TREE_CODE (scope) == FUNCTION_DECL)
return type_dependent_expression_p (scope);
return false;
}
bool
dependent_type_p (tree type)
{
if (!processing_template_decl)
return false;
if (!type)
return true;
if (type == error_mark_node)
return false;
if (!TYPE_DEPENDENT_P_VALID (type))
{
TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
TYPE_DEPENDENT_P_VALID (type) = 1;
}
return TYPE_DEPENDENT_P (type);
}
static bool
dependent_scope_ref_p (tree expression, bool criterion (tree))
{
tree scope;
tree name;
gcc_assert (TREE_CODE (expression) == SCOPE_REF);
if (!TYPE_P (TREE_OPERAND (expression, 0)))
return true;
scope = TREE_OPERAND (expression, 0);
name = TREE_OPERAND (expression, 1);
if (DECL_P (name)
&& currently_open_class (scope)
&& !criterion (name))
return false;
if (dependent_type_p (scope))
return true;
return false;
}
bool
value_dependent_expression_p (tree expression)
{
if (!processing_template_decl)
return false;
if (TREE_CODE (expression) == IDENTIFIER_NODE
|| (DECL_P (expression)
&& type_dependent_expression_p (expression)))
return true;
if ((TREE_CODE (expression) == CONST_DECL
&& DECL_TEMPLATE_PARM_P (expression))
|| TREE_CODE (expression) == TEMPLATE_PARM_INDEX)
return true;
if (TREE_CODE (expression) == VAR_DECL
&& DECL_INITIAL (expression)
&& INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (expression))
&& value_dependent_expression_p (DECL_INITIAL (expression)))
return true;
if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
|| TREE_CODE (expression) == STATIC_CAST_EXPR
|| TREE_CODE (expression) == CONST_CAST_EXPR
|| TREE_CODE (expression) == REINTERPRET_CAST_EXPR
|| TREE_CODE (expression) == CAST_EXPR)
{
tree type = TREE_TYPE (expression);
if (dependent_type_p (type))
return true;
expression = TREE_OPERAND (expression, 0);
if (!expression)
{
gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
return false;
}
if (TREE_CODE (expression) == TREE_LIST)
{
do
{
if (value_dependent_expression_p (TREE_VALUE (expression)))
return true;
expression = TREE_CHAIN (expression);
}
while (expression);
return false;
}
else
return value_dependent_expression_p (expression);
}
if (TREE_CODE (expression) == SIZEOF_EXPR
|| TREE_CODE (expression) == ALIGNOF_EXPR)
{
expression = TREE_OPERAND (expression, 0);
if (TYPE_P (expression))
return dependent_type_p (expression);
return type_dependent_expression_p (expression);
}
if (TREE_CODE (expression) == SCOPE_REF)
return dependent_scope_ref_p (expression, value_dependent_expression_p);
if (TREE_CODE (expression) == COMPONENT_REF)
return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
|| value_dependent_expression_p (TREE_OPERAND (expression, 1)));
if (TREE_CODE (expression) == CALL_EXPR)
{
tree function = TREE_OPERAND (expression, 0);
tree args = TREE_OPERAND (expression, 1);
if (value_dependent_expression_p (function))
return true;
else if (! args)
return false;
else if (TREE_CODE (args) == TREE_LIST)
{
do
{
if (value_dependent_expression_p (TREE_VALUE (args)))
return true;
args = TREE_CHAIN (args);
}
while (args);
return false;
}
else
return value_dependent_expression_p (args);
}
if (EXPR_P (expression))
{
switch (TREE_CODE_CLASS (TREE_CODE (expression)))
{
case tcc_reference:
case tcc_unary:
return (value_dependent_expression_p
(TREE_OPERAND (expression, 0)));
case tcc_comparison:
case tcc_binary:
return ((value_dependent_expression_p
(TREE_OPERAND (expression, 0)))
|| (value_dependent_expression_p
(TREE_OPERAND (expression, 1))));
case tcc_expression:
{
int i;
for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (expression)); ++i)
if (TREE_OPERAND (expression, i)
&& (value_dependent_expression_p
(TREE_OPERAND (expression, i))))
return true;
return false;
}
case tcc_statement:
return false;
default:
gcc_unreachable ();
}
}
return false;
}
bool
type_dependent_expression_p (tree expression)
{
if (!processing_template_decl)
return false;
if (expression == error_mark_node)
return false;
if (TREE_CODE (expression) == IDENTIFIER_NODE)
return true;
if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
|| TREE_CODE (expression) == SIZEOF_EXPR
|| TREE_CODE (expression) == ALIGNOF_EXPR
|| TREE_CODE (expression) == TYPEID_EXPR
|| TREE_CODE (expression) == DELETE_EXPR
|| TREE_CODE (expression) == VEC_DELETE_EXPR
|| TREE_CODE (expression) == THROW_EXPR)
return false;
if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
|| TREE_CODE (expression) == STATIC_CAST_EXPR
|| TREE_CODE (expression) == CONST_CAST_EXPR
|| TREE_CODE (expression) == REINTERPRET_CAST_EXPR
|| TREE_CODE (expression) == CAST_EXPR)
return dependent_type_p (TREE_TYPE (expression));
if (TREE_CODE (expression) == NEW_EXPR
|| TREE_CODE (expression) == VEC_NEW_EXPR)
{
tree type = TREE_OPERAND (expression, 1);
if (TREE_CODE (type) == TREE_LIST)
return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
|| value_dependent_expression_p
(TREE_OPERAND (TREE_VALUE (type), 1));
else
return dependent_type_p (type);
}
if (TREE_CODE (expression) == SCOPE_REF
&& dependent_scope_ref_p (expression,
type_dependent_expression_p))
return true;
if (TREE_CODE (expression) == FUNCTION_DECL
&& DECL_LANG_SPECIFIC (expression)
&& DECL_TEMPLATE_INFO (expression)
&& (any_dependent_template_arguments_p
(INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
return true;
if (TREE_CODE (expression) == TEMPLATE_DECL
&& !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
return false;
if (TREE_TYPE (expression) == unknown_type_node)
{
if (TREE_CODE (expression) == ADDR_EXPR)
return type_dependent_expression_p (TREE_OPERAND (expression, 0));
if (TREE_CODE (expression) == COMPONENT_REF
|| TREE_CODE (expression) == OFFSET_REF)
{
if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
return true;
expression = TREE_OPERAND (expression, 1);
if (TREE_CODE (expression) == IDENTIFIER_NODE)
return false;
}
if (TREE_CODE (expression) == SCOPE_REF)
return false;
if (TREE_CODE (expression) == BASELINK)
expression = BASELINK_FUNCTIONS (expression);
if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
{
if (any_dependent_template_arguments_p
(TREE_OPERAND (expression, 1)))
return true;
expression = TREE_OPERAND (expression, 0);
}
gcc_assert (TREE_CODE (expression) == OVERLOAD);
while (expression)
{
if (type_dependent_expression_p (OVL_CURRENT (expression)))
return true;
expression = OVL_NEXT (expression);
}
return false;
}
return (dependent_type_p (TREE_TYPE (expression)));
}
bool
any_type_dependent_arguments_p (tree args)
{
while (args)
{
tree arg = TREE_VALUE (args);
if (type_dependent_expression_p (arg))
return true;
args = TREE_CHAIN (args);
}
return false;
}
static bool
dependent_template_arg_p (tree arg)
{
if (!processing_template_decl)
return false;
if (TREE_CODE (arg) == TEMPLATE_DECL
|| TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
return dependent_template_p (arg);
else if (TYPE_P (arg))
return dependent_type_p (arg);
else
return (type_dependent_expression_p (arg)
|| value_dependent_expression_p (arg));
}
bool
any_dependent_template_arguments_p (tree args)
{
int i;
int j;
if (!args)
return false;
for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
{
tree level = TMPL_ARGS_LEVEL (args, i + 1);
for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
return true;
}
return false;
}
bool
dependent_template_p (tree tmpl)
{
if (TREE_CODE (tmpl) == OVERLOAD)
{
while (tmpl)
{
if (dependent_template_p (OVL_FUNCTION (tmpl)))
return true;
tmpl = OVL_CHAIN (tmpl);
}
return false;
}
if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
|| TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
return true;
if (TREE_CODE (tmpl) == SCOPE_REF
|| TREE_CODE (tmpl) == IDENTIFIER_NODE)
return true;
if (TYPE_P (CP_DECL_CONTEXT (tmpl)))
return dependent_type_p (DECL_CONTEXT (tmpl));
return false;
}
bool
dependent_template_id_p (tree tmpl, tree args)
{
return (dependent_template_p (tmpl)
|| any_dependent_template_arguments_p (args));
}
tree
resolve_typename_type (tree type, bool only_current_p)
{
tree scope;
tree name;
tree decl;
int quals;
tree pushed_scope;
gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
scope = TYPE_CONTEXT (type);
name = TYPE_IDENTIFIER (type);
if (TREE_CODE (scope) == TYPENAME_TYPE)
scope = resolve_typename_type (scope, only_current_p);
if (scope == error_mark_node || TREE_CODE (scope) == TYPENAME_TYPE)
return error_mark_node;
if (TREE_CODE (scope) == TEMPLATE_TYPE_PARM)
return type;
if (only_current_p && !currently_open_class (scope))
return error_mark_node;
scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
pushed_scope = push_scope (scope);
decl = lookup_member (scope, name, 0, true);
quals = cp_type_quals (type);
if (!decl)
type = error_mark_node;
else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == IDENTIFIER_NODE
&& TREE_CODE (decl) == TYPE_DECL)
type = TREE_TYPE (decl);
else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
&& DECL_CLASS_TEMPLATE_P (decl))
{
tree tmpl;
tree args;
tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
type = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
0, tf_error | tf_user);
}
else
type = error_mark_node;
if (type != error_mark_node && quals)
type = cp_build_qualified_type (type, quals);
if (pushed_scope)
pop_scope (pushed_scope);
return type;
}
tree
build_non_dependent_expr (tree expr)
{
tree inner_expr;
if (null_ptr_cst_p (expr))
return expr;
inner_expr = (TREE_CODE (expr) == ADDR_EXPR ?
TREE_OPERAND (expr, 0) : expr);
if (is_overloaded_fn (inner_expr)
|| TREE_CODE (inner_expr) == OFFSET_REF)
return expr;
if (TREE_CODE (expr) == VAR_DECL)
return expr;
if (TREE_CODE (expr) == STRING_CST)
return expr;
if (TREE_CODE (expr) == INTEGER_CST || TREE_CODE (expr) == REAL_CST)
return expr;
if (TREE_CODE (expr) == THROW_EXPR)
return expr;
if (TREE_CODE (expr) == COND_EXPR)
return build3 (COND_EXPR,
TREE_TYPE (expr),
TREE_OPERAND (expr, 0),
(TREE_OPERAND (expr, 1)
? build_non_dependent_expr (TREE_OPERAND (expr, 1))
: build_non_dependent_expr (TREE_OPERAND (expr, 0))),
build_non_dependent_expr (TREE_OPERAND (expr, 2)));
if (TREE_CODE (expr) == COMPOUND_EXPR
&& !COMPOUND_EXPR_OVERLOADED (expr))
return build2 (COMPOUND_EXPR,
TREE_TYPE (expr),
TREE_OPERAND (expr, 0),
build_non_dependent_expr (TREE_OPERAND (expr, 1)));
return build1 (NON_DEPENDENT_EXPR, non_reference (TREE_TYPE (expr)), expr);
}
tree
build_non_dependent_args (tree args)
{
tree a;
tree new_args;
new_args = NULL_TREE;
for (a = args; a; a = TREE_CHAIN (a))
new_args = tree_cons (NULL_TREE,
build_non_dependent_expr (TREE_VALUE (a)),
new_args);
return nreverse (new_args);
}
#include "gt-cp-pt.h"