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 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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, 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"
51 #include "diagnostic.h"
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
58 static void push_binding_level (struct cp_binding_level *, int,
60 static void pop_binding_level (void);
61 static void suspend_binding_level (void);
62 static void resume_binding_level (struct cp_binding_level *);
63 static struct cp_binding_level *make_binding_level (void);
64 static void declare_namespace_level (void);
65 static int decl_jump_unsafe (tree);
66 static void storedecls (tree);
67 static void require_complete_types_for_parms (tree);
68 static int ambi_op_p (enum tree_code);
69 static int unary_op_p (enum tree_code);
70 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
71 static tree lookup_tag_reverse (tree, tree);
72 static void push_local_name (tree);
73 static void warn_extern_redeclared_static (tree, tree);
74 static tree grok_reference_init (tree, tree, tree);
75 static tree grokfndecl (tree, tree, tree, tree, int,
76 enum overload_flags, tree,
77 tree, int, int, int, int, int, int, tree);
78 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
79 static tree follow_tag_typedef (tree);
80 static tree lookup_tag (enum tree_code, tree,
81 struct cp_binding_level *, int);
82 static void set_identifier_type_value_with_scope
83 (tree, tree, struct cp_binding_level *);
84 static void record_unknown_type (tree, const char *);
85 static tree builtin_function_1 (const char *, tree, tree, int,
86 enum built_in_class, const char *,
88 static tree build_library_fn_1 (tree, enum tree_code, tree);
89 static int member_function_or_else (tree, tree, enum overload_flags);
90 static void bad_specifiers (tree, const char *, int, int, int, int,
92 static tree maybe_process_template_type_declaration
93 (tree, int, struct cp_binding_level*);
94 static void check_for_uninitialized_const_var (tree);
95 static hashval_t typename_hash (const void *);
96 static int typename_compare (const void *, const void *);
97 static void push_binding (tree, tree, struct cp_binding_level*);
98 static int add_binding (tree, tree);
99 static void pop_binding (tree, tree);
100 static tree local_variable_p_walkfn (tree *, int *, void *);
101 static tree select_decl (cxx_binding *, int);
102 static int lookup_flags (int, int);
103 static tree qualify_lookup (tree, int);
104 static tree record_builtin_java_type (const char *, int);
105 static const char *tag_name (enum tag_types code);
106 static void find_class_binding_level (void);
107 static struct cp_binding_level *innermost_nonclass_level (void);
108 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
109 static int walk_globals_r (tree, void*);
110 static int walk_vtables_r (tree, void*);
111 static void add_decl_to_level (tree, struct cp_binding_level *);
112 static tree make_label_decl (tree, int);
113 static void use_label (tree);
114 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
116 static void check_previous_goto (struct named_label_use_list *);
117 static void check_switch_goto (struct cp_binding_level *);
118 static void check_previous_gotos (tree);
119 static void pop_label (tree, tree);
120 static void pop_labels (tree);
121 static void maybe_deduce_size_from_array_init (tree, tree);
122 static void layout_var_decl (tree);
123 static void maybe_commonize_var (tree);
124 static tree check_initializer (tree, tree, int);
125 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
126 static void save_function_data (tree);
127 static void check_function_type (tree, tree);
128 static void begin_constructor_body (void);
129 static void finish_constructor_body (void);
130 static void begin_destructor_body (void);
131 static void finish_destructor_body (void);
132 static tree create_array_type_for_decl (tree, tree, tree);
133 static tree get_atexit_node (void);
134 static tree get_dso_handle_node (void);
135 static tree start_cleanup_fn (void);
136 static void end_cleanup_fn (void);
137 static tree cp_make_fname_decl (tree, int);
138 static void initialize_predefined_identifiers (void);
139 static tree check_special_function_return_type
140 (special_function_kind, tree, tree);
141 static tree push_cp_library_fn (enum tree_code, tree);
142 static tree build_cp_library_fn (tree, enum tree_code, tree);
143 static void store_parm_decls (tree);
144 static int cp_missing_noreturn_ok_p (tree);
145 static void initialize_local_var (tree, tree);
146 static void expand_static_init (tree, tree);
147 static tree next_initializable_field (tree);
148 static tree reshape_init (tree, tree *);
149 static tree build_typename_type (tree, tree, tree);
151 #if defined (DEBUG_BINDING_LEVELS)
152 static void indent (void);
155 /* Erroneous argument lists can use this *IFF* they do not modify it. */
156 tree error_mark_list;
158 /* The following symbols are subsumed in the cp_global_trees array, and
159 listed here individually for documentation purposes.
162 tree wchar_decl_node;
164 tree vtable_entry_type;
165 tree delta_type_node;
166 tree __t_desc_type_node;
167 tree ti_desc_type_node;
168 tree bltn_desc_type_node, ptr_desc_type_node;
169 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
170 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
171 tree ptm_desc_type_node;
172 tree base_desc_type_node;
174 tree class_type_node, record_type_node, union_type_node, enum_type_node;
175 tree unknown_type_node;
177 Array type `vtable_entry_type[]'
180 tree vtbl_ptr_type_node;
187 A FUNCTION_DECL which can call `abort'. Not necessarily the
188 one that the user will declare, but sufficient to be called
189 by routines that want to abort the program.
193 The FUNCTION_DECL for the default `::operator delete'.
195 tree global_delete_fndecl;
198 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
203 tree cp_global_trees[CPTI_MAX];
205 /* Indicates that there is a type value in some namespace, although
206 that is not necessarily in scope at the moment. */
208 static GTY(()) tree global_type_node;
210 /* Used only for jumps to as-yet undefined labels, since jumps to
211 defined labels can have their validity checked immediately. */
213 struct named_label_use_list GTY(())
215 struct cp_binding_level *binding_level;
218 const char *filename_o_goto;
220 struct named_label_use_list *next;
223 #define named_label_uses cp_function_chain->x_named_label_uses
225 #define local_names cp_function_chain->x_local_names
227 /* A list of objects which have constructors or destructors
228 which reside in the global scope. The decl is stored in
229 the TREE_VALUE slot and the initializer is stored
230 in the TREE_PURPOSE slot. */
231 tree static_aggregates;
235 /* A node for the integer constants 2, and 3. */
237 tree integer_two_node, integer_three_node;
239 /* Similar, for last_function_parm_tags. */
240 tree last_function_parms;
242 /* A list of all LABEL_DECLs in the function that have names. Here so
243 we can clear out their names' definitions at the end of the
244 function, and so we can check the validity of jumps to these labels. */
246 struct named_label_list GTY(())
248 struct cp_binding_level *binding_level;
253 struct named_label_list *next;
254 unsigned int in_try_scope : 1;
255 unsigned int in_catch_scope : 1;
258 #define named_labels cp_function_chain->x_named_labels
260 /* The name of the anonymous namespace, throughout this translation
262 tree anonymous_namespace_name;
264 /* The number of function bodies which we are currently processing.
265 (Zero if we are at namespace scope, one inside the body of a
266 function, two inside the body of a function in a local class, etc.) */
269 /* States indicating how grokdeclarator() should handle declspecs marked
270 with __attribute__((deprecated)). An object declared as
271 __attribute__((deprecated)) suppresses warnings of uses of other
274 enum deprecated_states {
279 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
281 /* Set by add_implicitly_declared_members() to keep those members from
282 being flagged as deprecated or reported as using deprecated
284 int adding_implicit_members = 0;
286 /* True if a declaration with an `extern' linkage specifier is being
288 bool have_extern_spec;
291 /* For each binding contour we allocate a binding_level structure
292 which records the names defined in that contour.
295 1) one for each function definition,
296 where internal declarations of the parameters appear.
297 2) one for each compound statement,
298 to record its declarations.
300 The current meaning of a name can be found by searching the levels
301 from the current one out to the global one.
303 Off to the side, may be the class_binding_level. This exists only
304 to catch class-local declarations. It is otherwise nonexistent.
306 Also there may be binding levels that catch cleanups that must be
307 run when exceptions occur. Thus, to see whether a name is bound in
308 the current scope, it is not enough to look in the
309 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
312 /* Note that the information in the `names' component of the global contour
313 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
315 struct cp_binding_level GTY(())
317 /* A chain of _DECL nodes for all variables, constants, functions,
318 and typedef types. These are in the reverse of the order
319 supplied. There may be OVERLOADs on this list, too, but they
320 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
323 /* Count of elements in names chain. */
326 /* A chain of NAMESPACE_DECL nodes. */
329 /* An array of static functions and variables (for namespaces only) */
330 varray_type static_decls;
332 /* A chain of VTABLE_DECL nodes. */
335 /* A list of structure, union and enum definitions, for looking up
337 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
338 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
339 or ENUMERAL_TYPE node.
341 C++: the TREE_VALUE nodes can be simple types for
342 component_bindings. */
345 /* A list of USING_DECL nodes. */
348 /* A list of used namespaces. PURPOSE is the namespace,
349 VALUE the common ancestor with this binding_level's namespace. */
350 tree using_directives;
352 /* If this binding level is the binding level for a class, then
353 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
354 is the name of an entity bound in the class. The TREE_TYPE is
355 the DECL bound by this name in the class. */
358 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
359 is used for all binding levels. In addition the TREE_VALUE is the
360 IDENTIFIER_TYPE_VALUE before we entered the class. */
363 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
364 label in this scope. The TREE_PURPOSE is the previous value of
365 the IDENTIFIER_LABEL VALUE. */
366 tree shadowed_labels;
368 /* For each level (except not the global one),
369 a chain of BLOCK nodes for all the levels
370 that were entered and exited one level down. */
373 /* The _TYPE node for this level, if parm_flag == 2. */
376 /* The binding level which this one is contained in (inherits from). */
377 struct cp_binding_level *level_chain;
379 /* List of VAR_DECLS saved from a previous for statement.
380 These would be dead in ISO-conforming code, but might
381 be referenced in ARM-era code. These are stored in a
382 TREE_LIST; the TREE_VALUE is the actual declaration. */
383 tree dead_vars_from_for;
385 /* 1 for the level that holds the parameters of a function.
386 2 for the level that holds a class declaration. */
387 unsigned parm_flag : 2;
389 /* 1 means make a BLOCK for this level regardless of all else.
390 2 for temporary binding contours created by the compiler. */
393 /* Nonzero if this level "doesn't exist" for tags. */
394 unsigned tag_transparent : 1;
396 /* Nonzero if this level can safely have additional
397 cleanup-needing variables added to it. */
398 unsigned more_cleanups_ok : 1;
399 unsigned have_cleanups : 1;
401 /* Nonzero if this scope is for storing the decls for template
402 parameters and generic decls; these decls will be discarded and
403 replaced with a TEMPLATE_DECL. */
404 unsigned template_parms_p : 1;
406 /* Nonzero if this scope corresponds to the `<>' in a
407 `template <>' clause. Whenever this flag is set,
408 TEMPLATE_PARMS_P will be set as well. */
409 unsigned template_spec_p : 1;
411 /* This is set for a namespace binding level. */
412 unsigned namespace_p : 1;
414 /* True if this level is that of a for-statement where we need to
415 worry about ambiguous (ARM or ISO) scope rules. */
416 unsigned is_for_scope : 1;
418 /* True if this level corresponds to a TRY block. Currently this
419 information is only available while building the tree structure. */
420 unsigned is_try_scope : 1;
422 /* True if this level corresponds to a CATCH block. Currently this
423 information is only available while building the tree structure. */
424 unsigned is_catch_scope : 1;
426 /* Three bits left for this word. */
428 /* Binding depth at which this level began. */
429 unsigned binding_depth;
432 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
434 /* The binding level currently in effect. */
436 #define current_binding_level \
437 (*(cfun && cp_function_chain->bindings \
438 ? &cp_function_chain->bindings \
439 : &scope_chain->bindings))
441 /* The binding level of the current class, if any. */
443 #define class_binding_level scope_chain->class_bindings
445 /* A chain of binding_level structures awaiting reuse. */
447 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
449 /* The outermost binding level, for names of file scope.
450 This is created when the compiler is started and exists
451 through the entire run. */
453 static GTY(()) struct cp_binding_level *global_binding_level;
455 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
457 static int keep_next_level_flag;
459 /* A TREE_LIST of VAR_DECLs. The TREE_PURPOSE is a RECORD_TYPE or
460 UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type. At the
461 time the VAR_DECL was declared, the type was incomplete. */
463 static GTY(()) tree incomplete_vars;
465 #if defined(DEBUG_BINDING_LEVELS)
466 static int binding_depth = 0;
467 static int is_class_level = 0;
474 for (i = 0; i < binding_depth*2; i++)
477 #endif /* defined(DEBUG_BINDING_LEVELS) */
479 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
482 push_binding_level (struct cp_binding_level *newlevel,
486 /* Add this level to the front of the chain (stack) of levels that
488 memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
489 newlevel->level_chain = current_binding_level;
490 current_binding_level = newlevel;
491 newlevel->tag_transparent = tag_transparent;
492 newlevel->more_cleanups_ok = 1;
494 newlevel->keep = keep;
495 #if defined(DEBUG_BINDING_LEVELS)
496 newlevel->binding_depth = binding_depth;
498 fprintf (stderr, "push %s level 0x%08x line %d\n",
499 (is_class_level) ? "class" : "block", newlevel, lineno);
502 #endif /* defined(DEBUG_BINDING_LEVELS) */
505 /* Find the innermost enclosing class scope, and reset
506 CLASS_BINDING_LEVEL appropriately. */
509 find_class_binding_level (void)
511 struct cp_binding_level *level = current_binding_level;
513 while (level && level->parm_flag != 2)
514 level = level->level_chain;
515 if (level && level->parm_flag == 2)
516 class_binding_level = level;
518 class_binding_level = 0;
522 pop_binding_level (void)
524 if (global_binding_level)
526 /* Cannot pop a level, if there are none left to pop. */
527 if (current_binding_level == global_binding_level)
530 /* Pop the current level, and free the structure for reuse. */
531 #if defined(DEBUG_BINDING_LEVELS)
534 fprintf (stderr, "pop %s level 0x%08x line %d\n",
535 (is_class_level) ? "class" : "block",
536 current_binding_level, lineno);
537 if (is_class_level != (current_binding_level == class_binding_level))
540 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
543 #endif /* defined(DEBUG_BINDING_LEVELS) */
545 register struct cp_binding_level *level = current_binding_level;
546 current_binding_level = current_binding_level->level_chain;
547 level->level_chain = free_binding_level;
548 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
549 if (level->binding_depth != binding_depth)
551 #endif /* defined(DEBUG_BINDING_LEVELS) */
552 free_binding_level = level;
553 find_class_binding_level ();
558 suspend_binding_level (void)
560 if (class_binding_level)
561 current_binding_level = class_binding_level;
563 if (global_binding_level)
565 /* Cannot suspend a level, if there are none left to suspend. */
566 if (current_binding_level == global_binding_level)
569 /* Suspend the current level. */
570 #if defined(DEBUG_BINDING_LEVELS)
573 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
574 (is_class_level) ? "class" : "block",
575 current_binding_level, lineno);
576 if (is_class_level != (current_binding_level == class_binding_level))
579 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
582 #endif /* defined(DEBUG_BINDING_LEVELS) */
583 current_binding_level = current_binding_level->level_chain;
584 find_class_binding_level ();
588 resume_binding_level (struct cp_binding_level* b)
590 /* Resuming binding levels is meant only for namespaces,
591 and those cannot nest into classes. */
592 my_friendly_assert(!class_binding_level, 386);
593 /* Also, resuming a non-directly nested namespace is a no-no. */
594 my_friendly_assert(b->level_chain == current_binding_level, 386);
595 current_binding_level = b;
596 #if defined(DEBUG_BINDING_LEVELS)
597 b->binding_depth = binding_depth;
599 fprintf (stderr, "resume %s level 0x%08x line %d\n",
600 (is_class_level) ? "class" : "block", b, lineno);
603 #endif /* defined(DEBUG_BINDING_LEVELS) */
606 /* Create a new `struct cp_binding_level'. */
609 struct cp_binding_level *
610 make_binding_level (void)
613 return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
616 /* Nonzero if we are currently in the global binding level. */
619 global_bindings_p (void)
621 return current_binding_level == global_binding_level;
624 /* Return the innermost binding level that is not for a class scope. */
626 static struct cp_binding_level *
627 innermost_nonclass_level (void)
629 struct cp_binding_level *b;
631 b = current_binding_level;
632 while (b->parm_flag == 2)
638 /* Nonzero if we are currently in a toplevel binding level. This
639 means either the global binding level or a namespace in a toplevel
640 binding level. Since there are no non-toplevel namespace levels,
641 this really means any namespace or template parameter level. We
642 also include a class whose context is toplevel. */
645 toplevel_bindings_p (void)
647 struct cp_binding_level *b = innermost_nonclass_level ();
649 return b->namespace_p || b->template_parms_p;
652 /* Nonzero if this is a namespace scope, or if we are defining a class
653 which is itself at namespace scope, or whose enclosing class is
654 such a class, etc. */
657 namespace_bindings_p (void)
659 struct cp_binding_level *b = innermost_nonclass_level ();
661 return b->namespace_p;
664 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
665 unconditionally. Otherwise, use the normal logic to decide whether
666 or not to create a BLOCK. */
669 keep_next_level (int keep)
671 keep_next_level_flag = keep;
674 /* Nonzero if the current level needs to have a BLOCK made. */
679 return (current_binding_level->blocks != NULL_TREE
680 || current_binding_level->keep
681 || current_binding_level->names != NULL_TREE
682 || (current_binding_level->tags != NULL_TREE
683 && !current_binding_level->tag_transparent));
687 declare_namespace_level (void)
689 current_binding_level->namespace_p = 1;
692 /* Returns nonzero if this scope was created to store template
696 template_parm_scope_p (void)
698 return current_binding_level->template_parms_p;
701 /* Returns the kind of template specialization we are currently
702 processing, given that it's declaration contained N_CLASS_SCOPES
703 explicit scope qualifications. */
706 current_tmpl_spec_kind (int n_class_scopes)
708 int n_template_parm_scopes = 0;
709 int seen_specialization_p = 0;
710 int innermost_specialization_p = 0;
711 struct cp_binding_level *b;
713 /* Scan through the template parameter scopes. */
714 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
716 /* If we see a specialization scope inside a parameter scope,
717 then something is wrong. That corresponds to a declaration
720 template <class T> template <> ...
722 which is always invalid since [temp.expl.spec] forbids the
723 specialization of a class member template if the enclosing
724 class templates are not explicitly specialized as well. */
725 if (b->template_spec_p)
727 if (n_template_parm_scopes == 0)
728 innermost_specialization_p = 1;
730 seen_specialization_p = 1;
732 else if (seen_specialization_p == 1)
733 return tsk_invalid_member_spec;
735 ++n_template_parm_scopes;
738 /* Handle explicit instantiations. */
739 if (processing_explicit_instantiation)
741 if (n_template_parm_scopes != 0)
742 /* We've seen a template parameter list during an explicit
743 instantiation. For example:
745 template <class T> template void f(int);
747 This is erroneous. */
748 return tsk_invalid_expl_inst;
750 return tsk_expl_inst;
753 if (n_template_parm_scopes < n_class_scopes)
754 /* We've not seen enough template headers to match all the
755 specialized classes present. For example:
757 template <class T> void R<T>::S<T>::f(int);
759 This is invalid; there needs to be one set of template
760 parameters for each class. */
761 return tsk_insufficient_parms;
762 else if (n_template_parm_scopes == n_class_scopes)
763 /* We're processing a non-template declaration (even though it may
764 be a member of a template class.) For example:
766 template <class T> void S<T>::f(int);
768 The `class T' maches the `S<T>', leaving no template headers
769 corresponding to the `f'. */
771 else if (n_template_parm_scopes > n_class_scopes + 1)
772 /* We've got too many template headers. For example:
774 template <> template <class T> void f (T);
776 There need to be more enclosing classes. */
777 return tsk_excessive_parms;
779 /* This must be a template. It's of the form:
781 template <class T> template <class U> void S<T>::f(U);
783 This is a specialization if the innermost level was a
784 specialization; otherwise it's just a definition of the
786 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
790 set_class_shadows (tree shadows)
792 class_binding_level->class_shadowed = shadows;
795 /* Enter a new binding level.
796 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
797 not for that of tags. */
800 pushlevel (int tag_transparent)
802 struct cp_binding_level *newlevel;
804 if (cfun && !doing_semantic_analysis_p ())
807 /* Reuse or create a struct for this binding level. */
808 #if defined(DEBUG_BINDING_LEVELS)
810 #else /* !defined(DEBUG_BINDING_LEVELS) */
811 if (free_binding_level)
812 #endif /* !defined(DEBUG_BINDING_LEVELS) */
814 newlevel = free_binding_level;
815 free_binding_level = free_binding_level->level_chain;
818 newlevel = make_binding_level ();
820 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
821 keep_next_level_flag = 0;
824 /* We're defining an object of type TYPE. If it needs a cleanup, but
825 we're not allowed to add any more objects with cleanups to the current
826 scope, create a new binding level. */
829 maybe_push_cleanup_level (tree type)
831 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
832 && current_binding_level->more_cleanups_ok == 0)
837 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
841 /* Enter a new scope. The KIND indicates what kind of scope is being
845 begin_scope (scope_kind sk)
855 current_binding_level->is_try_scope = 1;
859 current_binding_level->is_catch_scope = 1;
863 current_binding_level->is_for_scope = 1;
866 case sk_template_spec:
867 current_binding_level->template_spec_p = 1;
870 case sk_template_parms:
871 current_binding_level->template_parms_p = 1;
879 /* Exit the current scope. */
887 /* Make DECL the innermost binding for ID. The LEVEL is the binding
888 level at which this declaration is being bound. */
891 push_binding (tree id, tree decl, cxx_scope* level)
893 cxx_binding *binding = cxx_binding_make (decl, NULL);
895 /* Now, fill in the binding information. */
896 binding->previous = IDENTIFIER_BINDING (id);
897 BINDING_SCOPE (binding) = level;
898 INHERITED_VALUE_BINDING_P (binding) = 0;
899 LOCAL_BINDING_P (binding) = (level != class_binding_level);
901 /* And put it on the front of the list of bindings for ID. */
902 IDENTIFIER_BINDING (id) = binding;
905 /* ID is already bound in the current scope. But, DECL is an
906 additional binding for ID in the same scope. This is the `struct
907 stat' hack whereby a non-typedef class-name or enum-name can be
908 bound at the same level as some other kind of entity. It's the
909 responsibility of the caller to check that inserting this name is
910 valid here. Returns nonzero if the new binding was successful. */
912 add_binding (tree id, tree decl)
914 cxx_binding *binding = IDENTIFIER_BINDING (id);
917 timevar_push (TV_NAME_LOOKUP);
918 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
919 /* The new name is the type name. */
920 BINDING_TYPE (binding) = decl;
921 else if (!BINDING_VALUE (binding))
922 /* This situation arises when push_class_level_binding moves an
923 inherited type-binding out of the way to make room for a new
925 BINDING_VALUE (binding) = decl;
926 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
927 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
929 /* The old binding was a type name. It was placed in
930 BINDING_VALUE because it was thought, at the point it was
931 declared, to be the only entity with such a name. Move the
932 type name into the type slot; it is now hidden by the new
934 BINDING_TYPE (binding) = BINDING_VALUE (binding);
935 BINDING_VALUE (binding) = decl;
936 INHERITED_VALUE_BINDING_P (binding) = 0;
938 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
939 && TREE_CODE (decl) == TYPE_DECL
940 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
941 && (same_type_p (TREE_TYPE (decl),
942 TREE_TYPE (BINDING_VALUE (binding)))
943 /* If either type involves template parameters, we must
944 wait until instantiation. */
945 || uses_template_parms (TREE_TYPE (decl))
946 || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
947 /* We have two typedef-names, both naming the same type to have
948 the same name. This is OK because of:
952 In a given scope, a typedef specifier can be used to redefine
953 the name of any type declared in that scope to refer to the
954 type to which it already refers. */
956 /* There can be two block-scope declarations of the same variable,
957 so long as they are `extern' declarations. */
958 else if (TREE_CODE (decl) == VAR_DECL
959 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
960 && DECL_EXTERNAL (decl)
961 && DECL_EXTERNAL (BINDING_VALUE (binding)))
963 duplicate_decls (decl, BINDING_VALUE (binding));
968 error ("declaration of `%#D'", decl);
969 cp_error_at ("conflicts with previous declaration `%#D'",
970 BINDING_VALUE (binding));
974 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
977 /* Add DECL to the list of things declared in B. */
980 add_decl_to_level (tree decl,
981 struct cp_binding_level* b)
983 if (TREE_CODE (decl) == NAMESPACE_DECL
984 && !DECL_NAMESPACE_ALIAS (decl))
986 TREE_CHAIN (decl) = b->namespaces;
987 b->namespaces = decl;
989 else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
991 TREE_CHAIN (decl) = b->vtables;
996 /* We build up the list in reverse order, and reverse it later if
998 TREE_CHAIN (decl) = b->names;
1002 /* If appropriate, add decl to separate list of statics */
1004 if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1005 || (TREE_CODE (decl) == FUNCTION_DECL
1006 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1007 VARRAY_PUSH_TREE (b->static_decls, decl);
1011 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1012 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1013 doesn't really belong to this binding level, that it got here
1014 through a using-declaration. */
1017 push_local_binding (tree id, tree decl, int flags)
1019 struct cp_binding_level *b;
1021 /* Skip over any local classes. This makes sense if we call
1022 push_local_binding with a friend decl of a local class. */
1023 b = current_binding_level;
1024 while (b->parm_flag == 2)
1027 if (lookup_name_current_level (id))
1029 /* Supplement the existing binding. */
1030 if (!add_binding (id, decl))
1031 /* It didn't work. Something else must be bound at this
1032 level. Do not add DECL to the list of things to pop
1037 /* Create a new binding. */
1038 push_binding (id, decl, b);
1040 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1041 /* We must put the OVERLOAD into a TREE_LIST since the
1042 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1043 decls that got here through a using-declaration. */
1044 decl = build_tree_list (NULL_TREE, decl);
1046 /* And put DECL on the list of things declared by the current
1048 add_decl_to_level (decl, b);
1051 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1052 binding was successful. */
1055 push_class_binding (tree id, tree decl)
1058 cxx_binding *binding = IDENTIFIER_BINDING (id);
1061 timevar_push (TV_NAME_LOOKUP);
1062 /* Note that we declared this value so that we can issue an error if
1063 this is an invalid redeclaration of a name already used for some
1065 note_name_declared_in_class (id, decl);
1067 if (binding && BINDING_SCOPE (binding) == class_binding_level)
1068 /* Supplement the existing binding. */
1069 result = add_binding (id, decl);
1071 /* Create a new binding. */
1072 push_binding (id, decl, class_binding_level);
1074 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1075 class-level declaration. Note that we do not use DECL here
1076 because of the possibility of the `struct stat' hack; if DECL is
1077 a class-name or enum-name we might prefer a field-name, or some
1079 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1081 /* If this is a binding from a base class, mark it as such. */
1082 binding = IDENTIFIER_BINDING (id);
1083 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1085 if (TREE_CODE (decl) == OVERLOAD)
1086 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1089 my_friendly_assert (DECL_P (decl), 0);
1090 context = context_for_name_lookup (decl);
1093 if (is_properly_derived_from (current_class_type, context))
1094 INHERITED_VALUE_BINDING_P (binding) = 1;
1096 INHERITED_VALUE_BINDING_P (binding) = 0;
1098 else if (BINDING_VALUE (binding) == decl)
1099 /* We only encounter a TREE_LIST when push_class_decls detects an
1100 ambiguity. Such an ambiguity can be overridden by a definition
1102 INHERITED_VALUE_BINDING_P (binding) = 1;
1104 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1107 /* Remove the binding for DECL which should be the innermost binding
1111 pop_binding (tree id, tree decl)
1113 cxx_binding *binding;
1115 if (id == NULL_TREE)
1116 /* It's easiest to write the loops that call this function without
1117 checking whether or not the entities involved have names. We
1118 get here for such an entity. */
1121 /* Get the innermost binding for ID. */
1122 binding = IDENTIFIER_BINDING (id);
1124 /* The name should be bound. */
1125 my_friendly_assert (binding != NULL, 0);
1127 /* The DECL will be either the ordinary binding or the type
1128 binding for this identifier. Remove that binding. */
1129 if (BINDING_VALUE (binding) == decl)
1130 BINDING_VALUE (binding) = NULL_TREE;
1131 else if (BINDING_TYPE (binding) == decl)
1132 BINDING_TYPE (binding) = NULL_TREE;
1136 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1138 /* We're completely done with the innermost binding for this
1139 identifier. Unhook it from the list of bindings. */
1140 IDENTIFIER_BINDING (id) = binding->previous;
1142 /* Add it to the free list. */
1143 cxx_binding_free (binding);
1145 /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1147 BINDING_SCOPE (binding) = NULL;
1151 /* When a label goes out of scope, check to see if that label was used
1152 in a valid manner, and issue any appropriate warnings or errors. */
1155 pop_label (tree label, tree old_value)
1157 if (!processing_template_decl && doing_semantic_analysis_p ())
1159 if (DECL_INITIAL (label) == NULL_TREE)
1161 cp_error_at ("label `%D' used but not defined", label);
1162 /* Avoid crashing later. */
1163 define_label (input_filename, 1, DECL_NAME (label));
1165 else if (warn_unused_label && !TREE_USED (label))
1166 cp_warning_at ("label `%D' defined but not used", label);
1169 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1172 /* At the end of a function, all labels declared within the function
1173 go out of scope. BLOCK is the top-level block for the
1177 pop_labels (tree block)
1179 struct named_label_list *link;
1181 /* Clear out the definitions of all label names, since their scopes
1183 for (link = named_labels; link; link = link->next)
1185 pop_label (link->label_decl, link->old_value);
1186 /* Put the labels into the "variables" of the top-level block,
1187 so debugger can see them. */
1188 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1189 BLOCK_VARS (block) = link->label_decl;
1192 named_labels = NULL;
1195 /* Exit a binding level.
1196 Pop the level off, and restore the state of the identifier-decl mappings
1197 that were in effect when this level was entered.
1199 If KEEP == 1, this level had explicit declarations, so
1200 and create a "block" (a BLOCK node) for the level
1201 to record its declarations and subblocks for symbol table output.
1203 If FUNCTIONBODY is nonzero, this level is the body of a function,
1204 so create a block as if KEEP were set and also clear out all
1207 If REVERSE is nonzero, reverse the order of decls before putting
1208 them into the BLOCK. */
1211 poplevel (int keep, int reverse, int functionbody)
1214 /* The chain of decls was accumulated in reverse order.
1215 Put it into forward order, just for cleanliness. */
1217 int tmp = functionbody;
1218 int real_functionbody;
1221 tree block = NULL_TREE;
1223 int leaving_for_scope;
1225 timevar_push (TV_NAME_LOOKUP);
1226 if (cfun && !doing_semantic_analysis_p ())
1227 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1229 my_friendly_assert (current_binding_level->parm_flag != 2,
1232 real_functionbody = (current_binding_level->keep == 2
1233 ? ((functionbody = 0), tmp) : functionbody);
1234 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1235 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1237 my_friendly_assert (!current_binding_level->class_shadowed,
1240 /* We used to use KEEP == 2 to indicate that the new block should go
1241 at the beginning of the list of blocks at this binding level,
1242 rather than the end. This hack is no longer used. */
1243 my_friendly_assert (keep == 0 || keep == 1, 0);
1245 if (current_binding_level->keep == 1)
1248 /* Any uses of undefined labels, and any defined labels, now operate
1249 under constraints of next binding contour. */
1250 if (cfun && !functionbody)
1252 struct cp_binding_level *level_chain;
1253 level_chain = current_binding_level->level_chain;
1256 struct named_label_use_list *uses;
1257 struct named_label_list *labels;
1258 for (labels = named_labels; labels; labels = labels->next)
1259 if (labels->binding_level == current_binding_level)
1262 if (current_binding_level->is_try_scope)
1263 labels->in_try_scope = 1;
1264 if (current_binding_level->is_catch_scope)
1265 labels->in_catch_scope = 1;
1266 for (decl = labels->names_in_scope; decl;
1267 decl = TREE_CHAIN (decl))
1268 if (decl_jump_unsafe (decl))
1269 labels->bad_decls = tree_cons (NULL_TREE, decl,
1271 labels->binding_level = level_chain;
1272 labels->names_in_scope = level_chain->names;
1275 for (uses = named_label_uses; uses; uses = uses->next)
1276 if (uses->binding_level == current_binding_level)
1278 uses->binding_level = level_chain;
1279 uses->names_in_scope = level_chain->names;
1284 /* Get the decls in the order they were written.
1285 Usually current_binding_level->names is in reverse order.
1286 But parameter decls were previously put in forward order. */
1289 current_binding_level->names
1290 = decls = nreverse (current_binding_level->names);
1292 decls = current_binding_level->names;
1294 /* Output any nested inline functions within this block
1295 if they weren't already output. */
1296 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1297 if (TREE_CODE (decl) == FUNCTION_DECL
1298 && ! TREE_ASM_WRITTEN (decl)
1299 && DECL_INITIAL (decl) != NULL_TREE
1300 && TREE_ADDRESSABLE (decl)
1301 && decl_function_context (decl) == current_function_decl)
1303 /* If this decl was copied from a file-scope decl
1304 on account of a block-scope extern decl,
1305 propagate TREE_ADDRESSABLE to the file-scope decl. */
1306 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1307 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1310 push_function_context ();
1311 output_inline_function (decl);
1312 pop_function_context ();
1316 /* When not in function-at-a-time mode, expand_end_bindings will
1317 warn about unused variables. But, in function-at-a-time mode
1318 expand_end_bindings is not passed the list of variables in the
1319 current scope, and therefore no warning is emitted. So, we
1320 explicitly warn here. */
1321 if (!processing_template_decl)
1322 warn_about_unused_variables (getdecls ());
1324 /* If there were any declarations or structure tags in that level,
1325 or if this level is a function body,
1326 create a BLOCK to record them for the life of this function. */
1328 if (keep == 1 || functionbody)
1329 block = make_node (BLOCK);
1330 if (block != NULL_TREE)
1332 BLOCK_VARS (block) = decls;
1333 BLOCK_SUBBLOCKS (block) = subblocks;
1336 /* In each subblock, record that this is its superior. */
1338 for (link = subblocks; link; link = TREE_CHAIN (link))
1339 BLOCK_SUPERCONTEXT (link) = block;
1341 /* We still support the old for-scope rules, whereby the variables
1342 in a for-init statement were in scope after the for-statement
1343 ended. We only use the new rules if flag_new_for_scope is
1346 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1348 /* Remove declarations for all the DECLs in this level. */
1349 for (link = decls; link; link = TREE_CHAIN (link))
1351 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1352 && DECL_NAME (link))
1354 cxx_binding *outer_binding
1355 = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1359 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1361 ns_binding = NULL_TREE;
1364 && (BINDING_SCOPE (outer_binding)
1365 == current_binding_level->level_chain))
1366 /* We have something like:
1371 and we are leaving the `for' scope. There's no reason to
1372 keep the binding of the inner `i' in this case. */
1373 pop_binding (DECL_NAME (link), link);
1374 else if ((outer_binding
1375 && (TREE_CODE (BINDING_VALUE (outer_binding))
1378 && TREE_CODE (ns_binding) == TYPE_DECL))
1379 /* Here, we have something like:
1387 We must pop the for-scope binding so we know what's a
1388 type and what isn't. */
1389 pop_binding (DECL_NAME (link), link);
1392 /* Mark this VAR_DECL as dead so that we can tell we left it
1393 there only for backward compatibility. */
1394 DECL_DEAD_FOR_LOCAL (link) = 1;
1396 /* Keep track of what should of have happenned when we
1397 popped the binding. */
1398 if (outer_binding && BINDING_VALUE (outer_binding))
1399 DECL_SHADOWED_FOR_VAR (link)
1400 = BINDING_VALUE (outer_binding);
1402 /* Add it to the list of dead variables in the next
1403 outermost binding to that we can remove these when we
1404 leave that binding. */
1405 current_binding_level->level_chain->dead_vars_from_for
1406 = tree_cons (NULL_TREE, link,
1407 current_binding_level->level_chain->
1408 dead_vars_from_for);
1410 /* Although we don't pop the cxx_binding, we do clear
1411 its BINDING_SCOPE since the level is going away now. */
1412 BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1417 /* Remove the binding. */
1419 if (TREE_CODE (decl) == TREE_LIST)
1420 decl = TREE_VALUE (decl);
1422 pop_binding (DECL_NAME (decl), decl);
1423 else if (TREE_CODE (decl) == OVERLOAD)
1424 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1430 /* Remove declarations for any `for' variables from inner scopes
1431 that we kept around. */
1432 for (link = current_binding_level->dead_vars_from_for;
1433 link; link = TREE_CHAIN (link))
1434 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1436 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1437 for (link = current_binding_level->type_shadowed;
1438 link; link = TREE_CHAIN (link))
1439 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1441 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1442 for (link = current_binding_level->shadowed_labels;
1444 link = TREE_CHAIN (link))
1445 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1447 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1448 list if a `using' declaration put them there. The debugging
1449 back-ends won't understand OVERLOAD, so we remove them here.
1450 Because the BLOCK_VARS are (temporarily) shared with
1451 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1452 popped all the bindings. */
1457 for (d = &BLOCK_VARS (block); *d; )
1459 if (TREE_CODE (*d) == TREE_LIST)
1460 *d = TREE_CHAIN (*d);
1462 d = &TREE_CHAIN (*d);
1466 /* If the level being exited is the top level of a function,
1467 check over all the labels. */
1470 /* Since this is the top level block of a function, the vars are
1471 the function's parameters. Don't leave them in the BLOCK
1472 because they are found in the FUNCTION_DECL instead. */
1473 BLOCK_VARS (block) = 0;
1477 tmp = current_binding_level->keep;
1479 pop_binding_level ();
1481 DECL_INITIAL (current_function_decl) = block;
1483 current_binding_level->blocks
1484 = chainon (current_binding_level->blocks, block);
1486 /* If we did not make a block for the level just exited,
1487 any blocks made for inner levels
1488 (since they cannot be recorded as subblocks in that level)
1489 must be carried forward so they will later become subblocks
1490 of something else. */
1492 current_binding_level->blocks
1493 = chainon (current_binding_level->blocks, subblocks);
1495 /* Each and every BLOCK node created here in `poplevel' is important
1496 (e.g. for proper debugging information) so if we created one
1497 earlier, mark it as "used". */
1499 TREE_USED (block) = 1;
1501 /* Take care of compiler's internal binding structures. */
1507 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1510 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1511 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1514 block = poplevel (keep, reverse, functionbody);
1517 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1520 /* Delete the node BLOCK from the current binding level.
1521 This is used for the block inside a stmt expr ({...})
1522 so that the block can be reinserted where appropriate. */
1525 delete_block (tree block)
1528 if (current_binding_level->blocks == block)
1529 current_binding_level->blocks = TREE_CHAIN (block);
1530 for (t = current_binding_level->blocks; t;)
1532 if (TREE_CHAIN (t) == block)
1533 TREE_CHAIN (t) = TREE_CHAIN (block);
1537 TREE_CHAIN (block) = NULL_TREE;
1538 /* Clear TREE_USED which is always set by poplevel.
1539 The flag is set again if insert_block is called. */
1540 TREE_USED (block) = 0;
1543 /* Insert BLOCK at the end of the list of subblocks of the
1544 current binding level. This is used when a BIND_EXPR is expanded,
1545 to handle the BLOCK node inside the BIND_EXPR. */
1548 insert_block (tree block)
1550 TREE_USED (block) = 1;
1551 current_binding_level->blocks
1552 = chainon (current_binding_level->blocks, block);
1555 /* Set the BLOCK node for the innermost scope
1556 (the one we are currently in). */
1559 set_block (tree block ATTRIBUTE_UNUSED )
1561 /* The RTL expansion machinery requires us to provide this callback,
1562 but it is not applicable in function-at-a-time mode. */
1563 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1566 /* Do a pushlevel for class declarations. */
1569 pushlevel_class (void)
1571 register struct cp_binding_level *newlevel;
1573 /* Reuse or create a struct for this binding level. */
1574 #if defined(DEBUG_BINDING_LEVELS)
1576 #else /* !defined(DEBUG_BINDING_LEVELS) */
1577 if (free_binding_level)
1578 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1580 newlevel = free_binding_level;
1581 free_binding_level = free_binding_level->level_chain;
1584 newlevel = make_binding_level ();
1586 #if defined(DEBUG_BINDING_LEVELS)
1588 #endif /* defined(DEBUG_BINDING_LEVELS) */
1590 push_binding_level (newlevel, 0, 0);
1592 class_binding_level = current_binding_level;
1593 class_binding_level->parm_flag = 2;
1594 class_binding_level->this_class = current_class_type;
1597 /* ...and a poplevel for class declarations. */
1600 poplevel_class (void)
1602 register struct cp_binding_level *level = class_binding_level;
1605 timevar_push (TV_NAME_LOOKUP);
1606 my_friendly_assert (level != 0, 354);
1608 /* If we're leaving a toplevel class, don't bother to do the setting
1609 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1610 shouldn't even be used when current_class_type isn't set, and second,
1611 if we don't touch it here, we're able to use the cache effect if the
1612 next time we're entering a class scope, it is the same class. */
1613 if (current_class_depth != 1)
1615 struct cp_binding_level* b;
1617 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1618 for (shadowed = level->class_shadowed;
1620 shadowed = TREE_CHAIN (shadowed))
1621 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1623 /* Find the next enclosing class, and recreate
1624 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1625 b = level->level_chain;
1626 while (b && b->parm_flag != 2)
1630 for (shadowed = b->class_shadowed;
1632 shadowed = TREE_CHAIN (shadowed))
1634 cxx_binding *binding;
1636 binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1637 while (binding && BINDING_SCOPE (binding) != b)
1638 binding = binding->previous;
1641 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1642 = BINDING_VALUE (binding);
1646 /* Remember to save what IDENTIFIER's were bound in this scope so we
1647 can recover from cache misses. */
1649 previous_class_type = current_class_type;
1650 previous_class_values = class_binding_level->class_shadowed;
1652 for (shadowed = level->type_shadowed;
1654 shadowed = TREE_CHAIN (shadowed))
1655 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1657 /* Remove the bindings for all of the class-level declarations. */
1658 for (shadowed = level->class_shadowed;
1660 shadowed = TREE_CHAIN (shadowed))
1661 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1663 /* Now, pop out of the binding level which we created up in the
1664 `pushlevel_class' routine. */
1665 #if defined(DEBUG_BINDING_LEVELS)
1667 #endif /* defined(DEBUG_BINDING_LEVELS) */
1669 pop_binding_level ();
1670 timevar_pop (TV_NAME_LOOKUP);
1673 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1674 for any names in enclosing classes. */
1677 clear_identifier_class_values (void)
1681 if (!class_binding_level)
1684 for (t = class_binding_level->class_shadowed;
1687 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1690 /* Returns nonzero if T is a virtual function table. */
1693 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1695 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1698 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1702 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1704 return (TREE_CODE (t) == TYPE_DECL
1705 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1706 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1709 /* Return the declarations that are members of the namespace NS. */
1712 cp_namespace_decls (tree ns)
1714 return NAMESPACE_LEVEL (ns)->names;
1717 struct walk_globals_data {
1718 walk_globals_pred p;
1723 /* Walk the vtable declarations in NAMESPACE. Whenever one is found
1724 for which P returns nonzero, call F with its address. If any call
1725 to F returns a nonzero value, return a nonzero value. */
1728 walk_vtables_r (tree namespace, void* data)
1730 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1731 walk_globals_fn f = wgd->f;
1732 void *d = wgd->data;
1733 tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1736 for (; decl ; decl = TREE_CHAIN (decl))
1737 result |= (*f) (&decl, d);
1742 /* Walk the vtable declarations. Whenever one is found for which P
1743 returns nonzero, call F with its address. If any call to F
1744 returns a nonzero value, return a nonzero value. */
1746 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1748 struct walk_globals_data wgd;
1753 return walk_namespaces (walk_vtables_r, &wgd);
1756 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1757 itself, calling F for each. The DATA is passed to F as well. */
1760 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1763 tree current = NAMESPACE_LEVEL (namespace)->namespaces;
1765 result |= (*f) (namespace, data);
1767 for (; current; current = TREE_CHAIN (current))
1768 result |= walk_namespaces_r (current, f, data);
1773 /* Walk all the namespaces, calling F for each. The DATA is passed to
1777 walk_namespaces (walk_namespaces_fn f, void* data)
1779 return walk_namespaces_r (global_namespace, f, data);
1782 /* Walk the global declarations in NAMESPACE. Whenever one is found
1783 for which P returns nonzero, call F with its address. If any call
1784 to F returns a nonzero value, return a nonzero value. */
1787 walk_globals_r (tree namespace, void* data)
1789 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1790 walk_globals_pred p = wgd->p;
1791 walk_globals_fn f = wgd->f;
1792 void *d = wgd->data;
1796 t = &NAMESPACE_LEVEL (namespace)->names;
1803 result |= (*f) (t, d);
1805 /* If F changed *T, then *T still points at the next item to
1808 t = &TREE_CHAIN (*t);
1814 /* Walk the global declarations. Whenever one is found for which P
1815 returns true, call F with its address. If any call to F
1816 returns true, return true. */
1819 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1821 struct walk_globals_data wgd;
1826 return walk_namespaces (walk_globals_r, &wgd);
1829 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1830 DATA is non-NULL, this is the last time we will call
1831 wrapup_global_declarations for this NAMESPACE. */
1834 wrapup_globals_for_namespace (tree namespace, void* data)
1836 struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1837 varray_type statics = level->static_decls;
1838 tree *vec = &VARRAY_TREE (statics, 0);
1839 int len = VARRAY_ACTIVE_SIZE (statics);
1840 int last_time = (data != 0);
1844 check_global_declarations (vec, len);
1848 /* Write out any globals that need to be output. */
1849 return wrapup_global_declarations (vec, len);
1853 /* For debugging. */
1854 static int no_print_functions = 0;
1855 static int no_print_builtins = 0;
1858 print_binding_level (struct cp_binding_level* lvl)
1862 fprintf (stderr, " blocks=");
1863 fprintf (stderr, HOST_PTR_PRINTF, (void *) lvl->blocks);
1864 if (lvl->tag_transparent)
1865 fprintf (stderr, " tag-transparent");
1866 if (lvl->more_cleanups_ok)
1867 fprintf (stderr, " more-cleanups-ok");
1868 if (lvl->have_cleanups)
1869 fprintf (stderr, " have-cleanups");
1870 fprintf (stderr, "\n");
1873 fprintf (stderr, " names:\t");
1874 /* We can probably fit 3 names to a line? */
1875 for (t = lvl->names; t; t = TREE_CHAIN (t))
1877 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1879 if (no_print_builtins
1880 && (TREE_CODE (t) == TYPE_DECL)
1881 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1884 /* Function decls tend to have longer names. */
1885 if (TREE_CODE (t) == FUNCTION_DECL)
1892 fprintf (stderr, "\n\t");
1895 print_node_brief (stderr, "", t, 0);
1896 if (t == error_mark_node)
1900 fprintf (stderr, "\n");
1904 fprintf (stderr, " tags:\t");
1906 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1908 if (TREE_PURPOSE (t) == NULL_TREE)
1910 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1917 fprintf (stderr, "\n\t");
1920 if (TREE_PURPOSE (t) == NULL_TREE)
1922 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1923 fprintf (stderr, ">");
1925 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1926 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1929 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1930 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1931 fprintf (stderr, ">");
1935 fprintf (stderr, "\n");
1937 if (lvl->class_shadowed)
1939 fprintf (stderr, " class-shadowed:");
1940 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1942 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1944 fprintf (stderr, "\n");
1946 if (lvl->type_shadowed)
1948 fprintf (stderr, " type-shadowed:");
1949 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1951 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1953 fprintf (stderr, "\n");
1958 print_other_binding_stack (struct cp_binding_level *stack)
1960 struct cp_binding_level *level;
1961 for (level = stack; level != global_binding_level; level = level->level_chain)
1963 fprintf (stderr, "binding level ");
1964 fprintf (stderr, HOST_PTR_PRINTF, (void *) level);
1965 fprintf (stderr, "\n");
1966 print_binding_level (level);
1971 print_binding_stack (void)
1973 struct cp_binding_level *b;
1974 fprintf (stderr, "current_binding_level=");
1975 fprintf (stderr, HOST_PTR_PRINTF, (void *) current_binding_level);
1976 fprintf (stderr, "\nclass_binding_level=");
1977 fprintf (stderr, HOST_PTR_PRINTF, (void *) class_binding_level);
1978 fprintf (stderr, "\nglobal_binding_level=");
1979 fprintf (stderr, HOST_PTR_PRINTF, (void *) global_binding_level);
1980 fprintf (stderr, "\n");
1981 if (class_binding_level)
1983 for (b = class_binding_level; b; b = b->level_chain)
1984 if (b == current_binding_level)
1987 b = class_binding_level;
1989 b = current_binding_level;
1992 b = current_binding_level;
1993 print_other_binding_stack (b);
1994 fprintf (stderr, "global:\n");
1995 print_binding_level (global_binding_level);
1998 /* Namespace binding access routines: The namespace_bindings field of
1999 the identifier is polymorphic, with three possible values:
2000 NULL_TREE, a list of "cxx_binding"s. */
2002 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2003 select a name that is unique to this compilation unit. */
2006 push_namespace (tree name)
2010 int implicit_use = 0;
2013 timevar_push (TV_NAME_LOOKUP);
2015 if (!global_namespace)
2017 /* This must be ::. */
2018 my_friendly_assert (name == get_identifier ("::"), 377);
2023 /* The name of anonymous namespace is unique for the translation
2025 if (!anonymous_namespace_name)
2026 anonymous_namespace_name = get_file_function_name ('N');
2027 name = anonymous_namespace_name;
2028 d = IDENTIFIER_NAMESPACE_VALUE (name);
2030 /* Reopening anonymous namespace. */
2036 /* Check whether this is an extended namespace definition. */
2037 d = IDENTIFIER_NAMESPACE_VALUE (name);
2038 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2041 if (DECL_NAMESPACE_ALIAS (d))
2043 error ("namespace alias `%D' not allowed here, assuming `%D'",
2044 d, DECL_NAMESPACE_ALIAS (d));
2045 d = DECL_NAMESPACE_ALIAS (d);
2052 /* Make a new namespace, binding the name to it. */
2053 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2054 /* The global namespace is not pushed, and the global binding
2055 level is set elsewhere. */
2058 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2061 declare_namespace_level ();
2062 NAMESPACE_LEVEL (d) = current_binding_level;
2063 VARRAY_TREE_INIT (current_binding_level->static_decls,
2064 name != std_identifier ? 10 : 200,
2065 "Static declarations");
2069 resume_binding_level (NAMESPACE_LEVEL (d));
2072 do_using_directive (d);
2073 /* Enter the name space. */
2074 current_namespace = d;
2076 timevar_pop (TV_NAME_LOOKUP);
2079 /* Pop from the scope of the current namespace. */
2082 pop_namespace (void)
2084 my_friendly_assert (current_namespace != global_namespace, 20010801);
2085 current_namespace = CP_DECL_CONTEXT (current_namespace);
2086 /* The binding level is not popped, as it might be re-opened later. */
2087 suspend_binding_level ();
2090 /* Push into the scope of the namespace NS, even if it is deeply
2091 nested within another namespace. */
2094 push_nested_namespace (tree ns)
2096 if (ns == global_namespace)
2097 push_to_top_level ();
2100 push_nested_namespace (CP_DECL_CONTEXT (ns));
2101 push_namespace (DECL_NAME (ns));
2105 /* Pop back from the scope of the namespace NS, which was previously
2106 entered with push_nested_namespace. */
2109 pop_nested_namespace (tree ns)
2111 timevar_push (TV_NAME_LOOKUP);
2112 while (ns != global_namespace)
2115 ns = CP_DECL_CONTEXT (ns);
2118 pop_from_top_level ();
2119 timevar_pop (TV_NAME_LOOKUP);
2123 /* Allocate storage for saving a C++ binding. */
2124 #define cxx_saved_binding_make() \
2125 (ggc_alloc (sizeof (cxx_saved_binding)))
2127 struct cxx_saved_binding GTY(())
2129 /* Link that chains saved C++ bindings for a given name into a stack. */
2130 cxx_saved_binding *previous;
2131 /* The name of the current binding. */
2133 /* The binding we're saving. */
2134 cxx_binding *binding;
2136 tree real_type_value;
2139 /* Subroutines for reverting temporarily to top-level for instantiation
2140 of templates and such. We actually need to clear out the class- and
2141 local-value slots of all identifiers, so that only the global values
2142 are at all visible. Simply setting current_binding_level to the global
2143 scope isn't enough, because more binding levels may be pushed. */
2144 struct saved_scope *scope_chain;
2146 static cxx_saved_binding *
2147 store_bindings (tree names, cxx_saved_binding *old_bindings)
2150 cxx_saved_binding *search_bindings = old_bindings;
2152 timevar_push (TV_NAME_LOOKUP);
2153 for (t = names; t; t = TREE_CHAIN (t))
2156 cxx_saved_binding *saved;
2157 cxx_saved_binding *t1;
2159 if (TREE_CODE (t) == TREE_LIST)
2160 id = TREE_PURPOSE (t);
2165 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2166 we have no IDENTIFIER_BINDING if we have left the class
2167 scope, but cached the class-level declarations. */
2168 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2171 for (t1 = search_bindings; t1; t1 = t1->previous)
2172 if (t1->identifier == id)
2175 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2176 saved = cxx_saved_binding_make ();
2177 saved->previous = old_bindings;
2178 saved->identifier = id;
2179 saved->binding = IDENTIFIER_BINDING (id);
2180 saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2181 saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2182 IDENTIFIER_BINDING (id) = NULL;
2183 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2184 old_bindings = saved;
2188 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2192 maybe_push_to_top_level (int pseudo)
2194 struct saved_scope *s;
2195 struct cp_binding_level *b;
2196 cxx_saved_binding *old_bindings;
2199 timevar_push (TV_NAME_LOOKUP);
2200 s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2202 b = scope_chain ? current_binding_level : 0;
2204 /* If we're in the middle of some function, save our state. */
2208 push_function_context_to (NULL_TREE);
2213 old_bindings = NULL;
2214 if (scope_chain && previous_class_type)
2215 old_bindings = store_bindings (previous_class_values, old_bindings);
2217 /* Have to include global_binding_level, because class-level decls
2218 aren't listed anywhere useful. */
2219 for (; b; b = b->level_chain)
2223 /* Template IDs are inserted into the global level. If they were
2224 inserted into namespace level, finish_file wouldn't find them
2225 when doing pending instantiations. Therefore, don't stop at
2226 namespace level, but continue until :: . */
2227 if (b == global_binding_level || (pseudo && b->template_parms_p))
2230 old_bindings = store_bindings (b->names, old_bindings);
2231 /* We also need to check class_shadowed to save class-level type
2232 bindings, since pushclass doesn't fill in b->names. */
2233 if (b->parm_flag == 2)
2234 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2236 /* Unwind type-value slots back to top level. */
2237 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2238 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2240 s->prev = scope_chain;
2241 s->old_bindings = old_bindings;
2243 s->need_pop_function_context = need_pop;
2244 s->function_decl = current_function_decl;
2245 s->last_parms = last_function_parms;
2246 s->check_access = flag_access_control;
2249 current_function_decl = NULL_TREE;
2250 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2251 current_lang_name = lang_name_cplusplus;
2252 current_namespace = global_namespace;
2253 timevar_pop (TV_NAME_LOOKUP);
2257 push_to_top_level (void)
2259 maybe_push_to_top_level (0);
2263 pop_from_top_level (void)
2265 struct saved_scope *s = scope_chain;
2266 cxx_saved_binding *saved;
2268 timevar_push (TV_NAME_LOOKUP);
2269 /* Clear out class-level bindings cache. */
2270 if (previous_class_type)
2271 invalidate_class_lookup_cache ();
2273 current_lang_base = 0;
2275 scope_chain = s->prev;
2276 for (saved = s->old_bindings; saved; saved = saved->previous)
2278 tree id = saved->identifier;
2280 IDENTIFIER_BINDING (id) = saved->binding;
2281 IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2282 SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2285 /* If we were in the middle of compiling a function, restore our
2287 if (s->need_pop_function_context)
2288 pop_function_context_from (NULL_TREE);
2289 current_function_decl = s->function_decl;
2290 last_function_parms = s->last_parms;
2291 timevar_pop (TV_NAME_LOOKUP);
2294 /* Push a definition of struct, union or enum tag "name".
2295 into binding_level "b". "type" should be the type node,
2296 We assume that the tag "name" is not already defined.
2298 Note that the definition may really be just a forward reference.
2299 In that case, the TYPE_SIZE will be a NULL_TREE.
2301 C++ gratuitously puts all these tags in the name space. */
2303 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2304 record the shadowed value for this binding contour. TYPE is
2305 the type that ID maps to. */
2308 set_identifier_type_value_with_scope (tree id,
2310 struct cp_binding_level* b)
2312 if (!b->namespace_p)
2314 /* Shadow the marker, not the real thing, so that the marker
2315 gets restored later. */
2316 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2318 = tree_cons (id, old_type_value, b->type_shadowed);
2322 cxx_binding *binding =
2323 binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2324 BINDING_TYPE (binding) = type;
2325 /* Store marker instead of real type. */
2326 type = global_type_node;
2328 SET_IDENTIFIER_TYPE_VALUE (id, type);
2331 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2334 set_identifier_type_value (tree id, tree type)
2336 set_identifier_type_value_with_scope (id, type, current_binding_level);
2339 /* Return the type associated with id. */
2342 identifier_type_value (tree id)
2344 timevar_push (TV_NAME_LOOKUP);
2345 /* There is no type with that name, anywhere. */
2346 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2347 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2348 /* This is not the type marker, but the real thing. */
2349 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2350 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2351 /* Have to search for it. It must be on the global level, now.
2352 Ask lookup_name not to return non-types. */
2353 id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2355 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2356 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2359 /* Pop off extraneous binding levels left over due to syntax errors.
2361 We don't pop past namespaces, as they might be valid. */
2364 pop_everything (void)
2366 #ifdef DEBUG_BINDING_LEVELS
2367 fprintf (stderr, "XXX entering pop_everything ()\n");
2369 while (!toplevel_bindings_p ())
2371 if (current_binding_level->parm_flag == 2)
2372 pop_nested_class ();
2376 #ifdef DEBUG_BINDING_LEVELS
2377 fprintf (stderr, "XXX leaving pop_everything ()\n");
2381 /* The type TYPE is being declared. If it is a class template, or a
2382 specialization of a class template, do any processing required and
2383 perform error-checking. If IS_FRIEND is nonzero, this TYPE is
2384 being declared a friend. B is the binding level at which this TYPE
2387 Returns the TYPE_DECL for TYPE, which may have been altered by this
2391 maybe_process_template_type_declaration (tree type,
2393 struct cp_binding_level* b)
2395 tree decl = TYPE_NAME (type);
2397 if (processing_template_parmlist)
2398 /* You can't declare a new template type in a template parameter
2399 list. But, you can declare a non-template type:
2401 template <class A*> struct S;
2403 is a forward-declaration of `A'. */
2407 maybe_check_template_type (type);
2409 my_friendly_assert (IS_AGGR_TYPE (type)
2410 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2413 if (processing_template_decl)
2415 /* This may change after the call to
2416 push_template_decl_real, but we want the original value. */
2417 tree name = DECL_NAME (decl);
2419 decl = push_template_decl_real (decl, globalize);
2420 /* If the current binding level is the binding level for the
2421 template parameters (see the comment in
2422 begin_template_parm_list) and the enclosing level is a class
2423 scope, and we're not looking at a friend, push the
2424 declaration of the member class into the class scope. In the
2425 friend case, push_template_decl will already have put the
2426 friend into global scope, if appropriate. */
2427 if (TREE_CODE (type) != ENUMERAL_TYPE
2428 && !globalize && b->template_parms_p
2429 && b->level_chain->parm_flag == 2)
2431 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2432 /* Put this tag on the list of tags for the class, since
2433 that won't happen below because B is not the class
2434 binding level, but is instead the pseudo-global level. */
2435 b->level_chain->tags =
2436 tree_cons (name, type, b->level_chain->tags);
2437 if (!COMPLETE_TYPE_P (current_class_type))
2439 maybe_add_class_template_decl_list (current_class_type,
2440 type, /*friend_p=*/0);
2441 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2450 /* In C++, you don't have to write `struct S' to refer to `S'; you
2451 can just use `S'. We accomplish this by creating a TYPE_DECL as
2452 if the user had written `typedef struct S S'. Create and return
2453 the TYPE_DECL for TYPE. */
2456 create_implicit_typedef (tree name, tree type)
2460 decl = build_decl (TYPE_DECL, name, type);
2461 DECL_ARTIFICIAL (decl) = 1;
2462 /* There are other implicit type declarations, like the one *within*
2463 a class that allows you to write `S::S'. We must distinguish
2465 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2466 TYPE_NAME (type) = decl;
2471 /* Remember a local name for name-mangling purposes. */
2474 push_local_name (tree decl)
2479 timevar_push (TV_NAME_LOOKUP);
2481 VARRAY_TREE_INIT (local_names, 8, "local_names");
2483 name = DECL_NAME (decl);
2485 nelts = VARRAY_ACTIVE_SIZE (local_names);
2486 for (i = 0; i < nelts; i++)
2488 t = VARRAY_TREE (local_names, i);
2489 if (DECL_NAME (t) == name)
2491 if (!DECL_LANG_SPECIFIC (decl))
2492 retrofit_lang_decl (decl);
2493 DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2494 if (DECL_LANG_SPECIFIC (t))
2495 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2497 DECL_DISCRIMINATOR (decl) = 1;
2499 VARRAY_TREE (local_names, i) = decl;
2500 timevar_pop (TV_NAME_LOOKUP);
2505 VARRAY_PUSH_TREE (local_names, decl);
2506 timevar_pop (TV_NAME_LOOKUP);
2509 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2510 Normally put it into the inner-most non-tag-transparent scope,
2511 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2512 The latter is needed for implicit declarations. */
2515 pushtag (tree name, tree type, int globalize)
2517 register struct cp_binding_level *b;
2519 timevar_push (TV_NAME_LOOKUP);
2520 b = current_binding_level;
2521 while (b->tag_transparent
2522 || (b->parm_flag == 2
2524 /* We may be defining a new type in the initializer
2525 of a static member variable. We allow this when
2526 not pedantic, and it is particularly useful for
2527 type punning via an anonymous union. */
2528 || COMPLETE_TYPE_P (b->this_class))))
2531 b->tags = tree_cons (name, type, b->tags);
2535 /* Do C++ gratuitous typedefing. */
2536 if (IDENTIFIER_TYPE_VALUE (name) != type)
2538 register tree d = NULL_TREE;
2540 tree context = TYPE_CONTEXT (type);
2544 tree cs = current_scope ();
2548 else if (cs != NULL_TREE && TYPE_P (cs))
2549 /* When declaring a friend class of a local class, we want
2550 to inject the newly named class into the scope
2551 containing the local class, not the namespace scope. */
2552 context = decl_function_context (get_type_decl (cs));
2555 context = current_namespace;
2557 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2558 || b->parm_flag == 2)
2561 if (current_lang_name == lang_name_java)
2562 TYPE_FOR_JAVA (type) = 1;
2564 d = create_implicit_typedef (name, type);
2565 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2567 set_identifier_type_value_with_scope (name, type, b);
2569 d = maybe_process_template_type_declaration (type,
2572 if (b->parm_flag == 2)
2574 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2575 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2576 class. But if it's a member template class, we
2577 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2579 finish_member_declaration (d);
2581 pushdecl_class_level (d);
2584 d = pushdecl_with_scope (d, b);
2586 /* FIXME what if it gets a name from typedef? */
2587 if (ANON_AGGRNAME_P (name))
2588 DECL_IGNORED_P (d) = 1;
2590 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2592 /* If this is a local class, keep track of it. We need this
2593 information for name-mangling, and so that it is possible to find
2594 all function definitions in a translation unit in a convenient
2595 way. (It's otherwise tricky to find a member function definition
2596 it's only pointed to from within a local class.) */
2597 if (TYPE_CONTEXT (type)
2598 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2599 && !processing_template_decl)
2600 VARRAY_PUSH_TREE (local_classes, type);
2602 if (b->parm_flag == 2)
2604 if (!COMPLETE_TYPE_P (current_class_type))
2606 maybe_add_class_template_decl_list (current_class_type,
2607 type, /*friend_p=*/0);
2608 CLASSTYPE_TAGS (current_class_type) = b->tags;
2613 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2614 /* Use the canonical TYPE_DECL for this node. */
2615 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2618 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2619 will be the tagged type we just added to the current
2620 binding level. This fake NULL-named TYPE_DECL node helps
2621 dwarfout.c to know when it needs to output a
2622 representation of a tagged type, and it also gives us a
2623 convenient place to record the "scope start" address for
2626 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2627 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2629 timevar_pop (TV_NAME_LOOKUP);
2632 /* Counter used to create anonymous type names. */
2634 static GTY(()) int anon_cnt;
2636 /* Return an IDENTIFIER which can be used as a name for
2637 anonymous structs and unions. */
2640 make_anon_name (void)
2644 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2645 return get_identifier (buf);
2648 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2649 This keeps dbxout from getting confused. */
2652 clear_anon_tags (void)
2654 register struct cp_binding_level *b;
2656 static int last_cnt = 0;
2658 /* Fast out if no new anon names were declared. */
2659 if (last_cnt == anon_cnt)
2662 b = current_binding_level;
2663 while (b->tag_transparent)
2668 /* A NULL purpose means we have already processed all tags
2669 from here to the end of the list. */
2670 if (TREE_PURPOSE (tags) == NULL_TREE)
2672 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2673 TREE_PURPOSE (tags) = NULL_TREE;
2674 tags = TREE_CHAIN (tags);
2676 last_cnt = anon_cnt;
2679 /* Subroutine of duplicate_decls: return truthvalue of whether
2680 or not types of these decls match.
2682 For C++, we must compare the parameter list so that `int' can match
2683 `int&' in a parameter position, but `int&' is not confused with
2687 decls_match (tree newdecl, tree olddecl)
2691 if (newdecl == olddecl)
2694 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2695 /* If the two DECLs are not even the same kind of thing, we're not
2696 interested in their types. */
2699 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2701 tree f1 = TREE_TYPE (newdecl);
2702 tree f2 = TREE_TYPE (olddecl);
2703 tree p1 = TYPE_ARG_TYPES (f1);
2704 tree p2 = TYPE_ARG_TYPES (f2);
2706 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2707 && ! (DECL_EXTERN_C_P (newdecl)
2708 && DECL_EXTERN_C_P (olddecl)))
2711 if (TREE_CODE (f1) != TREE_CODE (f2))
2714 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2716 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2717 && (DECL_BUILT_IN (olddecl)
2718 #ifndef NO_IMPLICIT_EXTERN_C
2719 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2720 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2724 types_match = self_promoting_args_p (p1);
2725 if (p1 == void_list_node)
2726 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2728 #ifndef NO_IMPLICIT_EXTERN_C
2729 else if (p1 == NULL_TREE
2730 && (DECL_EXTERN_C_P (olddecl)
2731 && DECL_IN_SYSTEM_HEADER (olddecl)
2732 && !DECL_CLASS_SCOPE_P (olddecl))
2733 && (DECL_EXTERN_C_P (newdecl)
2734 && DECL_IN_SYSTEM_HEADER (newdecl)
2735 && !DECL_CLASS_SCOPE_P (newdecl)))
2737 types_match = self_promoting_args_p (p2);
2738 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2742 types_match = compparms (p1, p2);
2747 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2749 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2750 DECL_TEMPLATE_PARMS (olddecl)))
2753 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2754 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2757 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2760 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2761 DECL_TEMPLATE_RESULT (newdecl));
2765 if (TREE_TYPE (newdecl) == error_mark_node)
2766 types_match = TREE_TYPE (olddecl) == error_mark_node;
2767 else if (TREE_TYPE (olddecl) == NULL_TREE)
2768 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2769 else if (TREE_TYPE (newdecl) == NULL_TREE)
2772 types_match = comptypes (TREE_TYPE (newdecl),
2773 TREE_TYPE (olddecl),
2774 COMPARE_REDECLARATION);
2780 /* If NEWDECL is `static' and an `extern' was seen previously,
2781 warn about it. OLDDECL is the previous declaration.
2783 Note that this does not apply to the C++ case of declaring
2784 a variable `extern const' and then later `const'.
2786 Don't complain about built-in functions, since they are beyond
2787 the user's control. */
2790 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2792 static const char *const explicit_extern_static_warning
2793 = "`%D' was declared `extern' and later `static'";
2794 static const char *const implicit_extern_static_warning
2795 = "`%D' was declared implicitly `extern' and later `static'";
2799 if (TREE_CODE (newdecl) == TYPE_DECL
2800 || TREE_CODE (newdecl) == TEMPLATE_DECL
2801 || TREE_CODE (newdecl) == CONST_DECL)
2804 /* Don't get confused by static member functions; that's a different
2806 if (TREE_CODE (newdecl) == FUNCTION_DECL
2807 && DECL_STATIC_FUNCTION_P (newdecl))
2810 /* If the old declaration was `static', or the new one isn't, then
2811 then everything is OK. */
2812 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2815 /* It's OK to declare a builtin function as `static'. */
2816 if (TREE_CODE (olddecl) == FUNCTION_DECL
2817 && DECL_ARTIFICIAL (olddecl))
2820 name = DECL_ASSEMBLER_NAME (newdecl);
2821 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2822 ? implicit_extern_static_warning
2823 : explicit_extern_static_warning, newdecl);
2824 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2827 /* Handle when a new declaration NEWDECL has the same name as an old
2828 one OLDDECL in the same binding contour. Prints an error message
2831 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2832 Otherwise, return 0. */
2835 duplicate_decls (tree newdecl, tree olddecl)
2837 unsigned olddecl_uid = DECL_UID (olddecl);
2838 int olddecl_friend = 0, types_match = 0;
2839 int new_defines_function = 0;
2841 if (newdecl == olddecl)
2844 types_match = decls_match (newdecl, olddecl);
2846 /* If either the type of the new decl or the type of the old decl is an
2847 error_mark_node, then that implies that we have already issued an
2848 error (earlier) for some bogus type specification, and in that case,
2849 it is rather pointless to harass the user with yet more error message
2850 about the same declaration, so just pretend the types match here. */
2851 if (TREE_TYPE (newdecl) == error_mark_node
2852 || TREE_TYPE (olddecl) == error_mark_node)
2855 if (DECL_P (olddecl)
2856 && TREE_CODE (newdecl) == FUNCTION_DECL
2857 && TREE_CODE (olddecl) == FUNCTION_DECL
2858 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2860 if (DECL_DECLARED_INLINE_P (newdecl)
2861 && DECL_UNINLINABLE (newdecl)
2862 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2863 /* Already warned elsewhere. */;
2864 else if (DECL_DECLARED_INLINE_P (olddecl)
2865 && DECL_UNINLINABLE (olddecl)
2866 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2867 /* Already warned. */;
2868 else if (DECL_DECLARED_INLINE_P (newdecl)
2869 && DECL_UNINLINABLE (olddecl)
2870 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2872 warning ("%Hfunction '%D' redeclared as inline",
2873 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2874 warning ("%Hprevious declaration of '%D' with attribute noinline",
2875 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2877 else if (DECL_DECLARED_INLINE_P (olddecl)
2878 && DECL_UNINLINABLE (newdecl)
2879 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2881 warning ("%Hfunction '%D' redeclared with attribute noinline",
2882 &DECL_SOURCE_LOCATION (newdecl), newdecl);
2883 warning ("%Hprevious declaration of '%D' was inline",
2884 &DECL_SOURCE_LOCATION (olddecl), olddecl);
2888 /* Check for redeclaration and other discrepancies. */
2889 if (TREE_CODE (olddecl) == FUNCTION_DECL
2890 && DECL_ARTIFICIAL (olddecl))
2892 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2894 /* Avoid warnings redeclaring anticipated built-ins. */
2895 if (DECL_ANTICIPATED (olddecl))
2898 /* If you declare a built-in or predefined function name as static,
2899 the old definition is overridden, but optionally warn this was a
2900 bad choice of name. */
2901 if (! TREE_PUBLIC (newdecl))
2904 warning ("shadowing %s function `%#D'",
2905 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2907 /* Discard the old built-in function. */
2910 /* If the built-in is not ansi, then programs can override
2911 it even globally without an error. */
2912 else if (! DECL_BUILT_IN (olddecl))
2913 warning ("library function `%#D' redeclared as non-function `%#D'",
2917 error ("declaration of `%#D'", newdecl);
2918 error ("conflicts with built-in declaration `%#D'",
2923 else if (!types_match)
2925 /* Avoid warnings redeclaring anticipated built-ins. */
2926 if (DECL_ANTICIPATED (olddecl))
2927 ; /* Do nothing yet. */
2928 else if ((DECL_EXTERN_C_P (newdecl)
2929 && DECL_EXTERN_C_P (olddecl))
2930 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2931 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2933 /* A near match; override the builtin. */
2935 if (TREE_PUBLIC (newdecl))
2937 warning ("new declaration `%#D'", newdecl);
2938 warning ("ambiguates built-in declaration `%#D'",
2941 else if (warn_shadow)
2942 warning ("shadowing %s function `%#D'",
2943 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2947 /* Discard the old built-in function. */
2950 /* Replace the old RTL to avoid problems with inlining. */
2951 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2953 /* Even if the types match, prefer the new declarations type
2954 for anitipated built-ins, for exception lists, etc... */
2955 else if (DECL_ANTICIPATED (olddecl))
2956 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2958 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2960 /* If a builtin function is redeclared as `static', merge
2961 the declarations, but make the original one static. */
2962 DECL_THIS_STATIC (olddecl) = 1;
2963 TREE_PUBLIC (olddecl) = 0;
2965 /* Make the old declaration consistent with the new one so
2966 that all remnants of the builtin-ness of this function
2967 will be banished. */
2968 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2969 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2972 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2974 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2975 && TREE_CODE (newdecl) != TYPE_DECL
2976 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2977 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2978 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2979 && TREE_CODE (olddecl) != TYPE_DECL
2980 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2981 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2984 /* We do nothing special here, because C++ does such nasty
2985 things with TYPE_DECLs. Instead, just let the TYPE_DECL
2986 get shadowed, and know that if we need to find a TYPE_DECL
2987 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2988 slot of the identifier. */
2992 if ((TREE_CODE (newdecl) == FUNCTION_DECL
2993 && DECL_FUNCTION_TEMPLATE_P (olddecl))
2994 || (TREE_CODE (olddecl) == FUNCTION_DECL
2995 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2998 error ("`%#D' redeclared as different kind of symbol", newdecl);
2999 if (TREE_CODE (olddecl) == TREE_LIST)
3000 olddecl = TREE_VALUE (olddecl);
3001 cp_error_at ("previous declaration of `%#D'", olddecl);
3003 /* New decl is completely inconsistent with the old one =>
3004 tell caller to replace the old one. */
3008 else if (!types_match)
3010 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3011 /* These are certainly not duplicate declarations; they're
3012 from different scopes. */
3015 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3017 /* The name of a class template may not be declared to refer to
3018 any other template, class, function, object, namespace, value,
3019 or type in the same scope. */
3020 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3021 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3023 error ("declaration of template `%#D'", newdecl);
3024 cp_error_at ("conflicts with previous declaration `%#D'",
3027 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3028 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3029 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3030 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3031 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3032 DECL_TEMPLATE_PARMS (olddecl))
3033 /* Template functions can be disambiguated by
3035 && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3036 TREE_TYPE (TREE_TYPE (olddecl))))
3038 error ("new declaration `%#D'", newdecl);
3039 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3043 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3045 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3047 error ("declaration of C function `%#D' conflicts with",
3049 cp_error_at ("previous declaration `%#D' here", olddecl);
3051 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3052 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3054 error ("new declaration `%#D'", newdecl);
3055 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3061 /* Already complained about this, so don't do so again. */
3062 else if (current_class_type == NULL_TREE
3063 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3065 error ("conflicting types for `%#D'", newdecl);
3066 cp_error_at ("previous declaration as `%#D'", olddecl);
3069 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3070 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3071 && (!DECL_TEMPLATE_INFO (newdecl)
3072 || (DECL_TI_TEMPLATE (newdecl)
3073 != DECL_TI_TEMPLATE (olddecl))))
3074 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3075 && (!DECL_TEMPLATE_INFO (olddecl)
3076 || (DECL_TI_TEMPLATE (olddecl)
3077 != DECL_TI_TEMPLATE (newdecl))))))
3078 /* It's OK to have a template specialization and a non-template
3079 with the same type, or to have specializations of two
3080 different templates with the same type. Note that if one is a
3081 specialization, and the other is an instantiation of the same
3082 template, that we do not exit at this point. That situation
3083 can occur if we instantiate a template class, and then
3084 specialize one of its methods. This situation is valid, but
3085 the declarations must be merged in the usual way. */
3087 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3088 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3089 && !DECL_USE_TEMPLATE (newdecl))
3090 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3091 && !DECL_USE_TEMPLATE (olddecl))))
3092 /* One of the declarations is a template instantiation, and the
3093 other is not a template at all. That's OK. */
3095 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3096 && DECL_NAMESPACE_ALIAS (newdecl)
3097 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3098 /* Redeclaration of namespace alias, ignore it. */
3102 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3105 error (errmsg, newdecl);
3106 if (DECL_NAME (olddecl) != NULL_TREE)
3107 cp_error_at ((DECL_INITIAL (olddecl)
3108 && namespace_bindings_p ())
3109 ? "`%#D' previously defined here"
3110 : "`%#D' previously declared here", olddecl);
3113 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3114 && DECL_INITIAL (olddecl) != NULL_TREE
3115 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3116 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3118 /* Prototype decl follows defn w/o prototype. */
3119 cp_warning_at ("prototype for `%#D'", newdecl);
3120 cp_warning_at ("follows non-prototype definition here", olddecl);
3122 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3123 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3125 /* extern "C" int foo ();
3126 int foo () { bar (); }
3128 if (current_lang_depth () == 0)
3129 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3132 cp_error_at ("previous declaration of `%#D' with %L linkage",
3133 olddecl, DECL_LANGUAGE (olddecl));
3134 error ("conflicts with new declaration with %L linkage",
3135 DECL_LANGUAGE (newdecl));
3139 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3141 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3143 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3144 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3147 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3148 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3150 for (; t1 && t1 != void_list_node;
3151 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3152 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3154 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3157 pedwarn ("default argument given for parameter %d of `%#D'",
3159 cp_pedwarn_at ("after previous specification in `%#D'",
3164 error ("default argument given for parameter %d of `%#D'",
3166 cp_error_at ("after previous specification in `%#D'",
3171 if (DECL_DECLARED_INLINE_P (newdecl)
3172 && ! DECL_DECLARED_INLINE_P (olddecl)
3173 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3175 warning ("`%#D' was used before it was declared inline",
3177 cp_warning_at ("previous non-inline declaration here",
3183 /* Do not merge an implicit typedef with an explicit one. In:
3187 typedef class A A __attribute__ ((foo));
3189 the attribute should apply only to the typedef. */
3190 if (TREE_CODE (olddecl) == TYPE_DECL
3191 && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3192 || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3195 /* If new decl is `static' and an `extern' was seen previously,
3197 warn_extern_redeclared_static (newdecl, olddecl);
3199 /* We have committed to returning 1 at this point. */
3200 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3202 /* Now that functions must hold information normally held
3203 by field decls, there is extra work to do so that
3204 declaration information does not get destroyed during
3206 if (DECL_VINDEX (olddecl))
3207 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3208 if (DECL_CONTEXT (olddecl))
3209 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3210 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3211 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3212 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3213 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3214 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3215 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3216 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3217 SET_OVERLOADED_OPERATOR_CODE
3218 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3219 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3221 /* Optionally warn about more than one declaration for the same
3222 name, but don't warn about a function declaration followed by a
3224 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3225 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3226 /* Don't warn about extern decl followed by definition. */
3227 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3228 /* Don't warn about friends, let add_friend take care of it. */
3229 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3231 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3232 cp_warning_at ("previous declaration of `%D'", olddecl);
3236 /* Deal with C++: must preserve virtual function table size. */
3237 if (TREE_CODE (olddecl) == TYPE_DECL)
3239 register tree newtype = TREE_TYPE (newdecl);
3240 register tree oldtype = TREE_TYPE (olddecl);
3242 if (newtype != error_mark_node && oldtype != error_mark_node
3243 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3244 CLASSTYPE_FRIEND_CLASSES (newtype)
3245 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3247 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3250 /* Copy all the DECL_... slots specified in the new decl
3251 except for any that we copy here from the old type. */
3252 DECL_ATTRIBUTES (newdecl)
3253 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3255 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3257 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3258 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3259 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3260 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3262 /* If the new declaration is a definition, update the file and
3263 line information on the declaration. */
3264 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3265 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3267 DECL_SOURCE_LOCATION (olddecl)
3268 = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3269 = DECL_SOURCE_LOCATION (newdecl);
3277 /* Automatically handles default parameters. */
3278 tree oldtype = TREE_TYPE (olddecl);
3281 /* Merge the data types specified in the two decls. */
3282 newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3284 /* If merge_types produces a non-typedef type, just use the old type. */
3285 if (TREE_CODE (newdecl) == TYPE_DECL
3286 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3289 if (TREE_CODE (newdecl) == VAR_DECL)
3291 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3292 DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3295 /* Do this after calling `merge_types' so that default
3296 parameters don't confuse us. */
3297 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3298 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3299 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3301 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3302 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3303 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3304 TYPE_RAISES_EXCEPTIONS (oldtype));
3306 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3307 && DECL_SOURCE_LINE (olddecl) != 0
3309 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3310 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3312 error ("declaration of `%F' throws different exceptions",
3314 cp_error_at ("than previous declaration `%F'", olddecl);
3317 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3319 /* Lay the type out, unless already done. */
3320 if (! same_type_p (newtype, oldtype)
3321 && TREE_TYPE (newdecl) != error_mark_node
3322 && !(processing_template_decl && uses_template_parms (newdecl)))
3323 layout_type (TREE_TYPE (newdecl));
3325 if ((TREE_CODE (newdecl) == VAR_DECL
3326 || TREE_CODE (newdecl) == PARM_DECL
3327 || TREE_CODE (newdecl) == RESULT_DECL
3328 || TREE_CODE (newdecl) == FIELD_DECL
3329 || TREE_CODE (newdecl) == TYPE_DECL)
3330 && !(processing_template_decl && uses_template_parms (newdecl)))
3331 layout_decl (newdecl, 0);
3333 /* Merge the type qualifiers. */
3334 if (TREE_READONLY (newdecl))
3335 TREE_READONLY (olddecl) = 1;
3336 if (TREE_THIS_VOLATILE (newdecl))
3337 TREE_THIS_VOLATILE (olddecl) = 1;
3339 /* Merge the initialization information. */
3340 if (DECL_INITIAL (newdecl) == NULL_TREE
3341 && DECL_INITIAL (olddecl) != NULL_TREE)
3343 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3344 DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3345 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3346 && DECL_LANG_SPECIFIC (newdecl)
3347 && DECL_LANG_SPECIFIC (olddecl))
3348 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3351 /* Merge the section attribute.
3352 We want to issue an error if the sections conflict but that must be
3353 done later in decl_attributes since we are called before attributes
3355 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3356 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3358 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3360 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3361 |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3362 DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3363 TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3364 TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3365 TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3366 DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3367 DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3368 /* Keep the old RTL. */
3369 COPY_DECL_RTL (olddecl, newdecl);
3371 else if (TREE_CODE (newdecl) == VAR_DECL
3372 && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3374 /* Keep the old RTL. We cannot keep the old RTL if the old
3375 declaration was for an incomplete object and the new
3376 declaration is not since many attributes of the RTL will
3378 COPY_DECL_RTL (olddecl, newdecl);
3381 /* If cannot merge, then use the new type and qualifiers,
3382 and don't preserve the old rtl. */
3385 /* Clean out any memory we had of the old declaration. */
3386 tree oldstatic = value_member (olddecl, static_aggregates);
3388 TREE_VALUE (oldstatic) = error_mark_node;
3390 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3391 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3392 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3393 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3396 /* Merge the storage class information. */
3397 merge_weak (newdecl, olddecl);
3399 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3400 DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3401 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3402 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3403 if (! DECL_EXTERNAL (olddecl))
3404 DECL_EXTERNAL (newdecl) = 0;
3406 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3408 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3409 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3410 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3411 DECL_TEMPLATE_INSTANTIATED (newdecl)
3412 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3413 /* Don't really know how much of the language-specific
3414 values we should copy from old to new. */
3415 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3416 DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3417 DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3418 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3419 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3420 DECL_INITIALIZED_IN_CLASS_P (newdecl)
3421 |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3422 olddecl_friend = DECL_FRIEND_P (olddecl);
3424 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3425 if (TREE_CODE (newdecl) == FUNCTION_DECL
3426 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3428 DECL_BEFRIENDING_CLASSES (newdecl)
3429 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3430 DECL_BEFRIENDING_CLASSES (olddecl));
3431 /* DECL_THUNKS is only valid for virtual functions,
3432 otherwise it is a DECL_FRIEND_CONTEXT. */
3433 if (DECL_VIRTUAL_P (newdecl))
3434 DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3438 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3440 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3441 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3443 /* If newdecl is not a specialization, then it is not a
3444 template-related function at all. And that means that we
3445 should have exited above, returning 0. */
3446 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3449 if (TREE_USED (olddecl))
3450 /* From [temp.expl.spec]:
3452 If a template, a member template or the member of a class
3453 template is explicitly specialized then that
3454 specialization shall be declared before the first use of
3455 that specialization that would cause an implicit
3456 instantiation to take place, in every translation unit in
3457 which such a use occurs. */
3458 error ("explicit specialization of %D after first use",
3461 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3463 /* [temp.expl.spec/14] We don't inline explicit specialization
3464 just because the primary template says so. */
3468 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3469 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3471 DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3473 /* If either decl says `inline', this fn is inline, unless
3474 its definition was passed already. */
3475 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3476 DECL_INLINE (olddecl) = 1;
3477 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3479 DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3480 = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3483 /* Preserve abstractness on cloned [cd]tors. */
3484 DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3488 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3489 COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3490 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3492 if (! types_match || new_defines_function)
3494 /* These need to be copied so that the names are available.
3495 Note that if the types do match, we'll preserve inline
3496 info and other bits, but if not, we won't. */
3497 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3498 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3500 if (new_defines_function)
3501 /* If defining a function declared with other language
3502 linkage, use the previously declared language linkage. */
3503 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3504 else if (types_match)
3506 /* If redeclaring a builtin function, and not a definition,
3507 it stays built in. */
3508 if (DECL_BUILT_IN (olddecl))
3510 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3511 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3512 /* If we're keeping the built-in definition, keep the rtl,
3513 regardless of declaration matches. */
3514 SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3517 DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3519 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3520 /* Don't clear out the arguments if we're redefining a function. */
3521 if (DECL_ARGUMENTS (olddecl))
3522 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3525 else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3526 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3528 /* Now preserve various other info from the definition. */
3529 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3530 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3531 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3532 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3534 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3538 function_size = sizeof (struct tree_decl);
3540 memcpy ((char *) olddecl + sizeof (struct tree_common),
3541 (char *) newdecl + sizeof (struct tree_common),
3542 function_size - sizeof (struct tree_common));
3544 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3546 /* If newdecl is a template instantiation, it is possible that
3547 the following sequence of events has occurred:
3549 o A friend function was declared in a class template. The
3550 class template was instantiated.
3552 o The instantiation of the friend declaration was
3553 recorded on the instantiation list, and is newdecl.
3555 o Later, however, instantiate_class_template called pushdecl
3556 on the newdecl to perform name injection. But, pushdecl in
3557 turn called duplicate_decls when it discovered that another
3558 declaration of a global function with the same name already
3561 o Here, in duplicate_decls, we decided to clobber newdecl.
3563 If we're going to do that, we'd better make sure that
3564 olddecl, and not newdecl, is on the list of
3565 instantiations so that if we try to do the instantiation
3566 again we won't get the clobbered declaration. */
3568 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3569 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3571 for (; decls; decls = TREE_CHAIN (decls))
3572 if (TREE_VALUE (decls) == newdecl)
3573 TREE_VALUE (decls) = olddecl;
3578 memcpy ((char *) olddecl + sizeof (struct tree_common),
3579 (char *) newdecl + sizeof (struct tree_common),
3580 sizeof (struct tree_decl) - sizeof (struct tree_common)
3581 + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3584 DECL_UID (olddecl) = olddecl_uid;
3586 DECL_FRIEND_P (olddecl) = 1;
3588 /* NEWDECL contains the merged attribute lists.
3589 Update OLDDECL to be the same. */
3590 DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3595 /* Record a decl-node X as belonging to the current lexical scope.
3596 Check for errors (such as an incompatible declaration for the same
3597 name already seen in the same scope).
3599 Returns either X or an old decl for the same name.
3600 If an old decl is returned, it may have been smashed
3601 to agree with what X says. */
3608 int need_new_binding;
3610 timevar_push (TV_NAME_LOOKUP);
3611 /* We shouldn't be calling pushdecl when we're generating RTL for a
3612 function that we already did semantic analysis on previously. */
3613 my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3616 need_new_binding = 1;
3618 if (DECL_TEMPLATE_PARM_P (x))
3619 /* Template parameters have no context; they are not X::T even
3620 when declared within a class or namespace. */
3624 if (current_function_decl && x != current_function_decl
3625 /* A local declaration for a function doesn't constitute
3627 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3628 /* A local declaration for an `extern' variable is in the
3629 scope of the current namespace, not the current
3631 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3632 && !DECL_CONTEXT (x))
3633 DECL_CONTEXT (x) = current_function_decl;
3635 /* If this is the declaration for a namespace-scope function,
3636 but the declaration itself is in a local scope, mark the
3638 if (TREE_CODE (x) == FUNCTION_DECL
3639 && DECL_NAMESPACE_SCOPE_P (x)
3640 && current_function_decl
3641 && x != current_function_decl)
3642 DECL_LOCAL_FUNCTION_P (x) = 1;
3645 name = DECL_NAME (x);
3648 int different_binding_level = 0;
3650 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3651 name = TREE_OPERAND (name, 0);
3653 /* In case this decl was explicitly namespace-qualified, look it
3654 up in its namespace context. */
3655 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3656 && namespace_bindings_p ())
3657 t = namespace_binding (name, DECL_CONTEXT (x));
3659 t = lookup_name_current_level (name);
3661 /* [basic.link] If there is a visible declaration of an entity
3662 with linkage having the same name and type, ignoring entities
3663 declared outside the innermost enclosing namespace scope, the
3664 block scope declaration declares that same entity and
3665 receives the linkage of the previous declaration. */
3666 if (! t && current_function_decl && x != current_function_decl
3667 && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3668 && DECL_EXTERNAL (x))
3670 /* Look in block scope. */
3671 t = IDENTIFIER_VALUE (name);
3672 /* Or in the innermost namespace. */
3674 t = namespace_binding (name, DECL_CONTEXT (x));
3675 /* Does it have linkage? Note that if this isn't a DECL, it's an
3676 OVERLOAD, which is OK. */
3677 if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3680 different_binding_level = 1;
3683 /* If we are declaring a function, and the result of name-lookup
3684 was an OVERLOAD, look for an overloaded instance that is
3685 actually the same as the function we are declaring. (If
3686 there is one, we have to merge our declaration with the
3687 previous declaration.) */
3688 if (t && TREE_CODE (t) == OVERLOAD)
3692 if (TREE_CODE (x) == FUNCTION_DECL)
3693 for (match = t; match; match = OVL_NEXT (match))
3695 if (decls_match (OVL_CURRENT (match), x))
3699 /* Just choose one. */
3703 t = OVL_CURRENT (match);
3708 if (t == error_mark_node)
3710 /* error_mark_node is 0 for a while during initialization! */
3712 cp_error_at ("`%#D' used prior to declaration", x);
3714 else if (t != NULL_TREE)
3716 if (different_binding_level)
3718 if (decls_match (x, t))
3719 /* The standard only says that the local extern
3720 inherits linkage from the previous decl; in
3721 particular, default args are not shared. It would
3722 be nice to propagate inlining info, though. FIXME. */
3723 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3725 else if (TREE_CODE (t) == PARM_DECL)
3727 if (DECL_CONTEXT (t) == NULL_TREE)
3728 /* This is probaby caused by too many errors, but calling
3729 abort will say that if errors have occurred. */
3732 /* Check for duplicate params. */
3733 if (duplicate_decls (x, t))
3734 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3736 else if ((DECL_EXTERN_C_FUNCTION_P (x)
3737 || DECL_FUNCTION_TEMPLATE_P (x))
3738 && is_overloaded_fn (t))
3739 /* Don't do anything just yet. */;
3740 else if (t == wchar_decl_node)
3742 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3743 pedwarn ("redeclaration of `wchar_t' as `%T'",
3746 /* Throw away the redeclaration. */
3747 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3749 else if (TREE_CODE (t) != TREE_CODE (x))
3751 if (duplicate_decls (x, t))
3752 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3754 else if (duplicate_decls (x, t))
3756 if (TREE_CODE (t) == TYPE_DECL)
3757 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3758 else if (TREE_CODE (t) == FUNCTION_DECL)
3759 check_default_args (t);
3761 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3763 else if (DECL_MAIN_P (x))
3765 /* A redeclaration of main, but not a duplicate of the
3770 This function shall not be overloaded. */
3771 cp_error_at ("invalid redeclaration of `%D'", t);
3772 error ("as `%D'", x);
3773 /* We don't try to push this declaration since that
3775 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3779 check_template_shadow (x);
3781 /* If this is a function conjured up by the backend, massage it
3782 so it looks friendly. */
3783 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3785 retrofit_lang_decl (x);
3786 SET_DECL_LANGUAGE (x, lang_c);
3789 if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3791 t = push_overloaded_decl (x, PUSH_LOCAL);
3793 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3794 if (!namespace_bindings_p ())
3795 /* We do not need to create a binding for this name;
3796 push_overloaded_decl will have already done so if
3798 need_new_binding = 0;
3800 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3802 t = push_overloaded_decl (x, PUSH_GLOBAL);
3804 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3805 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3808 /* If declaring a type as a typedef, copy the type (unless we're
3809 at line 0), and install this TYPE_DECL as the new type's typedef
3810 name. See the extensive comment in ../c-decl.c (pushdecl). */
3811 if (TREE_CODE (x) == TYPE_DECL)
3813 tree type = TREE_TYPE (x);
3814 if (DECL_SOURCE_LINE (x) == 0)
3816 if (TYPE_NAME (type) == 0)
3817 TYPE_NAME (type) = x;
3819 else if (type != error_mark_node && TYPE_NAME (type) != x
3820 /* We don't want to copy the type when all we're
3821 doing is making a TYPE_DECL for the purposes of
3823 && (!TYPE_NAME (type)
3824 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3826 DECL_ORIGINAL_TYPE (x) = type;
3827 type = build_type_copy (type);
3828 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3829 TYPE_NAME (type) = x;
3830 TREE_TYPE (x) = type;
3833 if (type != error_mark_node
3835 && TYPE_IDENTIFIER (type))
3836 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3837 current_binding_level);
3841 /* Multiple external decls of the same identifier ought to match.
3843 We get warnings about inline functions where they are defined.
3844 We get warnings about other functions from push_overloaded_decl.
3846 Avoid duplicate warnings where they are used. */
3847 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3851 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3852 if (decl && TREE_CODE (decl) == OVERLOAD)
3853 decl = OVL_FUNCTION (decl);
3855 if (decl && decl != error_mark_node
3856 && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3857 /* If different sort of thing, we already gave an error. */
3858 && TREE_CODE (decl) == TREE_CODE (x)
3859 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3861 pedwarn ("type mismatch with previous external decl", x);
3862 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3866 /* This name is new in its binding level.
3867 Install the new declaration and return it. */
3868 if (namespace_bindings_p ())
3870 /* Install a global value. */
3872 /* If the first global decl has external linkage,
3873 warn if we later see static one. */
3874 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3875 TREE_PUBLIC (name) = 1;
3877 /* Bind the name for the entity. */
3878 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3880 && (TREE_CODE (x) == TYPE_DECL
3881 || TREE_CODE (x) == VAR_DECL
3882 || TREE_CODE (x) == ALIAS_DECL
3883 || TREE_CODE (x) == NAMESPACE_DECL
3884 || TREE_CODE (x) == CONST_DECL
3885 || TREE_CODE (x) == TEMPLATE_DECL))
3886 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3888 /* Don't forget if the function was used via an implicit decl. */
3889 if (IDENTIFIER_IMPLICIT_DECL (name)
3890 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3893 /* Don't forget if its address was taken in that way. */
3894 if (IDENTIFIER_IMPLICIT_DECL (name)
3895 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3896 TREE_ADDRESSABLE (x) = 1;
3898 /* Warn about mismatches against previous implicit decl. */
3899 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3900 /* If this real decl matches the implicit, don't complain. */
3901 && ! (TREE_CODE (x) == FUNCTION_DECL
3902 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3904 ("`%D' was previously implicitly declared to return `int'", x);
3906 /* If new decl is `static' and an `extern' was seen previously,
3908 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3909 warn_extern_redeclared_static (x, t);
3913 /* Here to install a non-global value. */
3914 tree oldlocal = IDENTIFIER_VALUE (name);
3915 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3917 if (need_new_binding)
3919 push_local_binding (name, x, 0);
3920 /* Because push_local_binding will hook X on to the
3921 current_binding_level's name list, we don't want to
3922 do that again below. */
3923 need_new_binding = 0;
3926 /* If this is a TYPE_DECL, push it into the type value slot. */
3927 if (TREE_CODE (x) == TYPE_DECL)
3928 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3929 current_binding_level);
3931 /* Clear out any TYPE_DECL shadowed by a namespace so that
3932 we won't think this is a type. The C struct hack doesn't
3933 go through namespaces. */
3934 if (TREE_CODE (x) == NAMESPACE_DECL)
3935 set_identifier_type_value_with_scope (name, NULL_TREE,
3936 current_binding_level);
3943 && TREE_CODE (oldlocal) == VAR_DECL
3944 && DECL_DEAD_FOR_LOCAL (oldlocal))
3945 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3947 if (oldlocal == NULL_TREE)
3948 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3951 /* If this is an extern function declaration, see if we
3952 have a global definition or declaration for the function. */
3953 if (oldlocal == NULL_TREE
3954 && DECL_EXTERNAL (x)
3955 && oldglobal != NULL_TREE
3956 && TREE_CODE (x) == FUNCTION_DECL
3957 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3959 /* We have one. Their types must agree. */
3960 if (decls_match (x, oldglobal))
3964 warning ("extern declaration of `%#D' doesn't match", x);
3965 cp_warning_at ("global declaration `%#D'", oldglobal);
3968 /* If we have a local external declaration,
3969 and no file-scope declaration has yet been seen,
3970 then if we later have a file-scope decl it must not be static. */
3971 if (oldlocal == NULL_TREE
3972 && oldglobal == NULL_TREE
3973 && DECL_EXTERNAL (x)
3975 TREE_PUBLIC (name) = 1;
3977 /* Warn if shadowing an argument at the top level of the body. */
3978 if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3979 /* Inline decls shadow nothing. */
3980 && !DECL_FROM_INLINE (x)
3981 && TREE_CODE (oldlocal) == PARM_DECL
3982 /* Don't check the `this' parameter. */
3983 && !DECL_ARTIFICIAL (oldlocal))
3987 /* Don't complain if it's from an enclosing function. */
3988 if (DECL_CONTEXT (oldlocal) == current_function_decl
3989 && TREE_CODE (x) != PARM_DECL)
3991 /* Go to where the parms should be and see if we find
3993 struct cp_binding_level *b = current_binding_level->level_chain;
3995 /* Skip the ctor/dtor cleanup level. */
3999 if (b->parm_flag == 1)
4001 error ("declaration of `%#D' shadows a parameter",
4007 if (warn_shadow && !err)
4008 shadow_warning (SW_PARAM,
4009 IDENTIFIER_POINTER (name), oldlocal);
4012 /* Maybe warn if shadowing something else. */
4013 else if (warn_shadow && !DECL_EXTERNAL (x)
4014 /* No shadow warnings for internally generated vars. */
4015 && ! DECL_ARTIFICIAL (x)
4016 /* No shadow warnings for vars made for inlining. */
4017 && ! DECL_FROM_INLINE (x))
4019 if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4020 && current_class_ptr
4021 && !TREE_STATIC (name))
4022 warning ("declaration of `%s' shadows a member of `this'",
4023 IDENTIFIER_POINTER (name));
4024 else if (oldlocal != NULL_TREE
4025 && TREE_CODE (oldlocal) == VAR_DECL)
4026 shadow_warning (SW_LOCAL,
4027 IDENTIFIER_POINTER (name), oldlocal);
4028 else if (oldglobal != NULL_TREE
4029 && TREE_CODE (oldglobal) == VAR_DECL)
4030 /* XXX shadow warnings in outer-more namespaces */
4031 shadow_warning (SW_GLOBAL,
4032 IDENTIFIER_POINTER (name), oldglobal);
4036 if (TREE_CODE (x) == FUNCTION_DECL)
4037 check_default_args (x);
4039 if (TREE_CODE (x) == VAR_DECL)
4040 maybe_register_incomplete_var (x);
4043 if (need_new_binding)
4044 add_decl_to_level (x,
4045 DECL_NAMESPACE_SCOPE_P (x)
4046 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4047 : current_binding_level);
4049 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4052 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4053 caller to set DECL_CONTEXT properly. */
4056 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4058 register struct cp_binding_level *b;
4059 tree function_decl = current_function_decl;
4061 timevar_push (TV_NAME_LOOKUP);
4062 current_function_decl = NULL_TREE;
4063 if (level->parm_flag == 2)
4065 b = class_binding_level;
4066 class_binding_level = level;
4067 pushdecl_class_level (x);
4068 class_binding_level = b;
4072 b = current_binding_level;
4073 current_binding_level = level;
4075 current_binding_level = b;
4077 current_function_decl = function_decl;
4078 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4081 /* Like pushdecl, only it places X in the current namespace,
4085 pushdecl_namespace_level (tree x)
4087 register struct cp_binding_level *b = current_binding_level;
4090 timevar_push (TV_NAME_LOOKUP);
4091 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4093 /* Now, the type_shadowed stack may screw us. Munge it so it does
4095 if (TREE_CODE (x) == TYPE_DECL)
4097 tree name = DECL_NAME (x);
4099 tree *ptr = (tree *)0;
4100 for (; b != global_binding_level; b = b->level_chain)
4102 tree shadowed = b->type_shadowed;
4103 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4104 if (TREE_PURPOSE (shadowed) == name)
4106 ptr = &TREE_VALUE (shadowed);
4107 /* Can't break out of the loop here because sometimes
4108 a binding level will have duplicate bindings for
4109 PT names. It's gross, but I haven't time to fix it. */