1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; 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. */
38 #include "tree-inline.h"
50 #include "diagnostic.h"
52 extern const struct attribute_spec *lang_attribute_table;
54 #ifndef BOOL_TYPE_SIZE
55 /* `bool' has size and alignment `1', on all platforms. */
56 #define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
59 static tree grokparms PARAMS ((tree));
60 static const char *redeclaration_error_message PARAMS ((tree, tree));
62 static void push_binding_level PARAMS ((struct binding_level *, int,
64 static void pop_binding_level PARAMS ((void));
65 static void suspend_binding_level PARAMS ((void));
66 static void resume_binding_level PARAMS ((struct binding_level *));
67 static struct binding_level *make_binding_level PARAMS ((void));
68 static void declare_namespace_level PARAMS ((void));
69 static int decl_jump_unsafe PARAMS ((tree));
70 static void storedecls PARAMS ((tree));
71 static void require_complete_types_for_parms PARAMS ((tree));
72 static int ambi_op_p PARAMS ((enum tree_code));
73 static int unary_op_p PARAMS ((enum tree_code));
74 static tree store_bindings PARAMS ((tree, tree));
75 static tree lookup_tag_reverse PARAMS ((tree, tree));
76 static tree obscure_complex_init PARAMS ((tree, tree));
77 static tree lookup_name_real PARAMS ((tree, int, int, int));
78 static void push_local_name PARAMS ((tree));
79 static void warn_extern_redeclared_static PARAMS ((tree, tree));
80 static tree grok_reference_init PARAMS ((tree, tree, tree));
81 static tree grokfndecl PARAMS ((tree, tree, tree, tree, int,
82 enum overload_flags, tree,
83 tree, int, int, int, int, int, int, tree));
84 static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree));
85 static tree lookup_tag PARAMS ((enum tree_code, tree,
86 struct binding_level *, int));
87 static void set_identifier_type_value_with_scope
88 PARAMS ((tree, tree, struct binding_level *));
89 static void record_unknown_type PARAMS ((tree, const char *));
90 static tree build_library_fn_1 PARAMS ((tree, enum tree_code, tree));
91 static int member_function_or_else PARAMS ((tree, tree, enum overload_flags));
92 static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int,
94 static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct binding_level*));
95 static void check_for_uninitialized_const_var PARAMS ((tree));
96 static unsigned long typename_hash PARAMS ((hash_table_key));
97 static bool typename_compare PARAMS ((hash_table_key, hash_table_key));
98 static void push_binding PARAMS ((tree, tree, struct binding_level*));
99 static int add_binding PARAMS ((tree, tree));
100 static void pop_binding PARAMS ((tree, tree));
101 static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *));
102 static tree find_binding PARAMS ((tree, tree));
103 static tree select_decl PARAMS ((tree, int));
104 static int lookup_flags PARAMS ((int, int));
105 static tree qualify_lookup PARAMS ((tree, int));
106 static tree record_builtin_java_type PARAMS ((const char *, int));
107 static const char *tag_name PARAMS ((enum tag_types code));
108 static void find_class_binding_level PARAMS ((void));
109 static struct binding_level *innermost_nonclass_level PARAMS ((void));
110 static void warn_about_implicit_typename_lookup PARAMS ((tree, tree));
111 static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *));
112 static int walk_globals_r PARAMS ((tree, void *));
113 static void add_decl_to_level PARAMS ((tree, struct binding_level *));
114 static tree make_label_decl PARAMS ((tree, int));
115 static void use_label PARAMS ((tree));
116 static void check_previous_goto_1 PARAMS ((tree, struct binding_level *, tree,
118 static void check_previous_goto PARAMS ((struct named_label_use_list *));
119 static void check_switch_goto PARAMS ((struct binding_level *));
120 static void check_previous_gotos PARAMS ((tree));
121 static void pop_label PARAMS ((tree, tree));
122 static void pop_labels PARAMS ((tree));
123 static void maybe_deduce_size_from_array_init PARAMS ((tree, tree));
124 static void layout_var_decl PARAMS ((tree));
125 static void maybe_commonize_var PARAMS ((tree));
126 static tree check_initializer PARAMS ((tree, tree));
127 static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *));
128 static void push_cp_function_context PARAMS ((struct function *));
129 static void pop_cp_function_context PARAMS ((struct function *));
130 static void mark_binding_level PARAMS ((void *));
131 static void mark_named_label_lists PARAMS ((void *, void *));
132 static void mark_cp_function_context PARAMS ((struct function *));
133 static void mark_saved_scope PARAMS ((void *));
134 static void mark_lang_function PARAMS ((struct cp_language_function *));
135 static void save_function_data PARAMS ((tree));
136 static void check_function_type PARAMS ((tree, tree));
137 static void destroy_local_var PARAMS ((tree));
138 static void begin_constructor_body PARAMS ((void));
139 static void finish_constructor_body PARAMS ((void));
140 static void begin_destructor_body PARAMS ((void));
141 static void finish_destructor_body PARAMS ((void));
142 static tree create_array_type_for_decl PARAMS ((tree, tree, tree));
143 static tree get_atexit_node PARAMS ((void));
144 static tree get_dso_handle_node PARAMS ((void));
145 static tree start_cleanup_fn PARAMS ((void));
146 static void end_cleanup_fn PARAMS ((void));
147 static tree cp_make_fname_decl PARAMS ((tree, int));
148 static void initialize_predefined_identifiers PARAMS ((void));
149 static tree check_special_function_return_type
150 PARAMS ((special_function_kind, tree, tree));
151 static tree push_cp_library_fn PARAMS ((enum tree_code, tree));
152 static tree build_cp_library_fn PARAMS ((tree, enum tree_code, tree));
153 static void store_parm_decls PARAMS ((tree));
154 static int cp_missing_noreturn_ok_p PARAMS ((tree));
156 #if defined (DEBUG_CP_BINDING_LEVELS)
157 static void indent PARAMS ((void));
160 /* Erroneous argument lists can use this *IFF* they do not modify it. */
161 tree error_mark_list;
163 /* The following symbols are subsumed in the cp_global_trees array, and
164 listed here individually for documentation purposes.
167 tree wchar_decl_node;
169 tree vtable_entry_type;
170 tree delta_type_node;
171 tree __t_desc_type_node;
172 tree ti_desc_type_node;
173 tree bltn_desc_type_node, ptr_desc_type_node;
174 tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
175 tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
176 tree ptm_desc_type_node;
177 tree base_desc_type_node;
179 tree class_type_node, record_type_node, union_type_node, enum_type_node;
180 tree unknown_type_node;
182 Array type `vtable_entry_type[]'
185 tree vtbl_ptr_type_node;
192 A FUNCTION_DECL which can call `abort'. Not necessarily the
193 one that the user will declare, but sufficient to be called
194 by routines that want to abort the program.
198 The FUNCTION_DECL for the default `::operator delete'.
200 tree global_delete_fndecl;
203 tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
208 tree cp_global_trees[CPTI_MAX];
210 /* Indicates that there is a type value in some namespace, although
211 that is not necessarily in scope at the moment. */
213 static tree global_type_node;
215 /* Expect only namespace names now. */
216 static int only_namespace_names;
218 /* Used only for jumps to as-yet undefined labels, since jumps to
219 defined labels can have their validity checked immediately. */
221 struct named_label_use_list
223 struct binding_level *binding_level;
226 const char *filename_o_goto;
228 struct named_label_use_list *next;
231 #define named_label_uses cp_function_chain->x_named_label_uses
233 #define local_names cp_function_chain->x_local_names
235 /* A list of objects which have constructors or destructors
236 which reside in the global scope. The decl is stored in
237 the TREE_VALUE slot and the initializer is stored
238 in the TREE_PURPOSE slot. */
239 tree static_aggregates;
243 /* A node for the integer constants 2, and 3. */
245 tree integer_two_node, integer_three_node;
247 /* Similar, for last_function_parm_tags. */
248 tree last_function_parms;
250 /* A list of all LABEL_DECLs in the function that have names. Here so
251 we can clear out their names' definitions at the end of the
252 function, and so we can check the validity of jumps to these labels. */
254 struct named_label_list
256 struct binding_level *binding_level;
261 struct named_label_list *next;
262 unsigned int in_try_scope : 1;
263 unsigned int in_catch_scope : 1;
266 #define named_labels cp_function_chain->x_named_labels
268 /* Nonzero means use the ISO C94 dialect of C. */
272 /* Nonzero means use the ISO C99 dialect of C. */
276 /* Nonzero means we are a hosted implementation for code shared with C. */
280 /* Nonzero means add default format_arg attributes for functions not
283 int flag_noniso_default_format_attributes = 1;
285 /* Nonzero if we want to conserve space in the .o files. We do this
286 by putting uninitialized data and runtime initialized data into
287 .common instead of .data at the expense of not flagging multiple
289 extern int flag_conserve_space;
291 /* C and C++ flags are in decl2.c. */
293 /* A expression of value 0 with the same precision as a sizetype
295 tree signed_size_zero_node;
297 /* The name of the anonymous namespace, throughout this translation
299 tree anonymous_namespace_name;
301 /* The number of function bodies which we are currently processing.
302 (Zero if we are at namespace scope, one inside the body of a
303 function, two inside the body of a function in a local class, etc.) */
306 /* States indicating how grokdeclarator() should handle declspecs marked
307 with __attribute__((deprecated)). An object declared as
308 __attribute__((deprecated)) suppresses warnings of uses of other
311 enum deprecated_states {
316 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
318 /* Set by add_implicitly_declared_members() to keep those members from
319 being flagged as deprecated or reported as using deprecated
321 int adding_implicit_members = 0;
323 /* For each binding contour we allocate a binding_level structure
324 which records the names defined in that contour.
327 1) one for each function definition,
328 where internal declarations of the parameters appear.
329 2) one for each compound statement,
330 to record its declarations.
332 The current meaning of a name can be found by searching the levels
333 from the current one out to the global one.
335 Off to the side, may be the class_binding_level. This exists only
336 to catch class-local declarations. It is otherwise nonexistent.
338 Also there may be binding levels that catch cleanups that must be
339 run when exceptions occur. Thus, to see whether a name is bound in
340 the current scope, it is not enough to look in the
341 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
344 /* Note that the information in the `names' component of the global contour
345 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
349 /* A chain of _DECL nodes for all variables, constants, functions,
350 and typedef types. These are in the reverse of the order
351 supplied. There may be OVERLOADs on this list, too, but they
352 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
355 /* A list of structure, union and enum definitions, for looking up
357 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
358 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
359 or ENUMERAL_TYPE node.
361 C++: the TREE_VALUE nodes can be simple types for
362 component_bindings. */
365 /* A list of USING_DECL nodes. */
368 /* A list of used namespaces. PURPOSE is the namespace,
369 VALUE the common ancestor with this binding_level's namespace. */
370 tree using_directives;
372 /* If this binding level is the binding level for a class, then
373 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
374 is the name of an entity bound in the class. The TREE_TYPE is
375 the DECL bound by this name in the class. */
378 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
379 is used for all binding levels. In addition the TREE_VALUE is the
380 IDENTIFIER_TYPE_VALUE before we entered the class. */
383 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
384 label in this scope. The TREE_PURPOSE is the previous value of
385 the IDENTIFIER_LABEL VALUE. */
386 tree shadowed_labels;
388 /* For each level (except not the global one),
389 a chain of BLOCK nodes for all the levels
390 that were entered and exited one level down. */
393 /* The _TYPE node for this level, if parm_flag == 2. */
396 /* The binding level which this one is contained in (inherits from). */
397 struct binding_level *level_chain;
399 /* List of decls in `names' that have incomplete
400 structure or union types. */
403 /* List of VAR_DECLS saved from a previous for statement.
404 These would be dead in ISO-conforming code, but might
405 be referenced in ARM-era code. These are stored in a
406 TREE_LIST; the TREE_VALUE is the actual declaration. */
407 tree dead_vars_from_for;
409 /* 1 for the level that holds the parameters of a function.
410 2 for the level that holds a class declaration. */
411 unsigned parm_flag : 2;
413 /* 1 means make a BLOCK for this level regardless of all else.
414 2 for temporary binding contours created by the compiler. */
417 /* Nonzero if this level "doesn't exist" for tags. */
418 unsigned tag_transparent : 1;
420 /* Nonzero if this level can safely have additional
421 cleanup-needing variables added to it. */
422 unsigned more_cleanups_ok : 1;
423 unsigned have_cleanups : 1;
425 /* Nonzero if this scope is for storing the decls for template
426 parameters and generic decls; these decls will be discarded and
427 replaced with a TEMPLATE_DECL. */
428 unsigned template_parms_p : 1;
430 /* Nonzero if this scope corresponds to the `<>' in a
431 `template <>' clause. Whenever this flag is set,
432 TEMPLATE_PARMS_P will be set as well. */
433 unsigned template_spec_p : 1;
435 /* This is set for a namespace binding level. */
436 unsigned namespace_p : 1;
438 /* True if this level is that of a for-statement where we need to
439 worry about ambiguous (ARM or ISO) scope rules. */
440 unsigned is_for_scope : 1;
442 /* True if this level corresponds to a TRY block. Currently this
443 information is only available while building the tree structure. */
444 unsigned is_try_scope : 1;
446 /* True if this level corresponds to a CATCH block. Currently this
447 information is only available while building the tree structure. */
448 unsigned is_catch_scope : 1;
450 /* Three bits left for this word. */
452 #if defined(DEBUG_CP_BINDING_LEVELS)
453 /* Binding depth at which this level began. */
454 unsigned binding_depth;
455 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
458 #define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
460 /* The binding level currently in effect. */
462 #define current_binding_level \
463 (cfun && cp_function_chain->bindings \
464 ? cp_function_chain->bindings \
465 : scope_chain->bindings)
467 /* The binding level of the current class, if any. */
469 #define class_binding_level scope_chain->class_bindings
471 /* A chain of binding_level structures awaiting reuse. */
473 static struct binding_level *free_binding_level;
475 /* The outermost binding level, for names of file scope.
476 This is created when the compiler is started and exists
477 through the entire run. */
479 static struct binding_level *global_binding_level;
481 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
483 static int keep_next_level_flag;
485 #if defined(DEBUG_CP_BINDING_LEVELS)
486 static int binding_depth = 0;
487 static int is_class_level = 0;
494 for (i = 0; i < binding_depth*2; i++)
497 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
499 static tree pushdecl_with_scope PARAMS ((tree, struct binding_level *));
502 push_binding_level (newlevel, tag_transparent, keep)
503 struct binding_level *newlevel;
504 int tag_transparent, keep;
506 /* Add this level to the front of the chain (stack) of levels that
508 memset ((char*) newlevel, 0, sizeof (struct binding_level));
509 newlevel->level_chain = current_binding_level;
510 current_binding_level = newlevel;
511 newlevel->tag_transparent = tag_transparent;
512 newlevel->more_cleanups_ok = 1;
514 newlevel->keep = keep;
515 #if defined(DEBUG_CP_BINDING_LEVELS)
516 newlevel->binding_depth = binding_depth;
518 fprintf (stderr, "push %s level 0x%08x line %d\n",
519 (is_class_level) ? "class" : "block", newlevel, lineno);
522 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
525 /* Find the innermost enclosing class scope, and reset
526 CLASS_BINDING_LEVEL appropriately. */
529 find_class_binding_level ()
531 struct binding_level *level = current_binding_level;
533 while (level && level->parm_flag != 2)
534 level = level->level_chain;
535 if (level && level->parm_flag == 2)
536 class_binding_level = level;
538 class_binding_level = 0;
544 if (global_binding_level)
546 /* Cannot pop a level, if there are none left to pop. */
547 if (current_binding_level == global_binding_level)
550 /* Pop the current level, and free the structure for reuse. */
551 #if defined(DEBUG_CP_BINDING_LEVELS)
554 fprintf (stderr, "pop %s level 0x%08x line %d\n",
555 (is_class_level) ? "class" : "block",
556 current_binding_level, lineno);
557 if (is_class_level != (current_binding_level == class_binding_level))
560 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
563 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
565 register struct binding_level *level = current_binding_level;
566 current_binding_level = current_binding_level->level_chain;
567 level->level_chain = free_binding_level;
568 #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
569 if (level->binding_depth != binding_depth)
571 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
572 free_binding_level = level;
573 find_class_binding_level ();
578 suspend_binding_level ()
580 if (class_binding_level)
581 current_binding_level = class_binding_level;
583 if (global_binding_level)
585 /* Cannot suspend a level, if there are none left to suspend. */
586 if (current_binding_level == global_binding_level)
589 /* Suspend the current level. */
590 #if defined(DEBUG_CP_BINDING_LEVELS)
593 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
594 (is_class_level) ? "class" : "block",
595 current_binding_level, lineno);
596 if (is_class_level != (current_binding_level == class_binding_level))
599 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
602 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
603 current_binding_level = current_binding_level->level_chain;
604 find_class_binding_level ();
608 resume_binding_level (b)
609 struct binding_level *b;
611 /* Resuming binding levels is meant only for namespaces,
612 and those cannot nest into classes. */
613 my_friendly_assert(!class_binding_level, 386);
614 /* Also, resuming a non-directly nested namespace is a no-no. */
615 my_friendly_assert(b->level_chain == current_binding_level, 386);
616 current_binding_level = b;
617 #if defined(DEBUG_CP_BINDING_LEVELS)
618 b->binding_depth = binding_depth;
620 fprintf (stderr, "resume %s level 0x%08x line %d\n",
621 (is_class_level) ? "class" : "block", b, lineno);
624 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
627 /* Create a new `struct binding_level'. */
630 struct binding_level *
631 make_binding_level ()
634 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
637 /* Nonzero if we are currently in the global binding level. */
642 return current_binding_level == global_binding_level;
645 /* Return the innermost binding level that is not for a class scope. */
647 static struct binding_level *
648 innermost_nonclass_level ()
650 struct binding_level *b;
652 b = current_binding_level;
653 while (b->parm_flag == 2)
659 /* Nonzero if we are currently in a toplevel binding level. This
660 means either the global binding level or a namespace in a toplevel
661 binding level. Since there are no non-toplevel namespace levels,
662 this really means any namespace or template parameter level. We
663 also include a class whose context is toplevel. */
666 toplevel_bindings_p ()
668 struct binding_level *b = innermost_nonclass_level ();
670 return b->namespace_p || b->template_parms_p;
673 /* Nonzero if this is a namespace scope, or if we are defining a class
674 which is itself at namespace scope, or whose enclosing class is
675 such a class, etc. */
678 namespace_bindings_p ()
680 struct binding_level *b = innermost_nonclass_level ();
682 return b->namespace_p;
685 /* If KEEP is non-zero, make a BLOCK node for the next binding level,
686 unconditionally. Otherwise, use the normal logic to decide whether
687 or not to create a BLOCK. */
690 keep_next_level (keep)
693 keep_next_level_flag = keep;
696 /* Nonzero if the current level needs to have a BLOCK made. */
701 return (current_binding_level->blocks != NULL_TREE
702 || current_binding_level->keep
703 || current_binding_level->names != NULL_TREE
704 || (current_binding_level->tags != NULL_TREE
705 && !current_binding_level->tag_transparent));
709 declare_namespace_level ()
711 current_binding_level->namespace_p = 1;
714 /* Returns non-zero if this scope was created to store template
718 template_parm_scope_p ()
720 return current_binding_level->template_parms_p;
723 /* Returns the kind of template specialization we are currently
724 processing, given that it's declaration contained N_CLASS_SCOPES
725 explicit scope qualifications. */
728 current_tmpl_spec_kind (n_class_scopes)
731 int n_template_parm_scopes = 0;
732 int seen_specialization_p = 0;
733 int innermost_specialization_p = 0;
734 struct binding_level *b;
736 /* Scan through the template parameter scopes. */
737 for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
739 /* If we see a specialization scope inside a parameter scope,
740 then something is wrong. That corresponds to a declaration
743 template <class T> template <> ...
745 which is always illegal since [temp.expl.spec] forbids the
746 specialization of a class member template if the enclosing
747 class templates are not explicitly specialized as well. */
748 if (b->template_spec_p)
750 if (n_template_parm_scopes == 0)
751 innermost_specialization_p = 1;
753 seen_specialization_p = 1;
755 else if (seen_specialization_p == 1)
756 return tsk_invalid_member_spec;
758 ++n_template_parm_scopes;
761 /* Handle explicit instantiations. */
762 if (processing_explicit_instantiation)
764 if (n_template_parm_scopes != 0)
765 /* We've seen a template parameter list during an explicit
766 instantiation. For example:
768 template <class T> template void f(int);
770 This is erroneous. */
771 return tsk_invalid_expl_inst;
773 return tsk_expl_inst;
776 if (n_template_parm_scopes < n_class_scopes)
777 /* We've not seen enough template headers to match all the
778 specialized classes present. For example:
780 template <class T> void R<T>::S<T>::f(int);
782 This is illegal; there needs to be one set of template
783 parameters for each class. */
784 return tsk_insufficient_parms;
785 else if (n_template_parm_scopes == n_class_scopes)
786 /* We're processing a non-template declaration (even though it may
787 be a member of a template class.) For example:
789 template <class T> void S<T>::f(int);
791 The `class T' maches the `S<T>', leaving no template headers
792 corresponding to the `f'. */
794 else if (n_template_parm_scopes > n_class_scopes + 1)
795 /* We've got too many template headers. For example:
797 template <> template <class T> void f (T);
799 There need to be more enclosing classes. */
800 return tsk_excessive_parms;
802 /* This must be a template. It's of the form:
804 template <class T> template <class U> void S<T>::f(U);
806 This is a specialization if the innermost level was a
807 specialization; otherwise it's just a definition of the
809 return innermost_specialization_p ? tsk_expl_spec : tsk_template;
813 set_class_shadows (shadows)
816 class_binding_level->class_shadowed = shadows;
819 /* Enter a new binding level.
820 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
821 not for that of tags. */
824 pushlevel (tag_transparent)
827 struct binding_level *newlevel;
829 if (cfun && !doing_semantic_analysis_p ())
832 /* Reuse or create a struct for this binding level. */
833 #if defined(DEBUG_CP_BINDING_LEVELS)
835 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
836 if (free_binding_level)
837 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
839 newlevel = free_binding_level;
840 free_binding_level = free_binding_level->level_chain;
843 newlevel = make_binding_level ();
845 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
846 keep_next_level_flag = 0;
849 /* We're defining an object of type TYPE. If it needs a cleanup, but
850 we're not allowed to add any more objects with cleanups to the current
851 scope, create a new binding level. */
854 maybe_push_cleanup_level (type)
857 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
858 && current_binding_level->more_cleanups_ok == 0)
863 add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
867 /* Enter a new scope. The KIND indicates what kind of scope is being
878 case sk_template_spec:
879 current_binding_level->template_spec_p = 1;
882 case sk_template_parms:
883 current_binding_level->template_parms_p = 1;
891 /* Exit the current scope. */
900 note_level_for_for ()
902 current_binding_level->is_for_scope = 1;
905 /* Record that the current binding level represents a try block. */
908 note_level_for_try ()
910 current_binding_level->is_try_scope = 1;
913 /* Record that the current binding level represents a catch block. */
916 note_level_for_catch ()
918 current_binding_level->is_catch_scope = 1;
921 /* For a binding between a name and an entity at a block scope,
922 this is the `struct binding_level' for the block. */
923 #define BINDING_LEVEL(NODE) \
924 (((struct tree_binding*)(NODE))->scope.level)
926 /* A free list of CPLUS_BINDING nodes, connected by their
929 static tree free_bindings;
931 /* Make DECL the innermost binding for ID. The LEVEL is the binding
932 level at which this declaration is being bound. */
935 push_binding (id, decl, level)
938 struct binding_level* level;
944 binding = free_bindings;
945 free_bindings = TREE_CHAIN (binding);
948 binding = make_node (CPLUS_BINDING);
950 /* Now, fill in the binding information. */
951 BINDING_VALUE (binding) = decl;
952 BINDING_TYPE (binding) = NULL_TREE;
953 BINDING_LEVEL (binding) = level;
954 INHERITED_VALUE_BINDING_P (binding) = 0;
955 LOCAL_BINDING_P (binding) = (level != class_binding_level);
956 BINDING_HAS_LEVEL_P (binding) = 1;
958 /* And put it on the front of the list of bindings for ID. */
959 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
960 IDENTIFIER_BINDING (id) = binding;
963 /* ID is already bound in the current scope. But, DECL is an
964 additional binding for ID in the same scope. This is the `struct
965 stat' hack whereby a non-typedef class-name or enum-name can be
966 bound at the same level as some other kind of entity. It's the
967 responsibility of the caller to check that inserting this name is
968 legal here. Returns nonzero if the new binding was successful. */
970 add_binding (id, decl)
974 tree binding = IDENTIFIER_BINDING (id);
977 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
978 /* The new name is the type name. */
979 BINDING_TYPE (binding) = decl;
980 else if (!BINDING_VALUE (binding))
981 /* This situation arises when push_class_level_binding moves an
982 inherited type-binding out of the way to make room for a new
984 BINDING_VALUE (binding) = decl;
985 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
986 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
988 /* The old binding was a type name. It was placed in
989 BINDING_VALUE because it was thought, at the point it was
990 declared, to be the only entity with such a name. Move the
991 type name into the type slot; it is now hidden by the new
993 BINDING_TYPE (binding) = BINDING_VALUE (binding);
994 BINDING_VALUE (binding) = decl;
995 INHERITED_VALUE_BINDING_P (binding) = 0;
997 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
998 && TREE_CODE (decl) == TYPE_DECL
999 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
1000 && (same_type_p (TREE_TYPE (decl),
1001 TREE_TYPE (BINDING_VALUE (binding)))
1002 /* If either type involves template parameters, we must
1003 wait until instantiation. */
1004 || uses_template_parms (TREE_TYPE (decl))
1005 || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
1006 /* We have two typedef-names, both naming the same type to have
1007 the same name. This is OK because of:
1011 In a given scope, a typedef specifier can be used to redefine
1012 the name of any type declared in that scope to refer to the
1013 type to which it already refers. */
1015 /* There can be two block-scope declarations of the same variable,
1016 so long as they are `extern' declarations. */
1017 else if (TREE_CODE (decl) == VAR_DECL
1018 && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
1019 && DECL_EXTERNAL (decl)
1020 && DECL_EXTERNAL (BINDING_VALUE (binding)))
1022 duplicate_decls (decl, BINDING_VALUE (binding));
1027 error ("declaration of `%#D'", decl);
1028 cp_error_at ("conflicts with previous declaration `%#D'",
1029 BINDING_VALUE (binding));
1036 /* Add DECL to the list of things declared in B. */
1039 add_decl_to_level (decl, b)
1041 struct binding_level *b;
1043 /* We build up the list in reverse order, and reverse it later if
1045 TREE_CHAIN (decl) = b->names;
1049 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1050 binding level. If PUSH_USING is set in FLAGS, we know that DECL
1051 doesn't really belong to this binding level, that it got here
1052 through a using-declaration. */
1055 push_local_binding (id, decl, flags)
1060 struct binding_level *b;
1062 /* Skip over any local classes. This makes sense if we call
1063 push_local_binding with a friend decl of a local class. */
1064 b = current_binding_level;
1065 while (b->parm_flag == 2)
1068 if (lookup_name_current_level (id))
1070 /* Supplement the existing binding. */
1071 if (!add_binding (id, decl))
1072 /* It didn't work. Something else must be bound at this
1073 level. Do not add DECL to the list of things to pop
1078 /* Create a new binding. */
1079 push_binding (id, decl, b);
1081 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1082 /* We must put the OVERLOAD into a TREE_LIST since the
1083 TREE_CHAIN of an OVERLOAD is already used. Similarly for
1084 decls that got here through a using-declaration. */
1085 decl = build_tree_list (NULL_TREE, decl);
1087 /* And put DECL on the list of things declared by the current
1089 add_decl_to_level (decl, b);
1092 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
1093 binding was successful. */
1096 push_class_binding (id, decl)
1101 tree binding = IDENTIFIER_BINDING (id);
1104 /* Note that we declared this value so that we can issue an error if
1105 this an illegal redeclaration of a name already used for some
1107 note_name_declared_in_class (id, decl);
1109 if (binding && BINDING_LEVEL (binding) == class_binding_level)
1110 /* Supplement the existing binding. */
1111 result = add_binding (id, decl);
1113 /* Create a new binding. */
1114 push_binding (id, decl, class_binding_level);
1116 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1117 class-level declaration. Note that we do not use DECL here
1118 because of the possibility of the `struct stat' hack; if DECL is
1119 a class-name or enum-name we might prefer a field-name, or some
1121 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1123 /* If this is a binding from a base class, mark it as such. */
1124 binding = IDENTIFIER_BINDING (id);
1125 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1127 /* Any implicit typename must be from a base-class. The
1128 context for an implicit typename declaration is always
1129 the derived class in which the lookup was done, so the checks
1130 based on the context of DECL below will not trigger. */
1131 if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
1132 INHERITED_VALUE_BINDING_P (binding) = 1;
1135 if (TREE_CODE (decl) == OVERLOAD)
1136 context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1139 my_friendly_assert (DECL_P (decl), 0);
1140 context = context_for_name_lookup (decl);
1143 if (is_properly_derived_from (current_class_type, context))
1144 INHERITED_VALUE_BINDING_P (binding) = 1;
1146 INHERITED_VALUE_BINDING_P (binding) = 0;
1149 else if (BINDING_VALUE (binding) == decl)
1150 /* We only encounter a TREE_LIST when push_class_decls detects an
1151 ambiguity. Such an ambiguity can be overridden by a definition
1153 INHERITED_VALUE_BINDING_P (binding) = 1;
1158 /* Remove the binding for DECL which should be the innermost binding
1162 pop_binding (id, decl)
1168 if (id == NULL_TREE)
1169 /* It's easiest to write the loops that call this function without
1170 checking whether or not the entities involved have names. We
1171 get here for such an entity. */
1174 /* Get the innermost binding for ID. */
1175 binding = IDENTIFIER_BINDING (id);
1177 /* The name should be bound. */
1178 my_friendly_assert (binding != NULL_TREE, 0);
1180 /* The DECL will be either the ordinary binding or the type
1181 binding for this identifier. Remove that binding. */
1182 if (BINDING_VALUE (binding) == decl)
1183 BINDING_VALUE (binding) = NULL_TREE;
1184 else if (BINDING_TYPE (binding) == decl)
1185 BINDING_TYPE (binding) = NULL_TREE;
1189 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1191 /* We're completely done with the innermost binding for this
1192 identifier. Unhook it from the list of bindings. */
1193 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1195 /* Add it to the free list. */
1196 TREE_CHAIN (binding) = free_bindings;
1197 free_bindings = binding;
1199 /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1201 BINDING_LEVEL (binding) = NULL;
1205 /* When a label goes out of scope, check to see if that label was used
1206 in a valid manner, and issue any appropriate warnings or errors. */
1209 pop_label (label, old_value)
1213 if (!processing_template_decl && doing_semantic_analysis_p ())
1215 if (DECL_INITIAL (label) == NULL_TREE)
1217 cp_error_at ("label `%D' used but not defined", label);
1218 /* Avoid crashing later. */
1219 define_label (input_filename, 1, DECL_NAME (label));
1221 else if (warn_unused_label && !TREE_USED (label))
1222 cp_warning_at ("label `%D' defined but not used", label);
1225 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1228 /* At the end of a function, all labels declared within the function
1229 go out of scope. BLOCK is the top-level block for the
1236 struct named_label_list *link;
1238 /* Clear out the definitions of all label names, since their scopes
1240 for (link = named_labels; link; link = link->next)
1242 pop_label (link->label_decl, link->old_value);
1243 /* Put the labels into the "variables" of the top-level block,
1244 so debugger can see them. */
1245 TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1246 BLOCK_VARS (block) = link->label_decl;
1249 named_labels = NULL;
1252 /* Exit a binding level.
1253 Pop the level off, and restore the state of the identifier-decl mappings
1254 that were in effect when this level was entered.
1256 If KEEP == 1, this level had explicit declarations, so
1257 and create a "block" (a BLOCK node) for the level
1258 to record its declarations and subblocks for symbol table output.
1260 If FUNCTIONBODY is nonzero, this level is the body of a function,
1261 so create a block as if KEEP were set and also clear out all
1264 If REVERSE is nonzero, reverse the order of decls before putting
1265 them into the BLOCK. */
1268 poplevel (keep, reverse, functionbody)
1274 /* The chain of decls was accumulated in reverse order.
1275 Put it into forward order, just for cleanliness. */
1277 int tmp = functionbody;
1278 int real_functionbody;
1281 tree block = NULL_TREE;
1283 int leaving_for_scope;
1285 if (cfun && !doing_semantic_analysis_p ())
1288 my_friendly_assert (current_binding_level->parm_flag != 2,
1291 real_functionbody = (current_binding_level->keep == 2
1292 ? ((functionbody = 0), tmp) : functionbody);
1293 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1294 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1296 my_friendly_assert (!current_binding_level->class_shadowed,
1299 /* We used to use KEEP == 2 to indicate that the new block should go
1300 at the beginning of the list of blocks at this binding level,
1301 rather than the end. This hack is no longer used. */
1302 my_friendly_assert (keep == 0 || keep == 1, 0);
1304 if (current_binding_level->keep == 1)
1307 /* Any uses of undefined labels, and any defined labels, now operate
1308 under constraints of next binding contour. */
1309 if (cfun && !functionbody)
1311 struct binding_level *level_chain;
1312 level_chain = current_binding_level->level_chain;
1315 struct named_label_use_list *uses;
1316 struct named_label_list *labels;
1317 for (labels = named_labels; labels; labels = labels->next)
1318 if (labels->binding_level == current_binding_level)
1321 if (current_binding_level->is_try_scope)
1322 labels->in_try_scope = 1;
1323 if (current_binding_level->is_catch_scope)
1324 labels->in_catch_scope = 1;
1325 for (decl = labels->names_in_scope; decl;
1326 decl = TREE_CHAIN (decl))
1327 if (decl_jump_unsafe (decl))
1328 labels->bad_decls = tree_cons (NULL_TREE, decl,
1330 labels->binding_level = level_chain;
1331 labels->names_in_scope = level_chain->names;
1334 for (uses = named_label_uses; uses; uses = uses->next)
1335 if (uses->binding_level == current_binding_level)
1337 uses->binding_level = level_chain;
1338 uses->names_in_scope = level_chain->names;
1343 /* Get the decls in the order they were written.
1344 Usually current_binding_level->names is in reverse order.
1345 But parameter decls were previously put in forward order. */
1348 current_binding_level->names
1349 = decls = nreverse (current_binding_level->names);
1351 decls = current_binding_level->names;
1353 /* Output any nested inline functions within this block
1354 if they weren't already output. */
1355 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1356 if (TREE_CODE (decl) == FUNCTION_DECL
1357 && ! TREE_ASM_WRITTEN (decl)
1358 && DECL_INITIAL (decl) != NULL_TREE
1359 && TREE_ADDRESSABLE (decl)
1360 && decl_function_context (decl) == current_function_decl)
1362 /* If this decl was copied from a file-scope decl
1363 on account of a block-scope extern decl,
1364 propagate TREE_ADDRESSABLE to the file-scope decl. */
1365 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1366 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1369 push_function_context ();
1370 output_inline_function (decl);
1371 pop_function_context ();
1375 /* When not in function-at-a-time mode, expand_end_bindings will
1376 warn about unused variables. But, in function-at-a-time mode
1377 expand_end_bindings is not passed the list of variables in the
1378 current scope, and therefore no warning is emitted. So, we
1379 explicitly warn here. */
1380 if (!processing_template_decl)
1381 warn_about_unused_variables (getdecls ());
1383 /* If there were any declarations or structure tags in that level,
1384 or if this level is a function body,
1385 create a BLOCK to record them for the life of this function. */
1387 if (keep == 1 || functionbody)
1388 block = make_node (BLOCK);
1389 if (block != NULL_TREE)
1391 BLOCK_VARS (block) = decls;
1392 BLOCK_SUBBLOCKS (block) = subblocks;
1395 /* In each subblock, record that this is its superior. */
1397 for (link = subblocks; link; link = TREE_CHAIN (link))
1398 BLOCK_SUPERCONTEXT (link) = block;
1400 /* We still support the old for-scope rules, whereby the variables
1401 in a for-init statement were in scope after the for-statement
1402 ended. We only use the new rules in flag_new_for_scope is
1405 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1407 /* Remove declarations for all the DECLs in this level. */
1408 for (link = decls; link; link = TREE_CHAIN (link))
1410 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1411 && DECL_NAME (link))
1414 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1418 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1420 ns_binding = NULL_TREE;
1423 && (BINDING_LEVEL (outer_binding)
1424 == current_binding_level->level_chain))
1425 /* We have something like:
1430 and we are leaving the `for' scope. There's no reason to
1431 keep the binding of the inner `i' in this case. */
1432 pop_binding (DECL_NAME (link), link);
1433 else if ((outer_binding
1434 && (TREE_CODE (BINDING_VALUE (outer_binding))
1437 && TREE_CODE (ns_binding) == TYPE_DECL))
1438 /* Here, we have something like:
1446 We must pop the for-scope binding so we know what's a
1447 type and what isn't. */
1448 pop_binding (DECL_NAME (link), link);
1451 /* Mark this VAR_DECL as dead so that we can tell we left it
1452 there only for backward compatibility. */
1453 DECL_DEAD_FOR_LOCAL (link) = 1;
1455 /* Keep track of what should of have happenned when we
1456 popped the binding. */
1457 if (outer_binding && BINDING_VALUE (outer_binding))
1458 DECL_SHADOWED_FOR_VAR (link)
1459 = BINDING_VALUE (outer_binding);
1461 /* Add it to the list of dead variables in the next
1462 outermost binding to that we can remove these when we
1463 leave that binding. */
1464 current_binding_level->level_chain->dead_vars_from_for
1465 = tree_cons (NULL_TREE, link,
1466 current_binding_level->level_chain->
1467 dead_vars_from_for);
1469 /* Although we don't pop the CPLUS_BINDING, we do clear
1470 its BINDING_LEVEL since the level is going away now. */
1471 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1477 /* Remove the binding. */
1479 if (TREE_CODE (decl) == TREE_LIST)
1480 decl = TREE_VALUE (decl);
1482 pop_binding (DECL_NAME (decl), decl);
1483 else if (TREE_CODE (decl) == OVERLOAD)
1484 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1490 /* Remove declarations for any `for' variables from inner scopes
1491 that we kept around. */
1492 for (link = current_binding_level->dead_vars_from_for;
1493 link; link = TREE_CHAIN (link))
1494 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1496 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1497 for (link = current_binding_level->type_shadowed;
1498 link; link = TREE_CHAIN (link))
1499 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1501 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1502 for (link = current_binding_level->shadowed_labels;
1504 link = TREE_CHAIN (link))
1505 pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1507 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1508 list if a `using' declaration put them there. The debugging
1509 back-ends won't understand OVERLOAD, so we remove them here.
1510 Because the BLOCK_VARS are (temporarily) shared with
1511 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1512 popped all the bindings. */
1517 for (d = &BLOCK_VARS (block); *d; )
1519 if (TREE_CODE (*d) == TREE_LIST)
1520 *d = TREE_CHAIN (*d);
1522 d = &TREE_CHAIN (*d);
1526 /* If the level being exited is the top level of a function,
1527 check over all the labels. */
1530 /* Since this is the top level block of a function, the vars are
1531 the function's parameters. Don't leave them in the BLOCK
1532 because they are found in the FUNCTION_DECL instead. */
1533 BLOCK_VARS (block) = 0;
1537 tmp = current_binding_level->keep;
1539 pop_binding_level ();
1541 DECL_INITIAL (current_function_decl) = block;
1543 current_binding_level->blocks
1544 = chainon (current_binding_level->blocks, block);
1546 /* If we did not make a block for the level just exited,
1547 any blocks made for inner levels
1548 (since they cannot be recorded as subblocks in that level)
1549 must be carried forward so they will later become subblocks
1550 of something else. */
1552 current_binding_level->blocks
1553 = chainon (current_binding_level->blocks, subblocks);
1555 /* Each and every BLOCK node created here in `poplevel' is important
1556 (e.g. for proper debugging information) so if we created one
1557 earlier, mark it as "used". */
1559 TREE_USED (block) = 1;
1561 /* Take care of compiler's internal binding structures. */
1567 = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1570 SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1571 SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1574 block = poplevel (keep, reverse, functionbody);
1580 /* Delete the node BLOCK from the current binding level.
1581 This is used for the block inside a stmt expr ({...})
1582 so that the block can be reinserted where appropriate. */
1585 delete_block (block)
1589 if (current_binding_level->blocks == block)
1590 current_binding_level->blocks = TREE_CHAIN (block);
1591 for (t = current_binding_level->blocks; t;)
1593 if (TREE_CHAIN (t) == block)
1594 TREE_CHAIN (t) = TREE_CHAIN (block);
1598 TREE_CHAIN (block) = NULL_TREE;
1599 /* Clear TREE_USED which is always set by poplevel.
1600 The flag is set again if insert_block is called. */
1601 TREE_USED (block) = 0;
1604 /* Insert BLOCK at the end of the list of subblocks of the
1605 current binding level. This is used when a BIND_EXPR is expanded,
1606 to handle the BLOCK node inside the BIND_EXPR. */
1609 insert_block (block)
1612 TREE_USED (block) = 1;
1613 current_binding_level->blocks
1614 = chainon (current_binding_level->blocks, block);
1617 /* Set the BLOCK node for the innermost scope
1618 (the one we are currently in). */
1622 tree block ATTRIBUTE_UNUSED;
1624 /* The RTL expansion machinery requires us to provide this callback,
1625 but it is not applicable in function-at-a-time mode. */
1626 my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1629 /* Do a pushlevel for class declarations. */
1634 register struct binding_level *newlevel;
1636 /* Reuse or create a struct for this binding level. */
1637 #if defined(DEBUG_CP_BINDING_LEVELS)
1639 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1640 if (free_binding_level)
1641 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1643 newlevel = free_binding_level;
1644 free_binding_level = free_binding_level->level_chain;
1647 newlevel = make_binding_level ();
1649 #if defined(DEBUG_CP_BINDING_LEVELS)
1651 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1653 push_binding_level (newlevel, 0, 0);
1655 class_binding_level = current_binding_level;
1656 class_binding_level->parm_flag = 2;
1657 class_binding_level->this_class = current_class_type;
1660 /* ...and a poplevel for class declarations. */
1665 register struct binding_level *level = class_binding_level;
1668 my_friendly_assert (level != 0, 354);
1670 /* If we're leaving a toplevel class, don't bother to do the setting
1671 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1672 shouldn't even be used when current_class_type isn't set, and second,
1673 if we don't touch it here, we're able to use the cache effect if the
1674 next time we're entering a class scope, it is the same class. */
1675 if (current_class_depth != 1)
1677 struct binding_level* b;
1679 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1680 for (shadowed = level->class_shadowed;
1682 shadowed = TREE_CHAIN (shadowed))
1683 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1685 /* Find the next enclosing class, and recreate
1686 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1687 b = level->level_chain;
1688 while (b && b->parm_flag != 2)
1692 for (shadowed = b->class_shadowed;
1694 shadowed = TREE_CHAIN (shadowed))
1698 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1699 while (t && BINDING_LEVEL (t) != b)
1703 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1704 = BINDING_VALUE (t);
1708 /* Remember to save what IDENTIFIER's were bound in this scope so we
1709 can recover from cache misses. */
1711 previous_class_type = current_class_type;
1712 previous_class_values = class_binding_level->class_shadowed;
1714 for (shadowed = level->type_shadowed;
1716 shadowed = TREE_CHAIN (shadowed))
1717 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1719 /* Remove the bindings for all of the class-level declarations. */
1720 for (shadowed = level->class_shadowed;
1722 shadowed = TREE_CHAIN (shadowed))
1723 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1725 /* Now, pop out of the binding level which we created up in the
1726 `pushlevel_class' routine. */
1727 #if defined(DEBUG_CP_BINDING_LEVELS)
1729 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1731 pop_binding_level ();
1734 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1735 for any names in enclosing classes. */
1738 clear_identifier_class_values ()
1742 if (!class_binding_level)
1745 for (t = class_binding_level->class_shadowed;
1748 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1751 /* Returns non-zero if T is a virtual function table. */
1754 vtable_decl_p (t, data)
1756 void *data ATTRIBUTE_UNUSED;
1758 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1761 /* Returns non-zero if T is a TYPE_DECL for a type with virtual
1765 vtype_decl_p (t, data)
1767 void *data ATTRIBUTE_UNUSED;
1769 return (TREE_CODE (t) == TYPE_DECL
1770 && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1771 && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1774 /* Return the declarations that are members of the namespace NS. */
1777 cp_namespace_decls (ns)
1780 return NAMESPACE_LEVEL (ns)->names;
1783 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1784 itself, calling F for each. The DATA is passed to F as well. */
1787 walk_namespaces_r (namespace, f, data)
1789 walk_namespaces_fn f;
1795 result |= (*f) (namespace, data);
1797 for (current = cp_namespace_decls (namespace);
1799 current = TREE_CHAIN (current))
1801 if (TREE_CODE (current) != NAMESPACE_DECL
1802 || DECL_NAMESPACE_ALIAS (current))
1805 /* We found a namespace. */
1806 result |= walk_namespaces_r (current, f, data);
1812 /* Walk all the namespaces, calling F for each. The DATA is passed to
1816 walk_namespaces (f, data)
1817 walk_namespaces_fn f;
1820 return walk_namespaces_r (global_namespace, f, data);
1823 struct walk_globals_data {
1824 walk_globals_pred p;
1829 /* Walk the global declarations in NAMESPACE. Whenever one is found
1830 for which P returns non-zero, call F with its address. If any call
1831 to F returns a non-zero value, return a non-zero value. */
1834 walk_globals_r (namespace, data)
1838 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1839 walk_globals_pred p = wgd->p;
1840 walk_globals_fn f = wgd->f;
1841 void *d = wgd->data;
1845 t = &NAMESPACE_LEVEL (namespace)->names;
1852 result |= (*f) (t, d);
1854 /* If F changed *T, then *T still points at the next item to
1857 t = &TREE_CHAIN (*t);
1863 /* Walk the global declarations. Whenever one is found for which P
1864 returns non-zero, call F with its address. If any call to F
1865 returns a non-zero value, return a non-zero value. */
1868 walk_globals (p, f, data)
1869 walk_globals_pred p;
1873 struct walk_globals_data wgd;
1878 return walk_namespaces (walk_globals_r, &wgd);
1881 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1882 DATA is non-NULL, this is the last time we will call
1883 wrapup_global_declarations for this NAMESPACE. */
1886 wrapup_globals_for_namespace (namespace, data)
1890 tree globals = cp_namespace_decls (namespace);
1891 int len = list_length (globals);
1892 tree *vec = (tree *) alloca (sizeof (tree) * len);
1896 int last_time = (data != 0);
1898 if (last_time && namespace == global_namespace)
1899 /* Let compile_file handle the global namespace. */
1902 /* Process the decls in reverse order--earliest first.
1903 Put them into VEC from back to front, then take out from front. */
1904 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1905 vec[len - i - 1] = decl;
1909 check_global_declarations (vec, len);
1913 /* Temporarily mark vtables as external. That prevents
1914 wrapup_global_declarations from writing them out; we must process
1915 them ourselves in finish_vtable_vardecl. */
1916 for (i = 0; i < len; ++i)
1917 if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
1919 DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
1920 DECL_EXTERNAL (vec[i]) = 1;
1923 /* Write out any globals that need to be output. */
1924 result = wrapup_global_declarations (vec, len);
1926 /* Undo the hack to DECL_EXTERNAL above. */
1927 for (i = 0; i < len; ++i)
1928 if (vtable_decl_p (vec[i], /*data=*/0)
1929 && DECL_NOT_REALLY_EXTERN (vec[i]))
1931 DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
1932 DECL_EXTERNAL (vec[i]) = 0;
1939 /* Mark ARG (which is really a struct binding_level **) for GC. */
1942 mark_binding_level (arg)
1945 struct binding_level *lvl = *(struct binding_level **)arg;
1947 for (; lvl; lvl = lvl->level_chain)
1949 ggc_mark_tree (lvl->names);
1950 ggc_mark_tree (lvl->tags);
1951 ggc_mark_tree (lvl->usings);
1952 ggc_mark_tree (lvl->using_directives);
1953 ggc_mark_tree (lvl->class_shadowed);
1954 ggc_mark_tree (lvl->type_shadowed);
1955 ggc_mark_tree (lvl->shadowed_labels);
1956 ggc_mark_tree (lvl->blocks);
1957 ggc_mark_tree (lvl->this_class);
1958 ggc_mark_tree (lvl->incomplete);
1959 ggc_mark_tree (lvl->dead_vars_from_for);
1964 mark_named_label_lists (labs, uses)
1968 struct named_label_list *l = *(struct named_label_list **)labs;
1969 struct named_label_use_list *u = *(struct named_label_use_list **)uses;
1971 for (; l; l = l->next)
1974 mark_binding_level (l->binding_level);
1975 ggc_mark_tree (l->old_value);
1976 ggc_mark_tree (l->label_decl);
1977 ggc_mark_tree (l->bad_decls);
1980 for (; u; u = u->next)
1984 /* For debugging. */
1985 static int no_print_functions = 0;
1986 static int no_print_builtins = 0;
1989 print_binding_level (lvl)
1990 struct binding_level *lvl;
1994 fprintf (stderr, " blocks=");
1995 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1996 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
1997 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
1998 if (lvl->tag_transparent)
1999 fprintf (stderr, " tag-transparent");
2000 if (lvl->more_cleanups_ok)
2001 fprintf (stderr, " more-cleanups-ok");
2002 if (lvl->have_cleanups)
2003 fprintf (stderr, " have-cleanups");
2004 fprintf (stderr, "\n");
2007 fprintf (stderr, " names:\t");
2008 /* We can probably fit 3 names to a line? */
2009 for (t = lvl->names; t; t = TREE_CHAIN (t))
2011 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
2013 if (no_print_builtins
2014 && (TREE_CODE (t) == TYPE_DECL)
2015 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
2018 /* Function decls tend to have longer names. */
2019 if (TREE_CODE (t) == FUNCTION_DECL)
2026 fprintf (stderr, "\n\t");
2029 print_node_brief (stderr, "", t, 0);
2030 if (t == error_mark_node)
2034 fprintf (stderr, "\n");
2038 fprintf (stderr, " tags:\t");
2040 for (t = lvl->tags; t; t = TREE_CHAIN (t))
2042 if (TREE_PURPOSE (t) == NULL_TREE)
2044 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2051 fprintf (stderr, "\n\t");
2054 if (TREE_PURPOSE (t) == NULL_TREE)
2056 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
2057 fprintf (stderr, ">");
2059 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2060 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2063 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
2064 print_node_brief (stderr, "", TREE_VALUE (t), 0);
2065 fprintf (stderr, ">");
2069 fprintf (stderr, "\n");
2071 if (lvl->class_shadowed)
2073 fprintf (stderr, " class-shadowed:");
2074 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
2076 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2078 fprintf (stderr, "\n");
2080 if (lvl->type_shadowed)
2082 fprintf (stderr, " type-shadowed:");
2083 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2085 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2087 fprintf (stderr, "\n");
2092 print_other_binding_stack (stack)
2093 struct binding_level *stack;
2095 struct binding_level *level;
2096 for (level = stack; level != global_binding_level; level = level->level_chain)
2098 fprintf (stderr, "binding level ");
2099 fprintf (stderr, HOST_PTR_PRINTF, level);
2100 fprintf (stderr, "\n");
2101 print_binding_level (level);
2106 print_binding_stack ()
2108 struct binding_level *b;
2109 fprintf (stderr, "current_binding_level=");
2110 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2111 fprintf (stderr, "\nclass_binding_level=");
2112 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2113 fprintf (stderr, "\nglobal_binding_level=");
2114 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2115 fprintf (stderr, "\n");
2116 if (class_binding_level)
2118 for (b = class_binding_level; b; b = b->level_chain)
2119 if (b == current_binding_level)
2122 b = class_binding_level;
2124 b = current_binding_level;
2127 b = current_binding_level;
2128 print_other_binding_stack (b);
2129 fprintf (stderr, "global:\n");
2130 print_binding_level (global_binding_level);
2133 /* Namespace binding access routines: The namespace_bindings field of
2134 the identifier is polymorphic, with three possible values:
2135 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2136 indicating the BINDING_VALUE of global_namespace. */
2138 /* Check whether the a binding for the name to scope is known.
2139 Assumes that the bindings of the name are already a list
2140 of bindings. Returns the binding found, or NULL_TREE. */
2143 find_binding (name, scope)
2147 tree iter, prev = NULL_TREE;
2149 scope = ORIGINAL_NAMESPACE (scope);
2151 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2152 iter = TREE_CHAIN (iter))
2154 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2155 if (BINDING_SCOPE (iter) == scope)
2157 /* Move binding found to the front of the list, so
2158 subsequent lookups will find it faster. */
2161 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2162 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2163 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2172 /* Always returns a binding for name in scope. If the
2173 namespace_bindings is not a list, convert it to one first.
2174 If no binding is found, make a new one. */
2177 binding_for_name (name, scope)
2181 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2184 scope = ORIGINAL_NAMESPACE (scope);
2186 if (b && TREE_CODE (b) != CPLUS_BINDING)
2188 /* Get rid of optimization for global scope. */
2189 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2190 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2191 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2193 if (b && (result = find_binding (name, scope)))
2195 /* Not found, make a new one. */
2196 result = make_node (CPLUS_BINDING);
2197 TREE_CHAIN (result) = b;
2198 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2199 BINDING_SCOPE (result) = scope;
2200 BINDING_TYPE (result) = NULL_TREE;
2201 BINDING_VALUE (result) = NULL_TREE;
2205 /* Return the binding value for name in scope, considering that
2206 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2209 namespace_binding (name, scope)
2213 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2216 if (scope == NULL_TREE)
2217 scope = global_namespace;
2218 if (TREE_CODE (b) != CPLUS_BINDING)
2219 return (scope == global_namespace) ? b : NULL_TREE;
2220 name = find_binding (name,scope);
2221 if (name == NULL_TREE)
2223 return BINDING_VALUE (name);
2226 /* Set the binding value for name in scope. If modifying the binding
2227 of global_namespace is attempted, try to optimize it. */
2230 set_namespace_binding (name, scope, val)
2237 if (scope == NULL_TREE)
2238 scope = global_namespace;
2240 if (scope == global_namespace)
2242 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2243 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2245 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2249 b = binding_for_name (name, scope);
2250 BINDING_VALUE (b) = val;
2253 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2254 select a name that is unique to this compilation unit. */
2257 push_namespace (name)
2262 int implicit_use = 0;
2264 if (!global_namespace)
2266 /* This must be ::. */
2267 my_friendly_assert (name == get_identifier ("::"), 377);
2272 /* The name of anonymous namespace is unique for the translation
2274 if (!anonymous_namespace_name)
2275 anonymous_namespace_name = get_file_function_name ('N');
2276 name = anonymous_namespace_name;
2277 d = IDENTIFIER_NAMESPACE_VALUE (name);
2279 /* Reopening anonymous namespace. */
2285 /* Check whether this is an extended namespace definition. */
2286 d = IDENTIFIER_NAMESPACE_VALUE (name);
2287 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2290 if (DECL_NAMESPACE_ALIAS (d))
2292 error ("namespace alias `%D' not allowed here, assuming `%D'",
2293 d, DECL_NAMESPACE_ALIAS (d));
2294 d = DECL_NAMESPACE_ALIAS (d);
2301 /* Make a new namespace, binding the name to it. */
2302 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2303 /* The global namespace is not pushed, and the global binding
2304 level is set elsewhere. */
2307 DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2310 declare_namespace_level ();
2311 NAMESPACE_LEVEL (d) = current_binding_level;
2315 resume_binding_level (NAMESPACE_LEVEL (d));
2318 do_using_directive (d);
2319 /* Enter the name space. */
2320 current_namespace = d;
2323 /* Pop from the scope of the current namespace. */
2328 my_friendly_assert (current_namespace != global_namespace, 20010801);
2329 current_namespace = CP_DECL_CONTEXT (current_namespace);
2330 /* The binding level is not popped, as it might be re-opened later. */
2331 suspend_binding_level ();
2334 /* Push into the scope of the namespace NS, even if it is deeply
2335 nested within another namespace. */
2338 push_nested_namespace (ns)
2341 if (ns == global_namespace)
2342 push_to_top_level ();
2345 push_nested_namespace (CP_DECL_CONTEXT (ns));
2346 push_namespace (DECL_NAME (ns));
2350 /* Pop back from the scope of the namespace NS, which was previously
2351 entered with push_nested_namespace. */
2354 pop_nested_namespace (ns)
2357 while (ns != global_namespace)
2360 ns = CP_DECL_CONTEXT (ns);
2363 pop_from_top_level ();
2367 /* Subroutines for reverting temporarily to top-level for instantiation
2368 of templates and such. We actually need to clear out the class- and
2369 local-value slots of all identifiers, so that only the global values
2370 are at all visible. Simply setting current_binding_level to the global
2371 scope isn't enough, because more binding levels may be pushed. */
2372 struct saved_scope *scope_chain;
2374 /* Mark ARG (which is really a struct saved_scope **) for GC. */
2377 mark_saved_scope (arg)
2380 struct saved_scope *t = *(struct saved_scope **)arg;
2383 mark_binding_level (&t->class_bindings);
2384 ggc_mark_tree (t->old_bindings);
2385 ggc_mark_tree (t->old_namespace);
2386 ggc_mark_tree (t->decl_ns_list);
2387 ggc_mark_tree (t->class_name);
2388 ggc_mark_tree (t->class_type);
2389 ggc_mark_tree (t->access_specifier);
2390 ggc_mark_tree (t->function_decl);
2392 ggc_mark_tree_varray (t->lang_base);
2393 ggc_mark_tree (t->lang_name);
2394 ggc_mark_tree (t->template_parms);
2395 ggc_mark_tree (t->x_previous_class_type);
2396 ggc_mark_tree (t->x_previous_class_values);
2397 ggc_mark_tree (t->x_saved_tree);
2398 ggc_mark_tree (t->incomplete);
2399 ggc_mark_tree (t->lookups);
2401 mark_stmt_tree (&t->x_stmt_tree);
2402 mark_binding_level (&t->bindings);
2408 store_bindings (names, old_bindings)
2409 tree names, old_bindings;
2412 tree search_bindings = old_bindings;
2414 for (t = names; t; t = TREE_CHAIN (t))
2416 tree binding, t1, id;
2418 if (TREE_CODE (t) == TREE_LIST)
2419 id = TREE_PURPOSE (t);
2424 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2425 we have no IDENTIFIER_BINDING if we have left the class
2426 scope, but cached the class-level declarations. */
2427 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2430 for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1))
2431 if (TREE_VEC_ELT (t1, 0) == id)
2434 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2435 binding = make_tree_vec (4);
2436 TREE_VEC_ELT (binding, 0) = id;
2437 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2438 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2439 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2440 IDENTIFIER_BINDING (id) = NULL_TREE;
2441 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2442 TREE_CHAIN (binding) = old_bindings;
2443 old_bindings = binding;
2447 return old_bindings;
2451 maybe_push_to_top_level (pseudo)
2454 struct saved_scope *s;
2455 struct binding_level *b;
2459 s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope));
2461 b = scope_chain ? current_binding_level : 0;
2463 /* If we're in the middle of some function, save our state. */
2467 push_function_context_to (NULL_TREE);
2472 old_bindings = NULL_TREE;
2473 if (scope_chain && previous_class_type)
2474 old_bindings = store_bindings (previous_class_values, old_bindings);
2476 /* Have to include global_binding_level, because class-level decls
2477 aren't listed anywhere useful. */
2478 for (; b; b = b->level_chain)
2482 /* Template IDs are inserted into the global level. If they were
2483 inserted into namespace level, finish_file wouldn't find them
2484 when doing pending instantiations. Therefore, don't stop at
2485 namespace level, but continue until :: . */
2486 if (b == global_binding_level || (pseudo && b->template_parms_p))
2489 old_bindings = store_bindings (b->names, old_bindings);
2490 /* We also need to check class_shadowed to save class-level type
2491 bindings, since pushclass doesn't fill in b->names. */
2492 if (b->parm_flag == 2)
2493 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2495 /* Unwind type-value slots back to top level. */
2496 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2497 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2499 s->prev = scope_chain;
2500 s->old_bindings = old_bindings;
2502 s->need_pop_function_context = need_pop;
2503 s->function_decl = current_function_decl;
2506 current_function_decl = NULL_TREE;
2507 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2508 current_lang_name = lang_name_cplusplus;
2509 current_namespace = global_namespace;
2513 push_to_top_level ()
2515 maybe_push_to_top_level (0);
2519 pop_from_top_level ()
2521 struct saved_scope *s = scope_chain;
2524 /* Clear out class-level bindings cache. */
2525 if (previous_class_type)
2526 invalidate_class_lookup_cache ();
2528 VARRAY_FREE (current_lang_base);
2530 scope_chain = s->prev;
2531 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
2533 tree id = TREE_VEC_ELT (t, 0);
2535 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2536 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2537 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2540 /* If we were in the middle of compiling a function, restore our
2542 if (s->need_pop_function_context)
2543 pop_function_context_from (NULL_TREE);
2544 current_function_decl = s->function_decl;
2549 /* Push a definition of struct, union or enum tag "name".
2550 into binding_level "b". "type" should be the type node,
2551 We assume that the tag "name" is not already defined.
2553 Note that the definition may really be just a forward reference.
2554 In that case, the TYPE_SIZE will be a NULL_TREE.
2556 C++ gratuitously puts all these tags in the name space. */
2558 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2559 record the shadowed value for this binding contour. TYPE is
2560 the type that ID maps to. */
2563 set_identifier_type_value_with_scope (id, type, b)
2566 struct binding_level *b;
2568 if (!b->namespace_p)
2570 /* Shadow the marker, not the real thing, so that the marker
2571 gets restored later. */
2572 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2574 = tree_cons (id, old_type_value, b->type_shadowed);
2578 tree binding = binding_for_name (id, current_namespace);
2579 BINDING_TYPE (binding) = type;
2580 /* Store marker instead of real type. */
2581 type = global_type_node;
2583 SET_IDENTIFIER_TYPE_VALUE (id, type);
2586 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2589 set_identifier_type_value (id, type)
2593 set_identifier_type_value_with_scope (id, type, current_binding_level);
2596 /* Return the type associated with id. */
2599 identifier_type_value (id)
2602 /* There is no type with that name, anywhere. */
2603 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2605 /* This is not the type marker, but the real thing. */
2606 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2607 return REAL_IDENTIFIER_TYPE_VALUE (id);
2608 /* Have to search for it. It must be on the global level, now.
2609 Ask lookup_name not to return non-types. */
2610 id = lookup_name_real (id, 2, 1, 0);
2612 return TREE_TYPE (id);
2616 /* Pop off extraneous binding levels left over due to syntax errors.
2618 We don't pop past namespaces, as they might be valid. */
2623 #ifdef DEBUG_CP_BINDING_LEVELS
2624 fprintf (stderr, "XXX entering pop_everything ()\n");
2626 while (!toplevel_bindings_p ())
2628 if (current_binding_level->parm_flag == 2)
2629 pop_nested_class ();
2633 #ifdef DEBUG_CP_BINDING_LEVELS
2634 fprintf (stderr, "XXX leaving pop_everything ()\n");
2638 /* The type TYPE is being declared. If it is a class template, or a
2639 specialization of a class template, do any processing required and
2640 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2641 being declared a friend. B is the binding level at which this TYPE
2644 Returns the TYPE_DECL for TYPE, which may have been altered by this
2648 maybe_process_template_type_declaration (type, globalize, b)
2651 struct binding_level* b;
2653 tree decl = TYPE_NAME (type);
2655 if (processing_template_parmlist)
2656 /* You can't declare a new template type in a template parameter
2657 list. But, you can declare a non-template type:
2659 template <class A*> struct S;
2661 is a forward-declaration of `A'. */
2665 maybe_check_template_type (type);
2667 my_friendly_assert (IS_AGGR_TYPE (type)
2668 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2671 if (processing_template_decl)
2673 /* This may change after the call to
2674 push_template_decl_real, but we want the original value. */
2675 tree name = DECL_NAME (decl);
2677 decl = push_template_decl_real (decl, globalize);
2678 /* If the current binding level is the binding level for the
2679 template parameters (see the comment in
2680 begin_template_parm_list) and the enclosing level is a class
2681 scope, and we're not looking at a friend, push the
2682 declaration of the member class into the class scope. In the
2683 friend case, push_template_decl will already have put the
2684 friend into global scope, if appropriate. */
2685 if (TREE_CODE (type) != ENUMERAL_TYPE
2686 && !globalize && b->template_parms_p
2687 && b->level_chain->parm_flag == 2)
2689 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2690 /* Put this tag on the list of tags for the class, since
2691 that won't happen below because B is not the class
2692 binding level, but is instead the pseudo-global level. */
2693 b->level_chain->tags =
2694 tree_cons (name, type, b->level_chain->tags);
2695 if (!COMPLETE_TYPE_P (current_class_type))
2696 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2704 /* In C++, you don't have to write `struct S' to refer to `S'; you
2705 can just use `S'. We accomplish this by creating a TYPE_DECL as
2706 if the user had written `typedef struct S S'. Create and return
2707 the TYPE_DECL for TYPE. */
2710 create_implicit_typedef (name, type)
2716 decl = build_decl (TYPE_DECL, name, type);
2717 DECL_ARTIFICIAL (decl) = 1;
2718 /* There are other implicit type declarations, like the one *within*
2719 a class that allows you to write `S::S'. We must distinguish
2721 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2722 TYPE_NAME (type) = decl;
2727 /* Remember a local name for name-mangling purposes. */
2730 push_local_name (decl)
2737 VARRAY_TREE_INIT (local_names, 8, "local_names");
2739 name = DECL_NAME (decl);
2741 nelts = VARRAY_ACTIVE_SIZE (local_names);
2742 for (i = 0; i < nelts; i++)
2744 t = VARRAY_TREE (local_names, i);
2745 if (DECL_NAME (t) == name)
2747 if (!DECL_LANG_SPECIFIC (decl))
2748 retrofit_lang_decl (decl);
2749 if (DECL_LANG_SPECIFIC (t))
2750 DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2752 DECL_DISCRIMINATOR (decl) = 1;
2754 VARRAY_TREE (local_names, i) = decl;
2759 VARRAY_PUSH_TREE (local_names, decl);
2762 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2763 Normally put it into the inner-most non-tag-transparent scope,
2764 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2765 The latter is needed for implicit declarations. */
2768 pushtag (name, type, globalize)
2772 register struct binding_level *b;
2774 b = current_binding_level;
2775 while (b->tag_transparent
2776 || (b->parm_flag == 2
2778 /* We may be defining a new type in the initializer
2779 of a static member variable. We allow this when
2780 not pedantic, and it is particularly useful for
2781 type punning via an anonymous union. */
2782 || COMPLETE_TYPE_P (b->this_class))))
2785 b->tags = tree_cons (name, type, b->tags);
2789 /* Do C++ gratuitous typedefing. */
2790 if (IDENTIFIER_TYPE_VALUE (name) != type)
2792 register tree d = NULL_TREE;
2794 tree context = TYPE_CONTEXT (type);
2798 tree cs = current_scope ();
2802 else if (cs != NULL_TREE && TYPE_P (cs))
2803 /* When declaring a friend class of a local class, we want
2804 to inject the newly named class into the scope
2805 containing the local class, not the namespace scope. */
2806 context = decl_function_context (get_type_decl (cs));
2809 context = current_namespace;
2811 if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2812 || b->parm_flag == 2)
2815 if (current_lang_name == lang_name_java)
2816 TYPE_FOR_JAVA (type) = 1;
2818 d = create_implicit_typedef (name, type);
2819 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2821 set_identifier_type_value_with_scope (name, type, b);
2823 d = maybe_process_template_type_declaration (type,
2826 if (b->parm_flag == 2)
2828 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2829 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2830 class. But if it's a member template class, we
2831 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2833 finish_member_declaration (d);
2835 pushdecl_class_level (d);
2838 d = pushdecl_with_scope (d, b);
2840 /* FIXME what if it gets a name from typedef? */
2841 if (ANON_AGGRNAME_P (name))
2842 DECL_IGNORED_P (d) = 1;
2844 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2846 /* If this is a local class, keep track of it. We need this
2847 information for name-mangling, and so that it is possible to find
2848 all function definitions in a translation unit in a convenient
2849 way. (It's otherwise tricky to find a member function definition
2850 it's only pointed to from within a local class.) */
2851 if (TYPE_CONTEXT (type)
2852 && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2853 && !processing_template_decl)
2854 VARRAY_PUSH_TREE (local_classes, type);
2856 if (b->parm_flag == 2)
2858 if (!COMPLETE_TYPE_P (current_class_type))
2859 CLASSTYPE_TAGS (current_class_type) = b->tags;
2863 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2864 /* Use the canonical TYPE_DECL for this node. */
2865 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2868 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2869 will be the tagged type we just added to the current
2870 binding level. This fake NULL-named TYPE_DECL node helps
2871 dwarfout.c to know when it needs to output a
2872 representation of a tagged type, and it also gives us a
2873 convenient place to record the "scope start" address for
2876 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2877 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2881 /* Counter used to create anonymous type names. */
2883 static int anon_cnt = 0;
2885 /* Return an IDENTIFIER which can be used as a name for
2886 anonymous structs and unions. */
2893 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2894 return get_identifier (buf);
2897 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2898 This keeps dbxout from getting confused. */
2903 register struct binding_level *b;
2905 static int last_cnt = 0;
2907 /* Fast out if no new anon names were declared. */
2908 if (last_cnt == anon_cnt)
2911 b = current_binding_level;
2912 while (b->tag_transparent)
2917 /* A NULL purpose means we have already processed all tags
2918 from here to the end of the list. */
2919 if (TREE_PURPOSE (tags) == NULL_TREE)
2921 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2922 TREE_PURPOSE (tags) = NULL_TREE;
2923 tags = TREE_CHAIN (tags);
2925 last_cnt = anon_cnt;
2928 /* Subroutine of duplicate_decls: return truthvalue of whether
2929 or not types of these decls match.
2931 For C++, we must compare the parameter list so that `int' can match
2932 `int&' in a parameter position, but `int&' is not confused with
2936 decls_match (newdecl, olddecl)
2937 tree newdecl, olddecl;
2941 if (newdecl == olddecl)
2944 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2945 /* If the two DECLs are not even the same kind of thing, we're not
2946 interested in their types. */
2949 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2951 tree f1 = TREE_TYPE (newdecl);
2952 tree f2 = TREE_TYPE (olddecl);
2953 tree p1 = TYPE_ARG_TYPES (f1);
2954 tree p2 = TYPE_ARG_TYPES (f2);
2956 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2957 && ! (DECL_EXTERN_C_P (newdecl)
2958 && DECL_EXTERN_C_P (olddecl)))
2961 if (TREE_CODE (f1) != TREE_CODE (f2))
2964 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2966 if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2967 && (DECL_BUILT_IN (olddecl)
2968 #ifndef NO_IMPLICIT_EXTERN_C
2969 || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2970 || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2974 types_match = self_promoting_args_p (p1);
2975 if (p1 == void_list_node)
2976 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2978 #ifndef NO_IMPLICIT_EXTERN_C
2979 else if (p1 == NULL_TREE
2980 && (DECL_EXTERN_C_P (olddecl)
2981 && DECL_IN_SYSTEM_HEADER (olddecl)
2982 && !DECL_CLASS_SCOPE_P (olddecl))
2983 && (DECL_EXTERN_C_P (newdecl)
2984 && DECL_IN_SYSTEM_HEADER (newdecl)
2985 && !DECL_CLASS_SCOPE_P (newdecl)))
2987 types_match = self_promoting_args_p (p2);
2988 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2992 types_match = compparms (p1, p2);
2997 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2999 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3000 DECL_TEMPLATE_PARMS (olddecl)))
3003 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
3004 != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
3007 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3010 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
3011 DECL_TEMPLATE_RESULT (newdecl));
3015 if (TREE_TYPE (newdecl) == error_mark_node)
3016 types_match = TREE_TYPE (olddecl) == error_mark_node;
3017 else if (TREE_TYPE (olddecl) == NULL_TREE)
3018 types_match = TREE_TYPE (newdecl) == NULL_TREE;
3019 else if (TREE_TYPE (newdecl) == NULL_TREE)
3022 types_match = comptypes (TREE_TYPE (newdecl),
3023 TREE_TYPE (olddecl),
3024 COMPARE_REDECLARATION);
3030 /* If NEWDECL is `static' and an `extern' was seen previously,
3031 warn about it. OLDDECL is the previous declaration.
3033 Note that this does not apply to the C++ case of declaring
3034 a variable `extern const' and then later `const'.
3036 Don't complain about built-in functions, since they are beyond
3037 the user's control. */
3040 warn_extern_redeclared_static (newdecl, olddecl)
3041 tree newdecl, olddecl;
3043 static const char *const explicit_extern_static_warning
3044 = "`%D' was declared `extern' and later `static'";
3045 static const char *const implicit_extern_static_warning
3046 = "`%D' was declared implicitly `extern' and later `static'";
3050 if (TREE_CODE (newdecl) == TYPE_DECL
3051 || TREE_CODE (newdecl) == TEMPLATE_DECL
3052 || TREE_CODE (newdecl) == CONST_DECL)
3055 /* Don't get confused by static member functions; that's a different
3057 if (TREE_CODE (newdecl) == FUNCTION_DECL
3058 && DECL_STATIC_FUNCTION_P (newdecl))
3061 /* If the old declaration was `static', or the new one isn't, then
3062 then everything is OK. */
3063 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
3066 /* It's OK to declare a builtin function as `static'. */
3067 if (TREE_CODE (olddecl) == FUNCTION_DECL
3068 && DECL_ARTIFICIAL (olddecl))
3071 name = DECL_ASSEMBLER_NAME (newdecl);
3072 pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
3073 ? implicit_extern_static_warning
3074 : explicit_extern_static_warning, newdecl);
3075 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
3078 /* Handle when a new declaration NEWDECL has the same name as an old
3079 one OLDDECL in the same binding contour. Prints an error message
3082 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
3083 Otherwise, return 0. */
3086 duplicate_decls (newdecl, olddecl)
3087 tree newdecl, olddecl;
3089 unsigned olddecl_uid = DECL_UID (olddecl);
3090 int olddecl_friend = 0, types_match = 0;
3091 int new_defines_function = 0;
3093 if (newdecl == olddecl)
3096 types_match = decls_match (newdecl, olddecl);
3098 /* If either the type of the new decl or the type of the old decl is an
3099 error_mark_node, then that implies that we have already issued an
3100 error (earlier) for some bogus type specification, and in that case,
3101 it is rather pointless to harass the user with yet more error message
3102 about the same declaration, so just pretend the types match here. */
3103 if (TREE_TYPE (newdecl) == error_mark_node
3104 || TREE_TYPE (olddecl) == error_mark_node)
3107 if (DECL_P (olddecl)
3108 && TREE_CODE (newdecl) == FUNCTION_DECL
3109 && TREE_CODE (olddecl) == FUNCTION_DECL
3110 && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
3112 if (DECL_DECLARED_INLINE_P (newdecl)
3113 && DECL_UNINLINABLE (newdecl)
3114 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3115 /* Already warned elsewhere. */;
3116 else if (DECL_DECLARED_INLINE_P (olddecl)
3117 && DECL_UNINLINABLE (olddecl)
3118 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3119 /* Already warned. */;
3120 else if (DECL_DECLARED_INLINE_P (newdecl)
3121 && DECL_UNINLINABLE (olddecl)
3122 && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3124 warning_with_decl (newdecl,
3125 "function `%s' redeclared as inline");
3126 warning_with_decl (olddecl,
3127 "previous declaration of function `%s' with attribute noinline");
3129 else if (DECL_DECLARED_INLINE_P (olddecl)
3130 && DECL_UNINLINABLE (newdecl)
3131 && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3133 warning_with_decl (newdecl,
3134 "function `%s' redeclared with attribute noinline");
3135 warning_with_decl (olddecl,
3136 "previous declaration of function `%s' was inline");
3140 /* Check for redeclaration and other discrepancies. */
3141 if (TREE_CODE (olddecl) == FUNCTION_DECL
3142 && DECL_ARTIFICIAL (olddecl))
3144 if (TREE_CODE (newdecl) != FUNCTION_DECL)
3146 /* If you declare a built-in or predefined function name as static,
3147 the old definition is overridden, but optionally warn this was a
3148 bad choice of name. */
3149 if (! TREE_PUBLIC (newdecl))
3152 warning ("shadowing %s function `%#D'",
3153 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3155 /* Discard the old built-in function. */
3158 /* If the built-in is not ansi, then programs can override
3159 it even globally without an error. */
3160 else if (! DECL_BUILT_IN (olddecl))
3161 warning ("library function `%#D' redeclared as non-function `%#D'",
3165 error ("declaration of `%#D'", newdecl);
3166 error ("conflicts with built-in declaration `%#D'",
3171 else if (!types_match)
3173 if ((DECL_EXTERN_C_P (newdecl)
3174 && DECL_EXTERN_C_P (olddecl))
3175 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3176 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3178 /* A near match; override the builtin. */
3180 if (TREE_PUBLIC (newdecl))
3182 warning ("new declaration `%#D'", newdecl);
3183 warning ("ambiguates built-in declaration `%#D'",
3186 else if (warn_shadow)
3187 warning ("shadowing %s function `%#D'",
3188 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3192 /* Discard the old built-in function. */
3196 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3198 /* If a builtin function is redeclared as `static', merge
3199 the declarations, but make the original one static. */
3200 DECL_THIS_STATIC (olddecl) = 1;
3201 TREE_PUBLIC (olddecl) = 0;
3203 /* Make the old declaration consistent with the new one so
3204 that all remnants of the builtin-ness of this function
3205 will be banished. */
3206 SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3207 SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3208 COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3209 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (newdecl),
3213 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3215 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3216 && TREE_CODE (newdecl) != TYPE_DECL
3217 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3218 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3219 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3220 && TREE_CODE (olddecl) != TYPE_DECL
3221 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3222 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3225 /* We do nothing special here, because C++ does such nasty
3226 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3227 get shadowed, and know that if we need to find a TYPE_DECL
3228 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3229 slot of the identifier. */
3233 if ((TREE_CODE (newdecl) == FUNCTION_DECL
3234 && DECL_FUNCTION_TEMPLATE_P (olddecl))
3235 || (TREE_CODE (olddecl) == FUNCTION_DECL
3236 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3239 error ("`%#D' redeclared as different kind of symbol", newdecl);
3240 if (TREE_CODE (olddecl) == TREE_LIST)
3241 olddecl = TREE_VALUE (olddecl);
3242 cp_error_at ("previous declaration of `%#D'", olddecl);
3244 /* New decl is completely inconsistent with the old one =>
3245 tell caller to replace the old one. */
3249 else if (!types_match)
3251 if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3252 /* These are certainly not duplicate declarations; they're
3253 from different scopes. */
3256 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3258 /* The name of a class template may not be declared to refer to
3259 any other template, class, function, object, namespace, value,
3260 or type in the same scope. */
3261 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3262 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3264 error ("declaration of template `%#D'", newdecl);
3265 cp_error_at ("conflicts with previous declaration `%#D'",
3268 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3269 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3270 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3271 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3272 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3273 DECL_TEMPLATE_PARMS (olddecl)))
3275 error ("new declaration `%#D'", newdecl);
3276 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3280 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3282 if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3284 error ("declaration of C function `%#D' conflicts with",
3286 cp_error_at ("previous declaration `%#D' here", olddecl);
3288 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3289 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3291 error ("new declaration `%#D'", newdecl);
3292 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3298 /* Already complained about this, so don't do so again. */
3299 else if (current_class_type == NULL_TREE
3300 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3302 error ("conflicting types for `%#D'", newdecl);
3303 cp_error_at ("previous declaration as `%#D'", olddecl);
3306 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3307 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3308 && (!DECL_TEMPLATE_INFO (newdecl)
3309 || (DECL_TI_TEMPLATE (newdecl)
3310 != DECL_TI_TEMPLATE (olddecl))))
3311 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3312 && (!DECL_TEMPLATE_INFO (olddecl)
3313 || (DECL_TI_TEMPLATE (olddecl)
3314 != DECL_TI_TEMPLATE (newdecl))))))
3315 /* It's OK to have a template specialization and a non-template
3316 with the same type, or to have specializations of two
3317 different templates with the same type. Note that if one is a
3318 specialization, and the other is an instantiation of the same
3319 template, that we do not exit at this point. That situation
3320 can occur if we instantiate a template class, and then
3321 specialize one of its methods. This situation is legal, but
3322 the declarations must be merged in the usual way. */
3324 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3325 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3326 && !DECL_USE_TEMPLATE (newdecl))
3327 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3328 && !DECL_USE_TEMPLATE (olddecl))))
3329 /* One of the declarations is a template instantiation, and the
3330 other is not a template at all. That's OK. */
3332 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3333 && DECL_NAMESPACE_ALIAS (newdecl)
3334 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3335 /* Redeclaration of namespace alias, ignore it. */
3339 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3342 error (errmsg, newdecl);
3343 if (DECL_NAME (olddecl) != NULL_TREE)
3344 cp_error_at ((DECL_INITIAL (olddecl)
3345 && namespace_bindings_p ())
3346 ? "`%#D' previously defined here"
3347 : "`%#D' previously declared here", olddecl);
3350 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3351 && DECL_INITIAL (olddecl) != NULL_TREE
3352 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3353 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3355 /* Prototype decl follows defn w/o prototype. */
3356 cp_warning_at ("prototype for `%#D'", newdecl);
3357 cp_warning_at ("follows non-prototype definition here", olddecl);
3359 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3360 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3362 /* extern "C" int foo ();
3363 int foo () { bar (); }
3365 if (current_lang_depth () == 0)
3366 SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3369 cp_error_at ("previous declaration of `%#D' with %L linkage",
3370 olddecl, DECL_LANGUAGE (olddecl));
3371 error ("conflicts with new declaration with %L linkage",
3372 DECL_LANGUAGE (newdecl));
3376 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3378 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3380 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3381 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3384 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3385 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3387 for (; t1 && t1 != void_list_node;
3388 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3389 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3391 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3394 pedwarn ("default argument given for parameter %d of `%#D'",
3396 cp_pedwarn_at ("after previous specification in `%#D'",
3401 error ("default argument given for parameter %d of `%#D'",
3403 cp_error_at ("after previous specification in `%#D'",
3408 if (DECL_DECLARED_INLINE_P (newdecl)
3409 && ! DECL_DECLARED_INLINE_P (olddecl)
3410 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3412 warning ("`%#D' was used before it was declared inline",
3414 cp_warning_at ("previous non-inline declaration here",
3420 /* If new decl is `static' and an `extern' was seen previously,
3422 warn_extern_redeclared_static (newdecl, olddecl);
3424 /* We have committed to returning 1 at this point. */
3425 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3427 /* Now that functions must hold information normally held
3428 by field decls, there is extra work to do so that
3429 declaration information does not get destroyed during
3431 if (DECL_VINDEX (olddecl))
3432 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3433 if (DECL_VIRTUAL_CONTEXT (olddecl))
3434 DECL_VIRTUAL_CONTEXT (newdecl) = DECL_VIRTUAL_CONTEXT (olddecl);
3435 if (DECL_CONTEXT (olddecl))
3436 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3437 if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3438 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3439 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3440 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3441 DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3442 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3443 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3444 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3445 if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3446 SET_OVERLOADED_OPERATOR_CODE
3447 (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3448 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3450 /* Optionally warn about more than one declaration for the same
3451 name, but don't warn about a function declaration followed by a
3453 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3454 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3455 /* Don't warn about extern decl followed by definition. */
3456 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3457 /* Don't warn about friends, let add_friend take care of it. */
3458 && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3460 warning ("redundant redeclaration of `%D' in same scope", newdecl);
3461 cp_warning_at ("previous declaration of `%D'", olddecl);
3465 /* Deal with C++: must preserve virtual function table size. */
3466 if (TREE_CODE (olddecl) == TYPE_DECL)
3468 register tree newtype = TREE_TYPE (newdecl);
3469 register tree oldtype = TREE_TYPE (olddecl);
3471 if (newtype != error_mark_node && oldtype != error_mark_node
3472 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3474 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3475 CLASSTYPE_FRIEND_CLASSES (newtype)
3476 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3479 DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3482 /* Copy all the DECL_... slots specified in the new decl
3483 except for any that we copy here from the old type. */
3484 DECL_ATTRIBUTES (newdecl)
3485 = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3486 decl_attributes (&newdecl, DECL_ATTRIBUTES (newdecl), 0);
3488 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3490 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3491 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3492 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3493 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3495 /* If the new declaration is a definition, update the file and
3496 line information on the declaration. */
3497 if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3498 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3500 DECL_SOURCE_LINE (olddecl)
3501 = DECL_SOURCE_LINE (DECL_TEMPLATE_RESULT (olddecl))
3502 = DECL_SOURCE_LINE (newdecl);
3503 DECL_SOURCE_FILE (olddecl)
3504 = DECL_SOURCE_FILE (DECL_TEMPLATE_RESULT (olddecl))
3505 = DECL_SOURCE_FILE (newdecl);
3513 /* Automatically handles default parameters. */
3514 tree oldtype = TREE_TYPE (olddecl);
3517 /* Merge the data types specified in the two decls. */
3518 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3520 /* If common_type produces a non-typedef type, just use the old type. */
3521 if (TREE_CODE (newdecl) == TYPE_DECL
3522 && newtype == DECL_ORIGINAL_TYPE (newdecl))
3525 if (TREE_CODE (newdecl) == VAR_DECL)
3526 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3527 /* Do this after calling `common_type' so that default
3528 parameters don't confuse us. */
3529 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3530 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3531 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3533 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3534 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3535 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3536 TYPE_RAISES_EXCEPTIONS (oldtype));
3538 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3539 && DECL_SOURCE_LINE (olddecl) != 0
3541 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3542 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3544 error ("declaration of `%F' throws different exceptions",
3546 cp_error_at ("than previous declaration `%F'", olddecl);
3549 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3551 /* Lay the type out, unless already done. */
3552 if (! same_type_p (newtype, oldtype)
3553 && TREE_TYPE (newdecl) != error_mark_node
3554 && !(processing_template_decl && uses_template_parms (newdecl)))
3555 layout_type (TREE_TYPE (newdecl));
3557 if ((TREE_CODE (newdecl) == VAR_DECL
3558 || TREE_CODE (newdecl) == PARM_DECL
3559 || TREE_CODE (newdecl) == RESULT_DECL
3560 || TREE_CODE (newdecl) == FIELD_DECL
3561 || TREE_CODE (newdecl) == TYPE_DECL)
3562 && !(processing_template_decl && uses_template_parms (newdecl)))
3563 layout_decl (newdecl, 0);
3565 /* Merge the type qualifiers. */
3566 if (TREE_READONLY (newdecl))
3567 TREE_READONLY (olddecl) = 1;
3568 if (TREE_THIS_VOLATILE (newdecl))
3569 TREE_THIS_VOLATILE (olddecl) = 1;
3571 /* Merge the initialization information. */
3572 if (DECL_INITIAL (newdecl) == NULL_TREE
3573 && DECL_INITIAL (olddecl) != NULL_TREE)
3575 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3576 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3577 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
3578 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3579 && DECL_LANG_SPECIFIC (newdecl)
3580 && DECL_LANG_SPECIFIC (olddecl))
3581 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3584 /* Merge the section attribute.
3585 We want to issue an error if the sections conflict but that must be
3586 done later in decl_attributes since we are called before attributes