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 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
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"
53 #include "tree-flow.h"
55 static tree grokparms (cp_parameter_declarator *, tree *);
56 static const char *redeclaration_error_message (tree, tree);
58 static int decl_jump_unsafe (tree);
59 static void require_complete_types_for_parms (tree);
60 static int ambi_op_p (enum tree_code);
61 static int unary_op_p (enum tree_code);
62 static void push_local_name (tree);
63 static tree grok_reference_init (tree, tree, tree, tree *);
64 static tree grokvardecl (tree, tree, const cp_decl_specifier_seq *,
66 static void record_unknown_type (tree, const char *);
67 static tree builtin_function_1 (const char *, tree, tree,
68 enum built_in_function code,
69 enum built_in_class cl, const char *,
71 static tree build_library_fn_1 (tree, enum tree_code, tree);
72 static int member_function_or_else (tree, tree, enum overload_flags);
73 static void bad_specifiers (tree, const char *, int, int, int, int,
75 static void check_for_uninitialized_const_var (tree);
76 static hashval_t typename_hash (const void *);
77 static int typename_compare (const void *, const void *);
78 static tree local_variable_p_walkfn (tree *, int *, void *);
79 static tree record_builtin_java_type (const char *, int);
80 static const char *tag_name (enum tag_types);
81 static tree lookup_and_check_tag (enum tag_types, tree, tag_scope, bool);
82 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
83 static tree make_label_decl (tree, int);
84 static void use_label (tree);
85 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
87 static void check_previous_goto (struct named_label_use_list *);
88 static void check_switch_goto (struct cp_binding_level *);
89 static void check_previous_gotos (tree);
90 static void pop_label (tree, tree);
91 static void pop_labels (tree);
92 static void maybe_deduce_size_from_array_init (tree, tree);
93 static void layout_var_decl (tree);
94 static void maybe_commonize_var (tree);
95 static tree check_initializer (tree, tree, int, tree *);
96 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
97 static void save_function_data (tree);
98 static void check_function_type (tree, tree);
99 static void finish_constructor_body (void);
100 static void begin_destructor_body (void);
101 static void finish_destructor_body (void);
102 static tree create_array_type_for_decl (tree, tree, tree);
103 static tree get_atexit_node (void);
104 static tree get_dso_handle_node (void);
105 static tree start_cleanup_fn (void);
106 static void end_cleanup_fn (void);
107 static tree cp_make_fname_decl (tree, int);
108 static void initialize_predefined_identifiers (void);
109 static tree check_special_function_return_type
110 (special_function_kind, tree, tree);
111 static tree push_cp_library_fn (enum tree_code, tree);
112 static tree build_cp_library_fn (tree, enum tree_code, tree);
113 static void store_parm_decls (tree);
114 static void initialize_local_var (tree, tree);
115 static void expand_static_init (tree, tree);
116 static tree next_initializable_field (tree);
117 static tree reshape_init (tree, tree);
119 /* Erroneous argument lists can use this *IFF* they do not modify it. */
120 tree error_mark_list;
122 /* The following symbols are subsumed in the cp_global_trees array, and
123 listed here individually for documentation purposes.
126 tree wchar_decl_node;
128 tree vtable_entry_type;
129 tree delta_type_node;
130 tree __t_desc_type_node;
132 tree class_type_node;
133 tree unknown_type_node;
135 Array type `vtable_entry_type[]'
138 tree vtbl_ptr_type_node;
145 A FUNCTION_DECL which can call `abort'. Not necessarily the
146 one that the user will declare, but sufficient to be called
147 by routines that want to abort the program.
151 The FUNCTION_DECL for the default `::operator delete'.
153 tree global_delete_fndecl;
156 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
161 tree cp_global_trees[CPTI_MAX];
163 /* Indicates that there is a type value in some namespace, although
164 that is not necessarily in scope at the moment. */
166 tree global_type_node;
168 /* The node that holds the "name" of the global scope. */
169 tree global_scope_name;
171 /* Used only for jumps to as-yet undefined labels, since jumps to
172 defined labels can have their validity checked immediately. */
174 struct named_label_use_list GTY(())
176 struct cp_binding_level *binding_level;
179 location_t o_goto_locus;
180 struct named_label_use_list *next;
183 #define named_label_uses cp_function_chain->x_named_label_uses
185 #define local_names cp_function_chain->x_local_names
187 /* A list of objects which have constructors or destructors
188 which reside in the global scope. The decl is stored in
189 the TREE_VALUE slot and the initializer is stored
190 in the TREE_PURPOSE slot. */
191 tree static_aggregates;
195 /* A node for the integer constants 2, and 3. */
197 tree integer_two_node, integer_three_node;
199 /* A list of all LABEL_DECLs in the function that have names. Here so
200 we can clear out their names' definitions at the end of the
201 function, and so we can check the validity of jumps to these labels. */
203 struct named_label_list GTY(())
205 struct cp_binding_level *binding_level;
210 struct named_label_list *next;
211 unsigned int in_try_scope : 1;
212 unsigned int in_catch_scope : 1;
215 #define named_labels cp_function_chain->x_named_labels
217 /* The number of function bodies which we are currently processing.
218 (Zero if we are at namespace scope, one inside the body of a
219 function, two inside the body of a function in a local class, etc.) */
222 /* States indicating how grokdeclarator() should handle declspecs marked
223 with __attribute__((deprecated)). An object declared as
224 __attribute__((deprecated)) suppresses warnings of uses of other
227 enum deprecated_states {
232 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
234 /* True if a declaration with an `extern' linkage specifier is being
236 bool have_extern_spec;
239 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
240 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
241 time the VAR_DECL was declared, the type was incomplete. */
243 static GTY(()) tree incomplete_vars;
245 /* Returns the kind of template specialization we are currently
246 processing, given that it's declaration contained N_CLASS_SCOPES
247 explicit scope qualifications. */
250 current_tmpl_spec_kind (int n_class_scopes)
252 int n_template_parm_scopes = 0;
253 int seen_specialization_p = 0;
254 int innermost_specialization_p = 0;
255 struct cp_binding_level *b;
257 /* Scan through the template parameter scopes. */
258 for (b = current_binding_level;
259 b->kind == sk_template_parms;
262 /* If we see a specialization scope inside a parameter scope,
263 then something is wrong. That corresponds to a declaration
266 template <class T> template <> ...
268 which is always invalid since [temp.expl.spec] forbids the
269 specialization of a class member template if the enclosing
270 class templates are not explicitly specialized as well. */
271 if (b->explicit_spec_p)
273 if (n_template_parm_scopes == 0)
274 innermost_specialization_p = 1;
276 seen_specialization_p = 1;
278 else if (seen_specialization_p == 1)
279 return tsk_invalid_member_spec;
281 ++n_template_parm_scopes;
284 /* Handle explicit instantiations. */
285 if (processing_explicit_instantiation)
287 if (n_template_parm_scopes != 0)
288 /* We've seen a template parameter list during an explicit
289 instantiation. For example:
291 template <class T> template void f(int);
293 This is erroneous. */
294 return tsk_invalid_expl_inst;
296 return tsk_expl_inst;
299 if (n_template_parm_scopes < n_class_scopes)
300 /* We've not seen enough template headers to match all the
301 specialized classes present. For example:
303 template <class T> void R<T>::S<T>::f(int);
305 This is invalid; there needs to be one set of template
306 parameters for each class. */
307 return tsk_insufficient_parms;
308 else if (n_template_parm_scopes == n_class_scopes)
309 /* We're processing a non-template declaration (even though it may
310 be a member of a template class.) For example:
312 template <class T> void S<T>::f(int);
314 The `class T' maches the `S<T>', leaving no template headers
315 corresponding to the `f'. */
317 else if (n_template_parm_scopes > n_class_scopes + 1)
318 /* We've got too many template headers. For example:
320 template <> template <class T> void f (T);
322 There need to be more enclosing classes. */
323 return tsk_excessive_parms;
325 /* This must be a template. It's of the form:
327 template <class T> template <class U> void S<T>::f(U);
329 This is a specialization if the innermost level was a
330 specialization; otherwise it's just a definition of the
332 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
335 /* Exit the current scope. */
343 /* When a label goes out of scope, check to see if that label was used
344 in a valid manner, and issue any appropriate warnings or errors. */
347 pop_label (tree label, tree old_value)
349 if (!processing_template_decl)
351 if (DECL_INITIAL (label) == NULL_TREE)
355 error ("label %q+D used but not defined", label);
356 #ifdef USE_MAPPED_LOCATION
357 location = input_location; /* FIXME want (input_filename, (line)0) */
359 location.file = input_filename;
362 /* Avoid crashing later. */
363 define_label (location, DECL_NAME (label));
365 else if (warn_unused_label && !TREE_USED (label))
366 warning (0, "label %q+D defined but not used", label);
369 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
372 /* At the end of a function, all labels declared within the function
373 go out of scope. BLOCK is the top-level block for the
377 pop_labels (tree block)
379 struct named_label_list *link;
381 /* Clear out the definitions of all label names, since their scopes
383 for (link = named_labels; link; link = link->next)
385 pop_label (link->label_decl, link->old_value);
386 /* Put the labels into the "variables" of the top-level block,
387 so debugger can see them. */
388 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
389 BLOCK_VARS (block) = link->label_decl;
395 /* The following two routines are used to interface to Objective-C++.
396 The binding level is purposely treated as an opaque type. */
399 objc_get_current_scope (void)
401 return current_binding_level;
404 /* The following routine is used by the NeXT-style SJLJ exceptions;
405 variables get marked 'volatile' so as to not be clobbered by
406 _setjmp()/_longjmp() calls. All variables in the current scope,
407 as well as parent scopes up to (but not including) ENCLOSING_BLK
408 shall be thusly marked. */
411 objc_mark_locals_volatile (void *enclosing_blk)
413 struct cp_binding_level *scope;
415 for (scope = current_binding_level;
416 scope && scope != enclosing_blk;
417 scope = scope->level_chain)
421 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
422 objc_volatilize_decl (decl);
424 /* Do not climb up past the current function. */
425 if (scope->kind == sk_function_parms)
430 /* Exit a binding level.
431 Pop the level off, and restore the state of the identifier-decl mappings
432 that were in effect when this level was entered.
434 If KEEP == 1, this level had explicit declarations, so
435 and create a "block" (a BLOCK node) for the level
436 to record its declarations and subblocks for symbol table output.
438 If FUNCTIONBODY is nonzero, this level is the body of a function,
439 so create a block as if KEEP were set and also clear out all
442 If REVERSE is nonzero, reverse the order of decls before putting
443 them into the BLOCK. */
446 poplevel (int keep, int reverse, int functionbody)
449 /* The chain of decls was accumulated in reverse order.
450 Put it into forward order, just for cleanliness. */
452 int tmp = functionbody;
453 int real_functionbody;
457 int leaving_for_scope;
460 timevar_push (TV_NAME_LOOKUP);
465 gcc_assert (current_binding_level->kind != sk_class);
467 real_functionbody = (current_binding_level->kind == sk_cleanup
468 ? ((functionbody = 0), tmp) : functionbody);
469 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
471 gcc_assert (!VEC_length(cp_class_binding,
472 current_binding_level->class_shadowed));
474 /* We used to use KEEP == 2 to indicate that the new block should go
475 at the beginning of the list of blocks at this binding level,
476 rather than the end. This hack is no longer used. */
477 gcc_assert (keep == 0 || keep == 1);
479 if (current_binding_level->keep)
482 /* Any uses of undefined labels, and any defined labels, now operate
483 under constraints of next binding contour. */
484 if (cfun && !functionbody)
486 struct cp_binding_level *level_chain;
487 level_chain = current_binding_level->level_chain;
490 struct named_label_use_list *uses;
491 struct named_label_list *labels;
492 for (labels = named_labels; labels; labels = labels->next)
493 if (labels->binding_level == current_binding_level)
496 if (current_binding_level->kind == sk_try)
497 labels->in_try_scope = 1;
498 if (current_binding_level->kind == sk_catch)
499 labels->in_catch_scope = 1;
500 for (decl = labels->names_in_scope; decl;
501 decl = TREE_CHAIN (decl))
502 if (decl_jump_unsafe (decl))
503 labels->bad_decls = tree_cons (NULL_TREE, decl,
505 labels->binding_level = level_chain;
506 labels->names_in_scope = level_chain->names;
509 for (uses = named_label_uses; uses; uses = uses->next)
510 if (uses->binding_level == current_binding_level)
512 uses->binding_level = level_chain;
513 uses->names_in_scope = level_chain->names;
518 /* Get the decls in the order they were written.
519 Usually current_binding_level->names is in reverse order.
520 But parameter decls were previously put in forward order. */
523 current_binding_level->names
524 = decls = nreverse (current_binding_level->names);
526 decls = current_binding_level->names;
528 /* If there were any declarations or structure tags in that level,
529 or if this level is a function body,
530 create a BLOCK to record them for the life of this function. */
532 if (keep == 1 || functionbody)
533 block = make_node (BLOCK);
534 if (block != NULL_TREE)
536 BLOCK_VARS (block) = decls;
537 BLOCK_SUBBLOCKS (block) = subblocks;
540 /* In each subblock, record that this is its superior. */
542 for (link = subblocks; link; link = TREE_CHAIN (link))
543 BLOCK_SUPERCONTEXT (link) = block;
545 /* We still support the old for-scope rules, whereby the variables
546 in a for-init statement were in scope after the for-statement
547 ended. We only use the new rules if flag_new_for_scope is
550 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
552 /* Before we remove the declarations first check for unused variables. */
553 if (warn_unused_variable
554 && !processing_template_decl)
555 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
556 if (TREE_CODE (decl) == VAR_DECL
557 && ! TREE_USED (decl)
558 && ! DECL_IN_SYSTEM_HEADER (decl)
559 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
560 warning (0, "unused variable %q+D", decl);
562 /* Remove declarations for all the DECLs in this level. */
563 for (link = decls; link; link = TREE_CHAIN (link))
565 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
568 tree name = DECL_NAME (link);
572 ob = outer_binding (name,
573 IDENTIFIER_BINDING (name),
576 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
578 ns_binding = NULL_TREE;
580 if (ob && ob->scope == current_binding_level->level_chain)
581 /* We have something like:
586 and we are leaving the `for' scope. There's no reason to
587 keep the binding of the inner `i' in this case. */
588 pop_binding (name, link);
589 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
590 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
591 /* Here, we have something like:
599 We must pop the for-scope binding so we know what's a
600 type and what isn't. */
601 pop_binding (name, link);
604 /* Mark this VAR_DECL as dead so that we can tell we left it
605 there only for backward compatibility. */
606 DECL_DEAD_FOR_LOCAL (link) = 1;
608 /* Keep track of what should have happened when we
609 popped the binding. */
612 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
613 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
616 /* Add it to the list of dead variables in the next
617 outermost binding to that we can remove these when we
618 leave that binding. */
619 current_binding_level->level_chain->dead_vars_from_for
620 = tree_cons (NULL_TREE, link,
621 current_binding_level->level_chain->
624 /* Although we don't pop the cxx_binding, we do clear
625 its SCOPE since the scope is going away now. */
626 IDENTIFIER_BINDING (name)->scope
627 = current_binding_level->level_chain;
634 /* Remove the binding. */
637 if (TREE_CODE (decl) == TREE_LIST)
638 decl = TREE_VALUE (decl);
641 if (TREE_CODE (name) == OVERLOAD)
642 name = OVL_FUNCTION (name);
644 gcc_assert (DECL_P (name));
645 pop_binding (DECL_NAME (name), decl);
649 /* Remove declarations for any `for' variables from inner scopes
650 that we kept around. */
651 for (link = current_binding_level->dead_vars_from_for;
652 link; link = TREE_CHAIN (link))
653 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
655 /* Restore the IDENTIFIER_TYPE_VALUEs. */
656 for (link = current_binding_level->type_shadowed;
657 link; link = TREE_CHAIN (link))
658 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
660 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
661 for (link = current_binding_level->shadowed_labels;
663 link = TREE_CHAIN (link))
664 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
666 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
667 list if a `using' declaration put them there. The debugging
668 back-ends won't understand OVERLOAD, so we remove them here.
669 Because the BLOCK_VARS are (temporarily) shared with
670 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
671 popped all the bindings. */
676 for (d = &BLOCK_VARS (block); *d; )
678 if (TREE_CODE (*d) == TREE_LIST)
679 *d = TREE_CHAIN (*d);
681 d = &TREE_CHAIN (*d);
685 /* If the level being exited is the top level of a function,
686 check over all the labels. */
689 /* Since this is the top level block of a function, the vars are
690 the function's parameters. Don't leave them in the BLOCK
691 because they are found in the FUNCTION_DECL instead. */
692 BLOCK_VARS (block) = 0;
696 kind = current_binding_level->kind;
697 if (kind == sk_cleanup)
701 /* If this is a temporary binding created for a cleanup, then we'll
702 have pushed a statement list level. Pop that, create a new
703 BIND_EXPR for the block, and insert it into the stream. */
704 stmt = pop_stmt_list (current_binding_level->statement_list);
705 stmt = c_build_bind_expr (block, stmt);
711 DECL_INITIAL (current_function_decl) = block;
713 current_binding_level->blocks
714 = chainon (current_binding_level->blocks, block);
716 /* If we did not make a block for the level just exited,
717 any blocks made for inner levels
718 (since they cannot be recorded as subblocks in that level)
719 must be carried forward so they will later become subblocks
720 of something else. */
722 current_binding_level->blocks
723 = chainon (current_binding_level->blocks, subblocks);
725 /* Each and every BLOCK node created here in `poplevel' is important
726 (e.g. for proper debugging information) so if we created one
727 earlier, mark it as "used". */
729 TREE_USED (block) = 1;
731 /* All temporary bindings created for cleanups are popped silently. */
732 if (kind == sk_cleanup)
735 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
738 /* Insert BLOCK at the end of the list of subblocks of the
739 current binding level. This is used when a BIND_EXPR is expanded,
740 to handle the BLOCK node inside the BIND_EXPR. */
743 insert_block (tree block)
745 TREE_USED (block) = 1;
746 current_binding_level->blocks
747 = chainon (current_binding_level->blocks, block);
750 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
751 itself, calling F for each. The DATA is passed to F as well. */
754 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
757 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
759 result |= (*f) (namespace, data);
761 for (; current; current = TREE_CHAIN (current))
762 result |= walk_namespaces_r (current, f, data);
767 /* Walk all the namespaces, calling F for each. The DATA is passed to
771 walk_namespaces (walk_namespaces_fn f, void* data)
773 return walk_namespaces_r (global_namespace, f, data);
776 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
777 DATA is non-NULL, this is the last time we will call
778 wrapup_global_declarations for this NAMESPACE. */
781 wrapup_globals_for_namespace (tree namespace, void* data)
783 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
784 VEC(tree,gc) *statics = level->static_decls;
785 tree *vec = VEC_address (tree, statics);
786 int len = VEC_length (tree, statics);
787 int last_time = (data != 0);
791 check_global_declarations (vec, len);
792 emit_debug_global_declarations (vec, len);
796 /* Write out any globals that need to be output. */
797 return wrapup_global_declarations (vec, len);
801 /* In C++, you don't have to write `struct S' to refer to `S'; you
802 can just use `S'. We accomplish this by creating a TYPE_DECL as
803 if the user had written `typedef struct S S'. Create and return
804 the TYPE_DECL for TYPE. */
807 create_implicit_typedef (tree name, tree type)
811 decl = build_decl (TYPE_DECL, name, type);
812 DECL_ARTIFICIAL (decl) = 1;
813 /* There are other implicit type declarations, like the one *within*
814 a class that allows you to write `S::S'. We must distinguish
816 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
817 TYPE_NAME (type) = decl;
822 /* Remember a local name for name-mangling purposes. */
825 push_local_name (tree decl)
830 timevar_push (TV_NAME_LOOKUP);
832 name = DECL_NAME (decl);
834 nelts = VEC_length (tree, local_names);
835 for (i = 0; i < nelts; i++)
837 t = VEC_index (tree, local_names, i);
838 if (DECL_NAME (t) == name)
840 if (!DECL_LANG_SPECIFIC (decl))
841 retrofit_lang_decl (decl);
842 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
843 if (DECL_LANG_SPECIFIC (t))
844 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
846 DECL_DISCRIMINATOR (decl) = 1;
848 VEC_replace (tree, local_names, i, decl);
849 timevar_pop (TV_NAME_LOOKUP);
854 VEC_safe_push (tree, gc, local_names, decl);
855 timevar_pop (TV_NAME_LOOKUP);
858 /* Subroutine of duplicate_decls: return truthvalue of whether
859 or not types of these decls match.
861 For C++, we must compare the parameter list so that `int' can match
862 `int&' in a parameter position, but `int&' is not confused with
866 decls_match (tree newdecl, tree olddecl)
870 if (newdecl == olddecl)
873 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
874 /* If the two DECLs are not even the same kind of thing, we're not
875 interested in their types. */
878 if (TREE_CODE (newdecl) == FUNCTION_DECL)
880 tree f1 = TREE_TYPE (newdecl);
881 tree f2 = TREE_TYPE (olddecl);
882 tree p1 = TYPE_ARG_TYPES (f1);
883 tree p2 = TYPE_ARG_TYPES (f2);
885 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
886 && ! (DECL_EXTERN_C_P (newdecl)
887 && DECL_EXTERN_C_P (olddecl)))
890 if (TREE_CODE (f1) != TREE_CODE (f2))
893 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
895 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
896 && (DECL_BUILT_IN (olddecl)
897 #ifndef NO_IMPLICIT_EXTERN_C
898 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
899 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
903 types_match = self_promoting_args_p (p1);
904 if (p1 == void_list_node)
905 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
907 #ifndef NO_IMPLICIT_EXTERN_C
908 else if (p1 == NULL_TREE
909 && (DECL_EXTERN_C_P (olddecl)
910 && DECL_IN_SYSTEM_HEADER (olddecl)
911 && !DECL_CLASS_SCOPE_P (olddecl))
912 && (DECL_EXTERN_C_P (newdecl)
913 && DECL_IN_SYSTEM_HEADER (newdecl)
914 && !DECL_CLASS_SCOPE_P (newdecl)))
916 types_match = self_promoting_args_p (p2);
917 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
921 types_match = compparms (p1, p2);
926 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
928 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
929 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
932 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
933 DECL_TEMPLATE_PARMS (olddecl)))
936 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
937 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
938 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
940 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
941 DECL_TEMPLATE_RESULT (newdecl));
945 /* Need to check scope for variable declaration (VAR_DECL).
946 For typedef (TYPE_DECL), scope is ignored. */
947 if (TREE_CODE (newdecl) == VAR_DECL
948 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
951 if (TREE_TYPE (newdecl) == error_mark_node)
952 types_match = TREE_TYPE (olddecl) == error_mark_node;
953 else if (TREE_TYPE (olddecl) == NULL_TREE)
954 types_match = TREE_TYPE (newdecl) == NULL_TREE;
955 else if (TREE_TYPE (newdecl) == NULL_TREE)
958 types_match = comptypes (TREE_TYPE (newdecl),
960 COMPARE_REDECLARATION);
966 /* If NEWDECL is `static' and an `extern' was seen previously,
967 warn about it. OLDDECL is the previous declaration.
969 Note that this does not apply to the C++ case of declaring
970 a variable `extern const' and then later `const'.
972 Don't complain about built-in functions, since they are beyond
973 the user's control. */
976 warn_extern_redeclared_static (tree newdecl, tree olddecl)
980 if (TREE_CODE (newdecl) == TYPE_DECL
981 || TREE_CODE (newdecl) == TEMPLATE_DECL
982 || TREE_CODE (newdecl) == CONST_DECL
983 || TREE_CODE (newdecl) == NAMESPACE_DECL)
986 /* Don't get confused by static member functions; that's a different
988 if (TREE_CODE (newdecl) == FUNCTION_DECL
989 && DECL_STATIC_FUNCTION_P (newdecl))
992 /* If the old declaration was `static', or the new one isn't, then
993 then everything is OK. */
994 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
997 /* It's OK to declare a builtin function as `static'. */
998 if (TREE_CODE (olddecl) == FUNCTION_DECL
999 && DECL_ARTIFICIAL (olddecl))
1002 name = DECL_ASSEMBLER_NAME (newdecl);
1003 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
1004 pedwarn ("previous declaration of %q+D", olddecl);
1007 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1008 If the redeclaration is invalid, a diagnostic is issued, and the
1009 error_mark_node is returned. Otherwise, OLDDECL is returned.
1011 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1014 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1017 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1019 unsigned olddecl_uid = DECL_UID (olddecl);
1020 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1021 int new_defines_function = 0;
1023 if (newdecl == olddecl)
1026 types_match = decls_match (newdecl, olddecl);
1028 /* If either the type of the new decl or the type of the old decl is an
1029 error_mark_node, then that implies that we have already issued an
1030 error (earlier) for some bogus type specification, and in that case,
1031 it is rather pointless to harass the user with yet more error message
1032 about the same declaration, so just pretend the types match here. */
1033 if (TREE_TYPE (newdecl) == error_mark_node
1034 || TREE_TYPE (olddecl) == error_mark_node)
1037 if (DECL_P (olddecl)
1038 && TREE_CODE (newdecl) == FUNCTION_DECL
1039 && TREE_CODE (olddecl) == FUNCTION_DECL
1040 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1042 if (DECL_DECLARED_INLINE_P (newdecl)
1043 && DECL_UNINLINABLE (newdecl)
1044 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1045 /* Already warned elsewhere. */;
1046 else if (DECL_DECLARED_INLINE_P (olddecl)
1047 && DECL_UNINLINABLE (olddecl)
1048 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1049 /* Already warned. */;
1050 else if (DECL_DECLARED_INLINE_P (newdecl)
1051 && DECL_UNINLINABLE (olddecl)
1052 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1054 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1056 warning (OPT_Wattributes, "previous declaration of %q+D "
1057 "with attribute noinline", olddecl);
1059 else if (DECL_DECLARED_INLINE_P (olddecl)
1060 && DECL_UNINLINABLE (newdecl)
1061 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1063 warning (OPT_Wattributes, "function %q+D redeclared with "
1064 "attribute noinline", newdecl);
1065 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1070 /* Check for redeclaration and other discrepancies. */
1071 if (TREE_CODE (olddecl) == FUNCTION_DECL
1072 && DECL_ARTIFICIAL (olddecl))
1074 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1075 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1077 /* Avoid warnings redeclaring built-ins which have not been
1078 explicitly declared. */
1079 if (DECL_ANTICIPATED (olddecl))
1082 /* If you declare a built-in or predefined function name as static,
1083 the old definition is overridden, but optionally warn this was a
1084 bad choice of name. */
1085 if (! TREE_PUBLIC (newdecl))
1088 warning (0, "shadowing %s function %q#D",
1089 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1091 /* Discard the old built-in function. */
1094 /* If the built-in is not ansi, then programs can override
1095 it even globally without an error. */
1096 else if (! DECL_BUILT_IN (olddecl))
1097 warning (0, "library function %q#D redeclared as non-function %q#D",
1101 error ("declaration of %q#D", newdecl);
1102 error ("conflicts with built-in declaration %q#D",
1107 else if (!types_match)
1109 /* Avoid warnings redeclaring built-ins which have not been
1110 explicitly declared. */
1111 if (DECL_ANTICIPATED (olddecl))
1113 /* Deal with fileptr_type_node. FILE type is not known
1114 at the time we create the builtins. */
1117 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1118 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1120 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1123 else if (TREE_VALUE (t2) == fileptr_type_node)
1125 tree t = TREE_VALUE (t1);
1127 if (TREE_CODE (t) == POINTER_TYPE
1128 && TYPE_NAME (TREE_TYPE (t))
1129 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1130 == get_identifier ("FILE")
1131 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1133 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1135 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1136 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1137 types_match = decls_match (newdecl, olddecl);
1139 return duplicate_decls (newdecl, olddecl,
1141 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1144 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1147 else if ((DECL_EXTERN_C_P (newdecl)
1148 && DECL_EXTERN_C_P (olddecl))
1149 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1150 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1152 /* A near match; override the builtin. */
1154 if (TREE_PUBLIC (newdecl))
1156 warning (0, "new declaration %q#D", newdecl);
1157 warning (0, "ambiguates built-in declaration %q#D",
1160 else if (warn_shadow)
1161 warning (0, "shadowing %s function %q#D",
1162 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1166 /* Discard the old built-in function. */
1169 /* Replace the old RTL to avoid problems with inlining. */
1170 COPY_DECL_RTL (newdecl, olddecl);
1172 /* Even if the types match, prefer the new declarations type for
1173 built-ins which have not been explicitly declared, for
1174 exception lists, etc... */
1175 else if (DECL_ANTICIPATED (olddecl))
1177 tree type = TREE_TYPE (newdecl);
1178 tree attribs = (*targetm.merge_type_attributes)
1179 (TREE_TYPE (olddecl), type);
1181 type = cp_build_type_attribute_variant (type, attribs);
1182 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1185 /* Whether or not the builtin can throw exceptions has no
1186 bearing on this declarator. */
1187 TREE_NOTHROW (olddecl) = 0;
1189 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1191 /* If a builtin function is redeclared as `static', merge
1192 the declarations, but make the original one static. */
1193 DECL_THIS_STATIC (olddecl) = 1;
1194 TREE_PUBLIC (olddecl) = 0;
1196 /* Make the old declaration consistent with the new one so
1197 that all remnants of the builtin-ness of this function
1198 will be banished. */
1199 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1200 COPY_DECL_RTL (newdecl, olddecl);
1203 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1205 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1206 && TREE_CODE (newdecl) != TYPE_DECL
1207 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1208 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1209 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1210 && TREE_CODE (olddecl) != TYPE_DECL
1211 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1212 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1215 /* We do nothing special here, because C++ does such nasty
1216 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1217 get shadowed, and know that if we need to find a TYPE_DECL
1218 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1219 slot of the identifier. */
1223 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1224 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1225 || (TREE_CODE (olddecl) == FUNCTION_DECL
1226 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1229 error ("%q#D redeclared as different kind of symbol", newdecl);
1230 if (TREE_CODE (olddecl) == TREE_LIST)
1231 olddecl = TREE_VALUE (olddecl);
1232 error ("previous declaration of %q+#D", olddecl);
1234 return error_mark_node;
1236 else if (!types_match)
1238 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1239 /* These are certainly not duplicate declarations; they're
1240 from different scopes. */
1243 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1245 /* The name of a class template may not be declared to refer to
1246 any other template, class, function, object, namespace, value,
1247 or type in the same scope. */
1248 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1249 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1251 error ("declaration of template %q#D", newdecl);
1252 error ("conflicts with previous declaration %q+#D", olddecl);
1254 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1255 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1256 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1257 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1258 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1259 DECL_TEMPLATE_PARMS (olddecl))
1260 /* Template functions can be disambiguated by
1262 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1263 TREE_TYPE (TREE_TYPE (olddecl))))
1265 error ("new declaration %q#D", newdecl);
1266 error ("ambiguates old declaration %q+#D", olddecl);
1270 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1272 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1274 error ("declaration of C function %q#D conflicts with",
1276 error ("previous declaration %q+#D here", olddecl);
1278 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1279 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1281 error ("new declaration %q#D", newdecl);
1282 error ("ambiguates old declaration %q+#D", olddecl);
1289 error ("conflicting declaration %q#D", newdecl);
1290 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1291 return error_mark_node;
1294 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1295 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1296 && (!DECL_TEMPLATE_INFO (newdecl)
1297 || (DECL_TI_TEMPLATE (newdecl)
1298 != DECL_TI_TEMPLATE (olddecl))))
1299 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1300 && (!DECL_TEMPLATE_INFO (olddecl)
1301 || (DECL_TI_TEMPLATE (olddecl)
1302 != DECL_TI_TEMPLATE (newdecl))))))
1303 /* It's OK to have a template specialization and a non-template
1304 with the same type, or to have specializations of two
1305 different templates with the same type. Note that if one is a
1306 specialization, and the other is an instantiation of the same
1307 template, that we do not exit at this point. That situation
1308 can occur if we instantiate a template class, and then
1309 specialize one of its methods. This situation is valid, but
1310 the declarations must be merged in the usual way. */
1312 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1313 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1314 && !DECL_USE_TEMPLATE (newdecl))
1315 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1316 && !DECL_USE_TEMPLATE (olddecl))))
1317 /* One of the declarations is a template instantiation, and the
1318 other is not a template at all. That's OK. */
1320 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1322 /* In [namespace.alias] we have:
1324 In a declarative region, a namespace-alias-definition can be
1325 used to redefine a namespace-alias declared in that declarative
1326 region to refer only to the namespace to which it already
1329 Therefore, if we encounter a second alias directive for the same
1330 alias, we can just ignore the second directive. */
1331 if (DECL_NAMESPACE_ALIAS (newdecl)
1332 && (DECL_NAMESPACE_ALIAS (newdecl)
1333 == DECL_NAMESPACE_ALIAS (olddecl)))
1335 /* [namespace.alias]
1337 A namespace-name or namespace-alias shall not be declared as
1338 the name of any other entity in the same declarative region.
1339 A namespace-name defined at global scope shall not be
1340 declared as the name of any other entity in any global scope
1342 error ("declaration of namespace %qD conflicts with", newdecl);
1343 error ("previous declaration of namespace %q+D here", olddecl);
1344 return error_mark_node;
1348 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1351 error (errmsg, newdecl);
1352 if (DECL_NAME (olddecl) != NULL_TREE)
1353 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1354 ? "%q+#D previously defined here"
1355 : "%q+#D previously declared here", olddecl);
1356 return error_mark_node;
1358 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1359 && DECL_INITIAL (olddecl) != NULL_TREE
1360 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1361 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1363 /* Prototype decl follows defn w/o prototype. */
1364 warning (0, "prototype for %q+#D", newdecl);
1365 warning (0, "%Jfollows non-prototype definition here", olddecl);
1367 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1368 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1370 /* extern "C" int foo ();
1371 int foo () { bar (); }
1373 if (current_lang_depth () == 0)
1374 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1377 error ("previous declaration of %q+#D with %qL linkage",
1378 olddecl, DECL_LANGUAGE (olddecl));
1379 error ("conflicts with new declaration with %qL linkage",
1380 DECL_LANGUAGE (newdecl));
1384 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1386 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1388 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1389 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1392 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1393 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1395 for (; t1 && t1 != void_list_node;
1396 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1397 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1399 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1402 pedwarn ("default argument given for parameter %d of %q#D",
1404 pedwarn ("after previous specification in %q+#D", olddecl);
1408 error ("default argument given for parameter %d of %q#D",
1410 error ("after previous specification in %q+#D",
1415 if (DECL_DECLARED_INLINE_P (newdecl)
1416 && ! DECL_DECLARED_INLINE_P (olddecl)
1417 && TREE_ADDRESSABLE (olddecl) && warn_inline)
1419 warning (0, "%q#D was used before it was declared inline", newdecl);
1420 warning (0, "%Jprevious non-inline declaration here", olddecl);
1425 /* Do not merge an implicit typedef with an explicit one. In:
1429 typedef class A A __attribute__ ((foo));
1431 the attribute should apply only to the typedef. */
1432 if (TREE_CODE (olddecl) == TYPE_DECL
1433 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1434 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1437 /* If new decl is `static' and an `extern' was seen previously,
1439 warn_extern_redeclared_static (newdecl, olddecl);
1441 /* We have committed to returning 1 at this point. */
1442 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1444 /* Now that functions must hold information normally held
1445 by field decls, there is extra work to do so that
1446 declaration information does not get destroyed during
1448 if (DECL_VINDEX (olddecl))
1449 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1450 if (DECL_CONTEXT (olddecl))
1451 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1452 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1453 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1454 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1455 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1456 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1457 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1458 SET_OVERLOADED_OPERATOR_CODE
1459 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1460 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1462 /* Optionally warn about more than one declaration for the same
1463 name, but don't warn about a function declaration followed by a
1465 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1466 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1467 /* Don't warn about extern decl followed by definition. */
1468 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1469 /* Don't warn about friends, let add_friend take care of it. */
1470 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1472 warning (0, "redundant redeclaration of %qD in same scope", newdecl);
1473 warning (0, "previous declaration of %q+D", olddecl);
1477 /* Deal with C++: must preserve virtual function table size. */
1478 if (TREE_CODE (olddecl) == TYPE_DECL)
1480 tree newtype = TREE_TYPE (newdecl);
1481 tree oldtype = TREE_TYPE (olddecl);
1483 if (newtype != error_mark_node && oldtype != error_mark_node
1484 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1485 CLASSTYPE_FRIEND_CLASSES (newtype)
1486 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1488 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1491 /* Copy all the DECL_... slots specified in the new decl
1492 except for any that we copy here from the old type. */
1493 DECL_ATTRIBUTES (newdecl)
1494 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1496 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1498 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
1499 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1500 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1501 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1503 /* If the new declaration is a definition, update the file and
1504 line information on the declaration. */
1505 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
1506 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
1508 DECL_SOURCE_LOCATION (olddecl)
1509 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
1510 = DECL_SOURCE_LOCATION (newdecl);
1511 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1512 DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (olddecl))
1513 = DECL_ARGUMENTS (DECL_TEMPLATE_RESULT (newdecl));
1516 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1518 DECL_INLINE (DECL_TEMPLATE_RESULT (olddecl))
1519 |= DECL_INLINE (DECL_TEMPLATE_RESULT (newdecl));
1520 DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (olddecl))
1521 |= DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (newdecl));
1529 /* Automatically handles default parameters. */
1530 tree oldtype = TREE_TYPE (olddecl);
1533 /* Merge the data types specified in the two decls. */
1534 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1536 /* If merge_types produces a non-typedef type, just use the old type. */
1537 if (TREE_CODE (newdecl) == TYPE_DECL
1538 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1541 if (TREE_CODE (newdecl) == VAR_DECL)
1543 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1544 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1545 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1546 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1547 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1548 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1551 /* Do this after calling `merge_types' so that default
1552 parameters don't confuse us. */
1553 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1554 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
1555 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
1557 TREE_TYPE (newdecl) = build_exception_variant (newtype,
1558 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
1559 TREE_TYPE (olddecl) = build_exception_variant (newtype,
1560 TYPE_RAISES_EXCEPTIONS (oldtype));
1562 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
1563 && ! DECL_IS_BUILTIN (olddecl)
1565 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
1566 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
1568 error ("declaration of %qF throws different exceptions",
1570 error ("than previous declaration %q+F", olddecl);
1573 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1575 /* Lay the type out, unless already done. */
1576 if (! same_type_p (newtype, oldtype)
1577 && TREE_TYPE (newdecl) != error_mark_node
1578 && !(processing_template_decl && uses_template_parms (newdecl)))
1579 layout_type (TREE_TYPE (newdecl));
1581 if ((TREE_CODE (newdecl) == VAR_DECL
1582 || TREE_CODE (newdecl) == PARM_DECL
1583 || TREE_CODE (newdecl) == RESULT_DECL
1584 || TREE_CODE (newdecl) == FIELD_DECL
1585 || TREE_CODE (newdecl) == TYPE_DECL)
1586 && !(processing_template_decl && uses_template_parms (newdecl)))
1587 layout_decl (newdecl, 0);
1589 /* Merge the type qualifiers. */
1590 if (TREE_READONLY (newdecl))
1591 TREE_READONLY (olddecl) = 1;
1592 if (TREE_THIS_VOLATILE (newdecl))
1593 TREE_THIS_VOLATILE (olddecl) = 1;
1594 if (TREE_NOTHROW (newdecl))
1595 TREE_NOTHROW (olddecl) = 1;
1597 /* Merge deprecatedness. */
1598 if (TREE_DEPRECATED (newdecl))
1599 TREE_DEPRECATED (olddecl) = 1;
1601 /* Merge the initialization information. */
1602 if (DECL_INITIAL (newdecl) == NULL_TREE
1603 && DECL_INITIAL (olddecl) != NULL_TREE)
1605 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1606 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1607 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1608 && DECL_LANG_SPECIFIC (newdecl)
1609 && DECL_LANG_SPECIFIC (olddecl))
1611 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1612 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1616 /* Merge the section attribute.
1617 We want to issue an error if the sections conflict but that must be
1618 done later in decl_attributes since we are called before attributes
1620 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1621 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1623 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1625 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1626 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1627 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1628 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1629 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1630 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1631 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1632 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1633 /* Keep the old RTL. */
1634 COPY_DECL_RTL (olddecl, newdecl);
1636 else if (TREE_CODE (newdecl) == VAR_DECL
1637 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1639 /* Keep the old RTL. We cannot keep the old RTL if the old
1640 declaration was for an incomplete object and the new
1641 declaration is not since many attributes of the RTL will
1643 COPY_DECL_RTL (olddecl, newdecl);
1646 /* If cannot merge, then use the new type and qualifiers,
1647 and don't preserve the old rtl. */
1650 /* Clean out any memory we had of the old declaration. */
1651 tree oldstatic = value_member (olddecl, static_aggregates);
1653 TREE_VALUE (oldstatic) = error_mark_node;
1655 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1656 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1657 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1658 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1661 /* Merge the storage class information. */
1662 merge_weak (newdecl, olddecl);
1664 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1665 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1666 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1667 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1668 if (! DECL_EXTERNAL (olddecl))
1669 DECL_EXTERNAL (newdecl) = 0;
1671 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1673 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1674 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1675 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1676 DECL_TEMPLATE_INSTANTIATED (newdecl)
1677 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1679 /* If the OLDDECL is an instantiation and/or specialization,
1680 then the NEWDECL must be too. But, it may not yet be marked
1681 as such if the caller has created NEWDECL, but has not yet
1682 figured out that it is a redeclaration. */
1683 if (!DECL_USE_TEMPLATE (newdecl))
1684 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1686 /* Don't really know how much of the language-specific
1687 values we should copy from old to new. */
1688 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1689 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1690 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1691 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1692 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1693 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1694 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1695 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1696 olddecl_friend = DECL_FRIEND_P (olddecl);
1697 hidden_friend = (DECL_ANTICIPATED (olddecl)
1698 && DECL_HIDDEN_FRIEND_P (olddecl)
1699 && newdecl_is_friend);
1701 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1702 if (TREE_CODE (newdecl) == FUNCTION_DECL
1703 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1705 DECL_BEFRIENDING_CLASSES (newdecl)
1706 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1707 DECL_BEFRIENDING_CLASSES (olddecl));
1708 /* DECL_THUNKS is only valid for virtual functions,
1709 otherwise it is a DECL_FRIEND_CONTEXT. */
1710 if (DECL_VIRTUAL_P (newdecl))
1711 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1715 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1717 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1718 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1720 /* If newdecl is not a specialization, then it is not a
1721 template-related function at all. And that means that we
1722 should have exited above, returning 0. */
1723 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1725 if (TREE_USED (olddecl))
1726 /* From [temp.expl.spec]:
1728 If a template, a member template or the member of a class
1729 template is explicitly specialized then that
1730 specialization shall be declared before the first use of
1731 that specialization that would cause an implicit
1732 instantiation to take place, in every translation unit in
1733 which such a use occurs. */
1734 error ("explicit specialization of %qD after first use",
1737 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1739 /* [temp.expl.spec/14] We don't inline explicit specialization
1740 just because the primary template says so. */
1744 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1745 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1747 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1749 /* If either decl says `inline', this fn is inline, unless
1750 its definition was passed already. */
1751 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1752 DECL_INLINE (olddecl) = 1;
1753 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1755 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1756 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1759 /* Preserve abstractness on cloned [cd]tors. */
1760 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1764 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1765 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1766 COPY_DECL_RTL (newdecl, olddecl);
1768 if (! types_match || new_defines_function)
1770 /* These need to be copied so that the names are available.
1771 Note that if the types do match, we'll preserve inline
1772 info and other bits, but if not, we won't. */
1773 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1774 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
1776 if (new_defines_function)
1777 /* If defining a function declared with other language
1778 linkage, use the previously declared language linkage. */
1779 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1780 else if (types_match)
1782 /* If redeclaring a builtin function, and not a definition,
1783 it stays built in. */
1784 if (DECL_BUILT_IN (olddecl))
1786 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1787 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1788 /* If we're keeping the built-in definition, keep the rtl,
1789 regardless of declaration matches. */
1790 COPY_DECL_RTL (olddecl, newdecl);
1793 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1794 /* Don't clear out the arguments if we're redefining a function. */
1795 if (DECL_ARGUMENTS (olddecl))
1796 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1799 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1800 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
1802 /* Now preserve various other info from the definition. */
1803 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1804 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1805 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1806 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1808 /* Warn about conflicting visibility specifications. */
1809 if (DECL_VISIBILITY_SPECIFIED (olddecl)
1810 && DECL_VISIBILITY_SPECIFIED (newdecl)
1811 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1813 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
1814 "because it", newdecl);
1815 warning (OPT_Wattributes, "%Jconflicts with previous "
1816 "declaration here", olddecl);
1818 /* Choose the declaration which specified visibility. */
1819 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1821 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1822 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1824 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
1825 so keep this behavior. */
1826 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
1828 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
1829 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
1832 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
1833 with that from NEWDECL below. */
1834 if (DECL_LANG_SPECIFIC (olddecl))
1836 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1837 != DECL_LANG_SPECIFIC (newdecl));
1838 ggc_free (DECL_LANG_SPECIFIC (olddecl));
1841 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1845 function_size = sizeof (struct tree_decl_common);
1847 memcpy ((char *) olddecl + sizeof (struct tree_common),
1848 (char *) newdecl + sizeof (struct tree_common),
1849 function_size - sizeof (struct tree_common));
1851 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1852 (char *) newdecl + sizeof (struct tree_decl_common),
1853 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
1854 if (DECL_TEMPLATE_INFO (newdecl))
1855 /* If newdecl is a template instantiation, it is possible that
1856 the following sequence of events has occurred:
1858 o A friend function was declared in a class template. The
1859 class template was instantiated.
1861 o The instantiation of the friend declaration was
1862 recorded on the instantiation list, and is newdecl.
1864 o Later, however, instantiate_class_template called pushdecl
1865 on the newdecl to perform name injection. But, pushdecl in
1866 turn called duplicate_decls when it discovered that another
1867 declaration of a global function with the same name already
1870 o Here, in duplicate_decls, we decided to clobber newdecl.
1872 If we're going to do that, we'd better make sure that
1873 olddecl, and not newdecl, is on the list of
1874 instantiations so that if we try to do the instantiation
1875 again we won't get the clobbered declaration. */
1876 reregister_specialization (newdecl,
1877 DECL_TI_TEMPLATE (newdecl),
1882 size_t size = tree_code_size (TREE_CODE (olddecl));
1883 memcpy ((char *) olddecl + sizeof (struct tree_common),
1884 (char *) newdecl + sizeof (struct tree_common),
1885 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
1886 switch (TREE_CODE (olddecl))
1896 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1897 (char *) newdecl + sizeof (struct tree_decl_common),
1898 size - sizeof (struct tree_decl_common)
1899 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
1903 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1904 (char *) newdecl + sizeof (struct tree_decl_common),
1905 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
1906 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
1910 DECL_UID (olddecl) = olddecl_uid;
1912 DECL_FRIEND_P (olddecl) = 1;
1915 DECL_ANTICIPATED (olddecl) = 1;
1916 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
1919 /* NEWDECL contains the merged attribute lists.
1920 Update OLDDECL to be the same. */
1921 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
1923 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
1924 so that encode_section_info has a chance to look at the new decl
1925 flags and attributes. */
1926 if (DECL_RTL_SET_P (olddecl)
1927 && (TREE_CODE (olddecl) == FUNCTION_DECL
1928 || (TREE_CODE (olddecl) == VAR_DECL
1929 && TREE_STATIC (olddecl))))
1930 make_decl_rtl (olddecl);
1932 /* The NEWDECL will no longer be needed. Because every out-of-class
1933 declaration of a member results in a call to duplicate_decls,
1934 freeing these nodes represents in a significant savings. */
1940 /* Return zero if the declaration NEWDECL is valid
1941 when the declaration OLDDECL (assumed to be for the same name)
1942 has already been seen.
1943 Otherwise return an error message format string with a %s
1944 where the identifier should go. */
1947 redeclaration_error_message (tree newdecl, tree olddecl)
1949 if (TREE_CODE (newdecl) == TYPE_DECL)
1951 /* Because C++ can put things into name space for free,
1952 constructs like "typedef struct foo { ... } foo"
1953 would look like an erroneous redeclaration. */
1954 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
1957 return "redefinition of %q#D";
1959 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1961 /* If this is a pure function, its olddecl will actually be
1962 the original initialization to `0' (which we force to call
1963 abort()). Don't complain about redefinition in this case. */
1964 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
1965 && DECL_INITIAL (olddecl) == NULL_TREE)
1968 /* If both functions come from different namespaces, this is not
1969 a redeclaration - this is a conflict with a used function. */
1970 if (DECL_NAMESPACE_SCOPE_P (olddecl)
1971 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
1972 && ! decls_match (olddecl, newdecl))
1973 return "%qD conflicts with used function";
1975 /* We'll complain about linkage mismatches in
1976 warn_extern_redeclared_static. */
1978 /* Defining the same name twice is no good. */
1979 if (DECL_INITIAL (olddecl) != NULL_TREE
1980 && DECL_INITIAL (newdecl) != NULL_TREE)
1982 if (DECL_NAME (olddecl) == NULL_TREE)
1983 return "%q#D not declared in class";
1985 return "redefinition of %q#D";
1989 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1993 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1995 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
1996 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
1997 return "redefinition of %q#D";
2001 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2002 || (DECL_TEMPLATE_RESULT (newdecl)
2003 == DECL_TEMPLATE_RESULT (olddecl)))
2006 nt = DECL_TEMPLATE_RESULT (newdecl);
2007 if (DECL_TEMPLATE_INFO (nt))
2008 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2009 ot = DECL_TEMPLATE_RESULT (olddecl);
2010 if (DECL_TEMPLATE_INFO (ot))
2011 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2012 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2013 return "redefinition of %q#D";
2017 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2019 /* Objects declared at top level: */
2020 /* If at least one is a reference, it's ok. */
2021 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2023 /* Reject two definitions. */
2024 return "redefinition of %q#D";
2028 /* Objects declared with block scope: */
2029 /* Reject two definitions, and reject a definition
2030 together with an external reference. */
2031 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2032 return "redeclaration of %q#D";
2037 /* Create a new label, named ID. */
2040 make_label_decl (tree id, int local_p)
2044 decl = build_decl (LABEL_DECL, id, void_type_node);
2046 DECL_CONTEXT (decl) = current_function_decl;
2047 DECL_MODE (decl) = VOIDmode;
2048 C_DECLARED_LABEL_FLAG (decl) = local_p;
2050 /* Say where one reference is to the label, for the sake of the
2051 error if it is not defined. */
2052 DECL_SOURCE_LOCATION (decl) = input_location;
2054 /* Record the fact that this identifier is bound to this label. */
2055 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2060 /* Record this label on the list of used labels so that we can check
2061 at the end of the function to see whether or not the label was
2062 actually defined, and so we can check when the label is defined whether
2063 this use is valid. */
2066 use_label (tree decl)
2068 if (named_label_uses == NULL
2069 || named_label_uses->names_in_scope != current_binding_level->names
2070 || named_label_uses->label_decl != decl)
2072 struct named_label_use_list *new_ent;
2073 new_ent = GGC_NEW (struct named_label_use_list);
2074 new_ent->label_decl = decl;
2075 new_ent->names_in_scope = current_binding_level->names;
2076 new_ent->binding_level = current_binding_level;
2077 new_ent->o_goto_locus = input_location;
2078 new_ent->next = named_label_uses;
2079 named_label_uses = new_ent;
2083 /* Look for a label named ID in the current function. If one cannot
2084 be found, create one. (We keep track of used, but undefined,
2085 labels, and complain about them at the end of a function.) */
2088 lookup_label (tree id)
2091 struct named_label_list *ent;
2093 timevar_push (TV_NAME_LOOKUP);
2094 /* You can't use labels at global scope. */
2095 if (current_function_decl == NULL_TREE)
2097 error ("label %qE referenced outside of any function", id);
2098 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2101 /* See if we've already got this label. */
2102 decl = IDENTIFIER_LABEL_VALUE (id);
2103 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2104 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2106 /* Record this label on the list of labels used in this function.
2107 We do this before calling make_label_decl so that we get the
2108 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2109 ent = GGC_CNEW (struct named_label_list);
2110 ent->old_value = IDENTIFIER_LABEL_VALUE (id);
2111 ent->next = named_labels;
2114 /* We need a new label. */
2115 decl = make_label_decl (id, /*local_p=*/0);
2117 /* Now fill in the information we didn't have before. */
2118 ent->label_decl = decl;
2120 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2123 /* Declare a local label named ID. */
2126 declare_local_label (tree id)
2130 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2131 this scope we can restore the old value of
2132 IDENTIFIER_TYPE_VALUE. */
2133 current_binding_level->shadowed_labels
2134 = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2135 current_binding_level->shadowed_labels);
2136 /* Look for the label. */
2137 decl = make_label_decl (id, /*local_p=*/1);
2138 /* Now fill in the information we didn't have before. */
2139 TREE_VALUE (current_binding_level->shadowed_labels) = decl;
2144 /* Returns nonzero if it is ill-formed to jump past the declaration of
2145 DECL. Returns 2 if it's also a real problem. */
2148 decl_jump_unsafe (tree decl)
2150 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
2153 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2154 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2157 if (pod_type_p (TREE_TYPE (decl)))
2160 /* The POD stuff is just pedantry; why should it matter if the class
2161 contains a field of pointer to member type? */
2165 /* Check that a single previously seen jump to a newly defined label
2166 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2167 the jump context; NAMES are the names in scope in LEVEL at the jump
2168 context; FILE and LINE are the source position of the jump or 0. */
2171 check_previous_goto_1 (tree decl,
2172 struct cp_binding_level* level,
2173 tree names, const location_t *locus)
2177 struct cp_binding_level *b = current_binding_level;
2178 for (; b; b = b->level_chain)
2180 tree new_decls = b->names;
2181 tree old_decls = (b == level ? names : NULL_TREE);
2182 for (; new_decls != old_decls;
2183 new_decls = TREE_CHAIN (new_decls))
2185 int problem = decl_jump_unsafe (new_decls);
2192 pedwarn ("jump to label %qD", decl);
2194 pedwarn ("jump to case label");
2197 pedwarn ("%H from here", locus);
2202 error (" crosses initialization of %q+#D", new_decls);
2204 pedwarn (" enters scope of non-POD %q+#D", new_decls);
2209 if ((b->kind == sk_try || b->kind == sk_catch) && ! saw_eh)
2214 pedwarn ("jump to label %qD", decl);
2216 pedwarn ("jump to case label");
2219 pedwarn ("%H from here", locus);
2222 if (b->kind == sk_try)
2223 error (" enters try block");
2225 error (" enters catch block");
2232 check_previous_goto (struct named_label_use_list* use)
2234 check_previous_goto_1 (use->label_decl, use->binding_level,
2235 use->names_in_scope, &use->o_goto_locus);
2239 check_switch_goto (struct cp_binding_level* level)
2241 check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
2244 /* Check that any previously seen jumps to a newly defined label DECL
2245 are OK. Called by define_label. */
2248 check_previous_gotos (tree decl)
2250 struct named_label_use_list **usep;
2252 if (! TREE_USED (decl))
2255 for (usep = &named_label_uses; *usep; )
2257 struct named_label_use_list *use = *usep;
2258 if (use->label_decl == decl)
2260 check_previous_goto (use);
2264 usep = &(use->next);
2268 /* Check that a new jump to a label DECL is OK. Called by
2269 finish_goto_stmt. */
2272 check_goto (tree decl)
2276 struct named_label_list *lab;
2278 /* We can't know where a computed goto is jumping. So we assume
2280 if (! DECL_P (decl))
2283 /* If the label hasn't been defined yet, defer checking. */
2284 if (! DECL_INITIAL (decl))
2290 for (lab = named_labels; lab; lab = lab->next)
2291 if (decl == lab->label_decl)
2294 /* If the label is not on named_labels it's a gcc local label, so
2295 it must be in an outer scope, so jumping to it is always OK. */
2299 if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
2302 pedwarn ("jump to label %q+D", decl);
2303 pedwarn (" from here");
2307 for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
2309 tree b = TREE_VALUE (bad);
2310 int u = decl_jump_unsafe (b);
2312 if (u > 1 && DECL_ARTIFICIAL (b))
2313 /* Can't skip init of __exception_info. */
2314 error ("%J enters catch block", b);
2316 error (" skips initialization of %q+#D", b);
2318 pedwarn (" enters scope of non-POD %q+#D", b);
2321 if (lab->in_try_scope)
2322 error (" enters try block");
2323 else if (lab->in_catch_scope)
2324 error (" enters catch block");
2327 /* Define a label, specifying the location in the source file.
2328 Return the LABEL_DECL node for the label. */
2331 define_label (location_t location, tree name)
2333 tree decl = lookup_label (name);
2334 struct named_label_list *ent;
2335 struct cp_binding_level *p;
2337 timevar_push (TV_NAME_LOOKUP);
2338 for (ent = named_labels; ent; ent = ent->next)
2339 if (ent->label_decl == decl)
2342 /* After labels, make any new cleanups in the function go into their
2343 own new (temporary) binding contour. */
2344 for (p = current_binding_level;
2345 p->kind != sk_function_parms;
2347 p->more_cleanups_ok = 0;
2349 if (name == get_identifier ("wchar_t"))
2350 pedwarn ("label named wchar_t");
2352 if (DECL_INITIAL (decl) != NULL_TREE)
2353 error ("duplicate label %qD", decl);
2356 /* Mark label as having been defined. */
2357 DECL_INITIAL (decl) = error_mark_node;
2358 /* Say where in the source. */
2359 DECL_SOURCE_LOCATION (decl) = location;
2362 ent->names_in_scope = current_binding_level->names;
2363 ent->binding_level = current_binding_level;
2365 check_previous_gotos (decl);
2368 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2373 struct cp_binding_level *level;
2374 struct cp_switch *next;
2375 /* The SWITCH_STMT being built. */
2377 /* A splay-tree mapping the low element of a case range to the high
2378 element, or NULL_TREE if there is no high element. Used to
2379 determine whether or not a new case label duplicates an old case
2380 label. We need a tree, rather than simply a hash table, because
2381 of the GNU case range extension. */
2385 /* A stack of the currently active switch statements. The innermost
2386 switch statement is on the top of the stack. There is no need to
2387 mark the stack for garbage collection because it is only active
2388 during the processing of the body of a function, and we never
2389 collect at that point. */
2391 static struct cp_switch *switch_stack;
2393 /* Called right after a switch-statement condition is parsed.
2394 SWITCH_STMT is the switch statement being parsed. */
2397 push_switch (tree switch_stmt)
2399 struct cp_switch *p = xmalloc (sizeof (struct cp_switch));
2400 p->level = current_binding_level;
2401 p->next = switch_stack;
2402 p->switch_stmt = switch_stmt;
2403 p->cases = splay_tree_new (case_compare, NULL, NULL);
2410 struct cp_switch *cs = switch_stack;
2411 location_t switch_location;
2413 /* Emit warnings as needed. */
2414 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2415 switch_location = EXPR_LOCATION (cs->switch_stmt);
2417 switch_location = input_location;
2418 if (!processing_template_decl)
2419 c_do_switch_warnings (cs->cases, switch_location,
2420 SWITCH_STMT_TYPE (cs->switch_stmt),
2421 SWITCH_STMT_COND (cs->switch_stmt));
2423 splay_tree_delete (cs->cases);
2424 switch_stack = switch_stack->next;
2428 /* Note that we've seen a definition of a case label, and complain if this
2429 is a bad place for one. */
2432 finish_case_label (tree low_value, tree high_value)
2435 struct cp_binding_level *p;
2437 if (processing_template_decl)
2441 /* For templates, just add the case label; we'll do semantic
2442 analysis at instantiation-time. */
2443 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2444 return add_stmt (build_case_label (low_value, high_value, label));
2447 /* Find the condition on which this switch statement depends. */
2448 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2449 if (cond && TREE_CODE (cond) == TREE_LIST)
2450 cond = TREE_VALUE (cond);
2452 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2453 low_value, high_value);
2455 check_switch_goto (switch_stack->level);
2457 /* After labels, make any new cleanups in the function go into their
2458 own new (temporary) binding contour. */
2459 for (p = current_binding_level;
2460 p->kind != sk_function_parms;
2462 p->more_cleanups_ok = 0;
2467 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2470 typename_hash (const void* k)
2475 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2476 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2481 typedef struct typename_info {
2489 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2492 typename_compare (const void * k1, const void * k2)
2495 const typename_info *t2;
2498 t2 = (const typename_info *) k2;
2500 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2501 && TYPE_CONTEXT (t1) == t2->scope
2502 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2503 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2504 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2507 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2508 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2510 Returns the new TYPENAME_TYPE. */
2512 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2515 build_typename_type (tree context, tree name, tree fullname,
2516 enum tag_types tag_type)
2524 if (typename_htab == NULL)
2525 typename_htab = htab_create_ggc (61, &typename_hash,
2526 &typename_compare, NULL);
2528 ti.scope = FROB_CONTEXT (context);
2530 ti.template_id = fullname;
2531 ti.enum_p = tag_type == enum_type;
2532 ti.class_p = (tag_type == class_type
2533 || tag_type == record_type
2534 || tag_type == union_type);
2535 hash = (htab_hash_pointer (ti.scope)
2536 ^ htab_hash_pointer (ti.name));
2538 /* See if we already have this type. */
2539 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2544 /* Build the TYPENAME_TYPE. */
2545 t = make_aggr_type (TYPENAME_TYPE);
2546 TYPE_CONTEXT (t) = ti.scope;
2547 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2548 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2549 TYPENAME_IS_CLASS_P (t) = ti.class_p;
2551 /* Build the corresponding TYPE_DECL. */
2552 d = build_decl (TYPE_DECL, name, t);
2553 TYPE_NAME (TREE_TYPE (d)) = d;
2554 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2555 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2556 DECL_ARTIFICIAL (d) = 1;
2558 /* Store it in the hash table. */
2565 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2566 provided to name the type. Returns an appropriate type, unless an
2567 error occurs, in which case error_mark_node is returned. If we
2568 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2569 return that, rather than the _TYPE it corresponds to, in other
2570 cases we look through the type decl. If TF_ERROR is set, complain
2571 about errors, otherwise be quiet. */
2574 make_typename_type (tree context, tree name, enum tag_types tag_type,
2575 tsubst_flags_t complain)
2579 if (name == error_mark_node
2580 || context == NULL_TREE
2581 || context == error_mark_node)
2582 return error_mark_node;
2586 if (!(TYPE_LANG_SPECIFIC (name)
2587 && (CLASSTYPE_IS_TEMPLATE (name)
2588 || CLASSTYPE_USE_TEMPLATE (name))))
2589 name = TYPE_IDENTIFIER (name);
2591 /* Create a TEMPLATE_ID_EXPR for the type. */
2592 name = build_nt (TEMPLATE_ID_EXPR,
2593 CLASSTYPE_TI_TEMPLATE (name),
2594 CLASSTYPE_TI_ARGS (name));
2596 else if (TREE_CODE (name) == TYPE_DECL)
2597 name = DECL_NAME (name);
2601 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2603 name = TREE_OPERAND (name, 0);
2604 if (TREE_CODE (name) == TEMPLATE_DECL)
2605 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2607 if (TREE_CODE (name) == TEMPLATE_DECL)
2609 error ("%qD used without template parameters", name);
2610 return error_mark_node;
2612 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2613 gcc_assert (TYPE_P (context));
2615 if (!dependent_type_p (context)
2616 || currently_open_class (context))
2618 if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
2620 tree tmpl = NULL_TREE;
2621 if (IS_AGGR_TYPE (context))
2622 tmpl = lookup_field (context, name, 0, false);
2623 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2625 if (complain & tf_error)
2626 error ("no class template named %q#T in %q#T",
2628 return error_mark_node;
2631 if (complain & tf_error)
2632 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2634 return lookup_template_class (tmpl,
2635 TREE_OPERAND (fullname, 1),
2637 /*entering_scope=*/0,
2638 tf_error | tf_warning | tf_user);
2644 if (!IS_AGGR_TYPE (context))
2646 if (complain & tf_error)
2647 error ("no type named %q#T in %q#T", name, context);
2648 return error_mark_node;
2651 t = lookup_field (context, name, 0, true);
2654 if (TREE_CODE (t) != TYPE_DECL)
2656 if (complain & tf_error)
2657 error ("no type named %q#T in %q#T", name, context);
2658 return error_mark_node;
2661 if (complain & tf_error)
2662 perform_or_defer_access_check (TYPE_BINFO (context), t);
2664 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2672 /* If the CONTEXT is not a template type, then either the field is
2673 there now or its never going to be. */
2674 if (!dependent_type_p (context))
2676 if (complain & tf_error)
2677 error ("no type named %q#T in %q#T", name, context);
2678 return error_mark_node;
2681 return build_typename_type (context, name, fullname, tag_type);
2684 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
2685 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
2686 in which case error_mark_node is returned.
2688 If PARM_LIST is non-NULL, also make sure that the template parameter
2689 list of TEMPLATE_DECL matches.
2691 If COMPLAIN zero, don't complain about any errors that occur. */
2694 make_unbound_class_template (tree context, tree name, tree parm_list,
2695 tsubst_flags_t complain)
2701 name = TYPE_IDENTIFIER (name);
2702 else if (DECL_P (name))
2703 name = DECL_NAME (name);
2704 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2706 if (!dependent_type_p (context)
2707 || currently_open_class (context))
2709 tree tmpl = NULL_TREE;
2711 if (IS_AGGR_TYPE (context))
2712 tmpl = lookup_field (context, name, 0, false);
2714 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2716 if (complain & tf_error)
2717 error ("no class template named %q#T in %q#T", name, context);
2718 return error_mark_node;
2722 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
2724 if (complain & tf_error)
2726 error ("template parameters do not match template");
2727 error ("%q+D declared here", tmpl);
2729 return error_mark_node;
2732 if (complain & tf_error)
2733 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2738 /* Build the UNBOUND_CLASS_TEMPLATE. */
2739 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2740 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2741 TREE_TYPE (t) = NULL_TREE;
2743 /* Build the corresponding TEMPLATE_DECL. */
2744 d = build_decl (TEMPLATE_DECL, name, t);
2745 TYPE_NAME (TREE_TYPE (d)) = d;
2746 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2747 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2748 DECL_ARTIFICIAL (d) = 1;
2749 DECL_TEMPLATE_PARMS (d) = parm_list;
2756 /* Push the declarations of builtin types into the namespace.
2757 RID_INDEX is the index of the builtin type in the array
2758 RID_POINTERS. NAME is the name used when looking up the builtin
2759 type. TYPE is the _TYPE node for the builtin type. */
2762 record_builtin_type (enum rid rid_index,
2766 tree rname = NULL_TREE, tname = NULL_TREE;
2767 tree tdecl = NULL_TREE;
2769 if ((int) rid_index < (int) RID_MAX)
2770 rname = ridpointers[(int) rid_index];
2772 tname = get_identifier (name);
2774 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
2775 eliminated. Built-in types should not be looked up name; their
2776 names are keywords that the parser can recognize. However, there
2777 is code in c-common.c that uses identifier_global_value to look
2778 up built-in types by name. */
2781 tdecl = build_decl (TYPE_DECL, tname, type);
2782 DECL_ARTIFICIAL (tdecl) = 1;
2783 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
2789 tdecl = build_decl (TYPE_DECL, rname, type);
2790 DECL_ARTIFICIAL (tdecl) = 1;
2792 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
2795 if (!TYPE_NAME (type))
2796 TYPE_NAME (type) = tdecl;
2799 debug_hooks->type_decl (tdecl, 0);
2802 /* Record one of the standard Java types.
2803 * Declare it as having the given NAME.
2804 * If SIZE > 0, it is the size of one of the integral types;
2805 * otherwise it is the negative of the size of one of the other types. */
2808 record_builtin_java_type (const char* name, int size)
2812 type = make_signed_type (size);
2813 else if (size > -32)
2814 { /* "__java_char" or ""__java_boolean". */
2815 type = make_unsigned_type (-size);
2816 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
2819 { /* "__java_float" or ""__java_double". */
2820 type = make_node (REAL_TYPE);
2821 TYPE_PRECISION (type) = - size;
2824 record_builtin_type (RID_MAX, name, type);
2825 decl = TYPE_NAME (type);
2827 /* Suppress generate debug symbol entries for these types,
2828 since for normal C++ they are just clutter.
2829 However, push_lang_context undoes this if extern "Java" is seen. */
2830 DECL_IGNORED_P (decl) = 1;
2832 TYPE_FOR_JAVA (type) = 1;
2836 /* Push a type into the namespace so that the back-ends ignore it. */
2839 record_unknown_type (tree type, const char* name)
2841 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
2842 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
2843 DECL_IGNORED_P (decl) = 1;
2844 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
2845 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
2846 TYPE_ALIGN (type) = 1;
2847 TYPE_USER_ALIGN (type) = 0;
2848 TYPE_MODE (type) = TYPE_MODE (void_type_node);
2851 /* A string for which we should create an IDENTIFIER_NODE at
2854 typedef struct predefined_identifier
2856 /* The name of the identifier. */
2857 const char *const name;
2858 /* The place where the IDENTIFIER_NODE should be stored. */
2860 /* Nonzero if this is the name of a constructor or destructor. */
2861 const int ctor_or_dtor_p;
2862 } predefined_identifier;
2864 /* Create all the predefined identifiers. */
2867 initialize_predefined_identifiers (void)
2869 const predefined_identifier *pid;
2871 /* A table of identifiers to create at startup. */
2872 static const predefined_identifier predefined_identifiers[] = {
2873 { "C++", &lang_name_cplusplus, 0 },
2874 { "C", &lang_name_c, 0 },
2875 { "Java", &lang_name_java, 0 },
2876 /* Some of these names have a trailing space so that it is
2877 impossible for them to conflict with names written by users. */
2878 { "__ct ", &ctor_identifier, 1 },
2879 { "__base_ctor ", &base_ctor_identifier, 1 },
2880 { "__comp_ctor ", &complete_ctor_identifier, 1 },
2881 { "__dt ", &dtor_identifier, 1 },
2882 { "__comp_dtor ", &complete_dtor_identifier, 1 },
2883 { "__base_dtor ", &base_dtor_identifier, 1 },
2884 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
2885 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
2886 { "nelts", &nelts_identifier, 0 },
2887 { THIS_NAME, &this_identifier, 0 },
2888 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
2889 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
2890 { "_vptr", &vptr_identifier, 0 },
2891 { "__vtt_parm", &vtt_parm_identifier, 0 },
2892 { "::", &global_scope_name, 0 },
2893 { "std", &std_identifier, 0 },
2897 for (pid = predefined_identifiers; pid->name; ++pid)
2899 *pid->node = get_identifier (pid->name);
2900 if (pid->ctor_or_dtor_p)
2901 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
2905 /* Create the predefined scalar types of C,
2906 and some nodes representing standard constants (0, 1, (void *)0).
2907 Initialize the global binding level.
2908 Make definitions for built-in primitive functions. */
2911 cxx_init_decl_processing (void)
2914 tree void_ftype_ptr;
2916 build_common_tree_nodes (flag_signed_char, false);
2918 /* Create all the identifiers we need. */
2919 initialize_predefined_identifiers ();
2921 /* Create the global variables. */
2922 push_to_top_level ();
2924 current_function_decl = NULL_TREE;
2925 current_binding_level = NULL;
2926 /* Enter the global namespace. */
2927 gcc_assert (global_namespace == NULL_TREE);
2928 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
2930 begin_scope (sk_namespace, global_namespace);
2932 current_lang_name = NULL_TREE;
2934 /* Adjust various flags based on command-line settings. */
2935 if (!flag_permissive)
2936 flag_pedantic_errors = 1;
2937 if (!flag_no_inline)
2939 flag_inline_trees = 1;
2942 if (flag_inline_functions)
2943 flag_inline_trees = 2;
2945 /* Force minimum function alignment if using the least significant
2946 bit of function pointers to store the virtual bit. */
2947 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
2948 && force_align_functions_log < 1)
2949 force_align_functions_log = 1;
2952 current_lang_name = lang_name_c;
2954 error_mark_list = build_tree_list (error_mark_node, error_mark_node);
2955 TREE_TYPE (error_mark_list) = error_mark_node;
2957 /* Create the `std' namespace. */
2958 push_namespace (std_identifier);
2959 std_node = current_namespace;
2962 c_common_nodes_and_builtins ();
2964 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
2965 java_short_type_node = record_builtin_java_type ("__java_short", 16);
2966 java_int_type_node = record_builtin_java_type ("__java_int", 32);
2967 java_long_type_node = record_builtin_java_type ("__java_long", 64);
2968 java_float_type_node = record_builtin_java_type ("__java_float", -32);
2969 java_double_type_node = record_builtin_java_type ("__java_double", -64);
2970 java_char_type_node = record_builtin_java_type ("__java_char", -16);
2971 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
2973 integer_two_node = build_int_cst (NULL_TREE, 2);
2974 integer_three_node = build_int_cst (NULL_TREE, 3);
2976 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
2977 truthvalue_type_node = boolean_type_node;
2978 truthvalue_false_node = boolean_false_node;
2979 truthvalue_true_node = boolean_true_node;
2981 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
2984 record_builtin_type (RID_MAX, NULL, string_type_node);
2987 delta_type_node = ptrdiff_type_node;
2988 vtable_index_type = ptrdiff_type_node;
2990 vtt_parm_type = build_pointer_type (const_ptr_type_node);
2991 void_ftype = build_function_type (void_type_node, void_list_node);
2992 void_ftype_ptr = build_function_type (void_type_node,
2993 tree_cons (NULL_TREE,
2997 = build_exception_variant (void_ftype_ptr, empty_except_spec);
2999 /* C++ extensions */
3001 unknown_type_node = make_node (UNKNOWN_TYPE);
3002 record_unknown_type (unknown_type_node, "unknown type");
3004 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3005 TREE_TYPE (unknown_type_node) = unknown_type_node;
3007 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3009 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3010 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3013 /* Make sure we get a unique function type, so we can give
3014 its pointer type a name. (This wins for gdb.) */
3015 tree vfunc_type = make_node (FUNCTION_TYPE);
3016 TREE_TYPE (vfunc_type) = integer_type_node;
3017 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3018 layout_type (vfunc_type);
3020 vtable_entry_type = build_pointer_type (vfunc_type);
3022 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3025 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3026 layout_type (vtbl_type_node);
3027 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3028 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3029 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3030 layout_type (vtbl_ptr_type_node);
3031 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3033 push_namespace (get_identifier ("__cxxabiv1"));
3034 abi_node = current_namespace;
3037 global_type_node = make_node (LANG_TYPE);
3038 record_unknown_type (global_type_node, "global type");
3041 current_lang_name = lang_name_cplusplus;
3045 tree bad_alloc_type_node;
3046 tree bad_alloc_decl;
3047 tree newtype, deltype;
3048 tree ptr_ftype_sizetype;
3050 push_namespace (std_identifier);
3051 bad_alloc_id = get_identifier ("bad_alloc");
3052 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3053 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3055 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3056 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3057 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3061 = build_function_type (ptr_type_node,
3062 tree_cons (NULL_TREE,
3065 newtype = build_exception_variant
3066 (ptr_ftype_sizetype, add_exception_specifier
3067 (NULL_TREE, bad_alloc_type_node, -1));
3068 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3069 push_cp_library_fn (NEW_EXPR, newtype);
3070 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3071 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3072 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3076 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3078 /* Perform other language dependent initializations. */
3079 init_class_processing ();
3080 init_rtti_processing ();
3082 if (flag_exceptions)
3083 init_exception_processing ();
3085 if (! supports_one_only ())
3088 make_fname_decl = cp_make_fname_decl;
3089 start_fname_decls ();
3091 /* Show we use EH for cleanups. */
3092 if (flag_exceptions)
3093 using_eh_for_cleanups ();
3096 /* Generate an initializer for a function naming variable from
3097 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3098 filled in with the type of the init. */
3101 cp_fname_init (const char* name, tree *type_p)
3103 tree domain = NULL_TREE;
3105 tree init = NULL_TREE;
3110 length = strlen (name);
3111 domain = build_index_type (size_int (length));
3112 init = build_string (length + 1, name);
3115 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3116 type = build_cplus_array_type (type, domain);
3121 TREE_TYPE (init) = type;
3123 init = error_mark_node;
3128 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3129 decl, NAME is the initialization string and TYPE_DEP indicates whether
3130 NAME depended on the type of the function. We make use of that to detect
3131 __PRETTY_FUNCTION__ inside a template fn. This is being done
3132 lazily at the point of first use, so we mustn't push the decl now. */
3135 cp_make_fname_decl (tree id, int type_dep)
3137 const char *const name = (type_dep && processing_template_decl
3138 ? NULL : fname_as_string (type_dep));
3140 tree init = cp_fname_init (name, &type);
3141 tree decl = build_decl (VAR_DECL, id, type);
3144 free ((char *) name);
3146 /* As we're using pushdecl_with_scope, we must set the context. */
3147 DECL_CONTEXT (decl) = current_function_decl;
3148 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3150 TREE_STATIC (decl) = 1;
3151 TREE_READONLY (decl) = 1;
3152 DECL_ARTIFICIAL (decl) = 1;
3153 DECL_INITIAL (decl) = init;
3155 TREE_USED (decl) = 1;
3157 if (current_function_decl)
3159 struct cp_binding_level *b = current_binding_level;
3160 while (b->level_chain->kind != sk_function_parms)
3162 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3163 cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
3166 pushdecl_top_level_and_finish (decl, init);
3171 /* Make a definition for a builtin function named NAME in the current
3172 namespace, whose data type is TYPE and whose context is CONTEXT.
3173 TYPE should be a function type with argument types.
3175 CLASS and CODE tell later passes how to compile calls to this function.
3176 See tree.h for possible values.
3178 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3179 the name to be called if we can't opencode the function.
3180 If ATTRS is nonzero, use that for the function's attribute
3184 builtin_function_1 (const char* name,
3187 enum built_in_function code,
3188 enum built_in_class class,
3189 const char* libname,
3192 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
3193 DECL_BUILT_IN_CLASS (decl) = class;
3194 DECL_FUNCTION_CODE (decl) = code;
3195 DECL_CONTEXT (decl) = context;
3199 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
3200 we cannot change DECL_ASSEMBLER_NAME until we have installed this
3201 function in the namespace. */
3203 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
3205 /* A function in the user's namespace should have an explicit
3206 declaration before it is used. Mark the built-in function as
3207 anticipated but not actually declared. */
3208 if (name[0] != '_' || name[1] != '_')
3209 DECL_ANTICIPATED (decl) = 1;
3211 /* Possibly apply some default attributes to this built-in function. */
3213 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
3215 decl_attributes (&decl, NULL_TREE, 0);
3220 /* Entry point for the benefit of c_common_nodes_and_builtins.
3222 Make a definition for a builtin function named NAME and whose data type
3223 is TYPE. TYPE should be a function type with argument types. This
3224 function places the anticipated declaration in the global namespace
3225 and additionally in the std namespace if appropriate.
3227 CLASS and CODE tell later passes how to compile calls to this function.
3228 See tree.h for possible values.
3230 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3231 the name to be called if we can't opencode the function.
3233 If ATTRS is nonzero, use that for the function's attribute
3237 builtin_function (const char* name,
3240 enum built_in_class cl,
3241 const char* libname,
3244 /* All builtins that don't begin with an '_' should additionally
3245 go in the 'std' namespace. */
3248 push_namespace (std_identifier);
3249 builtin_function_1 (name, type, std_node, code, cl, libname, attrs);
3253 return builtin_function_1 (name, type, NULL_TREE, code,
3254 cl, libname, attrs);
3257 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3258 function. Not called directly. */
3261 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3263 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3264 DECL_EXTERNAL (fn) = 1;
3265 TREE_PUBLIC (fn) = 1;
3266 DECL_ARTIFICIAL (fn) = 1;
3267 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3268 SET_DECL_LANGUAGE (fn, lang_c);
3269 /* Runtime library routines are, by definition, available in an
3270 external shared object. */
3271 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3272 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3276 /* Returns the _DECL for a library function with C linkage.
3277 We assume that such functions never throw; if this is incorrect,
3278 callers should unset TREE_NOTHROW. */
3281 build_library_fn (tree name, tree type)
3283 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3284 TREE_NOTHROW (fn) = 1;
3288 /* Returns the _DECL for a library function with C++ linkage. */
3291 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3293 tree fn = build_library_fn_1 (name, operator_code, type);
3294 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3295 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3296 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3300 /* Like build_library_fn, but takes a C string instead of an
3304 build_library_fn_ptr (const char* name, tree type)
3306 return build_library_fn (get_identifier (name), type);
3309 /* Like build_cp_library_fn, but takes a C string instead of an
3313 build_cp_library_fn_ptr (const char* name, tree type)
3315 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3318 /* Like build_library_fn, but also pushes the function so that we will
3319 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3322 push_library_fn (tree name, tree type)
3324 tree fn = build_library_fn (name, type);
3325 pushdecl_top_level (fn);
3329 /* Like build_cp_library_fn, but also pushes the function so that it
3330 will be found by normal lookup. */
3333 push_cp_library_fn (enum tree_code operator_code, tree type)
3335 tree fn = build_cp_library_fn (ansi_opname (operator_code),
3342 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
3346 push_void_library_fn (tree name, tree parmtypes)
3348 tree type = build_function_type (void_type_node, parmtypes);
3349 return push_library_fn (name, type);
3352 /* Like push_library_fn, but also note that this function throws
3353 and does not return. Used for __throw_foo and the like. */
3356 push_throw_library_fn (tree name, tree type)
3358 tree fn = push_library_fn (name, type);
3359 TREE_THIS_VOLATILE (fn) = 1;
3360 TREE_NOTHROW (fn) = 0;
3364 /* When we call finish_struct for an anonymous union, we create
3365 default copy constructors and such. But, an anonymous union
3366 shouldn't have such things; this function undoes the damage to the
3367 anonymous union type T.
3369 (The reason that we create the synthesized methods is that we don't
3370 distinguish `union { int i; }' from `typedef union { int i; } U'.
3371 The first is an anonymous union; the second is just an ordinary
3375 fixup_anonymous_aggr (tree t)
3379 /* Wipe out memory of synthesized methods. */
3380 TYPE_HAS_CONSTRUCTOR (t) = 0;
3381 TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
3382 TYPE_HAS_INIT_REF (t) = 0;
3383 TYPE_HAS_CONST_INIT_REF (t) = 0;
3384 TYPE_HAS_ASSIGN_REF (t) = 0;
3385 TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
3387 /* Splice the implicitly generated functions out of the TYPE_METHODS
3389 q = &TYPE_METHODS (t);
3392 if (DECL_ARTIFICIAL (*q))
3393 *q = TREE_CHAIN (*q);
3395 q = &TREE_CHAIN (*q);
3398 /* ISO C++ 9.5.3. Anonymous unions may not have function members. */
3399 if (TYPE_METHODS (t))
3400 error ("%Jan anonymous union cannot have function members",
3401 TYPE_MAIN_DECL (t));
3403 /* Anonymous aggregates cannot have fields with ctors, dtors or complex
3404 assignment operators (because they cannot have these methods themselves).
3405 For anonymous unions this is already checked because they are not allowed
3406 in any union, otherwise we have to check it. */
3407 if (TREE_CODE (t) != UNION_TYPE)
3411 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3412 if (TREE_CODE (field) == FIELD_DECL)
3414 type = TREE_TYPE (field);
3415 if (CLASS_TYPE_P (type))
3417 if (TYPE_NEEDS_CONSTRUCTING (type))
3418 error ("member %q+#D with constructor not allowed "
3419 "in anonymous aggregate", field);
3420 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3421 error ("member %q+#D with destructor not allowed "
3422 "in anonymous aggregate", field);
3423 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3424 error ("member %q+#D with copy assignment operator "
3425 "not allowed in anonymous aggregate", field);
3431 /* Make sure that a declaration with no declarator is well-formed, i.e.
3432 just declares a tagged type or anonymous union.
3434 Returns the type declared; or NULL_TREE if none. */
3437 check_tag_decl (cp_decl_specifier_seq *declspecs)
3439 int saw_friend = declspecs->specs[(int)ds_friend] != 0;
3440 int saw_typedef = declspecs->specs[(int)ds_typedef] != 0;
3441 /* If a class, struct, or enum type is declared by the DECLSPECS
3442 (i.e, if a class-specifier, enum-specifier, or non-typename
3443 elaborated-type-specifier appears in the DECLSPECS),
3444 DECLARED_TYPE is set to the corresponding type. */
3445 tree declared_type = NULL_TREE;
3446 bool error_p = false;
3448 if (declspecs->multiple_types_p)
3449 error ("multiple types in one declaration");
3450 else if (declspecs->redefined_builtin_type)
3452 if (!in_system_header)
3453 pedwarn ("redeclaration of C++ built-in type %qT",
3454 declspecs->redefined_builtin_type);
3459 && TYPE_P (declspecs->type)
3460 && ((TREE_CODE (declspecs->type) != TYPENAME_TYPE
3461 && IS_AGGR_TYPE (declspecs->type))
3462 || TREE_CODE (declspecs->type) == ENUMERAL_TYPE))
3463 declared_type = declspecs->type;
3464 else if (declspecs->type == error_mark_node)
3466 if (declared_type == NULL_TREE && ! saw_friend && !error_p)
3467 pedwarn ("declaration does not declare anything");
3468 /* Check for an anonymous union. */
3469 else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
3470 && TYPE_ANONYMOUS_P (declared_type))
3472 /* 7/3 In a simple-declaration, the optional init-declarator-list
3473 can be omitted only when declaring a class (clause 9) or
3474 enumeration (7.2), that is, when the decl-specifier-seq contains
3475 either a class-specifier, an elaborated-type-specifier with
3476 a class-key (9.1), or an enum-specifier. In these cases and
3477 whenever a class-specifier or enum-specifier is present in the
3478 decl-specifier-seq, the identifiers in these specifiers are among
3479 the names being declared by the declaration (as class-name,
3480 enum-names, or enumerators, depending on the syntax). In such
3481 cases, and except for the declaration of an unnamed bit-field (9.6),
3482 the decl-specifier-seq shall introduce one or more names into the
3483 program, or shall redeclare a name introduced by a previous
3484 declaration. [Example:
3485 enum { }; // ill-formed
3486 typedef class { }; // ill-formed
3490 error ("missing type-name in typedef-declaration");
3493 /* Anonymous unions are objects, so they can have specifiers. */;
3494 SET_ANON_AGGR_TYPE_P (declared_type);
3496 if (TREE_CODE (declared_type) != UNION_TYPE && pedantic
3497 && !in_system_header)
3498 pedwarn ("ISO C++ prohibits anonymous structs");
3503 if (declspecs->specs[(int)ds_inline]
3504 || declspecs->specs[(int)ds_virtual])
3505 error ("%qs can only be specified for functions",
3506 declspecs->specs[(int)ds_inline]
3507 ? "inline" : "virtual");
3509 && (!current_class_type
3510 || current_scope () != current_class_type))
3511 error ("%<friend%> can only be specified inside a class");
3512 else if (declspecs->specs[(int)ds_explicit])
3513 error ("%<explicit%> can only be specified for constructors");
3514 else if (declspecs->storage_class)
3515 error ("a storage class can only be specified for objects "
3517 else if (declspecs->specs[(int)ds_const]