1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* Process declarations and symbol lookup for C front end.
24 Also constructs types; the standard scalar types at initialization,
25 and structure, union, array and enum types when they are declared. */
27 /* ??? not all decl nodes are given the most useful possible
28 line numbers. For example, the CONST_DECLs for enum values. */
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
51 extern struct obstack permanent_obstack;
52 extern struct obstack* saveable_obstack;
54 extern int current_class_depth;
56 extern tree static_ctors, static_dtors;
58 extern tree global_namespace;
60 extern int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree));
62 /* Use garbage collection. */
66 #ifndef WCHAR_UNSIGNED
67 #define WCHAR_UNSIGNED 0
70 #ifndef CHAR_TYPE_SIZE
71 #define CHAR_TYPE_SIZE BITS_PER_UNIT
74 #ifndef BOOL_TYPE_SIZE
75 #ifdef SLOW_BYTE_ACCESS
76 #define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
78 #define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
82 /* We let tm.h override the types used here, to handle trivial differences
83 such as the choice of unsigned int or long unsigned int for size_t.
84 When machines start needing nontrivial differences in the size type,
85 it would be best to do something here to figure out automatically
86 from other information what type to use. */
89 #define SIZE_TYPE "long unsigned int"
93 #define PTRDIFF_TYPE "long int"
97 #define WCHAR_TYPE "int"
100 static tree grokparms PROTO((tree, int));
101 static const char *redeclaration_error_message PROTO((tree, tree));
103 static void push_binding_level PROTO((struct binding_level *, int,
105 static void pop_binding_level PROTO((void));
106 static void suspend_binding_level PROTO((void));
107 static void resume_binding_level PROTO((struct binding_level *));
108 static struct binding_level *make_binding_level PROTO((void));
109 static void declare_namespace_level PROTO((void));
110 static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
111 static void storedecls PROTO((tree));
112 static void require_complete_types_for_parms PROTO((tree));
113 static void push_overloaded_decl_1 PROTO((tree));
114 static int ambi_op_p PROTO((tree));
115 static int unary_op_p PROTO((tree));
116 static tree store_bindings PROTO((tree, tree));
117 static tree lookup_tag_reverse PROTO((tree, tree));
118 static tree obscure_complex_init PROTO((tree, tree));
119 static tree maybe_build_cleanup_1 PROTO((tree, tree));
120 static tree lookup_name_real PROTO((tree, int, int, int));
121 static void warn_extern_redeclared_static PROTO((tree, tree));
122 static void grok_reference_init PROTO((tree, tree, tree));
123 static tree grokfndecl PROTO((tree, tree, tree, tree, int,
124 enum overload_flags, tree,
125 tree, int, int, int, int, int, int, tree));
126 static tree grokvardecl PROTO((tree, tree, RID_BIT_TYPE *, int, int, tree));
127 static tree lookup_tag PROTO((enum tree_code, tree,
128 struct binding_level *, int));
129 static void set_identifier_type_value_with_scope
130 PROTO((tree, tree, struct binding_level *));
131 static void record_builtin_type PROTO((enum rid, const char *, tree));
132 static void record_unknown_type PROTO((tree, const char *));
133 static int member_function_or_else PROTO((tree, tree, const char *));
134 static void bad_specifiers PROTO((tree, const char *, int, int, int, int,
136 static void lang_print_error_function PROTO((const char *));
137 static tree maybe_process_template_type_declaration PROTO((tree, int, struct binding_level*));
138 static void check_for_uninitialized_const_var PROTO((tree));
139 static unsigned long typename_hash PROTO((hash_table_key));
140 static boolean typename_compare PROTO((hash_table_key, hash_table_key));
141 static void push_binding PROTO((tree, tree, struct binding_level*));
142 static int add_binding PROTO((tree, tree));
143 static void pop_binding PROTO((tree, tree));
144 static tree local_variable_p PROTO((tree *));
145 static tree find_binding PROTO((tree, tree));
146 static tree select_decl PROTO((tree, int));
147 static int lookup_flags PROTO((int, int));
148 static tree qualify_lookup PROTO((tree, int));
149 static tree record_builtin_java_type PROTO((const char *, int));
150 static const char *tag_name PROTO((enum tag_types code));
151 static void find_class_binding_level PROTO((void));
152 static struct binding_level *innermost_nonclass_level PROTO((void));
153 static void warn_about_implicit_typename_lookup PROTO((tree, tree));
154 static int walk_namespaces_r PROTO((tree, walk_namespaces_fn, void *));
155 static int walk_globals_r PROTO((tree, void *));
156 static void add_decl_to_level PROTO((tree, struct binding_level *));
157 static tree make_label_decl PROTO((tree, int));
158 static void pop_label PROTO((tree));
159 static void pop_labels PROTO((tree));
160 static void maybe_deduce_size_from_array_init PROTO((tree, tree));
161 static void layout_var_decl PROTO((tree));
162 static void maybe_commonize_var PROTO((tree));
163 static tree check_initializer PROTO((tree, tree));
164 static void make_rtl_for_nonlocal_decl PROTO((tree, tree, const char *));
165 static void push_cp_function_context PROTO((struct function *));
166 static void pop_cp_function_context PROTO((struct function *));
167 static void mark_binding_level PROTO((void *));
168 static void mark_cp_function_context PROTO((struct function *));
169 static void mark_saved_scope PROTO((void *));
170 static void mark_lang_function PROTO((struct language_function *));
171 static void save_function_data PROTO((tree));
172 static void check_function_type PROTO((tree));
173 static void destroy_local_static PROTO((tree));
174 static void destroy_local_var PROTO((tree));
175 static void finish_constructor_body PROTO((void));
176 static void finish_destructor_body PROTO((void));
178 #if defined (DEBUG_CP_BINDING_LEVELS)
179 static void indent PROTO((void));
182 /* Erroneous argument lists can use this *IFF* they do not modify it. */
183 tree error_mark_list;
185 /* The following symbols are subsumed in the cp_global_trees array, and
186 listed here individually for documentation purposes.
189 tree wchar_decl_node;
192 tree vtable_entry_type;
193 tree delta_type_node;
196 tree __baselist_desc_type_node;
197 tree __i_desc_type_node, __m_desc_type_node;
198 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
200 tree __t_desc_type_node;
202 tree __tp_desc_type_node;
204 tree __access_mode_type_node;
205 tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
206 tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
207 tree __ptmf_desc_type_node, __ptmd_desc_type_node;
209 Not needed yet? May be needed one day?
210 tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
211 tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
212 tree __ptmf_desc_array_type, __ptmd_desc_array_type;
215 tree class_star_type_node;
216 tree class_type_node, record_type_node, union_type_node, enum_type_node;
217 tree unknown_type_node;
219 Array type `vtable_entry_type[]'
222 tree vtbl_ptr_type_node;
228 A FUNCTION_DECL which can call `abort'. Not necessarily the
229 one that the user will declare, but sufficient to be called
230 by routines that want to abort the program.
234 The FUNCTION_DECL for the default `::operator delete'.
236 tree global_delete_fndecl;
239 tree type_info_type_node, tinfo_fn_id, tinfo_fn_type;
243 tree cp_global_trees[CPTI_MAX];
245 /* Indicates that there is a type value in some namespace, although
246 that is not necessarily in scope at the moment. */
248 static tree global_type_node;
253 /* Expect only namespace names now. */
254 static int only_namespace_names;
256 /* In a destructor, the last insn emitted after the start of the
257 function and the parms. */
259 #define last_dtor_insn cp_function_chain->x_last_dtor_insn
261 /* In a constructor, the last insn emitted after the start of the
262 function and the parms, the exception specification and any
263 function-try-block. The constructor initializers are emitted after
266 #define last_parm_cleanup_insn cp_function_chain->x_last_parm_cleanup_insn
268 /* If original DECL_RESULT of current function was a register,
269 but due to being an addressable named return value, would up
270 on the stack, this variable holds the named return value's
271 original location. */
273 #define original_result_rtx cp_function_chain->x_result_rtx
275 struct named_label_list
277 struct binding_level *binding_level;
280 const char *filename_o_goto;
282 struct named_label_list *next;
285 /* Used only for jumps to as-yet undefined labels, since jumps to
286 defined labels can have their validity checked by stmt.c. */
288 #define named_label_uses cp_function_chain->x_named_label_uses
290 /* A list of objects which have constructors or destructors
291 which reside in the global scope. The decl is stored in
292 the TREE_VALUE slot and the initializer is stored
293 in the TREE_PURPOSE slot. */
294 tree static_aggregates;
298 /* A node for the integer constants 2, and 3. */
300 tree integer_two_node, integer_three_node;
302 /* While defining an enum type, this is 1 plus the last enumerator
305 static tree enum_next_value;
307 /* Nonzero means that there was overflow computing enum_next_value. */
309 static int enum_overflow;
311 /* Parsing a function declarator leaves here a chain of structure
312 and enum types declared in the parmlist. */
314 static tree last_function_parm_tags;
316 /* Similar, for last_function_parm_tags. */
317 tree last_function_parms;
318 static tree current_function_parm_tags;
320 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
321 that have names. Here so we can clear out their names' definitions
322 at the end of the function. The TREE_VALUE is a LABEL_DECL; the
323 TREE_PURPOSE is the previous binding of the label. */
325 #define named_labels cp_function_chain->x_named_labels
327 /* The FUNCTION_DECL for the function currently being compiled,
328 or 0 if between functions. */
329 tree current_function_decl;
331 /* Set to 0 at beginning of a function definition, and whenever
332 a label (case or named) is defined. Set to value of expression
333 returned from function when that value can be transformed into
334 a named return value. */
336 tree current_function_return_value;
338 /* Nonzero means give `double' the same size as `float'. */
340 extern int flag_short_double;
342 /* Nonzero means don't recognize any builtin functions. */
344 extern int flag_no_builtin;
346 /* Nonzero means don't recognize the non-ANSI builtin functions.
349 extern int flag_no_nonansi_builtin;
351 /* Nonzero means enable obscure ANSI features and disable GNU extensions
352 that might cause ANSI-compliant code to be miscompiled. */
354 extern int flag_ansi;
356 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
358 extern int flag_huge_objects;
360 /* Nonzero if we want to conserve space in the .o files. We do this
361 by putting uninitialized data and runtime initialized data into
362 .common instead of .data at the expense of not flagging multiple
364 extern int flag_conserve_space;
366 /* C and C++ flags are in decl2.c. */
368 /* Flag used when debugging spew.c */
370 extern int spew_debug;
372 /* A expression of value 0 with the same precision as a sizetype
374 tree signed_size_zero_node;
376 /* The name of the anonymous namespace, throughout this translation
378 tree anonymous_namespace_name;
381 /* For each binding contour we allocate a binding_level structure
382 which records the names defined in that contour.
385 1) one for each function definition,
386 where internal declarations of the parameters appear.
387 2) one for each compound statement,
388 to record its declarations.
390 The current meaning of a name can be found by searching the levels
391 from the current one out to the global one.
393 Off to the side, may be the class_binding_level. This exists only
394 to catch class-local declarations. It is otherwise nonexistent.
396 Also there may be binding levels that catch cleanups that must be
397 run when exceptions occur. Thus, to see whether a name is bound in
398 the current scope, it is not enough to look in the
399 CURRENT_BINDING_LEVEL. You should use lookup_name_current_level
402 /* Note that the information in the `names' component of the global contour
403 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
407 /* A chain of _DECL nodes for all variables, constants, functions,
408 and typedef types. These are in the reverse of the order
409 supplied. There may be OVERLOADs on this list, too, but they
410 are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD. */
413 /* A list of structure, union and enum definitions, for looking up
415 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
416 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
417 or ENUMERAL_TYPE node.
419 C++: the TREE_VALUE nodes can be simple types for
420 component_bindings. */
423 /* A list of USING_DECL nodes. */
426 /* A list of used namespaces. PURPOSE is the namespace,
427 VALUE the common ancestor with this binding_level's namespace. */
428 tree using_directives;
430 /* If this binding level is the binding level for a class, then
431 class_shadowed is a TREE_LIST. The TREE_PURPOSE of each node
432 is the name of an entity bound in the class; the TREE_VALUE is
433 the IDENTIFIER_CLASS_VALUE before we entered the class. Thus,
434 when leaving class scope, we can restore the
435 IDENTIFIER_CLASS_VALUE by walking this list. The TREE_TYPE is
436 the DECL bound by this name in the class. */
439 /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
440 is used for all binding levels. */
443 /* A TREE_LIST. Each TREE_VALUE is the LABEL_DECL for a local
444 label in this scope. The TREE_PURPOSE is the previous value of
445 the IDENTIFIER_LABEL VALUE. */
446 tree shadowed_labels;
448 /* For each level (except not the global one),
449 a chain of BLOCK nodes for all the levels
450 that were entered and exited one level down. */
453 /* The BLOCK node for this level, if one has been preallocated.
454 If 0, the BLOCK is allocated (if needed) when the level is popped. */
457 /* The binding level which this one is contained in (inherits from). */
458 struct binding_level *level_chain;
460 /* List of decls in `names' that have incomplete
461 structure or union types. */
464 /* List of VAR_DECLS saved from a previous for statement.
465 These would be dead in ANSI-conforming code, but might
466 be referenced in ARM-era code. These are stored in a
467 TREE_LIST; the TREE_VALUE is the actual declaration. */
468 tree dead_vars_from_for;
470 /* 1 for the level that holds the parameters of a function.
471 2 for the level that holds a class declaration.
472 3 for levels that hold parameter declarations. */
473 unsigned parm_flag : 4;
475 /* 1 means make a BLOCK for this level regardless of all else.
476 2 for temporary binding contours created by the compiler. */
479 /* Nonzero if this level "doesn't exist" for tags. */
480 unsigned tag_transparent : 1;
482 /* Nonzero if this level can safely have additional
483 cleanup-needing variables added to it. */
484 unsigned more_cleanups_ok : 1;
485 unsigned have_cleanups : 1;
487 /* Nonzero if this level is for storing the decls for template
488 parameters and generic decls; these decls will be discarded and
489 replaced with a TEMPLATE_DECL. */
490 unsigned pseudo_global : 1;
492 /* This is set for a namespace binding level. */
493 unsigned namespace_p : 1;
495 /* True if this level is that of a for-statement where we need to
496 worry about ambiguous (ARM or ANSI) scope rules. */
497 unsigned is_for_scope : 1;
499 /* True if this level corresponds to an EH region, as for a try block. */
500 unsigned eh_region : 1;
502 /* One bit left for this word. */
504 #if defined(DEBUG_CP_BINDING_LEVELS)
505 /* Binding depth at which this level began. */
506 unsigned binding_depth;
507 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
510 #define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
512 /* The binding level currently in effect. */
514 #define current_binding_level \
516 ? cp_function_chain->bindings \
517 : scope_chain->bindings)
519 /* The binding level of the current class, if any. */
521 #define class_binding_level scope_chain->class_bindings
523 /* A chain of binding_level structures awaiting reuse. */
525 static struct binding_level *free_binding_level;
527 /* The outermost binding level, for names of file scope.
528 This is created when the compiler is started and exists
529 through the entire run. */
531 static struct binding_level *global_binding_level;
533 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
535 static int keep_next_level_flag;
537 #if defined(DEBUG_CP_BINDING_LEVELS)
538 static int binding_depth = 0;
539 static int is_class_level = 0;
546 for (i = 0; i < binding_depth*2; i++)
549 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
551 static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
554 push_binding_level (newlevel, tag_transparent, keep)
555 struct binding_level *newlevel;
556 int tag_transparent, keep;
558 /* Add this level to the front of the chain (stack) of levels that
560 bzero ((char*) newlevel, sizeof (struct binding_level));
561 newlevel->level_chain = current_binding_level;
562 current_binding_level = newlevel;
563 newlevel->tag_transparent = tag_transparent;
564 newlevel->more_cleanups_ok = 1;
566 /* We are called before expand_start_bindings, but after
567 expand_eh_region_start for a try block; so we check this now,
568 before the EH block is covered up. */
569 newlevel->eh_region = is_eh_region ();
571 newlevel->keep = keep;
572 #if defined(DEBUG_CP_BINDING_LEVELS)
573 newlevel->binding_depth = binding_depth;
575 fprintf (stderr, "push %s level 0x%08x line %d\n",
576 (is_class_level) ? "class" : "block", newlevel, lineno);
579 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
582 /* Find the innermost enclosing class scope, and reset
583 CLASS_BINDING_LEVEL appropriately. */
586 find_class_binding_level ()
588 struct binding_level *level = current_binding_level;
590 while (level && level->parm_flag != 2)
591 level = level->level_chain;
592 if (level && level->parm_flag == 2)
593 class_binding_level = level;
595 class_binding_level = 0;
601 if (global_binding_level)
603 /* Cannot pop a level, if there are none left to pop. */
604 if (current_binding_level == global_binding_level)
605 my_friendly_abort (123);
607 /* Pop the current level, and free the structure for reuse. */
608 #if defined(DEBUG_CP_BINDING_LEVELS)
611 fprintf (stderr, "pop %s level 0x%08x line %d\n",
612 (is_class_level) ? "class" : "block",
613 current_binding_level, lineno);
614 if (is_class_level != (current_binding_level == class_binding_level))
617 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
620 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
622 register struct binding_level *level = current_binding_level;
623 current_binding_level = current_binding_level->level_chain;
624 level->level_chain = free_binding_level;
625 #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
626 if (level->binding_depth != binding_depth)
628 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
629 free_binding_level = level;
630 find_class_binding_level ();
635 suspend_binding_level ()
637 if (class_binding_level)
638 current_binding_level = class_binding_level;
640 if (global_binding_level)
642 /* Cannot suspend a level, if there are none left to suspend. */
643 if (current_binding_level == global_binding_level)
644 my_friendly_abort (123);
646 /* Suspend the current level. */
647 #if defined(DEBUG_CP_BINDING_LEVELS)
650 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
651 (is_class_level) ? "class" : "block",
652 current_binding_level, lineno);
653 if (is_class_level != (current_binding_level == class_binding_level))
656 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
659 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
660 current_binding_level = current_binding_level->level_chain;
661 find_class_binding_level ();
665 resume_binding_level (b)
666 struct binding_level *b;
668 /* Resuming binding levels is meant only for namespaces,
669 and those cannot nest into classes. */
670 my_friendly_assert(!class_binding_level, 386);
671 /* Also, resuming a non-directly nested namespace is a no-no. */
672 my_friendly_assert(b->level_chain == current_binding_level, 386);
673 current_binding_level = b;
674 #if defined(DEBUG_CP_BINDING_LEVELS)
675 b->binding_depth = binding_depth;
677 fprintf (stderr, "resume %s level 0x%08x line %d\n",
678 (is_class_level) ? "class" : "block", b, lineno);
681 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
684 /* Create a new `struct binding_level'. */
687 struct binding_level *
688 make_binding_level ()
691 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
694 /* Nonzero if we are currently in the global binding level. */
699 return current_binding_level == global_binding_level;
702 /* Return the innermost binding level that is not for a class scope. */
704 static struct binding_level *
705 innermost_nonclass_level ()
707 struct binding_level *b;
709 b = current_binding_level;
710 while (b->parm_flag == 2)
716 /* Nonzero if we are currently in a toplevel binding level. This
717 means either the global binding level or a namespace in a toplevel
718 binding level. Since there are no non-toplevel namespace levels,
719 this really means any namespace or pseudo-global level. We also
720 include a class whose context is toplevel. */
723 toplevel_bindings_p ()
725 struct binding_level *b = innermost_nonclass_level ();
727 return b->namespace_p || b->pseudo_global;
730 /* Nonzero if this is a namespace scope, or if we are defining a class
731 which is itself at namespace scope, or whose enclosing class is
732 such a class, etc. */
735 namespace_bindings_p ()
737 struct binding_level *b = innermost_nonclass_level ();
739 return b->namespace_p;
742 /* If KEEP is non-zero, make a BLOCK node for the next binding level,
743 unconditionally. Otherwise, use the normal logic to decide whether
744 or not to create a BLOCK. */
747 keep_next_level (keep)
750 keep_next_level_flag = keep;
753 /* Nonzero if the current level needs to have a BLOCK made. */
758 return (current_binding_level->blocks != NULL_TREE
759 || current_binding_level->keep
760 || current_binding_level->names != NULL_TREE
761 || (current_binding_level->tags != NULL_TREE
762 && !current_binding_level->tag_transparent));
766 declare_pseudo_global_level ()
768 current_binding_level->pseudo_global = 1;
772 declare_namespace_level ()
774 current_binding_level->namespace_p = 1;
778 pseudo_global_level_p ()
780 return current_binding_level->pseudo_global;
784 set_class_shadows (shadows)
787 class_binding_level->class_shadowed = shadows;
790 /* Enter a new binding level.
791 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
792 not for that of tags. */
795 pushlevel (tag_transparent)
798 struct binding_level *newlevel;
800 if (current_function && !doing_semantic_analysis_p ())
803 /* Reuse or create a struct for this binding level. */
804 #if defined(DEBUG_CP_BINDING_LEVELS)
806 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
807 if (free_binding_level)
808 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
810 newlevel = free_binding_level;
811 free_binding_level = free_binding_level->level_chain;
814 newlevel = make_binding_level ();
816 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
817 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
818 keep_next_level_flag = 0;
822 note_level_for_for ()
824 current_binding_level->is_for_scope = 1;
828 pushlevel_temporary (tag_transparent)
831 pushlevel (tag_transparent);
832 current_binding_level->keep = 2;
835 /* Note we don't call push_momentary() here. Otherwise, it would cause
836 cleanups to be allocated on the momentary obstack, and they will be
837 overwritten by the next statement. */
839 expand_start_bindings (0);
842 /* For a binding between a name and an entity at a block scope,
843 this is the `struct binding_level' for the block. */
844 #define BINDING_LEVEL(NODE) \
845 (((struct tree_binding*)NODE)->scope.level)
847 /* Make DECL the innermost binding for ID. The LEVEL is the binding
848 level at which this declaration is being bound. */
851 push_binding (id, decl, level)
854 struct binding_level* level;
858 binding = make_node (CPLUS_BINDING);
860 /* Now, fill in the binding information. */
861 BINDING_VALUE (binding) = decl;
862 BINDING_TYPE (binding) = NULL_TREE;
863 BINDING_LEVEL (binding) = level;
864 INHERITED_VALUE_BINDING_P (binding) = 0;
865 LOCAL_BINDING_P (binding) = (level != class_binding_level);
866 BINDING_HAS_LEVEL_P (binding) = 1;
868 /* And put it on the front of the list of bindings for ID. */
869 TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
870 IDENTIFIER_BINDING (id) = binding;
873 /* ID is already bound in the current scope. But, DECL is an
874 additional binding for ID in the same scope. This is the `struct
875 stat' hack whereby a non-typedef class-name or enum-name can be
876 bound at the same level as some other kind of entity. It's the
877 responsibility of the caller to check that inserting this name is
878 legal here. Returns nonzero if the new binding was successful. */
880 add_binding (id, decl)
884 tree binding = IDENTIFIER_BINDING (id);
887 if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
888 /* The new name is the type name. */
889 BINDING_TYPE (binding) = decl;
890 else if (!BINDING_VALUE (binding))
891 /* This situation arises when push_class_level_binding moves an
892 inherited type-binding out of the way to make room for a new
894 BINDING_VALUE (binding) = decl;
895 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
896 && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
898 /* The old binding was a type name. It was placed in
899 BINDING_VALUE because it was thought, at the point it was
900 declared, to be the only entity with such a name. Move the
901 type name into the type slot; it is now hidden by the new
903 BINDING_TYPE (binding) = BINDING_VALUE (binding);
904 BINDING_VALUE (binding) = decl;
905 INHERITED_VALUE_BINDING_P (binding) = 0;
907 else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
908 && TREE_CODE (decl) == TYPE_DECL
909 && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
910 && same_type_p (TREE_TYPE (decl),
911 TREE_TYPE (BINDING_VALUE (binding))))
912 /* We have two typedef-names, both naming the same type to have
913 the same name. This is OK because of:
917 In a given scope, a typedef specifier can be used to redefine
918 the name of any type declared in that scope to refer to the
919 type to which it already refers. */
923 cp_error ("declaration of `%#D'", decl);
924 cp_error_at ("conflicts with previous declaration `%#D'",
925 BINDING_VALUE (binding));
932 /* Add DECL to the list of things declared in B. */
935 add_decl_to_level (decl, b)
937 struct binding_level *b;
939 /* We build up the list in reverse order, and reverse it later if
941 TREE_CHAIN (decl) = b->names;
945 /* Bind DECL to ID in the current_binding_level, assumed to be a local
946 binding level. If PUSH_USING is set in FLAGS, we know that DECL
947 doesn't really belong to this binding level, that it got here
948 through a using-declaration. */
951 push_local_binding (id, decl, flags)
956 struct binding_level *b;
958 /* Skip over any local classes. This makes sense if we call
959 push_local_binding with a friend decl of a local class. */
960 b = current_binding_level;
961 while (b->parm_flag == 2)
964 if (lookup_name_current_level (id))
966 /* Supplement the existing binding. */
967 if (!add_binding (id, decl))
968 /* It didn't work. Something else must be bound at this
969 level. Do not add DECL to the list of things to pop
974 /* Create a new binding. */
975 push_binding (id, decl, b);
977 if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
978 /* We must put the OVERLOAD into a TREE_LIST since the
979 TREE_CHAIN of an OVERLOAD is already used. Similarly for
980 decls that got here through a using-declaration. */
981 decl = build_tree_list (NULL_TREE, decl);
983 /* And put DECL on the list of things declared by the current
985 add_decl_to_level (decl, b);
988 /* Bind DECL to ID in the class_binding_level. Returns nonzero if the
989 binding was successful. */
992 push_class_binding (id, decl)
997 tree binding = IDENTIFIER_BINDING (id);
1000 /* Note that we declared this value so that we can issue an error if
1001 this an illegal redeclaration of a name already used for some
1003 note_name_declared_in_class (id, decl);
1005 if (binding && BINDING_LEVEL (binding) == class_binding_level)
1006 /* Supplement the existing binding. */
1007 result = add_binding (id, decl);
1009 /* Create a new binding. */
1010 push_binding (id, decl, class_binding_level);
1012 /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1013 class-level declaration. Note that we do not use DECL here
1014 because of the possibility of the `struct stat' hack; if DECL is
1015 a class-name or enum-name we might prefer a field-name, or some
1017 IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1019 /* If this is a binding from a base class, mark it as such. */
1020 binding = IDENTIFIER_BINDING (id);
1021 if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1023 /* Any implicit typename must be from a base-class. The
1024 context for an implicit typename declaration is always
1025 the derived class in which the lookup was done, so the checks
1026 based on the context of DECL below will not trigger. */
1027 if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
1028 INHERITED_VALUE_BINDING_P (binding) = 1;
1031 if (TREE_CODE (decl) == OVERLOAD)
1032 context = DECL_REAL_CONTEXT (OVL_CURRENT (decl));
1035 my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd',
1037 context = DECL_REAL_CONTEXT (decl);
1040 if (is_properly_derived_from (current_class_type, context))
1041 INHERITED_VALUE_BINDING_P (binding) = 1;
1043 INHERITED_VALUE_BINDING_P (binding) = 0;
1046 else if (BINDING_VALUE (binding) == decl)
1047 /* We only encounter a TREE_LIST when push_class_decls detects an
1048 ambiguity. Such an ambiguity can be overridden by a definition
1050 INHERITED_VALUE_BINDING_P (binding) = 1;
1055 /* Remove the binding for DECL which should be the innermost binding
1059 pop_binding (id, decl)
1065 if (id == NULL_TREE)
1066 /* It's easiest to write the loops that call this function without
1067 checking whether or not the entities involved have names. We
1068 get here for such an entity. */
1071 /* Get the innermost binding for ID. */
1072 binding = IDENTIFIER_BINDING (id);
1074 /* The name should be bound. */
1075 my_friendly_assert (binding != NULL_TREE, 0);
1077 /* The DECL will be either the ordinary binding or the type
1078 binding for this identifier. Remove that binding. */
1079 if (BINDING_VALUE (binding) == decl)
1080 BINDING_VALUE (binding) = NULL_TREE;
1081 else if (BINDING_TYPE (binding) == decl)
1082 BINDING_TYPE (binding) = NULL_TREE;
1084 my_friendly_abort (0);
1086 if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1087 /* We're completely done with the innermost binding for this
1088 identifier. Unhook it from the list of bindings. */
1089 IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1092 /* When a label goes out of scope, check to see if that label was used
1093 in a valid manner, and issue any appropriate warnings or errors. */
1099 tree label = TREE_VALUE (link);
1101 if (!processing_template_decl && doing_semantic_analysis_p ())
1103 if (DECL_INITIAL (label) == NULL_TREE)
1105 cp_error_at ("label `%D' used but not defined", label);
1106 /* Avoid crashing later. */
1107 define_label (input_filename, 1, DECL_NAME (label));
1109 else if (warn_unused && !TREE_USED (label))
1110 cp_warning_at ("label `%D' defined but not used", label);
1113 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), TREE_PURPOSE (link));
1116 /* At the end of a function, all labels declared within the fucntion
1117 go out of scope. BLOCK is the top-level block for the
1126 /* Clear out the definitions of all label names, since their scopes
1128 for (link = named_labels; link; link = TREE_CHAIN (link))
1131 /* Put the labels into the "variables" of the top-level block,
1132 so debugger can see them. */
1133 TREE_CHAIN (TREE_VALUE (link)) = BLOCK_VARS (block);
1134 BLOCK_VARS (block) = TREE_VALUE (link);
1137 named_labels = NULL_TREE;
1140 /* Exit a binding level.
1141 Pop the level off, and restore the state of the identifier-decl mappings
1142 that were in effect when this level was entered.
1144 If KEEP == 1, this level had explicit declarations, so
1145 and create a "block" (a BLOCK node) for the level
1146 to record its declarations and subblocks for symbol table output.
1148 If FUNCTIONBODY is nonzero, this level is the body of a function,
1149 so create a block as if KEEP were set and also clear out all
1152 If REVERSE is nonzero, reverse the order of decls before putting
1153 them into the BLOCK. */
1156 poplevel (keep, reverse, functionbody)
1162 /* The chain of decls was accumulated in reverse order.
1163 Put it into forward order, just for cleanliness. */
1165 int tmp = functionbody;
1166 int real_functionbody;
1169 tree block = NULL_TREE;
1171 int block_previously_created;
1172 int leaving_for_scope;
1174 if (current_function && !doing_semantic_analysis_p ())
1177 my_friendly_assert (current_binding_level->parm_flag != 2,
1180 real_functionbody = (current_binding_level->keep == 2
1181 ? ((functionbody = 0), tmp) : functionbody);
1182 tags = functionbody >= 0 ? current_binding_level->tags : 0;
1183 subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1185 my_friendly_assert (!current_binding_level->class_shadowed,
1188 /* We used to use KEEP == 2 to indicate that the new block should go
1189 at the beginning of the list of blocks at this binding level,
1190 rather than the end. This hack is no longer used. */
1191 my_friendly_assert (keep == 0 || keep == 1, 0);
1193 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1194 (HOST_WIDE_INT) current_binding_level->level_chain,
1195 current_binding_level->parm_flag,
1196 current_binding_level->keep);
1198 if (current_binding_level->keep == 1)
1201 /* Get the decls in the order they were written.
1202 Usually current_binding_level->names is in reverse order.
1203 But parameter decls were previously put in forward order. */
1206 current_binding_level->names
1207 = decls = nreverse (current_binding_level->names);
1209 decls = current_binding_level->names;
1211 /* Output any nested inline functions within this block
1212 if they weren't already output. */
1214 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1215 if (TREE_CODE (decl) == FUNCTION_DECL
1216 && ! TREE_ASM_WRITTEN (decl)
1217 && DECL_INITIAL (decl) != NULL_TREE
1218 && TREE_ADDRESSABLE (decl)
1219 && decl_function_context (decl) == current_function_decl)
1221 /* If this decl was copied from a file-scope decl
1222 on account of a block-scope extern decl,
1223 propagate TREE_ADDRESSABLE to the file-scope decl. */
1224 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1225 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1228 push_function_context ();
1229 output_inline_function (decl);
1230 pop_function_context ();
1234 /* If there were any declarations or structure tags in that level,
1235 or if this level is a function body,
1236 create a BLOCK to record them for the life of this function. */
1239 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1240 if (block_previously_created)
1241 block = current_binding_level->this_block;
1242 else if (keep == 1 || functionbody)
1243 block = make_node (BLOCK);
1244 if (block != NULL_TREE)
1246 if (block_previously_created)
1248 if (decls || tags || subblocks)
1250 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
1251 warning ("internal compiler error: debugging info corrupted");
1253 BLOCK_VARS (block) = decls;
1254 BLOCK_TYPE_TAGS (block) = tags;
1256 /* We can have previous subblocks and new subblocks when
1257 doing fixup_gotos with complex cleanups. We chain the new
1258 subblocks onto the end of any pre-existing subblocks. */
1259 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1262 /* If we created the block earlier on, and we are just
1263 diddling it now, then it already should have a proper
1264 BLOCK_END_NOTE value associated with it. */
1268 BLOCK_VARS (block) = decls;
1269 BLOCK_TYPE_TAGS (block) = tags;
1270 BLOCK_SUBBLOCKS (block) = subblocks;
1271 /* Otherwise, for a new block, install a new BLOCK_END_NOTE
1273 remember_end_note (block);
1277 /* In each subblock, record that this is its superior. */
1280 for (link = subblocks; link; link = TREE_CHAIN (link))
1281 BLOCK_SUPERCONTEXT (link) = block;
1283 /* We still support the old for-scope rules, whereby the variables
1284 in a for-init statement were in scope after the for-statement
1285 ended. We only use the new rules in flag_new_for_scope is
1288 = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1290 /* Remove declarations for all the DECLs in this level. */
1291 for (link = decls; link; link = TREE_CHAIN (link))
1293 if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1296 = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1300 ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1302 ns_binding = NULL_TREE;
1305 && (BINDING_LEVEL (outer_binding)
1306 == current_binding_level->level_chain))
1307 /* We have something like:
1312 and we are leaving the `for' scope. There's no reason to
1313 keep the binding of the inner `i' in this case. */
1314 pop_binding (DECL_NAME (link), link);
1315 else if ((outer_binding
1316 && (TREE_CODE (BINDING_VALUE (outer_binding))
1319 && TREE_CODE (ns_binding) == TYPE_DECL))
1320 /* Here, we have something like:
1328 We must pop the for-scope binding so we know what's a
1329 type and what isn't. */
1330 pop_binding (DECL_NAME (link), link);
1333 /* Mark this VAR_DECL as dead so that we can tell we left it
1334 there only for backward compatibility. */
1335 DECL_DEAD_FOR_LOCAL (link) = 1;
1337 /* Keep track of what should of have happenned when we
1338 popped the binding. */
1339 if (outer_binding && BINDING_VALUE (outer_binding))
1340 DECL_SHADOWED_FOR_VAR (link)
1341 = BINDING_VALUE (outer_binding);
1343 /* Add it to the list of dead variables in the next
1344 outermost binding to that we can remove these when we
1345 leave that binding. */
1346 current_binding_level->level_chain->dead_vars_from_for
1347 = tree_cons (NULL_TREE, link,
1348 current_binding_level->level_chain->
1349 dead_vars_from_for);
1351 /* Although we don't pop the CPLUS_BINDING, we do clear
1352 its BINDING_LEVEL since the level is going away now. */
1353 BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1359 /* Remove the binding. */
1361 if (TREE_CODE (decl) == TREE_LIST)
1362 decl = TREE_VALUE (decl);
1363 if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1364 pop_binding (DECL_NAME (decl), decl);
1365 else if (TREE_CODE (decl) == OVERLOAD)
1366 pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1368 my_friendly_abort (0);
1372 /* Remove declarations for any `for' variables from inner scopes
1373 that we kept around. */
1374 for (link = current_binding_level->dead_vars_from_for;
1375 link; link = TREE_CHAIN (link))
1376 pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1378 /* Restore the IDENTIFIER_TYPE_VALUEs. */
1379 for (link = current_binding_level->type_shadowed;
1380 link; link = TREE_CHAIN (link))
1381 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1383 /* Restore the IDENTIFIER_LABEL_VALUEs for local labels. */
1384 for (link = current_binding_level->shadowed_labels;
1386 link = TREE_CHAIN (link))
1389 /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1390 list if a `using' declaration put them there. The debugging
1391 back-ends won't understand OVERLOAD, so we remove them here.
1392 Because the BLOCK_VARS are (temporarily) shared with
1393 CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1394 popped all the bindings. */
1399 for (d = &BLOCK_VARS (block); *d; )
1401 if (TREE_CODE (*d) == TREE_LIST)
1402 *d = TREE_CHAIN (*d);
1404 d = &TREE_CHAIN (*d);
1408 /* If the level being exited is the top level of a function,
1409 check over all the labels. */
1412 /* Since this is the top level block of a function, the vars are
1413 the function's parameters. Don't leave them in the BLOCK
1414 because they are found in the FUNCTION_DECL instead. */
1415 BLOCK_VARS (block) = 0;
1419 /* Any uses of undefined labels now operate under constraints
1420 of next binding contour. */
1421 if (current_function)
1423 struct binding_level *level_chain;
1424 level_chain = current_binding_level->level_chain;
1427 struct named_label_list *labels;
1428 for (labels = named_label_uses; labels; labels = labels->next)
1429 if (labels->binding_level == current_binding_level)
1431 labels->binding_level = level_chain;
1432 labels->names_in_scope = level_chain->names;
1437 tmp = current_binding_level->keep;
1439 pop_binding_level ();
1441 DECL_INITIAL (current_function_decl) = block;
1444 if (!block_previously_created)
1445 current_binding_level->blocks
1446 = chainon (current_binding_level->blocks, block);
1448 /* If we did not make a block for the level just exited,
1449 any blocks made for inner levels
1450 (since they cannot be recorded as subblocks in that level)
1451 must be carried forward so they will later become subblocks
1452 of something else. */
1454 current_binding_level->blocks
1455 = chainon (current_binding_level->blocks, subblocks);
1457 /* Take care of compiler's internal binding structures. */
1460 expand_end_bindings (getdecls (), keep, 1);
1461 /* Each and every BLOCK node created here in `poplevel' is important
1462 (e.g. for proper debugging information) so if we created one
1463 earlier, mark it as "used". */
1465 TREE_USED (block) = 1;
1466 block = poplevel (keep, reverse, real_functionbody);
1469 /* Each and every BLOCK node created here in `poplevel' is important
1470 (e.g. for proper debugging information) so if we created one
1471 earlier, mark it as "used". */
1473 TREE_USED (block) = 1;
1477 /* Delete the node BLOCK from the current binding level.
1478 This is used for the block inside a stmt expr ({...})
1479 so that the block can be reinserted where appropriate. */
1482 delete_block (block)
1486 if (current_binding_level->blocks == block)
1487 current_binding_level->blocks = TREE_CHAIN (block);
1488 for (t = current_binding_level->blocks; t;)
1490 if (TREE_CHAIN (t) == block)
1491 TREE_CHAIN (t) = TREE_CHAIN (block);
1495 TREE_CHAIN (block) = NULL_TREE;
1496 /* Clear TREE_USED which is always set by poplevel.
1497 The flag is set again if insert_block is called. */
1498 TREE_USED (block) = 0;
1501 /* Insert BLOCK at the end of the list of subblocks of the
1502 current binding level. This is used when a BIND_EXPR is expanded,
1503 to handle the BLOCK node inside the BIND_EXPR. */
1506 insert_block (block)
1509 TREE_USED (block) = 1;
1510 current_binding_level->blocks
1511 = chainon (current_binding_level->blocks, block);
1514 /* Set the BLOCK node for the innermost scope
1515 (the one we are currently in). */
1519 register tree block;
1521 current_binding_level->this_block = block;
1524 /* Do a pushlevel for class declarations. */
1529 register struct binding_level *newlevel;
1531 /* Reuse or create a struct for this binding level. */
1532 #if defined(DEBUG_CP_BINDING_LEVELS)
1534 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1535 if (free_binding_level)
1536 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1538 newlevel = free_binding_level;
1539 free_binding_level = free_binding_level->level_chain;
1542 newlevel = make_binding_level ();
1544 #if defined(DEBUG_CP_BINDING_LEVELS)
1546 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1548 push_binding_level (newlevel, 0, 0);
1550 class_binding_level = current_binding_level;
1551 class_binding_level->parm_flag = 2;
1554 /* ...and a poplevel for class declarations. */
1559 register struct binding_level *level = class_binding_level;
1562 my_friendly_assert (level != 0, 354);
1564 /* If we're leaving a toplevel class, don't bother to do the setting
1565 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1566 shouldn't even be used when current_class_type isn't set, and second,
1567 if we don't touch it here, we're able to use the cache effect if the
1568 next time we're entering a class scope, it is the same class. */
1569 if (current_class_depth != 1)
1571 struct binding_level* b;
1573 /* Clear out our IDENTIFIER_CLASS_VALUEs. */
1574 for (shadowed = level->class_shadowed;
1576 shadowed = TREE_CHAIN (shadowed))
1577 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1579 /* Find the next enclosing class, and recreate
1580 IDENTIFIER_CLASS_VALUEs appropriate for that class. */
1581 b = level->level_chain;
1582 while (b && b->parm_flag != 2)
1586 for (shadowed = b->class_shadowed;
1588 shadowed = TREE_CHAIN (shadowed))
1592 t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1593 while (t && BINDING_LEVEL (t) != b)
1597 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1598 = BINDING_VALUE (t);
1602 /* Remember to save what IDENTIFIER's were bound in this scope so we
1603 can recover from cache misses. */
1605 previous_class_type = current_class_type;
1606 previous_class_values = class_binding_level->class_shadowed;
1608 for (shadowed = level->type_shadowed;
1610 shadowed = TREE_CHAIN (shadowed))
1611 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1613 /* Remove the bindings for all of the class-level declarations. */
1614 for (shadowed = level->class_shadowed;
1616 shadowed = TREE_CHAIN (shadowed))
1617 pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1619 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1620 (HOST_WIDE_INT) class_binding_level->level_chain,
1621 class_binding_level->parm_flag,
1622 class_binding_level->keep);
1624 /* Now, pop out of the binding level which we created up in the
1625 `pushlevel_class' routine. */
1626 #if defined(DEBUG_CP_BINDING_LEVELS)
1628 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1630 pop_binding_level ();
1633 /* We are entering the scope of a class. Clear IDENTIFIER_CLASS_VALUE
1634 for any names in enclosing classes. */
1637 clear_identifier_class_values ()
1641 if (!class_binding_level)
1644 for (t = class_binding_level->class_shadowed;
1647 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1650 /* Returns non-zero if T is a virtual function table. */
1653 vtable_decl_p (t, data)
1655 void *data ATTRIBUTE_UNUSED;
1657 return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1660 /* Returns non-zero if T is a TYPE_DECL for a type with virtual
1664 vtype_decl_p (t, data)
1666 void *data ATTRIBUTE_UNUSED;
1668 return (TREE_CODE (t) == TYPE_DECL
1669 && TREE_TYPE (t) != error_mark_node
1670 && TYPE_LANG_SPECIFIC (TREE_TYPE (t))
1671 && CLASSTYPE_VSIZE (TREE_TYPE (t)));
1674 /* Return the declarations that are members of the namespace NS. */
1677 cp_namespace_decls (ns)
1680 return NAMESPACE_LEVEL (ns)->names;
1683 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1684 itself, calling F for each. The DATA is passed to F as well. */
1687 walk_namespaces_r (namespace, f, data)
1689 walk_namespaces_fn f;
1695 result |= (*f) (namespace, data);
1697 for (current = cp_namespace_decls (namespace);
1699 current = TREE_CHAIN (current))
1701 if (TREE_CODE (current) != NAMESPACE_DECL
1702 || DECL_NAMESPACE_ALIAS (current))
1704 if (!DECL_LANG_SPECIFIC (current))
1707 my_friendly_assert (current == std_node, 393);
1711 /* We found a namespace. */
1712 result |= walk_namespaces_r (current, f, data);
1718 /* Walk all the namespaces, calling F for each. The DATA is passed to
1722 walk_namespaces (f, data)
1723 walk_namespaces_fn f;
1726 return walk_namespaces_r (global_namespace, f, data);
1729 struct walk_globals_data {
1730 walk_globals_pred p;
1735 /* Walk the global declarations in NAMESPACE. Whenever one is found
1736 for which P returns non-zero, call F with its address. If any call
1737 to F returns a non-zero value, return a non-zero value. */
1740 walk_globals_r (namespace, data)
1744 struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1745 walk_globals_pred p = wgd->p;
1746 walk_globals_fn f = wgd->f;
1747 void *d = wgd->data;
1751 t = &NAMESPACE_LEVEL (namespace)->names;
1758 result |= (*f) (t, d);
1760 /* If F changed *T, then *T still points at the next item to
1763 t = &TREE_CHAIN (*t);
1769 /* Walk the global declarations. Whenever one is found for which P
1770 returns non-zero, call F with its address. If any call to F
1771 returns a non-zero value, return a non-zero value. */
1774 walk_globals (p, f, data)
1775 walk_globals_pred p;
1779 struct walk_globals_data wgd;
1784 return walk_namespaces (walk_globals_r, &wgd);
1787 /* Call wrapup_globals_declarations for the globals in NAMESPACE. If
1788 DATA is non-NULL, this is the last time we will call
1789 wrapup_global_declarations for this NAMESPACE. */
1792 wrapup_globals_for_namespace (namespace, data)
1796 tree globals = cp_namespace_decls (namespace);
1797 int len = list_length (globals);
1798 tree *vec = (tree *) alloca (sizeof (tree) * len);
1802 int last_time = (data != 0);
1804 if (last_time && namespace == global_namespace)
1805 /* Let compile_file handle the global namespace. */
1808 /* Process the decls in reverse order--earliest first.
1809 Put them into VEC from back to front, then take out from front. */
1811 for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1812 vec[len - i - 1] = decl;
1816 check_global_declarations (vec, len);
1820 /* Temporarily mark vtables as external. That prevents
1821 wrapup_global_declarations from writing them out; we must process
1822 them ourselves in finish_vtable_vardecl. */
1823 for (i = 0; i < len; ++i)
1824 if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
1826 DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
1827 DECL_EXTERNAL (vec[i]) = 1;
1830 /* Write out any globals that need to be output. */
1831 result = wrapup_global_declarations (vec, len);
1833 /* Undo the hack to DECL_EXTERNAL above. */
1834 for (i = 0; i < len; ++i)
1835 if (vtable_decl_p (vec[i], /*data=*/0)
1836 && DECL_NOT_REALLY_EXTERN (vec[i]))
1838 DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
1839 DECL_EXTERNAL (vec[i]) = 0;
1846 /* Mark ARG (which is really a struct binding_level **) for GC. */
1849 mark_binding_level (arg)
1852 struct binding_level *lvl = *(struct binding_level **)arg;
1856 ggc_mark_tree (lvl->names);
1857 ggc_mark_tree (lvl->tags);
1858 ggc_mark_tree (lvl->usings);
1859 ggc_mark_tree (lvl->using_directives);
1860 ggc_mark_tree (lvl->class_shadowed);
1861 ggc_mark_tree (lvl->type_shadowed);
1862 ggc_mark_tree (lvl->shadowed_labels);
1863 ggc_mark_tree (lvl->blocks);
1864 ggc_mark_tree (lvl->this_block);
1865 ggc_mark_tree (lvl->incomplete);
1866 ggc_mark_tree (lvl->dead_vars_from_for);
1868 lvl = lvl->level_chain;
1872 /* For debugging. */
1873 static int no_print_functions = 0;
1874 static int no_print_builtins = 0;
1877 print_binding_level (lvl)
1878 struct binding_level *lvl;
1882 fprintf (stderr, " blocks=");
1883 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1884 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
1885 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
1886 if (lvl->tag_transparent)
1887 fprintf (stderr, " tag-transparent");
1888 if (lvl->more_cleanups_ok)
1889 fprintf (stderr, " more-cleanups-ok");
1890 if (lvl->have_cleanups)
1891 fprintf (stderr, " have-cleanups");
1892 fprintf (stderr, "\n");
1895 fprintf (stderr, " names:\t");
1896 /* We can probably fit 3 names to a line? */
1897 for (t = lvl->names; t; t = TREE_CHAIN (t))
1899 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1901 if (no_print_builtins
1902 && (TREE_CODE (t) == TYPE_DECL)
1903 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1906 /* Function decls tend to have longer names. */
1907 if (TREE_CODE (t) == FUNCTION_DECL)
1914 fprintf (stderr, "\n\t");
1917 print_node_brief (stderr, "", t, 0);
1918 if (t == error_mark_node)
1922 fprintf (stderr, "\n");
1926 fprintf (stderr, " tags:\t");
1928 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1930 if (TREE_PURPOSE (t) == NULL_TREE)
1932 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1939 fprintf (stderr, "\n\t");
1942 if (TREE_PURPOSE (t) == NULL_TREE)
1944 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1945 fprintf (stderr, ">");
1947 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1948 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1951 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1952 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1953 fprintf (stderr, ">");
1957 fprintf (stderr, "\n");
1959 if (lvl->class_shadowed)
1961 fprintf (stderr, " class-shadowed:");
1962 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1964 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1966 fprintf (stderr, "\n");
1968 if (lvl->type_shadowed)
1970 fprintf (stderr, " type-shadowed:");
1971 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1973 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1975 fprintf (stderr, "\n");
1980 print_other_binding_stack (stack)
1981 struct binding_level *stack;
1983 struct binding_level *level;
1984 for (level = stack; level != global_binding_level; level = level->level_chain)
1986 fprintf (stderr, "binding level ");
1987 fprintf (stderr, HOST_PTR_PRINTF, level);
1988 fprintf (stderr, "\n");
1989 print_binding_level (level);
1994 print_binding_stack ()
1996 struct binding_level *b;
1997 fprintf (stderr, "current_binding_level=");
1998 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1999 fprintf (stderr, "\nclass_binding_level=");
2000 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2001 fprintf (stderr, "\nglobal_binding_level=");
2002 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2003 fprintf (stderr, "\n");
2004 if (class_binding_level)
2006 for (b = class_binding_level; b; b = b->level_chain)
2007 if (b == current_binding_level)
2010 b = class_binding_level;
2012 b = current_binding_level;
2015 b = current_binding_level;
2016 print_other_binding_stack (b);
2017 fprintf (stderr, "global:\n");
2018 print_binding_level (global_binding_level);
2021 /* Namespace binding access routines: The namespace_bindings field of
2022 the identifier is polymorphic, with three possible values:
2023 NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2024 indicating the BINDING_VALUE of global_namespace. */
2026 /* Check whether the a binding for the name to scope is known.
2027 Assumes that the bindings of the name are already a list
2028 of bindings. Returns the binding found, or NULL_TREE. */
2031 find_binding (name, scope)
2035 tree iter, prev = NULL_TREE;
2037 scope = ORIGINAL_NAMESPACE (scope);
2039 for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2040 iter = TREE_CHAIN (iter))
2042 my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2043 if (BINDING_SCOPE (iter) == scope)
2045 /* Move binding found to the front of the list, so
2046 subsequent lookups will find it faster. */
2049 TREE_CHAIN (prev) = TREE_CHAIN (iter);
2050 TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2051 IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2060 /* Always returns a binding for name in scope. If the
2061 namespace_bindings is not a list, convert it to one first.
2062 If no binding is found, make a new one. */
2065 binding_for_name (name, scope)
2069 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2072 scope = ORIGINAL_NAMESPACE (scope);
2074 if (b && TREE_CODE (b) != CPLUS_BINDING)
2076 /* Get rid of optimization for global scope. */
2077 IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2078 BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2079 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2081 if (b && (result = find_binding (name, scope)))
2083 /* Not found, make a new permanent one. */
2084 push_obstacks (&permanent_obstack, &permanent_obstack);
2085 result = make_node (CPLUS_BINDING);
2086 TREE_CHAIN (result) = b;
2087 IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2088 BINDING_SCOPE (result) = scope;
2089 BINDING_TYPE (result) = NULL_TREE;
2090 BINDING_VALUE (result) = NULL_TREE;
2095 /* Return the binding value for name in scope, considering that
2096 namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2099 namespace_binding (name, scope)
2103 tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2106 if (scope == NULL_TREE)
2107 scope = global_namespace;
2108 if (TREE_CODE (b) != CPLUS_BINDING)
2109 return (scope == global_namespace) ? b : NULL_TREE;
2110 name = find_binding (name,scope);
2111 if (name == NULL_TREE)
2113 return BINDING_VALUE (name);
2116 /* Set the binding value for name in scope. If modifying the binding
2117 of global_namespace is attempted, try to optimize it. */
2120 set_namespace_binding (name, scope, val)
2127 if (scope == NULL_TREE)
2128 scope = global_namespace;
2130 if (scope == global_namespace)
2132 b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2133 if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2135 IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2139 b = binding_for_name (name, scope);
2140 BINDING_VALUE (b) = val;
2143 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
2144 select a name that is unique to this compilation unit. */
2147 push_namespace (name)
2152 int implicit_use = 0;
2154 if (!global_namespace)
2156 /* This must be ::. */
2157 my_friendly_assert (name == get_identifier ("::"), 377);
2162 /* The name of anonymous namespace is unique for the translation
2164 if (!anonymous_namespace_name)
2165 anonymous_namespace_name = get_file_function_name ('N');
2166 name = anonymous_namespace_name;
2167 d = IDENTIFIER_NAMESPACE_VALUE (name);
2169 /* Reopening anonymous namespace. */
2173 else if (current_namespace == global_namespace
2174 && name == DECL_NAME (std_node))
2181 /* Check whether this is an extended namespace definition. */
2182 d = IDENTIFIER_NAMESPACE_VALUE (name);
2183 if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2186 if (DECL_NAMESPACE_ALIAS (d))
2188 cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
2189 d, DECL_NAMESPACE_ALIAS (d));
2190 d = DECL_NAMESPACE_ALIAS (d);
2197 /* Make a new namespace, binding the name to it. */
2198 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2199 /* The global namespace is not pushed, and the global binding
2200 level is set elsewhere. */
2205 declare_namespace_level ();
2206 NAMESPACE_LEVEL (d) = current_binding_level;
2210 resume_binding_level (NAMESPACE_LEVEL (d));
2213 do_using_directive (d);
2214 /* Enter the name space. */
2215 current_namespace = d;
2218 /* Pop from the scope of the current namespace. */
2223 if (current_namespace == global_namespace)
2225 my_friendly_assert (in_std>0, 980421);
2229 current_namespace = CP_DECL_CONTEXT (current_namespace);
2230 /* The binding level is not popped, as it might be re-opened later. */
2231 suspend_binding_level ();
2234 /* Push into the scope of the namespace NS, even if it is deeply
2235 nested within another namespace. */
2238 push_nested_namespace (ns)
2241 if (ns == global_namespace)
2242 push_to_top_level ();
2245 push_nested_namespace (CP_DECL_CONTEXT (ns));
2246 push_namespace (DECL_NAME (ns));
2250 /* Pop back from the scope of the namespace NS, which was previously
2251 entered with push_nested_namespace. */
2254 pop_nested_namespace (ns)
2257 while (ns != global_namespace)
2260 ns = CP_DECL_CONTEXT (ns);
2263 pop_from_top_level ();
2267 /* Subroutines for reverting temporarily to top-level for instantiation
2268 of templates and such. We actually need to clear out the class- and
2269 local-value slots of all identifiers, so that only the global values
2270 are at all visible. Simply setting current_binding_level to the global
2271 scope isn't enough, because more binding levels may be pushed. */
2272 struct saved_scope *scope_chain;
2274 /* Mark ARG (which is really a struct saved_scope **) for GC. */
2277 mark_saved_scope (arg)
2280 struct saved_scope *t = *(struct saved_scope **)arg;
2283 mark_binding_level (&t->class_bindings);
2284 ggc_mark_tree (t->old_bindings);
2285 ggc_mark_tree (t->old_namespace);
2286 ggc_mark_tree (t->class_name);
2287 ggc_mark_tree (t->class_type);
2288 ggc_mark_tree (t->access_specifier);
2289 ggc_mark_tree (t->function_decl);
2291 ggc_mark_tree_varray (t->lang_base);
2292 ggc_mark_tree (t->lang_name);
2293 ggc_mark_tree (t->x_function_parms);
2294 ggc_mark_tree (t->template_parms);
2295 ggc_mark_tree (t->x_previous_class_type);
2296 ggc_mark_tree (t->x_previous_class_values);
2297 mark_binding_level (&t->bindings);
2303 store_bindings (names, old_bindings)
2304 tree names, old_bindings;
2307 for (t = names; t; t = TREE_CHAIN (t))
2309 tree binding, t1, id;
2311 if (TREE_CODE (t) == TREE_LIST)
2312 id = TREE_PURPOSE (t);
2317 /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2318 we have no IDENTIFIER_BINDING if we have left the class
2319 scope, but cached the class-level declarations. */
2320 || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2323 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
2324 if (TREE_VEC_ELT (t1, 0) == id)
2327 binding = make_tree_vec (4);
2331 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2332 TREE_VEC_ELT (binding, 0) = id;
2333 TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2334 TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2335 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2336 IDENTIFIER_BINDING (id) = NULL_TREE;
2337 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2339 TREE_CHAIN (binding) = old_bindings;
2340 old_bindings = binding;
2344 return old_bindings;
2348 maybe_push_to_top_level (pseudo)
2351 struct saved_scope *s;
2352 struct binding_level *b;
2356 s = (struct saved_scope *) xcalloc (1, sizeof (struct saved_scope));
2358 b = scope_chain ? current_binding_level : 0;
2360 /* If we're in the middle of some function, save our state. */
2361 if (current_function)
2364 push_function_context_to (NULL_TREE);
2369 old_bindings = NULL_TREE;
2370 if (scope_chain && previous_class_type)
2371 old_bindings = store_bindings (previous_class_values, old_bindings);
2373 /* Have to include global_binding_level, because class-level decls
2374 aren't listed anywhere useful. */
2375 for (; b; b = b->level_chain)
2379 /* Template IDs are inserted into the global level. If they were
2380 inserted into namespace level, finish_file wouldn't find them
2381 when doing pending instantiations. Therefore, don't stop at
2382 namespace level, but continue until :: . */
2383 if (b == global_binding_level || (pseudo && b->pseudo_global))
2386 old_bindings = store_bindings (b->names, old_bindings);
2387 /* We also need to check class_shadowed to save class-level type
2388 bindings, since pushclass doesn't fill in b->names. */
2389 if (b->parm_flag == 2)
2390 old_bindings = store_bindings (b->class_shadowed, old_bindings);
2392 /* Unwind type-value slots back to top level. */
2393 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2394 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2396 s->prev = scope_chain;
2397 s->old_bindings = old_bindings;
2399 s->need_pop_function_context = need_pop;
2400 s->function_decl = current_function_decl;
2403 current_function_decl = NULL_TREE;
2404 VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2405 current_lang_stack = &VARRAY_TREE (current_lang_base, 0);
2406 current_lang_name = lang_name_cplusplus;
2407 strict_prototype = strict_prototypes_lang_cplusplus;
2408 current_namespace = global_namespace;
2410 push_obstacks (&permanent_obstack, &permanent_obstack);
2414 push_to_top_level ()
2416 maybe_push_to_top_level (0);
2420 pop_from_top_level ()
2422 struct saved_scope *s = scope_chain;
2425 /* Clear out class-level bindings cache. */
2426 if (previous_class_type)
2427 invalidate_class_lookup_cache ();
2431 VARRAY_FREE (current_lang_base);
2433 scope_chain = s->prev;
2434 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
2436 tree id = TREE_VEC_ELT (t, 0);
2439 SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2440 IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2441 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2445 if (current_lang_name == lang_name_cplusplus)
2446 strict_prototype = strict_prototypes_lang_cplusplus;
2447 else if (current_lang_name == lang_name_c)
2448 strict_prototype = strict_prototypes_lang_c;
2450 /* If we were in the middle of compiling a function, restore our
2452 if (s->need_pop_function_context)
2453 pop_function_context_from (NULL_TREE);
2454 current_function_decl = s->function_decl;
2459 /* Push a definition of struct, union or enum tag "name".
2460 into binding_level "b". "type" should be the type node,
2461 We assume that the tag "name" is not already defined.
2463 Note that the definition may really be just a forward reference.
2464 In that case, the TYPE_SIZE will be a NULL_TREE.
2466 C++ gratuitously puts all these tags in the name space. */
2468 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2469 record the shadowed value for this binding contour. TYPE is
2470 the type that ID maps to. */
2473 set_identifier_type_value_with_scope (id, type, b)
2476 struct binding_level *b;
2478 if (!b->namespace_p)
2480 /* Shadow the marker, not the real thing, so that the marker
2481 gets restored later. */
2482 tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2484 = tree_cons (id, old_type_value, b->type_shadowed);
2488 tree binding = binding_for_name (id, current_namespace);
2489 BINDING_TYPE (binding) = type;
2490 /* Store marker instead of real type. */
2491 type = global_type_node;
2493 SET_IDENTIFIER_TYPE_VALUE (id, type);
2496 /* As set_identifier_type_value_with_scope, but using current_binding_level. */
2499 set_identifier_type_value (id, type)
2503 set_identifier_type_value_with_scope (id, type, current_binding_level);
2506 /* Return the type associated with id. */
2509 identifier_type_value (id)
2512 /* There is no type with that name, anywhere. */
2513 if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2515 /* This is not the type marker, but the real thing. */
2516 if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2517 return REAL_IDENTIFIER_TYPE_VALUE (id);
2518 /* Have to search for it. It must be on the global level, now.
2519 Ask lookup_name not to return non-types. */
2520 id = lookup_name_real (id, 2, 1, 0);
2522 return TREE_TYPE (id);
2526 /* Pop off extraneous binding levels left over due to syntax errors.
2528 We don't pop past namespaces, as they might be valid. */
2533 #ifdef DEBUG_CP_BINDING_LEVELS
2534 fprintf (stderr, "XXX entering pop_everything ()\n");
2536 while (!toplevel_bindings_p ())
2538 if (current_binding_level->parm_flag == 2)
2539 pop_nested_class ();
2543 #ifdef DEBUG_CP_BINDING_LEVELS
2544 fprintf (stderr, "XXX leaving pop_everything ()\n");
2548 /* The type TYPE is being declared. If it is a class template, or a
2549 specialization of a class template, do any processing required and
2550 perform error-checking. If IS_FRIEND is non-zero, this TYPE is
2551 being declared a friend. B is the binding level at which this TYPE
2554 Returns the TYPE_DECL for TYPE, which may have been altered by this
2558 maybe_process_template_type_declaration (type, globalize, b)
2561 struct binding_level* b;
2563 tree decl = TYPE_NAME (type);
2565 if (processing_template_parmlist)
2566 /* You can't declare a new template type in a template parameter
2567 list. But, you can declare a non-template type:
2569 template <class A*> struct S;
2571 is a forward-declaration of `A'. */
2575 maybe_check_template_type (type);
2577 my_friendly_assert (IS_AGGR_TYPE (type)
2578 || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2581 if (processing_template_decl)
2583 /* This may change after the call to
2584 push_template_decl_real, but we want the original value. */
2585 tree name = DECL_NAME (decl);
2587 decl = push_template_decl_real (decl, globalize);
2588 /* If the current binding level is the binding level for the
2589 template parameters (see the comment in
2590 begin_template_parm_list) and the enclosing level is a class
2591 scope, and we're not looking at a friend, push the
2592 declaration of the member class into the class scope. In the
2593 friend case, push_template_decl will already have put the
2594 friend into global scope, if appropriate. */
2595 if (TREE_CODE (type) != ENUMERAL_TYPE
2596 && !globalize && b->pseudo_global
2597 && b->level_chain->parm_flag == 2)
2599 finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2600 /* Put this tag on the list of tags for the class, since
2601 that won't happen below because B is not the class
2602 binding level, but is instead the pseudo-global level. */
2603 b->level_chain->tags =
2604 tree_cons (name, type, b->level_chain->tags);
2605 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2606 CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2614 /* In C++, you don't have to write `struct S' to refer to `S'; you
2615 can just use `S'. We accomplish this by creating a TYPE_DECL as
2616 if the user had written `typedef struct S S'. Create and return
2617 the TYPE_DECL for TYPE. */
2620 create_implicit_typedef (name, type)
2626 decl = build_decl (TYPE_DECL, name, type);
2627 SET_DECL_ARTIFICIAL (decl);
2628 /* There are other implicit type declarations, like the one *within*
2629 a class that allows you to write `S::S'. We must distinguish
2631 SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2632 TYPE_NAME (type) = decl;
2637 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2638 Normally put it into the inner-most non-tag-transparent scope,
2639 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2640 The latter is needed for implicit declarations. */
2643 pushtag (name, type, globalize)
2647 register struct binding_level *b;
2649 b = current_binding_level;
2650 while (b->tag_transparent
2651 || (globalize && b->parm_flag == 2))
2654 b->tags = tree_cons (name, type, b->tags);
2658 /* Do C++ gratuitous typedefing. */
2659 if (IDENTIFIER_TYPE_VALUE (name) != type)
2661 register tree d = NULL_TREE;
2663 tree context = TYPE_CONTEXT (type);
2667 tree cs = current_scope ();
2671 else if (cs != NULL_TREE
2672 && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2673 /* When declaring a friend class of a local class, we want
2674 to inject the newly named class into the scope
2675 containing the local class, not the namespace scope. */
2676 context = hack_decl_function_context (get_type_decl (cs));
2679 context = current_namespace;
2681 if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2682 || b->parm_flag == 2)
2685 if (current_lang_name == lang_name_java)
2686 TYPE_FOR_JAVA (type) = 1;
2688 d = create_implicit_typedef (name, type);
2689 DECL_CONTEXT (d) = FROB_CONTEXT (context);
2691 set_identifier_type_value_with_scope (name, type, b);
2693 d = maybe_process_template_type_declaration (type,
2696 if (b->parm_flag == 2)
2698 if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2699 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2700 class. But if it's a member template class, we
2701 want the TEMPLATE_DECL, not the TYPE_DECL, so this
2703 finish_member_declaration (d);
2705 pushdecl_class_level (d);
2708 d = pushdecl_with_scope (d, b);
2710 if (ANON_AGGRNAME_P (name))
2711 DECL_IGNORED_P (d) = 1;
2713 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2714 DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
2715 if (!uses_template_parms (type))
2716 DECL_ASSEMBLER_NAME (d)
2717 = get_identifier (build_overload_name (type, 1, 1));
2719 if (b->parm_flag == 2)
2721 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2722 CLASSTYPE_TAGS (current_class_type) = b->tags;
2726 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2727 /* Use the canonical TYPE_DECL for this node. */
2728 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2731 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2732 will be the tagged type we just added to the current
2733 binding level. This fake NULL-named TYPE_DECL node helps
2734 dwarfout.c to know when it needs to output a
2735 representation of a tagged type, and it also gives us a
2736 convenient place to record the "scope start" address for
2739 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2740 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2744 /* Counter used to create anonymous type names. */
2746 static int anon_cnt = 0;
2748 /* Return an IDENTIFIER which can be used as a name for
2749 anonymous structs and unions. */
2756 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2757 return get_identifier (buf);
2760 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2761 This keeps dbxout from getting confused. */
2766 register struct binding_level *b;
2768 static int last_cnt = 0;
2770 /* Fast out if no new anon names were declared. */
2771 if (last_cnt == anon_cnt)
2774 b = current_binding_level;
2775 while (b->tag_transparent)
2780 /* A NULL purpose means we have already processed all tags
2781 from here to the end of the list. */
2782 if (TREE_PURPOSE (tags) == NULL_TREE)
2784 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2785 TREE_PURPOSE (tags) = NULL_TREE;
2786 tags = TREE_CHAIN (tags);
2788 last_cnt = anon_cnt;
2791 /* Subroutine of duplicate_decls: return truthvalue of whether
2792 or not types of these decls match.
2794 For C++, we must compare the parameter list so that `int' can match
2795 `int&' in a parameter position, but `int&' is not confused with
2799 decls_match (newdecl, olddecl)
2800 tree newdecl, olddecl;
2804 if (newdecl == olddecl)
2807 if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2808 /* If the two DECLs are not even the same kind of thing, we're not
2809 interested in their types. */
2812 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2814 tree f1 = TREE_TYPE (newdecl);
2815 tree f2 = TREE_TYPE (olddecl);
2816 tree p1 = TYPE_ARG_TYPES (f1);
2817 tree p2 = TYPE_ARG_TYPES (f2);
2819 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
2820 && ! (DECL_LANGUAGE (newdecl) == lang_c
2821 && DECL_LANGUAGE (olddecl) == lang_c))
2824 /* When we parse a static member function definition,
2825 we put together a FUNCTION_DECL which thinks its type
2826 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2828 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
2829 revert_static_member_fn (&newdecl, &f1, &p1);
2830 else if (TREE_CODE (f2) == METHOD_TYPE
2831 && DECL_STATIC_FUNCTION_P (newdecl))
2832 revert_static_member_fn (&olddecl, &f2, &p2);
2834 /* Here we must take care of the case where new default
2835 parameters are specified. Also, warn if an old
2836 declaration becomes ambiguous because default
2837 parameters may cause the two to be ambiguous. */
2838 if (TREE_CODE (f1) != TREE_CODE (f2))
2840 if (TREE_CODE (f1) == OFFSET_TYPE)
2841 cp_compiler_error ("`%D' redeclared as member function", newdecl);
2843 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
2847 if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2849 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
2852 types_match = self_promoting_args_p (p1);
2853 if (p1 == void_list_node)
2854 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2856 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
2857 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
2859 types_match = self_promoting_args_p (p2);
2860 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2863 types_match = compparms (p1, p2);
2868 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2870 if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2871 DECL_TEMPLATE_PARMS (olddecl)))
2874 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2877 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2878 DECL_TEMPLATE_RESULT (newdecl));
2882 if (TREE_TYPE (newdecl) == error_mark_node)
2883 types_match = TREE_TYPE (olddecl) == error_mark_node;
2884 else if (TREE_TYPE (olddecl) == NULL_TREE)
2885 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2886 else if (TREE_TYPE (newdecl) == NULL_TREE)
2889 types_match = comptypes (TREE_TYPE (newdecl),
2890 TREE_TYPE (olddecl),
2891 COMPARE_REDECLARATION);
2897 /* If NEWDECL is `static' and an `extern' was seen previously,
2898 warn about it. OLDDECL is the previous declaration.
2900 Note that this does not apply to the C++ case of declaring
2901 a variable `extern const' and then later `const'.
2903 Don't complain about built-in functions, since they are beyond
2904 the user's control. */
2907 warn_extern_redeclared_static (newdecl, olddecl)
2908 tree newdecl, olddecl;
2910 static const char *explicit_extern_static_warning
2911 = "`%D' was declared `extern' and later `static'";
2912 static const char *implicit_extern_static_warning
2913 = "`%D' was declared implicitly `extern' and later `static'";
2917 if (TREE_CODE (newdecl) == TYPE_DECL)
2920 /* Don't get confused by static member functions; that's a different
2922 if (TREE_CODE (newdecl) == FUNCTION_DECL
2923 && DECL_STATIC_FUNCTION_P (newdecl))
2926 /* If the old declaration was `static', or the new one isn't, then
2927 then everything is OK. */
2928 if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2931 /* It's OK to declare a builtin function as `static'. */
2932 if (TREE_CODE (olddecl) == FUNCTION_DECL
2933 && DECL_ARTIFICIAL (olddecl))
2936 name = DECL_ASSEMBLER_NAME (newdecl);
2937 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2938 ? implicit_extern_static_warning
2939 : explicit_extern_static_warning, newdecl);
2940 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2943 /* Handle when a new declaration NEWDECL has the same name as an old
2944 one OLDDECL in the same binding contour. Prints an error message
2947 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2948 Otherwise, return 0. */
2951 duplicate_decls (newdecl, olddecl)
2952 tree newdecl, olddecl;
2954 unsigned olddecl_uid = DECL_UID (olddecl);
2955 int olddecl_friend = 0, types_match = 0;
2956 int new_defines_function = 0;
2958 if (newdecl == olddecl)
2961 types_match = decls_match (newdecl, olddecl);
2963 /* If either the type of the new decl or the type of the old decl is an
2964 error_mark_node, then that implies that we have already issued an
2965 error (earlier) for some bogus type specification, and in that case,
2966 it is rather pointless to harass the user with yet more error message
2967 about the same declaration, so just pretend the types match here. */
2968 if (TREE_TYPE (newdecl) == error_mark_node
2969 || TREE_TYPE (olddecl) == error_mark_node)
2972 /* Check for redeclaration and other discrepancies. */
2973 if (TREE_CODE (olddecl) == FUNCTION_DECL
2974 && DECL_ARTIFICIAL (olddecl))
2976 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2978 /* If you declare a built-in or predefined function name as static,
2979 the old definition is overridden, but optionally warn this was a
2980 bad choice of name. */
2981 if (! TREE_PUBLIC (newdecl))
2984 cp_warning ("shadowing %s function `%#D'",
2985 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2987 /* Discard the old built-in function. */
2990 /* If the built-in is not ansi, then programs can override
2991 it even globally without an error. */
2992 else if (! DECL_BUILT_IN (olddecl))
2993 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
2997 cp_error ("declaration of `%#D'", newdecl);
2998 cp_error ("conflicts with built-in declaration `%#D'",
3003 else if (!types_match)
3005 if ((DECL_LANGUAGE (newdecl) == lang_c
3006 && DECL_LANGUAGE (olddecl) == lang_c)
3007 || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3008 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3010 /* A near match; override the builtin. */
3012 if (TREE_PUBLIC (newdecl))
3014 cp_warning ("new declaration `%#D'", newdecl);
3015 cp_warning ("ambiguates built-in declaration `%#D'",
3018 else if (warn_shadow)
3019 cp_warning ("shadowing %s function `%#D'",
3020 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3024 /* Discard the old built-in function. */
3028 if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3030 /* If a builtin function is redeclared as `static', merge
3031 the declarations, but make the original one static. */
3032 DECL_THIS_STATIC (olddecl) = 1;
3033 TREE_PUBLIC (olddecl) = 0;
3035 /* Make the olddeclaration consistent with the new one so that
3036 all remnants of the builtin-ness of this function will be
3038 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3039 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3040 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
3041 SET_IDENTIFIER_GLOBAL_VALUE (DECL_ASSEMBLER_NAME (newdecl),
3045 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3047 if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3048 && TREE_CODE (newdecl) != TYPE_DECL
3049 && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3050 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3051 || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3052 && TREE_CODE (olddecl) != TYPE_DECL
3053 && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3054 && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3057 /* We do nothing special here, because C++ does such nasty
3058 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3059 get shadowed, and know that if we need to find a TYPE_DECL
3060 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3061 slot of the identifier. */
3065 if ((TREE_CODE (newdecl) == FUNCTION_DECL
3066 && DECL_FUNCTION_TEMPLATE_P (olddecl))
3067 || (TREE_CODE (olddecl) == FUNCTION_DECL
3068 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3071 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
3072 if (TREE_CODE (olddecl) == TREE_LIST)
3073 olddecl = TREE_VALUE (olddecl);
3074 cp_error_at ("previous declaration of `%#D'", olddecl);
3076 /* New decl is completely inconsistent with the old one =>
3077 tell caller to replace the old one. */
3081 else if (!types_match)
3083 if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
3084 /* These are certainly not duplicate declarations; they're
3085 from different scopes. */
3088 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3090 /* The name of a class template may not be declared to refer to
3091 any other template, class, function, object, namespace, value,
3092 or type in the same scope. */
3093 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3094 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3096 cp_error ("declaration of template `%#D'", newdecl);
3097 cp_error_at ("conflicts with previous declaration `%#D'",
3100 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3101 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3102 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3103 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3104 && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3105 DECL_TEMPLATE_PARMS (olddecl)))
3107 cp_error ("new declaration `%#D'", newdecl);
3108 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3112 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3114 if (DECL_LANGUAGE (newdecl) == lang_c
3115 && DECL_LANGUAGE (olddecl) == lang_c)
3117 cp_error ("declaration of C function `%#D' conflicts with",
3119 cp_error_at ("previous declaration `%#D' here", olddecl);
3121 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3122 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3124 cp_error ("new declaration `%#D'", newdecl);
3125 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3131 /* Already complained about this, so don't do so again. */
3132 else if (current_class_type == NULL_TREE
3133 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3135 cp_error ("conflicting types for `%#D'", newdecl);
3136 cp_error_at ("previous declaration as `%#D'", olddecl);
3139 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3140 && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3141 && (!DECL_TEMPLATE_INFO (newdecl)
3142 || (DECL_TI_TEMPLATE (newdecl)
3143 != DECL_TI_TEMPLATE (olddecl))))
3144 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3145 && (!DECL_TEMPLATE_INFO (olddecl)
3146 || (DECL_TI_TEMPLATE (olddecl)
3147 != DECL_TI_TEMPLATE (newdecl))))))
3148 /* It's OK to have a template specialization and a non-template
3149 with the same type, or to have specializations of two
3150 different templates with the same type. Note that if one is a
3151 specialization, and the other is an instantiation of the same
3152 template, that we do not exit at this point. That situation
3153 can occur if we instantiate a template class, and then
3154 specialize one of its methods. This situation is legal, but
3155 the declarations must be merged in the usual way. */
3157 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3158 && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3159 && !DECL_USE_TEMPLATE (newdecl))
3160 || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3161 && !DECL_USE_TEMPLATE (olddecl))))
3162 /* One of the declarations is a template instantiation, and the
3163 other is not a template at all. That's OK. */
3165 else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3166 && DECL_NAMESPACE_ALIAS (newdecl)
3167 && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3168 /* Redeclaration of namespace alias, ignore it. */
3172 const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3175 cp_error (errmsg, newdecl);
3176 if (DECL_NAME (olddecl) != NULL_TREE)
3177 cp_error_at ((DECL_INITIAL (olddecl)
3178 && namespace_bindings_p ())
3179 ? "`%#D' previously defined here"
3180 : "`%#D' previously declared here", olddecl);
3182 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3183 && DECL_INITIAL (olddecl) != NULL_TREE
3184 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3185 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3187 /* Prototype decl follows defn w/o prototype. */
3188 cp_warning_at ("prototype for `%#D'", newdecl);
3189 cp_warning_at ("follows non-prototype definition here", olddecl);
3191 else if (TREE_CODE (olddecl) == FUNCTION_DECL
3192 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3194 /* extern "C" int foo ();
3195 int foo () { bar (); }
3197 if (current_lang_stack
3198 == &VARRAY_TREE (current_lang_base, 0))
3199 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3202 cp_error_at ("previous declaration of `%#D' with %L linkage",
3203 olddecl, DECL_LANGUAGE (olddecl));
3204 cp_error ("conflicts with new declaration with %L linkage",
3205 DECL_LANGUAGE (newdecl));
3209 if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3211 else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3213 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3214 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3217 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3218 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3220 for (; t1 && t1 != void_list_node;
3221 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3222 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3224 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3229 cp_pedwarn ("default argument given for parameter %d of `%#D'",
3231 cp_pedwarn_at ("after previous specification in `%#D'",
3237 cp_error ("default argument given for parameter %d of `%#D'",
3239 cp_error_at ("after previous specification in `%#D'",
3244 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3245 && TREE_ADDRESSABLE (olddecl) && warn_inline)
3247 cp_warning ("`%#D' was used before it was declared inline",
3249 cp_warning_at ("previous non-inline declaration here",
3255 /* If new decl is `static' and an `extern' was seen previously,
3257 warn_extern_redeclared_static (newdecl, olddecl);
3259 /* We have committed to returning 1 at this point. */
3260 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3262 /* Now that functions must hold information normally held
3263 by field decls, there is extra work to do so that
3264 declaration information does not get destroyed during
3266 if (DECL_VINDEX (olddecl))
3267 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3268 if (DECL_CONTEXT (olddecl))
3269 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3270 if (DECL_CLASS_CONTEXT (olddecl))
3271 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
3272 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3273 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3274 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3275 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3276 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
3277 DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3278 DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3279 DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3280 new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3282 /* Optionally warn about more than one declaration for the same
3283 name, but don't warn about a function declaration followed by a
3285 if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3286 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3287 /* Don't warn about extern decl followed by definition. */
3288 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3289 /* Don't warn about friends, let add_friend take care of it. */
3290 && ! DECL_FRIEND_P (newdecl))
3292 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3293 cp_warning_at ("previous declaration of `%D'", olddecl);
3297 /* Deal with C++: must preserve virtual function table size. */
3298 if (TREE_CODE (olddecl) == TYPE_DECL)
3300 register tree newtype = TREE_TYPE (newdecl);
3301 register tree oldtype = TREE_TYPE (olddecl);
3303 if (newtype != error_mark_node && oldtype != error_mark_node
3304 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3306 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3307 CLASSTYPE_FRIEND_CLASSES (newtype)
3308 = CLASSTYPE_FRIEND_CLASSES (oldtype);
3312 /* Copy all the DECL_... slots specified in the new decl
3313 except for any that we copy here from the old type. */
3314 DECL_MACHINE_ATTRIBUTES (newdecl)
3315 = merge_machine_decl_attributes (olddecl, newdecl);
3317 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3319 if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3320 DECL_TEMPLATE_RESULT (olddecl)))
3321 cp_error ("invalid redeclaration of %D", newdecl);
3322 TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3323 DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3324 = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3325 DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3332 /* Automatically handles default parameters. */
3333 tree oldtype = TREE_TYPE (olddecl);
3336 /* Make sure we put the new type in the same obstack as the old one. */
3338 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
3340 push_permanent_obstack ();
3342 /* Merge the data types specified in the two decls. */
3343 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3345 if (TREE_CODE (newdecl) == VAR_DECL)
3346 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3347 /* Do this after calling `common_type' so that default
3348 parameters don't confuse us. */
3349 else if (TREE_CODE (newdecl) == FUNCTION_DECL
3350 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3351 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3353 TREE_TYPE (newdecl) = build_exception_variant (newtype,
3354 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3355 TREE_TYPE (olddecl) = build_exception_variant (newtype,
3356 TYPE_RAISES_EXCEPTIONS (oldtype));
3358 if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3359 && DECL_SOURCE_LINE (olddecl) != 0
3361 && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3362 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3364 cp_error ("declaration of `%F' throws different exceptions",
3366 cp_error_at ("to previous declaration `%F'", olddecl);
3369 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3371 /* Lay the type out, unless already done. */
3372 if (! same_type_p (newtype, oldtype)
3373 && TREE_TYPE (newdecl) != error_mark_node
3374 && !(processing_template_decl && uses_template_parms (newdecl)))
3375 layout_type (TREE_TYPE (newdecl));
3377 if ((TREE_CODE (newdecl) == VAR_DECL
3378 || TREE_CODE (newdecl) == PARM_DECL
3379 || TREE_CODE (newdecl) == RESULT_DECL
3380 || TREE_CODE (newdecl) == FIELD_DECL
3381 || TREE_CODE (newdecl) == TYPE_DECL)
3382 && !(processing_template_decl && uses_template_parms (newdecl)))
3383 layout_decl (newdecl, 0);
3385 /* Merge the type qualifiers. */
3386 if (TREE_READONLY (newdecl))
3387 TREE_READONLY (olddecl) = 1;
3388 if (TREE_THIS_VOLATILE (newdecl))
3389 TREE_THIS_VOLATILE (olddecl) = 1;
3391 /* Merge the initialization information. */
3392 if (DECL_INITIAL (newdecl) == NULL_TREE
3393 && DECL_INITIAL (olddecl) != NULL_TREE)
3395 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3396 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3397 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
3398 if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3399 && DECL_LANG_SPECIFIC (newdecl)
3400 && DECL_LANG_SPECIFIC (olddecl))
3401 DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3404 /* Merge the section attribute.
3405 We want to issue an error if the sections conflict but that must be
3406 done later in decl_attributes since we are called before attributes
3408 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3409 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3411 /* Keep the old rtl since we can safely use it. */
3412 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3416 /* If cannot merge, then use the new type and qualifiers,
3417 and don't preserve the old rtl. */
3420 /* Clean out any memory we had of the old declaration. */
3421 tree oldstatic = value_member (olddecl, static_aggregates);
3423 TREE_VALUE (oldstatic) = error_mark_node;
3425 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3426 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3427 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3428 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3431 /* Merge the storage class information. */
3432 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
3433 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3434 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3435 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3436 if (! DECL_EXTERNAL (olddecl))
3437 DECL_EXTERNAL (newdecl) = 0;
3439 if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3441 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3442 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3443 DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3444 DECL_TEMPLATE_INSTANTIATED (newdecl)
3445 |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3446 /* Don't really know how much of the language-specific
3447 values we should copy from old to new. */
3448 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3449 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3450 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3451 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3452 olddecl_friend = DECL_FRIEND_P (olddecl);
3454 /* Only functions have DECL_BEFRIENDING_CLASSES. */
3455 if (TREE_CODE (newdecl) == FUNCTION_DECL
3456 || DECL_FUNCTION_TEMPLATE_P (newdecl))
3457 DECL_BEFRIENDING_CLASSES (newdecl)
3458 = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3459 DECL_BEFRIENDING_CLASSES (olddecl));
3462 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3464 if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3465 && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3467 /* If newdecl is not a specialization, then it is not a
3468 template-related function at all. And that means that we
3469 shoud have exited above, returning 0. */
3470 my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3473 if (TREE_USED (olddecl))
3474 /* From [temp.expl.spec]:
3476 If a template, a member template or the member of a class
3477 template is explicitly specialized then that
3478 specialization shall be declared before the first use of
3479 that specialization that would cause an implicit
3480 instantiation to take place, in every translation unit in
3481 which such a use occurs. */
3482 cp_error ("explicit specialization of %D after first use",
3485 SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3487 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3489 /* If either decl says `inline', this fn is inline, unless its
3490 definition was passed already. */
3491 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3492 DECL_INLINE (olddecl) = 1;
3493 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3497 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3498 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
3499 DECL_RTL (olddecl) = DECL_RTL (newdecl);
3501 if (! types_match || new_defines_function)
3503 /* These need to be copied so that the names are available.
3504 Note that if the types do match, we'll preserve inline
3505 info and other bits, but if not, we won't. */
3506 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3507 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3509 if (new_defines_function)
3510 /* If defining a function declared with other language
3511 linkage, use the previously declared language linkage. */
3512 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3513 else if (types_match)
3515 /* If redeclaring a builtin function, and not a definition,
3516 it stays built in. */
3517 if (DECL_BUILT_IN (olddecl))
3519 DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3520 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3521 /* If we're keeping the built-in definition, keep the rtl,
3522 regardless of declaration matches. */
3523 DECL_RTL (newdecl) = DECL_RTL (olddecl);
3526 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3528 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3529 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
3530 /* Previously saved insns go together with
3531 the function's previous definition. */
3532 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3533 /* Don't clear out the arguments if we're redefining a function. */
3534 if (DECL_ARGUMENTS (olddecl))
3535 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3537 if (DECL_LANG_SPECIFIC (olddecl))
3538 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
3541 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3543 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3546 /* Now preserve various other info from the definition. */
3547 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3548 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3549 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3550 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
3552 if (TREE_CODE (newdecl) == FUNCTION_DECL)
3556 function_size = sizeof (struct tree_decl);
3558 bcopy ((char *) newdecl + sizeof (struct tree_common),
3559 (char *) olddecl + sizeof (struct tree_common),
3560 function_size - sizeof (struct tree_common));
3562 if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3564 /* If newdecl is a template instantiation, it is possible that
3565 the following sequence of events has occurred:
3567 o A friend function was declared in a class template. The
3568 class template was instantiated.
3570 o The instantiation of the friend declaration was
3571 recorded on the instantiation list, and is newdecl.
3573 o Later, however, instantiate_class_template called pushdecl
3574 on the newdecl to perform name injection. But, pushdecl in
3575 turn called duplicate_decls when it discovered that another
3576 declaration of a global function with the same name already
3579 o Here, in duplicate_decls, we decided to clobber newdecl.
3581 If we're going to do that, we'd better make sure that
3582 olddecl, and not newdecl, is on the list of
3583 instantiations so that if we try to do the instantiation
3584 again we won't get the clobbered declaration. */
3586 tree tmpl = DECL_TI_TEMPLATE (newdecl);
3587 tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3589 for (; decls; decls = TREE_CHAIN (decls))
3590 if (TREE_VALUE (decls) == newdecl)
3591 TREE_VALUE (decls) = olddecl;
3596 bcopy ((char *) newdecl + sizeof (struct tree_common),
3597 (char *) olddecl + sizeof (struct tree_common),
3598 sizeof (struct tree_decl) - sizeof (struct tree_common)
3599 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3602 DECL_UID (olddecl) = olddecl_uid;
3604 DECL_FRIEND_P (olddecl) = 1;
3606 /* NEWDECL contains the merged attribute lists.
3607 Update OLDDECL to be the same. */
3608 DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3613 /* Record a decl-node X as belonging to the current lexical scope.
3614 Check for errors (such as an incompatible declaration for the same
3615 name already seen in the same scope).
3617 Returns either X or an old decl for the same name.
3618 If an old decl is returned, it may have been smashed
3619 to agree with what X says. */
3627 int need_new_binding;
3629 /* We shouldn't be calling pushdecl when we're generating RTL for a
3630 function that we already did semantic analysis on previously. */
3631 my_friendly_assert (!current_function || doing_semantic_analysis_p (),
3634 name = DECL_ASSEMBLER_NAME (x);
3635 need_new_binding = 1;
3637 if (DECL_TEMPLATE_PARM_P (x))
3638 /* Template parameters have no context; they are not X::T even
3639 when declared within a class or namespace. */
3643 if (current_function_decl && x != current_function_decl
3644 /* A local declaration for a function doesn't constitute
3646 && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3647 /* A local declaration for an `extern' variable is in the
3648 scoped of the current namespace, not the current
3650 && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3651 /* Don't change DECL_CONTEXT of virtual methods. */
3652 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3653 && !DECL_CONTEXT (x))
3654 DECL_CONTEXT (x) = current_function_decl;
3655 if (!DECL_CONTEXT (x))
3656 DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3659 /* Type are looked up using the DECL_NAME, as that is what the rest of the
3660 compiler wants to use. */
3661 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
3662 || TREE_CODE (x) == NAMESPACE_DECL)
3663 name = DECL_NAME (x);
3668 /* Not needed...see below. */
3672 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3673 name = TREE_OPERAND (name, 0);
3675 /* Namespace-scoped variables are not found in the current level. */
3676 if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
3677 t = namespace_binding (name, DECL_CONTEXT (x));
3679 t = lookup_name_current_level (name);
3680 if (t == error_mark_node)
3682 /* error_mark_node is 0 for a while during initialization! */
3684 cp_error_at ("`%#D' used prior to declaration", x);
3687 else if (t != NULL_TREE)
3690 /* This is turned off until I have time to do it right (bpk). */
3691 /* With the code below that uses it... */
3692 file = DECL_SOURCE_FILE (t);
3693 line = DECL_SOURCE_LINE (t);
3695 if (TREE_CODE (t) == PARM_DECL)
3697 if (DECL_CONTEXT (t) == NULL_TREE)
3698 fatal ("parse errors have confused me too much");
3700 /* Check for duplicate params. */
3701 if (duplicate_decls (x, t))
3704 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
3705 || DECL_FUNCTION_TEMPLATE_P (x))
3706 && is_overloaded_fn (t))
3707 /* Don't do anything just yet. */;
3708 else if (t == wchar_decl_node)
3710 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3711 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
3713 /* Throw away the redeclaration. */
3716 else if (TREE_CODE (t) != TREE_CODE (x))
3718 if (duplicate_decls (x, t))
3721 else if (duplicate_decls (x, t))
3724 /* This is turned off until I have time to do it right (bpk). */
3726 /* Also warn if they did a prototype with `static' on it, but
3727 then later left the `static' off. */
3728 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
3730 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3735 cp_warning ("`static' missing from declaration of `%D'",
3737 warning_with_file_and_line (file, line,
3738 "previous declaration of `%s'",
3739 decl_as_string (t, 0));
3742 /* Now fix things so it'll do what they expect. */
3743 if (current_function_decl)
3744 TREE_PUBLIC (current_function_decl) = 0;
3746 /* Due to interference in memory reclamation (X may be
3747 obstack-deallocated at this point), we must guard against
3748 one really special case. [jason: This should be handled
3749 by start_function] */
3750 if (current_function_decl == x)
3751 current_function_decl = t;
3753 if (TREE_CODE (t) == TYPE_DECL)
3754 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3755 else if (TREE_CODE (t) == FUNCTION_DECL)
3756 check_default_args (t);
3760 else if (DECL_MAIN_P (x))
3762 /* A redeclaration of main, but not a duplicate of the
3767 This function shall not be overloaded. */
3768 cp_error_at ("invalid redeclaration of `%D'", t);
3769 cp_error ("as `%D'", x);
3770 /* We don't try to push this declaration since that
3776 check_template_shadow (x);
3778 /* If this is a function conjured up by the backend, massage it
3779 so it looks friendly. */
3780 if (TREE_CODE (x) == FUNCTION_DECL
3781 && ! DECL_LANG_SPECIFIC (x))
3783 retrofit_lang_decl (x);
3784 DECL_LANGUAGE (x) = lang_c;
3787 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3789 t = push_overloaded_decl (x, PUSH_LOCAL);
3792 if (!namespace_bindings_p ())
3793 /* We do not need to create a binding for this name;
3794 push_overloaded_decl will have already done so if
3796 need_new_binding = 0;
3798 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3800 t = push_overloaded_decl (x, PUSH_GLOBAL);
3802 add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3806 /* If declaring a type as a typedef, copy the type (unless we're
3807 at line 0), and install this TYPE_DECL as the new type's typedef
3808 name. See the extensive comment in ../c-decl.c (pushdecl). */
3809 if (TREE_CODE (x) == TYPE_DECL)
3811 tree type = TREE_TYPE (x);
3812 if (DECL_SOURCE_LINE (x) == 0)
3814 if (TYPE_NAME (type) == 0)
3815 TYPE_NAME (type) = x;
3817 else if (type != error_mark_node && TYPE_NAME (type) != x
3818 /* We don't want to copy the type when all we're
3819 doing is making a TYPE_DECL for the purposes of
3821 && (!TYPE_NAME (type)
3822 || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3824 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
3826 DECL_ORIGINAL_TYPE (x) = type;
3827 type = build_type_copy (type);
3828 TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3829 TYPE_NAME (type) = x;
3830 TREE_TYPE (x) = type;
3835 if (type != error_mark_node
3837 && TYPE_IDENTIFIER (type))
3838 set_identifier_type_value_with_scope (DECL_NAME (x), type,
3839 current_binding_level);
3843 /* Multiple external decls of the same identifier ought to match.
3845 We get warnings about inline functions where they are defined.
3846 We get warnings about other functions from push_overloaded_decl.
3848 Avoid duplicate warnings where they are used. */
3849 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3853 if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
3854 && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
3855 || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
3856 decl = IDENTIFIER_NAMESPACE_VALUE (name);
3861 /* If different sort of thing, we already gave an error. */
3862 && TREE_CODE (decl) == TREE_CODE (x)
3863 && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3865 cp_pedwarn ("type mismatch with previous external decl", x);
3866 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3870 /* This name is new in its binding level.
3871 Install the new declaration and return it. */
3872 if (namespace_bindings_p ())
3874 /* Install a global value. */
3876 /* If the first global decl has external linkage,
3877 warn if we later see static one. */
3878 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3879 TREE_PUBLIC (name) = 1;
3881 if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3883 /* For an ordinary function, we create a binding from
3884 the mangled name (i.e., NAME) to the DECL. But, for
3885 an `extern "C"' function, the mangled name and the
3886 ordinary name are the same so we need not do this. */
3887 && !(TREE_CODE (x) == FUNCTION_DECL &&
3888 DECL_LANGUAGE (x) == lang_c))
3890 if (TREE_CODE (x) == FUNCTION_DECL)
3892 ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
3893 || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
3894 SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3897 /* Don't forget if the function was used via an implicit decl. */
3898 if (IDENTIFIER_IMPLICIT_DECL (name)
3899 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3902 /* Don't forget if its address was taken in that way. */
3903 if (IDENTIFIER_IMPLICIT_DECL (name)
3904 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3905 TREE_ADDRESSABLE (x) = 1;
3907 /* Warn about mismatches against previous implicit decl. */
3908 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3909 /* If this real decl matches the implicit, don't complain. */
3910 && ! (TREE_CODE (x) == FUNCTION_DECL
3911 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3913 ("`%D' was previously implicitly declared to return `int'", x);
3915 /* If new decl is `static' and an `extern' was seen previously,
3917 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3918 warn_extern_redeclared_static (x, t);
3922 /* Here to install a non-global value. */
3923 tree oldlocal = IDENTIFIER_VALUE (name);
3924 tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3926 if (need_new_binding)
3928 push_local_binding (name, x, 0);
3929 /* Because push_local_binding will hook X on to the
3930 current_binding_level's name list, we don't want to
3931 do that again below. */
3932 need_new_binding = 0;
3935 /* If this is a TYPE_DECL, push it into the type value slot. */
3936 if (TREE_CODE (x) == TYPE_DECL)
3937 set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3938 current_binding_level);
3940 /* Clear out any TYPE_DECL shadowed by a namespace so that
3941 we won't think this is a type. The C struct hack doesn't
3942 go through namespaces. */
3943 if (TREE_CODE (x) == NAMESPACE_DECL)
3944 set_identifier_type_value_with_scope (name, NULL_TREE,
3945 current_binding_level);
3947 /* If this is an extern function declaration, see if we
3948 have a global definition or declaration for the function. */
3949 if (oldlocal == NULL_TREE
3950 && DECL_EXTERNAL (x)
3951 && oldglobal != NULL_TREE
3952 && TREE_CODE (x) == FUNCTION_DECL
3953 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3955 /* We have one. Their types must agree. */
3956 if (decls_match (x, oldglobal))
3960 cp_warning ("extern declaration of `%#D' doesn't match", x);
3961 cp_warning_at ("global declaration `%#D'", oldglobal);
3964 /* If we have a local external declaration,
3965 and no file-scope declaration has yet been seen,
3966 then if we later have a file-scope decl it must not be static. */
3967 if (oldlocal == NULL_TREE
3968 && oldglobal == NULL_TREE
3969 && DECL_EXTERNAL (x)
3971 TREE_PUBLIC (name) = 1;
3973 if (DECL_FROM_INLINE (x))
3974 /* Inline decls shadow nothing. */;
3976 /* Warn if shadowing an argument at the top level of the body. */
3977 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3978 && TREE_CODE (oldlocal) == PARM_DECL
3979 /* Don't complain if it's from an enclosing function. */
3980 && DECL_CONTEXT (oldlocal) == current_function_decl
3981 && TREE_CODE (x) != PARM_DECL)
3983 /* Go to where the parms should be and see if we
3985 struct binding_level *b = current_binding_level->level_chain;
3991 if (b->parm_flag == 1)
3992 cp_error ("declaration of `%#D' shadows a parameter", name);
3994 else if (warn_shadow && oldlocal != NULL_TREE
3995 && current_binding_level->is_for_scope
3996 && !DECL_DEAD_FOR_LOCAL (oldlocal))
3998 warning ("variable `%s' shadows local",
3999 IDENTIFIER_POINTER (name));
4000 cp_warning_at (" this is the shadowed declaration", oldlocal);
4002 /* Maybe warn if shadowing something else. */
4003 else if (warn_shadow && !DECL_EXTERNAL (x)
4004 /* No shadow warnings for internally generated vars. */
4005 && ! DECL_ARTIFICIAL (x)
4006 /* No shadow warnings for vars made for inlining. */
4007 && ! DECL_FROM_INLINE (x))
4009 const char *warnstring = NULL;
4011 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
4012 warnstring = "declaration of `%s' shadows a parameter";
4013 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4014 && current_class_ptr
4015 && !TREE_STATIC (name))
4016 warnstring = "declaration of `%s' shadows a member of `this'";
4017 else if (oldlocal != NULL_TREE)
4018 warnstring = "declaration of `%s' shadows previous local";
4019 else if (oldglobal != NULL_TREE)
4020 /* XXX shadow warnings in outer-more namespaces */
4021 warnstring = "declaration of `%s' shadows global declaration";
4024 warning (warnstring, IDENTIFIER_POINTER (name));
4028 if (TREE_CODE (x) == FUNCTION_DECL)
4029 check_default_args (x);
4031 /* Keep count of variables in this level with incomplete type. */
4032 if (TREE_CODE (x) == VAR_DECL
4033 && TREE_TYPE (x) != error_mark_node
4034 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4035 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4036 /* RTTI TD entries are created while defining the type_info. */
4037 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4038 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
4039 current_binding_level->incomplete
4040 = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
4043 if (need_new_binding)
4044 add_decl_to_level (x,
4045 DECL_NAMESPACE_SCOPE_P (x)
4046 ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4047 : current_binding_level);
4052 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
4053 caller to set DECL_CONTEXT properly. */
4056 pushdecl_with_scope (x, level)
4058 struct binding_level *level;
4060 register struct binding_level *b;
4061 tree function_decl = current_function_decl;
4063 current_function_decl = NULL_TREE;
4064 if (level->parm_flag == 2)
4066 b = class_binding_level;
4067 class_binding_level = level;
4068 pushdecl_class_level (x);
4069 class_binding_level = b;
4073 b = current_binding_level;
4074 current_binding_level = level;
4076 current_binding_level = b;
4078 current_function_decl = function_decl;
4082 /* Like pushdecl, only it places X in the current namespace,
4086 pushdecl_namespace_level (x)
4089 register struct binding_level *b = current_binding_level;
4092 t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4094 /* Now, the type_shadowed stack may screw us. Munge it so it does
4096 if (TREE_CODE (x) == TYPE_DECL)
4098 tree name = DECL_NAME (x);
4100 tree *ptr = (tree *)0;
4101 for (; b != global_binding_level; b = b->level_chain)
4103 tree shadowed = b->type_shadowed;
4104 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4105 if (TREE_PURPOSE (shadowed) == name)
4107 ptr = &TREE_VALUE (shadowed);
4108 /* Can't break out of the loop here because sometimes
4109 a binding level will have duplicate bindings for
4110 PT names. It's gross, but I haven't time to fix it. */
4113 newval = TREE_TYPE (x);
4114 if (ptr == (tree *)0)
4116 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
4117 up here if this is changed to an assertion. --KR */
4118 SET_IDENTIFIER_TYPE_VALUE (name, newval);
4128 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4132 pushdecl_top_level (x)
4135 push_to_top_level ();
4136 x = pushdecl_namespace_level (x);
4137 pop_from_top_level ();
4141 /* Make the declaration of X appear in CLASS scope. */
4144 pushdecl_class_level (x)
4147 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
4148 scope looks for the pre-mangled name. */