#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "flags.h"
#include "tree.h"
#include "cp-tree.h"
#include "name-lookup.h"
#include "timevar.h"
#include "toplev.h"
#include "diagnostic.h"
#include "debug.h"
struct scope_binding {
tree value;
tree type;
};
#define EMPTY_SCOPE_BINDING { NULL_TREE, NULL_TREE }
static cxx_scope *innermost_nonclass_level (void);
static tree select_decl (const struct scope_binding *, int);
static cxx_binding *binding_for_name (cxx_scope *, tree);
static tree lookup_name_innermost_nonclass_level (tree);
static tree push_overloaded_decl (tree, int);
static bool lookup_using_namespace (tree, struct scope_binding *, tree,
tree, int);
static bool qualified_lookup_using_namespace (tree, tree,
struct scope_binding *, int);
static tree lookup_type_current_level (tree);
static tree push_using_directive (tree);
static void cp_emit_debug_info_for_using (tree, tree);
tree global_namespace;
static GTY(()) tree anonymous_namespace_name;
#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
static GTY((deletable)) binding_entry free_binding_entry = NULL;
static inline binding_entry
binding_entry_make (tree name, tree type)
{
binding_entry entry;
if (free_binding_entry)
{
entry = free_binding_entry;
free_binding_entry = entry->chain;
}
else
entry = GGC_NEW (struct binding_entry_s);
entry->name = name;
entry->type = type;
entry->chain = NULL;
return entry;
}
#if 0
static inline void
binding_entry_free (binding_entry entry)
{
entry->name = NULL;
entry->type = NULL;
entry->chain = free_binding_entry;
free_binding_entry = entry;
}
#endif
struct binding_table_s GTY(())
{
binding_entry * GTY((length ("%h.chain_count"))) chain;
size_t chain_count;
size_t entry_count;
};
static inline void
binding_table_construct (binding_table table, size_t chain_count)
{
table->chain_count = chain_count;
table->entry_count = 0;
table->chain = GGC_CNEWVEC (binding_entry, table->chain_count);
}
#if 0
static void
binding_table_free (binding_table table)
{
size_t i;
size_t count;
if (table == NULL)
return;
for (i = 0, count = table->chain_count; i < count; ++i)
{
binding_entry temp = table->chain[i];
while (temp != NULL)
{
binding_entry entry = temp;
temp = entry->chain;
binding_entry_free (entry);
}
table->chain[i] = NULL;
}
table->entry_count = 0;
}
#endif
static inline binding_table
binding_table_new (size_t chain_count)
{
binding_table table = GGC_NEW (struct binding_table_s);
table->chain = NULL;
binding_table_construct (table, chain_count);
return table;
}
static void
binding_table_expand (binding_table table)
{
const size_t old_chain_count = table->chain_count;
const size_t old_entry_count = table->entry_count;
const size_t new_chain_count = 2 * old_chain_count;
binding_entry *old_chains = table->chain;
size_t i;
binding_table_construct (table, new_chain_count);
for (i = 0; i < old_chain_count; ++i)
{
binding_entry entry = old_chains[i];
for (; entry != NULL; entry = old_chains[i])
{
const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
const size_t j = ENTRY_INDEX (hash, new_chain_count);
old_chains[i] = entry->chain;
entry->chain = table->chain[j];
table->chain[j] = entry;
}
}
table->entry_count = old_entry_count;
}
static void
binding_table_insert (binding_table table, tree name, tree type)
{
const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
const size_t i = ENTRY_INDEX (hash, table->chain_count);
binding_entry entry = binding_entry_make (name, type);
entry->chain = table->chain[i];
table->chain[i] = entry;
++table->entry_count;
if (3 * table->chain_count < 5 * table->entry_count)
binding_table_expand (table);
}
binding_entry
binding_table_find (binding_table table, tree name)
{
const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
while (entry != NULL && entry->name != name)
entry = entry->chain;
return entry;
}
void
binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
{
const size_t chain_count = table->chain_count;
size_t i;
for (i = 0; i < chain_count; ++i)
{
binding_entry entry = table->chain[i];
for (; entry != NULL; entry = entry->chain)
proc (entry, data);
}
}
#ifndef ENABLE_SCOPE_CHECKING
# define ENABLE_SCOPE_CHECKING 0
#else
# define ENABLE_SCOPE_CHECKING 1
#endif
static GTY((deletable)) cxx_binding *free_bindings;
static inline void
cxx_binding_init (cxx_binding *binding, tree value, tree type)
{
binding->value = value;
binding->type = type;
binding->previous = NULL;
}
static cxx_binding *
cxx_binding_make (tree value, tree type)
{
cxx_binding *binding;
if (free_bindings)
{
binding = free_bindings;
free_bindings = binding->previous;
}
else
binding = GGC_NEW (cxx_binding);
cxx_binding_init (binding, value, type);
return binding;
}
static inline void
cxx_binding_free (cxx_binding *binding)
{
binding->scope = NULL;
binding->previous = free_bindings;
free_bindings = binding;
}
static cxx_binding *
new_class_binding (tree name, tree value, tree type, cxx_scope *scope)
{
cp_class_binding *cb;
cxx_binding *binding;
if (VEC_length (cp_class_binding, scope->class_shadowed))
{
cp_class_binding *old_base;
old_base = VEC_index (cp_class_binding, scope->class_shadowed, 0);
if (VEC_reserve (cp_class_binding, scope->class_shadowed, -1))
{
size_t i;
for (i = 0;
VEC_iterate (cp_class_binding, scope->class_shadowed, i, cb);
i++)
{
cxx_binding **b;
b = &IDENTIFIER_BINDING (cb->identifier);
while (*b != &old_base[i].base)
b = &((*b)->previous);
*b = &cb->base;
}
}
cb = VEC_quick_push (cp_class_binding, scope->class_shadowed, NULL);
}
else
cb = VEC_safe_push (cp_class_binding, scope->class_shadowed, NULL);
cb->identifier = name;
binding = &cb->base;
binding->scope = scope;
cxx_binding_init (binding, value, type);
return binding;
}
static void
push_binding (tree id, tree decl, cxx_scope* level)
{
cxx_binding *binding;
if (level != class_binding_level)
{
binding = cxx_binding_make (decl, NULL_TREE);
binding->scope = level;
}
else
binding = new_class_binding (id, decl, NULL_TREE, level);
binding->previous = IDENTIFIER_BINDING (id);
INHERITED_VALUE_BINDING_P (binding) = 0;
LOCAL_BINDING_P (binding) = (level != class_binding_level);
IDENTIFIER_BINDING (id) = binding;
}
void
pop_binding (tree id, tree decl)
{
cxx_binding *binding;
if (id == NULL_TREE)
return;
binding = IDENTIFIER_BINDING (id);
gcc_assert (binding != NULL);
if (binding->value == decl)
binding->value = NULL_TREE;
else
{
gcc_assert (binding->type == decl);
binding->type = NULL_TREE;
}
if (!binding->value && !binding->type)
{
IDENTIFIER_BINDING (id) = binding->previous;
cxx_binding_free (binding);
}
}
static bool
supplement_binding (cxx_binding *binding, tree decl)
{
tree bval = binding->value;
bool ok = true;
timevar_push (TV_NAME_LOOKUP);
if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
binding->type = decl;
else if (
!bval
|| bval == error_mark_node
|| (TREE_CODE (bval) == FUNCTION_DECL
&& DECL_ANTICIPATED (bval)))
binding->value = decl;
else if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval))
{
binding->type = bval;
binding->value = decl;
binding->value_is_inherited = false;
}
else if (TREE_CODE (bval) == TYPE_DECL
&& TREE_CODE (decl) == TYPE_DECL
&& DECL_NAME (decl) == DECL_NAME (bval)
&& binding->scope->kind != sk_class
&& (same_type_p (TREE_TYPE (decl), TREE_TYPE (bval))
|| uses_template_parms (TREE_TYPE (decl))
|| uses_template_parms (TREE_TYPE (bval))))
ok = false;
else if (TREE_CODE (decl) == VAR_DECL && TREE_CODE (bval) == VAR_DECL
&& DECL_EXTERNAL (decl) && DECL_EXTERNAL (bval)
&& !DECL_CLASS_SCOPE_P (decl))
{
duplicate_decls (decl, binding->value);
ok = false;
}
else if (TREE_CODE (decl) == NAMESPACE_DECL
&& TREE_CODE (bval) == NAMESPACE_DECL
&& DECL_NAMESPACE_ALIAS (decl)
&& DECL_NAMESPACE_ALIAS (bval)
&& ORIGINAL_NAMESPACE (bval) == ORIGINAL_NAMESPACE (decl))
ok = false;
else
{
error ("declaration of %q#D", decl);
cp_error_at ("conflicts with previous declaration %q#D", bval);
ok = false;
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
}
static void
add_decl_to_level (tree decl, cxx_scope *b)
{
if (TREE_CODE (decl) == NAMESPACE_DECL
&& !DECL_NAMESPACE_ALIAS (decl))
{
TREE_CHAIN (decl) = b->namespaces;
b->namespaces = decl;
}
else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
{
TREE_CHAIN (decl) = b->vtables;
b->vtables = decl;
}
else
{
TREE_CHAIN (decl) = b->names;
b->names = decl;
b->names_size++;
if (b->kind == sk_namespace)
if ((TREE_CODE (decl) == VAR_DECL
&& (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
|| (TREE_CODE (decl) == FUNCTION_DECL
&& (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
VARRAY_PUSH_TREE (b->static_decls, decl);
}
}
tree
pushdecl (tree x)
{
tree t;
tree name;
int need_new_binding;
timevar_push (TV_NAME_LOOKUP);
need_new_binding = 1;
if (DECL_TEMPLATE_PARM_P (x))
;
else
{
if (current_function_decl && x != current_function_decl
&& TREE_CODE (x) != FUNCTION_DECL
&& !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
&& !DECL_CONTEXT (x))
DECL_CONTEXT (x) = current_function_decl;
if (TREE_CODE (x) == FUNCTION_DECL
&& DECL_NAMESPACE_SCOPE_P (x)
&& current_function_decl
&& x != current_function_decl)
DECL_LOCAL_FUNCTION_P (x) = 1;
}
name = DECL_NAME (x);
if (name)
{
int different_binding_level = 0;
if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
name = TREE_OPERAND (name, 0);
if (DECL_NAMESPACE_SCOPE_P (x) && namespace_bindings_p ())
t = namespace_binding (name, DECL_CONTEXT (x));
else
t = lookup_name_innermost_nonclass_level (name);
if (! t && current_function_decl && x != current_function_decl
&& (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
&& DECL_EXTERNAL (x))
{
t = innermost_non_namespace_value (name);
if (! t)
t = namespace_binding (name, DECL_CONTEXT (x));
if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
t = NULL_TREE;
if (t)
different_binding_level = 1;
}
if (t && TREE_CODE (t) == OVERLOAD)
{
tree match;
if (TREE_CODE (x) == FUNCTION_DECL)
for (match = t; match; match = OVL_NEXT (match))
{
if (decls_match (OVL_CURRENT (match), x))
break;
}
else
match = t;
if (match)
t = OVL_CURRENT (match);
else
t = NULL_TREE;
}
if (t && t != error_mark_node)
{
if (different_binding_level)
{
if (decls_match (x, t))
{
TREE_PUBLIC (x) = TREE_PUBLIC (t);
DECL_UID (x) = DECL_UID (t);
}
}
else if (TREE_CODE (t) == PARM_DECL)
{
gcc_assert (DECL_CONTEXT (t));
if (duplicate_decls (x, t))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
else if ((DECL_EXTERN_C_FUNCTION_P (x)
|| DECL_FUNCTION_TEMPLATE_P (x))
&& is_overloaded_fn (t))
;
else if (t == wchar_decl_node)
{
if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
pedwarn ("redeclaration of %<wchar_t%> as %qT",
TREE_TYPE (x));
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
else
{
tree olddecl = duplicate_decls (x, t);
if (olddecl == error_mark_node)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
if (olddecl)
{
if (TREE_CODE (t) == TYPE_DECL)
SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
else if (TREE_CODE (t) == FUNCTION_DECL)
check_default_args (t);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
else if (DECL_MAIN_P (x) && TREE_CODE (t) == FUNCTION_DECL)
{
cp_error_at ("invalid redeclaration of %qD", t);
error ("as %qD", x);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
}
}
}
check_template_shadow (x);
if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
{
retrofit_lang_decl (x);
SET_DECL_LANGUAGE (x, lang_c);
}
if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
{
t = push_overloaded_decl (x, PUSH_LOCAL);
if (t != x)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
if (!namespace_bindings_p ())
need_new_binding = 0;
}
else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
{
t = push_overloaded_decl (x, PUSH_GLOBAL);
if (t == x)
add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
if (TREE_CODE (x) == TYPE_DECL)
{
tree type = TREE_TYPE (x);
if (DECL_IS_BUILTIN (x))
{
if (TYPE_NAME (type) == 0)
TYPE_NAME (type) = x;
}
else if (type != error_mark_node && TYPE_NAME (type) != x
&& (!TYPE_NAME (type)
|| TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
{
DECL_ORIGINAL_TYPE (x) = type;
type = build_variant_type_copy (type);
TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
TYPE_NAME (type) = x;
TREE_TYPE (x) = type;
}
if (type != error_mark_node
&& TYPE_NAME (type)
&& TYPE_IDENTIFIER (type))
set_identifier_type_value (DECL_NAME (x), x);
}
if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
{
tree decl;
decl = IDENTIFIER_NAMESPACE_VALUE (name);
if (decl && TREE_CODE (decl) == OVERLOAD)
decl = OVL_FUNCTION (decl);
if (decl && decl != error_mark_node
&& (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
&& TREE_CODE (decl) == TREE_CODE (x)
&& !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
{
pedwarn ("type mismatch with previous external decl of %q#D", x);
cp_pedwarn_at ("previous external decl of %q#D", decl);
}
}
if (namespace_bindings_p ())
{
if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
TREE_PUBLIC (name) = 1;
if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
&& t != NULL_TREE)
&& (TREE_CODE (x) == TYPE_DECL
|| TREE_CODE (x) == VAR_DECL
|| TREE_CODE (x) == ALIAS_DECL
|| TREE_CODE (x) == NAMESPACE_DECL
|| TREE_CODE (x) == CONST_DECL
|| TREE_CODE (x) == TEMPLATE_DECL))
SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
warn_extern_redeclared_static (x, t);
}
else
{
tree oldlocal = innermost_non_namespace_value (name);
tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
if (need_new_binding)
{
push_local_binding (name, x, 0);
need_new_binding = 0;
}
if (TREE_CODE (x) == TYPE_DECL)
set_identifier_type_value (name, x);
if (TREE_CODE (x) == NAMESPACE_DECL)
set_identifier_type_value (name, NULL_TREE);
if (oldlocal)
{
tree d = oldlocal;
while (oldlocal
&& TREE_CODE (oldlocal) == VAR_DECL
&& DECL_DEAD_FOR_LOCAL (oldlocal))
oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
if (oldlocal == NULL_TREE)
oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
}
if (oldlocal == NULL_TREE
&& DECL_EXTERNAL (x)
&& oldglobal != NULL_TREE
&& TREE_CODE (x) == FUNCTION_DECL
&& TREE_CODE (oldglobal) == FUNCTION_DECL)
{
if (decls_match (x, oldglobal))
;
else
{
warning ("extern declaration of %q#D doesn't match", x);
cp_warning_at ("global declaration %q#D", oldglobal);
}
}
if (oldlocal == NULL_TREE
&& oldglobal == NULL_TREE
&& DECL_EXTERNAL (x)
&& TREE_PUBLIC (x))
TREE_PUBLIC (name) = 1;
if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
&& !DECL_FROM_INLINE (x)
&& TREE_CODE (oldlocal) == PARM_DECL
&& !DECL_ARTIFICIAL (oldlocal))
{
bool err = false;
if (DECL_CONTEXT (oldlocal) == current_function_decl
&& TREE_CODE (x) != PARM_DECL)
{
struct cp_binding_level *b = current_binding_level->level_chain;
b = b->level_chain;
if (b->kind == sk_function_parms)
{
error ("declaration of %q#D shadows a parameter", x);
err = true;
}
}
if (warn_shadow && !err)
{
warning ("declaration of %q#D shadows a parameter", x);
warning ("%Jshadowed declaration is here", oldlocal);
}
}
else if (warn_shadow && !DECL_EXTERNAL (x)
&& ! DECL_ARTIFICIAL (x)
&& ! DECL_FROM_INLINE (x))
{
tree member;
if (current_class_ptr)
member = lookup_member (current_class_type,
name,
0,
false);
else
member = NULL_TREE;
if (member && !TREE_STATIC (member))
{
warning ("declaration of %qD shadows a member of 'this'",
x);
}
else if (oldlocal != NULL_TREE
&& TREE_CODE (oldlocal) == VAR_DECL)
{
warning ("declaration of %qD shadows a previous local", x);
warning ("%Jshadowed declaration is here", oldlocal);
}
else if (oldglobal != NULL_TREE
&& TREE_CODE (oldglobal) == VAR_DECL)
{
warning ("declaration of %qD shadows a global declaration",
x);
warning ("%Jshadowed declaration is here", oldglobal);
}
}
}
if (TREE_CODE (x) == FUNCTION_DECL)
check_default_args (x);
if (TREE_CODE (x) == VAR_DECL)
maybe_register_incomplete_var (x);
}
if (need_new_binding)
add_decl_to_level (x,
DECL_NAMESPACE_SCOPE_P (x)
? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
: current_binding_level);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
}
tree
maybe_push_decl (tree decl)
{
tree type = TREE_TYPE (decl);
if (decl == error_mark_node
|| (TREE_CODE (decl) != PARM_DECL
&& DECL_CONTEXT (decl) != NULL_TREE
&& TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
|| (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
|| TREE_CODE (type) == UNKNOWN_TYPE
|| (TREE_CODE (decl) == FUNCTION_DECL
&& DECL_TEMPLATE_SPECIALIZATION (decl)))
return decl;
else
return pushdecl (decl);
}
void
push_local_binding (tree id, tree decl, int flags)
{
struct cp_binding_level *b;
b = innermost_nonclass_level ();
if (lookup_name_innermost_nonclass_level (id))
{
if (!supplement_binding (IDENTIFIER_BINDING (id), decl))
return;
}
else
push_binding (id, decl, b);
if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
decl = build_tree_list (NULL_TREE, decl);
add_decl_to_level (decl, b);
}
tree
check_for_out_of_scope_variable (tree decl)
{
tree shadowed;
if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
return decl;
shadowed = DECL_SHADOWED_FOR_VAR (decl);
while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
&& DECL_DEAD_FOR_LOCAL (shadowed))
shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
if (!shadowed)
shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
if (shadowed)
{
if (!DECL_ERROR_REPORTED (decl))
{
warning ("name lookup of %qD changed", DECL_NAME (decl));
cp_warning_at (" matches this %qD under ISO standard rules",
shadowed);
cp_warning_at (" matches this %qD under old rules", decl);
DECL_ERROR_REPORTED (decl) = 1;
}
return shadowed;
}
if (DECL_ERROR_REPORTED (decl))
return decl;
DECL_ERROR_REPORTED (decl) = 1;
if (TREE_TYPE (decl) == error_mark_node)
return decl;
if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
{
error ("name lookup of %qD changed for new ISO %<for%> scoping",
DECL_NAME (decl));
cp_error_at (" cannot use obsolete binding at %qD because "
"it has a destructor", decl);
return error_mark_node;
}
else
{
pedwarn ("name lookup of %qD changed for new ISO %<for%> scoping",
DECL_NAME (decl));
cp_pedwarn_at (" using obsolete binding at %qD", decl);
}
return decl;
}
static bool keep_next_level_flag;
static int binding_depth = 0;
static int is_class_level = 0;
static void
indent (int depth)
{
int i;
for (i = 0; i < depth * 2; i++)
putc (' ', stderr);
}
static const char *
cxx_scope_descriptor (cxx_scope *scope)
{
static const char* scope_kind_names[] = {
"block-scope",
"cleanup-scope",
"try-scope",
"catch-scope",
"for-scope",
"function-parameter-scope",
"class-scope",
"namespace-scope",
"template-parameter-scope",
"template-explicit-spec-scope"
};
const scope_kind kind = scope->explicit_spec_p
? sk_template_spec : scope->kind;
return scope_kind_names[kind];
}
static void
cxx_scope_debug (cxx_scope *scope, int line, const char *action)
{
const char *desc = cxx_scope_descriptor (scope);
if (scope->this_entity)
verbatim ("%s %s(%E) %p %d\n", action, desc,
scope->this_entity, (void *) scope, line);
else
verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
}
static inline size_t
namespace_scope_ht_size (tree ns)
{
tree name = DECL_NAME (ns);
return name == std_identifier
? NAMESPACE_STD_HT_SIZE
: (name == global_scope_name
? GLOBAL_SCOPE_HT_SIZE
: NAMESPACE_ORDINARY_HT_SIZE);
}
static GTY((deletable)) struct cp_binding_level *free_binding_level;
void
push_binding_level (struct cp_binding_level *scope)
{
scope->level_chain = current_binding_level;
current_binding_level = scope;
keep_next_level_flag = false;
if (ENABLE_SCOPE_CHECKING)
{
scope->binding_depth = binding_depth;
indent (binding_depth);
cxx_scope_debug (scope, input_line, "push");
is_class_level = 0;
binding_depth++;
}
}
cxx_scope *
begin_scope (scope_kind kind, tree entity)
{
cxx_scope *scope;
if (!ENABLE_SCOPE_CHECKING && free_binding_level)
{
scope = free_binding_level;
free_binding_level = scope->level_chain;
}
else
scope = GGC_NEW (cxx_scope);
memset (scope, 0, sizeof (cxx_scope));
scope->this_entity = entity;
scope->more_cleanups_ok = true;
switch (kind)
{
case sk_cleanup:
scope->keep = true;
break;
case sk_template_spec:
scope->explicit_spec_p = true;
kind = sk_template_parms;
case sk_template_parms:
case sk_block:
case sk_try:
case sk_catch:
case sk_for:
case sk_class:
case sk_function_parms:
scope->keep = keep_next_level_flag;
break;
case sk_namespace:
NAMESPACE_LEVEL (entity) = scope;
VARRAY_TREE_INIT (scope->static_decls,
DECL_NAME (entity) == std_identifier
|| DECL_NAME (entity) == global_scope_name
? 200 : 10,
"Static declarations");
break;
default:
gcc_unreachable ();
break;
}
scope->kind = kind;
push_binding_level (scope);
return scope;
}
cxx_scope *
leave_scope (void)
{
cxx_scope *scope = current_binding_level;
if (scope->kind == sk_namespace && class_binding_level)
current_binding_level = class_binding_level;
if (NAMESPACE_LEVEL (global_namespace))
gcc_assert (!global_scope_p (scope));
if (ENABLE_SCOPE_CHECKING)
{
indent (--binding_depth);
cxx_scope_debug (scope, input_line, "leave");
if (is_class_level != (scope == class_binding_level))
{
indent (binding_depth);
verbatim ("XXX is_class_level != (current_scope == class_scope)\n");
}
is_class_level = 0;
}
current_binding_level = scope->level_chain;
if (scope->kind != sk_namespace
&& scope->kind != sk_class)
{
scope->level_chain = free_binding_level;
gcc_assert (!ENABLE_SCOPE_CHECKING
|| scope->binding_depth == binding_depth);
free_binding_level = scope;
}
if (scope->kind == sk_class)
{
class_binding_level = NULL;
for (scope = current_binding_level; scope; scope = scope->level_chain)
if (scope->kind == sk_class)
{
class_binding_level = scope;
break;
}
}
return current_binding_level;
}
static void
resume_scope (struct cp_binding_level* b)
{
gcc_assert (!class_binding_level);
gcc_assert (b->level_chain == current_binding_level);
current_binding_level = b;
if (ENABLE_SCOPE_CHECKING)
{
b->binding_depth = binding_depth;
indent (binding_depth);
cxx_scope_debug (b, input_line, "resume");
is_class_level = 0;
binding_depth++;
}
}
static cxx_scope *
innermost_nonclass_level (void)
{
cxx_scope *b;
b = current_binding_level;
while (b->kind == sk_class)
b = b->level_chain;
return b;
}
void
maybe_push_cleanup_level (tree type)
{
if (type != error_mark_node
&& TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
&& current_binding_level->more_cleanups_ok == 0)
{
begin_scope (sk_cleanup, NULL);
current_binding_level->statement_list = push_stmt_list ();
}
}
int
global_bindings_p (void)
{
return global_scope_p (current_binding_level);
}
bool
toplevel_bindings_p (void)
{
struct cp_binding_level *b = innermost_nonclass_level ();
return b->kind == sk_namespace || b->kind == sk_template_parms;
}
bool
namespace_bindings_p (void)
{
struct cp_binding_level *b = innermost_nonclass_level ();
return b->kind == sk_namespace;
}
bool
kept_level_p (void)
{
return (current_binding_level->blocks != NULL_TREE
|| current_binding_level->keep
|| current_binding_level->kind == sk_cleanup
|| current_binding_level->names != NULL_TREE);
}
scope_kind
innermost_scope_kind (void)
{
return current_binding_level->kind;
}
bool
template_parm_scope_p (void)
{
return innermost_scope_kind () == sk_template_parms;
}
void
keep_next_level (bool keep)
{
keep_next_level_flag = keep;
}
tree
getdecls (void)
{
return current_binding_level->names;
}
static int no_print_functions = 0;
static int no_print_builtins = 0;
static void
print_binding_level (struct cp_binding_level* lvl)
{
tree t;
int i = 0, len;
fprintf (stderr, " blocks=" HOST_PTR_PRINTF, (void *) lvl->blocks);
if (lvl->more_cleanups_ok)
fprintf (stderr, " more-cleanups-ok");
if (lvl->have_cleanups)
fprintf (stderr, " have-cleanups");
fprintf (stderr, "\n");
if (lvl->names)
{
fprintf (stderr, " names:\t");
for (t = lvl->names; t; t = TREE_CHAIN (t))
{
if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
continue;
if (no_print_builtins
&& (TREE_CODE (t) == TYPE_DECL)
&& DECL_IS_BUILTIN (t))
continue;
if (TREE_CODE (t) == FUNCTION_DECL)
len = 3;
else
len = 2;
i += len;
if (i > 6)
{
fprintf (stderr, "\n\t");
i = len;
}
print_node_brief (stderr, "", t, 0);
if (t == error_mark_node)
break;
}
if (i)
fprintf (stderr, "\n");
}
if (VEC_length (cp_class_binding, lvl->class_shadowed))
{
size_t i;
cp_class_binding *b;
fprintf (stderr, " class-shadowed:");
for (i = 0;
VEC_iterate(cp_class_binding, lvl->class_shadowed, i, b);
++i)
fprintf (stderr, " %s ", IDENTIFIER_POINTER (b->identifier));
fprintf (stderr, "\n");
}
if (lvl->type_shadowed)
{
fprintf (stderr, " type-shadowed:");
for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
{
fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
}
fprintf (stderr, "\n");
}
}
void
print_other_binding_stack (struct cp_binding_level *stack)
{
struct cp_binding_level *level;
for (level = stack; !global_scope_p (level); level = level->level_chain)
{
fprintf (stderr, "binding level " HOST_PTR_PRINTF "\n", (void *) level);
print_binding_level (level);
}
}
void
print_binding_stack (void)
{
struct cp_binding_level *b;
fprintf (stderr, "current_binding_level=" HOST_PTR_PRINTF
"\nclass_binding_level=" HOST_PTR_PRINTF
"\nNAMESPACE_LEVEL (global_namespace)=" HOST_PTR_PRINTF "\n",
(void *) current_binding_level, (void *) class_binding_level,
(void *) NAMESPACE_LEVEL (global_namespace));
if (class_binding_level)
{
for (b = class_binding_level; b; b = b->level_chain)
if (b == current_binding_level)
break;
if (b)
b = class_binding_level;
else
b = current_binding_level;
}
else
b = current_binding_level;
print_other_binding_stack (b);
fprintf (stderr, "global:\n");
print_binding_level (NAMESPACE_LEVEL (global_namespace));
}
tree
identifier_type_value (tree id)
{
timevar_push (TV_NAME_LOOKUP);
if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
id = lookup_name_real (id, 2, 1, true, 0, LOOKUP_COMPLAIN);
if (id)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
}
tree
identifier_global_value (tree t)
{
return IDENTIFIER_GLOBAL_VALUE (t);
}
static void
set_identifier_type_value_with_scope (tree id, tree decl, cxx_scope *b)
{
tree type;
if (b->kind != sk_namespace)
{
tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
b->type_shadowed
= tree_cons (id, old_type_value, b->type_shadowed);
type = decl ? TREE_TYPE (decl) : NULL_TREE;
TREE_TYPE (b->type_shadowed) = type;
}
else
{
cxx_binding *binding =
binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
gcc_assert (decl);
if (binding->value)
supplement_binding (binding, decl);
else
binding->value = decl;
type = global_type_node;
}
SET_IDENTIFIER_TYPE_VALUE (id, type);
}
void
set_identifier_type_value (tree id, tree decl)
{
set_identifier_type_value_with_scope (id, decl, current_binding_level);
}
static inline tree
constructor_name_full (tree type)
{
return TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
}
tree
constructor_name (tree type)
{
tree name;
name = constructor_name_full (type);
if (IDENTIFIER_TEMPLATE (name))
name = IDENTIFIER_TEMPLATE (name);
return name;
}
bool
constructor_name_p (tree name, tree type)
{
tree ctor_name;
if (!name)
return false;
if (TREE_CODE (name) != IDENTIFIER_NODE)
return false;
ctor_name = constructor_name_full (type);
if (name == ctor_name)
return true;
if (IDENTIFIER_TEMPLATE (ctor_name)
&& name == IDENTIFIER_TEMPLATE (ctor_name))
return true;
return false;
}
static GTY(()) int anon_cnt;
tree
make_anon_name (void)
{
char buf[32];
sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
return get_identifier (buf);
}
static inline cxx_binding *
find_binding (cxx_scope *scope, cxx_binding *binding)
{
timevar_push (TV_NAME_LOOKUP);
for (; binding != NULL; binding = binding->previous)
if (binding->scope == scope)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (cxx_binding *)0);
}
static inline cxx_binding *
cxx_scope_find_binding_for_name (cxx_scope *scope, tree name)
{
cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
if (b)
{
if (scope == b->scope && b->previous == NULL)
return b;
return find_binding (scope, b);
}
return NULL;
}
static cxx_binding *
binding_for_name (cxx_scope *scope, tree name)
{
cxx_binding *result;
result = cxx_scope_find_binding_for_name (scope, name);
if (result)
return result;
result = cxx_binding_make (NULL, NULL);
result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
result->scope = scope;
result->is_local = false;
result->value_is_inherited = false;
IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
return result;
}
tree
push_using_decl (tree scope, tree name)
{
tree decl;
timevar_push (TV_NAME_LOOKUP);
gcc_assert (TREE_CODE (scope) == NAMESPACE_DECL);
gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
break;
if (decl)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
namespace_bindings_p () ? decl : NULL_TREE);
decl = build_lang_decl (USING_DECL, name, void_type_node);
DECL_INITIAL (decl) = scope;
TREE_CHAIN (decl) = current_binding_level->usings;
current_binding_level->usings = decl;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
}
tree
pushdecl_with_scope (tree x, cxx_scope *level)
{
struct cp_binding_level *b;
tree function_decl = current_function_decl;
timevar_push (TV_NAME_LOOKUP);
current_function_decl = NULL_TREE;
if (level->kind == sk_class)
{
b = class_binding_level;
class_binding_level = level;
pushdecl_class_level (x);
class_binding_level = b;
}
else
{
b = current_binding_level;
current_binding_level = level;
x = pushdecl (x);
current_binding_level = b;
}
current_function_decl = function_decl;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
}
static tree
push_overloaded_decl (tree decl, int flags)
{
tree name = DECL_NAME (decl);
tree old;
tree new_binding;
int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
timevar_push (TV_NAME_LOOKUP);
if (doing_global)
old = namespace_binding (name, DECL_CONTEXT (decl));
else
old = lookup_name_innermost_nonclass_level (name);
if (old)
{
if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
{
tree t = TREE_TYPE (old);
if (IS_AGGR_TYPE (t) && warn_shadow
&& (! DECL_IN_SYSTEM_HEADER (decl)
|| ! DECL_IN_SYSTEM_HEADER (old)))
warning ("%q#D hides constructor for %q#T", decl, t);
old = NULL_TREE;
}
else if (is_overloaded_fn (old))
{
tree tmp;
for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
{
tree fn = OVL_CURRENT (tmp);
if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
&& !(flags & PUSH_USING)
&& compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
TYPE_ARG_TYPES (TREE_TYPE (decl)))
&& ! decls_match (fn, decl))
error ("%q#D conflicts with previous using declaration %q#D",
decl, fn);
if (duplicate_decls (decl, fn) == fn)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
}
if (TREE_CODE (old) == FUNCTION_DECL
&& DECL_ANTICIPATED (old))
old = NULL;
}
else if (old == error_mark_node)
old = NULL_TREE;
else
{
cp_error_at ("previous non-function declaration %q#D", old);
error ("conflicts with function declaration %q#D", decl);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
}
}
if (old || TREE_CODE (decl) == TEMPLATE_DECL
|| (flags & PUSH_USING))
{
if (old && TREE_CODE (old) != OVERLOAD)
new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
else
new_binding = ovl_cons (decl, old);
if (flags & PUSH_USING)
OVL_USED (new_binding) = 1;
}
else
new_binding = decl;
if (doing_global)
set_namespace_binding (name, current_namespace, new_binding);
else
{
if (TREE_CODE (new_binding) == OVERLOAD && old)
{
tree *d;
for (d = &IDENTIFIER_BINDING (name)->scope->names;
*d;
d = &TREE_CHAIN (*d))
if (*d == old
|| (TREE_CODE (*d) == TREE_LIST
&& TREE_VALUE (*d) == old))
{
if (TREE_CODE (*d) == TREE_LIST)
TREE_VALUE (*d) = new_binding;
else
*d = tree_cons (NULL_TREE, new_binding,
TREE_CHAIN (*d));
IDENTIFIER_BINDING (name)->value = new_binding;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
}
gcc_unreachable ();
}
push_local_binding (name, new_binding, flags);
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
}
static tree
validate_nonmember_using_decl (tree decl, tree scope, tree name)
{
if (TYPE_P (scope))
{
error ("%qT is not a namespace", scope);
return NULL_TREE;
}
else if (scope == error_mark_node)
return NULL_TREE;
if (TREE_CODE (decl) == TEMPLATE_ID_EXPR)
{
error ("a using-declaration cannot specify a template-id. "
"Try %<using %D%>", name);
return NULL_TREE;
}
if (TREE_CODE (decl) == NAMESPACE_DECL)
{
error ("namespace %qD not allowed in using-declaration", decl);
return NULL_TREE;
}
if (TREE_CODE (decl) == SCOPE_REF)
{
error ("%qT is not a namespace", TREE_OPERAND (decl, 0));
return NULL_TREE;
}
if (is_overloaded_fn (decl))
decl = get_first_fn (decl);
gcc_assert (DECL_P (decl));
return push_using_decl (scope, name);
}
static void
do_nonmember_using_decl (tree scope, tree name, tree oldval, tree oldtype,
tree *newval, tree *newtype)
{
struct scope_binding decls = EMPTY_SCOPE_BINDING;
*newval = *newtype = NULL_TREE;
if (!qualified_lookup_using_namespace (name, scope, &decls, 0))
return;
if (!decls.value && !decls.type)
{
error ("%qD not declared", name);
return;
}
if (oldval
&& TREE_CODE (oldval) == FUNCTION_DECL
&& DECL_ANTICIPATED (oldval))
oldval = NULL_TREE;
if (decls.value && is_overloaded_fn (decls.value))
{
tree tmp, tmp1;
if (oldval && !is_overloaded_fn (oldval))
{
if (!DECL_IMPLICIT_TYPEDEF_P (oldval))
error ("%qD is already declared in this scope", name);
oldval = NULL_TREE;
}
*newval = oldval;
for (tmp = decls.value; tmp; tmp = OVL_NEXT (tmp))
{
tree new_fn = OVL_CURRENT (tmp);
for (tmp1 = oldval; tmp1; tmp1 = OVL_NEXT (tmp1))
{
tree old_fn = OVL_CURRENT (tmp1);
if (new_fn == old_fn)
break;
else if (OVL_USED (tmp1))
continue;
else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (new_fn)),
TYPE_ARG_TYPES (TREE_TYPE (old_fn))))
{
gcc_assert (!DECL_ANTICIPATED (old_fn));
if (decls_match (new_fn, old_fn))
break;
else
{
error ("%qD is already declared in this scope", name);
break;
}
}
}
if (tmp1)
continue;
if (*newval && TREE_CODE (*newval) == OVERLOAD)
TREE_TYPE (*newval) = unknown_type_node;
*newval = build_overload (OVL_CURRENT (tmp), *newval);
if (TREE_CODE (*newval) != OVERLOAD)
{
*newval = ovl_cons (*newval, NULL_TREE);
TREE_TYPE (*newval) = TREE_TYPE (OVL_CURRENT (tmp));
}
OVL_USED (*newval) = 1;
}
}
else
{
*newval = decls.value;
if (oldval && !decls_match (*newval, oldval))
error ("%qD is already declared in this scope", name);
}
*newtype = decls.type;
if (oldtype && *newtype && !same_type_p (oldtype, *newtype))
{
error ("using declaration %qD introduced ambiguous type %qT",
name, oldtype);
return;
}
}
void
do_local_using_decl (tree decl, tree scope, tree name)
{
tree oldval, oldtype, newval, newtype;
tree orig_decl = decl;
decl = validate_nonmember_using_decl (decl, scope, name);
if (decl == NULL_TREE)
return;
if (building_stmt_tree ()
&& at_function_scope_p ())
add_decl_expr (decl);
oldval = lookup_name_innermost_nonclass_level (name);
oldtype = lookup_type_current_level (name);
do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
if (newval)
{
if (is_overloaded_fn (newval))
{
tree fn, term;
if (oldval && TREE_CODE (oldval) == OVERLOAD)
term = OVL_FUNCTION (oldval);
else
term = oldval;
for (fn = newval; fn && OVL_CURRENT (fn) != term;
fn = OVL_NEXT (fn))
push_overloaded_decl (OVL_CURRENT (fn),
PUSH_LOCAL | PUSH_USING);
}
else
push_local_binding (name, newval, PUSH_USING);
}
if (newtype)
{
push_local_binding (name, newtype, PUSH_USING);
set_identifier_type_value (name, newtype);
}
if (!processing_template_decl)
cp_emit_debug_info_for_using (orig_decl, current_scope());
}
bool
is_ancestor (tree root, tree child)
{
gcc_assert ((TREE_CODE (root) == NAMESPACE_DECL
|| TREE_CODE (root) == FUNCTION_DECL
|| CLASS_TYPE_P (root)));
gcc_assert ((TREE_CODE (child) == NAMESPACE_DECL
|| CLASS_TYPE_P (child)));
if (root == global_namespace)
return true;
while (true)
{
if (!child)
return false;
if (root == child)
return true;
if (TYPE_P (child))
child = TYPE_NAME (child);
child = DECL_CONTEXT (child);
}
}
tree
push_scope (tree t)
{
if (TREE_CODE (t) == NAMESPACE_DECL)
push_decl_namespace (t);
else if (CLASS_TYPE_P (t))
{
if (!at_class_scope_p ()
|| !same_type_p (current_class_type, t))
push_nested_class (t);
else
t = NULL_TREE;
}
return t;
}
void
pop_scope (tree t)
{
if (TREE_CODE (t) == NAMESPACE_DECL)
pop_decl_namespace ();
else if CLASS_TYPE_P (t)
pop_nested_class ();
}
static void
push_inner_scope_r (tree outer, tree inner)
{
tree prev;
if (outer == inner
|| (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
return;
prev = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
if (outer != prev)
push_inner_scope_r (outer, prev);
if (TREE_CODE (inner) == NAMESPACE_DECL)
{
struct cp_binding_level *save_template_parm = 0;
while (current_binding_level->kind == sk_template_parms)
{
struct cp_binding_level *b = current_binding_level;
current_binding_level = b->level_chain;
b->level_chain = save_template_parm;
save_template_parm = b;
}
resume_scope (NAMESPACE_LEVEL (inner));
current_namespace = inner;
while (save_template_parm)
{
struct cp_binding_level *b = save_template_parm;
save_template_parm = b->level_chain;
b->level_chain = current_binding_level;
current_binding_level = b;
}
}
else
pushclass (inner);
}
tree
push_inner_scope (tree inner)
{
tree outer = current_scope ();
if (!outer)
outer = current_namespace;
push_inner_scope_r (outer, inner);
return outer;
}
void
pop_inner_scope (tree outer, tree inner)
{
if (outer == inner
|| (TREE_CODE (inner) != NAMESPACE_DECL && !CLASS_TYPE_P (inner)))
return;
while (outer != inner)
{
if (TREE_CODE (inner) == NAMESPACE_DECL)
{
struct cp_binding_level *save_template_parm = 0;
while (current_binding_level->kind == sk_template_parms)
{
struct cp_binding_level *b = current_binding_level;
current_binding_level = b->level_chain;
b->level_chain = save_template_parm;
save_template_parm = b;
}
pop_namespace ();
while (save_template_parm)
{
struct cp_binding_level *b = save_template_parm;
save_template_parm = b->level_chain;
b->level_chain = current_binding_level;
current_binding_level = b;
}
}
else
popclass ();
inner = CP_DECL_CONTEXT (TREE_CODE (inner) == NAMESPACE_DECL ? inner : TYPE_NAME (inner));
}
}
void
pushlevel_class (void)
{
if (ENABLE_SCOPE_CHECKING)
is_class_level = 1;
class_binding_level = begin_scope (sk_class, current_class_type);
}
void
poplevel_class (void)
{
struct cp_binding_level *level = class_binding_level;
cp_class_binding *cb;
size_t i;
tree shadowed;
timevar_push (TV_NAME_LOOKUP);
gcc_assert (level != 0);
if (current_class_depth == 1)
previous_class_level = level;
for (shadowed = level->type_shadowed;
shadowed;
shadowed = TREE_CHAIN (shadowed))
SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
if (level->class_shadowed)
{
for (i = 0;
VEC_iterate (cp_class_binding, level->class_shadowed, i, cb);
++i)
IDENTIFIER_BINDING (cb->identifier) = cb->base.previous;
ggc_free (level->class_shadowed);
level->class_shadowed = NULL;
}
if (ENABLE_SCOPE_CHECKING)
is_class_level = 1;
leave_scope ();
timevar_pop (TV_NAME_LOOKUP);
}
static void
set_inherited_value_binding_p (cxx_binding *binding, tree decl,
tree class_type)
{
if (binding->value == decl && TREE_CODE (decl) != TREE_LIST)
{
tree context;
if (TREE_CODE (decl) == OVERLOAD)
context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
else
{
gcc_assert (DECL_P (decl));
context = context_for_name_lookup (decl);
}
if (is_properly_derived_from (class_type, context))
INHERITED_VALUE_BINDING_P (binding) = 1;
else
INHERITED_VALUE_BINDING_P (binding) = 0;
}
else if (binding->value == decl)
INHERITED_VALUE_BINDING_P (binding) = 1;
else
INHERITED_VALUE_BINDING_P (binding) = 0;
}
bool
pushdecl_class_level (tree x)
{
tree name;
bool is_valid = true;
timevar_push (TV_NAME_LOOKUP);
if (TREE_CODE (x) == OVERLOAD)
name = DECL_NAME (get_first_fn (x));
else
name = DECL_NAME (x);
if (name)
{
is_valid = push_class_level_binding (name, x);
if (TREE_CODE (x) == TYPE_DECL)
set_identifier_type_value (name, x);
}
else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
{
tree f;
for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
{
location_t save_location = input_location;
input_location = DECL_SOURCE_LOCATION (f);
if (!pushdecl_class_level (f))
is_valid = false;
input_location = save_location;
}
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, is_valid);
}
static cxx_binding *
get_class_binding (tree name, cxx_scope *scope)
{
tree class_type;
tree type_binding;
tree value_binding;
cxx_binding *binding;
class_type = scope->this_entity;
type_binding = lookup_member (class_type, name,
2, true);
value_binding = lookup_member (class_type, name,
2, false);
if (value_binding
&& (TREE_CODE (value_binding) == TYPE_DECL
|| DECL_CLASS_TEMPLATE_P (value_binding)
|| (TREE_CODE (value_binding) == TREE_LIST
&& TREE_TYPE (value_binding) == error_mark_node
&& (TREE_CODE (TREE_VALUE (value_binding))
== TYPE_DECL))))
;
else if (value_binding)
{
if (TREE_CODE (value_binding) == TREE_LIST
&& TREE_TYPE (value_binding) == error_mark_node)
;
else if (BASELINK_P (value_binding))
value_binding = BASELINK_FUNCTIONS (value_binding);
}
if (type_binding || value_binding)
{
binding = new_class_binding (name,
value_binding,
type_binding,
scope);
LOCAL_BINDING_P (binding) = 0;
set_inherited_value_binding_p (binding, value_binding, class_type);
}
else
binding = NULL;
return binding;
}
bool
push_class_level_binding (tree name, tree x)
{
cxx_binding *binding;
tree decl = x;
bool ok;
timevar_push (TV_NAME_LOOKUP);
if (!class_binding_level)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
gcc_assert (TYPE_BEING_DEFINED (current_class_type));
if (TREE_CODE (decl) == TREE_LIST
&& TREE_TYPE (decl) == error_mark_node)
decl = TREE_VALUE (decl);
check_template_shadow (decl);
if ((TREE_CODE (x) == VAR_DECL
|| TREE_CODE (x) == CONST_DECL
|| (TREE_CODE (x) == TYPE_DECL
&& !DECL_SELF_REFERENCE_P (x))
|| (TREE_CODE (x) == FIELD_DECL
&& DECL_CONTEXT (x) != current_class_type))
&& DECL_NAME (x) == constructor_name (current_class_type))
{
tree scope = context_for_name_lookup (x);
if (TYPE_P (scope) && same_type_p (scope, current_class_type))
{
error ("%qD has the same name as the class in which it is "
"declared",
x);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
}
}
binding = IDENTIFIER_BINDING (name);
if (!binding || binding->scope != class_binding_level)
{
binding = get_class_binding (name, class_binding_level);
if (binding)
{
binding->previous = IDENTIFIER_BINDING (name);
IDENTIFIER_BINDING (name) = binding;
}
}
if (binding && binding->value)
{
tree bval = binding->value;
tree old_decl = NULL_TREE;
if (INHERITED_VALUE_BINDING_P (binding))
{
if (TREE_CODE (bval) == TYPE_DECL && DECL_ARTIFICIAL (bval)
&& !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
{
old_decl = binding->type;
binding->type = bval;
binding->value = NULL_TREE;
INHERITED_VALUE_BINDING_P (binding) = 0;
}
else
old_decl = bval;
}
else if (TREE_CODE (x) == OVERLOAD && is_overloaded_fn (bval))
old_decl = bval;
else if (TREE_CODE (x) == USING_DECL && TREE_CODE (bval) == USING_DECL)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
else if (TREE_CODE (x) == USING_DECL && is_overloaded_fn (bval))
old_decl = bval;
else if (TREE_CODE (bval) == USING_DECL && is_overloaded_fn (x))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
if (old_decl && binding->scope == class_binding_level)
{
binding->value = x;
INHERITED_VALUE_BINDING_P (binding) = 0;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
}
}
note_name_declared_in_class (name, decl);
if (binding && binding->scope == class_binding_level)
ok = supplement_binding (binding, decl);
else
{
push_binding (name, decl, class_binding_level);
ok = true;
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
}
tree
do_class_using_decl (tree scope, tree name)
{
tree value, type;
if (!scope || !TYPE_P (scope))
{
error ("using-declaration for non-member at class scope");
return NULL_TREE;
}
if (TREE_CODE (name) == BIT_NOT_EXPR)
{
error ("using-declaration cannot name destructor");
return NULL_TREE;
}
gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
type = dependent_type_p (scope) ? NULL_TREE : void_type_node;
value = build_lang_decl (USING_DECL, name, type);
DECL_INITIAL (value) = scope;
if (scope && !processing_template_decl)
{
tree r;
r = lookup_qualified_name (scope, name, false, false);
if (r && (DECL_P (r) || TREE_CODE (r) == OVERLOAD))
cp_emit_debug_info_for_using (r, scope);
}
return value;
}
tree
namespace_binding (tree name, tree scope)
{
cxx_binding *binding;
if (scope == NULL)
scope = global_namespace;
else
scope = ORIGINAL_NAMESPACE (scope);
binding = cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
return binding ? binding->value : NULL_TREE;
}
void
set_namespace_binding (tree name, tree scope, tree val)
{
cxx_binding *b;
timevar_push (TV_NAME_LOOKUP);
if (scope == NULL_TREE)
scope = global_namespace;
b = binding_for_name (NAMESPACE_LEVEL (scope), name);
if (!b->value || TREE_CODE (val) == OVERLOAD || val == error_mark_node)
b->value = val;
else
supplement_binding (b, val);
timevar_pop (TV_NAME_LOOKUP);
}
void
set_decl_namespace (tree decl, tree scope, bool friendp)
{
tree old;
scope = ORIGINAL_NAMESPACE (scope);
if (!friendp && !is_ancestor (current_namespace, scope))
error ("declaration of %qD not in a namespace surrounding %qD",
decl, scope);
DECL_CONTEXT (decl) = FROB_CONTEXT (scope);
if (scope == current_namespace)
{
if (at_namespace_scope_p ())
error ("explicit qualification in declaration of `%D'",
decl);
return;
}
old = namespace_binding (DECL_NAME (decl), scope);
if (!old)
goto complain;
if (processing_explicit_instantiation)
return;
if (!is_overloaded_fn (decl))
return;
if (!is_overloaded_fn (old))
goto complain;
if (processing_template_decl || processing_specialization)
return;
if (is_overloaded_fn (old))
{
for (; old; old = OVL_NEXT (old))
if (decls_match (decl, OVL_CURRENT (old)))
return;
}
else if (decls_match (decl, old))
return;
complain:
error ("%qD should have been declared inside %qD", decl, scope);
}
static tree
current_decl_namespace (void)
{
tree result;
if (decl_namespace_list)
return TREE_PURPOSE (decl_namespace_list);
if (current_class_type)
result = decl_namespace_context (current_class_type);
else if (current_function_decl)
result = decl_namespace_context (current_function_decl);
else
result = current_namespace;
return result;
}
void
push_namespace (tree name)
{
tree d = NULL_TREE;
int need_new = 1;
int implicit_use = 0;
bool anon = !name;
timevar_push (TV_NAME_LOOKUP);
gcc_assert (global_namespace != NULL && name != global_scope_name);
if (anon)
{
if (!anonymous_namespace_name)
anonymous_namespace_name = get_file_function_name ('N');
name = anonymous_namespace_name;
d = IDENTIFIER_NAMESPACE_VALUE (name);
if (d)
need_new = 0;
implicit_use = 1;
}
else
{
d = IDENTIFIER_NAMESPACE_VALUE (name);
if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
{
need_new = 0;
if (DECL_NAMESPACE_ALIAS (d))
{
error ("namespace alias %qD not allowed here, assuming %qD",
d, DECL_NAMESPACE_ALIAS (d));
d = DECL_NAMESPACE_ALIAS (d);
}
}
}
if (need_new)
{
d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
pushdecl (d);
if (anon)
{
SET_DECL_ASSEMBLER_NAME (d, name);
DECL_NAME (d) = NULL_TREE;
}
begin_scope (sk_namespace, d);
}
else
resume_scope (NAMESPACE_LEVEL (d));
if (implicit_use)
do_using_directive (d);
current_namespace = d;
timevar_pop (TV_NAME_LOOKUP);
}
void
pop_namespace (void)
{
gcc_assert (current_namespace != global_namespace);
current_namespace = CP_DECL_CONTEXT (current_namespace);
leave_scope ();
}
void
push_nested_namespace (tree ns)
{
if (ns == global_namespace)
push_to_top_level ();
else
{
push_nested_namespace (CP_DECL_CONTEXT (ns));
push_namespace (DECL_NAME (ns));
}
}
void
pop_nested_namespace (tree ns)
{
timevar_push (TV_NAME_LOOKUP);
while (ns != global_namespace)
{
pop_namespace ();
ns = CP_DECL_CONTEXT (ns);
}
pop_from_top_level ();
timevar_pop (TV_NAME_LOOKUP);
}
void
push_decl_namespace (tree decl)
{
if (TREE_CODE (decl) != NAMESPACE_DECL)
decl = decl_namespace_context (decl);
decl_namespace_list = tree_cons (ORIGINAL_NAMESPACE (decl),
NULL_TREE, decl_namespace_list);
}
void
pop_decl_namespace (void)
{
decl_namespace_list = TREE_CHAIN (decl_namespace_list);
}
static tree
namespace_ancestor (tree ns1, tree ns2)
{
timevar_push (TV_NAME_LOOKUP);
if (is_ancestor (ns1, ns2))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ns1);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
namespace_ancestor (CP_DECL_CONTEXT (ns1), ns2));
}
void
do_namespace_alias (tree alias, tree namespace)
{
if (TREE_CODE (namespace) != NAMESPACE_DECL)
{
error ("unknown namespace %qD", namespace);
return;
}
namespace = ORIGINAL_NAMESPACE (namespace);
alias = build_lang_decl (NAMESPACE_DECL, alias, void_type_node);
DECL_NAMESPACE_ALIAS (alias) = namespace;
DECL_EXTERNAL (alias) = 1;
DECL_CONTEXT (alias) = FROB_CONTEXT (current_scope ());
pushdecl (alias);
(*debug_hooks->global_decl) (alias);
}
tree
pushdecl_namespace_level (tree x)
{
struct cp_binding_level *b = current_binding_level;
tree t;
timevar_push (TV_NAME_LOOKUP);
t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
if (TREE_CODE (t) == TYPE_DECL)
{
tree name = DECL_NAME (t);
tree newval;
tree *ptr = (tree *)0;
for (; !global_scope_p (b); b = b->level_chain)
{
tree shadowed = b->type_shadowed;
for (; shadowed; shadowed = TREE_CHAIN (shadowed))
if (TREE_PURPOSE (shadowed) == name)
{
ptr = &TREE_VALUE (shadowed);
}
}
newval = TREE_TYPE (t);
if (ptr == (tree *)0)
{
SET_IDENTIFIER_TYPE_VALUE (name, t);
}
else
{
*ptr = newval;
}
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
static void
add_using_namespace (tree user, tree used, bool indirect)
{
tree t;
timevar_push (TV_NAME_LOOKUP);
if (user == used)
{
timevar_pop (TV_NAME_LOOKUP);
return;
}
gcc_assert (TREE_CODE (user) == NAMESPACE_DECL);
gcc_assert (TREE_CODE (used) == NAMESPACE_DECL);
t = purpose_member (used, DECL_NAMESPACE_USING (user));
if (t != NULL_TREE)
{
if (!indirect)
TREE_INDIRECT_USING (t) = 0;
timevar_pop (TV_NAME_LOOKUP);
return;
}
DECL_NAMESPACE_USING (user)
= tree_cons (used, namespace_ancestor (user, used),
DECL_NAMESPACE_USING (user));
TREE_INDIRECT_USING (DECL_NAMESPACE_USING (user)) = indirect;
DECL_NAMESPACE_USERS (used)
= tree_cons (user, 0, DECL_NAMESPACE_USERS (used));
for (t = DECL_NAMESPACE_USING (used); t; t = TREE_CHAIN (t))
add_using_namespace (user, TREE_PURPOSE (t), 1);
for (t = DECL_NAMESPACE_USERS (user); t; t = TREE_CHAIN (t))
add_using_namespace (TREE_PURPOSE (t), used, 1);
timevar_pop (TV_NAME_LOOKUP);
}
void
do_toplevel_using_decl (tree decl, tree scope, tree name)
{
tree oldval, oldtype, newval, newtype;
tree orig_decl = decl;
cxx_binding *binding;
decl = validate_nonmember_using_decl (decl, scope, name);
if (decl == NULL_TREE)
return;
binding = binding_for_name (NAMESPACE_LEVEL (current_namespace), name);
oldval = binding->value;
oldtype = binding->type;
do_nonmember_using_decl (scope, name, oldval, oldtype, &newval, &newtype);
if (!processing_template_decl)
cp_emit_debug_info_for_using (orig_decl, current_namespace);
if (newval)
binding->value = newval;
if (newtype)
binding->type = newtype;
return;
}
void
do_using_directive (tree namespace)
{
tree context = NULL_TREE;
if (building_stmt_tree ())
add_stmt (build_stmt (USING_STMT, namespace));
if (TREE_CODE (namespace) == SCOPE_REF)
namespace = TREE_OPERAND (namespace, 1);
if (TREE_CODE (namespace) == IDENTIFIER_NODE)
{
if (!processing_template_decl)
error ("namespace %qT undeclared", namespace);
return;
}
if (TREE_CODE (namespace) != NAMESPACE_DECL)
{
if (!processing_template_decl)
error ("%qT is not a namespace", namespace);
return;
}
namespace = ORIGINAL_NAMESPACE (namespace);
if (!toplevel_bindings_p ())
{
push_using_directive (namespace);
context = current_scope ();
}
else
{
add_using_namespace (current_namespace, namespace, 0);
if (current_namespace != global_namespace)
context = current_namespace;
}
if (!processing_template_decl)
(*debug_hooks->imported_module_or_decl) (namespace, context);
}
void
parse_using_directive (tree namespace, tree attribs)
{
tree a;
do_using_directive (namespace);
for (a = attribs; a; a = TREE_CHAIN (a))
{
tree name = TREE_PURPOSE (a);
if (is_attribute_p ("strong", name))
{
if (!toplevel_bindings_p ())
error ("strong using only meaningful at namespace scope");
else if (namespace != error_mark_node)
DECL_NAMESPACE_ASSOCIATIONS (namespace)
= tree_cons (current_namespace, 0,
DECL_NAMESPACE_ASSOCIATIONS (namespace));
}
else
warning ("%qD attribute directive ignored", name);
}
}
static tree
pushdecl_top_level_1 (tree x, tree *init)
{
timevar_push (TV_NAME_LOOKUP);
push_to_top_level ();
x = pushdecl_namespace_level (x);
if (init)
cp_finish_decl (x, *init, NULL_TREE, 0);
pop_from_top_level ();
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
}
tree
pushdecl_top_level (tree x)
{
return pushdecl_top_level_1 (x, NULL);
}
tree
pushdecl_top_level_and_finish (tree x, tree init)
{
return pushdecl_top_level_1 (x, &init);
}
static tree
merge_functions (tree s1, tree s2)
{
for (; s2; s2 = OVL_NEXT (s2))
{
tree fn2 = OVL_CURRENT (s2);
tree fns1;
for (fns1 = s1; fns1; fns1 = OVL_NEXT (fns1))
{
tree fn1 = OVL_CURRENT (fns1);
if (fn1 == fn2
|| (DECL_EXTERN_C_P (fn1) && DECL_EXTERN_C_P (fn2)
&& DECL_NAME (fn1) == DECL_NAME (fn2)))
break;
}
if (!fns1)
s1 = build_overload (fn2, s1);
}
return s1;
}
static void
ambiguous_decl (tree name, struct scope_binding *old, cxx_binding *new,
int flags)
{
tree val, type;
gcc_assert (old != NULL);
val = new->value;
if (val)
switch (TREE_CODE (val))
{
case TEMPLATE_DECL:
if (LOOKUP_QUALIFIERS_ONLY (flags)
&& !DECL_CLASS_TEMPLATE_P (val))
val = NULL_TREE;
break;
case TYPE_DECL:
if (LOOKUP_NAMESPACES_ONLY (flags))
val = NULL_TREE;
break;
case NAMESPACE_DECL:
if (LOOKUP_TYPES_ONLY (flags))
val = NULL_TREE;
break;
case FUNCTION_DECL:
if (LOOKUP_QUALIFIERS_ONLY (flags) || DECL_ANTICIPATED (val))
val = NULL_TREE;
break;
default:
if (LOOKUP_QUALIFIERS_ONLY (flags))
val = NULL_TREE;
}
if (!old->value)
old->value = val;
else if (val && val != old->value)
{
if (is_overloaded_fn (old->value) && is_overloaded_fn (val))
old->value = merge_functions (old->value, val);
else
{
if (flags & LOOKUP_COMPLAIN)
{
if (old->value != error_mark_node)
{
error ("use of %qD is ambiguous", name);
cp_error_at (" first declared as %q#D here", old->value);
}
cp_error_at (" also declared as %q#D here", val);
}
old->value = error_mark_node;
}
}
type = new->type;
if (LOOKUP_NAMESPACES_ONLY (flags))
type = NULL_TREE;
if (!old->type)
old->type = type;
else if (type && old->type != type)
{
if (flags & LOOKUP_COMPLAIN)
{
error ("%qD denotes an ambiguous type",name);
error ("%J first type here", TYPE_MAIN_DECL (old->type));
error ("%J other type here", TYPE_MAIN_DECL (type));
}
}
}
tree
cp_namespace_decls (tree ns)
{
return NAMESPACE_LEVEL (ns)->names;
}
static int
lookup_flags (int prefer_type, int namespaces_only)
{
if (namespaces_only)
return LOOKUP_PREFER_NAMESPACES;
if (prefer_type > 1)
return LOOKUP_PREFER_TYPES;
if (prefer_type > 0)
return LOOKUP_PREFER_BOTH;
return 0;
}
static tree
qualify_lookup (tree val, int flags)
{
if (val == NULL_TREE)
return val;
if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
return val;
if ((flags & LOOKUP_PREFER_TYPES)
&& (TREE_CODE (val) == TYPE_DECL || TREE_CODE (val) == TEMPLATE_DECL))
return val;
if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
return NULL_TREE;
return val;
}
tree
lookup_namespace_name (tree namespace, tree name)
{
tree val;
tree template_id = NULL_TREE;
struct scope_binding binding = EMPTY_SCOPE_BINDING;
timevar_push (TV_NAME_LOOKUP);
gcc_assert (TREE_CODE (namespace) == NAMESPACE_DECL);
if (TREE_CODE (name) == NAMESPACE_DECL)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
else if (TREE_CODE (name) == TEMPLATE_DECL)
{
error ("invalid use of %qD", name);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
}
namespace = ORIGINAL_NAMESPACE (namespace);
if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
{
template_id = name;
name = TREE_OPERAND (name, 0);
if (TREE_CODE (name) == OVERLOAD)
name = DECL_NAME (OVL_CURRENT (name));
else if (DECL_P (name))
name = DECL_NAME (name);
}
gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
if (binding.value)
{
val = binding.value;
if (template_id)
{
if (DECL_CLASS_TEMPLATE_P (val))
val = lookup_template_class (val,
TREE_OPERAND (template_id, 1),
NULL_TREE,
NULL_TREE,
0,
tf_error | tf_warning);
else if (DECL_FUNCTION_TEMPLATE_P (val)
|| TREE_CODE (val) == OVERLOAD)
val = lookup_template_function (val,
TREE_OPERAND (template_id, 1));
else
{
error ("%<%D::%D%> is not a template", namespace, name);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
}
}
if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
val = OVL_FUNCTION (val);
if (!val || !DECL_P(val)
|| !DECL_LANG_SPECIFIC(val)
|| !DECL_ANTICIPATED (val))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
}
error ("%qD undeclared in namespace %qD", name, namespace);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
}
static tree
select_decl (const struct scope_binding *binding, int flags)
{
tree val;
val = binding->value;
timevar_push (TV_NAME_LOOKUP);
if (LOOKUP_NAMESPACES_ONLY (flags))
{
if (val && TREE_CODE (val) == NAMESPACE_DECL)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
}
if (binding->type && (!val || (flags & LOOKUP_PREFER_TYPES)))
val = binding->type;
else if (val && LOOKUP_TYPES_ONLY (flags)
&& ! DECL_DECLARES_TYPE_P (val))
val = NULL_TREE;
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
}
static tree
unqualified_namespace_lookup (tree name, int flags)
{
tree initial = current_decl_namespace ();
tree scope = initial;
tree siter;
struct cp_binding_level *level;
tree val = NULL_TREE;
struct scope_binding binding = EMPTY_SCOPE_BINDING;
timevar_push (TV_NAME_LOOKUP);
for (; !val; scope = CP_DECL_CONTEXT (scope))
{
cxx_binding *b =
cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
if (b)
{
if (b->value && DECL_P (b->value)
&& DECL_LANG_SPECIFIC (b->value)
&& DECL_ANTICIPATED (b->value))
;
else
binding.value = b->value;
binding.type = b->type;
}
for (level = current_binding_level;
level->kind != sk_namespace;
level = level->level_chain)
if (!lookup_using_namespace (name, &binding, level->using_directives,
scope, flags))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
siter = initial;
while (1)
{
if (!lookup_using_namespace (name, &binding,
DECL_NAMESPACE_USING (siter),
scope, flags))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
if (siter == scope) break;
siter = CP_DECL_CONTEXT (siter);
}
val = select_decl (&binding, flags);
if (scope == global_namespace)
break;
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
}
tree
lookup_qualified_name (tree scope, tree name, bool is_type_p, bool complain)
{
int flags = 0;
if (TREE_CODE (scope) == NAMESPACE_DECL)
{
struct scope_binding binding = EMPTY_SCOPE_BINDING;
flags |= LOOKUP_COMPLAIN;
if (is_type_p)
flags |= LOOKUP_PREFER_TYPES;
if (qualified_lookup_using_namespace (name, scope, &binding, flags))
return select_decl (&binding, flags);
}
else if (is_aggr_type (scope, complain))
{
tree t;
t = lookup_member (scope, name, 2, is_type_p);
if (t)
return t;
}
return error_mark_node;
}
static bool
lookup_using_namespace (tree name, struct scope_binding *val,
tree usings, tree scope, int flags)
{
tree iter;
timevar_push (TV_NAME_LOOKUP);
for (iter = usings; iter; iter = TREE_CHAIN (iter))
if (TREE_VALUE (iter) == scope)
{
tree used = ORIGINAL_NAMESPACE (TREE_PURPOSE (iter));
cxx_binding *val1 =
cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (used), name);
if (val1)
ambiguous_decl (name, val, val1, flags);
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val->value != error_mark_node);
}
static bool
qualified_lookup_using_namespace (tree name, tree scope,
struct scope_binding *result, int flags)
{
tree seen = NULL_TREE;
tree todo = NULL_TREE;
tree todo_maybe = NULL_TREE;
tree usings;
timevar_push (TV_NAME_LOOKUP);
scope = ORIGINAL_NAMESPACE (scope);
while (scope && result->value != error_mark_node)
{
cxx_binding *binding =
cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
seen = tree_cons (scope, NULL_TREE, seen);
if (binding)
ambiguous_decl (name, result, binding, flags);
for (usings = DECL_NAMESPACE_USING (scope); usings;
usings = TREE_CHAIN (usings))
if (!TREE_INDIRECT_USING (usings))
{
if (is_associated_namespace (scope, TREE_PURPOSE (usings))
&& !purpose_member (TREE_PURPOSE (usings), seen)
&& !purpose_member (TREE_PURPOSE (usings), todo))
todo = tree_cons (TREE_PURPOSE (usings), NULL_TREE, todo);
else if ((!result->value && !result->type)
&& !purpose_member (TREE_PURPOSE (usings), seen)
&& !purpose_member (TREE_PURPOSE (usings), todo)
&& !purpose_member (TREE_PURPOSE (usings), todo_maybe))
todo_maybe = tree_cons (TREE_PURPOSE (usings), NULL_TREE,
todo_maybe);
}
if (todo)
{
scope = TREE_PURPOSE (todo);
todo = TREE_CHAIN (todo);
}
else if (todo_maybe
&& (!result->value && !result->type))
{
scope = TREE_PURPOSE (todo_maybe);
todo = TREE_CHAIN (todo_maybe);
todo_maybe = NULL_TREE;
}
else
scope = NULL_TREE;
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result->value != error_mark_node);
}
cxx_binding *
outer_binding (tree name,
cxx_binding *binding,
bool class_p)
{
cxx_binding *outer;
cxx_scope *scope;
cxx_scope *outer_scope;
if (binding)
{
scope = binding->scope->level_chain;
outer = binding->previous;
}
else
{
scope = current_binding_level;
outer = IDENTIFIER_BINDING (name);
}
outer_scope = outer ? outer->scope : NULL;
if (class_p)
while (scope && scope != outer_scope && scope->kind != sk_namespace)
{
if (scope->kind == sk_class)
{
cxx_binding *class_binding;
class_binding = get_class_binding (name, scope);
if (class_binding)
{
class_binding->previous = outer;
if (binding)
binding->previous = class_binding;
else
IDENTIFIER_BINDING (name) = class_binding;
return class_binding;
}
}
scope = scope->level_chain;
}
return outer;
}
tree
innermost_non_namespace_value (tree name)
{
cxx_binding *binding;
binding = outer_binding (name, NULL, true);
return binding ? binding->value : NULL_TREE;
}
tree
lookup_name_real (tree name, int prefer_type, int nonclass, bool block_p,
int namespaces_only, int flags)
{
cxx_binding *iter;
tree val = NULL_TREE;
timevar_push (TV_NAME_LOOKUP);
if (IDENTIFIER_TYPENAME_P (name))
{
struct cp_binding_level *level;
for (level = current_binding_level;
level && level->kind != sk_namespace;
level = level->level_chain)
{
tree class_type;
tree operators;
if (level->kind != sk_class)
continue;
class_type = level->this_entity;
operators = lookup_fnfields (class_type, name, 0);
if (operators)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
}
flags |= lookup_flags (prefer_type, namespaces_only);
if (current_class_type == NULL_TREE)
nonclass = 1;
if (block_p || !nonclass)
for (iter = outer_binding (name, NULL, !nonclass);
iter;
iter = outer_binding (name, iter, !nonclass))
{
tree binding;
if (LOCAL_BINDING_P (iter) ? !block_p : nonclass)
continue;
if (qualify_lookup (iter->value, flags))
binding = iter->value;
else if ((flags & LOOKUP_PREFER_TYPES)
&& qualify_lookup (iter->type, flags))
binding = iter->type;
else
binding = NULL_TREE;
if (binding)
{
val = binding;
break;
}
}
if (!val)
val = unqualified_namespace_lookup (name, flags);
if (val)
{
if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
val = OVL_FUNCTION (val);
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
}
tree
lookup_name_nonclass (tree name)
{
return lookup_name_real (name, 0, 1, true, 0, LOOKUP_COMPLAIN);
}
tree
lookup_function_nonclass (tree name, tree args, bool block_p)
{
return
lookup_arg_dependent (name,
lookup_name_real (name, 0, 1, block_p, 0,
LOOKUP_COMPLAIN),
args);
}
tree
lookup_name (tree name, int prefer_type)
{
return lookup_name_real (name, prefer_type, 0, true,
0, LOOKUP_COMPLAIN);
}
tree
lookup_name_two (tree name, int prefer_type)
{
return lookup_name (name, prefer_type);
}
tree
lookup_type_scope (tree name, tag_scope scope)
{
cxx_binding *iter = NULL;
tree val = NULL_TREE;
timevar_push (TV_NAME_LOOKUP);
if (current_binding_level->kind != sk_namespace)
iter = outer_binding (name, NULL, true);
for (; iter; iter = outer_binding (name, iter, true))
{
if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES)
&& (scope != ts_current
|| LOCAL_BINDING_P (iter)
|| DECL_CONTEXT (iter->type) == iter->scope->this_entity))
val = iter->type;
else if ((scope != ts_current
|| !INHERITED_VALUE_BINDING_P (iter))
&& qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
val = iter->value;
if (val)
break;
}
if (!val)
{
iter = cxx_scope_find_binding_for_name
(NAMESPACE_LEVEL (current_decl_namespace ()), name);
if (iter)
{
if (qualify_lookup (iter->type, LOOKUP_PREFER_TYPES))
val = iter->type;
else if (qualify_lookup (iter->value, LOOKUP_PREFER_TYPES))
val = iter->value;
}
}
if (val)
{
struct cp_binding_level *b = current_binding_level;
while (b)
{
if (iter->scope == b)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
if (b->kind == sk_cleanup || b->kind == sk_template_parms)
b = b->level_chain;
else if (b->kind == sk_class
&& scope == ts_within_enclosing_non_class)
b = b->level_chain;
else
break;
}
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
}
static tree
lookup_name_innermost_nonclass_level (tree name)
{
struct cp_binding_level *b;
tree t = NULL_TREE;
timevar_push (TV_NAME_LOOKUP);
b = innermost_nonclass_level ();
if (b->kind == sk_namespace)
{
t = IDENTIFIER_NAMESPACE_VALUE (name);
if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
t = TREE_VALUE (t);
}
else if (IDENTIFIER_BINDING (name)
&& LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
{
cxx_binding *binding;
binding = IDENTIFIER_BINDING (name);
while (1)
{
if (binding->scope == b
&& !(TREE_CODE (binding->value) == VAR_DECL
&& DECL_DEAD_FOR_LOCAL (binding->value)))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, binding->value);
if (b->kind == sk_cleanup)
b = b->level_chain;
else
break;
}
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
static tree
lookup_type_current_level (tree name)
{
tree t = NULL_TREE;
timevar_push (TV_NAME_LOOKUP);
gcc_assert (current_binding_level->kind != sk_namespace);
if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
&& REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
{
struct cp_binding_level *b = current_binding_level;
while (1)
{
if (purpose_member (name, b->type_shadowed))
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
REAL_IDENTIFIER_TYPE_VALUE (name));
if (b->kind == sk_cleanup)
b = b->level_chain;
else
break;
}
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
}
struct arg_lookup
{
tree name;
tree namespaces;
tree classes;
tree functions;
};
static bool arg_assoc (struct arg_lookup*, tree);
static bool arg_assoc_args (struct arg_lookup*, tree);
static bool arg_assoc_type (struct arg_lookup*, tree);
static bool add_function (struct arg_lookup *, tree);
static bool arg_assoc_namespace (struct arg_lookup *, tree);
static bool arg_assoc_class (struct arg_lookup *, tree);
static bool arg_assoc_template_arg (struct arg_lookup*, tree);
static bool
add_function (struct arg_lookup *k, tree fn)
{
if (!k->functions)
k->functions = fn;
else if (fn == k->functions)
;
else if (is_overloaded_fn (k->functions) && is_overloaded_fn (fn))
k->functions = build_overload (fn, k->functions);
else
{
tree f1 = OVL_CURRENT (k->functions);
tree f2 = fn;
if (is_overloaded_fn (f1))
{
fn = f1; f1 = f2; f2 = fn;
}
cp_error_at ("%qD is not a function,", f1);
cp_error_at (" conflict with %qD", f2);
error (" in call to %qD", k->name);
return true;
}
return false;
}
bool
is_associated_namespace (tree current, tree scope)
{
tree seen = NULL_TREE;
tree todo = NULL_TREE;
tree t;
while (1)
{
if (scope == current)
return true;
seen = tree_cons (scope, NULL_TREE, seen);
for (t = DECL_NAMESPACE_ASSOCIATIONS (scope); t; t = TREE_CHAIN (t))
if (!purpose_member (TREE_PURPOSE (t), seen))
todo = tree_cons (TREE_PURPOSE (t), NULL_TREE, todo);
if (todo)
{
scope = TREE_PURPOSE (todo);
todo = TREE_CHAIN (todo);
}
else
return false;
}
}
static bool
arg_assoc_namespace (struct arg_lookup *k, tree scope)
{
tree value;
if (purpose_member (scope, k->namespaces))
return 0;
k->namespaces = tree_cons (scope, NULL_TREE, k->namespaces);
for (value = DECL_NAMESPACE_ASSOCIATIONS (scope); value;
value = TREE_CHAIN (value))
if (arg_assoc_namespace (k, TREE_PURPOSE (value)))
return true;
value = namespace_binding (k->name, scope);
if (!value)
return false;
for (; value; value = OVL_NEXT (value))
if (add_function (k, OVL_CURRENT (value)))
return true;
return false;
}
static bool
arg_assoc_template_arg (struct arg_lookup *k, tree arg)
{
if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
|| TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
return false;
else if (TREE_CODE (arg) == TEMPLATE_DECL)
{
tree ctx = CP_DECL_CONTEXT (arg);
if (TREE_CODE (ctx) == NAMESPACE_DECL)
return arg_assoc_namespace (k, ctx);
else
return arg_assoc_class (k, ctx);
}
else if (TYPE_P (arg))
return arg_assoc_type (k, arg);
else
return false;
}
static bool
arg_assoc_class (struct arg_lookup *k, tree type)
{
tree list, friends, context;
int i;
if (!CLASS_TYPE_P (type))
return false;
if (purpose_member (type, k->classes))
return false;
k->classes = tree_cons (type, NULL_TREE, k->classes);
context = decl_namespace_context (type);
if (arg_assoc_namespace (k, context))
return true;
if (TYPE_BINFO (type))
{
tree binfo, base_binfo;
for (binfo = TYPE_BINFO (type), i = 0;
BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
if (arg_assoc_class (k, BINFO_TYPE (base_binfo)))
return true;
}
for (list = DECL_FRIENDLIST (TYPE_MAIN_DECL (type)); list;
list = TREE_CHAIN (list))
if (k->name == FRIEND_NAME (list))
for (friends = FRIEND_DECLS (list); friends;
friends = TREE_CHAIN (friends))
{
tree fn = TREE_VALUE (friends);
if (CP_DECL_CONTEXT (fn) != context)
continue;
if (TREE_CODE (fn) == FUNCTION_DECL && DECL_USE_TEMPLATE (fn))
continue;
if (add_function (k, fn))
return true;
}
if (CLASSTYPE_TEMPLATE_INFO (type)
&& PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
{
list = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
for (i = 0; i < TREE_VEC_LENGTH (list); ++i)
arg_assoc_template_arg (k, TREE_VEC_ELT (list, i));
}
return false;
}
static bool
arg_assoc_type (struct arg_lookup *k, tree type)
{
if (!type)
return false;
if (TYPE_PTRMEM_P (type))
{
if (arg_assoc_type (k, TYPE_PTRMEM_CLASS_TYPE (type)))
return true;
return arg_assoc_type (k, TYPE_PTRMEM_POINTED_TO_TYPE (type));
}
else switch (TREE_CODE (type))
{
case ERROR_MARK:
return false;
case VOID_TYPE:
case INTEGER_TYPE:
case REAL_TYPE:
case COMPLEX_TYPE:
case VECTOR_TYPE:
case CHAR_TYPE:
case BOOLEAN_TYPE:
return false;
case RECORD_TYPE:
if (TYPE_PTRMEMFUNC_P (type))
return arg_assoc_type (k, TYPE_PTRMEMFUNC_FN_TYPE (type));
return arg_assoc_class (k, type);
case POINTER_TYPE:
case REFERENCE_TYPE:
case ARRAY_TYPE:
return arg_assoc_type (k, TREE_TYPE (type));
case UNION_TYPE:
case ENUMERAL_TYPE:
return arg_assoc_namespace (k, decl_namespace_context (type));
case METHOD_TYPE:
case FUNCTION_TYPE:
if (arg_assoc_args (k, TYPE_ARG_TYPES (type)))
return true;
return arg_assoc_type (k, TREE_TYPE (type));
case TEMPLATE_TYPE_PARM:
case BOUND_TEMPLATE_TEMPLATE_PARM:
return false;
case TYPENAME_TYPE:
return false;
case LANG_TYPE:
gcc_assert (type == unknown_type_node);
return false;
default:
gcc_unreachable ();
}
return false;
}
static bool
arg_assoc_args (struct arg_lookup *k, tree args)
{
for (; args; args = TREE_CHAIN (args))
if (arg_assoc (k, TREE_VALUE (args)))
return true;
return false;
}
static bool
arg_assoc (struct arg_lookup *k, tree n)
{
if (n == error_mark_node)
return false;
if (TYPE_P (n))
return arg_assoc_type (k, n);
if (! type_unknown_p (n))
return arg_assoc_type (k, TREE_TYPE (n));
if (TREE_CODE (n) == ADDR_EXPR)
n = TREE_OPERAND (n, 0);
if (TREE_CODE (n) == COMPONENT_REF)
n = TREE_OPERAND (n, 1);
if (TREE_CODE (n) == OFFSET_REF)
n = TREE_OPERAND (n, 1);
while (TREE_CODE (n) == TREE_LIST)
n = TREE_VALUE (n);
if (TREE_CODE (n) == BASELINK)
n = BASELINK_FUNCTIONS (n);
if (TREE_CODE (n) == FUNCTION_DECL)
return arg_assoc_type (k, TREE_TYPE (n));
if (TREE_CODE (n) == TEMPLATE_ID_EXPR)
{
tree template = TREE_OPERAND (n, 0);
tree args = TREE_OPERAND (n, 1);
tree ctx;
int ix;
if (TREE_CODE (template) == COMPONENT_REF)
template = TREE_OPERAND (template, 1);
template = OVL_CURRENT (template);
ctx = CP_DECL_CONTEXT (template);
if (TREE_CODE (ctx) == NAMESPACE_DECL)
{
if (arg_assoc_namespace (k, ctx) == 1)
return true;
}
else if (arg_assoc_class (k, ctx) == 1)
return true;
for (ix = TREE_VEC_LENGTH (args); ix--;)
if (arg_assoc_template_arg (k, TREE_VEC_ELT (args, ix)) == 1)
return true;
}
else if (TREE_CODE (n) == OVERLOAD)
{
for (; n; n = OVL_CHAIN (n))
if (arg_assoc_type (k, TREE_TYPE (OVL_FUNCTION (n))))
return true;
}
return false;
}
tree
lookup_arg_dependent (tree name, tree fns, tree args)
{
struct arg_lookup k;
tree fn = NULL_TREE;
timevar_push (TV_NAME_LOOKUP);
k.name = name;
k.functions = fns;
k.classes = NULL_TREE;
if (fns)
fn = OVL_CURRENT (fns);
if (fn && TREE_CODE (fn) == FUNCTION_DECL
&& (CP_DECL_CONTEXT (fn) != current_decl_namespace ()
|| DECL_LOCAL_FUNCTION_P (fn)))
k.namespaces = NULL_TREE;
else
k.namespaces = build_tree_list (current_decl_namespace (), NULL_TREE);
arg_assoc_args (&k, args);
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, k.functions);
}
static tree
push_using_directive (tree used)
{
tree ud = current_binding_level->using_directives;
tree iter, ancestor;
timevar_push (TV_NAME_LOOKUP);
if (purpose_member (used, ud) != NULL_TREE)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
ancestor = namespace_ancestor (current_decl_namespace (), used);
ud = current_binding_level->using_directives;
ud = tree_cons (used, ancestor, ud);
current_binding_level->using_directives = ud;
for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
push_using_directive (TREE_PURPOSE (iter));
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
}
static tree
maybe_process_template_type_declaration (tree type, int globalize,
cxx_scope *b)
{
tree decl = TYPE_NAME (type);
if (processing_template_parmlist)
;
else
{
gcc_assert (IS_AGGR_TYPE (type) || TREE_CODE (type) == ENUMERAL_TYPE);
if (processing_template_decl)
{
tree name = DECL_NAME (decl);
decl = push_template_decl_real (decl, globalize);
if (TREE_CODE (type) != ENUMERAL_TYPE
&& !globalize && b->kind == sk_template_parms
&& b->level_chain->kind == sk_class)
{
finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
if (!COMPLETE_TYPE_P (current_class_type))
{
maybe_add_class_template_decl_list (current_class_type,
type, 0);
if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
CLASSTYPE_NESTED_UTDS (current_class_type) =
binding_table_new (SCOPE_DEFAULT_HT_SIZE);
binding_table_insert
(CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
}
}
}
}
return decl;
}
tree
pushtag (tree name, tree type, int globalize)
{
struct cp_binding_level *b;
timevar_push (TV_NAME_LOOKUP);
b = current_binding_level;
while (
b->kind == sk_cleanup
|| (b->kind == sk_template_parms && b->explicit_spec_p)
|| (b->kind == sk_class
&& (globalize
|| COMPLETE_TYPE_P (b->this_entity))))
b = b->level_chain;
if (name)
{
if (IDENTIFIER_TYPE_VALUE (name) != type)
{
tree d = NULL_TREE;
int in_class = 0;
tree context = TYPE_CONTEXT (type);
if (! context)
{
tree cs = current_scope ();
if (! globalize)
context = cs;
else if (cs != NULL_TREE && TYPE_P (cs))
context = decl_function_context (get_type_decl (cs));
}
if (!context)
context = current_namespace;
if (b->kind == sk_class
|| (b->kind == sk_template_parms
&& b->level_chain->kind == sk_class))
in_class = 1;
if (current_lang_name == lang_name_java)
TYPE_FOR_JAVA (type) = 1;
d = create_implicit_typedef (name, type);
DECL_CONTEXT (d) = FROB_CONTEXT (context);
if (! in_class)
set_identifier_type_value_with_scope (name, d, b);
d = maybe_process_template_type_declaration (type,
globalize, b);
if (d == error_mark_node)
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
if (b->kind == sk_class)
{
if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
finish_member_declaration (d);
else
pushdecl_class_level (d);
}
else
d = pushdecl_with_scope (d, b);
TYPE_CONTEXT (type) = DECL_CONTEXT (d);
if (TYPE_CONTEXT (type)
&& TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL)
VARRAY_PUSH_TREE (local_classes, type);
}
if (b->kind == sk_class
&& !COMPLETE_TYPE_P (current_class_type))
{
maybe_add_class_template_decl_list (current_class_type,
type, 0);
if (CLASSTYPE_NESTED_UTDS (current_class_type) == NULL)
CLASSTYPE_NESTED_UTDS (current_class_type)
= binding_table_new (SCOPE_DEFAULT_HT_SIZE);
binding_table_insert
(CLASSTYPE_NESTED_UTDS (current_class_type), name, type);
}
}
if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
TYPE_STUB_DECL (type) = TYPE_NAME (type);
else
{
tree d = build_decl (TYPE_DECL, NULL_TREE, type);
TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
}
POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
}
struct saved_scope *scope_chain;
static void
store_binding (tree id, VEC(cxx_saved_binding) **old_bindings)
{
cxx_saved_binding *saved;
if (!id || !IDENTIFIER_BINDING (id))
return;
if (IDENTIFIER_MARKED (id))
return;
IDENTIFIER_MARKED (id) = 1;
saved = VEC_safe_push (cxx_saved_binding, *old_bindings, NULL);
saved->identifier = id;
saved->binding = IDENTIFIER_BINDING (id);
saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
IDENTIFIER_BINDING (id) = NULL;
}
static void
store_bindings (tree names, VEC(cxx_saved_binding) **old_bindings)
{
tree t;
timevar_push (TV_NAME_LOOKUP);
for (t = names; t; t = TREE_CHAIN (t))
{
tree id;
if (TREE_CODE (t) == TREE_LIST)
id = TREE_PURPOSE (t);
else
id = DECL_NAME (t);
store_binding (id, old_bindings);
}
timevar_pop (TV_NAME_LOOKUP);
}
static void
store_class_bindings (VEC(cp_class_binding) *names,
VEC(cxx_saved_binding) **old_bindings)
{
size_t i;
cp_class_binding *cb;
timevar_push (TV_NAME_LOOKUP);
for (i = 0; VEC_iterate(cp_class_binding, names, i, cb); ++i)
store_binding (cb->identifier, old_bindings);
timevar_pop (TV_NAME_LOOKUP);
}
void
push_to_top_level (void)
{
struct saved_scope *s;
struct cp_binding_level *b;
cxx_saved_binding *sb;
size_t i;
int need_pop;
timevar_push (TV_NAME_LOOKUP);
s = GGC_CNEW (struct saved_scope);
b = scope_chain ? current_binding_level : 0;
if (cfun)
{
need_pop = 1;
push_function_context_to (NULL_TREE);
}
else
need_pop = 0;
if (scope_chain && previous_class_level)
store_class_bindings (previous_class_level->class_shadowed,
&s->old_bindings);
for (; b; b = b->level_chain)
{
tree t;
if (global_scope_p (b))
break;
store_bindings (b->names, &s->old_bindings);
if (b->kind == sk_class)
store_class_bindings (b->class_shadowed, &s->old_bindings);
for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
}
for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, sb); ++i)
IDENTIFIER_MARKED (sb->identifier) = 0;
s->prev = scope_chain;
s->bindings = b;
s->need_pop_function_context = need_pop;
s->function_decl = current_function_decl;
scope_chain = s;
current_function_decl = NULL_TREE;
VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
current_lang_name = lang_name_cplusplus;
current_namespace = global_namespace;
timevar_pop (TV_NAME_LOOKUP);
}
void
pop_from_top_level (void)
{
struct saved_scope *s = scope_chain;
cxx_saved_binding *saved;
size_t i;
timevar_push (TV_NAME_LOOKUP);
if (previous_class_level)
invalidate_class_lookup_cache ();
current_lang_base = 0;
scope_chain = s->prev;
for (i = 0; VEC_iterate (cxx_saved_binding, s->old_bindings, i, saved); ++i)
{
tree id = saved->identifier;
IDENTIFIER_BINDING (id) = saved->binding;
SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
}
if (s->need_pop_function_context)
pop_function_context_from (NULL_TREE);
current_function_decl = s->function_decl;
timevar_pop (TV_NAME_LOOKUP);
}
void
pop_everything (void)
{
if (ENABLE_SCOPE_CHECKING)
verbatim ("XXX entering pop_everything ()\n");
while (!toplevel_bindings_p ())
{
if (current_binding_level->kind == sk_class)
pop_nested_class ();
else
poplevel (0, 0, 0);
}
if (ENABLE_SCOPE_CHECKING)
verbatim ("XXX leaving pop_everything ()\n");
}
static void
cp_emit_debug_info_for_using (tree t, tree context)
{
if (TREE_CODE (t) == FUNCTION_DECL
&& DECL_EXTERNAL (t)
&& DECL_BUILT_IN (t))
return;
if (context == global_namespace)
context = NULL_TREE;
if (BASELINK_P (t))
t = BASELINK_FUNCTIONS (t);
for (t = OVL_CURRENT (t); t; t = OVL_NEXT (t))
if (TREE_CODE (t) != TEMPLATE_DECL)
(*debug_hooks->imported_module_or_decl) (t, context);
}
#include "gt-cp-name-lookup.h"