1 /* Process declarations and variables for C++ compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006 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 void maybe_deduce_size_from_array_init (tree, tree);
84 static void layout_var_decl (tree);
85 static void maybe_commonize_var (tree);
86 static tree check_initializer (tree, tree, int, tree *);
87 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
88 static void save_function_data (tree);
89 static void check_function_type (tree, tree);
90 static void finish_constructor_body (void);
91 static void begin_destructor_body (void);
92 static void finish_destructor_body (void);
93 static tree create_array_type_for_decl (tree, tree, tree);
94 static tree get_atexit_node (void);
95 static tree get_dso_handle_node (void);
96 static tree start_cleanup_fn (void);
97 static void end_cleanup_fn (void);
98 static tree cp_make_fname_decl (tree, int);
99 static void initialize_predefined_identifiers (void);
100 static tree check_special_function_return_type
101 (special_function_kind, tree, tree);
102 static tree push_cp_library_fn (enum tree_code, tree);
103 static tree build_cp_library_fn (tree, enum tree_code, tree);
104 static void store_parm_decls (tree);
105 static void initialize_local_var (tree, tree);
106 static void expand_static_init (tree, tree);
107 static tree next_initializable_field (tree);
109 /* The following symbols are subsumed in the cp_global_trees array, and
110 listed here individually for documentation purposes.
113 tree wchar_decl_node;
115 tree vtable_entry_type;
116 tree delta_type_node;
117 tree __t_desc_type_node;
119 tree class_type_node;
120 tree unknown_type_node;
122 Array type `vtable_entry_type[]'
125 tree vtbl_ptr_type_node;
132 A FUNCTION_DECL which can call `abort'. Not necessarily the
133 one that the user will declare, but sufficient to be called
134 by routines that want to abort the program.
138 The FUNCTION_DECL for the default `::operator delete'.
140 tree global_delete_fndecl;
143 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
144 tree tinfo_var_id; */
146 tree cp_global_trees[CPTI_MAX];
148 /* Indicates that there is a type value in some namespace, although
149 that is not necessarily in scope at the moment. */
151 tree global_type_node;
153 /* The node that holds the "name" of the global scope. */
154 tree global_scope_name;
156 #define local_names cp_function_chain->x_local_names
158 /* A list of objects which have constructors or destructors
159 which reside in the global scope. The decl is stored in
160 the TREE_VALUE slot and the initializer is stored
161 in the TREE_PURPOSE slot. */
162 tree static_aggregates;
166 /* A node for the integer constants 2, and 3. */
168 tree integer_two_node, integer_three_node;
170 /* Used only for jumps to as-yet undefined labels, since jumps to
171 defined labels can have their validity checked immediately. */
173 struct named_label_use_entry GTY(())
175 struct named_label_use_entry *next;
176 /* The binding level to which this entry is *currently* attached.
177 This is initially the binding level in which the goto appeared,
178 but is modified as scopes are closed. */
179 struct cp_binding_level *binding_level;
180 /* The head of the names list that was current when the goto appeared,
181 or the inner scope popped. These are the decls that will *not* be
182 skipped when jumping to the label. */
184 /* The location of the goto, for error reporting. */
185 location_t o_goto_locus;
186 /* True if an OpenMP structured block scope has been closed since
187 the goto appeared. This means that the branch from the label will
188 illegally exit an OpenMP scope. */
192 /* A list of all LABEL_DECLs in the function that have names. Here so
193 we can clear out their names' definitions at the end of the
194 function, and so we can check the validity of jumps to these labels. */
196 struct named_label_entry GTY(())
198 /* The decl itself. */
201 /* The binding level to which the label is *currently* attached.
202 This is initially set to the binding level in which the label
203 is defined, but is modified as scopes are closed. */
204 struct cp_binding_level *binding_level;
205 /* The head of the names list that was current when the label was
206 defined, or the inner scope popped. These are the decls that will
207 be skipped when jumping to the label. */
209 /* A tree list of all decls from all binding levels that would be
210 crossed by a backward branch to the label. */
213 /* A list of uses of the label, before the label is defined. */
214 struct named_label_use_entry *uses;
216 /* The following bits are set after the label is defined, and are
217 updated as scopes are popped. They indicate that a backward jump
218 to the label will illegally enter a scope of the given flavor. */
224 #define named_labels cp_function_chain->x_named_labels
226 /* The number of function bodies which we are currently processing.
227 (Zero if we are at namespace scope, one inside the body of a
228 function, two inside the body of a function in a local class, etc.) */
231 /* States indicating how grokdeclarator() should handle declspecs marked
232 with __attribute__((deprecated)). An object declared as
233 __attribute__((deprecated)) suppresses warnings of uses of other
236 enum deprecated_states {
241 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
244 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
245 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
246 time the VAR_DECL was declared, the type was incomplete. */
248 static GTY(()) tree incomplete_vars;
250 /* Returns the kind of template specialization we are currently
251 processing, given that it's declaration contained N_CLASS_SCOPES
252 explicit scope qualifications. */
255 current_tmpl_spec_kind (int n_class_scopes)
257 int n_template_parm_scopes = 0;
258 int seen_specialization_p = 0;
259 int innermost_specialization_p = 0;
260 struct cp_binding_level *b;
262 /* Scan through the template parameter scopes. */
263 for (b = current_binding_level;
264 b->kind == sk_template_parms;
267 /* If we see a specialization scope inside a parameter scope,
268 then something is wrong. That corresponds to a declaration
271 template <class T> template <> ...
273 which is always invalid since [temp.expl.spec] forbids the
274 specialization of a class member template if the enclosing
275 class templates are not explicitly specialized as well. */
276 if (b->explicit_spec_p)
278 if (n_template_parm_scopes == 0)
279 innermost_specialization_p = 1;
281 seen_specialization_p = 1;
283 else if (seen_specialization_p == 1)
284 return tsk_invalid_member_spec;
286 ++n_template_parm_scopes;
289 /* Handle explicit instantiations. */
290 if (processing_explicit_instantiation)
292 if (n_template_parm_scopes != 0)
293 /* We've seen a template parameter list during an explicit
294 instantiation. For example:
296 template <class T> template void f(int);
298 This is erroneous. */
299 return tsk_invalid_expl_inst;
301 return tsk_expl_inst;
304 if (n_template_parm_scopes < n_class_scopes)
305 /* We've not seen enough template headers to match all the
306 specialized classes present. For example:
308 template <class T> void R<T>::S<T>::f(int);
310 This is invalid; there needs to be one set of template
311 parameters for each class. */
312 return tsk_insufficient_parms;
313 else if (n_template_parm_scopes == n_class_scopes)
314 /* We're processing a non-template declaration (even though it may
315 be a member of a template class.) For example:
317 template <class T> void S<T>::f(int);
319 The `class T' maches the `S<T>', leaving no template headers
320 corresponding to the `f'. */
322 else if (n_template_parm_scopes > n_class_scopes + 1)
323 /* We've got too many template headers. For example:
325 template <> template <class T> void f (T);
327 There need to be more enclosing classes. */
328 return tsk_excessive_parms;
330 /* This must be a template. It's of the form:
332 template <class T> template <class U> void S<T>::f(U);
334 This is a specialization if the innermost level was a
335 specialization; otherwise it's just a definition of the
337 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
340 /* Exit the current scope. */
348 /* When a label goes out of scope, check to see if that label was used
349 in a valid manner, and issue any appropriate warnings or errors. */
352 pop_label (tree label, tree old_value)
354 if (!processing_template_decl)
356 if (DECL_INITIAL (label) == NULL_TREE)
360 error ("label %q+D used but not defined", label);
361 #ifdef USE_MAPPED_LOCATION
362 location = input_location; /* FIXME want (input_filename, (line)0) */
364 location.file = input_filename;
367 /* Avoid crashing later. */
368 define_label (location, DECL_NAME (label));
370 else if (!TREE_USED (label))
371 warning (OPT_Wunused_label, "label %q+D defined but not used", label);
374 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
377 /* At the end of a function, all labels declared within the function
378 go out of scope. BLOCK is the top-level block for the
382 pop_labels_1 (void **slot, void *data)
384 struct named_label_entry *ent = (struct named_label_entry *) *slot;
385 tree block = (tree) data;
387 pop_label (ent->label_decl, NULL_TREE);
389 /* Put the labels into the "variables" of the top-level block,
390 so debugger can see them. */
391 TREE_CHAIN (ent->label_decl) = BLOCK_VARS (block);
392 BLOCK_VARS (block) = ent->label_decl;
394 htab_clear_slot (named_labels, slot);
400 pop_labels (tree block)
404 htab_traverse (named_labels, pop_labels_1, block);
409 /* At the end of a block with local labels, restore the outer definition. */
412 pop_local_label (tree label, tree old_value)
414 struct named_label_entry dummy;
417 pop_label (label, old_value);
419 dummy.label_decl = label;
420 slot = htab_find_slot (named_labels, &dummy, NO_INSERT);
421 htab_clear_slot (named_labels, slot);
424 /* The following two routines are used to interface to Objective-C++.
425 The binding level is purposely treated as an opaque type. */
428 objc_get_current_scope (void)
430 return current_binding_level;
433 /* The following routine is used by the NeXT-style SJLJ exceptions;
434 variables get marked 'volatile' so as to not be clobbered by
435 _setjmp()/_longjmp() calls. All variables in the current scope,
436 as well as parent scopes up to (but not including) ENCLOSING_BLK
437 shall be thusly marked. */
440 objc_mark_locals_volatile (void *enclosing_blk)
442 struct cp_binding_level *scope;
444 for (scope = current_binding_level;
445 scope && scope != enclosing_blk;
446 scope = scope->level_chain)
450 for (decl = scope->names; decl; decl = TREE_CHAIN (decl))
451 objc_volatilize_decl (decl);
453 /* Do not climb up past the current function. */
454 if (scope->kind == sk_function_parms)
459 /* Update data for defined and undefined labels when leaving a scope. */
462 poplevel_named_label_1 (void **slot, void *data)
464 struct named_label_entry *ent = (struct named_label_entry *) *slot;
465 struct cp_binding_level *bl = (struct cp_binding_level *) data;
466 struct cp_binding_level *obl = bl->level_chain;
468 if (ent->binding_level == bl)
472 for (decl = ent->names_in_scope; decl; decl = TREE_CHAIN (decl))
473 if (decl_jump_unsafe (decl))
474 ent->bad_decls = tree_cons (NULL, decl, ent->bad_decls);
476 ent->binding_level = obl;
477 ent->names_in_scope = obl->names;
481 ent->in_try_scope = true;
484 ent->in_catch_scope = true;
487 ent->in_omp_scope = true;
495 struct named_label_use_entry *use;
497 for (use = ent->uses; use ; use = use->next)
498 if (use->binding_level == bl)
500 use->binding_level = obl;
501 use->names_in_scope = obl->names;
502 if (bl->kind == sk_omp)
503 use->in_omp_scope = true;
510 /* Exit a binding level.
511 Pop the level off, and restore the state of the identifier-decl mappings
512 that were in effect when this level was entered.
514 If KEEP == 1, this level had explicit declarations, so
515 and create a "block" (a BLOCK node) for the level
516 to record its declarations and subblocks for symbol table output.
518 If FUNCTIONBODY is nonzero, this level is the body of a function,
519 so create a block as if KEEP were set and also clear out all
522 If REVERSE is nonzero, reverse the order of decls before putting
523 them into the BLOCK. */
526 poplevel (int keep, int reverse, int functionbody)
529 /* The chain of decls was accumulated in reverse order.
530 Put it into forward order, just for cleanliness. */
532 int tmp = functionbody;
533 int real_functionbody;
537 int leaving_for_scope;
540 timevar_push (TV_NAME_LOOKUP);
545 gcc_assert (current_binding_level->kind != sk_class);
547 real_functionbody = (current_binding_level->kind == sk_cleanup
548 ? ((functionbody = 0), tmp) : functionbody);
549 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
551 gcc_assert (!VEC_length(cp_class_binding,
552 current_binding_level->class_shadowed));
554 /* We used to use KEEP == 2 to indicate that the new block should go
555 at the beginning of the list of blocks at this binding level,
556 rather than the end. This hack is no longer used. */
557 gcc_assert (keep == 0 || keep == 1);
559 if (current_binding_level->keep)
562 /* Any uses of undefined labels, and any defined labels, now operate
563 under constraints of next binding contour. */
564 if (cfun && !functionbody && named_labels)
565 htab_traverse (named_labels, poplevel_named_label_1,
566 current_binding_level);
568 /* Get the decls in the order they were written.
569 Usually current_binding_level->names is in reverse order.
570 But parameter decls were previously put in forward order. */
573 current_binding_level->names
574 = decls = nreverse (current_binding_level->names);
576 decls = current_binding_level->names;
578 /* If there were any declarations or structure tags in that level,
579 or if this level is a function body,
580 create a BLOCK to record them for the life of this function. */
582 if (keep == 1 || functionbody)
583 block = make_node (BLOCK);
584 if (block != NULL_TREE)
586 BLOCK_VARS (block) = decls;
587 BLOCK_SUBBLOCKS (block) = subblocks;
590 /* In each subblock, record that this is its superior. */
592 for (link = subblocks; link; link = TREE_CHAIN (link))
593 BLOCK_SUPERCONTEXT (link) = block;
595 /* We still support the old for-scope rules, whereby the variables
596 in a for-init statement were in scope after the for-statement
597 ended. We only use the new rules if flag_new_for_scope is
600 = current_binding_level->kind == sk_for && flag_new_for_scope == 1;
602 /* Before we remove the declarations first check for unused variables. */
603 if (warn_unused_variable
604 && !processing_template_decl)
605 for (decl = getdecls (); decl; decl = TREE_CHAIN (decl))
606 if (TREE_CODE (decl) == VAR_DECL
607 && ! TREE_USED (decl)
608 && ! DECL_IN_SYSTEM_HEADER (decl)
609 && DECL_NAME (decl) && ! DECL_ARTIFICIAL (decl))
610 warning (OPT_Wunused_variable, "unused variable %q+D", decl);
612 /* Remove declarations for all the DECLs in this level. */
613 for (link = decls; link; link = TREE_CHAIN (link))
615 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
618 tree name = DECL_NAME (link);
622 ob = outer_binding (name,
623 IDENTIFIER_BINDING (name),
626 ns_binding = IDENTIFIER_NAMESPACE_VALUE (name);
628 ns_binding = NULL_TREE;
630 if (ob && ob->scope == current_binding_level->level_chain)
631 /* We have something like:
636 and we are leaving the `for' scope. There's no reason to
637 keep the binding of the inner `i' in this case. */
638 pop_binding (name, link);
639 else if ((ob && (TREE_CODE (ob->value) == TYPE_DECL))
640 || (ns_binding && TREE_CODE (ns_binding) == TYPE_DECL))
641 /* Here, we have something like:
649 We must pop the for-scope binding so we know what's a
650 type and what isn't. */
651 pop_binding (name, link);
654 /* Mark this VAR_DECL as dead so that we can tell we left it
655 there only for backward compatibility. */
656 DECL_DEAD_FOR_LOCAL (link) = 1;
658 /* Keep track of what should have happened when we
659 popped the binding. */
662 SET_DECL_SHADOWED_FOR_VAR (link, ob->value);
663 DECL_HAS_SHADOWED_FOR_VAR_P (link) = 1;
666 /* Add it to the list of dead variables in the next
667 outermost binding to that we can remove these when we
668 leave that binding. */
669 current_binding_level->level_chain->dead_vars_from_for
670 = tree_cons (NULL_TREE, link,
671 current_binding_level->level_chain->
674 /* Although we don't pop the cxx_binding, we do clear
675 its SCOPE since the scope is going away now. */
676 IDENTIFIER_BINDING (name)->scope
677 = current_binding_level->level_chain;
684 /* Remove the binding. */
687 if (TREE_CODE (decl) == TREE_LIST)
688 decl = TREE_VALUE (decl);
691 if (TREE_CODE (name) == OVERLOAD)
692 name = OVL_FUNCTION (name);
694 gcc_assert (DECL_P (name));
695 pop_binding (DECL_NAME (name), decl);
699 /* Remove declarations for any `for' variables from inner scopes
700 that we kept around. */
701 for (link = current_binding_level->dead_vars_from_for;
702 link; link = TREE_CHAIN (link))
703 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
705 /* Restore the IDENTIFIER_TYPE_VALUEs. */
706 for (link = current_binding_level->type_shadowed;
707 link; link = TREE_CHAIN (link))
708 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
710 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
711 for (link = current_binding_level->shadowed_labels;
713 link = TREE_CHAIN (link))
714 pop_local_label (TREE_VALUE (link), TREE_PURPOSE (link));
716 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
717 list if a `using' declaration put them there. The debugging
718 back-ends won't understand OVERLOAD, so we remove them here.
719 Because the BLOCK_VARS are (temporarily) shared with
720 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
721 popped all the bindings. */
726 for (d = &BLOCK_VARS (block); *d; )
728 if (TREE_CODE (*d) == TREE_LIST)
729 *d = TREE_CHAIN (*d);
731 d = &TREE_CHAIN (*d);
735 /* If the level being exited is the top level of a function,
736 check over all the labels. */
739 /* Since this is the top level block of a function, the vars are
740 the function's parameters. Don't leave them in the BLOCK
741 because they are found in the FUNCTION_DECL instead. */
742 BLOCK_VARS (block) = 0;
746 kind = current_binding_level->kind;
747 if (kind == sk_cleanup)
751 /* If this is a temporary binding created for a cleanup, then we'll
752 have pushed a statement list level. Pop that, create a new
753 BIND_EXPR for the block, and insert it into the stream. */
754 stmt = pop_stmt_list (current_binding_level->statement_list);
755 stmt = c_build_bind_expr (block, stmt);
761 DECL_INITIAL (current_function_decl) = block;
763 current_binding_level->blocks
764 = chainon (current_binding_level->blocks, block);
766 /* If we did not make a block for the level just exited,
767 any blocks made for inner levels
768 (since they cannot be recorded as subblocks in that level)
769 must be carried forward so they will later become subblocks
770 of something else. */
772 current_binding_level->blocks
773 = chainon (current_binding_level->blocks, subblocks);
775 /* Each and every BLOCK node created here in `poplevel' is important
776 (e.g. for proper debugging information) so if we created one
777 earlier, mark it as "used". */
779 TREE_USED (block) = 1;
781 /* All temporary bindings created for cleanups are popped silently. */
782 if (kind == sk_cleanup)
785 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
788 /* Insert BLOCK at the end of the list of subblocks of the
789 current binding level. This is used when a BIND_EXPR is expanded,
790 to handle the BLOCK node inside the BIND_EXPR. */
793 insert_block (tree block)
795 TREE_USED (block) = 1;
796 current_binding_level->blocks
797 = chainon (current_binding_level->blocks, block);
800 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
801 itself, calling F for each. The DATA is passed to F as well. */
804 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
807 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
809 result |= (*f) (namespace, data);
811 for (; current; current = TREE_CHAIN (current))
812 result |= walk_namespaces_r (current, f, data);
817 /* Walk all the namespaces, calling F for each. The DATA is passed to
821 walk_namespaces (walk_namespaces_fn f, void* data)
823 return walk_namespaces_r (global_namespace, f, data);
826 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
827 DATA is non-NULL, this is the last time we will call
828 wrapup_global_declarations for this NAMESPACE. */
831 wrapup_globals_for_namespace (tree namespace, void* data)
833 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
834 VEC(tree,gc) *statics = level->static_decls;
835 tree *vec = VEC_address (tree, statics);
836 int len = VEC_length (tree, statics);
837 int last_time = (data != 0);
841 check_global_declarations (vec, len);
842 emit_debug_global_declarations (vec, len);
846 /* Write out any globals that need to be output. */
847 return wrapup_global_declarations (vec, len);
851 /* In C++, you don't have to write `struct S' to refer to `S'; you
852 can just use `S'. We accomplish this by creating a TYPE_DECL as
853 if the user had written `typedef struct S S'. Create and return
854 the TYPE_DECL for TYPE. */
857 create_implicit_typedef (tree name, tree type)
861 decl = build_decl (TYPE_DECL, name, type);
862 DECL_ARTIFICIAL (decl) = 1;
863 /* There are other implicit type declarations, like the one *within*
864 a class that allows you to write `S::S'. We must distinguish
866 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
867 TYPE_NAME (type) = decl;
872 /* Remember a local name for name-mangling purposes. */
875 push_local_name (tree decl)
880 timevar_push (TV_NAME_LOOKUP);
882 name = DECL_NAME (decl);
884 nelts = VEC_length (tree, local_names);
885 for (i = 0; i < nelts; i++)
887 t = VEC_index (tree, local_names, i);
888 if (DECL_NAME (t) == name)
890 if (!DECL_LANG_SPECIFIC (decl))
891 retrofit_lang_decl (decl);
892 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
893 if (DECL_LANG_SPECIFIC (t))
894 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
896 DECL_DISCRIMINATOR (decl) = 1;
898 VEC_replace (tree, local_names, i, decl);
899 timevar_pop (TV_NAME_LOOKUP);
904 VEC_safe_push (tree, gc, local_names, decl);
905 timevar_pop (TV_NAME_LOOKUP);
908 /* Subroutine of duplicate_decls: return truthvalue of whether
909 or not types of these decls match.
911 For C++, we must compare the parameter list so that `int' can match
912 `int&' in a parameter position, but `int&' is not confused with
916 decls_match (tree newdecl, tree olddecl)
920 if (newdecl == olddecl)
923 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
924 /* If the two DECLs are not even the same kind of thing, we're not
925 interested in their types. */
928 if (TREE_CODE (newdecl) == FUNCTION_DECL)
930 tree f1 = TREE_TYPE (newdecl);
931 tree f2 = TREE_TYPE (olddecl);
932 tree p1 = TYPE_ARG_TYPES (f1);
933 tree p2 = TYPE_ARG_TYPES (f2);
935 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
936 && ! (DECL_EXTERN_C_P (newdecl)
937 && DECL_EXTERN_C_P (olddecl)))
940 if (TREE_CODE (f1) != TREE_CODE (f2))
943 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
945 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
946 && (DECL_BUILT_IN (olddecl)
947 #ifndef NO_IMPLICIT_EXTERN_C
948 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
949 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
953 types_match = self_promoting_args_p (p1);
954 if (p1 == void_list_node)
955 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
957 #ifndef NO_IMPLICIT_EXTERN_C
958 else if (p1 == NULL_TREE
959 && (DECL_EXTERN_C_P (olddecl)
960 && DECL_IN_SYSTEM_HEADER (olddecl)
961 && !DECL_CLASS_SCOPE_P (olddecl))
962 && (DECL_EXTERN_C_P (newdecl)
963 && DECL_IN_SYSTEM_HEADER (newdecl)
964 && !DECL_CLASS_SCOPE_P (newdecl)))
966 types_match = self_promoting_args_p (p2);
967 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
971 types_match = compparms (p1, p2);
976 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
978 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
979 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
982 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
983 DECL_TEMPLATE_PARMS (olddecl)))
986 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
987 types_match = same_type_p (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl)),
988 TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl)));
990 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
991 DECL_TEMPLATE_RESULT (newdecl));
995 /* Need to check scope for variable declaration (VAR_DECL).
996 For typedef (TYPE_DECL), scope is ignored. */
997 if (TREE_CODE (newdecl) == VAR_DECL
998 && CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
1000 Two declarations for an object with C language linkage
1001 with the same name (ignoring the namespace that qualify
1002 it) that appear in different namespace scopes refer to
1004 && !(DECL_EXTERN_C_P (olddecl) && DECL_EXTERN_C_P (newdecl)))
1007 if (TREE_TYPE (newdecl) == error_mark_node)
1008 types_match = TREE_TYPE (olddecl) == error_mark_node;
1009 else if (TREE_TYPE (olddecl) == NULL_TREE)
1010 types_match = TREE_TYPE (newdecl) == NULL_TREE;
1011 else if (TREE_TYPE (newdecl) == NULL_TREE)
1014 types_match = comptypes (TREE_TYPE (newdecl),
1015 TREE_TYPE (olddecl),
1016 COMPARE_REDECLARATION);
1022 /* If NEWDECL is `static' and an `extern' was seen previously,
1023 warn about it. OLDDECL is the previous declaration.
1025 Note that this does not apply to the C++ case of declaring
1026 a variable `extern const' and then later `const'.
1028 Don't complain about built-in functions, since they are beyond
1029 the user's control. */
1032 warn_extern_redeclared_static (tree newdecl, tree olddecl)
1036 if (TREE_CODE (newdecl) == TYPE_DECL
1037 || TREE_CODE (newdecl) == TEMPLATE_DECL
1038 || TREE_CODE (newdecl) == CONST_DECL
1039 || TREE_CODE (newdecl) == NAMESPACE_DECL)
1042 /* Don't get confused by static member functions; that's a different
1044 if (TREE_CODE (newdecl) == FUNCTION_DECL
1045 && DECL_STATIC_FUNCTION_P (newdecl))
1048 /* If the old declaration was `static', or the new one isn't, then
1049 then everything is OK. */
1050 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
1053 /* It's OK to declare a builtin function as `static'. */
1054 if (TREE_CODE (olddecl) == FUNCTION_DECL
1055 && DECL_ARTIFICIAL (olddecl))
1058 name = DECL_ASSEMBLER_NAME (newdecl);
1059 pedwarn ("%qD was declared %<extern%> and later %<static%>", newdecl);
1060 pedwarn ("previous declaration of %q+D", olddecl);
1063 /* NEW_DECL is a redeclaration of OLD_DECL; both are functions or
1064 function templates. If their exception specifications do not
1065 match, issue an a diagnostic. */
1068 check_redeclaration_exception_specification (tree new_decl,
1073 tree new_exceptions;
1074 tree old_exceptions;
1076 new_type = TREE_TYPE (new_decl);
1077 new_exceptions = TYPE_RAISES_EXCEPTIONS (new_type);
1078 old_type = TREE_TYPE (old_decl);
1079 old_exceptions = TYPE_RAISES_EXCEPTIONS (old_type);
1083 If any declaration of a function has an exception-specification,
1084 all declarations, including the definition and an explicit
1085 specialization, of that function shall have an
1086 exception-specification with the same set of type-ids. */
1087 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (old_decl))
1088 && ! DECL_IS_BUILTIN (old_decl)
1090 && !comp_except_specs (new_exceptions, old_exceptions,
1093 error ("declaration of %qF throws different exceptions", new_decl);
1094 error ("from previous declaration %q+F", old_decl);
1098 /* If NEWDECL is a redeclaration of OLDDECL, merge the declarations.
1099 If the redeclaration is invalid, a diagnostic is issued, and the
1100 error_mark_node is returned. Otherwise, OLDDECL is returned.
1102 If NEWDECL is not a redeclaration of OLDDECL, NULL_TREE is
1105 NEWDECL_IS_FRIEND is true if NEWDECL was declared as a friend. */
1108 duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
1110 unsigned olddecl_uid = DECL_UID (olddecl);
1111 int olddecl_friend = 0, types_match = 0, hidden_friend = 0;
1112 int new_defines_function = 0;
1115 if (newdecl == olddecl)
1118 types_match = decls_match (newdecl, olddecl);
1120 /* If either the type of the new decl or the type of the old decl is an
1121 error_mark_node, then that implies that we have already issued an
1122 error (earlier) for some bogus type specification, and in that case,
1123 it is rather pointless to harass the user with yet more error message
1124 about the same declaration, so just pretend the types match here. */
1125 if (TREE_TYPE (newdecl) == error_mark_node
1126 || TREE_TYPE (olddecl) == error_mark_node)
1127 return error_mark_node;
1129 if (DECL_P (olddecl)
1130 && TREE_CODE (newdecl) == FUNCTION_DECL
1131 && TREE_CODE (olddecl) == FUNCTION_DECL
1132 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
1134 if (DECL_DECLARED_INLINE_P (newdecl)
1135 && DECL_UNINLINABLE (newdecl)
1136 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1137 /* Already warned elsewhere. */;
1138 else if (DECL_DECLARED_INLINE_P (olddecl)
1139 && DECL_UNINLINABLE (olddecl)
1140 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1141 /* Already warned. */;
1142 else if (DECL_DECLARED_INLINE_P (newdecl)
1143 && DECL_UNINLINABLE (olddecl)
1144 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
1146 warning (OPT_Wattributes, "function %q+D redeclared as inline",
1148 warning (OPT_Wattributes, "previous declaration of %q+D "
1149 "with attribute noinline", olddecl);
1151 else if (DECL_DECLARED_INLINE_P (olddecl)
1152 && DECL_UNINLINABLE (newdecl)
1153 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
1155 warning (OPT_Wattributes, "function %q+D redeclared with "
1156 "attribute noinline", newdecl);
1157 warning (OPT_Wattributes, "previous declaration of %q+D was inline",
1162 /* Check for redeclaration and other discrepancies. */
1163 if (TREE_CODE (olddecl) == FUNCTION_DECL
1164 && DECL_ARTIFICIAL (olddecl))
1166 gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
1167 if (TREE_CODE (newdecl) != FUNCTION_DECL)
1169 /* Avoid warnings redeclaring built-ins which have not been
1170 explicitly declared. */
1171 if (DECL_ANTICIPATED (olddecl))
1174 /* If you declare a built-in or predefined function name as static,
1175 the old definition is overridden, but optionally warn this was a
1176 bad choice of name. */
1177 if (! TREE_PUBLIC (newdecl))
1179 warning (OPT_Wshadow, "shadowing %s function %q#D",
1180 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1182 /* Discard the old built-in function. */
1185 /* If the built-in is not ansi, then programs can override
1186 it even globally without an error. */
1187 else if (! DECL_BUILT_IN (olddecl))
1188 warning (0, "library function %q#D redeclared as non-function %q#D",
1192 error ("declaration of %q#D", newdecl);
1193 error ("conflicts with built-in declaration %q#D",
1198 else if (!types_match)
1200 /* Avoid warnings redeclaring built-ins which have not been
1201 explicitly declared. */
1202 if (DECL_ANTICIPATED (olddecl))
1204 /* Deal with fileptr_type_node. FILE type is not known
1205 at the time we create the builtins. */
1208 for (t1 = TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1209 t2 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1211 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
1214 else if (TREE_VALUE (t2) == fileptr_type_node)
1216 tree t = TREE_VALUE (t1);
1218 if (TREE_CODE (t) == POINTER_TYPE
1219 && TYPE_NAME (TREE_TYPE (t))
1220 && DECL_NAME (TYPE_NAME (TREE_TYPE (t)))
1221 == get_identifier ("FILE")
1222 && compparms (TREE_CHAIN (t1), TREE_CHAIN (t2)))
1224 tree oldargs = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1226 TYPE_ARG_TYPES (TREE_TYPE (olddecl))
1227 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1228 types_match = decls_match (newdecl, olddecl);
1230 return duplicate_decls (newdecl, olddecl,
1232 TYPE_ARG_TYPES (TREE_TYPE (olddecl)) = oldargs;
1235 else if (! same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
1238 else if ((DECL_EXTERN_C_P (newdecl)
1239 && DECL_EXTERN_C_P (olddecl))
1240 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1241 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1243 /* A near match; override the builtin. */
1245 if (TREE_PUBLIC (newdecl))
1247 warning (0, "new declaration %q#D", newdecl);
1248 warning (0, "ambiguates built-in declaration %q#D",
1252 warning (OPT_Wshadow, "shadowing %s function %q#D",
1253 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
1257 /* Discard the old built-in function. */
1260 /* Replace the old RTL to avoid problems with inlining. */
1261 COPY_DECL_RTL (newdecl, olddecl);
1263 /* Even if the types match, prefer the new declarations type for
1264 built-ins which have not been explicitly declared, for
1265 exception lists, etc... */
1266 else if (DECL_ANTICIPATED (olddecl))
1268 tree type = TREE_TYPE (newdecl);
1269 tree attribs = (*targetm.merge_type_attributes)
1270 (TREE_TYPE (olddecl), type);
1272 type = cp_build_type_attribute_variant (type, attribs);
1273 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
1276 /* Whether or not the builtin can throw exceptions has no
1277 bearing on this declarator. */
1278 TREE_NOTHROW (olddecl) = 0;
1280 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
1282 /* If a builtin function is redeclared as `static', merge
1283 the declarations, but make the original one static. */
1284 DECL_THIS_STATIC (olddecl) = 1;
1285 TREE_PUBLIC (olddecl) = 0;
1287 /* Make the old declaration consistent with the new one so
1288 that all remnants of the builtin-ness of this function
1289 will be banished. */
1290 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1291 COPY_DECL_RTL (newdecl, olddecl);
1294 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
1296 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
1297 && TREE_CODE (newdecl) != TYPE_DECL
1298 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
1299 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
1300 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
1301 && TREE_CODE (olddecl) != TYPE_DECL
1302 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
1303 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
1306 /* We do nothing special here, because C++ does such nasty
1307 things with TYPE_DECLs. Instead, just let the TYPE_DECL
1308 get shadowed, and know that if we need to find a TYPE_DECL
1309 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
1310 slot of the identifier. */
1314 if ((TREE_CODE (newdecl) == FUNCTION_DECL
1315 && DECL_FUNCTION_TEMPLATE_P (olddecl))
1316 || (TREE_CODE (olddecl) == FUNCTION_DECL
1317 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
1320 error ("%q#D redeclared as different kind of symbol", newdecl);
1321 if (TREE_CODE (olddecl) == TREE_LIST)
1322 olddecl = TREE_VALUE (olddecl);
1323 error ("previous declaration of %q+#D", olddecl);
1325 return error_mark_node;
1327 else if (!types_match)
1329 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
1330 /* These are certainly not duplicate declarations; they're
1331 from different scopes. */
1334 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1336 /* The name of a class template may not be declared to refer to
1337 any other template, class, function, object, namespace, value,
1338 or type in the same scope. */
1339 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
1340 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
1342 error ("declaration of template %q#D", newdecl);
1343 error ("conflicts with previous declaration %q+#D", olddecl);
1345 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
1346 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
1347 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
1348 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
1349 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
1350 DECL_TEMPLATE_PARMS (olddecl))
1351 /* Template functions can be disambiguated by
1353 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
1354 TREE_TYPE (TREE_TYPE (olddecl))))
1356 error ("new declaration %q#D", newdecl);
1357 error ("ambiguates old declaration %q+#D", olddecl);
1361 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1363 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
1365 error ("declaration of C function %q#D conflicts with",
1367 error ("previous declaration %q+#D here", olddecl);
1369 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
1370 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
1372 error ("new declaration %q#D", newdecl);
1373 error ("ambiguates old declaration %q+#D", olddecl);
1374 return error_mark_node;
1381 error ("conflicting declaration %q#D", newdecl);
1382 error ("%q+D has a previous declaration as %q#D", olddecl, olddecl);
1383 return error_mark_node;
1386 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1387 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
1388 && (!DECL_TEMPLATE_INFO (newdecl)
1389 || (DECL_TI_TEMPLATE (newdecl)
1390 != DECL_TI_TEMPLATE (olddecl))))
1391 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
1392 && (!DECL_TEMPLATE_INFO (olddecl)
1393 || (DECL_TI_TEMPLATE (olddecl)
1394 != DECL_TI_TEMPLATE (newdecl))))))
1395 /* It's OK to have a template specialization and a non-template
1396 with the same type, or to have specializations of two
1397 different templates with the same type. Note that if one is a
1398 specialization, and the other is an instantiation of the same
1399 template, that we do not exit at this point. That situation
1400 can occur if we instantiate a template class, and then
1401 specialize one of its methods. This situation is valid, but
1402 the declarations must be merged in the usual way. */
1404 else if (TREE_CODE (newdecl) == FUNCTION_DECL
1405 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
1406 && !DECL_USE_TEMPLATE (newdecl))
1407 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
1408 && !DECL_USE_TEMPLATE (olddecl))))
1409 /* One of the declarations is a template instantiation, and the
1410 other is not a template at all. That's OK. */
1412 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1414 /* In [namespace.alias] we have:
1416 In a declarative region, a namespace-alias-definition can be
1417 used to redefine a namespace-alias declared in that declarative
1418 region to refer only to the namespace to which it already
1421 Therefore, if we encounter a second alias directive for the same
1422 alias, we can just ignore the second directive. */
1423 if (DECL_NAMESPACE_ALIAS (newdecl)
1424 && (DECL_NAMESPACE_ALIAS (newdecl)
1425 == DECL_NAMESPACE_ALIAS (olddecl)))
1427 /* [namespace.alias]
1429 A namespace-name or namespace-alias shall not be declared as
1430 the name of any other entity in the same declarative region.
1431 A namespace-name defined at global scope shall not be
1432 declared as the name of any other entity in any global scope
1434 error ("declaration of namespace %qD conflicts with", newdecl);
1435 error ("previous declaration of namespace %q+D here", olddecl);
1436 return error_mark_node;
1440 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
1443 error (errmsg, newdecl);
1444 if (DECL_NAME (olddecl) != NULL_TREE)
1445 error ((DECL_INITIAL (olddecl) && namespace_bindings_p ())
1446 ? "%q+#D previously defined here"
1447 : "%q+#D previously declared here", olddecl);
1448 return error_mark_node;
1450 else if (TREE_CODE (olddecl) == FUNCTION_DECL
1451 && DECL_INITIAL (olddecl) != NULL_TREE
1452 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
1453 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
1455 /* Prototype decl follows defn w/o prototype. */
1456 warning (0, "prototype for %q+#D", newdecl);
1457 warning (0, "%Jfollows non-prototype definition here", olddecl);
1459 else if ((TREE_CODE (olddecl) == FUNCTION_DECL
1460 || TREE_CODE (olddecl) == VAR_DECL)
1461 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
1464 If two declarations of the same function or object
1465 specify different linkage-specifications ..., the program
1466 is ill-formed.... Except for functions with C++ linkage,
1467 a function declaration without a linkage specification
1468 shall not precede the first linkage specification for
1469 that function. A function can be declared without a
1470 linkage specification after an explicit linkage
1471 specification has been seen; the linkage explicitly
1472 specified in the earlier declaration is not affected by
1473 such a function declaration.
1475 DR 563 raises the question why the restrictions on
1476 functions should not also apply to objects. Older
1477 versions of G++ silently ignore the linkage-specification
1485 which is clearly wrong. Therefore, we now treat objects
1487 if (current_lang_depth () == 0)
1489 /* There is no explicit linkage-specification, so we use
1490 the linkage from the previous declaration. */
1491 if (!DECL_LANG_SPECIFIC (newdecl))
1492 retrofit_lang_decl (newdecl);
1493 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1497 error ("previous declaration of %q+#D with %qL linkage",
1498 olddecl, DECL_LANGUAGE (olddecl));
1499 error ("conflicts with new declaration with %qL linkage",
1500 DECL_LANGUAGE (newdecl));
1504 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
1506 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
1508 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
1509 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
1512 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
1513 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
1515 for (; t1 && t1 != void_list_node;
1516 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
1517 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
1519 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
1522 pedwarn ("default argument given for parameter %d of %q#D",
1524 pedwarn ("after previous specification in %q+#D", olddecl);
1528 error ("default argument given for parameter %d of %q#D",
1530 error ("after previous specification in %q+#D",
1535 if (DECL_DECLARED_INLINE_P (newdecl)
1536 && ! DECL_DECLARED_INLINE_P (olddecl)
1537 && TREE_ADDRESSABLE (olddecl) && warn_inline)
1539 warning (0, "%q#D was used before it was declared inline", newdecl);
1540 warning (0, "%Jprevious non-inline declaration here", olddecl);
1545 /* Do not merge an implicit typedef with an explicit one. In:
1549 typedef class A A __attribute__ ((foo));
1551 the attribute should apply only to the typedef. */
1552 if (TREE_CODE (olddecl) == TYPE_DECL
1553 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
1554 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
1557 /* If new decl is `static' and an `extern' was seen previously,
1559 warn_extern_redeclared_static (newdecl, olddecl);
1561 /* We have committed to returning 1 at this point. */
1562 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1564 /* Now that functions must hold information normally held
1565 by field decls, there is extra work to do so that
1566 declaration information does not get destroyed during
1568 if (DECL_VINDEX (olddecl))
1569 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
1570 if (DECL_CONTEXT (olddecl))
1571 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
1572 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
1573 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
1574 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
1575 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
1576 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
1577 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
1578 SET_OVERLOADED_OPERATOR_CODE
1579 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
1580 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
1582 /* Optionally warn about more than one declaration for the same
1583 name, but don't warn about a function declaration followed by a
1585 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
1586 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
1587 /* Don't warn about extern decl followed by definition. */
1588 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
1589 /* Don't warn about friends, let add_friend take care of it. */
1590 && ! (newdecl_is_friend || DECL_FRIEND_P (olddecl)))
1592 warning (OPT_Wredundant_decls, "redundant redeclaration of %qD in same scope", newdecl);
1593 warning (OPT_Wredundant_decls, "previous declaration of %q+D", olddecl);
1597 /* Deal with C++: must preserve virtual function table size. */
1598 if (TREE_CODE (olddecl) == TYPE_DECL)
1600 tree newtype = TREE_TYPE (newdecl);
1601 tree oldtype = TREE_TYPE (olddecl);
1603 if (newtype != error_mark_node && oldtype != error_mark_node
1604 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
1605 CLASSTYPE_FRIEND_CLASSES (newtype)
1606 = CLASSTYPE_FRIEND_CLASSES (oldtype);
1608 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
1611 /* Copy all the DECL_... slots specified in the new decl
1612 except for any that we copy here from the old type. */
1613 DECL_ATTRIBUTES (newdecl)
1614 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
1616 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
1620 old_result = DECL_TEMPLATE_RESULT (olddecl);
1621 new_result = DECL_TEMPLATE_RESULT (newdecl);
1622 TREE_TYPE (olddecl) = TREE_TYPE (old_result);
1623 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
1624 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
1625 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
1627 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1629 DECL_INLINE (old_result)
1630 |= DECL_INLINE (new_result);
1631 DECL_DECLARED_INLINE_P (old_result)
1632 |= DECL_DECLARED_INLINE_P (new_result);
1633 check_redeclaration_exception_specification (newdecl, olddecl);
1636 /* If the new declaration is a definition, update the file and
1637 line information on the declaration. */
1638 if (DECL_INITIAL (old_result) == NULL_TREE
1639 && DECL_INITIAL (new_result) != NULL_TREE)
1641 DECL_SOURCE_LOCATION (olddecl)
1642 = DECL_SOURCE_LOCATION (old_result)
1643 = DECL_SOURCE_LOCATION (newdecl);
1644 if (DECL_FUNCTION_TEMPLATE_P (newdecl))
1645 DECL_ARGUMENTS (old_result)
1646 = DECL_ARGUMENTS (new_result);
1654 /* Automatically handles default parameters. */
1655 tree oldtype = TREE_TYPE (olddecl);
1658 /* Merge the data types specified in the two decls. */
1659 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
1661 /* If merge_types produces a non-typedef type, just use the old type. */
1662 if (TREE_CODE (newdecl) == TYPE_DECL
1663 && newtype == DECL_ORIGINAL_TYPE (newdecl))
1666 if (TREE_CODE (newdecl) == VAR_DECL)
1668 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
1669 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
1670 DECL_NONTRIVIALLY_INITIALIZED_P (newdecl)
1671 |= DECL_NONTRIVIALLY_INITIALIZED_P (olddecl);
1672 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (newdecl)
1673 |= DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (olddecl);
1675 /* Merge the threadprivate attribute from OLDDECL into NEWDECL. */
1676 if (DECL_LANG_SPECIFIC (olddecl)
1677 && CP_DECL_THREADPRIVATE_P (olddecl))
1679 /* Allocate a LANG_SPECIFIC structure for NEWDECL, if needed. */
1680 if (!DECL_LANG_SPECIFIC (newdecl))
1681 retrofit_lang_decl (newdecl);
1683 DECL_TLS_MODEL (newdecl) = DECL_TLS_MODEL (olddecl);
1684 CP_DECL_THREADPRIVATE_P (newdecl) = 1;
1688 /* Do this after calling `merge_types' so that default
1689 parameters don't confuse us. */
1690 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
1691 check_redeclaration_exception_specification (newdecl, olddecl);
1692 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
1694 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1695 check_default_args (newdecl);
1697 /* Lay the type out, unless already done. */
1698 if (! same_type_p (newtype, oldtype)
1699 && TREE_TYPE (newdecl) != error_mark_node
1700 && !(processing_template_decl && uses_template_parms (newdecl)))
1701 layout_type (TREE_TYPE (newdecl));
1703 if ((TREE_CODE (newdecl) == VAR_DECL
1704 || TREE_CODE (newdecl) == PARM_DECL
1705 || TREE_CODE (newdecl) == RESULT_DECL
1706 || TREE_CODE (newdecl) == FIELD_DECL
1707 || TREE_CODE (newdecl) == TYPE_DECL)
1708 && !(processing_template_decl && uses_template_parms (newdecl)))
1709 layout_decl (newdecl, 0);
1711 /* Merge the type qualifiers. */
1712 if (TREE_READONLY (newdecl))
1713 TREE_READONLY (olddecl) = 1;
1714 if (TREE_THIS_VOLATILE (newdecl))
1715 TREE_THIS_VOLATILE (olddecl) = 1;
1716 if (TREE_NOTHROW (newdecl))
1717 TREE_NOTHROW (olddecl) = 1;
1719 /* Merge deprecatedness. */
1720 if (TREE_DEPRECATED (newdecl))
1721 TREE_DEPRECATED (olddecl) = 1;
1723 /* Merge the initialization information. */
1724 if (DECL_INITIAL (newdecl) == NULL_TREE
1725 && DECL_INITIAL (olddecl) != NULL_TREE)
1727 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
1728 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
1729 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
1730 && DECL_LANG_SPECIFIC (newdecl)
1731 && DECL_LANG_SPECIFIC (olddecl))
1733 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
1734 DECL_STRUCT_FUNCTION (newdecl) = DECL_STRUCT_FUNCTION (olddecl);
1738 /* Merge the section attribute.
1739 We want to issue an error if the sections conflict but that must be
1740 done later in decl_attributes since we are called before attributes
1742 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
1743 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
1745 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1747 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
1748 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
1749 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
1750 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
1751 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
1752 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
1753 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
1754 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
1755 /* Keep the old RTL. */
1756 COPY_DECL_RTL (olddecl, newdecl);
1758 else if (TREE_CODE (newdecl) == VAR_DECL
1759 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
1761 /* Keep the old RTL. We cannot keep the old RTL if the old
1762 declaration was for an incomplete object and the new
1763 declaration is not since many attributes of the RTL will
1765 COPY_DECL_RTL (olddecl, newdecl);
1768 /* If cannot merge, then use the new type and qualifiers,
1769 and don't preserve the old rtl. */
1772 /* Clean out any memory we had of the old declaration. */
1773 tree oldstatic = value_member (olddecl, static_aggregates);
1775 TREE_VALUE (oldstatic) = error_mark_node;
1777 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
1778 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
1779 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
1780 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
1783 /* Merge the storage class information. */
1784 merge_weak (newdecl, olddecl);
1786 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
1787 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
1788 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
1789 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
1790 if (! DECL_EXTERNAL (olddecl))
1791 DECL_EXTERNAL (newdecl) = 0;
1793 new_template = NULL_TREE;
1794 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
1796 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
1797 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
1798 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
1799 DECL_TEMPLATE_INSTANTIATED (newdecl)
1800 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
1802 /* If the OLDDECL is an instantiation and/or specialization,
1803 then the NEWDECL must be too. But, it may not yet be marked
1804 as such if the caller has created NEWDECL, but has not yet
1805 figured out that it is a redeclaration. */
1806 if (!DECL_USE_TEMPLATE (newdecl))
1807 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
1809 /* Don't really know how much of the language-specific
1810 values we should copy from old to new. */
1811 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
1812 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
1813 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
1814 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
1815 DECL_REPO_AVAILABLE_P (newdecl) = DECL_REPO_AVAILABLE_P (olddecl);
1816 if (DECL_TEMPLATE_INFO (newdecl))
1817 new_template = DECL_TI_TEMPLATE (newdecl);
1818 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
1819 DECL_INITIALIZED_IN_CLASS_P (newdecl)
1820 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
1821 olddecl_friend = DECL_FRIEND_P (olddecl);
1822 hidden_friend = (DECL_ANTICIPATED (olddecl)
1823 && DECL_HIDDEN_FRIEND_P (olddecl)
1824 && newdecl_is_friend);
1826 /* Only functions have DECL_BEFRIENDING_CLASSES. */
1827 if (TREE_CODE (newdecl) == FUNCTION_DECL
1828 || DECL_FUNCTION_TEMPLATE_P (newdecl))
1830 DECL_BEFRIENDING_CLASSES (newdecl)
1831 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
1832 DECL_BEFRIENDING_CLASSES (olddecl));
1833 /* DECL_THUNKS is only valid for virtual functions,
1834 otherwise it is a DECL_FRIEND_CONTEXT. */
1835 if (DECL_VIRTUAL_P (newdecl))
1836 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
1840 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1842 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
1843 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
1845 /* If newdecl is not a specialization, then it is not a
1846 template-related function at all. And that means that we
1847 should have exited above, returning 0. */
1848 gcc_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl));
1850 if (TREE_USED (olddecl))
1851 /* From [temp.expl.spec]:
1853 If a template, a member template or the member of a class
1854 template is explicitly specialized then that
1855 specialization shall be declared before the first use of
1856 that specialization that would cause an implicit
1857 instantiation to take place, in every translation unit in
1858 which such a use occurs. */
1859 error ("explicit specialization of %qD after first use",
1862 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
1864 /* Don't propagate visibility from the template to the
1865 specialization here. We'll do that in determine_visibility if
1867 DECL_VISIBILITY_SPECIFIED (olddecl) = 0;
1869 /* [temp.expl.spec/14] We don't inline explicit specialization
1870 just because the primary template says so. */
1874 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
1875 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
1877 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
1879 /* If either decl says `inline', this fn is inline, unless
1880 its definition was passed already. */
1881 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
1882 DECL_INLINE (olddecl) = 1;
1883 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
1885 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
1886 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
1889 /* Preserve abstractness on cloned [cd]tors. */
1890 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
1894 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
1895 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
1896 COPY_DECL_RTL (newdecl, olddecl);
1898 if (! types_match || new_defines_function)
1900 /* These need to be copied so that the names are available.
1901 Note that if the types do match, we'll preserve inline
1902 info and other bits, but if not, we won't. */
1903 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
1904 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
1906 if (new_defines_function)
1907 /* If defining a function declared with other language
1908 linkage, use the previously declared language linkage. */
1909 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
1910 else if (types_match)
1912 /* If redeclaring a builtin function, and not a definition,
1913 it stays built in. */
1914 if (DECL_BUILT_IN (olddecl))
1916 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
1917 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
1918 /* If we're keeping the built-in definition, keep the rtl,
1919 regardless of declaration matches. */
1920 COPY_DECL_RTL (olddecl, newdecl);
1923 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
1924 /* Don't clear out the arguments if we're redefining a function. */
1925 if (DECL_ARGUMENTS (olddecl))
1926 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
1929 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
1930 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
1932 /* Now preserve various other info from the definition. */
1933 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
1934 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
1935 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
1936 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
1938 /* Warn about conflicting visibility specifications. */
1939 if (DECL_VISIBILITY_SPECIFIED (olddecl)
1940 && DECL_VISIBILITY_SPECIFIED (newdecl)
1941 && DECL_VISIBILITY (newdecl) != DECL_VISIBILITY (olddecl))
1943 warning (OPT_Wattributes, "%q+D: visibility attribute ignored "
1944 "because it", newdecl);
1945 warning (OPT_Wattributes, "%Jconflicts with previous "
1946 "declaration here", olddecl);
1948 /* Choose the declaration which specified visibility. */
1949 if (DECL_VISIBILITY_SPECIFIED (olddecl))
1951 DECL_VISIBILITY (newdecl) = DECL_VISIBILITY (olddecl);
1952 DECL_VISIBILITY_SPECIFIED (newdecl) = 1;
1954 /* Init priority used to be merged from newdecl to olddecl by the memcpy,
1955 so keep this behavior. */
1956 if (TREE_CODE (newdecl) == VAR_DECL && DECL_HAS_INIT_PRIORITY_P (newdecl))
1958 SET_DECL_INIT_PRIORITY (olddecl, DECL_INIT_PRIORITY (newdecl));
1959 DECL_HAS_INIT_PRIORITY_P (olddecl) = 1;
1962 /* The DECL_LANG_SPECIFIC information in OLDDECL will be replaced
1963 with that from NEWDECL below. */
1964 if (DECL_LANG_SPECIFIC (olddecl))
1966 gcc_assert (DECL_LANG_SPECIFIC (olddecl)
1967 != DECL_LANG_SPECIFIC (newdecl));
1968 ggc_free (DECL_LANG_SPECIFIC (olddecl));
1971 if (TREE_CODE (newdecl) == FUNCTION_DECL)
1975 function_size = sizeof (struct tree_decl_common);
1977 memcpy ((char *) olddecl + sizeof (struct tree_common),
1978 (char *) newdecl + sizeof (struct tree_common),
1979 function_size - sizeof (struct tree_common));
1981 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
1982 (char *) newdecl + sizeof (struct tree_decl_common),
1983 sizeof (struct tree_function_decl) - sizeof (struct tree_decl_common));
1985 /* If newdecl is a template instantiation, it is possible that
1986 the following sequence of events has occurred:
1988 o A friend function was declared in a class template. The
1989 class template was instantiated.
1991 o The instantiation of the friend declaration was
1992 recorded on the instantiation list, and is newdecl.
1994 o Later, however, instantiate_class_template called pushdecl
1995 on the newdecl to perform name injection. But, pushdecl in
1996 turn called duplicate_decls when it discovered that another
1997 declaration of a global function with the same name already
2000 o Here, in duplicate_decls, we decided to clobber newdecl.
2002 If we're going to do that, we'd better make sure that
2003 olddecl, and not newdecl, is on the list of
2004 instantiations so that if we try to do the instantiation
2005 again we won't get the clobbered declaration. */
2006 reregister_specialization (newdecl,
2012 size_t size = tree_code_size (TREE_CODE (olddecl));
2013 memcpy ((char *) olddecl + sizeof (struct tree_common),
2014 (char *) newdecl + sizeof (struct tree_common),
2015 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
2016 switch (TREE_CODE (olddecl))
2026 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2027 (char *) newdecl + sizeof (struct tree_decl_common),
2028 size - sizeof (struct tree_decl_common)
2029 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2033 memcpy ((char *) olddecl + sizeof (struct tree_decl_common),
2034 (char *) newdecl + sizeof (struct tree_decl_common),
2035 sizeof (struct tree_decl_non_common) - sizeof (struct tree_decl_common)
2036 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
2040 DECL_UID (olddecl) = olddecl_uid;
2042 DECL_FRIEND_P (olddecl) = 1;
2045 DECL_ANTICIPATED (olddecl) = 1;
2046 DECL_HIDDEN_FRIEND_P (olddecl) = 1;
2049 /* NEWDECL contains the merged attribute lists.
2050 Update OLDDECL to be the same. */
2051 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
2053 /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
2054 so that encode_section_info has a chance to look at the new decl
2055 flags and attributes. */
2056 if (DECL_RTL_SET_P (olddecl)
2057 && (TREE_CODE (olddecl) == FUNCTION_DECL
2058 || (TREE_CODE (olddecl) == VAR_DECL
2059 && TREE_STATIC (olddecl))))
2060 make_decl_rtl (olddecl);
2062 /* The NEWDECL will no longer be needed. Because every out-of-class
2063 declaration of a member results in a call to duplicate_decls,
2064 freeing these nodes represents in a significant savings. */
2070 /* Return zero if the declaration NEWDECL is valid
2071 when the declaration OLDDECL (assumed to be for the same name)
2072 has already been seen.
2073 Otherwise return an error message format string with a %s
2074 where the identifier should go. */
2077 redeclaration_error_message (tree newdecl, tree olddecl)
2079 if (TREE_CODE (newdecl) == TYPE_DECL)
2081 /* Because C++ can put things into name space for free,
2082 constructs like "typedef struct foo { ... } foo"
2083 would look like an erroneous redeclaration. */
2084 if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2087 return "redefinition of %q#D";
2089 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
2091 /* If this is a pure function, its olddecl will actually be
2092 the original initialization to `0' (which we force to call
2093 abort()). Don't complain about redefinition in this case. */
2094 if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl)
2095 && DECL_INITIAL (olddecl) == NULL_TREE)
2098 /* If both functions come from different namespaces, this is not
2099 a redeclaration - this is a conflict with a used function. */
2100 if (DECL_NAMESPACE_SCOPE_P (olddecl)
2101 && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl)
2102 && ! decls_match (olddecl, newdecl))
2103 return "%qD conflicts with used function";
2105 /* We'll complain about linkage mismatches in
2106 warn_extern_redeclared_static. */
2108 /* Defining the same name twice is no good. */
2109 if (DECL_INITIAL (olddecl) != NULL_TREE
2110 && DECL_INITIAL (newdecl) != NULL_TREE)
2112 if (DECL_NAME (olddecl) == NULL_TREE)
2113 return "%q#D not declared in class";
2115 return "redefinition of %q#D";
2119 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2123 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2125 if (COMPLETE_TYPE_P (TREE_TYPE (newdecl))
2126 && COMPLETE_TYPE_P (TREE_TYPE (olddecl)))
2127 return "redefinition of %q#D";
2131 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) != FUNCTION_DECL
2132 || (DECL_TEMPLATE_RESULT (newdecl)
2133 == DECL_TEMPLATE_RESULT (olddecl)))
2136 nt = DECL_TEMPLATE_RESULT (newdecl);
2137 if (DECL_TEMPLATE_INFO (nt))
2138 nt = DECL_TEMPLATE_RESULT (template_for_substitution (nt));
2139 ot = DECL_TEMPLATE_RESULT (olddecl);
2140 if (DECL_TEMPLATE_INFO (ot))
2141 ot = DECL_TEMPLATE_RESULT (template_for_substitution (ot));
2142 if (DECL_INITIAL (nt) && DECL_INITIAL (ot))
2143 return "redefinition of %q#D";
2147 else if (TREE_CODE (newdecl) == VAR_DECL
2148 && DECL_THREAD_LOCAL_P (newdecl) != DECL_THREAD_LOCAL_P (olddecl)
2149 && (! DECL_LANG_SPECIFIC (olddecl)
2150 || ! CP_DECL_THREADPRIVATE_P (olddecl)
2151 || DECL_THREAD_LOCAL_P (newdecl)))
2153 /* Only variables can be thread-local, and all declarations must
2154 agree on this property. */
2155 if (DECL_THREAD_LOCAL_P (newdecl))
2156 return "thread-local declaration of %q#D follows "
2157 "non-thread-local declaration";
2159 return "non-thread-local declaration of %q#D follows "
2160 "thread-local declaration";
2162 else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
2164 /* Objects declared at top level: */
2165 /* If at least one is a reference, it's ok. */
2166 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
2168 /* Reject two definitions. */
2169 return "redefinition of %q#D";
2173 /* Objects declared with block scope: */
2174 /* Reject two definitions, and reject a definition
2175 together with an external reference. */
2176 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
2177 return "redeclaration of %q#D";
2182 /* Hash and equality functions for the named_label table. */
2185 named_label_entry_hash (const void *data)
2187 const struct named_label_entry *ent = (const struct named_label_entry *) data;
2188 return DECL_UID (ent->label_decl);
2192 named_label_entry_eq (const void *a, const void *b)
2194 const struct named_label_entry *ent_a = (const struct named_label_entry *) a;
2195 const struct named_label_entry *ent_b = (const struct named_label_entry *) b;
2196 return ent_a->label_decl == ent_b->label_decl;
2199 /* Create a new label, named ID. */
2202 make_label_decl (tree id, int local_p)
2204 struct named_label_entry *ent;
2208 decl = build_decl (LABEL_DECL, id, void_type_node);
2210 DECL_CONTEXT (decl) = current_function_decl;
2211 DECL_MODE (decl) = VOIDmode;
2212 C_DECLARED_LABEL_FLAG (decl) = local_p;
2214 /* Say where one reference is to the label, for the sake of the
2215 error if it is not defined. */
2216 DECL_SOURCE_LOCATION (decl) = input_location;
2218 /* Record the fact that this identifier is bound to this label. */
2219 SET_IDENTIFIER_LABEL_VALUE (id, decl);
2221 /* Create the label htab for the function on demand. */
2223 named_labels = htab_create_ggc (13, named_label_entry_hash,
2224 named_label_entry_eq, NULL);
2226 /* Record this label on the list of labels used in this function.
2227 We do this before calling make_label_decl so that we get the
2228 IDENTIFIER_LABEL_VALUE before the new label is declared. */
2229 ent = GGC_CNEW (struct named_label_entry);
2230 ent->label_decl = decl;
2232 slot = htab_find_slot (named_labels, ent, INSERT);
2233 gcc_assert (*slot == NULL);
2239 /* Look for a label named ID in the current function. If one cannot
2240 be found, create one. (We keep track of used, but undefined,
2241 labels, and complain about them at the end of a function.) */
2244 lookup_label (tree id)
2248 timevar_push (TV_NAME_LOOKUP);
2249 /* You can't use labels at global scope. */
2250 if (current_function_decl == NULL_TREE)
2252 error ("label %qE referenced outside of any function", id);
2253 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2256 /* See if we've already got this label. */
2257 decl = IDENTIFIER_LABEL_VALUE (id);
2258 if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
2259 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2261 decl = make_label_decl (id, /*local_p=*/0);
2262 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2265 /* Declare a local label named ID. */
2268 declare_local_label (tree id)
2272 /* Add a new entry to the SHADOWED_LABELS list so that when we leave
2273 this scope we can restore the old value of IDENTIFIER_TYPE_VALUE. */
2274 shadow = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
2275 current_binding_level->shadowed_labels);
2276 current_binding_level->shadowed_labels = shadow;
2278 decl = make_label_decl (id, /*local_p=*/1);
2279 TREE_VALUE (shadow) = decl;
2284 /* Returns nonzero if it is ill-formed to jump past the declaration of
2285 DECL. Returns 2 if it's also a real problem. */
2288 decl_jump_unsafe (tree decl)
2290 if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl)
2291 || TREE_TYPE (decl) == error_mark_node)
2294 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
2295 || DECL_NONTRIVIALLY_INITIALIZED_P (decl))
2298 if (pod_type_p (TREE_TYPE (decl)))
2301 /* The POD stuff is just pedantry; why should it matter if the class
2302 contains a field of pointer to member type? */
2306 /* A subroutine of check_previous_goto_1 to identify a branch to the user. */
2309 identify_goto (tree decl, const location_t *locus)
2312 pedwarn ("jump to label %qD", decl);
2314 pedwarn ("jump to case label");
2316 pedwarn ("%H from here", locus);
2319 /* Check that a single previously seen jump to a newly defined label
2320 is OK. DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
2321 the jump context; NAMES are the names in scope in LEVEL at the jump
2322 context; LOCUS is the source position of the jump or 0. Returns
2323 true if all is well. */
2326 check_previous_goto_1 (tree decl, struct cp_binding_level* level, tree names,
2327 bool exited_omp, const location_t *locus)
2329 struct cp_binding_level *b;
2330 bool identified = false, saw_eh = false, saw_omp = false;
2334 identify_goto (decl, locus);
2335 error (" exits OpenMP structured block");
2336 identified = saw_omp = true;
2339 for (b = current_binding_level; b ; b = b->level_chain)
2341 tree new_decls, old_decls = (b == level ? names : NULL_TREE);
2343 for (new_decls = b->names; new_decls != old_decls;
2344 new_decls = TREE_CHAIN (new_decls))
2346 int problem = decl_jump_unsafe (new_decls);
2352 identify_goto (decl, locus);
2356 error (" crosses initialization of %q+#D", new_decls);
2358 pedwarn (" enters scope of non-POD %q+#D", new_decls);
2363 if ((b->kind == sk_try || b->kind == sk_catch) && !saw_eh)
2367 identify_goto (decl, locus);
2370 if (b->kind == sk_try)
2371 error (" enters try block");
2373 error (" enters catch block");
2376 if (b->kind == sk_omp && !saw_omp)
2380 identify_goto (decl, locus);
2383 error (" enters OpenMP structured block");
2392 check_previous_goto (tree decl, struct named_label_use_entry *use)
2394 check_previous_goto_1 (decl, use->binding_level,
2395 use->names_in_scope, use->in_omp_scope,
2396 &use->o_goto_locus);
2400 check_switch_goto (struct cp_binding_level* level)
2402 return check_previous_goto_1 (NULL_TREE, level, level->names, false, NULL);
2405 /* Check that a new jump to a label DECL is OK. Called by
2406 finish_goto_stmt. */
2409 check_goto (tree decl)
2411 struct named_label_entry *ent, dummy;
2412 bool saw_catch = false, identified = false;
2415 /* We can't know where a computed goto is jumping.
2416 So we assume that it's OK. */
2417 if (TREE_CODE (decl) != LABEL_DECL)
2420 /* We didn't record any information about this label when we created it,
2421 and there's not much point since it's trivial to analyze as a return. */
2422 if (decl == cdtor_label)
2425 dummy.label_decl = decl;
2426 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2427 gcc_assert (ent != NULL);
2429 /* If the label hasn't been defined yet, defer checking. */
2430 if (! DECL_INITIAL (decl))
2432 struct named_label_use_entry *new_use;
2434 /* Don't bother creating another use if the last goto had the
2435 same data, and will therefore create the same set of errors. */
2437 && ent->uses->names_in_scope == current_binding_level->names)
2440 new_use = GGC_NEW (struct named_label_use_entry);
2441 new_use->binding_level = current_binding_level;
2442 new_use->names_in_scope = current_binding_level->names;
2443 new_use->o_goto_locus = input_location;
2444 new_use->in_omp_scope = false;
2446 new_use->next = ent->uses;
2447 ent->uses = new_use;
2451 if (ent->in_try_scope || ent->in_catch_scope
2452 || ent->in_omp_scope || ent->bad_decls)
2454 pedwarn ("jump to label %q+D", decl);
2455 pedwarn (" from here");
2459 for (bad = ent->bad_decls; bad; bad = TREE_CHAIN (bad))
2461 tree b = TREE_VALUE (bad);
2462 int u = decl_jump_unsafe (b);
2464 if (u > 1 && DECL_ARTIFICIAL (b))
2466 /* Can't skip init of __exception_info. */
2467 error ("%J enters catch block", b);
2471 error (" skips initialization of %q+#D", b);
2473 pedwarn (" enters scope of non-POD %q+#D", b);
2476 if (ent->in_try_scope)
2477 error (" enters try block");
2478 else if (ent->in_catch_scope && !saw_catch)
2479 error (" enters catch block");
2481 if (ent->in_omp_scope)
2482 error (" enters OpenMP structured block");
2483 else if (flag_openmp)
2485 struct cp_binding_level *b;
2486 for (b = current_binding_level; b ; b = b->level_chain)
2488 if (b == ent->binding_level)
2490 if (b->kind == sk_omp)
2494 pedwarn ("jump to label %q+D", decl);
2495 pedwarn (" from here");
2498 error (" exits OpenMP structured block");
2505 /* Check that a return is ok wrt OpenMP structured blocks.
2506 Called by finish_return_stmt. Returns true if all is well. */
2509 check_omp_return (void)
2511 struct cp_binding_level *b;
2512 for (b = current_binding_level; b ; b = b->level_chain)
2513 if (b->kind == sk_omp)
2515 error ("invalid exit from OpenMP structured block");
2521 /* Define a label, specifying the location in the source file.
2522 Return the LABEL_DECL node for the label. */
2525 define_label (location_t location, tree name)
2527 struct named_label_entry *ent, dummy;
2528 struct cp_binding_level *p;
2531 timevar_push (TV_NAME_LOOKUP);
2533 decl = lookup_label (name);
2535 dummy.label_decl = decl;
2536 ent = (struct named_label_entry *) htab_find (named_labels, &dummy);
2537 gcc_assert (ent != NULL);
2539 /* After labels, make any new cleanups in the function go into their
2540 own new (temporary) binding contour. */
2541 for (p = current_binding_level;
2542 p->kind != sk_function_parms;
2544 p->more_cleanups_ok = 0;
2546 if (name == get_identifier ("wchar_t"))
2547 pedwarn ("label named wchar_t");
2549 if (DECL_INITIAL (decl) != NULL_TREE)
2551 error ("duplicate label %qD", decl);
2552 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
2556 struct named_label_use_entry *use;
2558 /* Mark label as having been defined. */
2559 DECL_INITIAL (decl) = error_mark_node;
2560 /* Say where in the source. */
2561 DECL_SOURCE_LOCATION (decl) = location;
2563 ent->binding_level = current_binding_level;
2564 ent->names_in_scope = current_binding_level->names;
2566 for (use = ent->uses; use ; use = use->next)
2567 check_previous_goto (decl, use);
2571 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
2576 struct cp_binding_level *level;
2577 struct cp_switch *next;
2578 /* The SWITCH_STMT being built. */
2580 /* A splay-tree mapping the low element of a case range to the high
2581 element, or NULL_TREE if there is no high element. Used to
2582 determine whether or not a new case label duplicates an old case
2583 label. We need a tree, rather than simply a hash table, because
2584 of the GNU case range extension. */
2588 /* A stack of the currently active switch statements. The innermost
2589 switch statement is on the top of the stack. There is no need to
2590 mark the stack for garbage collection because it is only active
2591 during the processing of the body of a function, and we never
2592 collect at that point. */
2594 static struct cp_switch *switch_stack;
2596 /* Called right after a switch-statement condition is parsed.
2597 SWITCH_STMT is the switch statement being parsed. */
2600 push_switch (tree switch_stmt)
2602 struct cp_switch *p = XNEW (struct cp_switch);
2603 p->level = current_binding_level;
2604 p->next = switch_stack;
2605 p->switch_stmt = switch_stmt;
2606 p->cases = splay_tree_new (case_compare, NULL, NULL);
2613 struct cp_switch *cs = switch_stack;
2614 location_t switch_location;
2616 /* Emit warnings as needed. */
2617 if (EXPR_HAS_LOCATION (cs->switch_stmt))
2618 switch_location = EXPR_LOCATION (cs->switch_stmt);
2620 switch_location = input_location;
2621 if (!processing_template_decl)
2622 c_do_switch_warnings (cs->cases, switch_location,
2623 SWITCH_STMT_TYPE (cs->switch_stmt),
2624 SWITCH_STMT_COND (cs->switch_stmt));
2626 splay_tree_delete (cs->cases);
2627 switch_stack = switch_stack->next;
2631 /* Note that we've seen a definition of a case label, and complain if this
2632 is a bad place for one. */
2635 finish_case_label (tree low_value, tree high_value)
2638 struct cp_binding_level *p;
2640 if (processing_template_decl)
2644 /* For templates, just add the case label; we'll do semantic
2645 analysis at instantiation-time. */
2646 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
2647 return add_stmt (build_case_label (low_value, high_value, label));
2650 /* Find the condition on which this switch statement depends. */
2651 cond = SWITCH_STMT_COND (switch_stack->switch_stmt);
2652 if (cond && TREE_CODE (cond) == TREE_LIST)
2653 cond = TREE_VALUE (cond);
2655 if (!check_switch_goto (switch_stack->level))
2656 return error_mark_node;
2658 r = c_add_case_label (switch_stack->cases, cond, TREE_TYPE (cond),
2659 low_value, high_value);
2661 /* After labels, make any new cleanups in the function go into their
2662 own new (temporary) binding contour. */
2663 for (p = current_binding_level;
2664 p->kind != sk_function_parms;
2666 p->more_cleanups_ok = 0;
2671 /* Hash a TYPENAME_TYPE. K is really of type `tree'. */
2674 typename_hash (const void* k)
2679 hash = (htab_hash_pointer (TYPE_CONTEXT (t))
2680 ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
2685 typedef struct typename_info {
2693 /* Compare two TYPENAME_TYPEs. K1 and K2 are really of type `tree'. */
2696 typename_compare (const void * k1, const void * k2)
2699 const typename_info *t2;
2702 t2 = (const typename_info *) k2;
2704 return (DECL_NAME (TYPE_NAME (t1)) == t2->name
2705 && TYPE_CONTEXT (t1) == t2->scope
2706 && TYPENAME_TYPE_FULLNAME (t1) == t2->template_id
2707 && TYPENAME_IS_ENUM_P (t1) == t2->enum_p
2708 && TYPENAME_IS_CLASS_P (t1) == t2->class_p);
2711 /* Build a TYPENAME_TYPE. If the type is `typename T::t', CONTEXT is
2712 the type of `T', NAME is the IDENTIFIER_NODE for `t'.
2714 Returns the new TYPENAME_TYPE. */
2716 static GTY ((param_is (union tree_node))) htab_t typename_htab;
2719 build_typename_type (tree context, tree name, tree fullname,
2720 enum tag_types tag_type)
2728 if (typename_htab == NULL)
2729 typename_htab = htab_create_ggc (61, &typename_hash,
2730 &typename_compare, NULL);
2732 ti.scope = FROB_CONTEXT (context);
2734 ti.template_id = fullname;
2735 ti.enum_p = tag_type == enum_type;
2736 ti.class_p = (tag_type == class_type
2737 || tag_type == record_type
2738 || tag_type == union_type);
2739 hash = (htab_hash_pointer (ti.scope)
2740 ^ htab_hash_pointer (ti.name));
2742 /* See if we already have this type. */
2743 e = htab_find_slot_with_hash (typename_htab, &ti, hash, INSERT);
2748 /* Build the TYPENAME_TYPE. */
2749 t = make_aggr_type (TYPENAME_TYPE);
2750 TYPE_CONTEXT (t) = ti.scope;
2751 TYPENAME_TYPE_FULLNAME (t) = ti.template_id;
2752 TYPENAME_IS_ENUM_P (t) = ti.enum_p;
2753 TYPENAME_IS_CLASS_P (t) = ti.class_p;
2755 /* Build the corresponding TYPE_DECL. */
2756 d = build_decl (TYPE_DECL, name, t);
2757 TYPE_NAME (TREE_TYPE (d)) = d;
2758 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2759 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2760 DECL_ARTIFICIAL (d) = 1;
2762 /* Store it in the hash table. */
2769 /* Resolve `typename CONTEXT::NAME'. TAG_TYPE indicates the tag
2770 provided to name the type. Returns an appropriate type, unless an
2771 error occurs, in which case error_mark_node is returned. If we
2772 locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is set, we
2773 return that, rather than the _TYPE it corresponds to, in other
2774 cases we look through the type decl. If TF_ERROR is set, complain
2775 about errors, otherwise be quiet. */
2778 make_typename_type (tree context, tree name, enum tag_types tag_type,
2779 tsubst_flags_t complain)
2785 if (name == error_mark_node
2786 || context == NULL_TREE
2787 || context == error_mark_node)
2788 return error_mark_node;
2792 if (!(TYPE_LANG_SPECIFIC (name)
2793 && (CLASSTYPE_IS_TEMPLATE (name)
2794 || CLASSTYPE_USE_TEMPLATE (name))))
2795 name = TYPE_IDENTIFIER (name);
2797 /* Create a TEMPLATE_ID_EXPR for the type. */
2798 name = build_nt (TEMPLATE_ID_EXPR,
2799 CLASSTYPE_TI_TEMPLATE (name),
2800 CLASSTYPE_TI_ARGS (name));
2802 else if (TREE_CODE (name) == TYPE_DECL)
2803 name = DECL_NAME (name);
2807 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
2809 name = TREE_OPERAND (name, 0);
2810 if (TREE_CODE (name) == TEMPLATE_DECL)
2811 name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
2813 if (TREE_CODE (name) == TEMPLATE_DECL)
2815 error ("%qD used without template parameters", name);
2816 return error_mark_node;
2818 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2819 gcc_assert (TYPE_P (context));
2821 /* When the CONTEXT is a dependent type, NAME could refer to a
2822 dependent base class of CONTEXT. So we cannot peek inside it,
2823 even if CONTEXT is a currently open scope. */
2824 if (dependent_type_p (context))
2825 return build_typename_type (context, name, fullname, tag_type);
2827 if (!IS_AGGR_TYPE (context))
2829 if (complain & tf_error)
2830 error ("%q#T is not a class", context);
2831 return error_mark_node;
2834 want_template = TREE_CODE (fullname) == TEMPLATE_ID_EXPR;
2836 /* We should only set WANT_TYPE when we're a nested typename type.
2837 Then we can give better diagnostics if we find a non-type. */
2838 t = lookup_field (context, name, 0, /*want_type=*/true);
2841 if (complain & tf_error)
2842 error (want_template ? "no class template named %q#T in %q#T"
2843 : "no type named %q#T in %q#T", name, context);
2844 return error_mark_node;
2847 if (want_template && !DECL_CLASS_TEMPLATE_P (t))
2849 if (complain & tf_error)
2850 error ("%<typename %T::%D%> names %q#T, which is not a class template",
2852 return error_mark_node;
2854 if (!want_template && TREE_CODE (t) != TYPE_DECL)
2856 if (complain & tf_error)
2857 error ("%<typename %T::%D%> names %q#T, which is not a type",
2859 return error_mark_node;
2862 if (complain & tf_error)
2863 perform_or_defer_access_check (TYPE_BINFO (context), t);
2866 return lookup_template_class (t, TREE_OPERAND (fullname, 1),
2868 /*entering_scope=*/0,
2869 tf_warning_or_error | tf_user);
2871 if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
2877 /* Resolve `CONTEXT::template NAME'. Returns a TEMPLATE_DECL if the name
2878 can be resolved or an UNBOUND_CLASS_TEMPLATE, unless an error occurs,
2879 in which case error_mark_node is returned.
2881 If PARM_LIST is non-NULL, also make sure that the template parameter
2882 list of TEMPLATE_DECL matches.
2884 If COMPLAIN zero, don't complain about any errors that occur. */
2887 make_unbound_class_template (tree context, tree name, tree parm_list,
2888 tsubst_flags_t complain)
2894 name = TYPE_IDENTIFIER (name);
2895 else if (DECL_P (name))
2896 name = DECL_NAME (name);
2897 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2899 if (!dependent_type_p (context)
2900 || currently_open_class (context))
2902 tree tmpl = NULL_TREE;
2904 if (IS_AGGR_TYPE (context))
2905 tmpl = lookup_field (context, name, 0, false);
2907 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
2909 if (complain & tf_error)
2910 error ("no class template named %q#T in %q#T", name, context);
2911 return error_mark_node;
2915 && !comp_template_parms (DECL_TEMPLATE_PARMS (tmpl), parm_list))
2917 if (complain & tf_error)
2919 error ("template parameters do not match template");
2920 error ("%q+D declared here", tmpl);
2922 return error_mark_node;
2925 if (complain & tf_error)
2926 perform_or_defer_access_check (TYPE_BINFO (context), tmpl);
2931 /* Build the UNBOUND_CLASS_TEMPLATE. */
2932 t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
2933 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
2934 TREE_TYPE (t) = NULL_TREE;
2936 /* Build the corresponding TEMPLATE_DECL. */
2937 d = build_decl (TEMPLATE_DECL, name, t);
2938 TYPE_NAME (TREE_TYPE (d)) = d;
2939 TYPE_STUB_DECL (TREE_TYPE (d)) = d;
2940 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2941 DECL_ARTIFICIAL (d) = 1;
2942 DECL_TEMPLATE_PARMS (d) = parm_list;
2949 /* Push the declarations of builtin types into the namespace.
2950 RID_INDEX is the index of the builtin type in the array
2951 RID_POINTERS. NAME is the name used when looking up the builtin
2952 type. TYPE is the _TYPE node for the builtin type. */
2955 record_builtin_type (enum rid rid_index,
2959 tree rname = NULL_TREE, tname = NULL_TREE;
2960 tree tdecl = NULL_TREE;
2962 if ((int) rid_index < (int) RID_MAX)
2963 rname = ridpointers[(int) rid_index];
2965 tname = get_identifier (name);
2967 /* The calls to SET_IDENTIFIER_GLOBAL_VALUE below should be
2968 eliminated. Built-in types should not be looked up name; their
2969 names are keywords that the parser can recognize. However, there
2970 is code in c-common.c that uses identifier_global_value to look
2971 up built-in types by name. */
2974 tdecl = build_decl (TYPE_DECL, tname, type);
2975 DECL_ARTIFICIAL (tdecl) = 1;
2976 SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
2982 tdecl = build_decl (TYPE_DECL, rname, type);
2983 DECL_ARTIFICIAL (tdecl) = 1;
2985 SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
2988 if (!TYPE_NAME (type))
2989 TYPE_NAME (type) = tdecl;
2992 debug_hooks->type_decl (tdecl, 0);
2995 /* Record one of the standard Java types.
2996 * Declare it as having the given NAME.
2997 * If SIZE > 0, it is the size of one of the integral types;
2998 * otherwise it is the negative of the size of one of the other types. */
3001 record_builtin_java_type (const char* name, int size)
3005 type = make_signed_type (size);
3006 else if (size > -32)
3007 { /* "__java_char" or ""__java_boolean". */
3008 type = make_unsigned_type (-size);
3009 /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
3012 { /* "__java_float" or ""__java_double". */
3013 type = make_node (REAL_TYPE);
3014 TYPE_PRECISION (type) = - size;
3017 record_builtin_type (RID_MAX, name, type);
3018 decl = TYPE_NAME (type);
3020 /* Suppress generate debug symbol entries for these types,
3021 since for normal C++ they are just clutter.
3022 However, push_lang_context undoes this if extern "Java" is seen. */
3023 DECL_IGNORED_P (decl) = 1;
3025 TYPE_FOR_JAVA (type) = 1;
3029 /* Push a type into the namespace so that the back-ends ignore it. */
3032 record_unknown_type (tree type, const char* name)
3034 tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
3035 /* Make sure the "unknown type" typedecl gets ignored for debug info. */
3036 DECL_IGNORED_P (decl) = 1;
3037 TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
3038 TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
3039 TYPE_ALIGN (type) = 1;
3040 TYPE_USER_ALIGN (type) = 0;
3041 TYPE_MODE (type) = TYPE_MODE (void_type_node);
3044 /* A string for which we should create an IDENTIFIER_NODE at
3047 typedef struct predefined_identifier
3049 /* The name of the identifier. */
3050 const char *const name;
3051 /* The place where the IDENTIFIER_NODE should be stored. */
3053 /* Nonzero if this is the name of a constructor or destructor. */
3054 const int ctor_or_dtor_p;
3055 } predefined_identifier;
3057 /* Create all the predefined identifiers. */
3060 initialize_predefined_identifiers (void)
3062 const predefined_identifier *pid;
3064 /* A table of identifiers to create at startup. */
3065 static const predefined_identifier predefined_identifiers[] = {
3066 { "C++", &lang_name_cplusplus, 0 },
3067 { "C", &lang_name_c, 0 },
3068 { "Java", &lang_name_java, 0 },
3069 /* Some of these names have a trailing space so that it is
3070 impossible for them to conflict with names written by users. */
3071 { "__ct ", &ctor_identifier, 1 },
3072 { "__base_ctor ", &base_ctor_identifier, 1 },
3073 { "__comp_ctor ", &complete_ctor_identifier, 1 },
3074 { "__dt ", &dtor_identifier, 1 },
3075 { "__comp_dtor ", &complete_dtor_identifier, 1 },
3076 { "__base_dtor ", &base_dtor_identifier, 1 },
3077 { "__deleting_dtor ", &deleting_dtor_identifier, 1 },
3078 { IN_CHARGE_NAME, &in_charge_identifier, 0 },
3079 { "nelts", &nelts_identifier, 0 },
3080 { THIS_NAME, &this_identifier, 0 },
3081 { VTABLE_DELTA_NAME, &delta_identifier, 0 },
3082 { VTABLE_PFN_NAME, &pfn_identifier, 0 },
3083 { "_vptr", &vptr_identifier, 0 },
3084 { "__vtt_parm", &vtt_parm_identifier, 0 },
3085 { "::", &global_scope_name, 0 },
3086 { "std", &std_identifier, 0 },
3090 for (pid = predefined_identifiers; pid->name; ++pid)
3092 *pid->node = get_identifier (pid->name);
3093 if (pid->ctor_or_dtor_p)
3094 IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
3098 /* Create the predefined scalar types of C,
3099 and some nodes representing standard constants (0, 1, (void *)0).
3100 Initialize the global binding level.
3101 Make definitions for built-in primitive functions. */
3104 cxx_init_decl_processing (void)
3107 tree void_ftype_ptr;
3109 build_common_tree_nodes (flag_signed_char, false);
3111 /* Create all the identifiers we need. */
3112 initialize_predefined_identifiers ();
3114 /* Create the global variables. */
3115 push_to_top_level ();
3117 current_function_decl = NULL_TREE;
3118 current_binding_level = NULL;
3119 /* Enter the global namespace. */
3120 gcc_assert (global_namespace == NULL_TREE);
3121 global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
3123 TREE_PUBLIC (global_namespace) = 1;
3124 begin_scope (sk_namespace, global_namespace);
3126 current_lang_name = NULL_TREE;
3128 /* Adjust various flags based on command-line settings. */
3129 if (!flag_permissive)
3130 flag_pedantic_errors = 1;
3131 if (!flag_no_inline)
3133 flag_inline_trees = 1;
3136 if (flag_inline_functions)
3137 flag_inline_trees = 2;
3139 /* Force minimum function alignment if using the least significant
3140 bit of function pointers to store the virtual bit. */
3141 if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
3142 && force_align_functions_log < 1)
3143 force_align_functions_log = 1;
3146 current_lang_name = lang_name_c;
3148 /* Create the `std' namespace. */
3149 push_namespace (std_identifier);
3150 std_node = current_namespace;
3153 c_common_nodes_and_builtins ();
3155 java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
3156 java_short_type_node = record_builtin_java_type ("__java_short", 16);
3157 java_int_type_node = record_builtin_java_type ("__java_int", 32);
3158 java_long_type_node = record_builtin_java_type ("__java_long", 64);
3159 java_float_type_node = record_builtin_java_type ("__java_float", -32);
3160 java_double_type_node = record_builtin_java_type ("__java_double", -64);
3161 java_char_type_node = record_builtin_java_type ("__java_char", -16);
3162 java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
3164 integer_two_node = build_int_cst (NULL_TREE, 2);
3165 integer_three_node = build_int_cst (NULL_TREE, 3);
3167 record_builtin_type (RID_BOOL, "bool", boolean_type_node);
3168 truthvalue_type_node = boolean_type_node;
3169 truthvalue_false_node = boolean_false_node;
3170 truthvalue_true_node = boolean_true_node;
3172 empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
3175 record_builtin_type (RID_MAX, NULL, string_type_node);
3178 delta_type_node = ptrdiff_type_node;
3179 vtable_index_type = ptrdiff_type_node;
3181 vtt_parm_type = build_pointer_type (const_ptr_type_node);
3182 void_ftype = build_function_type (void_type_node, void_list_node);
3183 void_ftype_ptr = build_function_type (void_type_node,
3184 tree_cons (NULL_TREE,
3188 = build_exception_variant (void_ftype_ptr, empty_except_spec);
3190 /* C++ extensions */
3192 unknown_type_node = make_node (UNKNOWN_TYPE);
3193 record_unknown_type (unknown_type_node, "unknown type");
3195 /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node. */
3196 TREE_TYPE (unknown_type_node) = unknown_type_node;
3198 /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
3200 TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
3201 TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
3204 /* Make sure we get a unique function type, so we can give
3205 its pointer type a name. (This wins for gdb.) */
3206 tree vfunc_type = make_node (FUNCTION_TYPE);
3207 TREE_TYPE (vfunc_type) = integer_type_node;
3208 TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
3209 layout_type (vfunc_type);
3211 vtable_entry_type = build_pointer_type (vfunc_type);
3213 record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
3216 = build_cplus_array_type (vtable_entry_type, NULL_TREE);
3217 layout_type (vtbl_type_node);
3218 vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
3219 record_builtin_type (RID_MAX, NULL, vtbl_type_node);
3220 vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
3221 layout_type (vtbl_ptr_type_node);
3222 record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
3224 push_namespace (get_identifier ("__cxxabiv1"));
3225 abi_node = current_namespace;
3228 global_type_node = make_node (LANG_TYPE);
3229 record_unknown_type (global_type_node, "global type");
3232 current_lang_name = lang_name_cplusplus;
3236 tree bad_alloc_type_node;
3237 tree bad_alloc_decl;
3238 tree newtype, deltype;
3239 tree ptr_ftype_sizetype;
3241 push_namespace (std_identifier);
3242 bad_alloc_id = get_identifier ("bad_alloc");
3243 bad_alloc_type_node = make_aggr_type (RECORD_TYPE);
3244 TYPE_CONTEXT (bad_alloc_type_node) = current_namespace;
3246 = create_implicit_typedef (bad_alloc_id, bad_alloc_type_node);
3247 DECL_CONTEXT (bad_alloc_decl) = current_namespace;
3248 TYPE_STUB_DECL (bad_alloc_type_node) = bad_alloc_decl;
3252 = build_function_type (ptr_type_node,
3253 tree_cons (NULL_TREE,
3256 newtype = build_exception_variant
3257 (ptr_ftype_sizetype, add_exception_specifier
3258 (NULL_TREE, bad_alloc_type_node, -1));
3259 deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
3260 push_cp_library_fn (NEW_EXPR, newtype);
3261 push_cp_library_fn (VEC_NEW_EXPR, newtype);
3262 global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
3263 push_cp_library_fn (VEC_DELETE_EXPR, deltype);
3267 = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
3269 /* Perform other language dependent initializations. */
3270 init_class_processing ();
3271 init_rtti_processing ();
3273 if (flag_exceptions)
3274 init_exception_processing ();
3276 if (! supports_one_only ())
3279 make_fname_decl = cp_make_fname_decl;
3280 start_fname_decls ();
3282 /* Show we use EH for cleanups. */
3283 if (flag_exceptions)
3284 using_eh_for_cleanups ();
3287 /* Generate an initializer for a function naming variable from
3288 NAME. NAME may be NULL, to indicate a dependent name. TYPE_P is
3289 filled in with the type of the init. */
3292 cp_fname_init (const char* name, tree *type_p)
3294 tree domain = NULL_TREE;
3296 tree init = NULL_TREE;
3301 length = strlen (name);
3302 domain = build_index_type (size_int (length));
3303 init = build_string (length + 1, name);
3306 type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
3307 type = build_cplus_array_type (type, domain);
3312 TREE_TYPE (init) = type;
3314 init = error_mark_node;
3319 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
3320 decl, NAME is the initialization string and TYPE_DEP indicates whether
3321 NAME depended on the type of the function. We make use of that to detect
3322 __PRETTY_FUNCTION__ inside a template fn. This is being done
3323 lazily at the point of first use, so we mustn't push the decl now. */
3326 cp_make_fname_decl (tree id, int type_dep)
3328 const char *const name = (type_dep && processing_template_decl
3329 ? NULL : fname_as_string (type_dep));
3331 tree init = cp_fname_init (name, &type);
3332 tree decl = build_decl (VAR_DECL, id, type);
3335 free ((char *) name);
3337 /* As we're using pushdecl_with_scope, we must set the context. */
3338 DECL_CONTEXT (decl) = current_function_decl;
3339 DECL_PRETTY_FUNCTION_P (decl) = type_dep;
3341 TREE_STATIC (decl) = 1;
3342 TREE_READONLY (decl) = 1;
3343 DECL_ARTIFICIAL (decl) = 1;
3345 TREE_USED (decl) = 1;
3347 if (current_function_decl)
3349 struct cp_binding_level *b = current_binding_level;
3350 while (b->level_chain->kind != sk_function_parms)
3352 pushdecl_with_scope (decl, b, /*is_friend=*/false);
3353 cp_finish_decl (decl, init, /*init_const_expr_p=*/false, NULL_TREE,
3354 LOOKUP_ONLYCONVERTING);
3357 pushdecl_top_level_and_finish (decl, init);
3362 /* Make a definition for a builtin function named NAME in the current
3363 namespace, whose data type is TYPE and whose context is CONTEXT.
3364 TYPE should be a function type with argument types.
3366 CLASS and CODE tell later passes how to compile calls to this function.
3367 See tree.h for possible values.
3369 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3370 the name to be called if we can't opencode the function.
3371 If ATTRS is nonzero, use that for the function's attribute
3375 builtin_function_1 (const char* name,
3378 enum built_in_function code,
3379 enum built_in_class class,
3380 const char* libname,
3383 tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
3384 DECL_BUILT_IN_CLASS (decl) = class;
3385 DECL_FUNCTION_CODE (decl) = code;
3386 DECL_CONTEXT (decl) = context;
3390 /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
3391 we cannot change DECL_ASSEMBLER_NAME until we have installed this
3392 function in the namespace. */
3394 SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
3396 /* A function in the user's namespace should have an explicit
3397 declaration before it is used. Mark the built-in function as
3398 anticipated but not actually declared. */
3399 if (name[0] != '_' || name[1] != '_')
3400 DECL_ANTICIPATED (decl) = 1;
3402 /* Possibly apply some default attributes to this built-in function. */
3404 decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
3406 decl_attributes (&decl, NULL_TREE, 0);
3411 /* Entry point for the benefit of c_common_nodes_and_builtins.
3413 Make a definition for a builtin function named NAME and whose data type
3414 is TYPE. TYPE should be a function type with argument types. This
3415 function places the anticipated declaration in the global namespace
3416 and additionally in the std namespace if appropriate.
3418 CLASS and CODE tell later passes how to compile calls to this function.
3419 See tree.h for possible values.
3421 If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
3422 the name to be called if we can't opencode the function.
3424 If ATTRS is nonzero, use that for the function's attribute
3428 builtin_function (const char* name,
3431 enum built_in_class cl,
3432 const char* libname,
3435 /* All builtins that don't begin with an '_' should additionally
3436 go in the 'std' namespace. */
3439 push_namespace (std_identifier);
3440 builtin_function_1 (name, type, std_node, code, cl, libname, attrs);
3444 return builtin_function_1 (name, type, NULL_TREE, code,
3445 cl, libname, attrs);
3448 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
3449 function. Not called directly. */
3452 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
3454 tree fn = build_lang_decl (FUNCTION_DECL, name, type);
3455 DECL_EXTERNAL (fn) = 1;
3456 TREE_PUBLIC (fn) = 1;
3457 DECL_ARTIFICIAL (fn) = 1;
3458 SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
3459 SET_DECL_LANGUAGE (fn, lang_c);
3460 /* Runtime library routines are, by definition, available in an
3461 external shared object. */
3462 DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
3463 DECL_VISIBILITY_SPECIFIED (fn) = 1;
3467 /* Returns the _DECL for a library function with C linkage.
3468 We assume that such functions never throw; if this is incorrect,
3469 callers should unset TREE_NOTHROW. */
3472 build_library_fn (tree name, tree type)
3474 tree fn = build_library_fn_1 (name, ERROR_MARK, type);
3475 TREE_NOTHROW (fn) = 1;
3479 /* Returns the _DECL for a library function with C++ linkage. */
3482 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
3484 tree fn = build_library_fn_1 (name, operator_code, type);
3485 TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
3486 DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
3487 SET_DECL_LANGUAGE (fn, lang_cplusplus);
3491 /* Like build_library_fn, but takes a C string instead of an
3495 build_library_fn_ptr (const char* name, tree type)
3497 return build_library_fn (get_identifier (name), type);
3500 /* Like build_cp_library_fn, but takes a C string instead of an
3504 build_cp_library_fn_ptr (const char* name, tree type)
3506 return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
3509 /* Like build_library_fn, but also pushes the function so that we will
3510 be able to find it via IDENTIFIER_GLOBAL_VALUE. */
3513 push_library_fn (tree name, tree type)
3515 tree fn = build_library_fn (name, type);
3516 pushdecl_top_level (fn);
3520 /* Like build_cp_library_fn, but also pushes the function so that it
3521 will be found by normal lookup. */
3524 push_cp_library_fn (enum tree_code operator_code, tree type)
3526 tree fn = build_cp_library_fn (ansi_opname (operator_code),