1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
5 Contributed by Michael Tiemann (tiemann@cygnus.com)
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 /* Process declarations and symbol lookup for C++ front end.
25 Also constructs types; the standard scalar types at initialization,
26 and structure, union, array and enum types when they are declared. */
28 /* ??? not all decl nodes are given the most useful possible
29 line numbers. For example, the CONST_DECLs for enum values. */
33 #include "coretypes.h"
40 #include "tree-inline.h"
50 #include "diagnostic.h"
54 #include "tree-flow.h"
55 #include "pointer-set.h"
57 static tree grokparms (tree parmlist, tree *);
58 static const char *redeclaration_error_message (tree, tree);
60 static int decl_jump_unsafe (tree);
61 static void require_complete_types_for_parms (tree);
62 static int ambi_op_p (enum tree_code);
63 static int unary_op_p (enum tree_code);
64 static void push_local_name (tree);
65 static tree grok_reference_init (tree, tree, tree, tree *);
66 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
68 static void record_unknown_type (tree, const char *);
69 static tree builtin_function_1 (tree, tree, bool);
70 static tree build_library_fn_1 (tree, enum tree_code, tree);
71 static int member_function_or_else (tree, tree, enum overload_flags);
72 static void bad_specifiers (tree, const char *, int, int, int, int,
74 static void check_for_uninitialized_const_var (tree);
75 static hashval_t typename_hash (const void *);
76 static int typename_compare (const void *, const void *);
77 static tree local_variable_p_walkfn (tree *, int *, void *);
78 static tree record_builtin_java_type (const char *, int);
79 static const char *tag_name (enum tag_types);
80 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
81 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
82 static void maybe_deduce_size_from_array_init (tree, tree);
83 static void layout_var_decl (tree);
84 static tree check_initializer (tree, tree, int, tree *);
85 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
86 static void save_function_data (tree);
87 static void check_function_type (tree, tree);
88 static void finish_constructor_body (void);
89 static void begin_destructor_body (void);
90 static void finish_destructor_body (void);
91 static tree create_array_type_for_decl (tree, tree, tree);
92 static tree get_atexit_node (void);
93 static tree get_dso_handle_node (void);
94 static tree start_cleanup_fn (void);
95 static void end_cleanup_fn (void);
96 static tree cp_make_fname_decl (tree, int);
97 static void initialize_predefined_identifiers (void);
98 static tree check_special_function_return_type
99 (special_function_kind, tree, tree);
100 static tree push_cp_library_fn (enum tree_code, tree);
101 static tree build_cp_library_fn (tree, enum tree_code, tree);
102 static void store_parm_decls (tree);
103 static void initialize_local_var (tree, tree);
104 static void expand_static_init (tree, tree);
105 static tree next_initializable_field (tree);
107 /* The following symbols are subsumed in the cp_global_trees array, and
108 listed here individually for documentation purposes.
111 tree wchar_decl_node;
113 tree vtable_entry_type;
114 tree delta_type_node;
115 tree __t_desc_type_node;
117 tree class_type_node;
118 tree unknown_type_node;
120 Array type `vtable_entry_type[]'
123 tree vtbl_ptr_type_node;
130 A FUNCTION_DECL which can call `abort'. Not necessarily the
131 one that the user will declare, but sufficient to be called
132 by routines that want to abort the program.
136 The FUNCTION_DECL for the default `::operator delete'.
138 tree global_delete_fndecl;
141 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
142 tree tinfo_var_id; */
144 tree cp_global_trees[CPTI_MAX];
146 /* Indicates that there is a type value in some namespace, although
147 that is not necessarily in scope at the moment. */
149 tree global_type_node;
151 /* The node that holds the "name" of the global scope. */
152 tree global_scope_name;
154 #define local_names cp_function_chain->x_local_names
156 /* A list of objects which have constructors or destructors
157 which reside in the global scope. The decl is stored in
158 the TREE_VALUE slot and the initializer is stored
159 in the TREE_PURPOSE slot. */
160 tree static_aggregates;
164 /* A node for the integer constants 2, and 3. */
166 tree integer_two_node, integer_three_node;
168 /* Used only for jumps to as-yet undefined labels, since jumps to
169 defined labels can have their validity checked immediately. */
171 struct named_label_use_entry GTY(())
173 struct named_label_use_entry *next;
174 /* The binding level to which this entry is *currently* attached.
175 This is initially the binding level in which the goto appeared,
176 but is modified as scopes are closed. */
177 struct cp_binding_level *binding_level;
178 /* The head of the names list that was current when the goto appeared,
179 or the inner scope popped. These are the decls that will *not* be
180 skipped when jumping to the label. */
182 /* The location of the goto, for error reporting. */
183 location_t o_goto_locus;
184 /* True if an OpenMP structured block scope has been closed since
185 the goto appeared. This means that the branch from the label will
186 illegally exit an OpenMP scope. */
190 /* A list of all LABEL_DECLs in the function that have names. Here so
191 we can clear out their names' definitions at the end of the
192 function, and so we can check the validity of jumps to these labels. */
194 struct named_label_entry GTY(())
196 /* The decl itself. */
199 /* The binding level to which the label is *currently* attached.
200 This is initially set to the binding level in which the label
201 is defined, but is modified as scopes are closed. */
202 struct cp_binding_level *binding_level;
203 /* The head of the names list that was current when the label was
204 defined, or the inner scope popped. These are the decls that will
205 be skipped when jumping to the label. */
207 /* A tree list of all decls from all binding levels that would be
208 crossed by a backward branch to the label. */
211 /* A list of uses of the label, before the label is defined. */
212 struct named_label_use_entry *uses;
214 /* The following bits are set after the label is defined, and are
215 updated as scopes are popped. They indicate that a backward jump
216 to the label will illegally enter a scope of the given flavor. */
222 #define named_labels cp_function_chain->x_named_labels
224 /* The number of function bodies which we are currently processing.
225 (Zero if we are at namespace scope, one inside the body of a
226 function, two inside the body of a function in a local class, etc.) */
229 /* To avoid unwanted recursion, finish_function defers all mark_used calls
230 encountered during its execution until it finishes. */
231 bool defer_mark_used_calls;
232 VEC(tree, gc) *deferred_mark_used_calls;
234 /* States indicating how grokdeclarator() should handle declspecs marked
235 with __attribute__((deprecated)). An object declared as
236 __attribute__((deprecated)) suppresses warnings of uses of other
238 enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
241 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
242 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
243 time the VAR_DECL was declared, the type was incomplete. */
245 static GTY(()) tree incomplete_vars;
247 /* Returns the kind of template specialization we are currently
248 processing, given that it's declaration contained N_CLASS_SCOPES
249 explicit scope qualifications. */
252 current_tmpl_spec_kind (int n_class_scopes)
254 int n_template_parm_scopes = 0;
255 int seen_specialization_p = 0;
256 int innermost_specialization_p = 0;
257 struct cp_binding_level *b;
259 /* Scan through the template parameter scopes. */
260 for (b = current_binding_level;
261 b->kind == sk_template_parms;
264 /* If we see a specialization scope inside a parameter scope,
265 then something is wrong. That corresponds to a declaration
268 template <class T> template <> ...
270 which is always invalid since [temp.expl.spec] forbids the
271 specialization of a class member template if the enclosing
272 class templates are not explicitly specialized as well. */
273 if (b->explicit_spec_p)
275 if (n_template_parm_scopes == 0)
276 innermost_specialization_p = 1;
278 seen_specialization_p = 1;
280 else if (seen_specialization_p == 1)
281 return tsk_invalid_member_spec;
283 ++n_template_parm_scopes;
286 /* Handle explicit instantiations. */
287 if (processing_explicit_instantiation)
289 if (n_template_parm_scopes != 0)
290 /* We've seen a template parameter list during an explicit
291 instantiation. For example:
293 template <class T> template void f(int);
295 This is erroneous. */
296 return tsk_invalid_expl_inst;
298 return tsk_expl_inst;
301 if (n_template_parm_scopes < n_class_scopes)
302 /* We've not seen enough template headers to match all the
303 specialized classes present. For example:
305 template <class T> void R<T>::S<T>::f(int);
307 This is invalid; there needs to be one set of template
308 parameters for each class. */
309 return tsk_insufficient_parms;
310 else if (n_template_parm_scopes == n_class_scopes)
311 /* We're processing a non-template declaration (even though it may
312 be a member of a template class.) For example:
314 template <class T> void S<T>::f(int);
316 The `class T' matches the `S<T>', leaving no template headers
317 corresponding to the `f'. */
319 else if (n_template_parm_scopes > n_class_scopes + 1)
320 /* We've got too many template headers. For example:
322 template <> template <class T> void f (T);
324 There need to be more enclosing classes. */
325 return tsk_excessive_parms;
327 /* This must be a template. It's of the form:
329 template <class T> template <class U> void S<T>::f(U);
331 This is a specialization if the innermost level was a
332 specialization; otherwise it's just a definition of the
334 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
337 /* Exit the current scope. */
345 /* When a label goes out of scope, check to see if that label was used
346 in a valid manner, and issue any appropriate warnings or errors. */
349 pop_label (tree label, tree old_value)
351 if (!processing_template_decl)
353 if (DECL_INITIAL (label) == NULL_TREE)
357 error ("label %q+D used but not defined", label);
358 location = input_location; /* FIXME want (input_filename, (line)0) */
359 /* Avoid crashing later. */
360 define_label (location, DECL_NAME (label));
363 warn_for_unused_label (label);
366 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
369 /* At the end of a function, all labels declared within the function
370 go out of scope. BLOCK is the top-level block for the
374 pop_labels_1 (void **slot, void *data)
376 struct named_label_entry *ent = (struct named_label_entry *) *slot;
377 tree block = (tree) data;
379 pop_label (ent->label_decl, NULL_TREE);
381 /* Put the labels into the "variables" of the top-level block,
382 so debugger can see them. */
383 TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block);
384 BLOCK_VARS (block) = ent->label_decl;
386 htab_clear_slot (named_labels, slot);
392 pop_labels (tree block)
396 htab_traverse (named_labels, pop_labels_1, block);
401 /* At the end of a block with local labels, restore the outer definition. */
404 pop_local_label (tree label, tree old_value)
406 struct named_label_entry dummy;
409 pop_label (label, old_value);
411 dummy.label_decl = label;
412 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
413 htab_clear_slot (named_labels, slot);
416 /* The following two routines are used to interface to Objective-C++.
417 The binding level is purposely treated as an opaque type. */
420 objc_get_current_scope (void)
422 return current_binding_level;
425 /* The following routine is used by the NeXT-style SJLJ exceptions;
426 variables get marked 'volatile' so as to not be clobbered by
427 _setjmp()/_longjmp() calls. All variables in the current scope,
428 as well as parent scopes up to (but not including) ENCLOSING_BLK
429 shall be thusly marked. */
432 objc_mark_locals_volatile (void *enclosing_blk)
434 struct cp_binding_level *scope;
436 for (scope = current_binding_level;
437 scope && scope != enclosing_blk;
438 scope = scope->level_chain)
442 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
443 objc_volatilize_decl (decl);
445 /* Do not climb up past the current function. */
446 if (scope->kind == sk_function_parms)
451 /* Update data for defined and undefined labels when leaving a scope. */
454 poplevel_named_label_1 (void **slot, void *data)
456 struct named_label_entry *ent = (struct named_label_entry *) *slot;
457 struct cp_binding_level *bl = (struct cp_binding_level *) data;
458 struct cp_binding_level *obl = bl->level_chain;
460 if (ent->binding_level == bl)
464 for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl))
465 if (decl_jump_unsafe (decl))
466 ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls);
468 ent->binding_level = obl;
469 ent->names_in_scope = obl->names;
473 ent->in_try_scope = true;
476 ent->in_catch_scope = true;
479 ent->in_omp_scope = true;
487 struct named_label_use_entry *use;
489 for (use = ent->uses; use ; use = use->next)
490 if (use->binding_level == bl)
492 use->binding_level = obl;
493 use->names_in_scope = obl->names;
494 if (bl->kind == sk_omp)
495 use->in_omp_scope = true;
502 /* Exit a binding level.
503 Pop the level off, and restore the state of the identifier-decl mappings
504 that were in effect when this level was entered.
506 If KEEP == 1, this level had explicit declarations, so
507 and create a "block" (a BLOCK node) for the level
508 to record its declarations and subblocks for symbol table output.
510 If FUNCTIONBODY is nonzero, this level is the body of a function,
511 so create a block as if KEEP were set and also clear out all
514 If REVERSE is nonzero, reverse the order of decls before putting
515 them into the BLOCK. */
518 poplevel (int keep, int reverse, int functionbody)
521 /* The chain of decls was accumulated in reverse order.
522 Put it into forward order, just for cleanliness. */
524 int tmp = functionbody;
525 int real_functionbody;
529 int leaving_for_scope;
532 timevar_push (TV_NAME_LOOKUP);
537 gcc_assert (current_binding_level->kind != sk_class);
539 real_functionbody = (current_binding_level->kind == sk_cleanup
540 ? ((functionbody = 0), tmp) : functionbody);
541 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
543 gcc_assert (!VEC_length(cp_class_binding,
544 current_binding_level->class_shadowed));
546 /* We used to use KEEP == 2 to indicate that the new block should go
547 at the beginning of the list of blocks at this binding level,
548 rather than the end. This hack is no longer used. */
549 gcc_assert (keep == 0 || keep == 1);
551 if (current_binding_level->keep)
554 /* Any uses of undefined labels, and any defined labels, now operate
555 under constraints of next binding contour. */
556 if (cfun && !functionbody && named_labels)
557 htab_traverse (named_labels, poplevel_named_label_1,
558 current_binding_level);
560 /* Get the decls in the order they were written.
561 Usually current_binding_level->names is in reverse order.
562 But parameter decls were previously put in forward order. */
565 current_binding_level->names
566 = decls = nreverse (current_binding_level->names);
568 decls = current_binding_level->names;
570 /* If there were any declarations or structure tags in that level,
571 or if this level is a function body,
572 create a BLOCK to record them for the life of this function. */
574 if (keep == 1 || functionbody)
575 block = make_node (BLOCK);
576 if (block != NULL_TREE)
578 BLOCK_VARS (block) = decls;
579 BLOCK_SUBBLOCKS (block) = subblocks;
582 /* In each subblock, record that this is its superior. */
584 for (link = subblocks; link; link = BLOCK_CHAIN (link))
585 BLOCK_SUPERCONTEXT (link) = block;
587 /* We still support the old for-scope rules, whereby the variables
588 in a for-init statement were in scope after the for-statement
589 ended. We only use the new rules if flag_new_for_scope is
592 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
594 /* Before we remove the declarations first check for unused variables. */
595 if (warn_unused_variable
596 && !processing_template_decl)
597 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
598 if (TREE_CODE (decl) == VAR_DECL
599 && ! TREE_USED (decl)
600 && ! DECL_IN_SYSTEM_HEADER (decl)
601 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
602 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
604 /* Remove declarations for all the DECLs in this level. */
605 for (link = decls; link; link = TREE_CHAIN (link))
607 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
610 tree name = DECL_NAME (link);
614 ob = outer_binding (name,
615 IDENTIFIER_BINDING (name),
618 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
620 ns_binding = NULL_TREE;
622 if (ob && ob->scope == current_binding_level->level_chain)
623 /* We have something like:
628 and we are leaving the `for' scope. There's no reason to
629 keep the binding of the inner `i' in this case. */
630 pop_binding (name, link);
631 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
632 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
633 /* Here, we have something like:
641 We must pop the for-scope binding so we know what's a
642 type and what isn't. */
643 pop_binding (name, link);
646 /* Mark this VAR_DECL as dead so that we can tell we left it
647 there only for backward compatibility. */
648 DECL_DEAD_FOR_LOCAL (link) = 1;
650 /* Keep track of what should have happened when we
651 popped the binding. */
654 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
655 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
658 /* Add it to the list of dead variables in the next
659 outermost binding to that we can remove these when we
660 leave that binding. */
661 current_binding_level->level_chain->dead_vars_from_for
662 = tree_cons (NULL_TREE, link,
663 current_binding_level->level_chain->
666 /* Although we don't pop the cxx_binding, we do clear
667 its SCOPE since the scope is going away now. */
668 IDENTIFIER_BINDING (name)->scope
669 = current_binding_level->level_chain;
676 /* Remove the binding. */
679 if (TREE_CODE (decl) == TREE_LIST)
680 decl = TREE_VALUE (decl);
683 if (TREE_CODE (name) == OVERLOAD)
684 name = OVL_FUNCTION (name);
686 gcc_assert (DECL_P (name));
687 pop_binding (DECL_NAME (name), decl);
691 /* Remove declarations for any `for' variables from inner scopes
692 that we kept around. */
693 for (link = current_binding_level->dead_vars_from_for;
694 link; link = TREE_CHAIN (link))
695 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
697 /* Restore the IDENTIFIER_TYPE_VALUEs. */
698 for (link = current_binding_level->type_shadowed;
699 link; link = TREE_CHAIN (link))
700 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
702 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
703 for (link = current_binding_level->shadowed_labels;
705 link = TREE_CHAIN (link))
706 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
708 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
709 list if a `using' declaration put them there. The debugging
710 back ends won't understand OVERLOAD, so we remove them here.
711 Because the BLOCK_VARS are (temporarily) shared with
712 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
713 popped all the bindings. */
718 for (d = &BLOCK_VARS (block); *d; )
720 if (TREE_CODE (*d) == TREE_LIST)
721 *d = TREE_CHAIN (*d);
723 d = &TREE_CHAIN (*d);
727 /* If the level being exited is the top level of a function,
728 check over all the labels. */
731 /* Since this is the top level block of a function, the vars are
732 the function's parameters. Don't leave them in the BLOCK
733 because they are found in the FUNCTION_DECL instead. */
734 BLOCK_VARS (block) = 0;
738 kind = current_binding_level->kind;
739 if (kind == sk_cleanup)
743 /* If this is a temporary binding created for a cleanup, then we'll
744 have pushed a statement list level. Pop that, create a new
745 BIND_EXPR for the block, and insert it into the stream. */
746 stmt = pop_stmt_list (current_binding_level->statement_list);
747 stmt = c_build_bind_expr (block, stmt);
754 /* The current function is being defined, so its DECL_INITIAL
755 should be error_mark_node. */
756 gcc_assert (DECL_INITIAL (current_function_decl) == error_mark_node);
757 DECL_INITIAL (current_function_decl) = block;
760 current_binding_level->blocks
761 = chainon (current_binding_level->blocks, block);
763 /* If we did not make a block for the level just exited,
764 any blocks made for inner levels
765 (since they cannot be recorded as subblocks in that level)
766 must be carried forward so they will later become subblocks
767 of something else. */
769 current_binding_level->blocks
770 = chainon (current_binding_level->blocks, subblocks);
772 /* Each and every BLOCK node created here in `poplevel' is important
773 (e.g. for proper debugging information) so if we created one
774 earlier, mark it as "used". */
776 TREE_USED (block) = 1;
778 /* All temporary bindings created for cleanups are popped silently. */
779 if (kind == sk_cleanup)
782 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
785 /* Insert BLOCK at the end of the list of subblocks of the
786 current binding level. This is used when a BIND_EXPR is expanded,
787 to handle the BLOCK node inside the BIND_EXPR. */
790 insert_block (tree block)
792 TREE_USED (block) = 1;
793 current_binding_level->blocks
794 = chainon (current_binding_level->blocks, block);
797 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
798 itself, calling F for each. The DATA is passed to F as well. */
801 walk_namespaces_r (tree name_space, walk_namespaces_fn f, void* data)
804 tree current = NAMESPACE_LEVEL (name_space)->namespaces;
806 result |= (*f) (name_space, data);
808 for (; current; current = TREE_CHAIN (current))
809 result |= walk_namespaces_r (current, f, data);
814 /* Walk all the namespaces, calling F for each. The DATA is passed to
818 walk_namespaces (walk_namespaces_fn f, void* data)
820 return walk_namespaces_r (global_namespace, f, data);
823 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
824 DATA is non-NULL, this is the last time we will call
825 wrapup_global_declarations for this NAMESPACE. */
828 wrapup_globals_for_namespace (tree name_space, void* data)
830 struct cp_binding_level *level = NAMESPACE_LEVEL (name_space);
831 VEC(tree,gc) *statics = level->static_decls;
832 tree *vec = VEC_address (tree, statics);
833 int len = VEC_length (tree, statics);
834 int last_time = (data != 0);
838 check_global_declarations (vec, len);
839 emit_debug_global_declarations (vec, len);
843 /* Write out any globals that need to be output. */
844 return wrapup_global_declarations (vec, len);
848 /* In C++, you don't have to write `struct S' to refer to `S'; you
849 can just use `S'. We accomplish this by creating a TYPE_DECL as
850 if the user had written `typedef struct S S'. Create and return
851 the TYPE_DECL for TYPE. */
854 create_implicit_typedef (tree name, tree type)
858 decl = build_decl (TYPE_DECL, name, type);
859 DECL_ARTIFICIAL (decl) = 1;
860 /* There are other implicit type declarations, like the one *within*
861 a class that allows you to write `S::S'. We must distinguish
863 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
864 TYPE_NAME (type) = decl;
869 /* Remember a local name for name-mangling purposes. */
872 push_local_name (tree decl)
877 timevar_push (TV_NAME_LOOKUP);
879 name = DECL_NAME (decl);
881 nelts = VEC_length (tree, local_names);
882 for (i = 0; i < nelts; i++)
884 t = VEC_index (tree, local_names, i);
885 if (DECL_NAME (t) == name)
887 if (!DECL_LANG_SPECIFIC (decl))
888 retrofit_lang_decl (decl);
889 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
890 if (DECL_LANG_SPECIFIC (t))
891 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
893 DECL_DISCRIMINATOR (decl) = 1;
895 VEC_replace (tree, local_names, i, decl);
896 timevar_pop (TV_NAME_LOOKUP);
901 VEC_safe_push (tree, gc, local_names, decl);
902 timevar_pop (TV_NAME_LOOKUP);
905 /* Subroutine of duplicate_decls: return truthvalue of whether
906 or not types of these decls match.
908 For C++, we must compare the parameter list so that `int' can match
909 `int&' in a parameter position, but `int&' is not confused with
913 decls_match (tree newdecl, tree olddecl)
917 if (newdecl == olddecl)
920 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
921 /* If the two DECLs are not even the same kind of thing, we're not
922 interested in their types. */
925 if (TREE_CODE (newdecl) == FUNCTION_DECL)
927 tree f1 = TREE_TYPE (newdecl);
928 tree f2 = TREE_TYPE (olddecl);
929 tree p1 = TYPE_ARG_TYPES (f1);
930 tree p2 = TYPE_ARG_TYPES (f2);
932 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
933 && ! (DECL_EXTERN_C_P (newdecl)
934 && DECL_EXTERN_C_P (olddecl)))
937 if (TREE_CODE (f1) != TREE_CODE (f2))
940 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
942 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
943 && (DECL_BUILT_IN (olddecl)
944 #ifndef NO_IMPLICIT_EXTERN_C
945 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
946 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
950 types_match = self_promoting_args_p (p1);
951 if (p1 == void_list_node)
952 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
954 #ifndef NO_IMPLICIT_EXTERN_C
955 else if (p1 == NULL_TREE
956 && (DECL_EXTERN_C_P (olddecl)
957 && DECL_IN_SYSTEM_HEADER (olddecl)
958 && !DECL_CLASS_SCOPE_P (olddecl))
959 && (DECL_EXTERN_C_P (newdecl)
960 && DECL_IN_SYSTEM_HEADER (newdecl)
961 && !DECL_CLASS_SCOPE_P (newdecl)))
963 types_match = self_promoting_args_p (p2);
964 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
968 types_match = compparms (p1, p2);
973 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
975 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
976 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
979 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
980 DECL_TEMPLATE_PARMS (olddecl)))
983 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
984 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
985 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
987 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
988 DECL_TEMPLATE_RESULT (newdecl));
992 /* Need to check scope for variable declaration (VAR_DECL).
993 For typedef (TYPE_DECL), scope is ignored. */
994 if (TREE_CODE (newdecl) == VAR_DECL
995 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
997 Two declarations for an object with C language linkage
998 with the same name (ignoring the namespace that qualify
999 it) that appear in different namespace scopes refer to
1001 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1004 if (TREE_TYPE (newdecl) == error_mark_node)
1005 types_match = TREE_TYPE (olddecl) == error_mark_node;
1006 else if (TREE_TYPE (olddecl) == NULL_TREE)
1007 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1008 else if (TREE_TYPE (newdecl) == NULL_TREE)
1011 types_match = comptypes (TREE_TYPE (newdecl),
1012 TREE_TYPE (olddecl),
1013 COMPARE_REDECLARATION);
1019 /* If NEWDECL is `static' and an `extern' was seen previously,
1020 warn about it. OLDDECL is the previous declaration.
1022 Note that this does not apply to the C++ case of declaring
1023 a variable `extern const' and then later `const'.
1025 Don't complain about built-in functions, since they are beyond
1026 the user's control. */
1029 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1033 if (TREE_CODE (newdecl) == TYPE_DECL
1034 || TREE_CODE (newdecl) == TEMPLATE_DECL
1035 || TREE_CODE (newdecl) == CONST_DECL
1036 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1039 /* Don't get confused by static member functions; that's a different
1041 if (TREE_CODE (newdecl) == FUNCTION_DECL
1042 && DECL_STATIC_FUNCTION_P (newdecl))
1045 /* If the old declaration was `static', or the new one isn't, then
1046 then everything is OK. */
1047 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1050 /* It's OK to declare a builtin function as `static'. */
1051 if (TREE_CODE (olddecl) == FUNCTION_DECL
1052 && DECL_ARTIFICIAL (olddecl))
1055 name = DECL_ASSEMBLER_NAME (newdecl);
1056 permerror (input_location, "%qD was declared %<extern%> and later %<static%>", newdecl);
1057 permerror (input_location, "previous declaration of %q+D", olddecl);
1060 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1061 function templates. If their exception specifications do not
1062 match, issue a diagnostic. */
1065 check_redeclaration_exception_specification (tree new_decl,
1070 tree new_exceptions;
1071 tree old_exceptions;
1073 new_type = TREE_TYPE (new_decl);
1074 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1075 old_type = TREE_TYPE (old_decl);
1076 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1080 If any declaration of a function has an exception-specification,
1081 all declarations, including the definition and an explicit
1082 specialization, of that function shall have an
1083 exception-specification with the same set of type-ids. */
1084 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1085 && ! DECL_IS_BUILTIN (old_decl)
1087 && !comp_except_specs (new_exceptions, old_exceptions,
1090 error ("declaration of %qF throws different exceptions", new_decl);
1091 error ("from previous declaration %q+F", old_decl);
1095 #define GNU_INLINE_P(fn) (DECL_DECLARED_INLINE_P (fn) \
1096 && lookup_attribute ("gnu_inline", \
1097 DECL_ATTRIBUTES (fn)))
1099 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1100 If the redeclaration is invalid, a diagnostic is issued, and the
1101 error_mark_node is returned. Otherwise, OLDDECL is returned.
1103 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1106 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1109 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1111 unsigned olddecl_uid = DECL_UID (olddecl);
1112 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1113 int new_defines_function = 0;
1116 if (newdecl == olddecl)
1119 types_match = decls_match (newdecl, olddecl);
1121 /* If either the type of the new decl or the type of the old decl is an
1122 error_mark_node, then that implies that we have already issued an
1123 error (earlier) for some bogus type specification, and in that case,
1124 it is rather pointless to harass the user with yet more error message
1125 about the same declaration, so just pretend the types match here. */
1126 if (TREE_TYPE (newdecl) == error_mark_node
1127 || TREE_TYPE (olddecl) == error_mark_node)
1128 return error_mark_node;
1130 if (DECL_P (olddecl)
1131 && TREE_CODE (newdecl) == FUNCTION_DECL
1132 && TREE_CODE (olddecl) == FUNCTION_DECL
1133 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1135 if (DECL_DECLARED_INLINE_P (newdecl)
1136 && DECL_UNINLINABLE (newdecl)
1137 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1138 /* Already warned elsewhere. */;
1139 else if (DECL_DECLARED_INLINE_P (olddecl)
1140 && DECL_UNINLINABLE (olddecl)
1141 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1142 /* Already warned. */;
1143 else if (DECL_DECLARED_INLINE_P (newdecl)
1144 && DECL_UNINLINABLE (olddecl)
1145 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1147 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1149 warning (OPT_Wattributes, "previous declaration of %q+D "
1150 "with attribute noinline", olddecl);
1152 else if (DECL_DECLARED_INLINE_P (olddecl)
1153 && DECL_UNINLINABLE (newdecl)
1154 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1156 warning (OPT_Wattributes, "function %q+D redeclared with "
1157 "attribute noinline", newdecl);
1158 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1163 /* Check for redeclaration and other discrepancies. */
1164 if (TREE_CODE (olddecl) == FUNCTION_DECL
1165 && DECL_ARTIFICIAL (olddecl))
1167 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1168 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1170 /* Avoid warnings redeclaring built-ins which have not been
1171 explicitly declared. */
1172 if (DECL_ANTICIPATED (olddecl))
1175 /* If you declare a built-in or predefined function name as static,
1176 the old definition is overridden, but optionally warn this was a
1177 bad choice of name. */
1178 if (! TREE_PUBLIC (newdecl))
1180 warning (OPT_Wshadow, "shadowing %s function %q#D",
1181 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1183 /* Discard the old built-in function. */
1186 /* If the built-in is not ansi, then programs can override
1187 it even globally without an error. */
1188 else if (! DECL_BUILT_IN (olddecl))
1189 warning (0, "library function %q#D redeclared as non-function %q#D",
1193 error ("declaration of %q#D", newdecl);
1194 error ("conflicts with built-in declaration %q#D",
1199 else if (!types_match)
1201 /* Avoid warnings redeclaring built-ins which have not been
1202 explicitly declared. */
1203 if (DECL_ANTICIPATED (olddecl))
1205 /* Deal with fileptr_type_node. FILE type is not known
1206 at the time we create the builtins. */
1209 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1210 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1212 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1215 else if (TREE_VALUE (t2) == fileptr_type_node)
1217 tree t = TREE_VALUE (t1);
1219 if (TREE_CODE (t) == POINTER_TYPE
1220 && TYPE_NAME (TREE_TYPE (t))
1221 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1222 == get_identifier ("FILE")
1223 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1225 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1227 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1228 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1229 types_match = decls_match (newdecl, olddecl);
1231 return duplicate_decls (newdecl, olddecl,
1233 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1236 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1239 else if ((DECL_EXTERN_C_P (newdecl)
1240 && DECL_EXTERN_C_P (olddecl))
1241 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1242 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1244 /* A near match; override the builtin. */
1246 if (TREE_PUBLIC (newdecl))
1248 warning (0, "new declaration %q#D", newdecl);
1249 warning (0, "ambiguates built-in declaration %q#D",
1253 warning (OPT_Wshadow, "shadowing %s function %q#D",
1254 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1258 /* Discard the old built-in function. */
1261 /* Replace the old RTL to avoid problems with inlining. */
1262 COPY_DECL_RTL (newdecl, olddecl);
1264 /* Even if the types match, prefer the new declarations type for
1265 built-ins which have not been explicitly declared, for
1266 exception lists, etc... */
1267 else if (DECL_ANTICIPATED (olddecl))
1269 tree type = TREE_TYPE (newdecl);
1270 tree attribs = (*targetm.merge_type_attributes)
1271 (TREE_TYPE (olddecl), type);
1273 type = cp_build_type_attribute_variant (type, attribs);
1274 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1277 /* If a function is explicitly declared "throw ()", propagate that to
1278 the corresponding builtin. */
1279 if (DECL_BUILT_IN_CLASS (olddecl) == BUILT_IN_NORMAL
1280 && DECL_ANTICIPATED (olddecl)
1281 && TREE_NOTHROW (newdecl)
1282 && !TREE_NOTHROW (olddecl)
1283 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != NULL_TREE
1284 && built_in_decls [DECL_FUNCTION_CODE (olddecl)] != olddecl
1286 TREE_NOTHROW (built_in_decls [DECL_FUNCTION_CODE (olddecl)]) = 1;
1288 /* Whether or not the builtin can throw exceptions has no
1289 bearing on this declarator. */
1290 TREE_NOTHROW (olddecl) = 0;
1292 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1294 /* If a builtin function is redeclared as `static', merge
1295 the declarations, but make the original one static. */
1296 DECL_THIS_STATIC (olddecl) = 1;
1297 TREE_PUBLIC (olddecl) = 0;
1299 /* Make the old declaration consistent with the new one so
1300 that all remnants of the builtin-ness of this function
1301 will be banished. */
1302 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1303 COPY_DECL_RTL (newdecl, olddecl);
1306 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1308 /* C++ Standard, 3.3, clause 4:
1309 "[Note: a namespace name or a class template name must be unique
1310 in its declarative region (7.3.2, clause 14). ]" */
1311 if (TREE_CODE (olddecl) != NAMESPACE_DECL
1312 && TREE_CODE (newdecl) != NAMESPACE_DECL
1313 && (TREE_CODE (olddecl) != TEMPLATE_DECL
1314 || TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) != TYPE_DECL)
1315 && (TREE_CODE (newdecl) != TEMPLATE_DECL
1316 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != TYPE_DECL))
1318 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1319 && TREE_CODE (newdecl) != TYPE_DECL)
1320 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1321 && TREE_CODE (olddecl) != TYPE_DECL))
1323 /* We do nothing special here, because C++ does such nasty
1324 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1325 get shadowed, and know that if we need to find a TYPE_DECL
1326 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1327 slot of the identifier. */
1331 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1332 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1333 || (TREE_CODE (olddecl) == FUNCTION_DECL
1334 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1338 error ("%q#D redeclared as different kind of symbol", newdecl);
1339 if (TREE_CODE (olddecl) == TREE_LIST)
1340 olddecl = TREE_VALUE (olddecl);
1341 error ("previous declaration of %q+#D", olddecl);
1343 return error_mark_node;
1345 else if (!types_match)
1347 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1348 /* These are certainly not duplicate declarations; they're
1349 from different scopes. */
1352 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1354 /* The name of a class template may not be declared to refer to
1355 any other template, class, function, object, namespace, value,
1356 or type in the same scope. */
1357 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1358 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1360 error ("declaration of template %q#D", newdecl);
1361 error ("conflicts with previous declaration %q+#D", olddecl);
1363 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1364 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1365 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1366 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1367 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1368 DECL_TEMPLATE_PARMS (olddecl))
1369 /* Template functions can be disambiguated by
1371 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1372 TREE_TYPE (TREE_TYPE (olddecl))))
1374 error ("new declaration %q#D", newdecl);
1375 error ("ambiguates old declaration %q+#D", olddecl);
1379 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1381 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1383 error ("declaration of C function %q#D conflicts with",
1385 error ("previous declaration %q+#D here", olddecl);
1387 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1388 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1390 error ("new declaration %q#D", newdecl);
1391 error ("ambiguates old declaration %q+#D", olddecl);
1392 return error_mark_node;
1399 error ("conflicting declaration %q#D", newdecl);
1400 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1401 return error_mark_node;
1404 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1405 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1406 && (!DECL_TEMPLATE_INFO (newdecl)
1407 || (DECL_TI_TEMPLATE (newdecl)
1408 != DECL_TI_TEMPLATE (olddecl))))
1409 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1410 && (!DECL_TEMPLATE_INFO (olddecl)
1411 || (DECL_TI_TEMPLATE (olddecl)
1412 != DECL_TI_TEMPLATE (newdecl))))))
1413 /* It's OK to have a template specialization and a non-template
1414 with the same type, or to have specializations of two
1415 different templates with the same type. Note that if one is a
1416 specialization, and the other is an instantiation of the same
1417 template, that we do not exit at this point. That situation
1418 can occur if we instantiate a template class, and then
1419 specialize one of its methods. This situation is valid, but
1420 the declarations must be merged in the usual way. */
1422 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1423 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1424 && !DECL_USE_TEMPLATE (newdecl))
1425 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1426 && !DECL_USE_TEMPLATE (olddecl))))
1427 /* One of the declarations is a template instantiation, and the
1428 other is not a template at all. That's OK. */
1430 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1432 /* In [namespace.alias] we have:
1434 In a declarative region, a namespace-alias-definition can be
1435 used to redefine a namespace-alias declared in that declarative
1436 region to refer only to the namespace to which it already
1439 Therefore, if we encounter a second alias directive for the same
1440 alias, we can just ignore the second directive. */
1441 if (DECL_NAMESPACE_ALIAS (newdecl)
1442 && (DECL_NAMESPACE_ALIAS (newdecl)
1443 == DECL_NAMESPACE_ALIAS (olddecl)))
1445 /* [namespace.alias]
1447 A namespace-name or namespace-alias shall not be declared as
1448 the name of any other entity in the same declarative region.
1449 A namespace-name defined at global scope shall not be
1450 declared as the name of any other entity in any global scope
1452 error ("declaration of namespace %qD conflicts with", newdecl);
1453 error ("previous declaration of namespace %q+D here", olddecl);
1454 return error_mark_node;
1458 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1461 error_at (DECL_SOURCE_LOCATION (newdecl), errmsg, newdecl);
1462 if (DECL_NAME (olddecl) != NULL_TREE)
1463 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1464 ? "%q+#D previously defined here"
1465 : "%q+#D previously declared here", olddecl);
1466 return error_mark_node;
1468 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1469 && DECL_INITIAL (olddecl) != NULL_TREE
1470 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1471 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1473 /* Prototype decl follows defn w/o prototype. */
1474 warning (0, "prototype for %q+#D", newdecl);
1475 warning (0, "%Jfollows non-prototype definition here", olddecl);
1477 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1478 || TREE_CODE (olddecl) == VAR_DECL)
1479 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1482 If two declarations of the same function or object
1483 specify different linkage-specifications ..., the program
1484 is ill-formed.... Except for functions with C++ linkage,
1485 a function declaration without a linkage specification
1486 shall not precede the first linkage specification for
1487 that function. A function can be declared without a
1488 linkage specification after an explicit linkage
1489 specification has been seen; the linkage explicitly
1490 specified in the earlier declaration is not affected by
1491 such a function declaration.
1493 DR 563 raises the question why the restrictions on
1494 functions should not also apply to objects. Older
1495 versions of G++ silently ignore the linkage-specification
1503 which is clearly wrong. Therefore, we now treat objects
1505 if (current_lang_depth () == 0)
1507 /* There is no explicit linkage-specification, so we use
1508 the linkage from the previous declaration. */
1509 if (!DECL_LANG_SPECIFIC (newdecl))
1510 retrofit_lang_decl (newdecl);
1511 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1515 error ("previous declaration of %q+#D with %qL linkage",
1516 olddecl, DECL_LANGUAGE (olddecl));
1517 error ("conflicts with new declaration with %qL linkage",
1518 DECL_LANGUAGE (newdecl));
1522 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1524 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1526 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1527 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1530 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1531 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1533 for (; t1 && t1 != void_list_node;
1534 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1535 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1537 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1540 permerror (input_location, "default argument given for parameter %d of %q#D",
1542 permerror (input_location, "after previous specification in %q+#D", olddecl);
1546 error ("default argument given for parameter %d of %q#D",
1548 error ("after previous specification in %q+#D",
1555 /* Do not merge an implicit typedef with an explicit one. In:
1559 typedef class A A __attribute__ ((foo));
1561 the attribute should apply only to the typedef. */
1562 if (TREE_CODE (olddecl) == TYPE_DECL
1563 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1564 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1567 /* If new decl is `static' and an `extern' was seen previously,
1569 warn_extern_redeclared_static (newdecl, olddecl);
1571 /* We have committed to returning 1 at this point. */
1572 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1574 /* Now that functions must hold information normally held
1575 by field decls, there is extra work to do so that
1576 declaration information does not get destroyed during
1578 if (DECL_VINDEX (olddecl))
1579 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1580 if (DECL_CONTEXT (olddecl))
1581 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1582 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1583 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1584 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1585 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1586 DECL_INVALID_OVERRIDER_P (newdecl) |= DECL_INVALID_OVERRIDER_P (olddecl);
1587 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1588 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1589 SET_OVERLOADED_OPERATOR_CODE
1590 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1591 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1593 /* Optionally warn about more than one declaration for the same
1594 name, but don't warn about a function declaration followed by a
1596 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1597 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1598 /* Don't warn about extern decl followed by definition. */
1599 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1600 /* Don't warn about friends, let add_friend take care of it. */
1601 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1603 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1604 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1607 if (DECL_DELETED_FN (newdecl))
1609 error ("deleted definition of %qD", newdecl);
1610 error ("after previous declaration %q+D", olddecl);
1614 /* Deal with C++: must preserve virtual function table size. */
1615 if (TREE_CODE (olddecl) == TYPE_DECL)
1617 tree newtype = TREE_TYPE (newdecl);
1618 tree oldtype = TREE_TYPE (olddecl);
1620 if (newtype != error_mark_node && oldtype != error_mark_node
1621 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1622 CLASSTYPE_FRIEND_CLASSES (newtype)
1623 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1625 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1628 /* Copy all the DECL_... slots specified in the new decl
1629 except for any that we copy here from the old type. */
1630 DECL_ATTRIBUTES (newdecl)
1631 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1633 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1637 old_result = DECL_TEMPLATE_RESULT (olddecl);
1638 new_result = DECL_TEMPLATE_RESULT (newdecl);
1639 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1640 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1641 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1642 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1644 DECL_ATTRIBUTES (old_result)
1645 = (*targetm.merge_decl_attributes) (old_result, new_result);
1647 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1649 if (GNU_INLINE_P (old_result) != GNU_INLINE_P (new_result)
1650 && DECL_INITIAL (new_result))
1652 if (DECL_INITIAL (old_result))
1653 DECL_UNINLINABLE (old_result) = 1;
1655 DECL_UNINLINABLE (old_result) = DECL_UNINLINABLE (new_result);
1656 DECL_EXTERNAL (old_result) = DECL_EXTERNAL (new_result);
1657 DECL_NOT_REALLY_EXTERN (old_result)
1658 = DECL_NOT_REALLY_EXTERN (new_result);
1659 DECL_INTERFACE_KNOWN (old_result)
1660 = DECL_INTERFACE_KNOWN (new_result);
1661 DECL_DECLARED_INLINE_P (old_result)
1662 = DECL_DECLARED_INLINE_P (new_result);
1663 DECL_DISREGARD_INLINE_LIMITS (old_result)
1664 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1669 DECL_DECLARED_INLINE_P (old_result)
1670 |= DECL_DECLARED_INLINE_P (new_result);
1671 DECL_DISREGARD_INLINE_LIMITS (old_result)
1672 |= DECL_DISREGARD_INLINE_LIMITS (new_result);
1673 check_redeclaration_exception_specification (newdecl, olddecl);
1677 /* If the new declaration is a definition, update the file and
1678 line information on the declaration, and also make
1679 the old declaration the same definition. */
1680 if (DECL_INITIAL (new_result) != NULL_TREE)
1682 DECL_SOURCE_LOCATION (olddecl)
1683 = DECL_SOURCE_LOCATION (old_result)
1684 = DECL_SOURCE_LOCATION (newdecl);
1685 DECL_INITIAL (old_result) = DECL_INITIAL (new_result);
1686 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1689 DECL_ARGUMENTS (old_result)
1690 = DECL_ARGUMENTS (new_result);
1691 for (parm = DECL_ARGUMENTS (old_result); parm;
1692 parm = TREE_CHAIN (parm))
1693 DECL_CONTEXT (parm) = old_result;
1702 /* Automatically handles default parameters. */
1703 tree oldtype = TREE_TYPE (olddecl);
1706 /* Merge the data types specified in the two decls. */
1707 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1709 /* If merge_types produces a non-typedef type, just use the old type. */
1710 if (TREE_CODE (newdecl) == TYPE_DECL
1711 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1714 if (TREE_CODE (newdecl) == VAR_DECL)
1716 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1717 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1718 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1719 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1720 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1721 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1723 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1724 if (DECL_LANG_SPECIFIC (olddecl)
1725 && CP_DECL_THREADPRIVATE_P (olddecl))
1727 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1728 if (!DECL_LANG_SPECIFIC (newdecl))
1729 retrofit_lang_decl (newdecl);
1731 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1732 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1736 /* Do this after calling `merge_types' so that default
1737 parameters don't confuse us. */
1738 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1739 check_redeclaration_exception_specification (newdecl, olddecl);
1740 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1742 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1743 check_default_args (newdecl);
1745 /* Lay the type out, unless already done. */
1746 if (! same_type_p (newtype, oldtype)
1747 && TREE_TYPE (newdecl) != error_mark_node
1748 && !(processing_template_decl && uses_template_parms (newdecl)))
1749 layout_type (TREE_TYPE (newdecl));
1751 if ((TREE_CODE (newdecl) == VAR_DECL
1752 || TREE_CODE (newdecl) == PARM_DECL
1753 || TREE_CODE (newdecl) == RESULT_DECL
1754 || TREE_CODE (newdecl) == FIELD_DECL
1755 || TREE_CODE (newdecl) == TYPE_DECL)
1756 && !(processing_template_decl && uses_template_parms (newdecl)))
1757 layout_decl (newdecl, 0);
1759 /* Merge the type qualifiers. */
1760 if (TREE_READONLY (newdecl))
1761 TREE_READONLY (olddecl) = 1;
1762 if (TREE_THIS_VOLATILE (newdecl))
1763 TREE_THIS_VOLATILE (olddecl) = 1;
1764 if (TREE_NOTHROW (newdecl))
1765 TREE_NOTHROW (olddecl) = 1;
1767 /* Merge deprecatedness. */
1768 if (TREE_DEPRECATED (newdecl))
1769 TREE_DEPRECATED (olddecl) = 1;
1771 /* Preserve function specific target and optimization options */
1772 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1774 if (DECL_FUNCTION_SPECIFIC_TARGET (olddecl)
1775 && !DECL_FUNCTION_SPECIFIC_TARGET (newdecl))
1776 DECL_FUNCTION_SPECIFIC_TARGET (newdecl)
1777 = DECL_FUNCTION_SPECIFIC_TARGET (olddecl);
1779 if (DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl)
1780 && !DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl))
1781 DECL_FUNCTION_SPECIFIC_OPTIMIZATION (newdecl)
1782 = DECL_FUNCTION_SPECIFIC_OPTIMIZATION (olddecl);
1785 /* Merge the initialization information. */
1786 if (DECL_INITIAL (newdecl) == NULL_TREE
1787 && DECL_INITIAL (olddecl) != NULL_TREE)
1789 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1790 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1791 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1792 && DECL_LANG_SPECIFIC (newdecl)
1793 && DECL_LANG_SPECIFIC (olddecl))
1795 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1796 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1800 /* Merge the section attribute.
1801 We want to issue an error if the sections conflict but that must be
1802 done later in decl_attributes since we are called before attributes
1804 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1805 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1807 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1809 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1810 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1811 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1812 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1813 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1814 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1815 DECL_IS_OPERATOR_NEW (newdecl) |= DECL_IS_OPERATOR_NEW (olddecl);
1816 DECL_PURE_P (newdecl) |= DECL_PURE_P (olddecl);
1817 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1818 DECL_LOOPING_CONST_OR_PURE_P (newdecl)
1819 |= DECL_LOOPING_CONST_OR_PURE_P (olddecl);
1820 /* Keep the old RTL. */
1821 COPY_DECL_RTL (olddecl, newdecl);
1823 else if (TREE_CODE (newdecl) == VAR_DECL
1824 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1826 /* Keep the old RTL. We cannot keep the old RTL if the old
1827 declaration was for an incomplete object and the new
1828 declaration is not since many attributes of the RTL will
1830 COPY_DECL_RTL (olddecl, newdecl);
1833 /* If cannot merge, then use the new type and qualifiers,
1834 and don't preserve the old rtl. */
1837 /* Clean out any memory we had of the old declaration. */
1838 tree oldstatic = value_member (olddecl, static_aggregates);
1840 TREE_VALUE (oldstatic) = error_mark_node;
1842 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1843 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1844 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1845 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1848 /* Merge the storage class information. */
1849 merge_weak (newdecl, olddecl);
1851 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1852 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1853 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1854 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1855 if (! DECL_EXTERNAL (olddecl))
1856 DECL_EXTERNAL (newdecl) = 0;
1858 new_template = NULL_TREE;
1859 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1861 bool new_redefines_gnu_inline = false;
1863 if (new_defines_function
1864 && ((DECL_INTERFACE_KNOWN (olddecl)
1865 && TREE_CODE (olddecl) == FUNCTION_DECL)
1866 || (TREE_CODE (olddecl) == TEMPLATE_DECL
1867 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1868 == FUNCTION_DECL))))
1872 if (TREE_CODE (fn) == TEMPLATE_DECL)
1873 fn = DECL_TEMPLATE_RESULT (olddecl);
1875 new_redefines_gnu_inline = GNU_INLINE_P (fn) && DECL_INITIAL (fn);
1878 if (!new_redefines_gnu_inline)
1880 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1881 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1882 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1884 DECL_TEMPLATE_INSTANTIATED (newdecl)
1885 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1887 /* If the OLDDECL is an instantiation and/or specialization,
1888 then the NEWDECL must be too. But, it may not yet be marked
1889 as such if the caller has created NEWDECL, but has not yet
1890 figured out that it is a redeclaration. */
1891 if (!DECL_USE_TEMPLATE (newdecl))
1892 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1894 /* Don't really know how much of the language-specific
1895 values we should copy from old to new. */
1896 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1897 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1898 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1899 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1900 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1901 if (DECL_TEMPLATE_INFO (newdecl))
1902 new_template = DECL_TI_TEMPLATE (newdecl);
1903 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1904 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1905 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1906 olddecl_friend = DECL_FRIEND_P (olddecl);
1907 hidden_friend = (DECL_ANTICIPATED (olddecl)
1908 && DECL_HIDDEN_FRIEND_P (olddecl)
1909 && newdecl_is_friend);
1911 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1912 if (TREE_CODE (newdecl) == FUNCTION_DECL
1913 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1915 DECL_BEFRIENDING_CLASSES (newdecl)
1916 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1917 DECL_BEFRIENDING_CLASSES (olddecl));
1918 /* DECL_THUNKS is only valid for virtual functions,
1919 otherwise it is a DECL_FRIEND_CONTEXT. */
1920 if (DECL_VIRTUAL_P (newdecl))
1921 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1925 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1929 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1930 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1932 /* If newdecl is not a specialization, then it is not a
1933 template-related function at all. And that means that we
1934 should have exited above, returning 0. */
1935 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1937 if (TREE_USED (olddecl))
1938 /* From [temp.expl.spec]:
1940 If a template, a member template or the member of a class
1941 template is explicitly specialized then that
1942 specialization shall be declared before the first use of
1943 that specialization that would cause an implicit
1944 instantiation to take place, in every translation unit in
1945 which such a use occurs. */
1946 error ("explicit specialization of %qD after first use",
1949 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1951 /* Don't propagate visibility from the template to the
1952 specialization here. We'll do that in determine_visibility if
1954 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1956 /* [temp.expl.spec/14] We don't inline explicit specialization
1957 just because the primary template says so. */
1959 else if (new_defines_function && DECL_INITIAL (olddecl))
1961 /* Never inline re-defined extern inline functions.
1962 FIXME: this could be better handled by keeping both
1963 function as separate declarations. */
1964 DECL_UNINLINABLE (newdecl) = 1;
1968 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1969 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1971 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1973 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1974 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1976 DECL_DISREGARD_INLINE_LIMITS (newdecl)
1977 = DECL_DISREGARD_INLINE_LIMITS (olddecl)
1978 = (DECL_DISREGARD_INLINE_LIMITS (newdecl)
1979 || DECL_DISREGARD_INLINE_LIMITS (olddecl));
1982 /* Preserve abstractness on cloned [cd]tors. */
1983 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1985 /* Update newdecl's parms to point at olddecl. */
1986 for (parm = DECL_ARGUMENTS (newdecl); parm;
1987 parm = TREE_CHAIN (parm))
1988 DECL_CONTEXT (parm) = olddecl;
1992 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1993 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1994 COPY_DECL_RTL (newdecl, olddecl);
1996 if (! types_match || new_defines_function)
1998 /* These need to be copied so that the names are available.
1999 Note that if the types do match, we'll preserve inline
2000 info and other bits, but if not, we won't. */
2001 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2002 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2004 if (new_defines_function)
2005 /* If defining a function declared with other language
2006 linkage, use the previously declared language linkage. */
2007 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
2008 else if (types_match)
2010 /* If redeclaring a builtin function, and not a definition,
2011 it stays built in. */
2012 if (DECL_BUILT_IN (olddecl))
2014 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
2015 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2016 /* If we're keeping the built-in definition, keep the rtl,
2017 regardless of declaration matches. */
2018 COPY_DECL_RTL (olddecl, newdecl);
2021 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2022 /* Don't clear out the arguments if we're just redeclaring a
2024 if (DECL_ARGUMENTS (olddecl))
2025 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2028 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2029 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2031 /* Now preserve various other info from the definition. */
2032 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2033 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2034 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2035 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
2037 /* Warn about conflicting visibility specifications. */
2038 if (DECL_VISIBILITY_SPECIFIED (olddecl)
2039 && DECL_VISIBILITY_SPECIFIED (newdecl)
2040 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
2042 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
2043 "because it", newdecl);
2044 warning (OPT_Wattributes, "%Jconflicts with previous "
2045 "declaration here", olddecl);
2047 /* Choose the declaration which specified visibility. */
2048 if (DECL_VISIBILITY_SPECIFIED (olddecl))
2050 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
2051 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
2053 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
2054 so keep this behavior. */
2055 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
2057 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
2058 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
2061 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
2062 with that from NEWDECL below. */
2063 if (DECL_LANG_SPECIFIC (olddecl))
2065 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
2066 != DECL_LANG_SPECIFIC (newdecl));
2067 ggc_free (DECL_LANG_SPECIFIC (olddecl));
2070 /* Merge the USED information. */
2071 if (TREE_USED (olddecl))
2072 TREE_USED (newdecl) = 1;
2073 else if (TREE_USED (newdecl))
2074 TREE_USED (olddecl) = 1;
2076 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2080 function_size = sizeof (struct tree_decl_common);
2082 memcpy ((char *) olddecl + sizeof (struct tree_common),
2083 (char *) newdecl + sizeof (struct tree_common),
2084 function_size - sizeof (struct tree_common));
2086 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2087 (char *) newdecl + sizeof (struct tree_decl_common),
2088 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
2090 /* If newdecl is a template instantiation, it is possible that
2091 the following sequence of events has occurred:
2093 o A friend function was declared in a class template. The
2094 class template was instantiated.
2096 o The instantiation of the friend declaration was
2097 recorded on the instantiation list, and is newdecl.
2099 o Later, however, instantiate_class_template called pushdecl
2100 on the newdecl to perform name injection. But, pushdecl in
2101 turn called duplicate_decls when it discovered that another
2102 declaration of a global function with the same name already
2105 o Here, in duplicate_decls, we decided to clobber newdecl.
2107 If we're going to do that, we'd better make sure that
2108 olddecl, and not newdecl, is on the list of
2109 instantiations so that if we try to do the instantiation
2110 again we won't get the clobbered declaration. */
2111 reregister_specialization (newdecl,
2117 size_t size = tree_code_size (TREE_CODE (olddecl));
2118 memcpy ((char *) olddecl + sizeof (struct tree_common),
2119 (char *) newdecl + sizeof (struct tree_common),
2120 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2121 switch (TREE_CODE (olddecl))
2131 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2132 (char *) newdecl + sizeof (struct tree_decl_common),
2133 size - sizeof (struct tree_decl_common)
2134 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2138 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2139 (char *) newdecl + sizeof (struct tree_decl_common),
2140 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2141 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2145 DECL_UID (olddecl) = olddecl_uid;
2147 DECL_FRIEND_P (olddecl) = 1;
2150 DECL_ANTICIPATED (olddecl) = 1;
2151 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2154 /* NEWDECL contains the merged attribute lists.
2155 Update OLDDECL to be the same. */
2156 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2158 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2159 so that encode_section_info has a chance to look at the new decl
2160 flags and attributes. */
2161 if (DECL_RTL_SET_P (olddecl)
2162 && (TREE_CODE (olddecl) == FUNCTION_DECL
2163 || (TREE_CODE (olddecl) == VAR_DECL
2164 && TREE_STATIC (olddecl))))
2165 make_decl_rtl (olddecl);
2167 /* The NEWDECL will no longer be needed. Because every out-of-class
2168 declaration of a member results in a call to duplicate_decls,
2169 freeing these nodes represents in a significant savings. */
2175 /* Return zero if the declaration NEWDECL is valid
2176 when the declaration OLDDECL (assumed to be for the same name)
2177 has already been seen.
2178 Otherwise return an error message format string with a %s
2179 where the identifier should go. */
2182 redeclaration_error_message (tree newdecl, tree olddecl)
2184 if (TREE_CODE (newdecl) == TYPE_DECL)
2186 /* Because C++ can put things into name space for free,
2187 constructs like "typedef struct foo { ... } foo"
2188 would look like an erroneous redeclaration. */
2189 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2192 return "redefinition of %q#D";
2194 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2196 /* If this is a pure function, its olddecl will actually be
2197 the original initialization to `0' (which we force to call
2198 abort()). Don't complain about redefinition in this case. */
2199 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2200 && DECL_INITIAL (olddecl) == NULL_TREE)
2203 /* If both functions come from different namespaces, this is not
2204 a redeclaration - this is a conflict with a used function. */
2205 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2206 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2207 && ! decls_match (olddecl, newdecl))
2208 return "%qD conflicts with used function";
2210 /* We'll complain about linkage mismatches in
2211 warn_extern_redeclared_static. */
2213 /* Defining the same name twice is no good. */
2214 if (DECL_INITIAL (olddecl) != NULL_TREE
2215 && DECL_INITIAL (newdecl) != NULL_TREE)
2217 if (DECL_NAME (olddecl) == NULL_TREE)
2218 return "%q#D not declared in class";
2219 else if (!GNU_INLINE_P (olddecl)
2220 || GNU_INLINE_P (newdecl))
2221 return "redefinition of %q#D";
2224 if (DECL_DECLARED_INLINE_P (olddecl) && DECL_DECLARED_INLINE_P (newdecl))
2226 bool olda = GNU_INLINE_P (olddecl);
2227 bool newa = GNU_INLINE_P (newdecl);
2232 return "%q+D redeclared inline with %<gnu_inline%> attribute";
2234 return "%q+D redeclared inline without %<gnu_inline%> attribute";
2240 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2244 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2246 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2247 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2248 return "redefinition of %q#D";
2252 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2253 || (DECL_TEMPLATE_RESULT (newdecl)
2254 == DECL_TEMPLATE_RESULT (olddecl)))
2257 nt = DECL_TEMPLATE_RESULT (newdecl);
2258 if (DECL_TEMPLATE_INFO (nt))
2259 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2260 ot = DECL_TEMPLATE_RESULT (olddecl);
2261 if (DECL_TEMPLATE_INFO (ot))
2262 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2263 if (DECL_INITIAL (nt) && DECL_INITIAL (ot)
2264 && (!GNU_INLINE_P (ot) || GNU_INLINE_P (nt)))
2265 return "redefinition of %q#D";
2267 if (DECL_DECLARED_INLINE_P (ot) && DECL_DECLARED_INLINE_P (nt))
2269 bool olda = GNU_INLINE_P (ot);
2270 bool newa = GNU_INLINE_P (nt);
2275 return "%q+D redeclared inline with %<gnu_inline%> attribute";
2277 return "%q+D redeclared inline without %<gnu_inline%> attribute";
2281 /* Core issue #226 (C++0x):
2283 If a friend function template declaration specifies a
2284 default template-argument, that declaration shall be a
2285 definition and shall be the only declaration of the
2286 function template in the translation unit. */
2287 if ((cxx_dialect != cxx98)
2288 && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
2289 && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
2290 /*is_primary=*/1, /*is_partial=*/0,
2291 /*is_friend_decl=*/2))
2292 return "redeclaration of friend %q#D may not have default template arguments";
2296 else if (TREE_CODE (newdecl) == VAR_DECL
2297 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2298 && (! DECL_LANG_SPECIFIC (olddecl)
2299 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2300 || DECL_THREAD_LOCAL_P (newdecl)))
2302 /* Only variables can be thread-local, and all declarations must
2303 agree on this property. */
2304 if (DECL_THREAD_LOCAL_P (newdecl))
2305 return "thread-local declaration of %q#D follows "
2306 "non-thread-local declaration";
2308 return "non-thread-local declaration of %q#D follows "
2309 "thread-local declaration";
2311 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2313 /* The objects have been declared at namespace scope. If either
2314 is a member of an anonymous union, then this is an invalid
2315 redeclaration. For example:
2321 if ((TREE_CODE (newdecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (newdecl))
2322 || (TREE_CODE (olddecl) == VAR_DECL && DECL_ANON_UNION_VAR_P (olddecl)))
2323 return "redeclaration of %q#D";
2324 /* If at least one declaration is a reference, there is no
2325 conflict. For example:
2331 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2333 /* Reject two definitions. */
2334 return "redefinition of %q#D";
2338 /* Objects declared with block scope: */
2339 /* Reject two definitions, and reject a definition
2340 together with an external reference. */
2341 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2342 return "redeclaration of %q#D";
2347 /* Hash and equality functions for the named_label table. */
2350 named_label_entry_hash (const void *data)
2352 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2353 return DECL_UID (ent->label_decl);
2357 named_label_entry_eq (const void *a, const void *b)
2359 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2360 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2361 return ent_a->label_decl == ent_b->label_decl;
2364 /* Create a new label, named ID. */
2367 make_label_decl (tree id, int local_p)
2369 struct named_label_entry *ent;
2373 decl = build_decl (LABEL_DECL, id, void_type_node);
2375 DECL_CONTEXT (decl) = current_function_decl;
2376 DECL_MODE (decl) = VOIDmode;
2377 C_DECLARED_LABEL_FLAG (decl) = local_p;
2379 /* Say where one reference is to the label, for the sake of the
2380 error if it is not defined. */
2381 DECL_SOURCE_LOCATION (decl) = input_location;
2383 /* Record the fact that this identifier is bound to this label. */
2384 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2386 /* Create the label htab for the function on demand. */
2388 named_labels = htab_create_ggc (13, named_label_entry_hash,
2389 named_label_entry_eq, NULL);
2391 /* Record this label on the list of labels used in this function.
2392 We do this before calling make_label_decl so that we get the
2393 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2394 ent = GGC_CNEW (struct named_label_entry);
2395 ent->label_decl = decl;
2397 slot = htab_find_slot (named_labels, ent, INSERT);
2398 gcc_assert (*slot == NULL);
2404 /* Look for a label named ID in the current function. If one cannot
2405 be found, create one. (We keep track of used, but undefined,
2406 labels, and complain about them at the end of a function.) */
2409 lookup_label (tree id)
2413 timevar_push (TV_NAME_LOOKUP);
2414 /* You can't use labels at global scope. */
2415 if (current_function_decl == NULL_TREE)
2417 error ("label %qE referenced outside of any function", id);
2418 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2421 /* See if we've already got this label. */
2422 decl = IDENTIFIER_LABEL_VALUE (id);
2423 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2424 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2426 decl = make_label_decl (id, /*local_p=*/0);
2427 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2430 /* Declare a local label named ID. */
2433 declare_local_label (tree id)
2437 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2438 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2439 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2440 current_binding_level->shadowed_labels);
2441 current_binding_level->shadowed_labels = shadow;
2443 decl = make_label_decl (id, /*local_p=*/1);
2444 TREE_VALUE (shadow) = decl;
2449 /* Returns nonzero if it is ill-formed to jump past the declaration of
2450 DECL. Returns 2 if it's also a real problem. */
2453 decl_jump_unsafe (tree decl)
2455 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2456 || TREE_TYPE (decl) == error_mark_node)
2459 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2460 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2463 if (pod_type_p (TREE_TYPE (decl)))
2466 /* The POD stuff is just pedantry; why should it matter if the class
2467 contains a field of pointer to member type? */
2471 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2474 identify_goto (tree decl, const location_t *locus)
2477 permerror (input_location, "jump to label %qD", decl);
2479 permerror (input_location, "jump to case label");
2481 permerror (input_location, "%H from here", locus);
2484 /* Check that a single previously seen jump to a newly defined label
2485 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2486 the jump context; NAMES are the names in scope in LEVEL at the jump
2487 context; LOCUS is the source position of the jump or 0. Returns
2488 true if all is well. */
2491 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2492 bool exited_omp, const location_t *locus)
2494 struct cp_binding_level *b;
2495 bool identified = false, saw_eh = false, saw_omp = false;
2499 identify_goto (decl, locus);
2500 error (" exits OpenMP structured block");
2501 identified = saw_omp = true;
2504 for (b = current_binding_level; b ; b = b->level_chain)
2506 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2508 for (new_decls = b->names; new_decls != old_decls;
2509 new_decls = TREE_CHAIN (new_decls))
2511 int problem = decl_jump_unsafe (new_decls);
2517 identify_goto (decl, locus);
2521 error (" crosses initialization of %q+#D", new_decls);
2523 permerror (input_location, " enters scope of non-POD %q+#D", new_decls);
2528 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2532 identify_goto (decl, locus);
2535 if (b->kind == sk_try)
2536 error (" enters try block");
2538 error (" enters catch block");
2541 if (b->kind == sk_omp && !saw_omp)
2545 identify_goto (decl, locus);
2548 error (" enters OpenMP structured block");
2557 check_previous_goto (tree decl, struct named_label_use_entry *use)
2559 check_previous_goto_1 (decl, use->binding_level,
2560 use->names_in_scope, use->in_omp_scope,
2561 &use->o_goto_locus);
2565 check_switch_goto (struct cp_binding_level* level)
2567 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2570 /* Check that a new jump to a label DECL is OK. Called by
2571 finish_goto_stmt. */
2574 check_goto (tree decl)
2576 struct named_label_entry *ent, dummy;
2577 bool saw_catch = false, identified = false;
2580 /* We can't know where a computed goto is jumping.
2581 So we assume that it's OK. */
2582 if (TREE_CODE (decl) != LABEL_DECL)
2585 /* We didn't record any information about this label when we created it,
2586 and there's not much point since it's trivial to analyze as a return. */
2587 if (decl == cdtor_label)
2590 dummy.label_decl = decl;
2591 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2592 gcc_assert (ent != NULL);
2594 /* If the label hasn't been defined yet, defer checking. */
2595 if (! DECL_INITIAL (decl))
2597 struct named_label_use_entry *new_use;
2599 /* Don't bother creating another use if the last goto had the
2600 same data, and will therefore create the same set of errors. */
2602 && ent->uses->names_in_scope == current_binding_level->names)
2605 new_use = GGC_NEW (struct named_label_use_entry);
2606 new_use->binding_level = current_binding_level;
2607 new_use->names_in_scope = current_binding_level->names;
2608 new_use->o_goto_locus = input_location;
2609 new_use->in_omp_scope = false;
2611 new_use->next = ent->uses;
2612 ent->uses = new_use;
2616 if (ent->in_try_scope || ent->in_catch_scope
2617 || ent->in_omp_scope || ent->bad_decls)
2619 permerror (input_location, "jump to label %q+D", decl);
2620 permerror (input_location, " from here");
2624 for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
2626 tree b = TREE_VALUE (bad);
2627 int u = decl_jump_unsafe (b);
2629 if (u > 1 && DECL_ARTIFICIAL (b))
2631 /* Can't skip init of __exception_info. */
2632 error ("%J enters catch block", b);
2636 error (" skips initialization of %q+#D", b);
2638 permerror (input_location, " enters scope of non-POD %q+#D", b);
2641 if (ent->in_try_scope)
2642 error (" enters try block");
2643 else if (ent->in_catch_scope && !saw_catch)
2644 error (" enters catch block");
2646 if (ent->in_omp_scope)
2647 error (" enters OpenMP structured block");
2648 else if (flag_openmp)
2650 struct cp_binding_level *b;
2651 for (b = current_binding_level; b ; b = b->level_chain)
2653 if (b == ent->binding_level)
2655 if (b->kind == sk_omp)
2659 permerror (input_location, "jump to label %q+D", decl);
2660 permerror (input_location, " from here");
2663 error (" exits OpenMP structured block");
2670 /* Check that a return is ok wrt OpenMP structured blocks.
2671 Called by finish_return_stmt. Returns true if all is well. */
2674 check_omp_return (void)
2676 struct cp_binding_level *b;
2677 for (b = current_binding_level; b ; b = b->level_chain)
2678 if (b->kind == sk_omp)
2680 error ("invalid exit from OpenMP structured block");
2686 /* Define a label, specifying the location in the source file.
2687 Return the LABEL_DECL node for the label. */
2690 define_label (location_t location, tree name)
2692 struct named_label_entry *ent, dummy;
2693 struct cp_binding_level *p;
2696 timevar_push (TV_NAME_LOOKUP);
2698 decl = lookup_label (name);
2700 dummy.label_decl = decl;
2701 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2702 gcc_assert (ent != NULL);
2704 /* After labels, make any new cleanups in the function go into their
2705 own new (temporary) binding contour. */
2706 for (p = current_binding_level;
2707 p->kind != sk_function_parms;
2709 p->more_cleanups_ok = 0;
2711 if (name == get_identifier ("wchar_t"))
2712 permerror (input_location, "label named wchar_t");
2714 if (DECL_INITIAL (decl) != NULL_TREE)
2716 error ("duplicate label %qD", decl);
2717 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2721 struct named_label_use_entry *use;
2723 /* Mark label as having been defined. */
2724 DECL_INITIAL (decl) = error_mark_node;
2725 /* Say where in the source. */
2726 DECL_SOURCE_LOCATION (decl) = location;
2728 ent->binding_level = current_binding_level;
2729 ent->names_in_scope = current_binding_level->names;
2731 for (use = ent->uses; use ; use = use->next)
2732 check_previous_goto (decl, use);
2736 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2741 struct cp_binding_level *level;
2742 struct cp_switch *next;
2743 /* The SWITCH_STMT being built. */
2745 /* A splay-tree mapping the low element of a case range to the high
2746 element, or NULL_TREE if there is no high element. Used to
2747 determine whether or not a new case label duplicates an old case
2748 label. We need a tree, rather than simply a hash table, because
2749 of the GNU case range extension. */
2753 /* A stack of the currently active switch statements. The innermost
2754 switch statement is on the top of the stack. There is no need to
2755 mark the stack for garbage collection because it is only active
2756 during the processing of the body of a function, and we never
2757 collect at that point. */
2759 static struct cp_switch *switch_stack;
2761 /* Called right after a switch-statement condition is parsed.
2762 SWITCH_STMT is the switch statement being parsed. */
2765 push_switch (tree switch_stmt)
2767 struct cp_switch *p = XNEW (struct cp_switch);
2768 p->level = current_binding_level;
2769 p->next = switch_stack;
2770 p->switch_stmt = switch_stmt;
2771 p->cases = splay_tree_new (case_compare, NULL, NULL);
2778 struct cp_switch *cs = switch_stack;
2779 location_t switch_location;
2781 /* Emit warnings as needed. */
2782 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2783 switch_location = EXPR_LOCATION (cs->switch_stmt);
2785 switch_location = input_location;
2786 if (!processing_template_decl)
2787 c_do_switch_warnings (cs->cases, switch_location,
2788 SWITCH_STMT_TYPE (cs->switch_stmt),
2789 SWITCH_STMT_COND (cs->switch_stmt));
2791 splay_tree_delete (cs->cases);
2792 switch_stack = switch_stack->next;
2796 /* Note that we've seen a definition of a case label, and complain if this
2797 is a bad place for one. */
2800 finish_case_label (tree low_value, tree high_value)
2803 struct cp_binding_level *p;
2805 if (processing_template_decl)
2809 /* For templates, just add the case label; we'll do semantic
2810 analysis at instantiation-time. */
2811 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2812 return add_stmt (build_case_label (low_value, high_value, label));
2815 /* Find the condition on which this switch statement depends. */
2816 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2817 if (cond && TREE_CODE (cond) == TREE_LIST)
2818 cond = TREE_VALUE (cond);
2820 if (!check_switch_goto (switch_stack->level))
2821 return error_mark_node;
2823 r = c_add_case_label (switch_stack->cases, cond,
2824 SWITCH_STMT_TYPE (switch_stack->switch_stmt),
2825 low_value, high_value);
2827 /* After labels, make any new cleanups in the function go into their
2828 own new (temporary) binding contour. */
2829 for (p = current_binding_level;
2830 p->kind != sk_function_parms;
2832 p->more_cleanups_ok = 0;
2837 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2840 typename_hash (const void* k)
2843 const_tree const t = (const_tree) k;
2845 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2846 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2851 typedef struct typename_info {
2859 /* Compare two TYPENAME_TYPEs. K1 is really of type `tree', K2 is
2860 really of type `typename_info*' */
2863 typename_compare (const void * k1, const void * k2)
2865 const_tree const t1 = (const_tree) k1;
2866 const typename_info *const t2 = (const typename_info *) k2;
2868 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2869 && TYPE_CONTEXT (t1) == t2->scope
2870 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2871 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2872 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2875 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2876 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2878 Returns the new TYPENAME_TYPE. */
2880 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2883 build_typename_type (tree context, tree name, tree fullname,
2884 enum tag_types tag_type)
2892 if (typename_htab == NULL)
2893 typename_htab = htab_create_ggc (61, &typename_hash,
2894 &typename_compare, NULL);
2896 ti.scope = FROB_CONTEXT (context);
2898 ti.template_id = fullname;
2899 ti.enum_p = tag_type == enum_type;
2900 ti.class_p = (tag_type == class_type
2901 || tag_type == record_type
2902 || tag_type == union_type);
2903 hash = (htab_hash_pointer (ti.scope)
2904 ^ htab_hash_pointer (ti.name));
2906 /* See if we already have this type. */
2907 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2912 /* Build the TYPENAME_TYPE. */
2913 t = cxx_make_type (TYPENAME_TYPE);
2914 TYPE_CONTEXT (t) = ti.scope;
2915 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2916 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2917 TYPENAME_IS_CLASS_P (t) = ti.class_p;
2919 /* Build the corresponding TYPE_DECL. */
2920 d = build_decl (TYPE_DECL, name, t);
2921 TYPE_NAME (TREE_TYPE (d)) = d;
2922 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2923 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2924 DECL_ARTIFICIAL (d) = 1;
2926 /* Store it in the hash table. */
2929 /* TYPENAME_TYPEs must always be compared structurally, because
2930 they may or may not resolve down to another type depending on
2931 the currently open classes. */
2932 SET_TYPE_STRUCTURAL_EQUALITY (t);
2938 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2939 provided to name the type. Returns an appropriate type, unless an
2940 error occurs, in which case error_mark_node is returned. If we
2941 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2942 return that, rather than the _TYPE it corresponds to, in other
2943 cases we look through the type decl. If TF_ERROR is set, complain
2944 about errors, otherwise be quiet. */
2947 make_typename_type (tree context, tree name, enum tag_types tag_type,
2948 tsubst_flags_t complain)
2954 if (name == error_mark_node
2955 || context == NULL_TREE
2956 || context == error_mark_node)
2957 return error_mark_node;
2961 if (!(TYPE_LANG_SPECIFIC (name)
2962 && (CLASSTYPE_IS_TEMPLATE (name)
2963 || CLASSTYPE_USE_TEMPLATE (name))))
2964 name = TYPE_IDENTIFIER (name);
2966 /* Create a TEMPLATE_ID_EXPR for the type. */
2967 name = build_nt (TEMPLATE_ID_EXPR,
2968 CLASSTYPE_TI_TEMPLATE (name),
2969 CLASSTYPE_TI_ARGS (name));
2971 else if (TREE_CODE (name) == TYPE_DECL)
2972 name = DECL_NAME (name);
2976 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2978 name = TREE_OPERAND (name, 0);
2979 if (TREE_CODE (name) == TEMPLATE_DECL)
2980 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2981 else if (TREE_CODE (name) == OVERLOAD)
2983 error ("%qD is not a type", name);
2984 return error_mark_node;
2987 if (TREE_CODE (name) == TEMPLATE_DECL)
2989 error ("%qD used without template parameters", name);
2990 return error_mark_node;
2992 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2993 gcc_assert (TYPE_P (context));
2995 if (!MAYBE_CLASS_TYPE_P (context))
2997 if (complain & tf_error)
2998 error ("%q#T is not a class", context);
2999 return error_mark_node;
3002 /* When the CONTEXT is a dependent type, NAME could refer to a
3003 dependent base class of CONTEXT. But look inside it anyway
3004 if CONTEXT is a currently open scope, in case it refers to a
3005 member of the current instantiation or a non-dependent base;
3006 lookup will stop when we hit a dependent base. */
3007 if (!dependent_scope_p (context))
3008 /* We should only set WANT_TYPE when we're a nested typename type.
3009 Then we can give better diagnostics if we find a non-type. */
3010 t = lookup_field (context, name, 0, /*want_type=*/true);
3014 if (!t && dependent_type_p (context))
3015 return build_typename_type (context, name, fullname, tag_type);
3017 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
3021 if (complain & tf_error)
3022 error (want_template ? "no class template named %q#T in %q#T"
3023 : "no type named %q#T in %q#T", name, context);
3024 return error_mark_node;
3027 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
3029 if (complain & tf_error)
3030 error ("%<typename %T::%D%> names %q#T, which is not a class template",
3032 return error_mark_node;
3034 if (!want_template && TREE_CODE (t) != TYPE_DECL)
3036 if (complain & tf_error)
3037 error ("%<typename %T::%D%> names %q#T, which is not a type",
3039 return error_mark_node;
3042 if (complain & tf_error)
3043 perform_or_defer_access_check (TYPE_BINFO (context), t, t);
3046 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
3048 /*entering_scope=*/0,
3049 tf_warning_or_error | tf_user);
3051 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
3057 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
3058 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
3059 in which case error_mark_node is returned.
3061 If PARM_LIST is non-NULL, also make sure that the template parameter
3062 list of TEMPLATE_DECL matches.
3064 If COMPLAIN zero, don't complain about any errors that occur. */
3067 make_unbound_class_template (tree context, tree name, tree parm_list,
3068 tsubst_flags_t complain)
3074 name = TYPE_IDENTIFIER (name);
3075 else if (DECL_P (name))
3076 name = DECL_NAME (name);
3077 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
3079 if (!dependent_type_p (context)
3080 || currently_open_class (context))
3082 tree tmpl = NULL_TREE;
3084 if (MAYBE_CLASS_TYPE_P (context))
3085 tmpl = lookup_field (context, name, 0, false);
3087 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
3089 if (complain & tf_error)
3090 error ("no class template named %q#T in %q#T", name, context);
3091 return error_mark_node;
3095 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
3097 if (complain & tf_error)
3099 error ("template parameters do not match template");
3100 error ("%q+D declared here", tmpl);
3102 return error_mark_node;
3105 if (complain & tf_error)
3106 perform_or_defer_access_check (TYPE_BINFO (context), tmpl, tmpl);
3111 /* Build the UNBOUND_CLASS_TEMPLATE. */
3112 t = cxx_make_type (UNBOUND_CLASS_TEMPLATE);
3113 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
3114 TREE_TYPE (t) = NULL_TREE;
3115 SET_TYPE_STRUCTURAL_EQUALITY (t);
3117 /* Build the corresponding TEMPLATE_DECL. */
3118 d = build_decl (TEMPLATE_DECL, name, t);
3119 TYPE_NAME (TREE_TYPE (d)) = d;
3120 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
3121 DECL_CONTEXT (d) = FROB_CONTEXT (context);
3122 DECL_ARTIFICIAL (d) = 1;
3123 DECL_TEMPLATE_PARMS (d) = parm_list;
3130 /* Push the declarations of builtin types into the namespace.
3131 RID_INDEX is the index of the builtin type in the array
3132 RID_POINTERS. NAME is the name used when looking up the builtin
3133 type. TYPE is the _TYPE node for the builtin type. */
3136 record_builtin_type (enum rid rid_index,
3140 tree rname = NULL_TREE, tname = NULL_TREE;
3141 tree tdecl = NULL_TREE;
3143 if ((int) rid_index < (int) RID_MAX)
3144 rname = ridpointers[(int) rid_index];
3146 tname = get_identifier (name);
3148 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
3149 eliminated. Built-in types should not be looked up name; their
3150 names are keywords that the parser can recognize. However, there
3151 is code in c-common.c that uses identifier_global_value to look
3152 up built-in types by name. */
3155 tdecl = build_decl (TYPE_DECL, tname, type);
3156 DECL_ARTIFICIAL (tdecl) = 1;
3157 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
3163 tdecl = build_decl (TYPE_DECL, rname, type);
3164 DECL_ARTIFICIAL (tdecl) = 1;
3166 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
3169 if (!TYPE_NAME (type))
3170 TYPE_NAME (type) = tdecl;
3173 debug_hooks->type_decl (tdecl, 0);
3176 /* Record one of the standard Java types.
3177 * Declare it as having the given NAME.
3178 * If SIZE > 0, it is the size of one of the integral types;
3179 * otherwise it is the negative of the size of one of the other types. */
3182 record_builtin_java_type (const char* name, int size)
3186 type = build_nonstandard_integer_type (size, 0);
3187 else if (size > -32)
3190 /* "__java_char" or ""__java_boolean". */
3191 type = build_nonstandard_integer_type (-size, 1);
3192 /* Get the signed type cached and attached to the unsigned type,
3193 so it doesn't get garbage-collected at "random" times,
3194 causing potential codegen differences out of different UIDs
3195 and different alias set numbers. */
3196 stype = build_nonstandard_integer_type (-size, 0);
3197 TREE_CHAIN (type) = stype;
3198 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3201 { /* "__java_float" or ""__java_double". */
3202 type = make_node (REAL_TYPE);
3203 TYPE_PRECISION (type) = - size;
3206 record_builtin_type (RID_MAX, name, type);
3207 decl = TYPE_NAME (type);
3209 /* Suppress generate debug symbol entries for these types,
3210 since for normal C++ they are just clutter.
3211 However, push_lang_context undoes this if extern "Java" is seen. */
3212 DECL_IGNORED_P (decl) = 1;
3214 TYPE_FOR_JAVA (type) = 1;
3218 /* Push a type into the namespace so that the back ends ignore it. */
3221 record_unknown_type (tree type, const char* name)
3223 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
3224 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3225 DECL_IGNORED_P (decl) = 1;
3226 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3227 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3228 TYPE_ALIGN (type) = 1;
3229 TYPE_USER_ALIGN (type) = 0;
3230 SET_TYPE_MODE (type, TYPE_MODE (void_type_node));
3233 /* A string for which we should create an IDENTIFIER_NODE at
3236 typedef struct predefined_identifier
3238 /* The name of the identifier. */
3239 const char *const name;
3240 /* The place where the IDENTIFIER_NODE should be stored. */
3242 /* Nonzero if this is the name of a constructor or destructor. */
3243 const int ctor_or_dtor_p;
3244 } predefined_identifier;
3246 /* Create all the predefined identifiers. */
3249 initialize_predefined_identifiers (void)
3251 const predefined_identifier *pid;
3253 /* A table of identifiers to create at startup. */
3254 static const predefined_identifier predefined_identifiers[] = {
3255 { "C++", &lang_name_cplusplus, 0 },
3256 { "C", &lang_name_c, 0 },
3257 { "Java", &lang_name_java, 0 },
3258 /* Some of these names have a trailing space so that it is
3259 impossible for them to conflict with names written by users. */
3260 { "__ct ", &ctor_identifier, 1 },
3261 { "__base_ctor ", &base_ctor_identifier, 1 },
3262 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3263 { "__dt ", &dtor_identifier, 1 },
3264 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3265 { "__base_dtor ", &base_dtor_identifier, 1 },
3266 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3267 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3268 { "nelts", &nelts_identifier, 0 },
3269 { THIS_NAME, &this_identifier, 0 },
3270 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3271 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3272 { "_vptr", &vptr_identifier, 0 },
3273 { "__vtt_parm", &vtt_parm_identifier, 0 },
3274 { "::", &global_scope_name, 0 },
3275 { "std", &std_identifier, 0 },
3279 for (pid = predefined_identifiers; pid->name; ++pid)
3281 *pid->node = get_identifier (pid->name);
3282 if (pid->ctor_or_dtor_p)
3283 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3287 /* Create the predefined scalar types of C,
3288 and some nodes representing standard constants (0, 1, (void *)0).
3289 Initialize the global binding level.
3290 Make definitions for built-in primitive functions. */
3293 cxx_init_decl_processing (void)
3296 tree void_ftype_ptr;
3298 build_common_tree_nodes (flag_signed_char, false);
3300 /* Create all the identifiers we need. */
3301 initialize_predefined_identifiers ();
3303 /* Create the global variables. */
3304 push_to_top_level ();
3306 current_function_decl = NULL_TREE;
3307 current_binding_level = NULL;
3308 /* Enter the global namespace. */
3309 gcc_assert (global_namespace == NULL_TREE);
3310 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3312 TREE_PUBLIC (global_namespace) = 1;
3313 begin_scope (sk_namespace, global_namespace);
3315 current_lang_name = NULL_TREE;
3317 if (flag_visibility_ms_compat)
3318 default_visibility = VISIBILITY_HIDDEN;
3321 current_lang_name = lang_name_c;
3323 /* Create the `std' namespace. */
3324 push_namespace (std_identifier);
3325 std_node = current_namespace;
3328 c_common_nodes_and_builtins ();
3330 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3331 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3332 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3333 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3334 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3335 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3336 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3337 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3339 integer_two_node = build_int_cst (NULL_TREE, 2);
3340 integer_three_node = build_int_cst (NULL_TREE, 3);
3342 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3343 truthvalue_type_node = boolean_type_node;
3344 truthvalue_false_node = boolean_false_node;
3345 truthvalue_true_node = boolean_true_node;
3347 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3350 record_builtin_type (RID_MAX, NULL, string_type_node);
3353 delta_type_node = ptrdiff_type_node;
3354 vtable_index_type = ptrdiff_type_node;
3356 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3357 void_ftype = build_function_type (void_type_node, void_list_node);
3358 void_ftype_ptr = build_function_type (void_type_node,
3359 tree_cons (NULL_TREE,
3363 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3365 /* C++ extensions */
3367 unknown_type_node = make_node (UNKNOWN_TYPE);
3368 record_unknown_type (unknown_type_node, "unknown type");
3370 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3371 TREE_TYPE (unknown_type_node) = unknown_type_node;
3373 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3375 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3376 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3378 init_list_type_node = make_node (UNKNOWN_TYPE);
3379 record_unknown_type (init_list_type_node, "init list");
3382 /* Make sure we get a unique function type, so we can give
3383 its pointer type a name. (This wins for gdb.) */
3384 tree vfunc_type = make_node (FUNCTION_TYPE);
3385 TREE_TYPE (vfunc_type) = integer_type_node;
3386 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3387 layout_type (vfunc_type);
3389 vtable_entry_type = build_pointer_type (vfunc_type);
3391 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3394 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3395 layout_type (vtbl_type_node);
3396 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3397 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3398 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3399 layout_type (vtbl_ptr_type_node);
3400 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3402 push_namespace (get_identifier ("__cxxabiv1"));
3403 abi_node = current_namespace;
3406 global_type_node = make_node (LANG_TYPE);
3407 record_unknown_type (global_type_node, "global type");
3410 current_lang_name = lang_name_cplusplus;
3414 tree bad_alloc_type_node;
3415 tree bad_alloc_decl;
3416 tree newtype, deltype;
3417 tree ptr_ftype_sizetype;
3419 push_namespace (std_identifier);
3420 bad_alloc_id = get_identifier ("bad_alloc");
3421 bad_alloc_type_node = make_class_type (RECORD_TYPE);
3422 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3424 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3425 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3426 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3430 = build_function_type (ptr_type_node,
3431 tree_cons (NULL_TREE,
3434 newtype = build_exception_variant
3435 (ptr_ftype_sizetype, add_exception_specifier
3436 (NULL_TREE, bad_alloc_type_node, -1));
3437 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3438 push_cp_library_fn (NEW_EXPR, newtype);
3439 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3440 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3441 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3445 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3447 /* Perform other language dependent initializations. */
3448 init_class_processing ();
3449 init_rtti_processing ();
3451 if (flag_exceptions)
3452 init_exception_processing ();
3454 if (! supports_one_only ())
3457 make_fname_decl = cp_make_fname_decl;
3458 start_fname_decls ();
3460 /* Show we use EH for cleanups. */
3461 if (flag_exceptions)
3462 using_eh_for_cleanups ();
3465 /* Generate an initializer for a function naming variable from
3466 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3467 filled in with the type of the init. */
3470 cp_fname_init (const char* name, tree *type_p)
3472 tree domain = NULL_TREE;
3474 tree init = NULL_TREE;
3479 length = strlen (name);
3480 domain = build_index_type (size_int (length));
3481 init = build_string (length + 1, name);
3484 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3485 type = build_cplus_array_type (type, domain);
3490 TREE_TYPE (init) = type;
3492 init = error_mark_node;
3497 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3498 decl, NAME is the initialization string and TYPE_DEP indicates whether
3499 NAME depended on the type of the function. We make use of that to detect
3500 __PRETTY_FUNCTION__ inside a template fn. This is being done
3501 lazily at the point of first use, so we mustn't push the decl now. */