1 /* Process declarations and variables for C compiler.
2 Copyright (C) 1988, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Hacked 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. */
38 #include <sys/types.h>
43 #define obstack_chunk_alloc xmalloc
44 #define obstack_chunk_free free
46 extern tree builtin_return_address_fndecl;
48 extern struct obstack permanent_obstack;
50 extern int current_class_depth;
52 extern tree cleanups_this_call;
54 extern tree static_ctors, static_dtors;
56 /* Stack of places to restore the search obstack back to. */
58 /* Obstack used for remembering local class declarations (like
59 enums and static (const) members. */
61 struct obstack decl_obstack;
62 static struct stack_level *decl_stack;
64 #ifndef CHAR_TYPE_SIZE
65 #define CHAR_TYPE_SIZE BITS_PER_UNIT
68 #ifndef SHORT_TYPE_SIZE
69 #define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
73 #define INT_TYPE_SIZE BITS_PER_WORD
76 #ifndef LONG_TYPE_SIZE
77 #define LONG_TYPE_SIZE BITS_PER_WORD
80 #ifndef LONG_LONG_TYPE_SIZE
81 #define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
84 #ifndef WCHAR_UNSIGNED
85 #define WCHAR_UNSIGNED 0
88 #ifndef FLOAT_TYPE_SIZE
89 #define FLOAT_TYPE_SIZE BITS_PER_WORD
92 #ifndef DOUBLE_TYPE_SIZE
93 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
96 #ifndef LONG_DOUBLE_TYPE_SIZE
97 #define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
100 #ifndef BOOL_TYPE_SIZE
101 #ifdef SLOW_BYTE_ACCESS
102 #define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
104 #define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
108 /* We let tm.h override the types used here, to handle trivial differences
109 such as the choice of unsigned int or long unsigned int for size_t.
110 When machines start needing nontrivial differences in the size type,
111 it would be best to do something here to figure out automatically
112 from other information what type to use. */
115 #define SIZE_TYPE "long unsigned int"
119 #define PTRDIFF_TYPE "long int"
123 #define WCHAR_TYPE "int"
126 static tree grokparms PROTO((tree, int));
127 static tree lookup_nested_type PROTO((tree, tree));
128 static char *redeclaration_error_message PROTO((tree, tree));
131 PROTO((char *, tree, enum built_in_function, void (*)(), char *));
133 /* a node which has tree code ERROR_MARK, and whose type is itself.
134 All erroneous expressions are replaced with this node. All functions
135 that accept nodes as arguments should avoid generating error messages
136 if this node is one of the arguments, since it is undesirable to get
137 multiple error messages from one error in the input. */
139 tree error_mark_node;
141 /* Erroneous argument lists can use this *IFF* they do not modify it. */
142 tree error_mark_list;
144 /* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
146 tree short_integer_type_node;
147 tree integer_type_node;
148 tree long_integer_type_node;
149 tree long_long_integer_type_node;
151 tree short_unsigned_type_node;
152 tree unsigned_type_node;
153 tree long_unsigned_type_node;
154 tree long_long_unsigned_type_node;
156 tree ptrdiff_type_node;
158 tree unsigned_char_type_node;
159 tree signed_char_type_node;
161 tree wchar_type_node;
162 tree signed_wchar_type_node;
163 tree unsigned_wchar_type_node;
165 tree wchar_decl_node;
167 tree float_type_node;
168 tree double_type_node;
169 tree long_double_type_node;
171 tree intQI_type_node;
172 tree intHI_type_node;
173 tree intSI_type_node;
174 tree intDI_type_node;
176 tree unsigned_intQI_type_node;
177 tree unsigned_intHI_type_node;
178 tree unsigned_intSI_type_node;
179 tree unsigned_intDI_type_node;
181 /* a VOID_TYPE node, and the same, packaged in a TREE_LIST. */
183 tree void_type_node, void_list_node;
186 /* Nodes for types `void *' and `const void *'. */
188 tree ptr_type_node, const_ptr_type_node;
190 /* Nodes for types `char *' and `const char *'. */
192 tree string_type_node, const_string_type_node;
194 /* Type `char[256]' or something like it.
195 Used when an array of char is needed and the size is irrelevant. */
197 tree char_array_type_node;
199 /* Type `int[256]' or something like it.
200 Used when an array of int needed and the size is irrelevant. */
202 tree int_array_type_node;
204 /* Type `wchar_t[256]' or something like it.
205 Used when a wide string literal is created. */
207 tree wchar_array_type_node;
209 /* The bool data type, and constants */
210 tree boolean_type_node, boolean_true_node, boolean_false_node;
212 /* type `int ()' -- used for implicit declaration of functions. */
214 tree default_function_type;
216 /* function types `double (double)' and `double (double, double)', etc. */
218 tree double_ftype_double, double_ftype_double_double;
219 tree int_ftype_int, long_ftype_long;
220 tree float_ftype_float;
221 tree ldouble_ftype_ldouble;
223 /* Function type `int (const void *, const void *, size_t)' */
224 static tree int_ftype_cptr_cptr_sizet;
227 tree vtable_entry_type;
228 tree delta_type_node;
230 /* Old rtti stuff. */
231 tree __baselist_desc_type_node;
232 tree __i_desc_type_node, __m_desc_type_node;
233 tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
235 tree __t_desc_type_node;
237 tree __tp_desc_type_node;
239 tree __access_mode_type_node;
240 tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
241 tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
242 tree __ptmf_desc_type_node, __ptmd_desc_type_node;
244 /* Not needed yet? May be needed one day? */
245 tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
246 tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
247 tree __ptmf_desc_array_type, __ptmd_desc_array_type;
250 tree class_star_type_node;
251 tree class_type_node, record_type_node, union_type_node, enum_type_node;
252 tree unknown_type_node;
253 tree opaque_type_node, signature_type_node;
254 tree sigtable_entry_type;
256 /* Array type `vtable_entry_type[]' */
259 /* In a destructor, the point at which all derived class destroying
260 has been done, just before any base class destroying will be done. */
264 /* In a destructor, the last insn emitted after the start of the
265 function and the parms. */
269 /* In a constructor, the point at which we are ready to return
270 the pointer to the initialized object. */
274 /* A FUNCTION_DECL which can call `abort'. Not necessarily the
275 one that the user will declare, but sufficient to be called
276 by routines that want to abort the program. */
280 extern rtx cleanup_label, return_label;
282 /* If original DECL_RESULT of current function was a register,
283 but due to being an addressable named return value, would up
284 on the stack, this variable holds the named return value's
285 original location. */
286 rtx original_result_rtx;
288 /* Sequence of insns which represents base initialization. */
291 /* C++: Keep these around to reduce calls to `get_identifier'.
292 Identifiers for `this' in member functions and the auto-delete
293 parameter for destructors. */
294 tree this_identifier, in_charge_identifier;
295 tree ctor_identifier, dtor_identifier;
296 /* Used in pointer to member functions, in vtables, and in sigtables. */
297 tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
298 tree pfn_or_delta2_identifier, tag_identifier;
299 tree vt_off_identifier;
301 struct named_label_list
303 struct binding_level *binding_level;
306 char *filename_o_goto;
308 struct named_label_list *next;
311 /* A list (chain of TREE_LIST nodes) of named label uses.
312 The TREE_PURPOSE field is the list of variables defined
313 the the label's scope defined at the point of use.
314 The TREE_VALUE field is the LABEL_DECL used.
315 The TREE_TYPE field holds `current_binding_level' at the
316 point of the label's use.
318 BWAHAHAAHAHahhahahahaah. No, no, no, said the little chicken.
320 Look at the pretty struct named_label_list. See the pretty struct
321 with the pretty named fields that describe what they do. See the
322 pretty lack of gratuitous casts. Notice the code got a lot cleaner.
324 Used only for jumps to as-yet undefined labels, since
325 jumps to defined labels can have their validity checked
328 static struct named_label_list *named_label_uses = NULL;
330 /* A list of objects which have constructors or destructors
331 which reside in the global scope. The decl is stored in
332 the TREE_VALUE slot and the initializer is stored
333 in the TREE_PURPOSE slot. */
334 tree static_aggregates;
338 /* Two expressions that are constants with value zero.
339 The first is of type `int', the second of type `void *'. */
341 tree integer_zero_node;
342 tree null_pointer_node;
344 /* The value for __null (NULL), either of type `void *' or, with -ansi,
345 an integer type of the same size. */
349 /* A node for the integer constants 1, 2, and 3. */
351 tree integer_one_node, integer_two_node, integer_three_node;
353 /* Nonzero if we have seen an invalid cross reference
354 to a struct, union, or enum, but not yet printed the message. */
356 tree pending_invalid_xref;
357 /* File and line to appear in the eventual error message. */
358 char *pending_invalid_xref_file;
359 int pending_invalid_xref_line;
361 /* While defining an enum type, this is 1 plus the last enumerator
364 static tree enum_next_value;
366 /* Nonzero means that there was overflow computing enum_next_value. */
368 static int enum_overflow;
370 /* Parsing a function declarator leaves a list of parameter names
371 or a chain or parameter decls here. */
373 tree last_function_parms;
375 /* Parsing a function declarator leaves here a chain of structure
376 and enum types declared in the parmlist. */
378 static tree last_function_parm_tags;
380 /* After parsing the declarator that starts a function definition,
381 `start_function' puts here the list of parameter names or chain of decls.
382 `store_parm_decls' finds it here. */
384 static tree current_function_parms;
386 /* Similar, for last_function_parm_tags. */
387 static tree current_function_parm_tags;
389 /* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
390 that have names. Here so we can clear out their names' definitions
391 at the end of the function. */
393 static tree named_labels;
395 /* A list of LABEL_DECLs from outer contexts that are currently shadowed. */
397 static tree shadowed_labels;
399 /* The FUNCTION_DECL for the function currently being compiled,
400 or 0 if between functions. */
401 tree current_function_decl;
403 /* Set to 0 at beginning of a function definition, set to 1 if
404 a return statement that specifies a return value is seen. */
406 int current_function_returns_value;
408 /* Set to 0 at beginning of a function definition, set to 1 if
409 a return statement with no argument is seen. */
411 int current_function_returns_null;
413 /* Set to 0 at beginning of a function definition, and whenever
414 a label (case or named) is defined. Set to value of expression
415 returned from function when that value can be transformed into
416 a named return value. */
418 tree current_function_return_value;
420 /* Set to nonzero by `grokdeclarator' for a function
421 whose return type is defaulted, if warnings for this are desired. */
423 static int warn_about_return_type;
425 /* Nonzero means give `double' the same size as `float'. */
427 extern int flag_short_double;
429 /* Nonzero means don't recognize any builtin functions. */
431 extern int flag_no_builtin;
433 /* Nonzero means don't recognize the non-ANSI builtin functions.
436 extern int flag_no_nonansi_builtin;
438 /* Nonzero means enable obscure ANSI features and disable GNU extensions
439 that might cause ANSI-compliant code to be miscompiled. */
441 extern int flag_ansi;
443 /* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
445 extern int flag_huge_objects;
447 /* Nonzero if we want to conserve space in the .o files. We do this
448 by putting uninitialized data and runtime initialized data into
449 .common instead of .data at the expense of not flagging multiple
451 extern int flag_conserve_space;
453 /* Pointers to the base and current top of the language name stack. */
455 extern tree *current_lang_base, *current_lang_stack;
457 /* C and C++ flags are in decl2.c. */
459 /* Set to 0 at beginning of a constructor, set to 1
460 if that function does an allocation before referencing its
461 instance variable. */
462 static int current_function_assigns_this;
463 int current_function_just_assigned_this;
465 /* Set to 0 at beginning of a function. Set non-zero when
466 store_parm_decls is called. Don't call store_parm_decls
467 if this flag is non-zero! */
468 int current_function_parms_stored;
470 /* Flag used when debugging spew.c */
472 extern int spew_debug;
474 /* This is a copy of the class_shadowed list of the previous class binding
475 contour when at global scope. It's used to reset IDENTIFIER_CLASS_VALUEs
476 when entering another class scope (i.e. a cache miss). */
477 extern tree previous_class_values;
479 /* A expression of value 0 with the same precision as a sizetype
481 tree signed_size_zero_node;
484 /* Allocate a level of searching. */
487 push_decl_level (stack, obstack)
488 struct stack_level *stack;
489 struct obstack *obstack;
491 struct stack_level tem;
494 return push_stack_level (obstack, (char *)&tem, sizeof (tem));
497 /* For each binding contour we allocate a binding_level structure
498 which records the names defined in that contour.
501 1) one for each function definition,
502 where internal declarations of the parameters appear.
503 2) one for each compound statement,
504 to record its declarations.
506 The current meaning of a name can be found by searching the levels
507 from the current one out to the global one.
509 Off to the side, may be the class_binding_level. This exists only
510 to catch class-local declarations. It is otherwise nonexistent.
512 Also there may be binding levels that catch cleanups that must be
513 run when exceptions occur. */
515 /* Note that the information in the `names' component of the global contour
516 is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers. */
520 /* A chain of _DECL nodes for all variables, constants, functions,
521 and typedef types. These are in the reverse of the order
525 /* A list of structure, union and enum definitions, for looking up
527 It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
528 or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
529 or ENUMERAL_TYPE node.
531 C++: the TREE_VALUE nodes can be simple types for
532 component_bindings. */
535 /* For each level, a list of shadowed outer-level local definitions
536 to be restored when this level is popped.
537 Each link is a TREE_LIST whose TREE_PURPOSE is an identifier and
538 whose TREE_VALUE is its old definition (a kind of ..._DECL node). */
541 /* Same, for IDENTIFIER_CLASS_VALUE. */
544 /* Same, for IDENTIFIER_TYPE_VALUE. */
547 /* For each level (except not the global one),
548 a chain of BLOCK nodes for all the levels
549 that were entered and exited one level down. */
552 /* The BLOCK node for this level, if one has been preallocated.
553 If 0, the BLOCK is allocated (if needed) when the level is popped. */
556 /* The binding level which this one is contained in (inherits from). */
557 struct binding_level *level_chain;
559 /* List of decls in `names' that have incomplete
560 structure or union types. */
563 /* List of VAR_DECLS saved from a previous for statement.
564 These would be dead in ANSI-conforming code, but might
565 be referenced in ARM-era code. */
566 tree dead_vars_from_for;
568 /* 1 for the level that holds the parameters of a function.
569 2 for the level that holds a class declaration.
570 3 for levels that hold parameter declarations. */
571 unsigned parm_flag : 4;
573 /* 1 means make a BLOCK for this level regardless of all else.
574 2 for temporary binding contours created by the compiler. */
577 /* Nonzero if this level "doesn't exist" for tags. */
578 unsigned tag_transparent : 1;
580 /* Nonzero if this level can safely have additional
581 cleanup-needing variables added to it. */
582 unsigned more_cleanups_ok : 1;
583 unsigned have_cleanups : 1;
585 /* Nonzero if this level is for storing the decls for template
586 parameters and generic decls; these decls will be discarded and
587 replaced with a TEMPLATE_DECL. */
588 unsigned pseudo_global : 1;
590 /* This is set for a namespace binding level. */
591 unsigned namespace_p : 1;
593 /* True if this level is that of a for-statement where we need to
594 worry about ambiguous (ARM or ANSI) scope rules. */
595 unsigned is_for_scope : 1;
597 /* Two bits left for this word. */
599 #if defined(DEBUG_CP_BINDING_LEVELS)
600 /* Binding depth at which this level began. */
601 unsigned binding_depth;
602 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
605 #define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
607 /* The (non-class) binding level currently in effect. */
609 static struct binding_level *current_binding_level;
611 /* The binding level of the current class, if any. */
613 static struct binding_level *class_binding_level;
615 /* The current (class or non-class) binding level currently in effect. */
617 #define inner_binding_level \
618 (class_binding_level ? class_binding_level : current_binding_level)
620 /* A chain of binding_level structures awaiting reuse. */
622 static struct binding_level *free_binding_level;
624 /* The outermost binding level, for names of file scope.
625 This is created when the compiler is started and exists
626 through the entire run. */
628 static struct binding_level *global_binding_level;
630 /* Binding level structures are initialized by copying this one. */
632 static struct binding_level clear_binding_level;
634 /* Nonzero means unconditionally make a BLOCK for the next level pushed. */
636 static int keep_next_level_flag;
638 #if defined(DEBUG_CP_BINDING_LEVELS)
639 static int binding_depth = 0;
640 static int is_class_level = 0;
647 for (i = 0; i < binding_depth*2; i++)
650 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
652 static tree pushdecl_with_scope PROTO((tree, struct binding_level *));
655 push_binding_level (newlevel, tag_transparent, keep)
656 struct binding_level *newlevel;
657 int tag_transparent, keep;
659 /* Add this level to the front of the chain (stack) of levels that
661 *newlevel = clear_binding_level;
662 if (class_binding_level)
664 newlevel->level_chain = class_binding_level;
665 class_binding_level = (struct binding_level *)0;
669 newlevel->level_chain = current_binding_level;
671 current_binding_level = newlevel;
672 newlevel->tag_transparent = tag_transparent;
673 newlevel->more_cleanups_ok = 1;
674 newlevel->keep = keep;
675 #if defined(DEBUG_CP_BINDING_LEVELS)
676 newlevel->binding_depth = binding_depth;
678 fprintf (stderr, "push %s level 0x%08x line %d\n",
679 (is_class_level) ? "class" : "block", newlevel, lineno);
682 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
688 if (class_binding_level)
689 current_binding_level = class_binding_level;
691 if (global_binding_level)
693 /* cannot pop a level, if there are none left to pop. */
694 if (current_binding_level == global_binding_level)
695 my_friendly_abort (123);
697 /* Pop the current level, and free the structure for reuse. */
698 #if defined(DEBUG_CP_BINDING_LEVELS)
701 fprintf (stderr, "pop %s level 0x%08x line %d\n",
702 (is_class_level) ? "class" : "block",
703 current_binding_level, lineno);
704 if (is_class_level != (current_binding_level == class_binding_level))
707 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
710 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
712 register struct binding_level *level = current_binding_level;
713 current_binding_level = current_binding_level->level_chain;
714 level->level_chain = free_binding_level;
715 #if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
716 if (level->binding_depth != binding_depth)
718 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
719 free_binding_level = level;
721 class_binding_level = current_binding_level;
722 if (class_binding_level->parm_flag != 2)
723 class_binding_level = 0;
724 while (current_binding_level->parm_flag == 2)
725 current_binding_level = current_binding_level->level_chain;
730 suspend_binding_level ()
732 if (class_binding_level)
733 current_binding_level = class_binding_level;
735 if (global_binding_level)
737 /* cannot suspend a level, if there are none left to suspend. */
738 if (current_binding_level == global_binding_level)
739 my_friendly_abort (123);
741 /* Suspend the current level. */
742 #if defined(DEBUG_CP_BINDING_LEVELS)
745 fprintf (stderr, "suspend %s level 0x%08x line %d\n",
746 (is_class_level) ? "class" : "block",
747 current_binding_level, lineno);
748 if (is_class_level != (current_binding_level == class_binding_level))
751 fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
754 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
756 current_binding_level = current_binding_level->level_chain;
757 class_binding_level = current_binding_level;
758 if (class_binding_level->parm_flag != 2)
759 class_binding_level = 0;
760 while (current_binding_level->parm_flag == 2)
761 current_binding_level = current_binding_level->level_chain;
766 resume_binding_level (b)
767 struct binding_level *b;
769 if (class_binding_level)
772 /* These are here because we cannot deal with shadows yet. */
773 sorry ("cannot resume a namespace inside class");
776 b->level_chain = class_binding_level;
777 class_binding_level = (struct binding_level *)0;
783 /* These are here because we cannot deal with shadows yet. */
784 if (b->level_chain != current_binding_level)
786 sorry ("cannot resume a namespace inside a different namespace");
790 b->level_chain = current_binding_level;
792 current_binding_level = b;
793 #if defined(DEBUG_CP_BINDING_LEVELS)
794 b->binding_depth = binding_depth;
796 fprintf (stderr, "resume %s level 0x%08x line %d\n",
797 (is_class_level) ? "class" : "block", b, lineno);
800 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
803 /* Create a new `struct binding_level'. */
806 struct binding_level *
807 make_binding_level ()
810 return (struct binding_level *) xmalloc (sizeof (struct binding_level));
813 /* Nonzero if we are currently in the global binding level. */
818 return current_binding_level == global_binding_level;
821 /* Nonzero if we are currently in a toplevel binding level. This
822 means either the global binding level or a namespace in a toplevel
826 toplevel_bindings_p ()
828 struct binding_level *b = current_binding_level;
832 if (b == global_binding_level)
834 if (b->pseudo_global)
836 if (! b->namespace_p)
842 /* Nonzero if this is a namespace scope. */
845 namespace_bindings_p ()
847 return current_binding_level->namespace_p;
853 keep_next_level_flag = 1;
856 /* Nonzero if the current level needs to have a BLOCK made. */
861 return (current_binding_level->blocks != NULL_TREE
862 || current_binding_level->keep
863 || current_binding_level->names != NULL_TREE
864 || (current_binding_level->tags != NULL_TREE
865 && !current_binding_level->tag_transparent));
868 /* Identify this binding level as a level of parameters. */
871 declare_parm_level ()
873 current_binding_level->parm_flag = 1;
877 declare_pseudo_global_level ()
879 current_binding_level->pseudo_global = 1;
883 declare_namespace_level ()
885 current_binding_level->namespace_p = 1;
889 pseudo_global_level_p ()
891 return current_binding_level->pseudo_global;
895 set_class_shadows (shadows)
898 class_binding_level->class_shadowed = shadows;
901 /* Enter a new binding level.
902 If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
903 not for that of tags. */
906 pushlevel (tag_transparent)
909 register struct binding_level *newlevel = NULL_BINDING_LEVEL;
911 /* If this is the top level of a function,
912 just make sure that NAMED_LABELS is 0.
913 They should have been set to 0 at the end of the previous function. */
915 if (current_binding_level == global_binding_level)
916 my_friendly_assert (named_labels == NULL_TREE, 134);
918 /* Reuse or create a struct for this binding level. */
920 #if defined(DEBUG_CP_BINDING_LEVELS)
922 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
923 if (free_binding_level)
924 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
926 newlevel = free_binding_level;
927 free_binding_level = free_binding_level->level_chain;
931 newlevel = make_binding_level ();
934 push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
935 GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
936 keep_next_level_flag = 0;
940 note_level_for_for ()
942 current_binding_level->is_for_scope = 1;
946 pushlevel_temporary (tag_transparent)
949 pushlevel (tag_transparent);
950 current_binding_level->keep = 2;
953 /* Note we don't call push_momentary() here. Otherwise, it would cause
954 cleanups to be allocated on the momentary obstack, and they will be
955 overwritten by the next statement. */
957 expand_start_bindings (0);
960 /* Exit a binding level.
961 Pop the level off, and restore the state of the identifier-decl mappings
962 that were in effect when this level was entered.
964 If KEEP == 1, this level had explicit declarations, so
965 and create a "block" (a BLOCK node) for the level
966 to record its declarations and subblocks for symbol table output.
968 If KEEP == 2, this level's subblocks go to the front,
969 not the back of the current binding level. This happens,
970 for instance, when code for constructors and destructors
971 need to generate code at the end of a function which must
972 be moved up to the front of the function.
974 If FUNCTIONBODY is nonzero, this level is the body of a function,
975 so create a block as if KEEP were set and also clear out all
978 If REVERSE is nonzero, reverse the order of decls before putting
979 them into the BLOCK. */
982 poplevel (keep, reverse, functionbody)
988 /* The chain of decls was accumulated in reverse order.
989 Put it into forward order, just for cleanliness. */
991 int tmp = functionbody;
992 int real_functionbody = current_binding_level->keep == 2
993 ? ((functionbody = 0), tmp) : functionbody;
994 tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
995 tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
996 tree block = NULL_TREE;
998 int block_previously_created;
1000 GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1001 (HOST_WIDE_INT) current_binding_level->level_chain,
1002 current_binding_level->parm_flag,
1003 current_binding_level->keep);
1005 if (current_binding_level->keep == 1)
1008 /* Get the decls in the order they were written.
1009 Usually current_binding_level->names is in reverse order.
1010 But parameter decls were previously put in forward order. */
1013 current_binding_level->names
1014 = decls = nreverse (current_binding_level->names);
1016 decls = current_binding_level->names;
1018 /* Output any nested inline functions within this block
1019 if they weren't already output. */
1021 for (decl = decls; decl; decl = TREE_CHAIN (decl))
1022 if (TREE_CODE (decl) == FUNCTION_DECL
1023 && ! TREE_ASM_WRITTEN (decl)
1024 && DECL_INITIAL (decl) != NULL_TREE
1025 && TREE_ADDRESSABLE (decl)
1026 && decl_function_context (decl) == current_function_decl)
1028 /* If this decl was copied from a file-scope decl
1029 on account of a block-scope extern decl,
1030 propagate TREE_ADDRESSABLE to the file-scope decl. */
1031 if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1032 TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1035 push_function_context ();
1036 output_inline_function (decl);
1037 pop_function_context ();
1041 /* If there were any declarations or structure tags in that level,
1042 or if this level is a function body,
1043 create a BLOCK to record them for the life of this function. */
1046 block_previously_created = (current_binding_level->this_block != NULL_TREE);
1047 if (block_previously_created)
1048 block = current_binding_level->this_block;
1049 else if (keep == 1 || functionbody)
1050 block = make_node (BLOCK);
1051 if (block != NULL_TREE)
1053 if (block_previously_created)
1055 if (decls || tags || subblocks)
1057 if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
1059 warning ("internal compiler error: debugging info corrupted");
1061 BLOCK_VARS (block) = decls;
1062 BLOCK_TYPE_TAGS (block) = tags;
1064 /* We can have previous subblocks and new subblocks when
1065 doing fixup_gotos with complex cleanups. We chain the new
1066 subblocks onto the end of any pre-existing subblocks. */
1067 BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1070 /* If we created the block earlier on, and we are just
1071 diddling it now, then it already should have a proper
1072 BLOCK_END_NOTE value associated with it. */
1076 BLOCK_VARS (block) = decls;
1077 BLOCK_TYPE_TAGS (block) = tags;
1078 BLOCK_SUBBLOCKS (block) = subblocks;
1079 /* Otherwise, for a new block, install a new BLOCK_END_NOTE value. */
1080 remember_end_note (block);
1084 /* In each subblock, record that this is its superior. */
1087 for (link = subblocks; link; link = TREE_CHAIN (link))
1088 BLOCK_SUPERCONTEXT (link) = block;
1090 /* Clear out the meanings of the local variables of this level. */
1092 if (current_binding_level->is_for_scope && flag_new_for_scope == 1)
1094 struct binding_level *outer = current_binding_level->level_chain;
1095 for (link = decls; link; link = TREE_CHAIN (link))
1097 if (TREE_CODE (link) == VAR_DECL)
1098 DECL_DEAD_FOR_LOCAL (link) = 1;
1100 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = NULL_TREE;
1103 /* Save declarations made in a 'for' statement so we can support pre-ANSI
1104 'for' scoping semantics. */
1106 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1108 tree id = TREE_PURPOSE (link);
1109 tree decl = IDENTIFIER_LOCAL_VALUE (id);
1111 if (decl && DECL_DEAD_FOR_LOCAL (decl))
1113 /* In this case keep the dead for-decl visible,
1114 but remember what (if anything) it shadowed. */
1115 DECL_SHADOWED_FOR_VAR (decl) = TREE_VALUE (link);
1116 TREE_CHAIN (decl) = outer->dead_vars_from_for;
1117 outer->dead_vars_from_for = decl;
1120 IDENTIFIER_LOCAL_VALUE (id) = TREE_VALUE (link);
1123 else /* Not special for scope. */
1125 for (link = decls; link; link = TREE_CHAIN (link))
1127 if (DECL_NAME (link) != NULL_TREE)
1129 /* If the ident. was used or addressed via a local extern decl,
1130 don't forget that fact. */
1131 if (DECL_EXTERNAL (link))
1133 if (TREE_USED (link))
1134 TREE_USED (DECL_ASSEMBLER_NAME (link)) = 1;
1135 if (TREE_ADDRESSABLE (link))
1136 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1138 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = NULL_TREE;
1142 /* Restore all name-meanings of the outer levels
1143 that were shadowed by this level. */
1145 for (link = current_binding_level->shadowed;
1146 link; link = TREE_CHAIN (link))
1147 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1149 /* We first restore the regular decls and *then* the dead_vars_from_for
1150 to handle this case:
1154 for (int i; ; ) { ...} // i#2
1158 In this case, we want remove the binding for i#3, restoring
1159 that of i#2. Then we want to remove the binding for i#2,
1160 and restore that of i#1. */
1162 link = current_binding_level->dead_vars_from_for;
1163 for (; link != NULL_TREE; link = TREE_CHAIN (link))
1165 tree id = DECL_NAME (link);
1166 if (IDENTIFIER_LOCAL_VALUE (id) == link)
1167 IDENTIFIER_LOCAL_VALUE (id) = DECL_SHADOWED_FOR_VAR (link);
1170 for (link = current_binding_level->class_shadowed;
1171 link; link = TREE_CHAIN (link))
1172 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1173 for (link = current_binding_level->type_shadowed;
1174 link; link = TREE_CHAIN (link))
1175 IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1178 /* If the level being exited is the top level of a function,
1179 check over all the labels. */
1183 /* If this is the top level block of a function,
1184 the vars are the function's parameters.
1185 Don't leave them in the BLOCK because they are
1186 found in the FUNCTION_DECL instead. */
1188 BLOCK_VARS (block) = 0;
1190 /* Clear out the definitions of all label names,
1191 since their scopes end here. */
1193 for (link = named_labels; link; link = TREE_CHAIN (link))
1195 register tree label = TREE_VALUE (link);
1197 if (DECL_INITIAL (label) == NULL_TREE)
1199 cp_error_at ("label `%D' used but not defined", label);
1200 /* Avoid crashing later. */
1201 define_label (input_filename, 1, DECL_NAME (label));
1203 else if (warn_unused && !TREE_USED (label))
1204 cp_warning_at ("label `%D' defined but not used", label);
1205 SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1207 /* Put the labels into the "variables" of the
1208 top-level block, so debugger can see them. */
1209 TREE_CHAIN (label) = BLOCK_VARS (block);
1210 BLOCK_VARS (block) = label;
1213 named_labels = NULL_TREE;
1216 /* Any uses of undefined labels now operate under constraints
1217 of next binding contour. */
1219 struct binding_level *level_chain;
1220 level_chain = current_binding_level->level_chain;
1223 struct named_label_list *labels;
1224 for (labels = named_label_uses; labels; labels = labels->next)
1225 if (labels->binding_level == current_binding_level)
1227 labels->binding_level = level_chain;
1228 labels->names_in_scope = level_chain->names;
1233 tmp = current_binding_level->keep;
1235 pop_binding_level ();
1237 DECL_INITIAL (current_function_decl) = block;
1240 if (!block_previously_created)
1241 current_binding_level->blocks
1242 = chainon (current_binding_level->blocks, block);
1244 /* If we did not make a block for the level just exited,
1245 any blocks made for inner levels
1246 (since they cannot be recorded as subblocks in that level)
1247 must be carried forward so they will later become subblocks
1248 of something else. */
1252 current_binding_level->blocks
1253 = chainon (subblocks, current_binding_level->blocks);
1255 current_binding_level->blocks
1256 = chainon (current_binding_level->blocks, subblocks);
1259 /* Take care of compiler's internal binding structures. */
1262 expand_end_bindings (getdecls (), keep, 1);
1263 /* Each and every BLOCK node created here in `poplevel' is important
1264 (e.g. for proper debugging information) so if we created one
1265 earlier, mark it as "used". */
1267 TREE_USED (block) = 1;
1268 block = poplevel (keep, reverse, real_functionbody);
1271 /* Each and every BLOCK node created here in `poplevel' is important
1272 (e.g. for proper debugging information) so if we created one
1273 earlier, mark it as "used". */
1275 TREE_USED (block) = 1;
1279 /* Resume a binding level for a namespace. */
1283 struct binding_level *b;
1287 resume_binding_level (b);
1289 /* Resume the variable caches. */
1290 decls = current_binding_level->names;
1292 /* Restore the meanings of the local variables of this level. */
1294 for (link = decls; link; link = TREE_CHAIN (link))
1296 /* If it doesn't have a name, there is nothing left to do with it. */
1297 if (DECL_NAME (link) == NULL_TREE)
1300 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = link;
1302 /* If this is a TYPE_DECL, push it into the type value slot. */
1303 if (TREE_CODE (link) == TYPE_DECL)
1304 SET_IDENTIFIER_TYPE_VALUE (DECL_NAME (link), TREE_TYPE (link));
1308 /* Delete the node BLOCK from the current binding level.
1309 This is used for the block inside a stmt expr ({...})
1310 so that the block can be reinserted where appropriate. */
1313 delete_block (block)
1317 if (current_binding_level->blocks == block)
1318 current_binding_level->blocks = TREE_CHAIN (block);
1319 for (t = current_binding_level->blocks; t;)
1321 if (TREE_CHAIN (t) == block)
1322 TREE_CHAIN (t) = TREE_CHAIN (block);
1326 TREE_CHAIN (block) = NULL_TREE;
1327 /* Clear TREE_USED which is always set by poplevel.
1328 The flag is set again if insert_block is called. */
1329 TREE_USED (block) = 0;
1332 /* Insert BLOCK at the end of the list of subblocks of the
1333 current binding level. This is used when a BIND_EXPR is expanded,
1334 to handle the BLOCK node inside the BIND_EXPR. */
1337 insert_block (block)
1340 TREE_USED (block) = 1;
1341 current_binding_level->blocks
1342 = chainon (current_binding_level->blocks, block);
1345 /* Add BLOCK to the current list of blocks for this binding contour. */
1348 add_block_current_level (block)
1351 current_binding_level->blocks
1352 = chainon (current_binding_level->blocks, block);
1355 /* Set the BLOCK node for the innermost scope
1356 (the one we are currently in). */
1360 register tree block;
1362 current_binding_level->this_block = block;
1365 /* Do a pushlevel for class declarations. */
1370 register struct binding_level *newlevel;
1372 /* Reuse or create a struct for this binding level. */
1373 #if defined(DEBUG_CP_BINDING_LEVELS)
1375 #else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1376 if (free_binding_level)
1377 #endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1379 newlevel = free_binding_level;
1380 free_binding_level = free_binding_level->level_chain;
1384 newlevel = make_binding_level ();
1387 #if defined(DEBUG_CP_BINDING_LEVELS)
1389 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1391 push_binding_level (newlevel, 0, 0);
1393 decl_stack = push_decl_level (decl_stack, &decl_obstack);
1394 class_binding_level = current_binding_level;
1395 class_binding_level->parm_flag = 2;
1396 /* We have just pushed into a new binding level. Now, fake out the rest
1397 of the compiler. Set the `current_binding_level' back to point to
1398 the most closely containing non-class binding level. */
1401 current_binding_level = current_binding_level->level_chain;
1403 while (current_binding_level->parm_flag == 2);
1406 /* ...and a poplevel for class declarations. FORCE is used to force
1407 clearing out of CLASS_VALUEs after a class definition. */
1410 poplevel_class (force)
1413 register struct binding_level *level = class_binding_level;
1414 tree block = NULL_TREE;
1417 my_friendly_assert (level != 0, 354);
1419 decl_stack = pop_stack_level (decl_stack);
1420 for (shadowed = level->shadowed; shadowed; shadowed = TREE_CHAIN (shadowed))
1421 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1422 /* If we're leaving a toplevel class, don't bother to do the setting
1423 of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1424 shouldn't even be used when current_class_type isn't set, and second,
1425 if we don't touch it here, we're able to use the cache effect if the
1426 next time we're entering a class scope, it is the same class. */
1427 if (current_class_depth != 1 || force)
1428 for (shadowed = level->class_shadowed;
1430 shadowed = TREE_CHAIN (shadowed))
1431 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1433 /* Remember to save what IDENTIFIER's were bound in this scope so we
1434 can recover from cache misses. */
1436 previous_class_type = current_class_type;
1437 previous_class_values = class_binding_level->class_shadowed;
1439 for (shadowed = level->type_shadowed;
1441 shadowed = TREE_CHAIN (shadowed))
1442 IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed)) = TREE_VALUE (shadowed);
1444 GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1445 (HOST_WIDE_INT) class_binding_level->level_chain,
1446 class_binding_level->parm_flag,
1447 class_binding_level->keep);
1449 if (class_binding_level->parm_flag != 2)
1450 class_binding_level = (struct binding_level *)0;
1452 /* Now, pop out of the the binding level which we created up in the
1453 `pushlevel_class' routine. */
1454 #if defined(DEBUG_CP_BINDING_LEVELS)
1456 #endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1458 pop_binding_level ();
1463 /* For debugging. */
1464 static int no_print_functions = 0;
1465 static int no_print_builtins = 0;
1468 print_binding_level (lvl)
1469 struct binding_level *lvl;
1473 fprintf (stderr, " blocks=");
1474 fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1475 fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
1476 list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
1477 if (lvl->tag_transparent)
1478 fprintf (stderr, " tag-transparent");
1479 if (lvl->more_cleanups_ok)
1480 fprintf (stderr, " more-cleanups-ok");
1481 if (lvl->have_cleanups)
1482 fprintf (stderr, " have-cleanups");
1483 fprintf (stderr, "\n");
1486 fprintf (stderr, " names:\t");
1487 /* We can probably fit 3 names to a line? */
1488 for (t = lvl->names; t; t = TREE_CHAIN (t))
1490 if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1492 if (no_print_builtins
1493 && (TREE_CODE (t) == TYPE_DECL)
1494 && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1497 /* Function decls tend to have longer names. */
1498 if (TREE_CODE (t) == FUNCTION_DECL)
1505 fprintf (stderr, "\n\t");
1508 print_node_brief (stderr, "", t, 0);
1509 if (TREE_CODE (t) == ERROR_MARK)
1513 fprintf (stderr, "\n");
1517 fprintf (stderr, " tags:\t");
1519 for (t = lvl->tags; t; t = TREE_CHAIN (t))
1521 if (TREE_PURPOSE (t) == NULL_TREE)
1523 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1530 fprintf (stderr, "\n\t");
1533 if (TREE_PURPOSE (t) == NULL_TREE)
1535 print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1536 fprintf (stderr, ">");
1538 else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1539 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1542 print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1543 print_node_brief (stderr, "", TREE_VALUE (t), 0);
1544 fprintf (stderr, ">");
1548 fprintf (stderr, "\n");
1552 fprintf (stderr, " shadowed:");
1553 for (t = lvl->shadowed; t; t = TREE_CHAIN (t))
1555 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1557 fprintf (stderr, "\n");
1559 if (lvl->class_shadowed)
1561 fprintf (stderr, " class-shadowed:");
1562 for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1564 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1566 fprintf (stderr, "\n");
1568 if (lvl->type_shadowed)
1570 fprintf (stderr, " type-shadowed:");
1571 for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1573 fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1575 fprintf (stderr, "\n");
1580 print_other_binding_stack (stack)
1581 struct binding_level *stack;
1583 struct binding_level *level;
1584 for (level = stack; level != global_binding_level; level = level->level_chain)
1586 fprintf (stderr, "binding level ");
1587 fprintf (stderr, HOST_PTR_PRINTF, level);
1588 fprintf (stderr, "\n");
1589 print_binding_level (level);
1594 print_binding_stack ()
1596 struct binding_level *b;
1597 fprintf (stderr, "current_binding_level=");
1598 fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
1599 fprintf (stderr, "\nclass_binding_level=");
1600 fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
1601 fprintf (stderr, "\nglobal_binding_level=");
1602 fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
1603 fprintf (stderr, "\n");
1604 if (class_binding_level)
1606 for (b = class_binding_level; b; b = b->level_chain)
1607 if (b == current_binding_level)
1610 b = class_binding_level;
1612 b = current_binding_level;
1615 b = current_binding_level;
1616 print_other_binding_stack (b);
1617 fprintf (stderr, "global:\n");
1618 print_binding_level (global_binding_level);
1621 extern char * first_global_object_name;
1623 /* Get a unique name for each call to this routine for unnamed namespaces.
1624 Mostly copied from get_file_function_name. */
1629 static int temp_name_counter = 0;
1633 if (first_global_object_name)
1634 p = first_global_object_name;
1635 else if (main_input_filename)
1636 p = main_input_filename;
1640 #define UNNAMED_NAMESPACE_FORMAT "__%s_%d"
1642 buf = (char *) alloca (sizeof (UNNAMED_NAMESPACE_FORMAT) + strlen (p));
1644 sprintf (buf, UNNAMED_NAMESPACE_FORMAT, p, temp_name_counter++);
1646 /* Don't need to pull weird characters out of global names. */
1647 if (p != first_global_object_name)
1649 for (p = buf+11; *p; p++)
1650 if (! ((*p >= '0' && *p <= '9')
1651 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
1654 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
1657 || (*p >= 'A' && *p <= 'Z')
1658 || (*p >= 'a' && *p <= 'z')))
1662 return get_identifier (buf);
1665 /* Push into the scope of the NAME namespace. If NAME is NULL_TREE, then we
1666 select a name that is unique to this compilation unit. */
1669 push_namespace (name)
1672 extern tree current_namespace;
1673 tree old_id = get_namespace_id ();
1679 /* Create a truly ugly name! */
1680 name = get_unique_name ();
1683 d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
1685 /* Mark them as external, so redeclaration_error_message doesn't think
1686 they are duplicates. */
1688 DECL_EXTERNAL (d) = 1;
1691 if (NAMESPACE_LEVEL (d) == 0)
1693 /* This is new for this compilation unit. */
1695 declare_namespace_level ();
1696 NAMESPACE_LEVEL (d) = current_binding_level;
1699 resume_level (NAMESPACE_LEVEL (d));
1701 /* This code is just is bit old now... */
1702 current_namespace = tree_cons (NULL_TREE, name, current_namespace);
1703 buf = (char *) alloca (4 + (old_id ? IDENTIFIER_LENGTH (old_id) : 0)
1704 + IDENTIFIER_LENGTH (name));
1705 sprintf (buf, "%s%s", old_id ? IDENTIFIER_POINTER (old_id) : "",
1706 IDENTIFIER_POINTER (name));
1707 TREE_PURPOSE (current_namespace) = get_identifier (buf);
1710 /* Pop from the scope of the current namespace. */
1715 extern tree current_namespace;
1717 current_namespace = TREE_CHAIN (current_namespace);
1719 /* Just in case we get out of sync. */
1720 if (! namespace_bindings_p ())
1723 decls = current_binding_level->names;
1725 /* Clear out the meanings of the local variables of this level. */
1727 for (link = decls; link; link = TREE_CHAIN (link))
1729 if (DECL_NAME (link) != NULL_TREE)
1731 /* If the ident. was used or addressed via a local extern decl,
1732 don't forget that fact. */
1733 if (DECL_EXTERNAL (link))
1735 if (TREE_USED (link))
1736 TREE_USED (DECL_ASSEMBLER_NAME (link)) = 1;
1737 if (TREE_ADDRESSABLE (link))
1738 TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (link)) = 1;
1740 IDENTIFIER_LOCAL_VALUE (DECL_NAME (link)) = NULL_TREE;
1744 /* Restore all name-meanings of the outer levels
1745 that were shadowed by this level. */
1747 for (link = current_binding_level->shadowed; link; link = TREE_CHAIN (link))
1748 IDENTIFIER_LOCAL_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1749 for (link = current_binding_level->class_shadowed;
1750 link; link = TREE_CHAIN (link))
1751 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1752 for (link = current_binding_level->type_shadowed;
1753 link; link = TREE_CHAIN (link))
1754 IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link)) = TREE_VALUE (link);
1756 /* suspend a level. */
1757 suspend_binding_level ();
1760 /* Subroutines for reverting temporarily to top-level for instantiation
1761 of templates and such. We actually need to clear out the class- and
1762 local-value slots of all identifiers, so that only the global values
1763 are at all visible. Simply setting current_binding_level to the global
1764 scope isn't enough, because more binding levels may be pushed. */
1765 struct saved_scope {
1766 struct binding_level *old_binding_level;
1768 struct saved_scope *prev;
1769 tree class_name, class_type, function_decl;
1770 struct binding_level *class_bindings;
1771 tree *lang_base, *lang_stack, lang_name;
1773 int minimal_parse_mode;
1774 tree last_function_parms;
1775 tree template_parms;
1776 HOST_WIDE_INT processing_template_decl;
1777 tree previous_class_type, previous_class_values;
1779 static struct saved_scope *current_saved_scope;
1782 store_bindings (names, old_bindings)
1783 tree names, old_bindings;
1786 for (t = names; t; t = TREE_CHAIN (t))
1788 tree binding, t1, id;
1790 if (TREE_CODE (t) == TREE_LIST)
1791 id = TREE_PURPOSE (t);
1796 || (!IDENTIFIER_LOCAL_VALUE (id)
1797 && !IDENTIFIER_CLASS_VALUE (id)))
1800 for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
1801 if (TREE_VEC_ELT (t1, 0) == id)
1804 binding = make_tree_vec (4);
1807 my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
1808 TREE_VEC_ELT (binding, 0) = id;
1809 TREE_VEC_ELT (binding, 1) = IDENTIFIER_TYPE_VALUE (id);
1810 TREE_VEC_ELT (binding, 2) = IDENTIFIER_LOCAL_VALUE (id);
1811 TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
1812 IDENTIFIER_LOCAL_VALUE (id) = NULL_TREE;
1813 IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
1815 TREE_CHAIN (binding) = old_bindings;
1816 old_bindings = binding;
1820 return old_bindings;
1824 maybe_push_to_top_level (pseudo)
1827 extern int current_lang_stacksize;
1828 struct saved_scope *s =
1829 (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
1830 struct binding_level *b = inner_binding_level;
1831 tree old_bindings = NULL_TREE;
1833 if (current_function_decl)
1834 push_cp_function_context (NULL_TREE);
1836 if (previous_class_type)
1837 old_bindings = store_bindings (previous_class_values, old_bindings);
1839 /* Have to include global_binding_level, because class-level decls
1840 aren't listed anywhere useful. */
1841 for (; b; b = b->level_chain)
1845 if (b == global_binding_level || (pseudo && b->pseudo_global))
1848 old_bindings = store_bindings (b->names, old_bindings);
1849 /* We also need to check class_shadowed to save class-level type
1850 bindings, since pushclass doesn't fill in b->names. */
1851 if (b->parm_flag == 2)
1852 old_bindings = store_bindings (b->class_shadowed, old_bindings);
1854 /* Unwind type-value slots back to top level. */
1855 for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
1856 SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
1859 s->old_binding_level = current_binding_level;
1860 current_binding_level = b;
1862 s->class_name = current_class_name;
1863 s->class_type = current_class_type;
1864 s->function_decl = current_function_decl;
1865 s->class_bindings = class_binding_level;
1866 s->lang_stack = current_lang_stack;
1867 s->lang_base = current_lang_base;
1868 s->lang_stacksize = current_lang_stacksize;
1869 s->lang_name = current_lang_name;
1870 s->minimal_parse_mode = minimal_parse_mode;
1871 s->last_function_parms = last_function_parms;
1872 s->template_parms = current_template_parms;
1873 s->processing_template_decl = processing_template_decl;
1874 s->previous_class_type = previous_class_type;
1875 s->previous_class_values = previous_class_values;
1877 current_class_name = current_class_type = NULL_TREE;
1878 current_function_decl = NULL_TREE;
1879 class_binding_level = (struct binding_level *)0;
1880 current_lang_stacksize = 10;
1881 current_lang_stack = current_lang_base
1882 = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
1883 current_lang_name = lang_name_cplusplus;
1884 strict_prototype = strict_prototypes_lang_cplusplus;
1885 named_labels = NULL_TREE;
1886 minimal_parse_mode = 0;
1887 previous_class_type = previous_class_values = NULL_TREE;
1890 current_template_parms = NULL_TREE;
1891 processing_template_decl = 0;
1894 s->prev = current_saved_scope;
1895 s->old_bindings = old_bindings;
1896 current_saved_scope = s;
1898 push_obstacks (&permanent_obstack, &permanent_obstack);
1902 push_to_top_level ()
1904 maybe_push_to_top_level (0);
1908 pop_from_top_level ()
1910 extern int current_lang_stacksize;
1911 struct saved_scope *s = current_saved_scope;
1914 /* Clear out class-level bindings cache. */
1915 if (previous_class_type)
1918 previous_class_type = NULL_TREE;
1923 current_binding_level = s->old_binding_level;
1924 current_saved_scope = s->prev;
1925 for (t = s->old_bindings; t; t = TREE_CHAIN (t))
1927 tree id = TREE_VEC_ELT (t, 0);
1930 IDENTIFIER_TYPE_VALUE (id) = TREE_VEC_ELT (t, 1);
1931 IDENTIFIER_LOCAL_VALUE (id) = TREE_VEC_ELT (t, 2);
1932 IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
1935 current_class_name = s->class_name;
1936 current_class_type = s->class_type;
1937 current_function_decl = s->function_decl;
1938 class_binding_level = s->class_bindings;
1939 free (current_lang_base);
1940 current_lang_base = s->lang_base;
1941 current_lang_stack = s->lang_stack;
1942 current_lang_name = s->lang_name;
1943 current_lang_stacksize = s->lang_stacksize;
1944 if (current_lang_name == lang_name_cplusplus)
1945 strict_prototype = strict_prototypes_lang_cplusplus;
1946 else if (current_lang_name == lang_name_c)
1947 strict_prototype = strict_prototypes_lang_c;
1948 minimal_parse_mode = s->minimal_parse_mode;
1949 last_function_parms = s->last_function_parms;
1950 current_template_parms = s->template_parms;
1951 processing_template_decl = s->processing_template_decl;
1952 previous_class_type = s->previous_class_type;
1953 previous_class_values = s->previous_class_values;
1957 if (current_function_decl)
1958 pop_cp_function_context (NULL_TREE);
1961 /* Push a definition of struct, union or enum tag "name".
1962 into binding_level "b". "type" should be the type node,
1963 We assume that the tag "name" is not already defined.
1965 Note that the definition may really be just a forward reference.
1966 In that case, the TYPE_SIZE will be a NULL_TREE.
1968 C++ gratuitously puts all these tags in the name space. */
1970 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
1971 record the shadowed value for this binding contour. TYPE is
1972 the type that ID maps to. */
1975 set_identifier_type_value_with_scope (id, type, b)
1978 struct binding_level *b;
1980 if (b != global_binding_level)
1982 tree old_type_value = IDENTIFIER_TYPE_VALUE (id);
1984 = tree_cons (id, old_type_value, b->type_shadowed);
1986 SET_IDENTIFIER_TYPE_VALUE (id, type);
1989 /* As set_identifier_type_value_with_scope, but using inner_binding_level. */
1992 set_identifier_type_value (id, type)
1996 set_identifier_type_value_with_scope (id, type, inner_binding_level);
1999 /* Pop off extraneous binding levels left over due to syntax errors.
2001 We don't pop past namespaces, as they might be valid. */
2006 #ifdef DEBUG_CP_BINDING_LEVELS
2007 fprintf (stderr, "XXX entering pop_everything ()\n");
2009 while (! toplevel_bindings_p () && ! pseudo_global_level_p ())
2011 if (class_binding_level)
2012 pop_nested_class (1);
2016 #ifdef DEBUG_CP_BINDING_LEVELS
2017 fprintf (stderr, "XXX leaving pop_everything ()\n");
2021 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2022 Normally put into into the inner-most non-tag-transparent scope,
2023 but if GLOBALIZE is true, put it in the inner-most non-class scope.
2024 The latter is needed for implicit declarations. */
2027 pushtag (name, type, globalize)
2031 register struct binding_level *b;
2035 b = inner_binding_level;
2036 while (b->tag_transparent
2037 || (globalize && b->parm_flag == 2))
2040 if (toplevel_bindings_p ())
2041 b->tags = perm_tree_cons (name, type, b->tags);
2043 b->tags = saveable_tree_cons (name, type, b->tags);
2047 context = type ? TYPE_CONTEXT (type) : NULL_TREE;
2048 if (! context && ! globalize)
2049 context = current_scope ();
2051 c_decl = TREE_CODE (context) == FUNCTION_DECL
2052 ? context : TYPE_MAIN_DECL (context);
2054 /* Do C++ gratuitous typedefing. */
2055 if (IDENTIFIER_TYPE_VALUE (name) != type)
2060 if (b->parm_flag != 2
2061 || TYPE_SIZE (current_class_type) != NULL_TREE)
2063 d = lookup_nested_type (type, c_decl);
2068 d = build_decl (TYPE_DECL, name, type);
2069 SET_DECL_ARTIFICIAL (d);
2070 set_identifier_type_value_with_scope (name, type, b);
2073 d = TYPE_MAIN_DECL (d);
2075 TYPE_NAME (type) = d;
2076 DECL_CONTEXT (d) = context;
2077 if (! globalize && processing_template_decl && IS_AGGR_TYPE (type))
2078 push_template_decl (d);
2080 if (b->parm_flag == 2)
2081 d = pushdecl_class_level (d);
2083 d = pushdecl_with_scope (d, b);
2087 /* Make nested declarations go into class-level scope. */
2089 d = build_decl (TYPE_DECL, name, type);
2090 SET_DECL_ARTIFICIAL (d);
2091 TYPE_NAME (type) = d;
2092 DECL_CONTEXT (d) = context;
2093 if (! globalize && processing_template_decl && IS_AGGR_TYPE (type))
2094 push_template_decl (d);
2096 d = pushdecl_class_level (d);
2100 if (ANON_AGGRNAME_P (name))
2101 DECL_IGNORED_P (d) = 1;
2103 TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2104 DECL_ASSEMBLER_NAME (d)
2105 = get_identifier (build_overload_name (type, 1, 1));
2108 if (b->parm_flag == 2)
2110 TREE_NONLOCAL_FLAG (type) = 1;
2111 if (TYPE_SIZE (current_class_type) == NULL_TREE)
2112 CLASSTYPE_TAGS (current_class_type) = b->tags;
2116 if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2117 /* Use the canonical TYPE_DECL for this node. */
2118 TYPE_STUB_DECL (type) = TYPE_NAME (type);
2121 /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2122 will be the tagged type we just added to the current
2123 binding level. This fake NULL-named TYPE_DECL node helps
2124 dwarfout.c to know when it needs to output a
2125 representation of a tagged type, and it also gives us a
2126 convenient place to record the "scope start" address for
2129 tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2130 TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2134 /* Counter used to create anonymous type names. */
2136 static int anon_cnt = 0;
2138 /* Return an IDENTIFIER which can be used as a name for
2139 anonymous structs and unions. */
2146 sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2147 return get_identifier (buf);
2150 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2151 This keeps dbxout from getting confused. */
2156 register struct binding_level *b;
2158 static int last_cnt = 0;
2160 /* Fast out if no new anon names were declared. */
2161 if (last_cnt == anon_cnt)
2164 b = current_binding_level;
2165 while (b->tag_transparent)
2170 /* A NULL purpose means we have already processed all tags
2171 from here to the end of the list. */
2172 if (TREE_PURPOSE (tags) == NULL_TREE)
2174 if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2175 TREE_PURPOSE (tags) = NULL_TREE;
2176 tags = TREE_CHAIN (tags);
2178 last_cnt = anon_cnt;
2181 /* Subroutine of duplicate_decls: return truthvalue of whether
2182 or not types of these decls match.
2184 For C++, we must compare the parameter list so that `int' can match
2185 `int&' in a parameter position, but `int&' is not confused with
2189 decls_match (newdecl, olddecl)
2190 tree newdecl, olddecl;
2194 if (TREE_CODE (newdecl) == FUNCTION_DECL
2195 && TREE_CODE (olddecl) == FUNCTION_DECL)
2197 tree f1 = TREE_TYPE (newdecl);
2198 tree f2 = TREE_TYPE (olddecl);
2199 tree p1 = TYPE_ARG_TYPES (f1);
2200 tree p2 = TYPE_ARG_TYPES (f2);
2202 /* When we parse a static member function definition,
2203 we put together a FUNCTION_DECL which thinks its type
2204 is METHOD_TYPE. Change that to FUNCTION_TYPE, and
2206 if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
2207 revert_static_member_fn (&newdecl, &f1, &p1);
2208 else if (TREE_CODE (f2) == METHOD_TYPE
2209 && DECL_STATIC_FUNCTION_P (newdecl))
2210 revert_static_member_fn (&olddecl, &f2, &p2);
2212 /* Here we must take care of the case where new default
2213 parameters are specified. Also, warn if an old
2214 declaration becomes ambiguous because default
2215 parameters may cause the two to be ambiguous. */
2216 if (TREE_CODE (f1) != TREE_CODE (f2))
2218 if (TREE_CODE (f1) == OFFSET_TYPE)
2219 cp_compiler_error ("`%D' redeclared as member function", newdecl);
2221 cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
2225 if (comptypes (TREE_TYPE (f1), TREE_TYPE (f2), 1))
2227 if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
2230 types_match = self_promoting_args_p (p1);
2231 if (p1 == void_list_node)
2232 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2234 else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
2235 && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
2237 types_match = self_promoting_args_p (p2);
2238 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2241 types_match = compparms (p1, p2, 3);
2246 else if (TREE_CODE (newdecl) == TEMPLATE_DECL
2247 && TREE_CODE (olddecl) == TEMPLATE_DECL)
2249 tree newargs = DECL_TEMPLATE_PARMS (newdecl);
2250 tree oldargs = DECL_TEMPLATE_PARMS (olddecl);
2251 int i, len = TREE_VEC_LENGTH (newargs);
2253 if (TREE_VEC_LENGTH (oldargs) != len)
2256 for (i = 0; i < len; i++)
2258 tree newarg = TREE_VALUE (TREE_VEC_ELT (newargs, i));
2259 tree oldarg = TREE_VALUE (TREE_VEC_ELT (oldargs, i));
2260 if (TREE_CODE (newarg) != TREE_CODE (oldarg))
2262 else if (TREE_CODE (newarg) == TYPE_DECL)
2264 else if (! comptypes (TREE_TYPE (newarg), TREE_TYPE (oldarg), 1))
2268 if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2271 types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2272 DECL_TEMPLATE_RESULT (newdecl));
2276 if (TREE_TYPE (newdecl) == error_mark_node)
2277 types_match = TREE_TYPE (olddecl) == error_mark_node;
2278 else if (TREE_TYPE (olddecl) == NULL_TREE)
2279 types_match = TREE_TYPE (newdecl) == NULL_TREE;
2280 else if (TREE_TYPE (newdecl) == NULL_TREE)
2282 /* Qualifiers must match, and they may be present on either, the type
2284 else if ((TREE_READONLY (newdecl)
2285 || TYPE_READONLY (TREE_TYPE (newdecl)))
2286 == (TREE_READONLY (olddecl)
2287 || TYPE_READONLY (TREE_TYPE (olddecl)))
2288 && (TREE_THIS_VOLATILE (newdecl)
2289 || TYPE_VOLATILE (TREE_TYPE (newdecl)))
2290 == (TREE_THIS_VOLATILE (olddecl)
2291 || TYPE_VOLATILE (TREE_TYPE (olddecl))))
2292 types_match = comptypes (TYPE_MAIN_VARIANT (TREE_TYPE (newdecl)),
2293 TYPE_MAIN_VARIANT (TREE_TYPE (olddecl)), 1);
2301 /* If NEWDECL is `static' and an `extern' was seen previously,
2302 warn about it. (OLDDECL may be NULL_TREE; NAME contains
2303 information about previous usage as an `extern'.)
2305 Note that this does not apply to the C++ case of declaring
2306 a variable `extern const' and then later `const'.
2308 Don't complain about built-in functions, since they are beyond
2309 the user's control. */
2312 warn_extern_redeclared_static (newdecl, olddecl)
2313 tree newdecl, olddecl;
2317 static char *explicit_extern_static_warning
2318 = "`%D' was declared `extern' and later `static'";
2319 static char *implicit_extern_static_warning
2320 = "`%D' was declared implicitly `extern' and later `static'";
2322 if (TREE_CODE (newdecl) == TYPE_DECL)
2325 name = DECL_ASSEMBLER_NAME (newdecl);
2326 if (TREE_PUBLIC (name) && DECL_THIS_STATIC (newdecl))
2328 /* It's okay to redeclare an ANSI built-in function as static,
2329 or to declare a non-ANSI built-in function as anything. */
2330 if (! (TREE_CODE (newdecl) == FUNCTION_DECL
2331 && olddecl != NULL_TREE
2332 && TREE_CODE (olddecl) == FUNCTION_DECL
2333 && (DECL_BUILT_IN (olddecl)
2334 || DECL_BUILT_IN_NONANSI (olddecl))))
2336 cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2337 ? implicit_extern_static_warning
2338 : explicit_extern_static_warning, newdecl);
2339 if (olddecl != NULL_TREE)
2340 cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2345 /* Handle when a new declaration NEWDECL has the same name as an old
2346 one OLDDECL in the same binding contour. Prints an error message
2349 If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2350 Otherwise, return 0. */
2353 duplicate_decls (newdecl, olddecl)
2354 register tree newdecl, olddecl;
2356 extern struct obstack permanent_obstack;
2357 unsigned olddecl_uid = DECL_UID (olddecl);
2358 int olddecl_friend = 0, types_match = 0;
2359 int new_defines_function;
2361 if (newdecl == olddecl)
2364 if (TREE_CODE_CLASS (TREE_CODE (olddecl)) == 'd')
2365 DECL_MACHINE_ATTRIBUTES (newdecl) = DECL_MACHINE_ATTRIBUTES (olddecl);
2367 types_match = decls_match (newdecl, olddecl);
2369 if (TREE_CODE (olddecl) != TREE_LIST)
2370 olddecl_friend = DECL_LANG_SPECIFIC (olddecl) && DECL_FRIEND_P (olddecl);
2372 /* If either the type of the new decl or the type of the old decl is an
2373 error_mark_node, then that implies that we have already issued an
2374 error (earlier) for some bogus type specification, and in that case,
2375 it is rather pointless to harass the user with yet more error message
2376 about the same declaration, so well just pretent the types match here. */
2377 if ((TREE_TYPE (newdecl)
2378 && TREE_CODE (TREE_TYPE (newdecl)) == ERROR_MARK)
2379 || (TREE_TYPE (olddecl)
2380 && TREE_CODE (TREE_TYPE (olddecl)) == ERROR_MARK))
2383 if (TREE_CODE (olddecl) == FUNCTION_DECL
2384 && DECL_ARTIFICIAL (olddecl)
2385 && (DECL_BUILT_IN (olddecl) || DECL_BUILT_IN_NONANSI (olddecl)))
2387 /* If you declare a built-in or predefined function name as static,
2388 the old definition is overridden, but optionally warn this was a
2389 bad choice of name. Ditto for overloads. */
2390 if (! DECL_PUBLIC (newdecl)
2391 || (TREE_CODE (newdecl) == FUNCTION_DECL
2392 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)))
2395 cp_warning ("shadowing %s function `%#D'",
2396 DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2398 /* Discard the old built-in function. */
2401 else if (! types_match)
2403 if (TREE_CODE (newdecl) != FUNCTION_DECL)
2405 /* If the built-in is not ansi, then programs can override
2406 it even globally without an error. */
2407 if (! DECL_BUILT_IN (olddecl))
2408 cp_warning ("library function `%#D' redeclared as non-function `%#D'",
2412 cp_error ("declaration of `%#D'", newdecl);
2413 cp_error ("conflicts with built-in declaration `%#D'",
2419 cp_warning ("declaration of `%#D'", newdecl);
2420 cp_warning ("conflicts with built-in declaration `%#D'",
2424 else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2426 if ((TREE_CODE (newdecl) == FUNCTION_DECL
2427 && DECL_FUNCTION_TEMPLATE_P (olddecl))
2428 || (TREE_CODE (olddecl) == FUNCTION_DECL
2429 && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2432 cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
2433 if (TREE_CODE (olddecl) == TREE_LIST)
2434 olddecl = TREE_VALUE (olddecl);
2435 cp_error_at ("previous declaration of `%#D'", olddecl);
2437 /* New decl is completely inconsistent with the old one =>
2438 tell caller to replace the old one. */
2442 else if (!types_match)
2444 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2446 /* The name of a class template may not be declared to refer to
2447 any other template, class, function, object, namespace, value,
2448 or type in the same scope. */
2449 if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
2450 || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2452 cp_error ("declaration of template `%#D'", newdecl);
2453 cp_error_at ("conflicts with previous declaration `%#D'",
2456 else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
2457 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
2458 && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
2459 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))), 3))
2461 cp_error ("new declaration `%#D'", newdecl);
2462 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2466 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2468 if (DECL_LANGUAGE (newdecl) == lang_c
2469 && DECL_LANGUAGE (olddecl) == lang_c)
2471 cp_error ("declaration of C function `%#D' conflicts with",
2473 cp_error_at ("previous declaration `%#D' here", olddecl);
2475 else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2476 TYPE_ARG_TYPES (TREE_TYPE (olddecl)), 3))
2478 cp_error ("new declaration `%#D'", newdecl);
2479 cp_error_at ("ambiguates old declaration `%#D'", olddecl);
2485 /* Already complained about this, so don't do so again. */
2486 else if (current_class_type == NULL_TREE
2487 || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
2489 cp_error ("conflicting types for `%#D'", newdecl);
2490 cp_error_at ("previous declaration as `%#D'", olddecl);
2495 char *errmsg = redeclaration_error_message (newdecl, olddecl);
2498 cp_error (errmsg, newdecl);
2499 if (DECL_NAME (olddecl) != NULL_TREE)
2500 cp_error_at ((DECL_INITIAL (olddecl)
2501 && current_binding_level == global_binding_level)
2502 ? "`%#D' previously defined here"
2503 : "`%#D' previously declared here", olddecl);
2505 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2506 && DECL_INITIAL (olddecl) != NULL_TREE
2507 && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
2508 && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
2510 /* Prototype decl follows defn w/o prototype. */
2511 cp_warning_at ("prototype for `%#D'", newdecl);
2512 cp_warning_at ("follows non-prototype definition here", olddecl);
2514 else if (TREE_CODE (olddecl) == FUNCTION_DECL
2515 && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
2517 /* extern "C" int foo ();
2518 int foo () { bar (); }
2520 if (current_lang_stack == current_lang_base)
2521 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
2524 cp_error_at ("previous declaration of `%#D' with %L linkage",
2525 olddecl, DECL_LANGUAGE (olddecl));
2526 cp_error ("conflicts with new declaration with %L linkage",
2527 DECL_LANGUAGE (newdecl));
2531 if (TREE_CODE (olddecl) == FUNCTION_DECL
2532 && ! DECL_USE_TEMPLATE (olddecl))
2534 tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
2535 tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
2538 if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
2539 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
2541 for (; t1 && t1 != void_list_node;
2542 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
2543 if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
2545 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
2550 cp_pedwarn ("default argument given for parameter %d of `%#D'",
2552 cp_pedwarn_at ("after previous specification in `%#D'",
2558 cp_error ("default argument given for parameter %d of `%#D'",
2560 cp_error_at ("conflicts with previous specification in `%#D'",
2565 if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
2566 && TREE_ADDRESSABLE (olddecl) && warn_inline)
2568 cp_warning ("`%#D' was used before it was declared inline",
2570 cp_warning_at ("previous non-inline declaration here",
2574 /* These bits are logically part of the type for non-functions. */
2575 else if (TREE_READONLY (newdecl) != TREE_READONLY (olddecl)
2576 || TREE_THIS_VOLATILE (newdecl) != TREE_THIS_VOLATILE (olddecl))
2578 cp_pedwarn ("type qualifiers for `%#D'", newdecl);
2579 cp_pedwarn_at ("conflict with previous decl `%#D'", olddecl);
2583 /* If new decl is `static' and an `extern' was seen previously,
2585 warn_extern_redeclared_static (newdecl, olddecl);
2587 /* We have committed to returning 1 at this point. */
2588 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2590 /* Now that functions must hold information normally held
2591 by field decls, there is extra work to do so that
2592 declaration information does not get destroyed during
2594 if (DECL_VINDEX (olddecl))
2595 DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
2596 if (DECL_CONTEXT (olddecl))
2597 DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
2598 if (DECL_CLASS_CONTEXT (olddecl))
2599 DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
2600 if (DECL_CHAIN (newdecl) == NULL_TREE)
2601 DECL_CHAIN (newdecl) = DECL_CHAIN (olddecl);
2602 if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
2603 DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
2604 DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
2605 DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
2606 DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
2609 /* Deal with C++: must preserve virtual function table size. */
2610 if (TREE_CODE (olddecl) == TYPE_DECL)
2612 register tree newtype = TREE_TYPE (newdecl);
2613 register tree oldtype = TREE_TYPE (olddecl);
2615 if (newtype != error_mark_node && oldtype != error_mark_node
2616 && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
2618 CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
2619 CLASSTYPE_FRIEND_CLASSES (newtype)
2620 = CLASSTYPE_FRIEND_CLASSES (oldtype);
2624 /* Special handling ensues if new decl is a function definition. */
2625 new_defines_function = (TREE_CODE (newdecl) == FUNCTION_DECL
2626 && DECL_INITIAL (newdecl) != NULL_TREE);
2628 /* Optionally warn about more than one declaration for the same name,
2629 but don't warn about a function declaration followed by a definition. */
2630 if (warn_redundant_decls
2631 && ! DECL_ARTIFICIAL (olddecl)
2632 && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
2633 /* Don't warn about extern decl followed by (tentative) definition. */
2634 && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl)))
2636 cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
2637 cp_warning_at ("previous declaration of `%D'", olddecl);
2640 /* Copy all the DECL_... slots specified in the new decl
2641 except for any that we copy here from the old type. */
2643 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2645 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2646 DECL_TEMPLATE_RESULT (olddecl) = DECL_TEMPLATE_RESULT (newdecl);
2647 DECL_TEMPLATE_PARMS (olddecl) = DECL_TEMPLATE_PARMS (newdecl);
2653 /* Automatically handles default parameters. */
2654 tree oldtype = TREE_TYPE (olddecl);
2657 /* Make sure we put the new type in the same obstack as the old one. */
2659 push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
2662 push_obstacks_nochange ();
2663 end_temporary_allocation ();
2666 /* Merge the data types specified in the two decls. */
2667 newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
2669 if (TREE_CODE (newdecl) == VAR_DECL)
2670 DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
2671 /* Do this after calling `common_type' so that default
2672 parameters don't confuse us. */
2673 else if (TREE_CODE (newdecl) == FUNCTION_DECL
2674 && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
2675 != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
2677 TREE_TYPE (newdecl) = build_exception_variant (newtype,
2678 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
2679 TREE_TYPE (olddecl) = build_exception_variant (newtype,
2680 TYPE_RAISES_EXCEPTIONS (oldtype));
2682 if (! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
2684 cp_error ("declaration of `%D' throws different exceptions...",
2686 cp_error_at ("...from previous declaration here", olddecl);
2689 TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
2691 /* Lay the type out, unless already done. */
2692 if (oldtype != TREE_TYPE (newdecl)
2693 && TREE_TYPE (newdecl) != error_mark_node
2694 && !(processing_template_decl && uses_template_parms (newdecl)))
2695 layout_type (TREE_TYPE (newdecl));
2697 if ((TREE_CODE (newdecl) == VAR_DECL
2698 || TREE_CODE (newdecl) == PARM_DECL
2699 || TREE_CODE (newdecl) == RESULT_DECL
2700 || TREE_CODE (newdecl) == FIELD_DECL
2701 || TREE_CODE (newdecl) == TYPE_DECL)
2702 && !(processing_template_decl && uses_template_parms (newdecl)))
2703 layout_decl (newdecl, 0);
2705 /* Merge the type qualifiers. */
2706 if (TREE_READONLY (newdecl))
2707 TREE_READONLY (olddecl) = 1;
2708 if (TREE_THIS_VOLATILE (newdecl))
2709 TREE_THIS_VOLATILE (olddecl) = 1;
2711 /* Merge the initialization information. */
2712 if (DECL_INITIAL (newdecl) == NULL_TREE
2713 && DECL_INITIAL (olddecl) != NULL_TREE)
2715 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2716 DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
2717 DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
2720 /* Merge the section attribute.
2721 We want to issue an error if the sections conflict but that must be
2722 done later in decl_attributes since we are called before attributes
2724 if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
2725 DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
2727 /* Keep the old rtl since we can safely use it, unless it's the
2728 call to abort() used for abstract virtuals. */
2729 if ((DECL_LANG_SPECIFIC (olddecl)
2730 && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
2731 || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
2732 DECL_RTL (newdecl) = DECL_RTL (olddecl);
2736 /* If cannot merge, then use the new type and qualifiers,
2737 and don't preserve the old rtl. */
2740 /* Clean out any memory we had of the old declaration. */
2741 tree oldstatic = value_member (olddecl, static_aggregates);
2743 TREE_VALUE (oldstatic) = error_mark_node;
2745 TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2746 TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
2747 TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
2748 TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
2751 /* Merge the storage class information. */
2752 DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
2753 DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
2754 TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
2755 TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
2756 if (! DECL_EXTERNAL (olddecl))
2757 DECL_EXTERNAL (newdecl) = 0;
2759 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2760 DECL_C_STATIC (newdecl) |= DECL_C_STATIC (olddecl);
2762 if (DECL_LANG_SPECIFIC (newdecl))
2764 DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
2765 DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
2768 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2770 DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
2772 /* If either decl says `inline', this fn is inline, unless its
2773 definition was passed already. */
2774 if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
2775 DECL_INLINE (olddecl) = 1;
2776 DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
2780 DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
2781 DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
2782 DECL_RTL (olddecl) = DECL_RTL (newdecl);
2784 if (! types_match || new_defines_function)
2786 /* These need to be copied so that the names are available. */
2787 DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
2788 DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
2790 if (new_defines_function)
2791 /* If defining a function declared with other language
2792 linkage, use the previously declared language linkage. */
2793 DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
2796 /* If redeclaring a builtin function, and not a definition,
2797 it stays built in. */
2798 if (DECL_BUILT_IN (olddecl))
2800 DECL_BUILT_IN (newdecl) = 1;
2801 DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
2802 /* If we're keeping the built-in definition, keep the rtl,
2803 regardless of declaration matches. */
2804 DECL_RTL (newdecl) = DECL_RTL (olddecl);
2807 DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
2809 DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
2810 if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
2811 /* Previously saved insns go together with
2812 the function's previous definition. */
2813 DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
2814 /* Don't clear out the arguments if we're redefining a function. */
2815 if (DECL_ARGUMENTS (olddecl))
2816 DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
2818 if (DECL_LANG_SPECIFIC (olddecl))
2819 DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
2822 if (TREE_CODE (newdecl) == NAMESPACE_DECL)
2824 NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
2827 if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2829 DECL_TEMPLATE_INSTANTIATIONS (newdecl)
2830 = DECL_TEMPLATE_INSTANTIATIONS (olddecl);
2831 if (DECL_CHAIN (newdecl) == NULL_TREE)
2832 DECL_CHAIN (newdecl) = DECL_CHAIN (olddecl);
2835 /* Now preserve various other info from the definition. */
2836 TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
2837 TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
2838 DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
2839 DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
2841 /* Don't really know how much of the language-specific
2842 values we should copy from old to new. */
2843 if (DECL_LANG_SPECIFIC (olddecl))
2845 DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
2846 DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
2847 DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
2848 if (DECL_TEMPLATE_INFO (newdecl) == NULL_TREE)
2850 DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
2851 DECL_USE_TEMPLATE (newdecl) = DECL_USE_TEMPLATE (olddecl);
2855 if (TREE_CODE (newdecl) == FUNCTION_DECL)
2858 struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
2859 struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
2861 function_size = sizeof (struct tree_decl);
2863 bcopy ((char *) newdecl + sizeof (struct tree_common),
2864 (char *) olddecl + sizeof (struct tree_common),
2865 function_size - sizeof (struct tree_common));
2867 /* Can we safely free the storage used by newdecl? */
2869 #define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
2870 & ~ obstack_alignment_mask (&permanent_obstack))
2872 if ((char *)newdecl + ROUND (function_size)
2873 + ROUND (sizeof (struct lang_decl))
2874 == obstack_next_free (&permanent_obstack))
2876 DECL_MAIN_VARIANT (newdecl) = olddecl;
2877 DECL_LANG_SPECIFIC (olddecl) = ol;
2878 bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
2880 obstack_free (&permanent_obstack, newdecl);
2882 else if (LANG_DECL_PERMANENT (ol) && ol != nl)
2884 if (DECL_MAIN_VARIANT (olddecl) == olddecl)
2886 /* Save these lang_decls that would otherwise be lost. */
2887 extern tree free_lang_decl_chain;
2888 tree free_lang_decl = (tree) ol;
2890 if (DECL_LANG_SPECIFIC (olddecl) == ol)
2893 TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
2894 free_lang_decl_chain = free_lang_decl;
2904 bcopy ((char *) newdecl + sizeof (struct tree_common),
2905 (char *) olddecl + sizeof (struct tree_common),
2906 sizeof (struct tree_decl) - sizeof (struct tree_common)
2907 + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
2910 DECL_UID (olddecl) = olddecl_uid;
2912 DECL_FRIEND_P (olddecl) = 1;
2917 /* Record a decl-node X as belonging to the current lexical scope.
2918 Check for errors (such as an incompatible declaration for the same
2919 name already seen in the same scope).
2921 Returns either X or an old decl for the same name.
2922 If an old decl is returned, it may have been smashed
2923 to agree with what X says. */
2930 register tree name = DECL_ASSEMBLER_NAME (x);
2931 register struct binding_level *b = current_binding_level;
2933 if (x != current_function_decl
2934 /* Don't change DECL_CONTEXT of virtual methods. */
2935 && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
2936 && ! DECL_CONTEXT (x))
2937 DECL_CONTEXT (x) = current_function_decl;
2938 /* A local declaration for a function doesn't constitute nesting. */
2939 if (TREE_CODE (x) == FUNCTION_DECL && DECL_INITIAL (x) == 0)
2940 DECL_CONTEXT (x) = 0;
2942 /* Type are looked up using the DECL_NAME, as that is what the rest of the
2943 compiler wants to use. */
2944 if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
2945 || TREE_CODE (x) == NAMESPACE_DECL)
2946 name = DECL_NAME (x);
2951 /* Not needed...see below. */
2956 t = lookup_name_current_level (name);
2957 if (t == error_mark_node)
2959 /* error_mark_node is 0 for a while during initialization! */
2961 cp_error_at ("`%#D' used prior to declaration", x);
2964 else if (t != NULL_TREE)
2967 /* This is turned off until I have time to do it right (bpk). */
2968 /* With the code below that uses it... */
2969 file = DECL_SOURCE_FILE (t);
2970 line = DECL_SOURCE_LINE (t);
2972 if (TREE_CODE (t) == PARM_DECL)
2974 if (DECL_CONTEXT (t) == NULL_TREE)
2975 fatal ("parse errors have confused me too much");
2977 /* Check for duplicate params. */
2978 if (duplicate_decls (x, t))
2981 else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
2982 || DECL_FUNCTION_TEMPLATE_P (x))
2983 && is_overloaded_fn (t))
2984 /* don't do anything just yet */;
2985 else if (t == wchar_decl_node)
2987 if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
2988 cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
2990 /* Throw away the redeclaration. */
2993 else if (TREE_CODE (t) != TREE_CODE (x))
2995 if ((TREE_CODE (t) == TYPE_DECL && DECL_ARTIFICIAL (t)
2996 && TREE_CODE (x) != TYPE_DECL
2997 && ! (TREE_CODE (x) == TEMPLATE_DECL
2998 && TREE_CODE (DECL_TEMPLATE_RESULT (x)) == TYPE_DECL))
2999 || (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3000 && TREE_CODE (t) != TYPE_DECL
3001 && ! (TREE_CODE (t) == TEMPLATE_DECL
3002 && (TREE_CODE (DECL_TEMPLATE_RESULT (t))
3005 /* We do nothing special here, because C++ does such nasty
3006 things with TYPE_DECLs. Instead, just let the TYPE_DECL
3007 get shadowed, and know that if we need to find a TYPE_DECL
3008 for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3009 slot of the identifier. */
3012 else if (duplicate_decls (x, t))
3015 else if (duplicate_decls (x, t))
3018 /* This is turned off until I have time to do it right (bpk). */
3020 /* Also warn if they did a prototype with `static' on it, but
3021 then later left the `static' off. */
3022 if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
3024 if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3029 cp_warning ("`static' missing from declaration of `%D'",
3031 warning_with_file_and_line (file, line,
3032 "previous declaration of `%s'",
3033 decl_as_string (t, 0));
3036 /* Now fix things so it'll do what they expect. */
3037 if (current_function_decl)
3038 TREE_PUBLIC (current_function_decl) = 0;
3040 /* Due to interference in memory reclamation (X may be
3041 obstack-deallocated at this point), we must guard against
3042 one really special case. [jason: This should be handled
3043 by start_function] */
3044 if (current_function_decl == x)
3045 current_function_decl = t;
3047 if (TREE_CODE (t) == TYPE_DECL)
3048 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3049 else if (TREE_CODE (t) == FUNCTION_DECL)
3050 check_default_args (t);
3056 if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
3058 t = push_overloaded_decl (x, 1);
3059 if (t != x || DECL_LANGUAGE (x) == lang_c)
3062 else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_CONTEXT (x) == NULL_TREE)
3063 return push_overloaded_decl (x, 0);
3065 /* If declaring a type as a typedef, and the type has no known
3066 typedef name, install this TYPE_DECL as its typedef name. */
3067 if (TREE_CODE (x) == TYPE_DECL)
3069 tree type = TREE_TYPE (x);
3070 tree name = (type != error_mark_node) ? TYPE_NAME (type) : x;
3072 if (name == NULL_TREE || TREE_CODE (name) != TYPE_DECL)
3074 /* If these are different names, and we're at the global
3075 binding level, make two equivalent definitions. */
3077 if (global_bindings_p ())
3078 TYPE_NAME (type) = x;
3080 my_friendly_assert (TREE_CODE (name) == TYPE_DECL, 140);
3082 if (type != error_mark_node
3084 && TYPE_IDENTIFIER (type))
3085 set_identifier_type_value_with_scope (DECL_NAME (x), type, b);
3088 /* Multiple external decls of the same identifier ought to match.
3090 We get warnings about inline functions where they are defined.
3091 We get warnings about other functions from push_overloaded_decl.
3093 Avoid duplicate warnings where they are used. */
3094 if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3098 if (IDENTIFIER_GLOBAL_VALUE (name) != NULL_TREE
3099 && (DECL_EXTERNAL (IDENTIFIER_GLOBAL_VALUE (name))
3100 || TREE_PUBLIC (IDENTIFIER_GLOBAL_VALUE (name))))
3101 decl = IDENTIFIER_GLOBAL_VALUE (name);
3106 /* If different sort of thing, we already gave an error. */
3107 && TREE_CODE (decl) == TREE_CODE (x)
3108 && ! comptypes (TREE_TYPE (x), TREE_TYPE (decl), 1))
3110 cp_pedwarn ("type mismatch with previous external decl", x);
3111 cp_pedwarn_at ("previous external decl of `%#D'", decl);
3115 /* This name is new in its binding level.
3116 Install the new declaration and return it. */
3117 if (b == global_binding_level)
3119 /* Install a global value. */
3121 /* If the first global decl has external linkage,
3122 warn if we later see static one. */
3123 if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && DECL_PUBLIC (x))
3124 TREE_PUBLIC (name) = 1;
3126 /* Don't install an artificial TYPE_DECL if we already have
3127 another _DECL with that name. */
3128 if (TREE_CODE (x) != TYPE_DECL
3130 || ! DECL_ARTIFICIAL (x))
3131 IDENTIFIER_GLOBAL_VALUE (name) = x;
3133 /* Don't forget if the function was used via an implicit decl. */
3134 if (IDENTIFIER_IMPLICIT_DECL (name)
3135 && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3138 /* Don't forget if its address was taken in that way. */
3139 if (IDENTIFIER_IMPLICIT_DECL (name)
3140 && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3141 TREE_ADDRESSABLE (x) = 1;
3143 /* Warn about mismatches against previous implicit decl. */
3144 if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3145 /* If this real decl matches the implicit, don't complain. */
3146 && ! (TREE_CODE (x) == FUNCTION_DECL
3147 && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3149 ("`%D' was previously implicitly declared to return `int'", x);
3151 /* If new decl is `static' and an `extern' was seen previously,
3153 if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3154 warn_extern_redeclared_static (x, t);
3158 /* Here to install a non-global value. */
3159 tree oldlocal = IDENTIFIER_LOCAL_VALUE (name);
3160 tree oldglobal = IDENTIFIER_GLOBAL_VALUE (name);
3162 /* Don't install an artificial TYPE_DECL if we already have
3163 another _DECL with that name. */
3164 if (TREE_CODE (x) != TYPE_DECL
3166 || ! DECL_ARTIFICIAL (x))
3168 b->shadowed = tree_cons (name, oldlocal, b->shadowed);
3169 IDENTIFIER_LOCAL_VALUE (name) = x;
3172 /* If this is a TYPE_DECL, push it into the type value slot. */
3173 if (TREE_CODE (x) == TYPE_DECL)
3174 set_identifier_type_value_with_scope (name, TREE_TYPE (x), b);
3176 /* Clear out any TYPE_DECL shadowed by a namespace so that
3177 we won't think this is a type. The C struct hack doesn't
3178 go through namespaces. */
3179 if (TREE_CODE (x) == NAMESPACE_DECL)
3180 set_identifier_type_value_with_scope (name, NULL_TREE, b);
3182 /* If this is an extern function declaration, see if we
3183 have a global definition or declaration for the function. */
3184 if (oldlocal == NULL_TREE
3185 && DECL_EXTERNAL (x)
3186 && oldglobal != NULL_TREE
3187 && TREE_CODE (x) == FUNCTION_DECL
3188 && TREE_CODE (oldglobal) == FUNCTION_DECL)
3190 /* We have one. Their types must agree. */
3191 if (decls_match (x, oldglobal))
3195 cp_warning ("extern declaration of `%#D' doesn't match", x);
3196 cp_warning_at ("global declaration `%#D'", oldglobal);
3199 /* If we have a local external declaration,
3200 and no file-scope declaration has yet been seen,
3201 then if we later have a file-scope decl it must not be static. */
3202 if (oldlocal == NULL_TREE
3203 && oldglobal == NULL_TREE
3204 && DECL_EXTERNAL (x)
3207 TREE_PUBLIC (name) = 1;
3210 if (DECL_FROM_INLINE (x))
3211 /* Inline decls shadow nothing. */;
3213 /* Warn if shadowing an argument at the top level of the body. */
3214 else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3215 && TREE_CODE (oldlocal) == PARM_DECL
3216 && TREE_CODE (x) != PARM_DECL)
3218 /* Go to where the parms should be and see if we
3220 struct binding_level *b = current_binding_level->level_chain;
3226 if (b->parm_flag == 1)
3227 cp_error ("declaration of `%#D' shadows a parameter", name);
3229 else if (warn_shadow && oldlocal != NULL_TREE && b->is_for_scope
3230 && !DECL_DEAD_FOR_LOCAL (oldlocal))
3232 warning ("variable `%s' shadows local",
3233 IDENTIFIER_POINTER (name));
3234 cp_warning_at (" this is the shadowed declaration", oldlocal);
3236 /* Maybe warn if shadowing something else. */
3237 else if (warn_shadow && !DECL_EXTERNAL (x)
3238 /* No shadow warnings for internally generated vars. */
3239 && ! DECL_ARTIFICIAL (x)
3240 /* No shadow warnings for vars made for inlining. */
3241 && ! DECL_FROM_INLINE (x))
3243 char *warnstring = NULL;
3245 if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
3246 warnstring = "declaration of `%s' shadows a parameter";
3247 else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
3248 && current_class_ptr
3249 && !TREE_STATIC (name))
3250 warnstring = "declaration of `%s' shadows a member of `this'";
3251 else if (oldlocal != NULL_TREE)
3252 warnstring = "declaration of `%s' shadows previous local";
3253 else if (oldglobal != NULL_TREE)
3254 warnstring = "declaration of `%s' shadows global declaration";
3257 warning (warnstring, IDENTIFIER_POINTER (name));
3261 if (TREE_CODE (x) == FUNCTION_DECL)
3262 check_default_args (x);
3264 /* Keep count of variables in this level with incomplete type. */
3265 if (TREE_CODE (x) == VAR_DECL
3266 && TREE_TYPE (x) != error_mark_node
3267 && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
3268 && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
3269 /* RTTI TD entries are created while defining the type_info. */
3270 || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
3271 && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
3272 b->incomplete = tree_cons (NULL_TREE, x, b->incomplete);
3275 /* Put decls on list in reverse order.
3276 We will reverse them later if necessary. */
3277 TREE_CHAIN (x) = b->names;
3279 if (! (b != global_binding_level || TREE_PERMANENT (x)))
3280 my_friendly_abort (124);
3285 /* Same as pushdecl, but define X in binding-level LEVEL. We rely on the
3286 caller to set DECL_CONTEXT properly. */
3289 pushdecl_with_scope (x, level)
3291 struct binding_level *level;
3293 register struct binding_level *b = current_binding_level;
3294 tree function_decl = current_function_decl;
3296 current_function_decl = NULL_TREE;
3297 current_binding_level = level;
3299 current_binding_level = b;
3300 current_function_decl = function_decl;
3304 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
3308 pushdecl_top_level (x)
3311 register struct binding_level *b = inner_binding_level;
3312 register tree t = pushdecl_with_scope (x, global_binding_level);
3314 /* Now, the type_shadowed stack may screw us. Munge it so it does
3316 if (TREE_CODE (x) == TYPE_DECL)
3318 tree name = DECL_NAME (x);
3320 tree *ptr = (tree *)0;
3321 for (; b != global_binding_level; b = b->level_chain)
3323 tree shadowed = b->type_shadowed;
3324 for (; shadowed; shadowed = TREE_CHAIN (shadowed))
3325 if (TREE_PURPOSE (shadowed) == name)
3327 ptr = &TREE_VALUE (shadowed);
3328 /* Can't break out of the loop here because sometimes
3329 a binding level will have duplicate bindings for
3330 PT names. It's gross, but I haven't time to fix it. */
3333 newval = TREE_TYPE (x);
3334 if (ptr == (tree *)0)
3336 /* @@ This shouldn't be needed. My test case "zstring.cc" trips
3337 up here if this is changed to an assertion. --KR */
3338 SET_IDENTIFIER_TYPE_VALUE (name, newval);
3348 /* Like push_overloaded_decl, only it places X in GLOBAL_BINDING_LEVEL,
3352 push_overloaded_decl_top_level (x, forget)
3356 struct binding_level *b = current_binding_level;
3358 current_binding_level = global_binding_level;
3359 push_overloaded_decl (x, forget);
3360 current_binding_level = b;
3363 /* Make the declaration of X appear in CLASS scope. */
3366 pushdecl_class_level (x)
3369 /* Don't use DECL_ASSEMBLER_NAME here! Everything that looks in class
3370 scope looks for the pre-mangled name. */
3371 register tree name = DECL_NAME (x);
3375 if (TYPE_BEING_DEFINED (current_class_type))
3377 /* Check for inconsistent use of this name in the class body.
3378 Types, enums, and static vars are checked here; other
3379 members are checked in finish_struct. */
3380 tree icv = IDENTIFIER_CLASS_VALUE (name);
3383 /* Don't complain about inherited names. */
3384 && id_in_current_class (name)
3385 /* Or shadowed tags. */
3386 && !(TREE_CODE (icv) == TYPE_DECL
3387 && DECL_CONTEXT (icv) == current_class_type))
3389 cp_error ("declaration of identifier `%D' as `%#D'", name, x);
3390 cp_error_at ("conflicts with previous use in class as `%#D'",
3395 push_class_level_binding (name, x);
3396 if (TREE_CODE (x) == TYPE_DECL)
3398 set_identifier_type_value (name, TREE_TYPE (x));
3404 /* This function is used to push the mangled decls for nested types into
3405 the appropriate scope. Previously pushdecl_top_level was used, but that
3406 is incorrect for members of local classes. */
3409 pushdecl_nonclass_level (x)
3412 struct binding_level *b = current_binding_level;
3414 my_friendly_assert (b->parm_flag != 2, 180);
3417 /* Get out of template binding levels */
3418 while (b->pseudo_global)
3422 pushdecl_with_scope (x, b);
3425 /* Make the declaration(s) of X appear in CLASS scope
3426 under the name NAME. */
3429 push_class_level_binding (name, x)
3433 if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3434 && purpose_member (name, class_binding_level->class_shadowed))
3437 maybe_push_cache_obstack ();
3438 class_binding_level->class_shadowed
3439 = tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
3440 class_binding_level->class_shadowed);
3442 IDENTIFIER_CLASS_VALUE (name) = x;
3443 obstack_ptr_grow (&decl_obstack, x);
3446 /* Tell caller how to interpret a TREE_LIST which contains
3447 chains of FUNCTION_DECLS. */
3450 overloaded_globals_p (list)
3453 my_friendly_assert (TREE_CODE (list) == TREE_LIST, 142);
3455 /* Don't commit caller to seeing them as globals. */
3456 if (TREE_NONLOCAL_FLAG (list))
3458 /* Do commit caller to not seeing them as globals. */
3459 if (TREE_CODE (TREE_VALUE (list)) == TREE_LIST)
3461 /* Do commit caller to seeing them as globals. */
3465 /* DECL is a FUNCTION_DECL which may have other definitions already in
3466 place. We get around this by making the value of the identifier point
3467 to a list of all the things that want to be referenced by that name. It
3468 is then up to the users of that name to decide what to do with that
3471 DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
3472 slot. It is dealt with the same way.
3474 The value returned may be a previous declaration if we guessed wrong
3475 about what language DECL should belong to (C or C++). Otherwise,
3476 it's always DECL (and never something that's not a _DECL). */
3479 push_overloaded_decl (decl, forgettable)
3483 tree orig_name = DECL_NAME (decl);
3485 int doing_global = (global_bindings_p () || ! forgettable);
3489 old = IDENTIFIER_GLOBAL_VALUE (orig_name);
3490 if (old && TREE_CODE (old) == FUNCTION_DECL
3491 && DECL_ARTIFICIAL (old)
3492 && (DECL_BUILT_IN (old) || DECL_BUILT_IN_NONANSI (old)))
3494 if (duplicate_decls (decl, old))
3501 old = IDENTIFIER_LOCAL_VALUE (orig_name);
3503 if (! purpose_member (orig_name, current_binding_level->shadowed))
3505 current_binding_level->shadowed
3506 = tree_cons (orig_name, old, current_binding_level->shadowed);
3513 if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
3515 tree t = TREE_TYPE (old);
3516 if (IS_AGGR_TYPE (t) && warn_shadow
3517 && (! DECL_IN_SYSTEM_HEADER (decl)
3518 || ! DECL_IN_SYSTEM_HEADER (old)))
3519 cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
3522 else if (is_overloaded_fn (old))
3526 for (tmp = get_first_fn (old); tmp; tmp = DECL_CHAIN (tmp))
3527 if (decl == tmp || duplicate_decls (decl, tmp))
3532 cp_error_at ("previous non-function declaration `%#D'", old);
3533 cp_error ("conflicts with function declaration `%#D'", decl);
3538 if (old || TREE_CODE (decl) == TEMPLATE_DECL)
3540 if (old && is_overloaded_fn (old))
3541 DECL_CHAIN (decl) = get_first_fn (old);
3543 DECL_CHAIN (decl) = NULL_TREE;
3544 old = tree_cons (orig_name, decl, NULL_TREE);
3545 TREE_TYPE (old) = unknown_type_node;
3548 /* orig_name is not ambiguous. */
3552 IDENTIFIER_GLOBAL_VALUE (orig_name) = old;
3554 IDENTIFIER_LOCAL_VALUE (orig_name) = old;
3559 /* Generate an implicit declaration for identifier FUNCTIONID
3560 as a function of type int (). Print a warning if appropriate. */
3563 implicitly_declare (functionid)
3567 int temp = allocation_temporary_p ();
3569 push_obstacks_nochange ();
3571 /* Save the decl permanently so we can warn if definition follows.
3572 In ANSI C, warn_implicit is usually false, so the saves little space.
3573 But in C++, it's usually true, hence the extra code. */
3574 if (temp && (! warn_implicit || toplevel_bindings_p ()))
3575 end_temporary_allocation ();
3577 /* We used to reuse an old implicit decl here,
3578 but this loses with inline functions because it can clobber
3579 the saved decl chains. */
3580 decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
3582 DECL_EXTERNAL (decl) = 1;
3583 TREE_PUBLIC (decl) = 1;
3584 DECL_ARTIFICIAL (decl) = 1;
3586 /* ANSI standard says implicit declarations are in the innermost block.
3587 So we record the decl in the standard fashion. */
3589 rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
3592 /* Only one warning per identifier. */
3593 && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
3595 cp_pedwarn ("implicit declaration of function `%#D'", decl);
3598 SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
3605 /* Return zero if the declaration NEWDECL is valid
3606 when the declaration OLDDECL (assumed to be for the same name)
3607 has already been seen.
3608 Otherwise return an error message format string with a %s
3609 where the identifier should go. */
3612 redeclaration_error_message (newdecl, olddecl)
3613 tree newdecl, olddecl;
3615 if (TREE_CODE (newdecl) == TYPE_DECL)
3617 /* Because C++ can put things into name space for free,
3618 constructs like "typedef struct foo { ... } foo"
3619 would look like an erroneous redeclaration. */
3620 if (comptypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl), 0))
3623 return "redefinition of `%#D'";
3625 else if (TREE_CODE (newdecl) == FUNCTION_DECL)
3627 /* If this is a pure function, its olddecl will actually be
3628 the original initialization to `0' (which we force to call
3629 abort()). Don't complain about redefinition in this case. */
3630 if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
3633 /* We'll complain about linkage mismatches in
3634 warn_extern_redeclared_static. */
3636 /* defining the same name twice is no good. */
3637 if (DECL_INITIAL (olddecl) != NULL_TREE
3638 && DECL_INITIAL (newdecl) != NULL_TREE)
3640 if (DECL_NAME (olddecl) == NULL_TREE)
3641 return "`%#D' not declared in class";
3643 return "redefinition of `%#D'";
3647 else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3649 if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3650 && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
3651 && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
3652 || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
3653 && TYPE_SIZE (TREE_TYPE (newdecl))
3654 && TYPE_SIZE (TREE_TYPE (olddecl))))
3655 return "redefinition of `%#D'";
3658 else if (toplevel_bindings_p ())
3660 /* Objects declared at top level: */
3661 /* If at least one is a reference, it's ok. */
3662 if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
3664 /* Reject two definitions. */
3665 return "redefinition of `%#D'";
3669 /* Objects declared with block scope: */
3670 /* Reject two definitions, and reject a definition
3671 together with an external reference. */
3672 if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
3673 return "redeclaration of `%#D'";
3678 /* Get the LABEL_DECL corresponding to identifier ID as a label.
3679 Create one if none exists so far for the current function.
3680 This function is called for both label definitions and label references. */
3686 register tree decl = IDENTIFIER_LABEL_VALUE (id);
3688 if (current_function_decl == NULL_TREE)
3690 error ("label `%s' referenced outside of any function",
3691 IDENTIFIER_POINTER (id));
3695 if ((decl == NULL_TREE
3696 || DECL_SOURCE_LINE (decl) == 0)
3697 && (named_label_uses == NULL
3698 || named_label_uses->names_in_scope != current_binding_level->names
3699 || named_label_uses->label_decl != decl))
3701 struct named_label_list *new_ent;
3703 = (struct named_label_list*)oballoc (sizeof (struct named_label_list));
3704 new_ent->label_decl = decl;
3705 new_ent->names_in_scope = current_binding_level->names;
3706 new_ent->binding_level = current_binding_level;
3707 new_ent->lineno_o_goto = lineno;
3708 new_ent->filename_o_goto = input_filename;
3709 new_ent->next = named_label_uses;
3710 named_label_uses = new_ent;
3713 /* Use a label already defined or ref'd with this name. */
3714 if (decl != NULL_TREE)
3716 /* But not if it is inherited and wasn't declared to be inheritable. */
3717 if (DECL_CONTEXT (decl) != current_function_decl
3718 && ! C_DECLARED_LABEL_FLAG (decl))
3719 return shadow_label (id);
3723 decl = build_decl (LABEL_DECL, id, void_type_node);
3725 /* Make sure every label has an rtx. */
3728 /* A label not explicitly declared must be local to where it's ref'd. */
3729 DECL_CONTEXT (decl) = current_function_decl;
3731 DECL_MODE (decl) = VOIDmode;
3733 /* Say where one reference is to the label,
3734 for the sake of the error if it is not defined. */
3735 DECL_SOURCE_LINE (decl) = lineno;
3736 DECL_SOURCE_FILE (decl) = input_filename;
3738 SET_IDENTIFIER_LABEL_VALUE (id, decl);
3740 named_labels = tree_cons (NULL_TREE, decl, named_labels);
3741 named_label_uses->label_decl = decl;
3746 /* Make a label named NAME in the current function,
3747 shadowing silently any that may be inherited from containing functions
3748 or containing scopes.
3750 Note that valid use, if the label being shadowed
3751 comes from another scope in the same function,
3752 requires calling declare_nonlocal_label right away. */
3758 register tree decl = IDENTIFIER_LABEL_VALUE (name);
3760 if (decl != NULL_TREE)
3762 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
3763 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
3764 SET_IDENTIFIER_LABEL_VALUE (decl, NULL_TREE);
3767 return lookup_label (name);
3770 /* Define a label, specifying the location in the source file.
3771 Return the LABEL_DECL node for the label, if the definition is valid.
3772 Otherwise return 0. */
3775 define_label (filename, line, name)
3782 if (minimal_parse_mode)
3784 push_obstacks (&permanent_obstack, &permanent_obstack);
3785 decl = build_decl (LABEL_DECL, name, void_type_node);
3787 DECL_SOURCE_LINE (decl) = line;
3788 DECL_SOURCE_FILE (decl) = filename;
3793 decl = lookup_label (name);
3795 /* After labels, make any new cleanups go into their
3796 own new (temporary) binding contour. */
3797 current_binding_level->more_cleanups_ok = 0;
3799 /* If label with this name is known from an outer context, shadow it. */
3800 if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
3802 shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
3803 SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
3804 decl = lookup_label (name);
3807 if (name == get_identifier ("wchar_t"))
3808 cp_pedwarn ("label named wchar_t");
3810 if (DECL_INITIAL (decl) != NULL_TREE)
3812 cp_error ("duplicate label `%D'", decl);
3817 struct named_label_list *uses, *prev;
3820 /* Mark label as having been defined. */
3821 DECL_INITIAL (decl) = error_mark_node;
3822 /* Say where in the source. */
3823 DECL_SOURCE_FILE (decl) = filename;
3824 DECL_SOURCE_LINE (decl) = line;
3827 uses = named_label_uses;
3828 while (uses != NULL)
3829 if (uses->label_decl == decl)
3831 struct binding_level *b = current_binding_level;
3834 tree new_decls = b->names;
3835 tree old_decls = (b == uses->binding_level)
3836 ? uses->names_in_scope : NULL_TREE;
3837 while (new_decls != old_decls)
3839 if (TREE_CODE (new_decls) == VAR_DECL
3840 /* Don't complain about crossing initialization
3841 of internal entities. They can't be accessed,
3842 and they should be cleaned up
3843 by the time we get to the label. */
3844 && ! DECL_ARTIFICIAL (new_decls)
3845 && ((DECL_INITIAL (new_decls) != NULL_TREE
3846 && DECL_INITIAL (new_decls) != error_mark_node)
3847 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
3851 cp_error ("jump to label `%D'", decl);
3852 error_with_file_and_line (uses->filename_o_goto,
3853 uses->lineno_o_goto,
3857 cp_error_at (" crosses initialization of `%#D'",
3860 new_decls = TREE_CHAIN (new_decls);
3862 if (b == uses->binding_level)
3868 prev->next = uses->next;
3870 named_label_uses = uses->next;
3879 current_function_return_value = NULL_TREE;
3886 struct binding_level *level;
3887 struct cp_switch *next;
3890 static struct cp_switch *switch_stack;
3896 = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
3897 p->level = current_binding_level;
3898 p->next = switch_stack;
3905 switch_stack = switch_stack->next;
3908 /* Same, but for CASE labels. If DECL is NULL_TREE, it's the default. */
3909 /* XXX Note decl is never actually used. (bpk) */
3912 define_case_label (decl)
3915 tree cleanup = last_cleanup_this_contour ();
3916 struct binding_level *b = current_binding_level;
3921 static int explained = 0;
3922 cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
3923 warning ("where case label appears here");
3926 warning ("(enclose actions of previous case statements requiring");
3927 warning ("destructors in their own binding contours.)");
3932 for (; b && b != switch_stack->level; b = b->level_chain)
3934 tree new_decls = b->names;
3935 for (; new_decls; new_decls = TREE_CHAIN (new_decls))
3937 if (TREE_CODE (new_decls) == VAR_DECL
3938 /* Don't complain about crossing initialization
3939 of internal entities. They can't be accessed,
3940 and they should be cleaned up
3941 by the time we get to the label. */
3942 && ! DECL_ARTIFICIAL (new_decls)
3943 && ((DECL_INITIAL (new_decls) != NULL_TREE
3944 && DECL_INITIAL (new_decls) != error_mark_node)
3945 || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
3948 error ("jump to case label");
3950 cp_error_at (" crosses initialization of `%#D'",
3956 /* After labels, make any new cleanups go into their
3957 own new (temporary) binding contour. */
3959 current_binding_level->more_cleanups_ok = 0;
3960 current_function_return_value = NULL_TREE;
3963 /* Return the list of declarations of the current level.
3964 Note that this list is in reverse order unless/until
3965 you nreverse it; and when you do nreverse it, you must
3966 store the result back using `storedecls' or you will lose. */
3971 return current_binding_level->names;
3974 /* Return the list of type-tags (for structs, etc) of the current level. */
3979 return current_binding_level->tags;
3982 /* Store the list of declarations of the current level.
3983 This is done for the parameter declarations of a function being defined,
3984 after they are modified in the light of any missing parameters. */
3990 current_binding_level->names = decls;
3993 /* Similarly, store the list of tags of the current level. */
3999 current_binding_level->tags = tags;
4002 /* Given NAME, an IDENTIFIER_NODE,
4003 return the structure (or union or enum) definition for that name.
4004 Searches binding levels from BINDING_LEVEL up to the global level.
4005 If THISLEVEL_ONLY is nonzero, searches only the specified context
4006 (but skips any tag-transparent contexts to find one that is
4007 meaningful for tags).
4008 FORM says which kind of type the caller wants;
4009 it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
4010 If the wrong kind of type is found, and it's not a template, an error is
4014 lookup_tag (form, name, binding_level, thislevel_only)
4015 enum tree_code form;
4017 struct binding_level *binding_level;
4020 register struct binding_level *level;
4022 for (level = binding_level; level; level = level->level_chain)
4025 if (ANON_AGGRNAME_P (name))
4026 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4028 /* There's no need for error checking here, because
4029 anon names are unique throughout the compilation. */
4030 if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
4031 return TREE_VALUE (tail);
4034 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4036 if (TREE_PURPOSE (tail) == name)
4038 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
4039 /* Should tighten this up; it'll probably permit
4040 UNION_TYPE and a struct template, for example. */
4042 && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
4044 /* Definition isn't the kind we were looking for. */
4045 cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
4049 return TREE_VALUE (tail);
4052 if (thislevel_only && ! level->tag_transparent)
4054 if (level->pseudo_global)
4056 tree t = IDENTIFIER_GLOBAL_VALUE (name);
4057 if (t && TREE_CODE (t) == TEMPLATE_DECL
4058 && TREE_CODE (DECL_TEMPLATE_RESULT (t)) == TYPE_DECL)
4059 return TREE_TYPE (t);
4063 if (current_class_type && level->level_chain == global_binding_level)
4065 /* Try looking in this class's tags before heading into
4066 global binding level. */
4067 tree context = current_class_type;
4070 switch (TREE_CODE_CLASS (TREE_CODE (context)))
4074 these_tags = CLASSTYPE_TAGS (context);
4075 if (ANON_AGGRNAME_P (name))
4078 if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
4080 return TREE_VALUE (tail);
4081 these_tags = TREE_CHAIN (these_tags);
4086 if (TREE_PURPOSE (these_tags) == name)
4088 if (TREE_CODE (TREE_VALUE (these_tags)) != form)
4090 cp_error ("`%#D' redeclared as %C in class scope",
4091 TREE_VALUE (tail), form);
4094 return TREE_VALUE (tail);
4096 these_tags = TREE_CHAIN (these_tags);
4098 /* If this type is not yet complete, then don't
4099 look at its context. */
4100 if (TYPE_SIZE (context) == NULL_TREE)
4102 /* Go to next enclosing type, if any. */
4103 context = DECL_CONTEXT (TYPE_MAIN_DECL (context));
4106 context = DECL_CONTEXT (context);
4109 my_friendly_abort (10);
4121 set_current_level_tags_transparency (tags_transparent)
4122 int tags_transparent;
4124 current_binding_level->tag_transparent = tags_transparent;
4127 /* Given a type, find the tag that was defined for it and return the tag name.
4128 Otherwise return 0. However, the value can never be 0
4129 in the cases in which this is used.
4131 C++: If NAME is non-zero, this is the new name to install. This is
4132 done when replacing anonymous tags with real tag names. */
4135 lookup_tag_reverse (type, name)
4139 register struct binding_level *level;
4141 for (level = current_binding_level; level; level = level->level_chain)
4144 for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
4146 if (TREE_VALUE (tail) == type)
4149 TREE_PURPOSE (tail) = name;
4150 return TREE_PURPOSE (tail);
4157 /* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
4158 Return the type value, or NULL_TREE if not found. */
4161 lookup_nested_type (type, context)
4165 if (context == NULL_TREE)
4169 switch (TREE_CODE (context))
4173 tree ctype = TREE_TYPE (context);
4174 tree match = value_member (type, CLASSTYPE_TAGS (ctype));
4176 return TREE_VALUE (match);
4177 context = DECL_CONTEXT (context);
4179 /* When we have a nested class whose&n