blob: 95937fe53aae674f5b1c0f32ae14a1e49c97f80e [file] [log] [blame]
/* Process declarations and variables for C++ compiler.
Copyright (C) 1988-2013 Free Software Foundation, Inc.
Contributed by Michael Tiemann (tiemann@cygnus.com)
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
/* Process declarations and symbol lookup for C++ front end.
Also constructs types; the standard scalar types at initialization,
and structure, union, array and enum types when they are declared. */
/* ??? not all decl nodes are given the most useful possible
line numbers. For example, the CONST_DECLs for enum values. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "tree.h"
#include "flags.h"
#include "cp-tree.h"
#include "tree-iterator.h"
#include "tree-inline.h"
#include "decl.h"
#include "intl.h"
#include "toplev.h"
#include "hashtab.h"
#include "tm_p.h"
#include "target.h"
#include "c-family/c-common.h"
#include "c-family/c-objc.h"
#include "c-family/c-pragma.h"
#include "diagnostic.h"
#include "intl.h"
#include "debug.h"
#include "timevar.h"
#include "pointer-set.h"
#include "splay-tree.h"
#include "plugin.h"
#include "cgraph.h"
/* Possible cases of bad specifiers type used by bad_specifiers. */
enum bad_spec_place {
BSP_VAR, /* variable */
BSP_PARM, /* parameter */
BSP_TYPE, /* type */
BSP_FIELD /* field */
};
static tree grokparms (tree parmlist, tree *);
static const char *redeclaration_error_message (tree, tree);
static int decl_jump_unsafe (tree);
static void require_complete_types_for_parms (tree);
static int ambi_op_p (enum tree_code);
static int unary_op_p (enum tree_code);
static void push_local_name (tree);
static tree grok_reference_init (tree, tree, tree, int);
static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
int, int, tree);
static int check_static_variable_definition (tree, tree);
static void record_unknown_type (tree, const char *);
static tree builtin_function_1 (tree, tree, bool);
static tree build_library_fn_1 (tree, enum tree_code, tree);
static int member_function_or_else (tree, tree, enum overload_flags);
static void bad_specifiers (tree, enum bad_spec_place, int, int, int, int,
int);
static void check_for_uninitialized_const_var (tree);
static hashval_t typename_hash (const void *);
static int typename_compare (const void *, const void *);
static tree local_variable_p_walkfn (tree *, int *, void *);
static tree record_builtin_java_type (const char *, int);
static const char *tag_name (enum tag_types);
static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
static void maybe_deduce_size_from_array_init (tree, tree);
static void layout_var_decl (tree);
static tree check_initializer (tree, tree, int, vec<tree, va_gc> **);
static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
static void save_function_data (tree);
static void copy_type_enum (tree , tree);
static void check_function_type (tree, tree);
static void finish_constructor_body (void);
static void begin_destructor_body (void);
static void finish_destructor_body (void);
static void record_key_method_defined (tree);
static tree create_array_type_for_decl (tree, tree, tree);
static tree get_atexit_node (void);
static tree get_dso_handle_node (void);
static tree start_cleanup_fn (void);
static void end_cleanup_fn (void);
static tree cp_make_fname_decl (location_t, tree, int);
static void initialize_predefined_identifiers (void);
static tree check_special_function_return_type
(special_function_kind, tree, tree);
static tree push_cp_library_fn (enum tree_code, tree);
static tree build_cp_library_fn (tree, enum tree_code, tree);
static void store_parm_decls (tree);
static void initialize_local_var (tree, tree);
static void expand_static_init (tree, tree);
/* The following symbols are subsumed in the cp_global_trees array, and
listed here individually for documentation purposes.
C++ extensions
tree wchar_decl_node;
tree vtable_entry_type;
tree delta_type_node;
tree __t_desc_type_node;
tree class_type_node;
tree unknown_type_node;
Array type `vtable_entry_type[]'
tree vtbl_type_node;
tree vtbl_ptr_type_node;
Namespaces,
tree std_node;
tree abi_node;
A FUNCTION_DECL which can call `abort'. Not necessarily the
one that the user will declare, but sufficient to be called
by routines that want to abort the program.
tree abort_fndecl;
The FUNCTION_DECL for the default `::operator delete'.
tree global_delete_fndecl;
Used by RTTI
tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
tree tinfo_var_id; */
tree cp_global_trees[CPTI_MAX];
/* Indicates that there is a type value in some namespace, although
that is not necessarily in scope at the moment. */
tree global_type_node;
/* The node that holds the "name" of the global scope. */
tree global_scope_name;
#define local_names cp_function_chain->x_local_names
/* A list of objects which have constructors or destructors
which reside in the global scope. The decl is stored in
the TREE_VALUE slot and the initializer is stored
in the TREE_PURPOSE slot. */
tree static_aggregates;
/* Like static_aggregates, but for thread_local variables. */
tree tls_aggregates;
/* -- end of C++ */
/* A node for the integer constant 2. */
tree integer_two_node;
/* Used only for jumps to as-yet undefined labels, since jumps to
defined labels can have their validity checked immediately. */
struct GTY((chain_next ("%h.next"))) named_label_use_entry {
struct named_label_use_entry *next;
/* The binding level to which this entry is *currently* attached.
This is initially the binding level in which the goto appeared,
but is modified as scopes are closed. */
cp_binding_level *binding_level;
/* The head of the names list that was current when the goto appeared,
or the inner scope popped. These are the decls that will *not* be
skipped when jumping to the label. */
tree names_in_scope;
/* The location of the goto, for error reporting. */
location_t o_goto_locus;
/* True if an OpenMP structured block scope has been closed since
the goto appeared. This means that the branch from the label will
illegally exit an OpenMP scope. */
bool in_omp_scope;
};
/* A list of all LABEL_DECLs in the function that have names. Here so
we can clear out their names' definitions at the end of the
function, and so we can check the validity of jumps to these labels. */
struct GTY(()) named_label_entry {
/* The decl itself. */
tree label_decl;
/* The binding level to which the label is *currently* attached.
This is initially set to the binding level in which the label
is defined, but is modified as scopes are closed. */
cp_binding_level *binding_level;
/* The head of the names list that was current when the label was
defined, or the inner scope popped. These are the decls that will
be skipped when jumping to the label. */
tree names_in_scope;
/* A vector of all decls from all binding levels that would be
crossed by a backward branch to the label. */
vec<tree, va_gc> *bad_decls;
/* A list of uses of the label, before the label is defined. */
struct named_label_use_entry *uses;
/* The following bits are set after the label is defined, and are
updated as scopes are popped. They indicate that a backward jump
to the label will illegally enter a scope of the given flavor. */
bool in_try_scope;
bool in_catch_scope;
bool in_omp_scope;
};
#define named_labels cp_function_chain->x_named_labels
/* The number of function bodies which we are currently processing.
(Zero if we are at namespace scope, one inside the body of a
function, two inside the body of a function in a local class, etc.) */
int function_depth;
/* To avoid unwanted recursion, finish_function defers all mark_used calls
encountered during its execution until it finishes. */
bool defer_mark_used_calls;
vec<tree, va_gc> *deferred_mark_used_calls;
/* States indicating how grokdeclarator() should handle declspecs marked
with __attribute__((deprecated)). An object declared as
__attribute__((deprecated)) suppresses warnings of uses of other
deprecated items. */
enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
/* A list of VAR_DECLs whose type was incomplete at the time the
variable was declared. */
typedef struct GTY(()) incomplete_var_d {
tree decl;
tree incomplete_type;
} incomplete_var;
static GTY(()) vec<incomplete_var, va_gc> *incomplete_vars;
/* Returns the kind of template specialization we are currently
processing, given that it's declaration contained N_CLASS_SCOPES
explicit scope qualifications. */
tmpl_spec_kind
current_tmpl_spec_kind (int n_class_scopes)
{
int n_template_parm_scopes = 0;
int seen_specialization_p = 0;
int innermost_specialization_p = 0;
cp_binding_level *b;
/* Scan through the template parameter scopes. */
for (b = current_binding_level;
b->kind == sk_template_parms;
b = b->level_chain)
{
/* If we see a specialization scope inside a parameter scope,
then something is wrong. That corresponds to a declaration
like:
template <class T> template <> ...
which is always invalid since [temp.expl.spec] forbids the
specialization of a class member template if the enclosing
class templates are not explicitly specialized as well. */
if (b->explicit_spec_p)
{
if (n_template_parm_scopes == 0)
innermost_specialization_p = 1;
else
seen_specialization_p = 1;
}
else if (seen_specialization_p == 1)
return tsk_invalid_member_spec;
++n_template_parm_scopes;
}
/* Handle explicit instantiations. */
if (processing_explicit_instantiation)
{
if (n_template_parm_scopes != 0)
/* We've seen a template parameter list during an explicit
instantiation. For example:
template <class T> template void f(int);
This is erroneous. */
return tsk_invalid_expl_inst;
else
return tsk_expl_inst;
}
if (n_template_parm_scopes < n_class_scopes)
/* We've not seen enough template headers to match all the
specialized classes present. For example:
template <class T> void R<T>::S<T>::f(int);
This is invalid; there needs to be one set of template
parameters for each class. */
return tsk_insufficient_parms;
else if (n_template_parm_scopes == n_class_scopes)
/* We're processing a non-template declaration (even though it may
be a member of a template class.) For example:
template <class T> void S<T>::f(int);
The `class T' matches the `S<T>', leaving no template headers
corresponding to the `f'. */
return tsk_none;
else if (n_template_parm_scopes > n_class_scopes + 1)
/* We've got too many template headers. For example:
template <> template <class T> void f (T);
There need to be more enclosing classes. */
return tsk_excessive_parms;
else
/* This must be a template. It's of the form:
template <class T> template <class U> void S<T>::f(U);
This is a specialization if the innermost level was a
specialization; otherwise it's just a definition of the
template. */
return innermost_specialization_p ? tsk_expl_spec : tsk_template;
}
/* Exit the current scope. */
void
finish_scope (void)
{
poplevel (0, 0, 0);
}
/* When a label goes out of scope, check to see if that label was used
in a valid manner, and issue any appropriate warnings or errors. */
static void
pop_label (tree label, tree old_value)
{
if (!processing_template_decl)
{
if (DECL_INITIAL (label) == NULL_TREE)
{
location_t location;
error ("label %q+D used but not defined", label);
location = input_location; /* FIXME want (input_filename, (line)0) */
/* Avoid crashing later. */
define_label (location, DECL_NAME (label));
}
else
warn_for_unused_label (label);
}
SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
}
/* At the end of a function, all labels declared within the function
go out of scope. BLOCK is the top-level block for the
function. */
static int
pop_labels_1 (void **slot, void *data)
{
struct named_label_entry *ent = (struct named_label_entry *) *slot;
tree block = (tree) data;
pop_label (ent->label_decl, NULL_TREE);
/* Put the labels into the "variables" of the top-level block,
so debugger can see them. */
DECL_CHAIN (ent->label_decl) = BLOCK_VARS (block);
BLOCK_VARS (block) = ent->label_decl;
htab_clear_slot (named_labels, slot);
return 1;
}
static void
pop_labels (tree block)
{
if (named_labels)
{
htab_traverse (named_labels, pop_labels_1, block);
named_labels = NULL;
}
}
/* At the end of a block with local labels, restore the outer definition. */
static void
pop_local_label (tree label, tree old_value)
{
struct named_label_entry dummy;
void **slot;
pop_label (label, old_value);
dummy.label_decl = label;
slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
htab_clear_slot (named_labels, slot);
}
/* The following two routines are used to interface to Objective-C++.
The binding level is purposely treated as an opaque type. */
void *
objc_get_current_scope (void)
{
return current_binding_level;
}
/* The following routine is used by the NeXT-style SJLJ exceptions;
variables get marked 'volatile' so as to not be clobbered by
_setjmp()/_longjmp() calls. All variables in the current scope,
as well as parent scopes up to (but not including) ENCLOSING_BLK
shall be thusly marked. */
void
objc_mark_locals_volatile (void *enclosing_blk)
{
cp_binding_level *scope;
for (scope = current_binding_level;
scope && scope != enclosing_blk;
scope = scope->level_chain)
{
tree decl;
for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
objc_volatilize_decl (decl);
/* Do not climb up past the current function. */
if (scope->kind == sk_function_parms)
break;
}
}
/* Update data for defined and undefined labels when leaving a scope. */
static int
poplevel_named_label_1 (void **slot, void *data)
{
struct named_label_entry *ent = (struct named_label_entry *) *slot;
cp_binding_level *bl = (cp_binding_level *) data;
cp_binding_level *obl = bl->level_chain;
if (ent->binding_level == bl)
{
tree decl;
/* ENT->NAMES_IN_SCOPE may contain a mixture of DECLs and
TREE_LISTs representing OVERLOADs, so be careful. */
for (decl = ent->names_in_scope; decl; decl = (DECL_P (decl)
? DECL_CHAIN (decl)
: TREE_CHAIN (decl)))
if (decl_jump_unsafe (decl))
vec_safe_push (ent->bad_decls, decl);
ent->binding_level = obl;
ent->names_in_scope = obl->names;
switch (bl->kind)
{
case sk_try:
ent->in_try_scope = true;
break;
case sk_catch:
ent->in_catch_scope = true;
break;
case sk_omp:
ent->in_omp_scope = true;
break;
default:
break;
}
}
else if (ent->uses)
{
struct named_label_use_entry *use;
for (use = ent->uses; use ; use = use->next)
if (use->binding_level == bl)
{
use->binding_level = obl;
use->names_in_scope = obl->names;
if (bl->kind == sk_omp)
use->in_omp_scope = true;
}
}
return 1;
}
/* Saved errorcount to avoid -Wunused-but-set-{parameter,variable} warnings
when errors were reported, except for -Werror-unused-but-set-*. */
static int unused_but_set_errorcount;
/* Exit a binding level.
Pop the level off, and restore the state of the identifier-decl mappings
that were in effect when this level was entered.
If KEEP == 1, this level had explicit declarations, so
and create a "block" (a BLOCK node) for the level
to record its declarations and subblocks for symbol table output.
If FUNCTIONBODY is nonzero, this level is the body of a function,
so create a block as if KEEP were set and also clear out all
label names.
If REVERSE is nonzero, reverse the order of decls before putting
them into the BLOCK. */
tree
poplevel (int keep, int reverse, int functionbody)
{
tree link;
/* The chain of decls was accumulated in reverse order.
Put it into forward order, just for cleanliness. */
tree decls;
tree subblocks;
tree block;
tree decl;
int leaving_for_scope;
scope_kind kind;
unsigned ix;
cp_label_binding *label_bind;
bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
restart:
block = NULL_TREE;
gcc_assert (current_binding_level->kind != sk_class);
if (current_binding_level->kind == sk_cleanup)
functionbody = 0;
subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
gcc_assert (!vec_safe_length (current_binding_level->class_shadowed));
/* We used to use KEEP == 2 to indicate that the new block should go
at the beginning of the list of blocks at this binding level,
rather than the end. This hack is no longer used. */
gcc_assert (keep == 0 || keep == 1);
if (current_binding_level->keep)
keep = 1;
/* Any uses of undefined labels, and any defined labels, now operate
under constraints of next binding contour. */
if (cfun && !functionbody && named_labels)
htab_traverse (named_labels, poplevel_named_label_1,
current_binding_level);
/* Get the decls in the order they were written.
Usually current_binding_level->names is in reverse order.
But parameter decls were previously put in forward order. */
if (reverse)
current_binding_level->names
= decls = nreverse (current_binding_level->names);
else
decls = current_binding_level->names;
/* If there were any declarations or structure tags in that level,
or if this level is a function body,
create a BLOCK to record them for the life of this function. */
block = NULL_TREE;
if (keep == 1 || functionbody)
block = make_node (BLOCK);
if (block != NULL_TREE)
{
BLOCK_VARS (block) = decls;
BLOCK_SUBBLOCKS (block) = subblocks;
}
/* In each subblock, record that this is its superior. */
if (keep >= 0)
for (link = subblocks; link; link = BLOCK_CHAIN (link))
BLOCK_SUPERCONTEXT (link) = block;
/* We still support the old for-scope rules, whereby the variables
in a for-init statement were in scope after the for-statement
ended. We only use the new rules if flag_new_for_scope is
nonzero. */
leaving_for_scope
= current_binding_level->kind == sk_for && flag_new_for_scope == 1;
/* Before we remove the declarations first check for unused variables. */
if ((warn_unused_variable || warn_unused_but_set_variable)
&& !processing_template_decl)
for (tree d = getdecls (); d; d = TREE_CHAIN (d))
{
/* There are cases where D itself is a TREE_LIST. See in
push_local_binding where the list of decls returned by
getdecls is built. */
decl = TREE_CODE (d) == TREE_LIST ? TREE_VALUE (d) : d;
if (TREE_CODE (decl) == VAR_DECL
&& (! TREE_USED (decl) || !DECL_READ_P (decl))
&& ! DECL_IN_SYSTEM_HEADER (decl)
&& DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl)
&& TREE_TYPE (decl) != error_mark_node
&& (!CLASS_TYPE_P (TREE_TYPE (decl))
|| !TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl))))
{
if (! TREE_USED (decl))
warning (OPT_Wunused_variable, "unused variable %q+D", decl);
else if (DECL_CONTEXT (decl) == current_function_decl
&& TREE_CODE (TREE_TYPE (decl)) != REFERENCE_TYPE
&& errorcount == unused_but_set_errorcount)
{
warning (OPT_Wunused_but_set_variable,
"variable %q+D set but not used", decl);
unused_but_set_errorcount = errorcount;
}
}
}
/* Remove declarations for all the DECLs in this level. */
for (link = decls; link; link = TREE_CHAIN (link))
{
if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
/* It's hard to make this ARM compatibility hack play nicely with
lambdas, and it really isn't necessary in C++11 mode. */
&& cxx_dialect < cxx0x
&& DECL_NAME (link))
{
tree name = DECL_NAME (link);
cxx_binding *ob;
tree ns_binding;
ob = outer_binding (name,
IDENTIFIER_BINDING (name),
/*class_p=*/true);
if (!ob)
ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
else
ns_binding = NULL_TREE;
if (ob && ob->scope == current_binding_level->level_chain)
/* We have something like:
int i;
for (int i; ;);
and we are leaving the `for' scope. There's no reason to
keep the binding of the inner `i' in this case. */
pop_binding (name, link);
else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
|| (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
/* Here, we have something like:
typedef int I;
void f () {
for (int I; ;);
}
We must pop the for-scope binding so we know what's a
type and what isn't. */
pop_binding (name, link);
else
{
/* Mark this VAR_DECL as dead so that we can tell we left it
there only for backward compatibility. */
DECL_DEAD_FOR_LOCAL (link) = 1;
/* Keep track of what should have happened when we
popped the binding. */
if (ob && ob->value)
{
SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
}
/* Add it to the list of dead variables in the next
outermost binding to that we can remove these when we
leave that binding. */
vec_safe_push (
current_binding_level->level_chain->dead_vars_from_for,
link);
/* Although we don't pop the cxx_binding, we do clear
its SCOPE since the scope is going away now. */
IDENTIFIER_BINDING (name)->scope
= current_binding_level->level_chain;
}
}
else
{
tree name;
/* Remove the binding. */
decl = link;
if (TREE_CODE (decl) == TREE_LIST)
decl = TREE_VALUE (decl);
name = decl;
if (TREE_CODE (name) == OVERLOAD)
name = OVL_FUNCTION (name);
gcc_assert (DECL_P (name));
pop_binding (DECL_NAME (name), decl);
}
}
/* Remove declarations for any `for' variables from inner scopes
that we kept around. */
FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->dead_vars_from_for,
ix, decl)
pop_binding (DECL_NAME (decl), decl);
/* Restore the IDENTIFIER_TYPE_VALUEs. */
for (link = current_binding_level->type_shadowed;
link; link = TREE_CHAIN (link))
SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
/* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
FOR_EACH_VEC_SAFE_ELT_REVERSE (current_binding_level->shadowed_labels,
ix, label_bind)
pop_local_label (label_bind->label, label_bind->prev_value);
/* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
list if a `using' declaration put them there. The debugging
back ends won't understand OVERLOAD, so we remove them here.
Because the BLOCK_VARS are (temporarily) shared with
CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
popped all the bindings. */
if (block)
{
tree* d;
for (d = &BLOCK_VARS (block); *d; )
{
if (TREE_CODE (*d) == TREE_LIST)
*d = TREE_CHAIN (*d);
else
d = &DECL_CHAIN (*d);
}
}
/* If the level being exited is the top level of a function,
check over all the labels. */
if (functionbody)
{
/* Since this is the top level block of a function, the vars are
the function's parameters. Don't leave them in the BLOCK
because they are found in the FUNCTION_DECL instead. */
BLOCK_VARS (block) = 0;
pop_labels (block);
}
kind = current_binding_level->kind;
if (kind == sk_cleanup)
{
tree stmt;
/* If this is a temporary binding created for a cleanup, then we'll
have pushed a statement list level. Pop that, create a new
BIND_EXPR for the block, and insert it into the stream. */
stmt = pop_stmt_list (current_binding_level->statement_list);
stmt = c_build_bind_expr (input_location, block, stmt);
add_stmt (stmt);
}
leave_scope ();
if (functionbody)
{
/* The current function is being defined, so its DECL_INITIAL
should be error_mark_node. */
gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
DECL_INITIAL (current_function_decl) = block;
}
else if (block)
current_binding_level->blocks
= block_chainon (current_binding_level->blocks, block);
/* If we did not make a block for the level just exited,
any blocks made for inner levels
(since they cannot be recorded as subblocks in that level)
must be carried forward so they will later become subblocks
of something else. */
else if (subblocks)
current_binding_level->blocks
= block_chainon (current_binding_level->blocks, subblocks);
/* Each and every BLOCK node created here in `poplevel' is important
(e.g. for proper debugging information) so if we created one
earlier, mark it as "used". */
if (block)
TREE_USED (block) = 1;
/* All temporary bindings created for cleanups are popped silently. */
if (kind == sk_cleanup)
goto restart;
timevar_cond_stop (TV_NAME_LOOKUP, subtime);
return block;
}
/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
itself, calling F for each. The DATA is passed to F as well. */
static int
walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
{
int result = 0;
tree current = NAMESPACE_LEVEL (name_space)->namespaces;
result |= (*f) (name_space, data);
for (; current; current = DECL_CHAIN (current))
result |= walk_namespaces_r (current, f, data);
return result;
}
/* Walk all the namespaces, calling F for each. The DATA is passed to
F as well. */
int
walk_namespaces (walk_namespaces_fn f, void* data)
{
return walk_namespaces_r (global_namespace, f, data);
}
/* Call wrapup_globals_declarations for the globals in NAMESPACE. If
DATA is non-NULL, this is the last time we will call
wrapup_global_declarations for this NAMESPACE. */
int
wrapup_globals_for_namespace (tree name_space, void* data)
{
cp_binding_level *level = NAMESPACE_LEVEL (name_space);
vec<tree, va_gc> *statics = level->static_decls;
tree *vec = statics->address ();
int len = statics->length ();
int last_time = (data != 0);
if (last_time)
{
check_global_declarations (vec, len);
emit_debug_global_declarations (vec, len);
return 0;
}
/* Write out any globals that need to be output. */
return wrapup_global_declarations (vec, len);
}
/* In C++, you don't have to write `struct S' to refer to `S'; you
can just use `S'. We accomplish this by creating a TYPE_DECL as
if the user had written `typedef struct S S'. Create and return
the TYPE_DECL for TYPE. */
tree
create_implicit_typedef (tree name, tree type)
{
tree decl;
decl = build_decl (input_location, TYPE_DECL, name, type);
DECL_ARTIFICIAL (decl) = 1;
/* There are other implicit type declarations, like the one *within*
a class that allows you to write `S::S'. We must distinguish
amongst these. */
SET_DECL_IMPLICIT_TYPEDEF_P (decl);
TYPE_NAME (type) = decl;
TYPE_STUB_DECL (type) = decl;
return decl;
}
/* Remember a local name for name-mangling purposes. */
static void
push_local_name (tree decl)
{
size_t i, nelts;
tree t, name;
timevar_start (TV_NAME_LOOKUP);
name = DECL_NAME (decl);
nelts = vec_safe_length (local_names);
for (i = 0; i < nelts; i++)
{
t = (*local_names)[i];
if (DECL_NAME (t) == name)
{
if (!DECL_LANG_SPECIFIC (decl))
retrofit_lang_decl (decl);
DECL_LANG_SPECIFIC (decl)->u.base.u2sel = 1;
if (DECL_DISCRIMINATOR_SET_P (t))
DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
else
DECL_DISCRIMINATOR (decl) = 1;
(*local_names)[i] = decl;
timevar_stop (TV_NAME_LOOKUP);
return;
}
}
vec_safe_push (local_names, decl);
timevar_stop (TV_NAME_LOOKUP);
}
/* Subroutine of duplicate_decls: return truthvalue of whether
or not types of these decls match.
For C++, we must compare the parameter list so that `int' can match
`int&' in a parameter position, but `int&' is not confused with
`const int&'. */
int
decls_match (tree newdecl, tree olddecl)
{
int types_match;
if (newdecl == olddecl)
return 1;
if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
/* If the two DECLs are not even the same kind of thing, we're not
interested in their types. */
return 0;
gcc_assert (DECL_P (newdecl));
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
tree f1 = TREE_TYPE (newdecl);
tree f2 = TREE_TYPE (olddecl);
tree p1 = TYPE_ARG_TYPES (f1);
tree p2 = TYPE_ARG_TYPES (f2);
tree r2;
/* Specializations of different templates are different functions
even if they have the same type. */
tree t1 = (DECL_USE_TEMPLATE (newdecl)
? DECL_TI_TEMPLATE (newdecl)
: NULL_TREE);
tree t2 = (DECL_USE_TEMPLATE (olddecl)
? DECL_TI_TEMPLATE (olddecl)
: NULL_TREE);
if (t1 != t2)
return 0;
if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
&& ! (DECL_EXTERN_C_P (newdecl)
&& DECL_EXTERN_C_P (olddecl)))
return 0;
/* A new declaration doesn't match a built-in one unless it
is also extern "C". */
if (DECL_IS_BUILTIN (olddecl)
&& DECL_EXTERN_C_P (olddecl) && !DECL_EXTERN_C_P (newdecl))
return 0;
if (TREE_CODE (f1) != TREE_CODE (f2))
return 0;
/* A declaration with deduced return type should use its pre-deduction
type for declaration matching. */
if (FNDECL_USED_AUTO (olddecl))
r2 = DECL_STRUCT_FUNCTION (olddecl)->language->x_auto_return_pattern;
else
r2 = TREE_TYPE (f2);
if (same_type_p (TREE_TYPE (f1), r2))
{
if (!prototype_p (f2) && DECL_EXTERN_C_P (olddecl)
&& (DECL_BUILT_IN (olddecl)
#ifndef NO_IMPLICIT_EXTERN_C
|| (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
|| (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
#endif
))
{
types_match = self_promoting_args_p (p1);
if (p1 == void_list_node)
TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
}
#ifndef NO_IMPLICIT_EXTERN_C
else if (!prototype_p (f1)
&& (DECL_EXTERN_C_P (olddecl)
&& DECL_IN_SYSTEM_HEADER (olddecl)
&& !DECL_CLASS_SCOPE_P (olddecl))
&& (DECL_EXTERN_C_P (newdecl)
&& DECL_IN_SYSTEM_HEADER (newdecl)
&& !DECL_CLASS_SCOPE_P (newdecl)))
{
types_match = self_promoting_args_p (p2);
TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
}
#endif
else
types_match =
compparms (p1, p2)
&& (TYPE_ATTRIBUTES (TREE_TYPE (newdecl)) == NULL_TREE
|| comp_type_attributes (TREE_TYPE (newdecl),
TREE_TYPE (olddecl)) != 0);
}
else
types_match = 0;
/* The decls dont match if they correspond to two different versions
of the same function. Disallow extern "C" functions to be
versions for now. */
if (types_match
&& !DECL_EXTERN_C_P (newdecl)
&& !DECL_EXTERN_C_P (olddecl)
&& targetm.target_option.function_versions (newdecl, olddecl))
{
/* Mark functions as versions if necessary. Modify the mangled decl
name if necessary. */
if (DECL_FUNCTION_VERSIONED (newdecl)
&& DECL_FUNCTION_VERSIONED (olddecl))
return 0;
if (!DECL_FUNCTION_VERSIONED (newdecl))
{
DECL_FUNCTION_VERSIONED (newdecl) = 1;
if (DECL_ASSEMBLER_NAME_SET_P (newdecl))
mangle_decl (newdecl);
}
if (!DECL_FUNCTION_VERSIONED (olddecl))
{
DECL_FUNCTION_VERSIONED (olddecl) = 1;
if (DECL_ASSEMBLER_NAME_SET_P (olddecl))
mangle_decl (olddecl);
}
record_function_versions (olddecl, newdecl);
return 0;
}
}
else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
{
if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
!= TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
return 0;
if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
DECL_TEMPLATE_PARMS (olddecl)))
return 0;
if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
else
types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
DECL_TEMPLATE_RESULT (newdecl));
}
else
{
/* Need to check scope for variable declaration (VAR_DECL).
For typedef (TYPE_DECL), scope is ignored. */
if (TREE_CODE (newdecl) == VAR_DECL
&& CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
/* [dcl.link]
Two declarations for an object with C language linkage
with the same name (ignoring the namespace that qualify
it) that appear in different namespace scopes refer to
the same object. */
&& !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
return 0;
if (TREE_TYPE (newdecl) == error_mark_node)
types_match = TREE_TYPE (olddecl) == error_mark_node;
else if (TREE_TYPE (olddecl) == NULL_TREE)
types_match = TREE_TYPE (newdecl) == NULL_TREE;
else if (TREE_TYPE (newdecl) == NULL_TREE)
types_match = 0;
else
types_match = comptypes (TREE_TYPE (newdecl),
TREE_TYPE (olddecl),
COMPARE_REDECLARATION);
}
return types_match;
}
/* If NEWDECL is `static' and an `extern' was seen previously,
warn about it. OLDDECL is the previous declaration.
Note that this does not apply to the C++ case of declaring
a variable `extern const' and then later `const'.
Don't complain about built-in functions, since they are beyond
the user's control. */
void
warn_extern_redeclared_static (tree newdecl, tree olddecl)
{
if (TREE_CODE (newdecl) == TYPE_DECL
|| TREE_CODE (newdecl) == TEMPLATE_DECL
|| TREE_CODE (newdecl) == CONST_DECL
|| TREE_CODE (newdecl) == NAMESPACE_DECL)
return;
/* Don't get confused by static member functions; that's a different
use of `static'. */
if (TREE_CODE (newdecl) == FUNCTION_DECL
&& DECL_STATIC_FUNCTION_P (newdecl))
return;
/* If the old declaration was `static', or the new one isn't, then
everything is OK. */
if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
return;
/* It's OK to declare a builtin function as `static'. */
if (TREE_CODE (olddecl) == FUNCTION_DECL
&& DECL_ARTIFICIAL (olddecl))
return;
permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
permerror (input_location, "previous declaration of %q+D", olddecl);
}
/* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
function templates. If their exception specifications do not
match, issue a diagnostic. */
static void
check_redeclaration_exception_specification (tree new_decl,
tree old_decl)
{
tree new_type;
tree old_type;
tree new_exceptions;
tree old_exceptions;
new_type = TREE_TYPE (new_decl);
new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
old_type = TREE_TYPE (old_decl);
old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
/* [except.spec]
If any declaration of a function has an exception-specification,
all declarations, including the definition and an explicit
specialization, of that function shall have an
exception-specification with the same set of type-ids. */
if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
&& ! DECL_IS_BUILTIN (old_decl)
&& flag_exceptions
&& !comp_except_specs (new_exceptions, old_exceptions, ce_normal))
{
error ("declaration of %qF has a different exception specifier",
new_decl);
error ("from previous declaration %q+F", old_decl);
}
}
/* Return true if OLD_DECL and NEW_DECL agree on constexprness.
Otherwise issue diagnostics. */
static bool
validate_constexpr_redeclaration (tree old_decl, tree new_decl)
{
old_decl = STRIP_TEMPLATE (old_decl);
new_decl = STRIP_TEMPLATE (new_decl);
if (!VAR_OR_FUNCTION_DECL_P (old_decl)
|| !VAR_OR_FUNCTION_DECL_P (new_decl))
return true;
if (DECL_DECLARED_CONSTEXPR_P (old_decl)
== DECL_DECLARED_CONSTEXPR_P (new_decl))
return true;
if (TREE_CODE (old_decl) == FUNCTION_DECL && DECL_BUILT_IN (old_decl))
{
/* Hide a built-in declaration. */
DECL_DECLARED_CONSTEXPR_P (old_decl)
= DECL_DECLARED_CONSTEXPR_P (new_decl);
return true;
}
error ("redeclaration %qD differs in %<constexpr%>", new_decl);
error ("from previous declaration %q+D", old_decl);
return false;
}
#define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
&& lookup_attribute ("gnu_inline", \
DECL_ATTRIBUTES (fn)))
/* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
If the redeclaration is invalid, a diagnostic is issued, and the
error_mark_node is returned. Otherwise, OLDDECL is returned.
If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
returned.
NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
tree
duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
{
unsigned olddecl_uid = DECL_UID (olddecl);
int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
int new_defines_function = 0;
tree new_template_info;
if (newdecl == olddecl)
return olddecl;
types_match = decls_match (newdecl, olddecl);
/* If either the type of the new decl or the type of the old decl is an
error_mark_node, then that implies that we have already issued an
error (earlier) for some bogus type specification, and in that case,
it is rather pointless to harass the user with yet more error message
about the same declaration, so just pretend the types match here. */
if (TREE_TYPE (newdecl) == error_mark_node
|| TREE_TYPE (olddecl) == error_mark_node)
return error_mark_node;
if (UDLIT_OPER_P (DECL_NAME (newdecl))
&& UDLIT_OPER_P (DECL_NAME (olddecl)))
{
if (TREE_CODE (newdecl) == TEMPLATE_DECL
&& TREE_CODE (olddecl) != TEMPLATE_DECL
&& check_raw_literal_operator (olddecl))
error ("literal operator template %q+D conflicts with"
" raw literal operator %qD", newdecl, olddecl);
else if (TREE_CODE (newdecl) != TEMPLATE_DECL
&& TREE_CODE (olddecl) == TEMPLATE_DECL
&& check_raw_literal_operator (newdecl))
error ("raw literal operator %q+D conflicts with"
" literal operator template %qD", newdecl, olddecl);
}
if (DECL_P (olddecl)
&& TREE_CODE (newdecl) == FUNCTION_DECL
&& TREE_CODE (olddecl) == FUNCTION_DECL
&& (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
{
if (DECL_DECLARED_INLINE_P (newdecl)
&& DECL_UNINLINABLE (newdecl)
&& lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
/* Already warned elsewhere. */;
else if (DECL_DECLARED_INLINE_P (olddecl)
&& DECL_UNINLINABLE (olddecl)
&& lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
/* Already warned. */;
else if (DECL_DECLARED_INLINE_P (newdecl)
&& DECL_UNINLINABLE (olddecl)
&& lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
{
warning (OPT_Wattributes, "function %q+D redeclared as inline",
newdecl);
warning (OPT_Wattributes, "previous declaration of %q+D "
"with attribute noinline", olddecl);
}
else if (DECL_DECLARED_INLINE_P (olddecl)
&& DECL_UNINLINABLE (newdecl)
&& lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
{
warning (OPT_Wattributes, "function %q+D redeclared with "
"attribute noinline", newdecl);
warning (OPT_Wattributes, "previous declaration of %q+D was inline",
olddecl);
}
}
/* Check for redeclaration and other discrepancies. */
if (TREE_CODE (olddecl) == FUNCTION_DECL
&& DECL_ARTIFICIAL (olddecl))
{
gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
if (TREE_CODE (newdecl) != FUNCTION_DECL)
{
/* Avoid warnings redeclaring built-ins which have not been
explicitly declared. */
if (DECL_ANTICIPATED (olddecl))
return NULL_TREE;
/* If you declare a built-in or predefined function name as static,
the old definition is overridden, but optionally warn this was a
bad choice of name. */
if (! TREE_PUBLIC (newdecl))
{
warning (OPT_Wshadow,
DECL_BUILT_IN (olddecl)
? G_("shadowing built-in function %q#D")
: G_("shadowing library function %q#D"), olddecl);
/* Discard the old built-in function. */
return NULL_TREE;
}
/* If the built-in is not ansi, then programs can override
it even globally without an error. */
else if (! DECL_BUILT_IN (olddecl))
warning (0, "library function %q#D redeclared as non-function %q#D",
olddecl, newdecl);
else
{
error ("declaration of %q#D", newdecl);
error ("conflicts with built-in declaration %q#D",
olddecl);
}
return NULL_TREE;
}
else if (!types_match)
{
/* Avoid warnings redeclaring built-ins which have not been
explicitly declared. */
if (DECL_ANTICIPATED (olddecl))
{
/* Deal with fileptr_type_node. FILE type is not known
at the time we create the builtins. */
tree t1, t2;
for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
t1 || t2;
t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
if (!t1 || !t2)
break;
else if (TREE_VALUE (t2) == fileptr_type_node)
{
tree t = TREE_VALUE (t1);
if (TREE_CODE (t) == POINTER_TYPE
&& TYPE_NAME (TREE_TYPE (t))
&& DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
== get_identifier ("FILE")
&& compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
{
tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
TYPE_ARG_TYPES (TREE_TYPE (olddecl))
= TYPE_ARG_TYPES (TREE_TYPE (newdecl));
types_match = decls_match (newdecl, olddecl);
if (types_match)
return duplicate_decls (newdecl, olddecl,
newdecl_is_friend);
TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
}
}
else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
break;
}
else if ((DECL_EXTERN_C_P (newdecl)
&& DECL_EXTERN_C_P (olddecl))
|| compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
{
/* A near match; override the builtin. */
if (TREE_PUBLIC (newdecl))
{
warning (0, "new declaration %q#D", newdecl);
warning (0, "ambiguates built-in declaration %q#D",
olddecl);
}
else
warning (OPT_Wshadow,
DECL_BUILT_IN (olddecl)
? G_("shadowing built-in function %q#D")
: G_("shadowing library function %q#D"), olddecl);
}
else
/* Discard the old built-in function. */
return NULL_TREE;
/* Replace the old RTL to avoid problems with inlining. */
COPY_DECL_RTL (newdecl, olddecl);
}
/* Even if the types match, prefer the new declarations type for
built-ins which have not been explicitly declared, for
exception lists, etc... */
else if (DECL_IS_BUILTIN (olddecl))
{
tree type = TREE_TYPE (newdecl);
tree attribs = (*targetm.merge_type_attributes)
(TREE_TYPE (olddecl), type);
type = cp_build_type_attribute_variant (type, attribs);
TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
}
/* If a function is explicitly declared "throw ()", propagate that to
the corresponding builtin. */
if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
&& DECL_ANTICIPATED (olddecl)
&& TREE_NOTHROW (newdecl)
&& !TREE_NOTHROW (olddecl))
{
enum built_in_function fncode = DECL_FUNCTION_CODE (olddecl);
tree tmpdecl = builtin_decl_explicit (fncode);
if (tmpdecl && tmpdecl != olddecl && types_match)
TREE_NOTHROW (tmpdecl) = 1;
}
/* Whether or not the builtin can throw exceptions has no
bearing on this declarator. */
TREE_NOTHROW (olddecl) = 0;
if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
{
/* If a builtin function is redeclared as `static', merge
the declarations, but make the original one static. */
DECL_THIS_STATIC (olddecl) = 1;
TREE_PUBLIC (olddecl) = 0;
/* Make the old declaration consistent with the new one so
that all remnants of the builtin-ness of this function
will be banished. */
SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
COPY_DECL_RTL (newdecl, olddecl);
}
}
else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
{
/* C++ Standard, 3.3, clause 4:
"[Note: a namespace name or a class template name must be unique
in its declarative region (7.3.2, clause 14). ]" */
if (TREE_CODE (olddecl) != NAMESPACE_DECL
&& TREE_CODE (newdecl) != NAMESPACE_DECL
&& (TREE_CODE (olddecl) != TEMPLATE_DECL
|| TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
&& (TREE_CODE (newdecl) != TEMPLATE_DECL
|| TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
{
if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
&& TREE_CODE (newdecl) != TYPE_DECL)
|| (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
&& TREE_CODE (olddecl) != TYPE_DECL))
{
/* We do nothing special here, because C++ does such nasty
things with TYPE_DECLs. Instead, just let the TYPE_DECL
get shadowed, and know that if we need to find a TYPE_DECL
for a given name, we can look in the IDENTIFIER_TYPE_VALUE
slot of the identifier. */
return NULL_TREE;
}
if ((TREE_CODE (newdecl) == FUNCTION_DECL
&& DECL_FUNCTION_TEMPLATE_P (olddecl))
|| (TREE_CODE (olddecl) == FUNCTION_DECL
&& DECL_FUNCTION_TEMPLATE_P (newdecl)))
return NULL_TREE;
}
error ("%q#D redeclared as different kind of symbol", newdecl);
if (TREE_CODE (olddecl) == TREE_LIST)
olddecl = TREE_VALUE (olddecl);
error ("previous declaration of %q+#D", olddecl);
return error_mark_node;
}
else if (!types_match)
{
if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
/* These are certainly not duplicate declarations; they're
from different scopes. */
return NULL_TREE;
if (TREE_CODE (newdecl) == TEMPLATE_DECL)
{
/* The name of a class template may not be declared to refer to
any other template, class, function, object, namespace, value,
or type in the same scope. */
if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
|| TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
{
error ("declaration of template %q#D", newdecl);
error ("conflicts with previous declaration %q+#D", olddecl);
return error_mark_node;
}
else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
&& TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
&& compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
&& comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
DECL_TEMPLATE_PARMS (olddecl))
/* Template functions can be disambiguated by
return type. */
&& same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
TREE_TYPE (TREE_TYPE (olddecl))))
{
error ("new declaration %q#D", newdecl);
error ("ambiguates old declaration %q+#D", olddecl);
}
return NULL_TREE;
}
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
{
error ("declaration of C function %q#D conflicts with",
newdecl);
error ("previous declaration %q+#D here", olddecl);
return NULL_TREE;
}
/* For function versions, params and types match, but they
are not ambiguous. */
else if ((!DECL_FUNCTION_VERSIONED (newdecl)
&& !DECL_FUNCTION_VERSIONED (olddecl))
&& compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
{
error ("new declaration %q#D", newdecl);
if (FNDECL_USED_AUTO (olddecl))
error_at (DECL_SOURCE_LOCATION (olddecl), "ambiguates old "
"declaration with deduced return type");
else
error ("ambiguates old declaration %q+#D", olddecl);
return error_mark_node;
}
else
return NULL_TREE;
}
else
{
error ("conflicting declaration %q#D", newdecl);
error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
return error_mark_node;
}
}
else if (TREE_CODE (newdecl) == FUNCTION_DECL
&& ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
&& (!DECL_TEMPLATE_INFO (newdecl)
|| (DECL_TI_TEMPLATE (newdecl)
!= DECL_TI_TEMPLATE (olddecl))))
|| (DECL_TEMPLATE_SPECIALIZATION (newdecl)
&& (!DECL_TEMPLATE_INFO (olddecl)
|| (DECL_TI_TEMPLATE (olddecl)
!= DECL_TI_TEMPLATE (newdecl))))))
/* It's OK to have a template specialization and a non-template
with the same type, or to have specializations of two
different templates with the same type. Note that if one is a
specialization, and the other is an instantiation of the same
template, that we do not exit at this point. That situation
can occur if we instantiate a template class, and then
specialize one of its methods. This situation is valid, but
the declarations must be merged in the usual way. */
return NULL_TREE;
else if (TREE_CODE (newdecl) == FUNCTION_DECL
&& ((DECL_TEMPLATE_INSTANTIATION (olddecl)
&& !DECL_USE_TEMPLATE (newdecl))
|| (DECL_TEMPLATE_INSTANTIATION (newdecl)
&& !DECL_USE_TEMPLATE (olddecl))))
/* One of the declarations is a template instantiation, and the
other is not a template at all. That's OK. */
return NULL_TREE;
else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
{
/* In [namespace.alias] we have:
In a declarative region, a namespace-alias-definition can be
used to redefine a namespace-alias declared in that declarative
region to refer only to the namespace to which it already
refers.
Therefore, if we encounter a second alias directive for the same
alias, we can just ignore the second directive. */
if (DECL_NAMESPACE_ALIAS (newdecl)
&& (DECL_NAMESPACE_ALIAS (newdecl)
== DECL_NAMESPACE_ALIAS (olddecl)))
return olddecl;
/* [namespace.alias]
A namespace-name or namespace-alias shall not be declared as
the name of any other entity in the same declarative region.
A namespace-name defined at global scope shall not be
declared as the name of any other entity in any global scope
of the program. */
error ("declaration of namespace %qD conflicts with", newdecl);
error ("previous declaration of namespace %q+D here", olddecl);
return error_mark_node;
}
else
{
const char *errmsg = redeclaration_error_message (newdecl, olddecl);
if (errmsg)
{
error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
if (DECL_NAME (olddecl) != NULL_TREE)
error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
? G_("%q+#D previously defined here")
: G_("%q+#D previously declared here"), olddecl);
return error_mark_node;
}
else if (TREE_CODE (olddecl) == FUNCTION_DECL
&& DECL_INITIAL (olddecl) != NULL_TREE
&& !prototype_p (TREE_TYPE (olddecl))
&& prototype_p (TREE_TYPE (newdecl)))
{
/* Prototype decl follows defn w/o prototype. */
warning_at (input_location, 0, "prototype for %q+#D", newdecl);
warning_at (DECL_SOURCE_LOCATION (olddecl), 0,
"follows non-prototype definition here");
}
else if ((TREE_CODE (olddecl) == FUNCTION_DECL
|| TREE_CODE (olddecl) == VAR_DECL)
&& DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
{
/* [dcl.link]
If two declarations of the same function or object
specify different linkage-specifications ..., the program
is ill-formed.... Except for functions with C++ linkage,
a function declaration without a linkage specification
shall not precede the first linkage specification for
that function. A function can be declared without a
linkage specification after an explicit linkage
specification has been seen; the linkage explicitly
specified in the earlier declaration is not affected by
such a function declaration.
DR 563 raises the question why the restrictions on
functions should not also apply to objects. Older
versions of G++ silently ignore the linkage-specification
for this example:
namespace N {
extern int i;
extern "C" int i;
}
which is clearly wrong. Therefore, we now treat objects
like functions. */
if (current_lang_depth () == 0)
{
/* There is no explicit linkage-specification, so we use
the linkage from the previous declaration. */
if (!DECL_LANG_SPECIFIC (newdecl))
retrofit_lang_decl (newdecl);
SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
}
else
{
error ("previous declaration of %q+#D with %qL linkage",
olddecl, DECL_LANGUAGE (olddecl));
error ("conflicts with new declaration with %qL linkage",
DECL_LANGUAGE (newdecl));
}
}
if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
;
else if (TREE_CODE (olddecl) == FUNCTION_DECL)
{
tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
int i = 1;
if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
for (; t1 && t1 != void_list_node;
t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
{
if (1 == simple_cst_equal (TREE_PURPOSE (t1),
TREE_PURPOSE (t2)))
{
permerror (input_location, "default argument given for parameter %d of %q#D",
i, newdecl);
permerror (input_location, "after previous specification in %q+#D", olddecl);
}
else
{
error ("default argument given for parameter %d of %q#D",
i, newdecl);
error ("after previous specification in %q+#D",
olddecl);
}
}
}
}
/* Do not merge an implicit typedef with an explicit one. In:
class A;
...
typedef class A A __attribute__ ((foo));
the attribute should apply only to the typedef. */
if (TREE_CODE (olddecl) == TYPE_DECL
&& (DECL_IMPLICIT_TYPEDEF_P (olddecl)
|| DECL_IMPLICIT_TYPEDEF_P (newdecl)))
return NULL_TREE;
/* If new decl is `static' and an `extern' was seen previously,
warn about it. */
warn_extern_redeclared_static (newdecl, olddecl);
if (!validate_constexpr_redeclaration (olddecl, newdecl))
return error_mark_node;
/* We have committed to returning 1 at this point. */
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
/* Now that functions must hold information normally held
by field decls, there is extra work to do so that
declaration information does not get destroyed during
definition. */
if (DECL_VINDEX (olddecl))
DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
if (DECL_CONTEXT (olddecl))
DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
SET_OVERLOADED_OPERATOR_CODE
(newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
/* Optionally warn about more than one declaration for the same
name, but don't warn about a function declaration followed by a
definition. */
if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
&& !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
/* Don't warn about extern decl followed by definition. */
&& !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
/* Don't warn about friends, let add_friend take care of it. */
&& ! (newdecl_is_friend || DECL_FRIEND_P (olddecl))
/* Don't warn about declaration followed by specialization. */
&& (! DECL_TEMPLATE_SPECIALIZATION (newdecl)
|| DECL_TEMPLATE_SPECIALIZATION (olddecl)))
{
warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
}
if (!(DECL_TEMPLATE_INSTANTIATION (olddecl)
&& DECL_TEMPLATE_SPECIALIZATION (newdecl)))
{
if (DECL_DELETED_FN (newdecl))
{
error ("deleted definition of %qD", newdecl);
error ("after previous declaration %q+D", olddecl);
}
DECL_DELETED_FN (newdecl) |= DECL_DELETED_FN (olddecl);
}
}
/* Deal with C++: must preserve virtual function table size. */
if (TREE_CODE (olddecl) == TYPE_DECL)
{
tree newtype = TREE_TYPE (newdecl);
tree oldtype = TREE_TYPE (olddecl);
if (newtype != error_mark_node && oldtype != error_mark_node
&& TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
CLASSTYPE_FRIEND_CLASSES (newtype)
= CLASSTYPE_FRIEND_CLASSES (oldtype);
DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
}
/* Copy all the DECL_... slots specified in the new decl
except for any that we copy here from the old type. */
DECL_ATTRIBUTES (newdecl)
= (*targetm.merge_decl_attributes) (olddecl, newdecl);
if (TREE_CODE (newdecl) == TEMPLATE_DECL)
{
tree old_result;
tree new_result;
old_result = DECL_TEMPLATE_RESULT (olddecl);
new_result = DECL_TEMPLATE_RESULT (newdecl);
TREE_TYPE (olddecl) = TREE_TYPE (old_result);
DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
= chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
DECL_ATTRIBUTES (old_result)
= (*targetm.merge_decl_attributes) (old_result, new_result);
if (DECL_FUNCTION_TEMPLATE_P (newdecl))
{
if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
&& DECL_INITIAL (new_result))
{
if (DECL_INITIAL (old_result))
DECL_UNINLINABLE (old_result) = 1;
else
DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
DECL_NOT_REALLY_EXTERN (old_result)
= DECL_NOT_REALLY_EXTERN (new_result);
DECL_INTERFACE_KNOWN (old_result)
= DECL_INTERFACE_KNOWN (new_result);
DECL_DECLARED_INLINE_P (old_result)
= DECL_DECLARED_INLINE_P (new_result);
DECL_DISREGARD_INLINE_LIMITS (old_result)
|= DECL_DISREGARD_INLINE_LIMITS (new_result);
}
else
{
DECL_DECLARED_INLINE_P (old_result)
|= DECL_DECLARED_INLINE_P (new_result);
DECL_DISREGARD_INLINE_LIMITS (old_result)
|= DECL_DISREGARD_INLINE_LIMITS (new_result);
check_redeclaration_exception_specification (newdecl, olddecl);
}
}
/* If the new declaration is a definition, update the file and
line information on the declaration, and also make
the old declaration the same definition. */
if (DECL_INITIAL (new_result) != NULL_TREE)
{
DECL_SOURCE_LOCATION (olddecl)
= DECL_SOURCE_LOCATION (old_result)
= DECL_SOURCE_LOCATION (newdecl);
DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
if (DECL_FUNCTION_TEMPLATE_P (newdecl))
{
tree parm;
DECL_ARGUMENTS (old_result)
= DECL_ARGUMENTS (new_result);
for (parm = DECL_ARGUMENTS (old_result); parm;
parm = DECL_CHAIN (parm))
DECL_CONTEXT (parm) = old_result;
}
}
return olddecl;
}
if (types_match)
{
/* Automatically handles default parameters. */
tree oldtype = TREE_TYPE (olddecl);
tree newtype;
if (TREE_CODE (newdecl) == FUNCTION_DECL)
maybe_instantiate_noexcept (olddecl);
/* Merge the data types specified in the two decls. */
newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
/* If merge_types produces a non-typedef type, just use the old type. */
if (TREE_CODE (newdecl) == TYPE_DECL
&& newtype == DECL_ORIGINAL_TYPE (newdecl))
newtype = oldtype;
if (TREE_CODE (newdecl) == VAR_DECL)
{
DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
|= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
|= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
/* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
if (DECL_LANG_SPECIFIC (olddecl)
&& CP_DECL_THREADPRIVATE_P (olddecl))
{
/* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
if (!DECL_LANG_SPECIFIC (newdecl))
retrofit_lang_decl (newdecl);
DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
CP_DECL_THREADPRIVATE_P (newdecl) = 1;
}
}
/* Do this after calling `merge_types' so that default
parameters don't confuse us. */
else if (TREE_CODE (newdecl) == FUNCTION_DECL)
check_redeclaration_exception_specification (newdecl, olddecl);
TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
if (TREE_CODE (newdecl) == FUNCTION_DECL)
check_default_args (newdecl);
/* Lay the type out, unless already done. */
if (! same_type_p (newtype, oldtype)
&& TREE_TYPE (newdecl) != error_mark_node
&& !(processing_template_decl && uses_template_parms (newdecl)))
layout_type (TREE_TYPE (newdecl));
if ((TREE_CODE (newdecl) == VAR_DECL
|| TREE_CODE (newdecl) == PARM_DECL
|| TREE_CODE (newdecl) == RESULT_DECL
|| TREE_CODE (newdecl) == FIELD_DECL
|| TREE_CODE (newdecl) == TYPE_DECL)
&& !(processing_template_decl && uses_template_parms (newdecl)))
layout_decl (newdecl, 0);
/* Merge the type qualifiers. */
if (TREE_READONLY (newdecl))
TREE_READONLY (olddecl) = 1;
if (TREE_THIS_VOLATILE (newdecl))
TREE_THIS_VOLATILE (olddecl) = 1;
if (TREE_NOTHROW (newdecl))
TREE_NOTHROW (olddecl) = 1;
/* Merge deprecatedness. */
if (TREE_DEPRECATED (newdecl))
TREE_DEPRECATED (olddecl) = 1;
/* Preserve function specific target and optimization options */
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
&& !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
= DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
&& !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
= DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
}
/* Merge the initialization information. */
if (DECL_INITIAL (newdecl) == NULL_TREE
&& DECL_INITIAL (olddecl) != NULL_TREE)
{
DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
}
}
/* Merge the section attribute.
We want to issue an error if the sections conflict but that must be
done later in decl_attributes since we are called before attributes
are assigned. */
if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
|= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
DECL_LOOPING_CONST_OR_PURE_P (newdecl)
|= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
/* Keep the old RTL. */
COPY_DECL_RTL (olddecl, newdecl);
}
else if (TREE_CODE (newdecl) == VAR_DECL
&& (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
{
/* Keep the old RTL. We cannot keep the old RTL if the old
declaration was for an incomplete object and the new
declaration is not since many attributes of the RTL will
change. */
COPY_DECL_RTL (olddecl, newdecl);
}
}
/* If cannot merge, then use the new type and qualifiers,
and don't preserve the old rtl. */
else
{
/* Clean out any memory we had of the old declaration. */
tree oldstatic = value_member (olddecl, static_aggregates);
if (oldstatic)
TREE_VALUE (oldstatic) = error_mark_node;
TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
}
/* Merge the storage class information. */
merge_weak (newdecl, olddecl);
if (DECL_ONE_ONLY (olddecl))
DECL_COMDAT_GROUP (newdecl) = DECL_COMDAT_GROUP (olddecl);
DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
if (! DECL_EXTERNAL (olddecl))
DECL_EXTERNAL (newdecl) = 0;
new_template_info = NULL_TREE;
if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
{
bool new_redefines_gnu_inline = false;
if (new_defines_function
&& ((DECL_INTERFACE_KNOWN (olddecl)
&& TREE_CODE (olddecl) == FUNCTION_DECL)
|| (TREE_CODE (olddecl) == TEMPLATE_DECL
&& (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
== FUNCTION_DECL))))
{
tree fn = olddecl;
if (TREE_CODE (fn) == TEMPLATE_DECL)
fn = DECL_TEMPLATE_RESULT (olddecl);
new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
}
if (!new_redefines_gnu_inline)
{
DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
}
DECL_TEMPLATE_INSTANTIATED (newdecl)
|= DECL_TEMPLATE_INSTANTIATED (olddecl);
DECL_ODR_USED (newdecl) |= DECL_ODR_USED (olddecl);
/* If the OLDDECL is an instantiation and/or specialization,
then the NEWDECL must be too. But, it may not yet be marked
as such if the caller has created NEWDECL, but has not yet
figured out that it is a redeclaration. */
if (!DECL_USE_TEMPLATE (newdecl))
DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
/* Don't really know how much of the language-specific
values we should copy from old to new. */
DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
DECL_INITIALIZED_IN_CLASS_P (newdecl)
|= DECL_INITIALIZED_IN_CLASS_P (olddecl);
if (LANG_DECL_HAS_MIN (newdecl))
{
DECL_LANG_SPECIFIC (newdecl)->u.min.u2 =
DECL_LANG_SPECIFIC (olddecl)->u.min.u2;
if (DECL_TEMPLATE_INFO (newdecl))
new_template_info = DECL_TEMPLATE_INFO (newdecl);
DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
}
/* Only functions have these fields. */
if (TREE_CODE (newdecl) == FUNCTION_DECL
|| DECL_FUNCTION_TEMPLATE_P (newdecl))
{
DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
olddecl_friend = DECL_FRIEND_P (olddecl);
hidden_friend = (DECL_ANTICIPATED (olddecl)
&& DECL_HIDDEN_FRIEND_P (olddecl)
&& newdecl_is_friend);
DECL_BEFRIENDING_CLASSES (newdecl)
= chainon (DECL_BEFRIENDING_CLASSES (newdecl),
DECL_BEFRIENDING_CLASSES (olddecl));
/* DECL_THUNKS is only valid for virtual functions,
otherwise it is a DECL_FRIEND_CONTEXT. */
if (DECL_VIRTUAL_P (newdecl))
SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
}
/* Only variables have this field. */
else if (TREE_CODE (newdecl) == VAR_DECL
&& VAR_HAD_UNKNOWN_BOUND (olddecl))
SET_VAR_HAD_UNKNOWN_BOUND (newdecl);
}
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
tree parm;
/* Merge parameter attributes. */
tree oldarg, newarg;
for (oldarg = DECL_ARGUMENTS(olddecl),
newarg = DECL_ARGUMENTS(newdecl);
oldarg && newarg;
oldarg = DECL_CHAIN(oldarg), newarg = DECL_CHAIN(newarg)) {
DECL_ATTRIBUTES (newarg)
= (*targetm.merge_decl_attributes) (oldarg, newarg);
DECL_ATTRIBUTES (oldarg) = DECL_ATTRIBUTES (newarg);
}
if (DECL_TEMPLATE_INSTANTIATION (olddecl)
&& !DECL_TEMPLATE_INSTANTIATION (newdecl))
{
/* If newdecl is not a specialization, then it is not a
template-related function at all. And that means that we
should have exited above, returning 0. */
gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
if (DECL_ODR_USED (olddecl))
/* From [temp.expl.spec]:
If a template, a member template or the member of a class
template is explicitly specialized then that
specialization shall be declared before the first use of
that specialization that would cause an implicit
instantiation to take place, in every translation unit in
which such a use occurs. */
error ("explicit specialization of %qD after first use",
olddecl);
SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
/* Don't propagate visibility from the template to the
specialization here. We'll do that in determine_visibility if
appropriate. */
DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
/* [temp.expl.spec/14] We don't inline explicit specialization
just because the primary template says so. */
/* But still keep DECL_DISREGARD_INLINE_LIMITS in sync with
the always_inline attribute. */
if (DECL_DISREGARD_INLINE_LIMITS (olddecl)
&& !DECL_DISREGARD_INLINE_LIMITS (newdecl))
{
if (DECL_DECLARED_INLINE_P (newdecl))
DECL_DISREGARD_INLINE_LIMITS (newdecl) = true;
else
DECL_ATTRIBUTES (newdecl)
= remove_attribute ("always_inline",
DECL_ATTRIBUTES (newdecl));
}
}
else if (new_defines_function && DECL_INITIAL (olddecl))
{
/* Never inline re-defined extern inline functions.
FIXME: this could be better handled by keeping both
function as separate declarations. */
DECL_UNINLINABLE (newdecl) = 1;
}
else
{
if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
= (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
DECL_DISREGARD_INLINE_LIMITS (newdecl)
= DECL_DISREGARD_INLINE_LIMITS (olddecl)
= (DECL_DISREGARD_INLINE_LIMITS (newdecl)
|| DECL_DISREGARD_INLINE_LIMITS (olddecl));
}
/* Preserve abstractness on cloned [cd]tors. */
DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
/* Update newdecl's parms to point at olddecl. */
for (parm = DECL_ARGUMENTS (newdecl); parm;
parm = DECL_CHAIN (parm))
DECL_CONTEXT (parm) = olddecl;
if (! types_match)
{
SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
COPY_DECL_RTL (newdecl, olddecl);
}
if (! types_match || new_defines_function)
{
/* These need to be copied so that the names are available.
Note that if the types do match, we'll preserve inline
info and other bits, but if not, we won't. */
DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
}
/* If redeclaring a builtin function, it stays built in
if newdecl is a gnu_inline definition, or if newdecl is just
a declaration. */
if (DECL_BUILT_IN (olddecl)
&& (new_defines_function ? GNU_INLINE_P (newdecl) : types_match))
{
DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
/* If we're keeping the built-in definition, keep the rtl,
regardless of declaration matches. */
COPY_DECL_RTL (olddecl, newdecl);
if (DECL_BUILT_IN_CLASS (newdecl) == BUILT_IN_NORMAL)
{
enum built_in_function fncode = DECL_FUNCTION_CODE (newdecl);
switch (fncode)
{
/* If a compatible prototype of these builtin functions
is seen, assume the runtime implements it with the
expected semantics. */
case BUILT_IN_STPCPY:
if (builtin_decl_explicit_p (fncode))
set_builtin_decl_implicit_p (fncode, true);
break;
default:
break;
}
}
}
if (new_defines_function)
/* If defining a function declared with other language
linkage, use the previously declared language linkage. */
SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
else if (types_match)
{
DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
/* Don't clear out the arguments if we're just redeclaring a
function. */
if (DECL_ARGUMENTS (olddecl))
DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
}
}
else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
/* Now preserve various other info from the definition. */
TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
/* Warn about conflicting visibility specifications. */
if (DECL_VISIBILITY_SPECIFIED (olddecl)
&& DECL_VISIBILITY_SPECIFIED (newdecl)
&& DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
{
warning_at (input_location, OPT_Wattributes,
"%q+D: visibility attribute ignored because it", newdecl);
warning_at (DECL_SOURCE_LOCATION (olddecl), OPT_Wattributes,
"conflicts with previous declaration here");
}
/* Choose the declaration which specified visibility. */
if (DECL_VISIBILITY_SPECIFIED (olddecl))
{
DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
}
/* Init priority used to be merged from newdecl to olddecl by the memcpy,
so keep this behavior. */
if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
{
SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
}
/* Likewise for DECL_ALIGN, DECL_USER_ALIGN and DECL_PACKED. */
if (DECL_ALIGN (olddecl) > DECL_ALIGN (newdecl))
{
DECL_ALIGN (newdecl) = DECL_ALIGN (olddecl);
DECL_USER_ALIGN (newdecl) |= DECL_USER_ALIGN (olddecl);
}
DECL_USER_ALIGN (olddecl) = DECL_USER_ALIGN (newdecl);
if (TREE_CODE (newdecl) == FIELD_DECL)
DECL_PACKED (olddecl) = DECL_PACKED (newdecl);
/* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
with that from NEWDECL below. */
if (DECL_LANG_SPECIFIC (olddecl))
{
gcc_assert (DECL_LANG_SPECIFIC (olddecl)
!= DECL_LANG_SPECIFIC (newdecl));
ggc_free (DECL_LANG_SPECIFIC (olddecl));
}
/* Merge the USED information. */
if (TREE_USED (olddecl))
TREE_USED (newdecl) = 1;
else if (TREE_USED (newdecl))
TREE_USED (olddecl) = 1;
if (TREE_CODE (newdecl) == VAR_DECL)
{
if (DECL_READ_P (olddecl))
DECL_READ_P (newdecl) = 1;
else if (DECL_READ_P (newdecl))
DECL_READ_P (olddecl) = 1;
}
if (DECL_PRESERVE_P (olddecl))
DECL_PRESERVE_P (newdecl) = 1;
else if (DECL_PRESERVE_P (newdecl))
DECL_PRESERVE_P (olddecl) = 1;
/* Merge the DECL_FUNCTION_VERSIONED information. newdecl will be copied
to olddecl and deleted. */
if (TREE_CODE (newdecl) == FUNCTION_DECL
&& DECL_FUNCTION_VERSIONED (olddecl))
{
/* Set the flag for newdecl so that it gets copied to olddecl. */
DECL_FUNCTION_VERSIONED (newdecl) = 1;
/* newdecl will be purged after copying to olddecl and is no longer
a version. */
delete_function_version (newdecl);
}
if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
int function_size;
function_size = sizeof (struct tree_decl_common);
memcpy ((char *) olddecl + sizeof (struct tree_common),
(char *) newdecl + sizeof (struct tree_common),
function_size - sizeof (struct tree_common));
memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
(char *) newdecl + sizeof (struct tree_decl_common),
sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
if (new_template_info)
/* If newdecl is a template instantiation, it is possible that
the following sequence of events has occurred:
o A friend function was declared in a class template. The
class template was instantiated.
o The instantiation of the friend declaration was
recorded on the instantiation list, and is newdecl.
o Later, however, instantiate_class_template called pushdecl
on the newdecl to perform name injection. But, pushdecl in
turn called duplicate_decls when it discovered that another
declaration of a global function with the same name already
existed.
o Here, in duplicate_decls, we decided to clobber newdecl.
If we're going to do that, we'd better make sure that
olddecl, and not newdecl, is on the list of
instantiations so that if we try to do the instantiation
again we won't get the clobbered declaration. */
reregister_specialization (newdecl,
new_template_info,
olddecl);
}
else
{
size_t size = tree_code_size (TREE_CODE (olddecl));
memcpy ((char *) olddecl + sizeof (struct tree_common),
(char *) newdecl + sizeof (struct tree_common),
sizeof (struct tree_decl_common) - sizeof (struct tree_common));
switch (TREE_CODE (olddecl))
{
case LABEL_DECL:
case VAR_DECL:
case RESULT_DECL:
case PARM_DECL:
case FIELD_DECL:
case TYPE_DECL:
case CONST_DECL:
{
memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
(char *) newdecl + sizeof (struct tree_decl_common),
size - sizeof (struct tree_decl_common)
+ TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
}
break;
default:
memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
(char *) newdecl + sizeof (struct tree_decl_common),
sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
+ TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
break;
}
}
DECL_UID (olddecl) = olddecl_uid;
if (olddecl_friend)
DECL_FRIEND_P (olddecl) = 1;
if (hidden_friend)
{
DECL_ANTICIPATED (olddecl) = 1;
DECL_HIDDEN_FRIEND_P (olddecl) = 1;
}
/* NEWDECL contains the merged attribute lists.
Update OLDDECL to be the same. */
DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
/* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
so that encode_section_info has a chance to look at the new decl
flags and attributes. */
if (DECL_RTL_SET_P (olddecl)
&& (TREE_CODE (olddecl) == FUNCTION_DECL
|| (TREE_CODE (olddecl) == VAR_DECL
&& TREE_STATIC (olddecl))))
make_decl_rtl (olddecl);
/* The NEWDECL will no longer be needed. Because every out-of-class
declaration of a member results in a call to duplicate_decls,
freeing these nodes represents in a significant savings. */
ggc_free (newdecl);
return olddecl;
}
/* Return zero if the declaration NEWDECL is valid
when the declaration OLDDECL (assumed to be for the same name)
has already been seen.
Otherwise return an error message format string with a %s
where the identifier should go. */
static const char *
redeclaration_error_message (tree newdecl, tree olddecl)
{
if (TREE_CODE (newdecl) == TYPE_DECL)
{
/* Because C++ can put things into name space for free,
constructs like "typedef struct foo { ... } foo"
would look like an erroneous redeclaration. */
if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
return NULL;
else
return G_("redefinition of %q#D");
}
else if (TREE_CODE (newdecl) == FUNCTION_DECL)
{
/* If this is a pure function, its olddecl will actually be
the original initialization to `0' (which we force to call
abort()). Don't complain about redefinition in this case. */
if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
&& DECL_INITIAL (olddecl) == NULL_TREE)
return NULL;
/* If both functions come from different namespaces, this is not
a redeclaration - this is a conflict with a used function. */
if (DECL_NAMESPACE_SCOPE_P (olddecl)
&& DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
&& ! decls_match (olddecl, newdecl))
return G_("%qD conflicts with used function");
/* We'll complain about linkage mismatches in
warn_extern_redeclared_static. */
/* Defining the same name twice is no good. */
if (DECL_INITIAL (olddecl) != NULL_TREE
&& DECL_INITIAL (newdecl) != NULL_TREE)
{
if (DECL_NAME (olddecl) == NULL_TREE)
return G_("%q#D not declared in class");
else if (!GNU_INLINE_P (olddecl)
|| GNU_INLINE_P (newdecl))
return G_("redefinition of %q#D");
}
if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
{
bool olda = GNU_INLINE_P (olddecl);
bool newa = GNU_INLINE_P (newdecl);
if (olda != newa)
{
if (newa)
return G_("%q+D redeclared inline with "
"%<gnu_inline%> attribute");
else
return G_("%q+D redeclared inline without "
"%<gnu_inline%> attribute");
}
}
check_abi_tag_redeclaration
(olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
return NULL;
}
else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
{
tree nt, ot;
if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
{
if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
&& COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
return G_("redefinition of %q#D");
return NULL;
}
if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
|| (DECL_TEMPLATE_RESULT (newdecl)
== DECL_TEMPLATE_RESULT (olddecl)))
return NULL;
nt = DECL_TEMPLATE_RESULT (newdecl);
if (DECL_TEMPLATE_INFO (nt))
nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
ot = DECL_TEMPLATE_RESULT (olddecl);
if (DECL_TEMPLATE_INFO (ot))
ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
&& (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
return G_("redefinition of %q#D");
if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
{
bool olda = GNU_INLINE_P (ot);
bool newa = GNU_INLINE_P (nt);
if (olda != newa)
{
if (newa)
return G_("%q+D redeclared inline with "
"%<gnu_inline%> attribute");
else
return G_("%q+D redeclared inline without "
"%<gnu_inline%> attribute");
}
}
/* Core issue #226 (C++0x):
If a friend function template declaration specifies a
default template-argument, that declaration shall be a
definition and shall be the only declaration of the
function template in the translation unit. */
if ((cxx_dialect != cxx98)
&& TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
&& !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
/*is_primary=*/true,
/*is_partial=*/false,
/*is_friend_decl=*/2))
return G_("redeclaration of friend %q#D "
"may not have default template arguments");
return NULL;
}
else if (TREE_CODE (newdecl) == VAR_DECL
&& DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
&& (! DECL_LANG_SPECIFIC (olddecl)
|| ! CP_DECL_THREADPRIVATE_P (olddecl)
|| DECL_THREAD_LOCAL_P (newdecl)))
{
/* Only variables can be thread-local, and all declarations must
agree on this property. */
if (DECL_THREAD_LOCAL_P (newdecl))
return G_("thread-local declaration of %q#D follows "
"non-thread-local declaration");
else
return G_("non-thread-local declaration of %q#D follows "
"thread-local declaration");
}
else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
{
/* The objects have been declared at namespace scope. If either
is a member of an anonymous union, then this is an invalid
redeclaration. For example:
int i;
union { int i; };
is invalid. */
if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
|| (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
return G_("redeclaration of %q#D");
/* If at least one declaration is a reference, there is no
conflict. For example:
int i = 3;
extern int i;
is valid. */
if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
return NULL;
/* Reject two definitions. */
return G_("redefinition of %q#D");
}
else
{
/* Objects declared with block scope: */
/* Reject two definitions, and reject a definition
together with an external reference. */
if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
return G_("redeclaration of %q#D");
return NULL;
}
}
/* Hash and equality functions for the named_label table. */
static hashval_t
named_label_entry_hash (const void *data)
{
const struct named_label_entry *ent = (const struct named_label_entry *) data;
return DECL_UID (ent->label_decl);
}
static int
named_label_entry_eq (const void *a, const void *b)
{
const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
return ent_a->label_decl == ent_b->label_decl;
}
/* Create a new label, named ID. */
static tree
make_label_decl (tree id, int local_p)
{
struct named_label_entry *ent;
void **slot;
tree decl;
decl = build_decl (input_location, LABEL_DECL, id, void_type_node);
DECL_CONTEXT (decl) = current_function_decl;
DECL_MODE (decl) = VOIDmode;
C_DECLARED_LABEL_FLAG (decl) = local_p;
/* Say where one reference is to the label, for the sake of the
error if it is not defined. */
DECL_SOURCE_LOCATION (decl) = input_location;
/* Record the fact that this identifier is bound to this label. */
SET_IDENTIFIER_LABEL_VALUE (id, decl);
/* Create the label htab for the function on demand. */
if (!named_labels)
named_labels = htab_create_ggc (13, named_label_entry_hash,
named_label_entry_eq, NULL);
/* Record this label on the list of labels used in this function.
We do this before calling make_label_decl so that we get the
IDENTIFIER_LABEL_VALUE before the new label is declared. */
ent = ggc_alloc_cleared_named_label_entry ();
ent->label_decl = decl;
slot = htab_find_slot (named_labels, ent, INSERT);
gcc_assert (*slot == NULL);
*slot = ent;
return decl;
}
/* Look for a label named ID in the current function. If one cannot
be found, create one. (We keep track of used, but undefined,
labels, and complain about them at the end of a function.) */
static tree
lookup_label_1 (tree id)
{
tree decl;
/* You can't use labels at global scope. */
if (current_function_decl == NULL_TREE)
{
error ("label %qE referenced outside of any function", id);
return NULL_TREE;
}
/* See if we've already got this label. */
decl = IDENTIFIER_LABEL_VALUE (id);
if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
return decl;
decl = make_label_decl (id, /*local_p=*/0);
return decl;
}
/* Wrapper for lookup_label_1. */
tree
lookup_label (tree id)
{
tree ret;
bool subtime = timevar_cond_start (TV_NAME_LOOKUP);
ret = lookup_label_1 (id);
timevar_cond_stop (TV_NAME_LOOKUP, subtime);
return ret;
}
/* Declare a local label named ID. */
tree
declare_local_label (tree id)
{
tree decl;
cp_label_binding bind;
/* Add a new entry to the SHADOWED_LABELS list so that when we leave
this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
bind.prev_value = IDENTIFIER_LABEL_VALUE (id);
decl = make_label_decl (id, /*local_p=*/1);
bind.label = decl;
vec_safe_push (current_binding_level->shadowed_labels, bind);
return decl;
}
/* Returns nonzero if it is ill-formed to jump past the declaration of
DECL. Returns 2 if it's also a real problem. */
static int
decl_jump_unsafe (tree decl)
{
/* [stmt.dcl]/3: A program that jumps from a point where a local variable
with automatic storage duration is not in scope to a point where it is
in scope is ill-formed unless the variable has scalar type, class type
with a trivial default constructor and a trivial destructor, a
cv-qualified version of one of these types, or an array of one of the
preceding types and is declared without an initializer (8.5). */
tree type = TREE_TYPE (decl);
if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
|| type == error_mark_node)
return 0;
type = strip_array_types (type);
if (DECL_NONTRIVIALLY_INITIALIZED_P (decl))
return 2;
if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
return 1;
return 0;
}
/* A subroutine of check_previous_goto_1 to identify a branch to the user. */
static void
identify_goto (tree decl, const location_t *locus)
{
if (decl)
permerror (input_location, "jump to label %qD", decl);
else
permerror (input_location, "jump to case label");
if (locus)
permerror (*locus, " from here");
}
/* Check that a single previously seen jump to a newly defined label
is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
the jump context; NAMES are the names in scope in LEVEL at the jump
context; LOCUS is the source position of the jump or 0. Returns
true if all is well. */
static bool
check_previous_goto_1 (tree decl, cp_binding_level* level, tree names,
bool exited_omp, const location_t *locus)
{
cp_binding_level *b;
bool identified = false, saw_eh = false, saw_omp = false;
if (exited_omp)
{
identify_goto (decl, locus);
error (" exits OpenMP structured block");
identified = saw_omp = true;
}
for (b = current_binding_level; b ; b = b->level_chain)
{
tree new_decls, old_decls = (b == level ? names : NULL_TREE);
for (new_decls = b->names; new_decls != old_decls;
new_decls = (DECL_P (new_decls) ? DECL_CHAIN (new_decls)
: TREE_CHAIN (new_decls)))
{
int problem = decl_jump_unsafe (new_decls);
if (! problem)
continue;
if (!identified)
{
identify_goto (decl, locus);
identified = true;
}
if (problem > 1)
error (" crosses initialization of %q+#D", new_decls);
else
permerror (input_location, " enters scope of %q+#D which has "
"non-trivial destructor", new_decls);
}
if (b == level)
break;
if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
{
if (!identified)
{
identify_goto (decl, locus);
identified = true;
}
if (b->kind == sk_try)
error (" enters try block");
else
error (" enters catch block");
saw_eh = true;
}
if (b->kind == sk_omp && !saw_omp)
{
if (!identified)
{
identify_goto (decl, locus);
identified = true;
}
error (" enters OpenMP structured block");
saw_omp = true;
}
}
return !identified;
}
static void
check_previous_goto (tree decl, struct named_label_use_entry *use)
{
check_previous_goto_1 (decl, use->binding_level,
use->names_in_scope, use->in_omp_scope,
&use->o_goto_locus);
}
static bool
check_switch_goto (cp_binding_level* level)
{
return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
}
/* Check that a new jump to a label DECL is OK. Called by
finish_goto_stmt. */
void
check_goto (tree decl)
{
struct named_label_entry *ent, dummy;
bool saw_catch = false, identified = false;
tree bad;
unsigned ix;
/* We can't know where a computed goto is jumping.
So we assume that it's OK. */
if (TREE_CODE (decl) != LABEL_DECL)
return;
/* We didn't record any information about this label when we created it,
and there's not much point since it's trivial to analyze as a return. */
if (decl == cdtor_label)
return;
dummy.label_decl = decl;
ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
gcc_assert (ent != NULL);
/* If the label hasn't been defined yet, defer checking. */
if (! DECL_INITIAL (decl))
{
struct named_label_use_entry *new_use;
/* Don't bother creating another use if the last goto had the
same data, and will therefore create the same set of errors. */
if (ent->uses
&& ent->uses->names_in_scope == current_binding_level->names)
return;
new_use = ggc_alloc_named_label_use_entry ();
new_use->binding_level = current_binding_level;
new_use->names_in_scope = current_binding_level->names;
new_use->o_goto_locus = input_location;
new_use->in_omp_scope = false;
new_use->next = ent->uses;
ent->uses = new_use;
return;
}
if (ent->in_try_scope || ent->in_catch_scope
|| ent->in_omp_scope || !vec_safe_is_empty (ent->bad_decls))
{
permerror (input_location, "jump to label %q+D", decl);
permerror (input_location, " from here");
identified = true;
}
FOR_EACH_VEC_SAFE_ELT (ent->bad_decls, ix, bad)
{
int u = decl_jump_unsafe (bad);
if (u > 1 && DECL_ARTIFICIAL (bad))
{
/* Can't skip init of __exception_info. */
error_at (DECL_SOURCE_LOCATION (bad), " enters catch block");
saw_catch = true;
}
else if (u > 1)
error (" skips initialization of %q+#D", bad);
else
permerror (input_location, " enters scope of %q+#D which has "
"non-trivial destructor", bad);
}
if (ent->in_try_scope)
error (" enters try block");
else if (ent->in_catch_scope && !saw_catch)
error (" enters catch block");
if (ent->in_omp_scope)
error (" enters OpenMP structured block");
else if (flag_openmp)
{
cp_binding_level *b;
for (b = current_binding_level; b ; b = b->level_chain)
{
if (b == ent->binding_level)
break;
if (b->kind == sk_omp)
{
if (!identified)
{
permerror (input_location, "jump to label %q+D", decl);
permerror (input_location, " from here");
identified = true;
}
error (" exits OpenMP structured block");
break;
}
}
}
}
/* Check that a return is ok wrt OpenMP structured blocks.
Called by finish_return_stmt. Returns true if all is well. */
bool
check_omp_return (void)
{
cp_binding_level *b;
for (b = current_binding_level; b ; b = b->level_chain)
if (b->kind == sk_omp)
{
error ("invalid exit from OpenMP structured block");
return false;
}
else if (b->kind == sk_function_parms)
break;
return true;
}
/* Define a label, specifying the location in the source file.
Return the LABEL_DECL node for the label. */
static tree
define_label_1 (location_t location, tree name)
{
struct named_label_entry *ent, dummy;
cp_binding_level *p;
tree decl;
decl = lookup_label (name);
dummy.label_decl = decl;
ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
gcc_assert (ent != NULL);
/* After labels, make any new cleanups in the function go into their
own new (temporary) binding contour. */
for (p = current_binding_level;
p->kind != sk_function_parms;
p = p->level_chain)
p->more_cleanups_ok = 0;
if (name == get_identifier ("wchar_t"))
permerror (input_location, "label named wchar_t");
if (DECL_INITIAL (decl) != NULL_TREE)
{
error ("duplicate label %qD", decl);
return error_mark_node;
}
else
{
struct named_label_use_entry *use;
/* Mark label as having been defined. */
DECL_INITIAL (decl) = error_mark_node;
/* Say where in the source. */
DECL_SOURCE_LOCATION (decl) = location;
ent->binding_level = current_binding_level;
ent->names_in_scope = current_binding_level->names;
for (use = ent->uses; use ; use = use->next)
check_previous_goto (decl, use);
ent->uses = NULL;
}
return decl;
}
/* Wrapper for define_label_1. */
tree
define_label (location_t location, tree name)
{
tree ret;
bool running = timevar_cond_start (TV_NAME_LOOKUP);
ret = define_label_1 (location, name);
timevar_cond_stop (TV_NAME_LOOKUP, running);
return ret;
}
struct cp_switch
{
cp_binding_level *level;
struct cp_switch *next;
/* The SWITCH_STMT being built. */
tree switch_stmt;
/* A splay-tree mapping the low element of a case range to the high
element, or NULL_TREE if there is no high element. Used to
determine whether or not a new case label duplicates an old case
label. We need a tree, rather than simply a hash table, because
of the GNU case range extension. */
splay_tree cases;
};
/* A stack of the currently active switch statements. The innermost
switch statement is on the top of the stack. There is no need to
mark the stack for garbage collection because it is only active
during the processing of the body of a function, and we never
collect at that point. */
static struct cp_switch *switch_stack;
/* Called right after a switch-statement condition is parsed.
SWITCH_STMT is the switch statement being parsed. */
void
push_switch (tree switch_stmt)
{
struct cp_switch *p = XNEW (struct cp_switch);
p->level = current_binding_level;
p->next = switch_stack;
p->switch_stmt = switch_stmt;
p->cases = splay_tree_new (case_compare, NULL, NULL);
switch_stack = p;
}
void
pop_switch (void)
{
struct cp_switch *cs = switch_stack;
location_t switch_location;
/* Emit warnings as needed. */
switch_location = EXPR_LOC_OR_HERE (cs->switch_stmt);
if (!processing_template_decl)
c_do_switch_warnings (cs->cases, switch_location,
SWITCH_STMT_TYPE (cs->switch_stmt),
SWITCH_STMT_COND (cs->switch_stmt));
splay_tree_delete (cs->cases);
switch_stack = switch_stack->next;
free (cs);
}
/* Convert a case constant VALUE in a switch to the type TYPE of the switch
condition. Note that if TYPE and VALUE are already integral we don't
really do the conversion because the language-independent
warning/optimization code will work better that way. */
static tree
case_conversion (tree type, tree value)
{
if (value == NULL_TREE)
return value;
if (cxx_dialect >= cxx0x
&& (SCOPED_ENUM_P (type)
|| !INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (value))))
{
if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (type))
type = type_promotes_to (type);
value = perform_implicit_conversion (type, value, tf_warning_or_error);
}
return cxx_constant_value (value);
}
/* Note that we've seen a definition of a case label, and complain if this
is a bad place for one. */
tree
finish_case_label (location_t loc, tree low_value, tree high_value)
{
tree cond, r;
cp_binding_level *p;
tree type;
if (processing_template_decl)
{
tree label;
/* For templates, just add the case label; we'll do semantic
analysis at instantiation-time. */
label = build_decl (loc, LABEL_DECL, NULL_TREE, NULL_TREE);
return add_stmt (build_case_label (low_value, high_value, label));
}
/* Find the condition on which this switch statement depends. */
cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
if (cond && TREE_CODE (cond) == TREE_LIST)
cond = TREE_VALUE (cond);
if (!check_switch_goto (switch_stack->level))
return error_mark_node;
type = SWITCH_STMT_TYPE (switch_stack->switch_stmt);
low_value = case_conversion (type, low_value);
high_value = case_conversion (type, high_value);
r = c_add_case_label (loc, switch_stack->cases, cond, type,
low_value, high_value);
/* After labels, make any new cleanups in the function go into their
own new (temporary) binding contour. */
for (p = current_binding_level;
p->kind != sk_function_parms;
p = p->level_chain)
p->more_cleanups_ok = 0;
return r;
}
/* Hash a TYPENAME_TYPE. K is really of type `tree'. */
static hashval_t
typename_hash (const void* k)
{
hashval_t hash;
const_tree const t = (const_tree) k;
hash = (htab_hash_pointer (TYPE_CONTEXT (t))
^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
return hash;
}
typedef struct typename_info {
tree scope;
tree name;
tree template_id;
bool enum_p;
bool class_p;
} typename_info;
/* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
really of type `typename_info*' */
static int
typename_compare (const void * k1, const void * k2)
{
const_tree const t1 = (const_tree) k1;
const typename_info *const t2 = (const typename_info *) k2;