1/* Process declarations and variables for C compiler.
2   Copyright (C) 1988, 92-98, 1999 Free Software Foundation, Inc.
3   Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA.  */
21
22
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.  */
26
27/* ??? not all decl nodes are given the most useful possible
28   line numbers.  For example, the CONST_DECLs for enum values.  */
29
30#include "config.h"
31#include "system.h"
32#include "tree.h"
33#include "rtl.h"
34#include "flags.h"
35#include "cp-tree.h"
36#include "decl.h"
37#include "lex.h"
38#include <signal.h>
39#include "obstack.h"
40#include "defaults.h"
41#include "output.h"
42#include "except.h"
43#include "toplev.h"
44#include "../hash.h"
45
46#define obstack_chunk_alloc xmalloc
47#define obstack_chunk_free free
48
49extern tree builtin_return_address_fndecl;
50
51extern struct obstack permanent_obstack;
52extern struct obstack* saveable_obstack;
53
54extern int current_class_depth;
55
56extern tree static_ctors, static_dtors;
57
58extern int static_labelno;
59
60extern tree current_namespace;
61extern tree global_namespace;
62
63extern void (*print_error_function) PROTO((char *));
64extern int (*valid_lang_attribute) PROTO ((tree, tree, tree, tree));
65
66/* Obstack used for remembering local class declarations (like
67   enums and static (const) members.  */
68#include "stack.h"
69struct obstack decl_obstack;
70static struct stack_level *decl_stack;
71
72#ifndef CHAR_TYPE_SIZE
73#define CHAR_TYPE_SIZE BITS_PER_UNIT
74#endif
75
76#ifndef SHORT_TYPE_SIZE
77#define SHORT_TYPE_SIZE (BITS_PER_UNIT * MIN ((UNITS_PER_WORD + 1) / 2, 2))
78#endif
79
80#ifndef INT_TYPE_SIZE
81#define INT_TYPE_SIZE BITS_PER_WORD
82#endif
83
84#ifndef LONG_TYPE_SIZE
85#define LONG_TYPE_SIZE BITS_PER_WORD
86#endif
87
88#ifndef LONG_LONG_TYPE_SIZE
89#define LONG_LONG_TYPE_SIZE (BITS_PER_WORD * 2)
90#endif
91
92#ifndef WCHAR_UNSIGNED
93#define WCHAR_UNSIGNED 0
94#endif
95
96#ifndef FLOAT_TYPE_SIZE
97#define FLOAT_TYPE_SIZE BITS_PER_WORD
98#endif
99
100#ifndef DOUBLE_TYPE_SIZE
101#define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
102#endif
103
104#ifndef LONG_DOUBLE_TYPE_SIZE
105#define LONG_DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
106#endif
107
108#ifndef BOOL_TYPE_SIZE
109#ifdef SLOW_BYTE_ACCESS
110#define BOOL_TYPE_SIZE ((SLOW_BYTE_ACCESS) ? (POINTER_SIZE) : (CHAR_TYPE_SIZE))
111#else
112#define BOOL_TYPE_SIZE CHAR_TYPE_SIZE
113#endif
114#endif
115
116/* We let tm.h override the types used here, to handle trivial differences
117   such as the choice of unsigned int or long unsigned int for size_t.
118   When machines start needing nontrivial differences in the size type,
119   it would be best to do something here to figure out automatically
120   from other information what type to use.  */
121
122#ifndef SIZE_TYPE
123#define SIZE_TYPE "long unsigned int"
124#endif
125
126#ifndef PTRDIFF_TYPE
127#define PTRDIFF_TYPE "long int"
128#endif
129
130#ifndef WCHAR_TYPE
131#define WCHAR_TYPE "int"
132#endif
133
134static tree grokparms				PROTO((tree, int));
135static tree lookup_nested_type			PROTO((tree, tree));
136static const char *redeclaration_error_message	PROTO((tree, tree));
137
138static struct stack_level *push_decl_level PROTO((struct stack_level *,
139						  struct obstack *));
140static void push_binding_level PROTO((struct binding_level *, int,
141				      int));
142static void pop_binding_level PROTO((void));
143static void suspend_binding_level PROTO((void));
144static void resume_binding_level PROTO((struct binding_level *));
145static struct binding_level *make_binding_level PROTO((void));
146static void declare_namespace_level PROTO((void));
147static void signal_catch PROTO((int)) ATTRIBUTE_NORETURN;
148static void storedecls PROTO((tree));
149static void require_complete_types_for_parms PROTO((tree));
150static void push_overloaded_decl_1 PROTO((tree));
151static int ambi_op_p PROTO((tree));
152static int unary_op_p PROTO((tree));
153static tree store_bindings PROTO((tree, tree));
154static tree lookup_tag_reverse PROTO((tree, tree));
155static tree obscure_complex_init PROTO((tree, tree));
156static tree maybe_build_cleanup_1 PROTO((tree, tree));
157static tree lookup_name_real PROTO((tree, int, int, int));
158static void warn_extern_redeclared_static PROTO((tree, tree));
159static void grok_reference_init PROTO((tree, tree, tree));
160static tree grokfndecl PROTO((tree, tree, tree, tree, int,
161			      enum overload_flags, tree,
162			      tree, int, int, int, int, int, int, tree));
163static tree grokvardecl PROTO((tree, tree, RID_BIT_TYPE *, int, int, tree));
164static tree lookup_tag PROTO((enum tree_code, tree,
165			      struct binding_level *, int));
166static void set_identifier_type_value_with_scope
167	PROTO((tree, tree, struct binding_level *));
168static void record_builtin_type PROTO((enum rid, const char *, tree));
169static void record_unknown_type PROTO((tree, const char *));
170static int member_function_or_else PROTO((tree, tree, const char *));
171static void bad_specifiers PROTO((tree, const char *, int, int, int, int,
172				  int));
173static void lang_print_error_function PROTO((char *));
174static tree maybe_process_template_type_declaration PROTO((tree, int, struct binding_level*));
175static void check_for_uninitialized_const_var PROTO((tree));
176static unsigned long typename_hash PROTO((hash_table_key));
177static boolean typename_compare PROTO((hash_table_key, hash_table_key));
178static void push_binding PROTO((tree, tree, struct binding_level*));
179static int add_binding PROTO((tree, tree));
180static void pop_binding PROTO((tree, tree));
181static tree local_variable_p PROTO((tree));
182static tree find_binding PROTO((tree, tree));
183static tree select_decl PROTO((tree, int));
184static tree unqualified_namespace_lookup PROTO((tree, int));
185static int lookup_flags PROTO((int, int));
186static tree qualify_lookup PROTO((tree, int));
187static tree record_builtin_java_type PROTO((const char *, int));
188static const char *tag_name PROTO((enum tag_types code));
189static void find_class_binding_level PROTO((void));
190static struct binding_level *innermost_nonclass_level PROTO((void));
191static void finish_dtor PROTO((void));
192static void finish_ctor PROTO((int));
193static tree poplevel_class PROTO((void));
194static void warn_about_implicit_typename_lookup PROTO((tree, tree));
195static int walk_namespaces_r PROTO((tree, walk_namespaces_fn, void *));
196static int walk_globals_r PROTO((tree, void *));
197
198#if defined (DEBUG_CP_BINDING_LEVELS)
199static void indent PROTO((void));
200#endif
201
202/* A node which has tree code ERROR_MARK, and whose type is itself.
203   All erroneous expressions are replaced with this node.  All functions
204   that accept nodes as arguments should avoid generating error messages
205   if this node is one of the arguments, since it is undesirable to get
206   multiple error messages from one error in the input.  */
207
208tree error_mark_node;
209
210/* Erroneous argument lists can use this *IFF* they do not modify it.  */
211tree error_mark_list;
212
213/* INTEGER_TYPE and REAL_TYPE nodes for the standard data types */
214
215tree short_integer_type_node;
216tree integer_type_node;
217tree long_integer_type_node;
218tree long_long_integer_type_node;
219
220tree short_unsigned_type_node;
221tree unsigned_type_node;
222tree long_unsigned_type_node;
223tree long_long_unsigned_type_node;
224
225tree ptrdiff_type_node;
226
227tree unsigned_char_type_node;
228tree signed_char_type_node;
229tree char_type_node;
230tree wchar_type_node;
231tree signed_wchar_type_node;
232tree unsigned_wchar_type_node;
233
234tree wchar_decl_node;
235
236tree float_type_node;
237tree double_type_node;
238tree long_double_type_node;
239
240tree complex_integer_type_node;
241tree complex_float_type_node;
242tree complex_double_type_node;
243tree complex_long_double_type_node;
244
245tree intQI_type_node;
246tree intHI_type_node;
247tree intSI_type_node;
248tree intDI_type_node;
249#if HOST_BITS_PER_WIDE_INT >= 64
250tree intTI_type_node;
251#endif
252
253tree unsigned_intQI_type_node;
254tree unsigned_intHI_type_node;
255tree unsigned_intSI_type_node;
256tree unsigned_intDI_type_node;
257#if HOST_BITS_PER_WIDE_INT >= 64
258tree unsigned_intTI_type_node;
259#endif
260
261tree java_byte_type_node;
262tree java_short_type_node;
263tree java_int_type_node;
264tree java_long_type_node;
265tree java_float_type_node;
266tree java_double_type_node;
267tree java_char_type_node;
268tree java_boolean_type_node;
269
270/* A VOID_TYPE node, and the same, packaged in a TREE_LIST.  */
271
272tree void_type_node, void_list_node;
273tree void_zero_node;
274
275/* Nodes for types `void *' and `const void *'.  */
276
277tree ptr_type_node;
278tree const_ptr_type_node;
279
280/* Nodes for types `char *' and `const char *'.  */
281
282tree string_type_node, const_string_type_node;
283
284/* Type `char[256]' or something like it.
285   Used when an array of char is needed and the size is irrelevant.  */
286
287tree char_array_type_node;
288
289/* Type `int[256]' or something like it.
290   Used when an array of int needed and the size is irrelevant.  */
291
292tree int_array_type_node;
293
294/* Type `wchar_t[256]' or something like it.
295   Used when a wide string literal is created.  */
296
297tree wchar_array_type_node;
298
299/* The bool data type, and constants */
300tree boolean_type_node, boolean_true_node, boolean_false_node;
301
302/* Type `int ()' -- used for implicit declaration of functions.  */
303
304tree default_function_type;
305
306/* Function types `double (double)' and `double (double, double)', etc.  */
307
308static tree double_ftype_double, double_ftype_double_double;
309static tree int_ftype_int, long_ftype_long;
310static tree float_ftype_float;
311static tree ldouble_ftype_ldouble;
312
313/* Function type `int (const void *, const void *, size_t)' */
314static tree int_ftype_cptr_cptr_sizet;
315
316/* C++ extensions */
317tree vtable_entry_type;
318tree delta_type_node;
319#if 0
320/* Old rtti stuff.  */
321tree __baselist_desc_type_node;
322tree __i_desc_type_node, __m_desc_type_node;
323tree __t_desc_array_type, __i_desc_array_type, __m_desc_array_type;
324#endif
325tree __t_desc_type_node;
326#if 0
327tree __tp_desc_type_node;
328#endif
329tree __access_mode_type_node;
330tree __bltn_desc_type_node, __user_desc_type_node, __class_desc_type_node;
331tree __ptr_desc_type_node, __attr_desc_type_node, __func_desc_type_node;
332tree __ptmf_desc_type_node, __ptmd_desc_type_node;
333#if 0
334/* Not needed yet?  May be needed one day?  */
335tree __bltn_desc_array_type, __user_desc_array_type, __class_desc_array_type;
336tree __ptr_desc_array_type, __attr_dec_array_type, __func_desc_array_type;
337tree __ptmf_desc_array_type, __ptmd_desc_array_type;
338#endif
339
340/* This is the identifier __vlist. */
341tree vlist_identifier;
342
343/* This is the type _Vlist = vtable_entry_type**. */
344tree vlist_type_node;
345
346/* A null pointer of type _Vlist. */
347tree vlist_zero_node;
348
349/* Indicates that there is a type value in some namespace, although
350   that is not necessarily in scope at the moment. */
351
352static tree global_type_node;
353
354tree class_star_type_node;
355tree class_type_node, record_type_node, union_type_node, enum_type_node;
356tree unknown_type_node;
357tree opaque_type_node, signature_type_node;
358tree sigtable_entry_type;
359
360/* Array type `vtable_entry_type[]' */
361tree vtbl_type_node;
362tree vtbl_ptr_type_node;
363
364/* namespace std */
365tree std_node;
366int in_std = 0;
367
368/* Expect only namespace names now. */
369static int only_namespace_names;
370
371/* In a destructor, the point at which all derived class destroying
372   has been done, just before any base class destroying will be done.  */
373
374tree dtor_label;
375
376/* In a destructor, the last insn emitted after the start of the
377   function and the parms.  */
378
379static rtx last_dtor_insn;
380
381/* In a constructor, the last insn emitted after the start of the
382   function and the parms, the exception specification and any
383   function-try-block.  The constructor initializers are emitted after
384   this insn.  */
385
386static rtx last_parm_cleanup_insn;
387
388/* In a constructor, the point at which we are ready to return
389   the pointer to the initialized object.  */
390
391tree ctor_label;
392
393/* A FUNCTION_DECL which can call `abort'.  Not necessarily the
394   one that the user will declare, but sufficient to be called
395   by routines that want to abort the program.  */
396
397tree abort_fndecl;
398
399/* A FUNCTION_DECL for the default `::operator delete'.  */
400
401tree global_delete_fndecl;
402
403extern rtx cleanup_label, return_label;
404
405/* If original DECL_RESULT of current function was a register,
406   but due to being an addressable named return value, would up
407   on the stack, this variable holds the named return value's
408   original location.  */
409static rtx original_result_rtx;
410
411/* Sequence of insns which represents base initialization.  */
412tree base_init_expr;
413
414/* C++: Keep these around to reduce calls to `get_identifier'.
415   Identifiers for `this' in member functions and the auto-delete
416   parameter for destructors.  */
417tree this_identifier, in_charge_identifier;
418tree ctor_identifier, dtor_identifier;
419/* Used in pointer to member functions, in vtables, and in sigtables.  */
420tree pfn_identifier, index_identifier, delta_identifier, delta2_identifier;
421tree pfn_or_delta2_identifier, tag_identifier;
422tree vt_off_identifier;
423
424struct named_label_list
425{
426  struct binding_level *binding_level;
427  tree names_in_scope;
428  tree label_decl;
429  char *filename_o_goto;
430  int lineno_o_goto;
431  struct named_label_list *next;
432};
433
434/* A list (chain of TREE_LIST nodes) of named label uses.
435   The TREE_PURPOSE field is the list of variables defined
436   in the label's scope defined at the point of use.
437   The TREE_VALUE field is the LABEL_DECL used.
438   The TREE_TYPE field holds `current_binding_level' at the
439   point of the label's use.
440
441   BWAHAHAAHAHahhahahahaah.  No, no, no, said the little chicken.
442
443   Look at the pretty struct named_label_list. See the pretty struct
444   with the pretty named fields that describe what they do. See the
445   pretty lack of gratuitous casts. Notice the code got a lot cleaner.
446
447   Used only for jumps to as-yet undefined labels, since
448   jumps to defined labels can have their validity checked
449   by stmt.c.  */
450
451static struct named_label_list *named_label_uses = NULL;
452
453/* A list of objects which have constructors or destructors
454   which reside in the global scope.  The decl is stored in
455   the TREE_VALUE slot and the initializer is stored
456   in the TREE_PURPOSE slot.  */
457tree static_aggregates;
458
459/* -- end of C++ */
460
461/* Two expressions that are constants with value zero.
462   The first is of type `int', the second of type `void *'.  */
463
464tree integer_zero_node;
465tree null_pointer_node;
466
467/* The value for __null (NULL), namely, a zero of an integer type with
468   the same number of bits as a pointer.  */
469tree null_node;
470
471/* A node for the integer constants 1, 2, and 3.  */
472
473tree integer_one_node, integer_two_node, integer_three_node;
474
475/* While defining an enum type, this is 1 plus the last enumerator
476   constant value.  */
477
478static tree enum_next_value;
479
480/* Nonzero means that there was overflow computing enum_next_value.  */
481
482static int enum_overflow;
483
484/* Parsing a function declarator leaves a list of parameter names
485   or a chain or parameter decls here.  */
486
487tree last_function_parms;
488
489/* Parsing a function declarator leaves here a chain of structure
490   and enum types declared in the parmlist.  */
491
492static tree last_function_parm_tags;
493
494/* After parsing the declarator that starts a function definition,
495   `start_function' puts here the list of parameter names or chain of decls.
496   `store_parm_decls' finds it here.  */
497
498static tree current_function_parms;
499
500/* Similar, for last_function_parm_tags.  */
501static tree current_function_parm_tags;
502
503/* A list (chain of TREE_LIST nodes) of all LABEL_DECLs in the function
504   that have names.  Here so we can clear out their names' definitions
505   at the end of the function.  */
506
507static tree named_labels;
508
509/* A list of LABEL_DECLs from outer contexts that are currently shadowed.  */
510
511static tree shadowed_labels;
512
513/* The FUNCTION_DECL for the function currently being compiled,
514   or 0 if between functions.  */
515tree current_function_decl;
516
517/* Set to 0 at beginning of a function definition, set to 1 if
518   a return statement that specifies a return value is seen.  */
519
520int current_function_returns_value;
521
522/* Set to 0 at beginning of a function definition, set to 1 if
523   a return statement with no argument is seen.  */
524
525int current_function_returns_null;
526
527/* Set to 0 at beginning of a function definition, and whenever
528   a label (case or named) is defined.  Set to value of expression
529   returned from function when that value can be transformed into
530   a named return value.  */
531
532tree current_function_return_value;
533
534/* Nonzero means give `double' the same size as `float'.  */
535
536extern int flag_short_double;
537
538/* Nonzero means don't recognize any builtin functions.  */
539
540extern int flag_no_builtin;
541
542/* Nonzero means don't recognize the non-ANSI builtin functions.
543   -ansi sets this.  */
544
545extern int flag_no_nonansi_builtin;
546
547/* Nonzero means enable obscure ANSI features and disable GNU extensions
548   that might cause ANSI-compliant code to be miscompiled.  */
549
550extern int flag_ansi;
551
552/* Nonzero if we want to support huge (> 2^(sizeof(short)*8-1) bytes)
553   objects.  */
554extern int flag_huge_objects;
555
556/* Nonzero if we want to conserve space in the .o files.  We do this
557   by putting uninitialized data and runtime initialized data into
558   .common instead of .data at the expense of not flagging multiple
559   definitions.  */
560extern int flag_conserve_space;
561
562/* Pointers to the base and current top of the language name stack.  */
563
564extern tree *current_lang_base, *current_lang_stack;
565
566/* C and C++ flags are in decl2.c.  */
567
568/* Set to 0 at beginning of a constructor, set to 1
569   if that function does an allocation before referencing its
570   instance variable.  */
571static int current_function_assigns_this;
572int current_function_just_assigned_this;
573
574/* Set to 0 at beginning of a function.  Set non-zero when
575   store_parm_decls is called.  Don't call store_parm_decls
576   if this flag is non-zero!  */
577int current_function_parms_stored;
578
579/* Flag used when debugging spew.c */
580
581extern int spew_debug;
582
583/* This is a copy of the class_shadowed list of the previous class binding
584   contour when at global scope.  It's used to reset IDENTIFIER_CLASS_VALUEs
585   when entering another class scope (i.e. a cache miss).  */
586extern tree previous_class_values;
587
588/* A expression of value 0 with the same precision as a sizetype
589   node, but signed.  */
590tree signed_size_zero_node;
591
592/* The name of the anonymous namespace, throughout this translation
593   unit.  */
594tree anonymous_namespace_name;
595
596
597/* Allocate a level of searching.  */
598
599static
600struct stack_level *
601push_decl_level (stack, obstack)
602     struct stack_level *stack;
603     struct obstack *obstack;
604{
605  struct stack_level tem;
606  tem.prev = stack;
607
608  return push_stack_level (obstack, (char *)&tem, sizeof (tem));
609}
610
611/* For each binding contour we allocate a binding_level structure
612   which records the names defined in that contour.
613   Contours include:
614    0) the global one
615    1) one for each function definition,
616       where internal declarations of the parameters appear.
617    2) one for each compound statement,
618       to record its declarations.
619
620   The current meaning of a name can be found by searching the levels
621   from the current one out to the global one.
622
623   Off to the side, may be the class_binding_level.  This exists only
624   to catch class-local declarations.  It is otherwise nonexistent.
625
626   Also there may be binding levels that catch cleanups that must be
627   run when exceptions occur.  Thus, to see whether a name is bound in
628   the current scope, it is not enough to look in the
629   CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
630   instead.  */
631
632/* Note that the information in the `names' component of the global contour
633   is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
634
635struct binding_level
636  {
637    /* A chain of _DECL nodes for all variables, constants, functions,
638       and typedef types.  These are in the reverse of the order
639       supplied.  There may be OVERLOADs on this list, too, but they
640       are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
641    tree names;
642
643    /* A list of structure, union and enum definitions, for looking up
644       tag names.
645       It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
646       or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
647       or ENUMERAL_TYPE node.
648
649       C++: the TREE_VALUE nodes can be simple types for
650       component_bindings.  */
651    tree tags;
652
653    /* A list of USING_DECL nodes. */
654    tree usings;
655
656    /* A list of used namespaces. PURPOSE is the namespace,
657       VALUE the common ancestor with this binding_level's namespace. */
658    tree using_directives;
659
660    /* If this binding level is the binding level for a class, then
661       class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
662       is the name of an entity bound in the class; the TREE_VALUE is
663       the IDENTIFIER_CLASS_VALUE before we entered the class.  Thus,
664       when leaving class scope, we can restore the
665       IDENTIFIER_CLASS_VALUE by walking this list.  The TREE_TYPE is
666       the DECL bound by this name in the class.  */
667    tree class_shadowed;
668
669    /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
670       is used for all binding levels.  */
671    tree type_shadowed;
672
673    /* For each level (except not the global one),
674       a chain of BLOCK nodes for all the levels
675       that were entered and exited one level down.  */
676    tree blocks;
677
678    /* The BLOCK node for this level, if one has been preallocated.
679       If 0, the BLOCK is allocated (if needed) when the level is popped.  */
680    tree this_block;
681
682    /* The binding level which this one is contained in (inherits from).  */
683    struct binding_level *level_chain;
684
685    /* List of decls in `names' that have incomplete
686       structure or union types.  */
687    tree incomplete;
688
689    /* List of VAR_DECLS saved from a previous for statement.
690       These would be dead in ANSI-conforming code, but might
691       be referenced in ARM-era code.  These are stored in a
692       TREE_LIST; the TREE_VALUE is the actual declaration.  */
693    tree dead_vars_from_for;
694
695    /* 1 for the level that holds the parameters of a function.
696       2 for the level that holds a class declaration.
697       3 for levels that hold parameter declarations.  */
698    unsigned parm_flag : 4;
699
700    /* 1 means make a BLOCK for this level regardless of all else.
701       2 for temporary binding contours created by the compiler.  */
702    unsigned keep : 3;
703
704    /* Nonzero if this level "doesn't exist" for tags.  */
705    unsigned tag_transparent : 1;
706
707    /* Nonzero if this level can safely have additional
708       cleanup-needing variables added to it.  */
709    unsigned more_cleanups_ok : 1;
710    unsigned have_cleanups : 1;
711
712    /* Nonzero if this level is for storing the decls for template
713       parameters and generic decls; these decls will be discarded and
714       replaced with a TEMPLATE_DECL.  */
715    unsigned pseudo_global : 1;
716
717    /* This is set for a namespace binding level.  */
718    unsigned namespace_p : 1;
719
720    /* True if this level is that of a for-statement where we need to
721       worry about ambiguous (ARM or ANSI) scope rules.  */
722    unsigned is_for_scope : 1;
723
724    /* Two bits left for this word.  */
725
726#if defined(DEBUG_CP_BINDING_LEVELS)
727    /* Binding depth at which this level began.  */
728    unsigned binding_depth;
729#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
730  };
731
732#define NULL_BINDING_LEVEL ((struct binding_level *) NULL)
733
734/* The binding level currently in effect.  */
735
736static struct binding_level *current_binding_level;
737
738/* The binding level of the current class, if any.  */
739
740static struct binding_level *class_binding_level;
741
742/* A chain of binding_level structures awaiting reuse.  */
743
744static struct binding_level *free_binding_level;
745
746/* The outermost binding level, for names of file scope.
747   This is created when the compiler is started and exists
748   through the entire run.  */
749
750static struct binding_level *global_binding_level;
751
752/* Binding level structures are initialized by copying this one.  */
753
754static struct binding_level clear_binding_level;
755
756/* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
757
758static int keep_next_level_flag;
759
760#if defined(DEBUG_CP_BINDING_LEVELS)
761static int binding_depth = 0;
762static int is_class_level = 0;
763
764static void
765indent ()
766{
767  register unsigned i;
768
769  for (i = 0; i < binding_depth*2; i++)
770    putc (' ', stderr);
771}
772#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
773
774static tree pushdecl_with_scope	PROTO((tree, struct binding_level *));
775
776static void
777push_binding_level (newlevel, tag_transparent, keep)
778     struct binding_level *newlevel;
779     int tag_transparent, keep;
780{
781  /* Add this level to the front of the chain (stack) of levels that
782     are active.  */
783  *newlevel = clear_binding_level;
784  newlevel->level_chain = current_binding_level;
785  current_binding_level = newlevel;
786  newlevel->tag_transparent = tag_transparent;
787  newlevel->more_cleanups_ok = 1;
788  newlevel->keep = keep;
789#if defined(DEBUG_CP_BINDING_LEVELS)
790  newlevel->binding_depth = binding_depth;
791  indent ();
792  fprintf (stderr, "push %s level 0x%08x line %d\n",
793	   (is_class_level) ? "class" : "block", newlevel, lineno);
794  is_class_level = 0;
795  binding_depth++;
796#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
797}
798
799/* Find the innermost enclosing class scope, and reset
800   CLASS_BINDING_LEVEL appropriately.  */
801
802static void
803find_class_binding_level ()
804{
805  struct binding_level *level = current_binding_level;
806
807  while (level && level->parm_flag != 2)
808    level = level->level_chain;
809  if (level && level->parm_flag == 2)
810    class_binding_level = level;
811  else
812    class_binding_level = 0;
813}
814
815static void
816pop_binding_level ()
817{
818  if (global_binding_level)
819    {
820      /* Cannot pop a level, if there are none left to pop.  */
821      if (current_binding_level == global_binding_level)
822	my_friendly_abort (123);
823    }
824  /* Pop the current level, and free the structure for reuse.  */
825#if defined(DEBUG_CP_BINDING_LEVELS)
826  binding_depth--;
827  indent ();
828  fprintf (stderr, "pop  %s level 0x%08x line %d\n",
829	  (is_class_level) ? "class" : "block",
830	  current_binding_level, lineno);
831  if (is_class_level != (current_binding_level == class_binding_level))
832    {
833      indent ();
834      fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
835    }
836  is_class_level = 0;
837#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
838  {
839    register struct binding_level *level = current_binding_level;
840    current_binding_level = current_binding_level->level_chain;
841    level->level_chain = free_binding_level;
842#if 0 /* defined(DEBUG_CP_BINDING_LEVELS) */
843    if (level->binding_depth != binding_depth)
844      abort ();
845#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
846    free_binding_level = level;
847    find_class_binding_level ();
848  }
849}
850
851static void
852suspend_binding_level ()
853{
854  if (class_binding_level)
855    current_binding_level = class_binding_level;
856
857  if (global_binding_level)
858    {
859      /* Cannot suspend a level, if there are none left to suspend.  */
860      if (current_binding_level == global_binding_level)
861	my_friendly_abort (123);
862    }
863  /* Suspend the current level.  */
864#if defined(DEBUG_CP_BINDING_LEVELS)
865  binding_depth--;
866  indent ();
867  fprintf (stderr, "suspend  %s level 0x%08x line %d\n",
868	  (is_class_level) ? "class" : "block",
869	  current_binding_level, lineno);
870  if (is_class_level != (current_binding_level == class_binding_level))
871    {
872      indent ();
873      fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
874    }
875  is_class_level = 0;
876#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
877  current_binding_level = current_binding_level->level_chain;
878  find_class_binding_level ();
879}
880
881static void
882resume_binding_level (b)
883     struct binding_level *b;
884{
885  /* Resuming binding levels is meant only for namespaces,
886     and those cannot nest into classes. */
887  my_friendly_assert(!class_binding_level, 386);
888  /* Also, resuming a non-directly nested namespace is a no-no.  */
889  my_friendly_assert(b->level_chain == current_binding_level, 386);
890  current_binding_level = b;
891#if defined(DEBUG_CP_BINDING_LEVELS)
892  b->binding_depth = binding_depth;
893  indent ();
894  fprintf (stderr, "resume %s level 0x%08x line %d\n",
895	   (is_class_level) ? "class" : "block", b, lineno);
896  is_class_level = 0;
897  binding_depth++;
898#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
899}
900
901/* Create a new `struct binding_level'.  */
902
903static
904struct binding_level *
905make_binding_level ()
906{
907  /* NOSTRICT */
908  return (struct binding_level *) xmalloc (sizeof (struct binding_level));
909}
910
911/* Nonzero if we are currently in the global binding level.  */
912
913int
914global_bindings_p ()
915{
916  return current_binding_level == global_binding_level;
917}
918
919/* Return the innermost binding level that is not for a class scope.  */
920
921static struct binding_level *
922innermost_nonclass_level ()
923{
924  struct binding_level *b;
925
926  b = current_binding_level;
927  while (b->parm_flag == 2)
928    b = b->level_chain;
929
930  return b;
931}
932
933/* Nonzero if we are currently in a toplevel binding level.  This
934   means either the global binding level or a namespace in a toplevel
935   binding level.  Since there are no non-toplevel namespace levels,
936   this really means any namespace or pseudo-global level.  We also
937   include a class whose context is toplevel.  */
938
939int
940toplevel_bindings_p ()
941{
942  struct binding_level *b = innermost_nonclass_level ();
943
944  return b->namespace_p || b->pseudo_global;
945}
946
947/* Nonzero if this is a namespace scope, or if we are defining a class
948   which is itself at namespace scope, or whose enclosing class is
949   such a class, etc.  */
950
951int
952namespace_bindings_p ()
953{
954  struct binding_level *b = innermost_nonclass_level ();
955
956  return b->namespace_p;
957}
958
959void
960keep_next_level ()
961{
962  keep_next_level_flag = 1;
963}
964
965/* Nonzero if the current level needs to have a BLOCK made.  */
966
967int
968kept_level_p ()
969{
970  return (current_binding_level->blocks != NULL_TREE
971	  || current_binding_level->keep
972	  || current_binding_level->names != NULL_TREE
973	  || (current_binding_level->tags != NULL_TREE
974	      && !current_binding_level->tag_transparent));
975}
976
977/* Identify this binding level as a level of parameters.  */
978
979void
980declare_parm_level ()
981{
982  current_binding_level->parm_flag = 1;
983}
984
985void
986declare_pseudo_global_level ()
987{
988  current_binding_level->pseudo_global = 1;
989}
990
991static void
992declare_namespace_level ()
993{
994  current_binding_level->namespace_p = 1;
995}
996
997int
998pseudo_global_level_p ()
999{
1000  struct binding_level *b = innermost_nonclass_level ();
1001
1002  return b->pseudo_global;
1003}
1004
1005void
1006set_class_shadows (shadows)
1007     tree shadows;
1008{
1009  class_binding_level->class_shadowed = shadows;
1010}
1011
1012/* Enter a new binding level.
1013   If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
1014   not for that of tags.  */
1015
1016void
1017pushlevel (tag_transparent)
1018     int tag_transparent;
1019{
1020  register struct binding_level *newlevel = NULL_BINDING_LEVEL;
1021
1022  /* If this is the top level of a function,
1023     just make sure that NAMED_LABELS is 0.
1024     They should have been set to 0 at the end of the previous function.  */
1025
1026  if (current_binding_level == global_binding_level)
1027    my_friendly_assert (named_labels == NULL_TREE, 134);
1028
1029  /* Reuse or create a struct for this binding level.  */
1030
1031#if defined(DEBUG_CP_BINDING_LEVELS)
1032  if (0)
1033#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1034  if (free_binding_level)
1035#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1036    {
1037      newlevel = free_binding_level;
1038      free_binding_level = free_binding_level->level_chain;
1039    }
1040  else
1041    {
1042      newlevel = make_binding_level ();
1043    }
1044
1045  push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
1046  GNU_xref_start_scope ((HOST_WIDE_INT) newlevel);
1047  keep_next_level_flag = 0;
1048}
1049
1050void
1051note_level_for_for ()
1052{
1053  current_binding_level->is_for_scope = 1;
1054}
1055
1056void
1057pushlevel_temporary (tag_transparent)
1058     int tag_transparent;
1059{
1060  pushlevel (tag_transparent);
1061  current_binding_level->keep = 2;
1062  clear_last_expr ();
1063
1064  /* Note we don't call push_momentary() here.  Otherwise, it would cause
1065     cleanups to be allocated on the momentary obstack, and they will be
1066     overwritten by the next statement.  */
1067
1068  expand_start_bindings (0);
1069}
1070
1071/* For a binding between a name and an entity at a block scope,
1072   this is the `struct binding_level' for the block.  */
1073#define BINDING_LEVEL(NODE) \
1074   (((struct tree_binding*)NODE)->scope.level)
1075
1076/* These are currently unused, but permanent, CPLUS_BINDING nodes.
1077   They are kept here because they are allocated from the permanent
1078   obstack and cannot be easily freed.  */
1079static tree free_binding_nodes;
1080
1081/* Make DECL the innermost binding for ID.  The LEVEL is the binding
1082   level at which this declaration is being bound.  */
1083
1084static void
1085push_binding (id, decl, level)
1086     tree id;
1087     tree decl;
1088     struct binding_level* level;
1089{
1090  tree binding;
1091
1092  if (!free_binding_nodes)
1093    {
1094      /* There are no free nodes, so we must build one here.  */
1095      push_obstacks_nochange ();
1096      end_temporary_allocation ();
1097      binding = make_node (CPLUS_BINDING);
1098      pop_obstacks ();
1099    }
1100  else
1101    {
1102      /* There are nodes on the free list.  Grab the first one.  */
1103      binding = free_binding_nodes;
1104
1105      /* And update the free list.  */
1106      free_binding_nodes = TREE_CHAIN (free_binding_nodes);
1107    }
1108
1109  /* Now, fill in the binding information.  */
1110  BINDING_VALUE (binding) = decl;
1111  BINDING_TYPE (binding) = NULL_TREE;
1112  BINDING_LEVEL (binding) = level;
1113  INHERITED_VALUE_BINDING_P (binding) = 0;
1114  LOCAL_BINDING_P (binding) = (level != class_binding_level);
1115
1116  /* And put it on the front of the list of bindings for ID.  */
1117  TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
1118  IDENTIFIER_BINDING (id) = binding;
1119}
1120
1121/* ID is already bound in the current scope.  But, DECL is an
1122   additional binding for ID in the same scope.  This is the `struct
1123   stat' hack whereby a non-typedef class-name or enum-name can be
1124   bound at the same level as some other kind of entity.  It's the
1125   responsibility of the caller to check that inserting this name is
1126   legal here.  Returns nonzero if the new binding was successful.  */
1127static int
1128add_binding (id, decl)
1129     tree id;
1130     tree decl;
1131{
1132  tree binding = IDENTIFIER_BINDING (id);
1133  int ok = 1;
1134
1135  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1136    /* The new name is the type name.  */
1137    BINDING_TYPE (binding) = decl;
1138  else if (!BINDING_VALUE (binding))
1139    /* This situation arises when push_class_level_binding moves an
1140       inherited type-binding out of the way to make room for a new
1141       value binding.  */
1142    BINDING_VALUE (binding) = decl;
1143  else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1144	   && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
1145    {
1146      /* The old binding was a type name.  It was placed in
1147	 BINDING_VALUE because it was thought, at the point it was
1148	 declared, to be the only entity with such a name.  Move the
1149	 type name into the type slot; it is now hidden by the new
1150	 binding.  */
1151      BINDING_TYPE (binding) = BINDING_VALUE (binding);
1152      BINDING_VALUE (binding) = decl;
1153      INHERITED_VALUE_BINDING_P (binding) = 0;
1154    }
1155  else
1156    {
1157      cp_error ("declaration of `%#D'", decl);
1158      cp_error_at ("conflicts with previous declaration `%#D'",
1159		   BINDING_VALUE (binding));
1160      ok = 0;
1161    }
1162
1163  return ok;
1164}
1165
1166/* Bind DECL to ID in the current_binding_level.
1167   If PUSH_USING is set in FLAGS, we know that DECL doesn't really belong
1168   to this binding level, that it got here through a using-declaration.  */
1169
1170void
1171push_local_binding (id, decl, flags)
1172     tree id;
1173     tree decl;
1174     int flags;
1175{
1176  struct binding_level *b;
1177
1178  /* Skip over any local classes.  This makes sense if we call
1179     push_local_binding with a friend decl of a local class.  */
1180  b = current_binding_level;
1181  while (b->parm_flag == 2)
1182    b = b->level_chain;
1183
1184  if (lookup_name_current_level (id))
1185    {
1186      /* Supplement the existing binding.  */
1187      if (!add_binding (id, decl))
1188	/* It didn't work.  Something else must be bound at this
1189	   level.  Do not add DECL to the list of things to pop
1190	   later.  */
1191	return;
1192    }
1193  else
1194    /* Create a new binding.  */
1195    push_binding (id, decl, b);
1196
1197  if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1198    /* We must put the OVERLOAD into a TREE_LIST since the
1199       TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1200       decls that got here through a using-declaration.  */
1201    decl = build_tree_list (NULL_TREE, decl);
1202
1203  /* And put DECL on the list of things declared by the current
1204     binding level.  */
1205  TREE_CHAIN (decl) = b->names;
1206  b->names = decl;
1207}
1208
1209/* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1210   binding was successful.  */
1211
1212int
1213push_class_binding (id, decl)
1214     tree id;
1215     tree decl;
1216{
1217  int result = 1;
1218  tree binding = IDENTIFIER_BINDING (id);
1219  tree context;
1220
1221  /* Note that we declared this value so that we can issue an error if
1222     this an illegal redeclaration of a name already used for some
1223     other purpose.  */
1224  note_name_declared_in_class (id, decl);
1225
1226  if (binding && BINDING_LEVEL (binding) == class_binding_level)
1227    /* Supplement the existing binding.  */
1228    result = add_binding (id, decl);
1229  else
1230    /* Create a new binding.  */
1231    push_binding (id, decl, class_binding_level);
1232
1233  /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1234     class-level declaration.  Note that we do not use DECL here
1235     because of the possibility of the `struct stat' hack; if DECL is
1236     a class-name or enum-name we might prefer a field-name, or some
1237     such.  */
1238  IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1239
1240  /* If this is a binding from a base class, mark it as such.  */
1241  binding = IDENTIFIER_BINDING (id);
1242  if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1243    {
1244      /* Any implicit typename must be from a base-class.  The
1245	 context for an implicit typename declaration is always
1246	 the derived class in which the lookup was done, so the checks
1247	 based on the context of DECL below will not trigger.  */
1248      if (TREE_CODE (decl) == TYPE_DECL
1249	  && IMPLICIT_TYPENAME_P (TREE_TYPE (decl)))
1250	INHERITED_VALUE_BINDING_P (binding) = 1;
1251      else
1252	{
1253	  if (TREE_CODE (decl) == OVERLOAD)
1254	    context = DECL_REAL_CONTEXT (OVL_CURRENT (decl));
1255	  else
1256	    {
1257	      my_friendly_assert (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd',
1258				  0);
1259	      context = DECL_REAL_CONTEXT (decl);
1260	    }
1261
1262	  if (is_properly_derived_from (current_class_type, context))
1263	    INHERITED_VALUE_BINDING_P (binding) = 1;
1264	  else
1265	    INHERITED_VALUE_BINDING_P (binding) = 0;
1266	}
1267    }
1268  else if (BINDING_VALUE (binding) == decl)
1269    /* We only encounter a TREE_LIST when push_class_decls detects an
1270       ambiguity.  Such an ambiguity can be overridden by a definition
1271       in this class.  */
1272    INHERITED_VALUE_BINDING_P (binding) = 1;
1273
1274  return result;
1275}
1276
1277/* Remove the binding for DECL which should be the innermost binding
1278   for ID.  */
1279
1280static void
1281pop_binding (id, decl)
1282     tree id;
1283     tree decl;
1284{
1285  tree binding;
1286
1287  if (id == NULL_TREE)
1288    /* It's easiest to write the loops that call this function without
1289       checking whether or not the entities involved have names.  We
1290       get here for such an entity.  */
1291    return;
1292
1293  /* Get the innermost binding for ID.  */
1294  binding = IDENTIFIER_BINDING (id);
1295
1296  /* The name should be bound.  */
1297  my_friendly_assert (binding != NULL_TREE, 0);
1298
1299  /* The DECL will be either the ordinary binding or the type
1300     binding for this identifier.  Remove that binding.  */
1301  if (BINDING_VALUE (binding) == decl)
1302    BINDING_VALUE (binding) = NULL_TREE;
1303  else if (BINDING_TYPE (binding) == decl)
1304    BINDING_TYPE (binding) = NULL_TREE;
1305  else
1306    my_friendly_abort (0);
1307
1308  if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1309    {
1310      /* We're completely done with the innermost binding for this
1311	 identifier.  Unhook it from the list of bindings.  */
1312      IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1313
1314      /* And place it on the free list.  */
1315      TREE_CHAIN (binding) = free_binding_nodes;
1316      free_binding_nodes = binding;
1317    }
1318}
1319
1320/* Exit a binding level.
1321   Pop the level off, and restore the state of the identifier-decl mappings
1322   that were in effect when this level was entered.
1323
1324   If KEEP == 1, this level had explicit declarations, so
1325   and create a "block" (a BLOCK node) for the level
1326   to record its declarations and subblocks for symbol table output.
1327
1328   If FUNCTIONBODY is nonzero, this level is the body of a function,
1329   so create a block as if KEEP were set and also clear out all
1330   label names.
1331
1332   If REVERSE is nonzero, reverse the order of decls before putting
1333   them into the BLOCK.  */
1334
1335tree
1336poplevel (keep, reverse, functionbody)
1337     int keep;
1338     int reverse;
1339     int functionbody;
1340{
1341  register tree link;
1342  /* The chain of decls was accumulated in reverse order.
1343     Put it into forward order, just for cleanliness.  */
1344  tree decls;
1345  int tmp = functionbody;
1346  int real_functionbody = current_binding_level->keep == 2
1347    ? ((functionbody = 0), tmp) : functionbody;
1348  tree tags = functionbody >= 0 ? current_binding_level->tags : 0;
1349  tree subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1350  tree block = NULL_TREE;
1351  tree decl;
1352  int block_previously_created;
1353  int leaving_for_scope;
1354
1355  if (current_binding_level->parm_flag == 2)
1356    return poplevel_class ();
1357
1358  my_friendly_assert (!current_binding_level->class_shadowed,
1359		      19990414);
1360
1361  /* We used to use KEEP == 2 to indicate that the new block should go
1362     at the beginning of the list of blocks at this binding level,
1363     rather than the end.  This hack is no longer used.  */
1364  my_friendly_assert (keep == 0 || keep == 1, 0);
1365
1366  GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
1367		      (HOST_WIDE_INT) current_binding_level->level_chain,
1368		      current_binding_level->parm_flag,
1369		      current_binding_level->keep);
1370
1371  if (current_binding_level->keep == 1)
1372    keep = 1;
1373
1374  /* Get the decls in the order they were written.
1375     Usually current_binding_level->names is in reverse order.
1376     But parameter decls were previously put in forward order.  */
1377
1378  if (reverse)
1379    current_binding_level->names
1380      = decls = nreverse (current_binding_level->names);
1381  else
1382    decls = current_binding_level->names;
1383
1384  /* Output any nested inline functions within this block
1385     if they weren't already output.  */
1386
1387  for (decl = decls; decl; decl = TREE_CHAIN (decl))
1388    if (TREE_CODE (decl) == FUNCTION_DECL
1389	&& ! TREE_ASM_WRITTEN (decl)
1390	&& DECL_INITIAL (decl) != NULL_TREE
1391	&& TREE_ADDRESSABLE (decl)
1392	&& decl_function_context (decl) == current_function_decl)
1393      {
1394	/* If this decl was copied from a file-scope decl
1395	   on account of a block-scope extern decl,
1396	   propagate TREE_ADDRESSABLE to the file-scope decl.  */
1397	if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1398	  TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1399	else
1400	  {
1401	    push_function_context ();
1402	    output_inline_function (decl);
1403	    pop_function_context ();
1404	  }
1405      }
1406
1407  /* If there were any declarations or structure tags in that level,
1408     or if this level is a function body,
1409     create a BLOCK to record them for the life of this function.  */
1410
1411  block = NULL_TREE;
1412  block_previously_created = (current_binding_level->this_block != NULL_TREE);
1413  if (block_previously_created)
1414    block = current_binding_level->this_block;
1415  else if (keep == 1 || functionbody)
1416    block = make_node (BLOCK);
1417  if (block != NULL_TREE)
1418    {
1419      if (block_previously_created)
1420	{
1421	  if (decls || tags || subblocks)
1422	    {
1423	      if (BLOCK_VARS (block) || BLOCK_TYPE_TAGS (block))
1424		warning ("internal compiler error: debugging info corrupted");
1425
1426	      BLOCK_VARS (block) = decls;
1427	      BLOCK_TYPE_TAGS (block) = tags;
1428
1429	      /* We can have previous subblocks and new subblocks when
1430		 doing fixup_gotos with complex cleanups.  We chain the new
1431		 subblocks onto the end of any pre-existing subblocks.  */
1432	      BLOCK_SUBBLOCKS (block) = chainon (BLOCK_SUBBLOCKS (block),
1433						 subblocks);
1434	    }
1435	  /* If we created the block earlier on, and we are just
1436	     diddling it now, then it already should have a proper
1437	     BLOCK_END_NOTE value associated with it.  */
1438	}
1439      else
1440	{
1441	  BLOCK_VARS (block) = decls;
1442	  BLOCK_TYPE_TAGS (block) = tags;
1443	  BLOCK_SUBBLOCKS (block) = subblocks;
1444	  /* Otherwise, for a new block, install a new BLOCK_END_NOTE
1445	     value.  */
1446	  remember_end_note (block);
1447	}
1448    }
1449
1450  /* In each subblock, record that this is its superior.  */
1451
1452  if (keep >= 0)
1453    for (link = subblocks; link; link = TREE_CHAIN (link))
1454      BLOCK_SUPERCONTEXT (link) = block;
1455
1456  /* We still support the old for-scope rules, whereby the variables
1457     in a for-init statement were in scope after the for-statement
1458     ended.  We only use the new rules in flag_new_for_scope is
1459     nonzero.  */
1460  leaving_for_scope
1461    = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1462
1463  /* Remove declarations for all the DECLs in this level.  */
1464  for (link = decls; link; link = TREE_CHAIN (link))
1465    {
1466      if (leaving_for_scope && TREE_CODE (link) == VAR_DECL)
1467	{
1468	  tree outer_binding
1469	    = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1470	  tree ns_binding;
1471
1472	  if (!outer_binding)
1473	    ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1474	  else
1475	    ns_binding = NULL_TREE;
1476
1477	  if (outer_binding
1478	      && (BINDING_LEVEL (outer_binding)
1479		  == current_binding_level->level_chain))
1480	    /* We have something like:
1481
1482	         int i;
1483	         for (int i; ;);
1484
1485	       and we are leaving the `for' scope.  There's no reason to
1486	       keep the binding of the inner `i' in this case.  */
1487	    pop_binding (DECL_NAME (link), link);
1488	  else if ((outer_binding
1489		    && (TREE_CODE (BINDING_VALUE (outer_binding))
1490			== TYPE_DECL))
1491		   || (ns_binding
1492		       && TREE_CODE (ns_binding) == TYPE_DECL))
1493	    /* Here, we have something like:
1494
1495		 typedef int I;
1496
1497		 void f () {
1498		   for (int I; ;);
1499		 }
1500
1501	       We must pop the for-scope binding so we know what's a
1502	       type and what isn't.  */
1503	    pop_binding (DECL_NAME (link), link);
1504	  else
1505	    {
1506	      /* Mark this VAR_DECL as dead so that we can tell we left it
1507		 there only for backward compatibility.  */
1508	      DECL_DEAD_FOR_LOCAL (link) = 1;
1509
1510	      /* Keep track of what should of have happenned when we
1511		 popped the binding.  */
1512	      if (outer_binding && BINDING_VALUE (outer_binding))
1513		DECL_SHADOWED_FOR_VAR (link)
1514		  = BINDING_VALUE (outer_binding);
1515
1516	      /* Add it to the list of dead variables in the next
1517		 outermost binding to that we can remove these when we
1518		 leave that binding.  */
1519	      current_binding_level->level_chain->dead_vars_from_for
1520		= tree_cons (NULL_TREE, link,
1521			     current_binding_level->level_chain->
1522			     dead_vars_from_for);
1523
1524	      /* Although we don't pop the CPLUS_BINDING, we do clear
1525		 its BINDING_LEVEL since the level is going away now.  */
1526	      BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1527		= 0;
1528	    }
1529	}
1530      else
1531	{
1532	  /* Remove the binding.  */
1533	  decl = link;
1534	  if (TREE_CODE (decl) == TREE_LIST)
1535	    decl = TREE_VALUE (decl);
1536	  if (TREE_CODE_CLASS (TREE_CODE (decl)) == 'd')
1537	    pop_binding (DECL_NAME (decl), decl);
1538	  else if (TREE_CODE (decl) == OVERLOAD)
1539	    pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1540	  else
1541	    my_friendly_abort (0);
1542	}
1543    }
1544
1545  /* Remove declarations for any `for' variables from inner scopes
1546     that we kept around.  */
1547  for (link = current_binding_level->dead_vars_from_for;
1548       link; link = TREE_CHAIN (link))
1549    pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1550
1551  /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1552  for (link = current_binding_level->type_shadowed;
1553       link; link = TREE_CHAIN (link))
1554    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1555
1556  /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1557     list if a `using' declaration put them there.  The debugging
1558     back-ends won't understand OVERLOAD, so we remove them here.
1559     Because the BLOCK_VARS are (temporarily) shared with
1560     CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1561     popped all the bindings.  */
1562  if (block)
1563    {
1564      tree* d;
1565
1566      for (d = &BLOCK_VARS (block); *d; )
1567	{
1568	  if (TREE_CODE (*d) == TREE_LIST)
1569	    *d = TREE_CHAIN (*d);
1570	  else
1571	    d = &TREE_CHAIN (*d);
1572	}
1573    }
1574
1575  /* If the level being exited is the top level of a function,
1576     check over all the labels.  */
1577
1578  if (functionbody)
1579    {
1580      /* If this is the top level block of a function,
1581         the vars are the function's parameters.
1582         Don't leave them in the BLOCK because they are
1583         found in the FUNCTION_DECL instead.  */
1584
1585      BLOCK_VARS (block) = 0;
1586
1587      /* Clear out the definitions of all label names,
1588	 since their scopes end here.  */
1589
1590      for (link = named_labels; link; link = TREE_CHAIN (link))
1591	{
1592	  register tree label = TREE_VALUE (link);
1593
1594	  if (DECL_INITIAL (label) == NULL_TREE)
1595	    {
1596	      cp_error_at ("label `%D' used but not defined", label);
1597	      /* Avoid crashing later.  */
1598	      define_label (input_filename, 1, DECL_NAME (label));
1599	    }
1600	  else if (warn_unused && !TREE_USED (label))
1601	    cp_warning_at ("label `%D' defined but not used", label);
1602	  SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), NULL_TREE);
1603
1604          /* Put the labels into the "variables" of the
1605             top-level block, so debugger can see them.  */
1606          TREE_CHAIN (label) = BLOCK_VARS (block);
1607          BLOCK_VARS (block) = label;
1608	}
1609
1610      named_labels = NULL_TREE;
1611    }
1612
1613  /* Any uses of undefined labels now operate under constraints
1614     of next binding contour.  */
1615  {
1616    struct binding_level *level_chain;
1617    level_chain = current_binding_level->level_chain;
1618    if (level_chain)
1619      {
1620	struct named_label_list *labels;
1621	for (labels = named_label_uses; labels; labels = labels->next)
1622	  if (labels->binding_level == current_binding_level)
1623	    {
1624	      labels->binding_level = level_chain;
1625	      labels->names_in_scope = level_chain->names;
1626	    }
1627      }
1628  }
1629
1630  tmp = current_binding_level->keep;
1631
1632  pop_binding_level ();
1633  if (functionbody)
1634    DECL_INITIAL (current_function_decl) = block;
1635  else if (block)
1636    {
1637      if (!block_previously_created)
1638        current_binding_level->blocks
1639          = chainon (current_binding_level->blocks, block);
1640    }
1641  /* If we did not make a block for the level just exited,
1642     any blocks made for inner levels
1643     (since they cannot be recorded as subblocks in that level)
1644     must be carried forward so they will later become subblocks
1645     of something else.  */
1646  else if (subblocks)
1647    current_binding_level->blocks
1648      = chainon (current_binding_level->blocks, subblocks);
1649
1650  /* Take care of compiler's internal binding structures.  */
1651  if (tmp == 2)
1652    {
1653      expand_end_bindings (getdecls (), keep, 1);
1654      /* Each and every BLOCK node created here in `poplevel' is important
1655	 (e.g. for proper debugging information) so if we created one
1656	 earlier, mark it as "used".  */
1657      if (block)
1658	TREE_USED (block) = 1;
1659      block = poplevel (keep, reverse, real_functionbody);
1660    }
1661
1662  /* Each and every BLOCK node created here in `poplevel' is important
1663     (e.g. for proper debugging information) so if we created one
1664     earlier, mark it as "used".  */
1665  if (block)
1666    TREE_USED (block) = 1;
1667  return block;
1668}
1669
1670/* Delete the node BLOCK from the current binding level.
1671   This is used for the block inside a stmt expr ({...})
1672   so that the block can be reinserted where appropriate.  */
1673
1674void
1675delete_block (block)
1676     tree block;
1677{
1678  tree t;
1679  if (current_binding_level->blocks == block)
1680    current_binding_level->blocks = TREE_CHAIN (block);
1681  for (t = current_binding_level->blocks; t;)
1682    {
1683      if (TREE_CHAIN (t) == block)
1684	TREE_CHAIN (t) = TREE_CHAIN (block);
1685      else
1686	t = TREE_CHAIN (t);
1687    }
1688  TREE_CHAIN (block) = NULL_TREE;
1689  /* Clear TREE_USED which is always set by poplevel.
1690     The flag is set again if insert_block is called.  */
1691  TREE_USED (block) = 0;
1692}
1693
1694/* Insert BLOCK at the end of the list of subblocks of the
1695   current binding level.  This is used when a BIND_EXPR is expanded,
1696   to handle the BLOCK node inside the BIND_EXPR.  */
1697
1698void
1699insert_block (block)
1700     tree block;
1701{
1702  TREE_USED (block) = 1;
1703  current_binding_level->blocks
1704    = chainon (current_binding_level->blocks, block);
1705}
1706
1707/* Set the BLOCK node for the innermost scope
1708   (the one we are currently in).  */
1709
1710void
1711set_block (block)
1712    register tree block;
1713{
1714  current_binding_level->this_block = block;
1715}
1716
1717/* Do a pushlevel for class declarations.  */
1718
1719void
1720pushlevel_class ()
1721{
1722  register struct binding_level *newlevel;
1723
1724  /* Reuse or create a struct for this binding level.  */
1725#if defined(DEBUG_CP_BINDING_LEVELS)
1726  if (0)
1727#else /* !defined(DEBUG_CP_BINDING_LEVELS) */
1728  if (free_binding_level)
1729#endif /* !defined(DEBUG_CP_BINDING_LEVELS) */
1730    {
1731      newlevel = free_binding_level;
1732      free_binding_level = free_binding_level->level_chain;
1733    }
1734  else
1735    newlevel = make_binding_level ();
1736
1737#if defined(DEBUG_CP_BINDING_LEVELS)
1738  is_class_level = 1;
1739#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1740
1741  push_binding_level (newlevel, 0, 0);
1742
1743  decl_stack = push_decl_level (decl_stack, &decl_obstack);
1744  class_binding_level = current_binding_level;
1745  class_binding_level->parm_flag = 2;
1746}
1747
1748/* ...and a poplevel for class declarations.  */
1749
1750static tree
1751poplevel_class ()
1752{
1753  register struct binding_level *level = class_binding_level;
1754  tree shadowed;
1755
1756  my_friendly_assert (level != 0, 354);
1757
1758  decl_stack = pop_stack_level (decl_stack);
1759  /* If we're leaving a toplevel class, don't bother to do the setting
1760     of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1761     shouldn't even be used when current_class_type isn't set, and second,
1762     if we don't touch it here, we're able to use the cache effect if the
1763     next time we're entering a class scope, it is the same class.  */
1764  if (current_class_depth != 1)
1765    {
1766      struct binding_level* b;
1767
1768      /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1769      for (shadowed = level->class_shadowed;
1770	   shadowed;
1771	   shadowed = TREE_CHAIN (shadowed))
1772	IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1773
1774      /* Find the next enclosing class, and recreate
1775	 IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1776      b = level->level_chain;
1777      while (b && b->parm_flag != 2)
1778	b = b->level_chain;
1779
1780      if (b)
1781	for (shadowed = b->class_shadowed;
1782	     shadowed;
1783	     shadowed = TREE_CHAIN (shadowed))
1784	  {
1785	    tree t;
1786
1787	    t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1788	    while (t && BINDING_LEVEL (t) != b)
1789	      t = TREE_CHAIN (t);
1790
1791	    if (t)
1792	      IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1793		= BINDING_VALUE (t);
1794	  }
1795    }
1796  else
1797    /* Remember to save what IDENTIFIER's were bound in this scope so we
1798       can recover from cache misses.  */
1799    {
1800      previous_class_type = current_class_type;
1801      previous_class_values = class_binding_level->class_shadowed;
1802    }
1803  for (shadowed = level->type_shadowed;
1804       shadowed;
1805       shadowed = TREE_CHAIN (shadowed))
1806    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1807
1808  /* Remove the bindings for all of the class-level declarations.  */
1809  for (shadowed = level->class_shadowed;
1810       shadowed;
1811       shadowed = TREE_CHAIN (shadowed))
1812    pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1813
1814  GNU_xref_end_scope ((HOST_WIDE_INT) class_binding_level,
1815		      (HOST_WIDE_INT) class_binding_level->level_chain,
1816		      class_binding_level->parm_flag,
1817		      class_binding_level->keep);
1818
1819  /* Now, pop out of the binding level which we created up in the
1820     `pushlevel_class' routine.  */
1821#if defined(DEBUG_CP_BINDING_LEVELS)
1822  is_class_level = 1;
1823#endif /* defined(DEBUG_CP_BINDING_LEVELS) */
1824
1825  pop_binding_level ();
1826
1827  return NULL_TREE;
1828}
1829
1830/* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1831   for any names in enclosing classes.  */
1832
1833void
1834clear_identifier_class_values ()
1835{
1836  tree t;
1837
1838  if (!class_binding_level)
1839    return;
1840
1841  for (t = class_binding_level->class_shadowed;
1842       t;
1843       t = TREE_CHAIN (t))
1844    IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1845}
1846
1847/* Returns non-zero if T is a virtual function table.  */
1848
1849int
1850vtable_decl_p (t, data)
1851     tree t;
1852     void *data ATTRIBUTE_UNUSED;
1853{
1854  return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1855}
1856
1857/* Returns non-zero if T is a TYPE_DECL for a type with virtual
1858   functions.  */
1859
1860int
1861vtype_decl_p (t, data)
1862     tree t;
1863     void *data ATTRIBUTE_UNUSED;
1864{
1865  return (TREE_CODE (t) == TYPE_DECL
1866	  && TREE_TYPE (t) != error_mark_node
1867	  && TYPE_LANG_SPECIFIC (TREE_TYPE (t))
1868	  && CLASSTYPE_VSIZE (TREE_TYPE (t)));
1869}
1870
1871/* Returns non-zero if T is a signature table.  */
1872
1873int
1874sigtable_decl_p (t, data)
1875     tree t;
1876     void *data ATTRIBUTE_UNUSED;
1877{
1878  return (TREE_CODE (t) == VAR_DECL
1879	  && TREE_TYPE (t) != error_mark_node
1880	  && IS_SIGNATURE (TREE_TYPE (t)));
1881}
1882
1883/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1884   itself, calling F for each.  The DATA is passed to F as well.  */
1885
1886static int
1887walk_namespaces_r (namespace, f, data)
1888     tree namespace;
1889     walk_namespaces_fn f;
1890     void *data;
1891{
1892  tree current;
1893  int result = 0;
1894
1895  result |= (*f) (namespace, data);
1896
1897  for (current = NAMESPACE_LEVEL (namespace)->names;
1898       current;
1899       current = TREE_CHAIN (current))
1900    {
1901      if (TREE_CODE (current) != NAMESPACE_DECL
1902	  || DECL_NAMESPACE_ALIAS (current))
1903	continue;
1904      if (!DECL_LANG_SPECIFIC (current))
1905	{
1906	  /* Hmm. std. */
1907	  my_friendly_assert (current == std_node, 393);
1908	  continue;
1909	}
1910
1911      /* We found a namespace.  */
1912      result |= walk_namespaces_r (current, f, data);
1913    }
1914
1915  return result;
1916}
1917
1918/* Walk all the namespaces, calling F for each.  The DATA is passed to
1919   F as well.  */
1920
1921int
1922walk_namespaces (f, data)
1923     walk_namespaces_fn f;
1924     void *data;
1925{
1926  return walk_namespaces_r (global_namespace, f, data);
1927}
1928
1929struct walk_globals_data {
1930  walk_globals_pred p;
1931  walk_globals_fn f;
1932  void *data;
1933};
1934
1935/* Walk the global declarations in NAMESPACE.  Whenever one is found
1936   for which P returns non-zero, call F with its address.  If any call
1937   to F returns a non-zero value, return a non-zero value.  */
1938
1939static int
1940walk_globals_r (namespace, data)
1941     tree namespace;
1942     void *data;
1943{
1944  struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1945  walk_globals_pred p = wgd->p;
1946  walk_globals_fn f = wgd->f;
1947  void *d = wgd->data;
1948  tree *t;
1949  int result = 0;
1950
1951  t = &NAMESPACE_LEVEL (namespace)->names;
1952
1953  while (*t)
1954    {
1955      tree glbl = *t;
1956
1957      if ((*p) (glbl, d))
1958	result |= (*f) (t, d);
1959
1960      /* If F changed *T, then *T still points at the next item to
1961	 examine.  */
1962      if (*t == glbl)
1963	t = &TREE_CHAIN (*t);
1964    }
1965
1966  return result;
1967}
1968
1969/* Walk the global declarations.  Whenever one is found for which P
1970   returns non-zero, call F with its address.  If any call to F
1971   returns a non-zero value, return a non-zero value.  */
1972
1973int
1974walk_globals (p, f, data)
1975     walk_globals_pred p;
1976     walk_globals_fn f;
1977     void *data;
1978{
1979  struct walk_globals_data wgd;
1980  wgd.p = p;
1981  wgd.f = f;
1982  wgd.data = data;
1983
1984  return walk_namespaces (walk_globals_r, &wgd);
1985}
1986
1987/* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1988   DATA is non-NULL, this is the last time we will call
1989   wrapup_global_declarations for this NAMESPACE.  */
1990
1991int
1992wrapup_globals_for_namespace (namespace, data)
1993     tree namespace;
1994     void *data;
1995{
1996  tree globals = NAMESPACE_LEVEL (namespace)->names;
1997  int len = list_length (globals);
1998  tree *vec = (tree *) alloca (sizeof (tree) * len);
1999  int i;
2000  int result;
2001  tree decl;
2002  int last_time = (data != 0);
2003
2004  if (last_time && namespace == global_namespace)
2005    /* Let compile_file handle the global namespace.  */
2006    return 0;
2007
2008  /* Process the decls in reverse order--earliest first.
2009     Put them into VEC from back to front, then take out from front.  */
2010
2011  for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
2012    vec[len - i - 1] = decl;
2013
2014  if (last_time)
2015    {
2016      check_global_declarations (vec, len);
2017      return 0;
2018    }
2019
2020  /* Temporarily mark vtables as external.  That prevents
2021     wrapup_global_declarations from writing them out; we must process
2022     them ourselves in finish_vtable_vardecl.  */
2023  for (i = 0; i < len; ++i)
2024    if (vtable_decl_p (vec[i], /*data=*/0) && !DECL_EXTERNAL (vec[i]))
2025      {
2026	DECL_NOT_REALLY_EXTERN (vec[i]) = 1;
2027	DECL_EXTERNAL (vec[i]) = 1;
2028      }
2029
2030  /* Write out any globals that need to be output.  */
2031  result = wrapup_global_declarations (vec, len);
2032
2033  /* Undo the hack to DECL_EXTERNAL above.  */
2034  for (i = 0; i < len; ++i)
2035    if (vtable_decl_p (vec[i], /*data=*/0)
2036	&& DECL_NOT_REALLY_EXTERN (vec[i]))
2037      {
2038	DECL_NOT_REALLY_EXTERN (vec[i]) = 0;
2039	DECL_EXTERNAL (vec[i]) = 0;
2040      }
2041
2042  return result;
2043}
2044
2045
2046/* For debugging.  */
2047static int no_print_functions = 0;
2048static int no_print_builtins = 0;
2049
2050void
2051print_binding_level (lvl)
2052     struct binding_level *lvl;
2053{
2054  tree t;
2055  int i = 0, len;
2056  fprintf (stderr, " blocks=");
2057  fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
2058  fprintf (stderr, " n_incomplete=%d parm_flag=%d keep=%d",
2059	   list_length (lvl->incomplete), lvl->parm_flag, lvl->keep);
2060  if (lvl->tag_transparent)
2061    fprintf (stderr, " tag-transparent");
2062  if (lvl->more_cleanups_ok)
2063    fprintf (stderr, " more-cleanups-ok");
2064  if (lvl->have_cleanups)
2065    fprintf (stderr, " have-cleanups");
2066  fprintf (stderr, "\n");
2067  if (lvl->names)
2068    {
2069      fprintf (stderr, " names:\t");
2070      /* We can probably fit 3 names to a line?  */
2071      for (t = lvl->names; t; t = TREE_CHAIN (t))
2072	{
2073	  if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
2074	    continue;
2075	  if (no_print_builtins
2076	      && (TREE_CODE (t) == TYPE_DECL)
2077	      && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
2078	    continue;
2079
2080	  /* Function decls tend to have longer names.  */
2081	  if (TREE_CODE (t) == FUNCTION_DECL)
2082	    len = 3;
2083	  else
2084	    len = 2;
2085	  i += len;
2086	  if (i > 6)
2087	    {
2088	      fprintf (stderr, "\n\t");
2089	      i = len;
2090	    }
2091	  print_node_brief (stderr, "", t, 0);
2092	  if (t == error_mark_node)
2093	    break;
2094	}
2095      if (i)
2096        fprintf (stderr, "\n");
2097    }
2098  if (lvl->tags)
2099    {
2100      fprintf (stderr, " tags:\t");
2101      i = 0;
2102      for (t = lvl->tags; t; t = TREE_CHAIN (t))
2103	{
2104	  if (TREE_PURPOSE (t) == NULL_TREE)
2105	    len = 3;
2106	  else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2107	    len = 2;
2108	  else
2109	    len = 4;
2110	  i += len;
2111	  if (i > 5)
2112	    {
2113	      fprintf (stderr, "\n\t");
2114	      i = len;
2115	    }
2116	  if (TREE_PURPOSE (t) == NULL_TREE)
2117	    {
2118	      print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
2119	      fprintf (stderr, ">");
2120	    }
2121	  else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
2122	    print_node_brief (stderr, "", TREE_VALUE (t), 0);
2123	  else
2124	    {
2125	      print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
2126	      print_node_brief (stderr, "", TREE_VALUE (t), 0);
2127	      fprintf (stderr, ">");
2128	    }
2129	}
2130      if (i)
2131	fprintf (stderr, "\n");
2132    }
2133  if (lvl->class_shadowed)
2134    {
2135      fprintf (stderr, " class-shadowed:");
2136      for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
2137	{
2138	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2139	}
2140      fprintf (stderr, "\n");
2141    }
2142  if (lvl->type_shadowed)
2143    {
2144      fprintf (stderr, " type-shadowed:");
2145      for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2146        {
2147	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2148        }
2149      fprintf (stderr, "\n");
2150    }
2151}
2152
2153void
2154print_other_binding_stack (stack)
2155     struct binding_level *stack;
2156{
2157  struct binding_level *level;
2158  for (level = stack; level != global_binding_level; level = level->level_chain)
2159    {
2160      fprintf (stderr, "binding level ");
2161      fprintf (stderr, HOST_PTR_PRINTF, level);
2162      fprintf (stderr, "\n");
2163      print_binding_level (level);
2164    }
2165}
2166
2167void
2168print_binding_stack ()
2169{
2170  struct binding_level *b;
2171  fprintf (stderr, "current_binding_level=");
2172  fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2173  fprintf (stderr, "\nclass_binding_level=");
2174  fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2175  fprintf (stderr, "\nglobal_binding_level=");
2176  fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2177  fprintf (stderr, "\n");
2178  if (class_binding_level)
2179    {
2180      for (b = class_binding_level; b; b = b->level_chain)
2181	if (b == current_binding_level)
2182	  break;
2183      if (b)
2184	b = class_binding_level;
2185      else
2186	b = current_binding_level;
2187    }
2188  else
2189    b = current_binding_level;
2190  print_other_binding_stack (b);
2191  fprintf (stderr, "global:\n");
2192  print_binding_level (global_binding_level);
2193}
2194
2195/* Namespace binding access routines: The namespace_bindings field of
2196   the identifier is polymorphic, with three possible values:
2197   NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2198   indicating the BINDING_VALUE of global_namespace. */
2199
2200/* Check whether the a binding for the name to scope is known.
2201   Assumes that the bindings of the name are already a list
2202   of bindings. Returns the binding found, or NULL_TREE. */
2203
2204static tree
2205find_binding (name, scope)
2206     tree name;
2207     tree scope;
2208{
2209  tree iter, prev = NULL_TREE;
2210
2211  scope = ORIGINAL_NAMESPACE (scope);
2212
2213  for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2214       iter = TREE_CHAIN (iter))
2215    {
2216      my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2217      if (BINDING_SCOPE (iter) == scope)
2218	{
2219	  /* Move binding found to the fron of the list, so
2220             subsequent lookups will find it faster. */
2221	  if (prev)
2222	    {
2223	      TREE_CHAIN (prev) = TREE_CHAIN (iter);
2224	      TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2225	      IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2226	    }
2227	  return iter;
2228	}
2229      prev = iter;
2230    }
2231  return NULL_TREE;
2232}
2233
2234/* Always returns a binding for name in scope. If the
2235   namespace_bindings is not a list, convert it to one first.
2236   If no binding is found, make a new one. */
2237
2238tree
2239binding_for_name (name, scope)
2240     tree name;
2241     tree scope;
2242{
2243  tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2244  tree result;
2245
2246  scope = ORIGINAL_NAMESPACE (scope);
2247
2248  if (b && TREE_CODE (b) != CPLUS_BINDING)
2249    {
2250      /* Get rid of optimization for global scope. */
2251      IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2252      BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2253      b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2254    }
2255  if (b && (result = find_binding (name, scope)))
2256    return result;
2257  /* Not found, make a new permanent one. */
2258  push_obstacks (&permanent_obstack, &permanent_obstack);
2259  result = make_node (CPLUS_BINDING);
2260  TREE_CHAIN (result) = b;
2261  IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2262  BINDING_SCOPE (result) = scope;
2263  BINDING_TYPE (result) = NULL_TREE;
2264  BINDING_VALUE (result) = NULL_TREE;
2265  pop_obstacks ();
2266  return result;
2267}
2268
2269/* Return the binding value for name in scope, considering that
2270   namespace_binding may or may not be a list of CPLUS_BINDINGS. */
2271
2272tree
2273namespace_binding (name, scope)
2274     tree name;
2275     tree scope;
2276{
2277  tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2278  if (b == NULL_TREE)
2279    return NULL_TREE;
2280  if (scope == NULL_TREE)
2281    scope = global_namespace;
2282  if (TREE_CODE (b) != CPLUS_BINDING)
2283    return (scope == global_namespace) ? b : NULL_TREE;
2284  name = find_binding (name,scope);
2285  if (name == NULL_TREE)
2286    return name;
2287  return BINDING_VALUE (name);
2288}
2289
2290/* Set the binding value for name in scope. If modifying the binding
2291   of global_namespace is attempted, try to optimize it. */
2292
2293void
2294set_namespace_binding (name, scope, val)
2295     tree name;
2296     tree scope;
2297     tree val;
2298{
2299  tree b;
2300
2301  if (scope == NULL_TREE)
2302    scope = global_namespace;
2303
2304  if (scope == global_namespace)
2305    {
2306      b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2307      if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2308	{
2309	  IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2310	  return;
2311	}
2312    }
2313  b = binding_for_name (name, scope);
2314  BINDING_VALUE (b) = val;
2315}
2316
2317/* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2318   select a name that is unique to this compilation unit.  */
2319
2320void
2321push_namespace (name)
2322     tree name;
2323{
2324  tree d = NULL_TREE;
2325  int need_new = 1;
2326  int implicit_use = 0;
2327  int global = 0;
2328  if (!global_namespace)
2329    {
2330      /* This must be ::. */
2331      my_friendly_assert (name == get_identifier ("::"), 377);
2332      global = 1;
2333    }
2334  else if (!name)
2335    {
2336      /* The name of anonymous namespace is unique for the translation
2337         unit.  */
2338      if (!anonymous_namespace_name)
2339        anonymous_namespace_name = get_file_function_name ('N');
2340      name = anonymous_namespace_name;
2341      d = IDENTIFIER_NAMESPACE_VALUE (name);
2342      if (d)
2343        /* Reopening anonymous namespace.  */
2344        need_new = 0;
2345      implicit_use = 1;
2346    }
2347  else if (current_namespace == global_namespace
2348	   && name == DECL_NAME (std_node))
2349    {
2350      in_std++;
2351      return;
2352    }
2353  else
2354    {
2355      /* Check whether this is an extended namespace definition. */
2356      d = IDENTIFIER_NAMESPACE_VALUE (name);
2357      if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2358        {
2359          need_new = 0;
2360          if (DECL_NAMESPACE_ALIAS (d))
2361            {
2362              cp_error ("namespace alias `%D' not allowed here, assuming `%D'",
2363                        d, DECL_NAMESPACE_ALIAS (d));
2364              d = DECL_NAMESPACE_ALIAS (d);
2365            }
2366        }
2367    }
2368
2369  if (need_new)
2370    {
2371      /* Make a new namespace, binding the name to it. */
2372      d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2373      /* The global namespace is not pushed, and the global binding
2374	 level is set elsewhere.  */
2375      if (!global)
2376	{
2377	  d = pushdecl (d);
2378	  pushlevel (0);
2379	  declare_namespace_level ();
2380	  NAMESPACE_LEVEL (d) = current_binding_level;
2381	}
2382    }
2383  else
2384    resume_binding_level (NAMESPACE_LEVEL (d));
2385
2386  if (implicit_use)
2387    do_using_directive (d);
2388  /* Enter the name space. */
2389  current_namespace = d;
2390}
2391
2392/* Pop from the scope of the current namespace.  */
2393
2394void
2395pop_namespace ()
2396{
2397  if (current_namespace == global_namespace)
2398    {
2399      my_friendly_assert (in_std>0, 980421);
2400      in_std--;
2401      return;
2402    }
2403  current_namespace = CP_DECL_CONTEXT (current_namespace);
2404  /* The binding level is not popped, as it might be re-opened later.  */
2405  suspend_binding_level ();
2406}
2407
2408
2409/* Subroutines for reverting temporarily to top-level for instantiation
2410   of templates and such.  We actually need to clear out the class- and
2411   local-value slots of all identifiers, so that only the global values
2412   are at all visible.  Simply setting current_binding_level to the global
2413   scope isn't enough, because more binding levels may be pushed.  */
2414struct saved_scope {
2415  struct binding_level *old_binding_level;
2416  tree old_bindings;
2417  tree old_namespace;
2418  struct saved_scope *prev;
2419  tree class_name, class_type;
2420  tree access_specifier;
2421  tree function_decl;
2422  struct binding_level *class_bindings;
2423  tree *lang_base, *lang_stack, lang_name;
2424  int lang_stacksize;
2425  int minimal_parse_mode;
2426  tree last_function_parms;
2427  tree template_parms;
2428  HOST_WIDE_INT processing_template_decl;
2429  tree previous_class_type, previous_class_values;
2430  int processing_specialization;
2431  int processing_explicit_instantiation;
2432  char *class_cache_firstobj;
2433};
2434static struct saved_scope *current_saved_scope;
2435
2436/* A chain of the binding vecs created by store_bindings.  We create a
2437   whole bunch of these during compilation, on permanent_obstack, so we
2438   can't just throw them away.  */
2439static tree free_binding_vecs;
2440
2441static tree
2442store_bindings (names, old_bindings)
2443     tree names, old_bindings;
2444{
2445  tree t;
2446  for (t = names; t; t = TREE_CHAIN (t))
2447    {
2448      tree binding, t1, id;
2449
2450      if (TREE_CODE (t) == TREE_LIST)
2451	id = TREE_PURPOSE (t);
2452      else
2453	id = DECL_NAME (t);
2454
2455      if (!id
2456	  /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2457	     we have no IDENTIFIER_BINDING if we have left the class
2458	     scope, but cached the class-level declarations.  */
2459	  || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2460	continue;
2461
2462      for (t1 = old_bindings; t1; t1 = TREE_CHAIN (t1))
2463	if (TREE_VEC_ELT (t1, 0) == id)
2464	  goto skip_it;
2465
2466      if (free_binding_vecs)
2467	{
2468	  binding = free_binding_vecs;
2469	  free_binding_vecs = TREE_CHAIN (free_binding_vecs);
2470	}
2471      else
2472	binding = make_tree_vec (4);
2473
2474      if (id)
2475	{
2476	  my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2477	  TREE_VEC_ELT (binding, 0) = id;
2478	  TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2479	  TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2480	  TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2481	  IDENTIFIER_BINDING (id) = NULL_TREE;
2482	  IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2483	}
2484      TREE_CHAIN (binding) = old_bindings;
2485      old_bindings = binding;
2486    skip_it:
2487      ;
2488    }
2489  return old_bindings;
2490}
2491
2492void
2493maybe_push_to_top_level (pseudo)
2494     int pseudo;
2495{
2496  extern int current_lang_stacksize;
2497  struct saved_scope *s
2498    = (struct saved_scope *) xmalloc (sizeof (struct saved_scope));
2499  struct binding_level *b = current_binding_level;
2500  tree old_bindings = NULL_TREE;
2501
2502  push_cp_function_context (NULL_TREE);
2503
2504  if (previous_class_type)
2505    old_bindings = store_bindings (previous_class_values, old_bindings);
2506
2507  /* Have to include global_binding_level, because class-level decls
2508     aren't listed anywhere useful.  */
2509  for (; b; b = b->level_chain)
2510    {
2511      tree t;
2512
2513      /* Template IDs are inserted into the global level. If they were
2514	 inserted into namespace level, finish_file wouldn't find them
2515	 when doing pending instantiations. Therefore, don't stop at
2516	 namespace level, but continue until :: .  */
2517      if (b == global_binding_level || (pseudo && b->pseudo_global))
2518	break;
2519
2520      old_bindings = store_bindings (b->names, old_bindings);
2521      /* We also need to check class_shadowed to save class-level type
2522	 bindings, since pushclass doesn't fill in b->names.  */
2523      if (b->parm_flag == 2)
2524	old_bindings = store_bindings (b->class_shadowed, old_bindings);
2525
2526      /* Unwind type-value slots back to top level.  */
2527      for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2528	SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2529    }
2530
2531  s->old_binding_level = current_binding_level;
2532  current_binding_level = b;
2533
2534  s->old_namespace = current_namespace;
2535  s->class_name = current_class_name;
2536  s->class_type = current_class_type;
2537  s->access_specifier = current_access_specifier;
2538  s->function_decl = current_function_decl;
2539  s->class_bindings = class_binding_level;
2540  s->lang_stack = current_lang_stack;
2541  s->lang_base = current_lang_base;
2542  s->lang_stacksize = current_lang_stacksize;
2543  s->lang_name = current_lang_name;
2544  s->minimal_parse_mode = minimal_parse_mode;
2545  s->last_function_parms = last_function_parms;
2546  s->template_parms = current_template_parms;
2547  s->processing_template_decl = processing_template_decl;
2548  s->previous_class_type = previous_class_type;
2549  s->previous_class_values = previous_class_values;
2550  s->class_cache_firstobj = class_cache_firstobj;
2551  s->processing_specialization = processing_specialization;
2552  s->processing_explicit_instantiation = processing_explicit_instantiation;
2553
2554  current_class_name = current_class_type = NULL_TREE;
2555  current_function_decl = NULL_TREE;
2556  class_binding_level = (struct binding_level *)0;
2557  current_lang_stacksize = 10;
2558  current_lang_stack = current_lang_base
2559    = (tree *) xmalloc (current_lang_stacksize * sizeof (tree));
2560  current_lang_name = lang_name_cplusplus;
2561  strict_prototype = strict_prototypes_lang_cplusplus;
2562  named_labels = NULL_TREE;
2563  shadowed_labels = NULL_TREE;
2564  minimal_parse_mode = 0;
2565  previous_class_type = previous_class_values = NULL_TREE;
2566  class_cache_firstobj = 0;
2567  processing_specialization = 0;
2568  processing_explicit_instantiation = 0;
2569  current_template_parms = NULL_TREE;
2570  processing_template_decl = 0;
2571  current_namespace = global_namespace;
2572
2573  s->prev = current_saved_scope;
2574  s->old_bindings = old_bindings;
2575  current_saved_scope = s;
2576
2577  push_obstacks (&permanent_obstack, &permanent_obstack);
2578}
2579
2580void
2581push_to_top_level ()
2582{
2583  maybe_push_to_top_level (0);
2584}
2585
2586void
2587pop_from_top_level ()
2588{
2589  extern int current_lang_stacksize;
2590  struct saved_scope *s = current_saved_scope;
2591  tree t;
2592
2593  /* Clear out class-level bindings cache.  */
2594  if (previous_class_type)
2595    invalidate_class_lookup_cache ();
2596
2597  pop_obstacks ();
2598
2599  current_binding_level = s->old_binding_level;
2600  current_saved_scope = s->prev;
2601  for (t = s->old_bindings; t; )
2602    {
2603      tree save = t;
2604      tree id = TREE_VEC_ELT (t, 0);
2605      if (id)
2606	{
2607	  SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2608	  IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2609	  IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2610	}
2611      t = TREE_CHAIN (t);
2612      TREE_CHAIN (save) = free_binding_vecs;
2613      free_binding_vecs = save;
2614    }
2615  current_namespace = s->old_namespace;
2616  current_class_name = s->class_name;
2617  current_class_type = s->class_type;
2618  current_access_specifier = s->access_specifier;
2619  current_function_decl = s->function_decl;
2620  class_binding_level = s->class_bindings;
2621  free (current_lang_base);
2622  current_lang_base = s->lang_base;
2623  current_lang_stack = s->lang_stack;
2624  current_lang_name = s->lang_name;
2625  current_lang_stacksize = s->lang_stacksize;
2626  if (current_lang_name == lang_name_cplusplus)
2627    strict_prototype = strict_prototypes_lang_cplusplus;
2628  else if (current_lang_name == lang_name_c)
2629    strict_prototype = strict_prototypes_lang_c;
2630  minimal_parse_mode = s->minimal_parse_mode;
2631  last_function_parms = s->last_function_parms;
2632  current_template_parms = s->template_parms;
2633  processing_template_decl = s->processing_template_decl;
2634  previous_class_type = s->previous_class_type;
2635  previous_class_values = s->previous_class_values;
2636  processing_specialization = s->processing_specialization;
2637  processing_explicit_instantiation = s->processing_explicit_instantiation;
2638  class_cache_firstobj = s->class_cache_firstobj;
2639
2640  free (s);
2641
2642  pop_cp_function_context (NULL_TREE);
2643}
2644
2645/* Push a definition of struct, union or enum tag "name".
2646   into binding_level "b".   "type" should be the type node,
2647   We assume that the tag "name" is not already defined.
2648
2649   Note that the definition may really be just a forward reference.
2650   In that case, the TYPE_SIZE will be a NULL_TREE.
2651
2652   C++ gratuitously puts all these tags in the name space.  */
2653
2654/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2655   record the shadowed value for this binding contour.  TYPE is
2656   the type that ID maps to.  */
2657
2658static void
2659set_identifier_type_value_with_scope (id, type, b)
2660     tree id;
2661     tree type;
2662     struct binding_level *b;
2663{
2664  if (!b->namespace_p)
2665    {
2666      /* Shadow the marker, not the real thing, so that the marker
2667	 gets restored later. */
2668      tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2669      b->type_shadowed
2670	= tree_cons (id, old_type_value, b->type_shadowed);
2671    }
2672  else
2673    {
2674      tree binding = binding_for_name (id, current_namespace);
2675      BINDING_TYPE (binding) = type;
2676      /* Store marker instead of real type. */
2677      type = global_type_node;
2678    }
2679  SET_IDENTIFIER_TYPE_VALUE (id, type);
2680}
2681
2682/* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2683
2684void
2685set_identifier_type_value (id, type)
2686     tree id;
2687     tree type;
2688{
2689  set_identifier_type_value_with_scope (id, type, current_binding_level);
2690}
2691
2692/* Return the type associated with id. */
2693
2694tree
2695identifier_type_value (id)
2696     tree id;
2697{
2698  /* There is no type with that name, anywhere. */
2699  if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2700    return NULL_TREE;
2701  /* This is not the type marker, but the real thing. */
2702  if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2703    return REAL_IDENTIFIER_TYPE_VALUE (id);
2704  /* Have to search for it. It must be on the global level, now.
2705     Ask lookup_name not to return non-types. */
2706  id = lookup_name_real (id, 2, 1, 0);
2707  if (id)
2708    return TREE_TYPE (id);
2709  return NULL_TREE;
2710}
2711
2712/* Pop off extraneous binding levels left over due to syntax errors.
2713
2714   We don't pop past namespaces, as they might be valid.  */
2715
2716void
2717pop_everything ()
2718{
2719#ifdef DEBUG_CP_BINDING_LEVELS
2720  fprintf (stderr, "XXX entering pop_everything ()\n");
2721#endif
2722  while (!toplevel_bindings_p ())
2723    {
2724      if (current_binding_level->parm_flag == 2)
2725	pop_nested_class ();
2726      else
2727	poplevel (0, 0, 0);
2728    }
2729#ifdef DEBUG_CP_BINDING_LEVELS
2730  fprintf (stderr, "XXX leaving pop_everything ()\n");
2731#endif
2732}
2733
2734/* The type TYPE is being declared.  If it is a class template, or a
2735   specialization of a class template, do any processing required and
2736   perform error-checking.  If IS_FRIEND is non-zero, this TYPE is
2737   being declared a friend.  B is the binding level at which this TYPE
2738   should be bound.
2739
2740   Returns the TYPE_DECL for TYPE, which may have been altered by this
2741   processing.  */
2742
2743static tree
2744maybe_process_template_type_declaration (type, globalize, b)
2745     tree type;
2746     int globalize;
2747     struct binding_level* b;
2748{
2749  tree decl = TYPE_NAME (type);
2750
2751  if (processing_template_parmlist)
2752    /* You can't declare a new template type in a template parameter
2753       list.  But, you can declare a non-template type:
2754
2755         template <class A*> struct S;
2756
2757       is a forward-declaration of `A'.  */
2758    ;
2759  else
2760    {
2761      maybe_check_template_type (type);
2762
2763      my_friendly_assert (IS_AGGR_TYPE (type)
2764			  || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2765
2766
2767      if (/* If !GLOBALIZE then we are looking at a definition.
2768	     It may not be a primary template.  (For example, in:
2769
2770	       template <class T>
2771	       struct S1 { class S2 {}; }
2772
2773	     we have to push_template_decl for S2.)  */
2774	  (processing_template_decl && !globalize)
2775	  /* If we are declaring a friend template class, we will
2776	     have GLOBALIZE set, since something like:
2777
2778	       template <class T>
2779	       struct S1 {
2780		 template <class U>
2781		 friend class S2;
2782	       };
2783
2784	     declares S2 to be at global scope.  */
2785	  || PROCESSING_REAL_TEMPLATE_DECL_P ())
2786	{
2787	  /* This may change after the call to
2788	     push_template_decl_real, but we want the original value.  */
2789	  tree name = DECL_NAME (decl);
2790
2791	  decl = push_template_decl_real (decl, globalize);
2792	  /* If the current binding level is the binding level for the
2793	     template parameters (see the comment in
2794	     begin_template_parm_list) and the enclosing level is a class
2795	     scope, and we're not looking at a friend, push the
2796	     declaration of the member class into the class scope.  In the
2797	     friend case, push_template_decl will already have put the
2798	     friend into global scope, if appropriate.  */
2799	  if (TREE_CODE (type) != ENUMERAL_TYPE
2800	      && !globalize && b->pseudo_global
2801	      && b->level_chain->parm_flag == 2)
2802	    {
2803	      finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2804	      /* Put this tag on the list of tags for the class, since
2805		 that won't happen below because B is not the class
2806		 binding level, but is instead the pseudo-global level.  */
2807	      b->level_chain->tags =
2808		saveable_tree_cons (name, type, b->level_chain->tags);
2809	      if (TYPE_SIZE (current_class_type) == NULL_TREE)
2810		CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2811	    }
2812	}
2813    }
2814
2815  return decl;
2816}
2817
2818/* Push a tag name NAME for struct/class/union/enum type TYPE.
2819   Normally put it into the inner-most non-tag-transparent scope,
2820   but if GLOBALIZE is true, put it in the inner-most non-class scope.
2821   The latter is needed for implicit declarations.  */
2822
2823void
2824pushtag (name, type, globalize)
2825     tree name, type;
2826     int globalize;
2827{
2828  register struct binding_level *b;
2829
2830  b = current_binding_level;
2831  while (b->tag_transparent
2832	 || (globalize && b->parm_flag == 2))
2833    b = b->level_chain;
2834
2835  if (toplevel_bindings_p ())
2836    b->tags = perm_tree_cons (name, type, b->tags);
2837  else
2838    b->tags = saveable_tree_cons (name, type, b->tags);
2839
2840  if (name)
2841    {
2842      /* Do C++ gratuitous typedefing.  */
2843      if (IDENTIFIER_TYPE_VALUE (name) != type)
2844        {
2845          register tree d = NULL_TREE;
2846	  int newdecl = 0, in_class = 0;
2847	  tree context;
2848	  tree c_decl = NULL_TREE;
2849
2850	  context = type ? TYPE_CONTEXT (type) : NULL_TREE;
2851	  if (! context)
2852	    {
2853	      tree cs = current_scope ();
2854
2855	      if (! globalize)
2856		context = cs;
2857	      else if (cs != NULL_TREE
2858		       && TREE_CODE_CLASS (TREE_CODE (cs)) == 't')
2859		/* When declaring a friend class of a local class, we want
2860		   to inject the newly named class into the scope
2861		   containing the local class, not the namespace scope.  */
2862		context = hack_decl_function_context (get_type_decl (cs));
2863	    }
2864	  if (context)
2865	    c_decl = TREE_CODE (context) == FUNCTION_DECL
2866	      ? context : TYPE_MAIN_DECL (context);
2867
2868	  if (!context)
2869	    context = current_namespace;
2870
2871	  if ((b->pseudo_global && b->level_chain->parm_flag == 2)
2872	      || b->parm_flag == 2)
2873	    in_class = 1;
2874	  else
2875	    d = lookup_nested_type (type, c_decl);
2876
2877	  if (d == NULL_TREE)
2878	    {
2879	      newdecl = 1;
2880	      d = build_decl (TYPE_DECL, name, type);
2881	      if (current_lang_name == lang_name_java)
2882		TYPE_FOR_JAVA (type) = 1;
2883	      SET_DECL_ARTIFICIAL (d);
2884	      if (! in_class)
2885		set_identifier_type_value_with_scope (name, type, b);
2886	    }
2887	  else
2888	    d = TYPE_MAIN_DECL (d);
2889
2890	  TYPE_NAME (type) = d;
2891	  DECL_CONTEXT (d) = FROB_CONTEXT (context);
2892
2893	  d = maybe_process_template_type_declaration (type,
2894						       globalize, b);
2895
2896	  if (b->parm_flag == 2)
2897	    {
2898	      if (newdecl && !PROCESSING_REAL_TEMPLATE_DECL_P ())
2899		/* Put this TYPE_DECL on the TYPE_FIELDS list for the
2900		   class.  But if it's a member template class, we
2901		   want the TEMPLATE_DECL, not the TYPE_DECL, so this
2902		   is done later.  */
2903		finish_member_declaration (d);
2904	      else
2905		pushdecl_class_level (d);
2906	    }
2907	  else
2908	    d = pushdecl_with_scope (d, b);
2909
2910	  if (newdecl)
2911	    {
2912	      if (ANON_AGGRNAME_P (name))
2913		DECL_IGNORED_P (d) = 1;
2914
2915	      TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2916	      DECL_ASSEMBLER_NAME (d) = DECL_NAME (d);
2917	      if (!uses_template_parms (type))
2918		DECL_ASSEMBLER_NAME (d)
2919		  = get_identifier (build_overload_name (type, 1, 1));
2920	    }
2921        }
2922      if (b->parm_flag == 2)
2923	{
2924	  if (TYPE_SIZE (current_class_type) == NULL_TREE)
2925	    CLASSTYPE_TAGS (current_class_type) = b->tags;
2926	}
2927    }
2928
2929  if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2930    /* Use the canonical TYPE_DECL for this node.  */
2931    TYPE_STUB_DECL (type) = TYPE_NAME (type);
2932  else
2933    {
2934      /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2935	 will be the tagged type we just added to the current
2936	 binding level.  This fake NULL-named TYPE_DECL node helps
2937	 dwarfout.c to know when it needs to output a
2938	 representation of a tagged type, and it also gives us a
2939	 convenient place to record the "scope start" address for
2940	 the tagged type.  */
2941
2942      tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2943      TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2944    }
2945}
2946
2947/* Counter used to create anonymous type names.  */
2948
2949static int anon_cnt = 0;
2950
2951/* Return an IDENTIFIER which can be used as a name for
2952   anonymous structs and unions.  */
2953
2954tree
2955make_anon_name ()
2956{
2957  char buf[32];
2958
2959  sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2960  return get_identifier (buf);
2961}
2962
2963/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2964   This keeps dbxout from getting confused.  */
2965
2966void
2967clear_anon_tags ()
2968{
2969  register struct binding_level *b;
2970  register tree tags;
2971  static int last_cnt = 0;
2972
2973  /* Fast out if no new anon names were declared.  */
2974  if (last_cnt == anon_cnt)
2975    return;
2976
2977  b = current_binding_level;
2978  while (b->tag_transparent)
2979    b = b->level_chain;
2980  tags = b->tags;
2981  while (tags)
2982    {
2983      /* A NULL purpose means we have already processed all tags
2984	 from here to the end of the list.  */
2985      if (TREE_PURPOSE (tags) == NULL_TREE)
2986	break;
2987      if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2988	TREE_PURPOSE (tags) = NULL_TREE;
2989      tags = TREE_CHAIN (tags);
2990    }
2991  last_cnt = anon_cnt;
2992}
2993
2994/* Subroutine of duplicate_decls: return truthvalue of whether
2995   or not types of these decls match.
2996
2997   For C++, we must compare the parameter list so that `int' can match
2998   `int&' in a parameter position, but `int&' is not confused with
2999   `const int&'.  */
3000
3001int
3002decls_match (newdecl, olddecl)
3003     tree newdecl, olddecl;
3004{
3005  int types_match;
3006
3007  if (newdecl == olddecl)
3008    return 1;
3009
3010  if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
3011    /* If the two DECLs are not even the same kind of thing, we're not
3012       interested in their types.  */
3013    return 0;
3014
3015  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3016    {
3017      tree f1 = TREE_TYPE (newdecl);
3018      tree f2 = TREE_TYPE (olddecl);
3019      tree p1 = TYPE_ARG_TYPES (f1);
3020      tree p2 = TYPE_ARG_TYPES (f2);
3021
3022      if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl)
3023	  && ! (DECL_LANGUAGE (newdecl) == lang_c
3024		&& DECL_LANGUAGE (olddecl) == lang_c))
3025	return 0;
3026
3027      /* When we parse a static member function definition,
3028	 we put together a FUNCTION_DECL which thinks its type
3029	 is METHOD_TYPE.  Change that to FUNCTION_TYPE, and
3030	 proceed.  */
3031      if (TREE_CODE (f1) == METHOD_TYPE && DECL_STATIC_FUNCTION_P (olddecl))
3032	revert_static_member_fn (&newdecl, &f1, &p1);
3033      else if (TREE_CODE (f2) == METHOD_TYPE
3034	       && DECL_STATIC_FUNCTION_P (newdecl))
3035	revert_static_member_fn (&olddecl, &f2, &p2);
3036
3037      /* Here we must take care of the case where new default
3038	 parameters are specified.  Also, warn if an old
3039	 declaration becomes ambiguous because default
3040	 parameters may cause the two to be ambiguous.  */
3041      if (TREE_CODE (f1) != TREE_CODE (f2))
3042	{
3043	  if (TREE_CODE (f1) == OFFSET_TYPE)
3044	    cp_compiler_error ("`%D' redeclared as member function", newdecl);
3045	  else
3046	    cp_compiler_error ("`%D' redeclared as non-member function", newdecl);
3047	  return 0;
3048	}
3049
3050      if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
3051	{
3052	  if (! strict_prototypes_lang_c && DECL_LANGUAGE (olddecl) == lang_c
3053	      && p2 == NULL_TREE)
3054	    {
3055	      types_match = self_promoting_args_p (p1);
3056	      if (p1 == void_list_node)
3057		TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3058	    }
3059	  else if (!strict_prototypes_lang_c && DECL_LANGUAGE (olddecl)==lang_c
3060		   && DECL_LANGUAGE (newdecl) == lang_c && p1 == NULL_TREE)
3061	    {
3062	      types_match = self_promoting_args_p (p2);
3063	      TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3064	    }
3065	  else
3066	    types_match = compparms (p1, p2);
3067	}
3068      else
3069	types_match = 0;
3070    }
3071  else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3072    {
3073      if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3074				DECL_TEMPLATE_PARMS (olddecl)))
3075	return 0;
3076
3077      if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3078	types_match = 1;
3079      else
3080	types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
3081				   DECL_TEMPLATE_RESULT (newdecl));
3082    }
3083  else
3084    {
3085      if (TREE_TYPE (newdecl) == error_mark_node)
3086	types_match = TREE_TYPE (olddecl) == error_mark_node;
3087      else if (TREE_TYPE (olddecl) == NULL_TREE)
3088	types_match = TREE_TYPE (newdecl) == NULL_TREE;
3089      else if (TREE_TYPE (newdecl) == NULL_TREE)
3090	types_match = 0;
3091      else
3092	types_match = comptypes (TREE_TYPE (newdecl),
3093				 TREE_TYPE (olddecl),
3094				 COMPARE_REDECLARATION);
3095    }
3096
3097  return types_match;
3098}
3099
3100/* If NEWDECL is `static' and an `extern' was seen previously,
3101   warn about it.  (OLDDECL may be NULL_TREE; NAME contains
3102   information about previous usage as an `extern'.)
3103
3104   Note that this does not apply to the C++ case of declaring
3105   a variable `extern const' and then later `const'.
3106
3107   Don't complain about built-in functions, since they are beyond
3108   the user's control.  */
3109
3110static void
3111warn_extern_redeclared_static (newdecl, olddecl)
3112     tree newdecl, olddecl;
3113{
3114  tree name;
3115
3116  static const char *explicit_extern_static_warning
3117    = "`%D' was declared `extern' and later `static'";
3118  static const char *implicit_extern_static_warning
3119    = "`%D' was declared implicitly `extern' and later `static'";
3120
3121  if (TREE_CODE (newdecl) == TYPE_DECL)
3122    return;
3123
3124  name = DECL_ASSEMBLER_NAME (newdecl);
3125  if (TREE_PUBLIC (name) && DECL_THIS_STATIC (newdecl))
3126    {
3127      /* It's okay to redeclare an ANSI built-in function as static,
3128	 or to declare a non-ANSI built-in function as anything.  */
3129      if (! (TREE_CODE (newdecl) == FUNCTION_DECL
3130	     && olddecl != NULL_TREE
3131	     && TREE_CODE (olddecl) == FUNCTION_DECL
3132	     && (DECL_BUILT_IN (olddecl)
3133		 || DECL_BUILT_IN_NONANSI (olddecl))))
3134	{
3135	  cp_pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
3136		      ? implicit_extern_static_warning
3137		      : explicit_extern_static_warning, newdecl);
3138	  if (olddecl != NULL_TREE)
3139	    cp_pedwarn_at ("previous declaration of `%D'", olddecl);
3140	}
3141    }
3142}
3143
3144/* Handle when a new declaration NEWDECL has the same name as an old
3145   one OLDDECL in the same binding contour.  Prints an error message
3146   if appropriate.
3147
3148   If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
3149   Otherwise, return 0.  */
3150
3151int
3152duplicate_decls (newdecl, olddecl)
3153     tree newdecl, olddecl;
3154{
3155  extern struct obstack permanent_obstack;
3156  unsigned olddecl_uid = DECL_UID (olddecl);
3157  int olddecl_friend = 0, types_match = 0;
3158  int new_defines_function = 0;
3159
3160  if (newdecl == olddecl)
3161    return 1;
3162
3163  types_match = decls_match (newdecl, olddecl);
3164
3165  /* If either the type of the new decl or the type of the old decl is an
3166     error_mark_node, then that implies that we have already issued an
3167     error (earlier) for some bogus type specification, and in that case,
3168     it is rather pointless to harass the user with yet more error message
3169     about the same declaration, so just pretend the types match here.  */
3170  if (TREE_TYPE (newdecl) == error_mark_node
3171      || TREE_TYPE (olddecl) == error_mark_node)
3172    types_match = 1;
3173
3174  /* Check for redeclaration and other discrepancies. */
3175  if (TREE_CODE (olddecl) == FUNCTION_DECL
3176      && DECL_ARTIFICIAL (olddecl)
3177      && (DECL_BUILT_IN (olddecl) || DECL_BUILT_IN_NONANSI (olddecl)))
3178    {
3179      /* If you declare a built-in or predefined function name as static,
3180	 the old definition is overridden, but optionally warn this was a
3181	 bad choice of name.  Ditto for overloads.  */
3182      if (! TREE_PUBLIC (newdecl)
3183	  || (TREE_CODE (newdecl) == FUNCTION_DECL
3184	      && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl)))
3185	{
3186	  if (warn_shadow)
3187	    cp_warning ("shadowing %s function `%#D'",
3188			DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3189			olddecl);
3190	  /* Discard the old built-in function.  */
3191	  return 0;
3192	}
3193      else if (! types_match)
3194	{
3195	  if (TREE_CODE (newdecl) != FUNCTION_DECL)
3196	    {
3197	      /* If the built-in is not ansi, then programs can override
3198		 it even globally without an error.  */
3199	      if (! DECL_BUILT_IN (olddecl))
3200		cp_warning ("library function `%#D' redeclared as non-function `%#D'",
3201			    olddecl, newdecl);
3202	      else
3203		{
3204		  cp_error ("declaration of `%#D'", newdecl);
3205		  cp_error ("conflicts with built-in declaration `%#D'",
3206			    olddecl);
3207		}
3208	      return 0;
3209	    }
3210
3211	  cp_warning ("declaration of `%#D'", newdecl);
3212	  cp_warning ("conflicts with built-in declaration `%#D'",
3213		      olddecl);
3214	}
3215    }
3216  else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3217    {
3218      if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3219	   && TREE_CODE (newdecl) != TYPE_DECL
3220	   && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3221		 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3222	  || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3223	      && TREE_CODE (olddecl) != TYPE_DECL
3224	      && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3225		    && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3226			== TYPE_DECL))))
3227	{
3228	  /* We do nothing special here, because C++ does such nasty
3229	     things with TYPE_DECLs.  Instead, just let the TYPE_DECL
3230	     get shadowed, and know that if we need to find a TYPE_DECL
3231	     for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3232	     slot of the identifier.  */
3233	  return 0;
3234	}
3235
3236      if ((TREE_CODE (newdecl) == FUNCTION_DECL
3237	   && DECL_FUNCTION_TEMPLATE_P (olddecl))
3238	  || (TREE_CODE (olddecl) == FUNCTION_DECL
3239	      && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3240	return 0;
3241
3242      cp_error ("`%#D' redeclared as different kind of symbol", newdecl);
3243      if (TREE_CODE (olddecl) == TREE_LIST)
3244	olddecl = TREE_VALUE (olddecl);
3245      cp_error_at ("previous declaration of `%#D'", olddecl);
3246
3247      /* New decl is completely inconsistent with the old one =>
3248	 tell caller to replace the old one.  */
3249
3250      return 0;
3251    }
3252  else if (!types_match)
3253    {
3254      if (DECL_REAL_CONTEXT (newdecl) != DECL_REAL_CONTEXT (olddecl))
3255	/* These are certainly not duplicate declarations; they're
3256	   from different scopes.  */
3257	return 0;
3258
3259      if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3260	{
3261	  /* The name of a class template may not be declared to refer to
3262	     any other template, class, function, object, namespace, value,
3263	     or type in the same scope.  */
3264	  if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3265	      || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3266	    {
3267	      cp_error ("declaration of template `%#D'", newdecl);
3268	      cp_error_at ("conflicts with previous declaration `%#D'",
3269			   olddecl);
3270	    }
3271	  else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3272		   && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3273		   && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3274				 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3275		   && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3276					   DECL_TEMPLATE_PARMS (olddecl)))
3277	    {
3278	      cp_error ("new declaration `%#D'", newdecl);
3279	      cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3280	    }
3281	  return 0;
3282	}
3283      if (TREE_CODE (newdecl) == FUNCTION_DECL)
3284	{
3285	  if (DECL_LANGUAGE (newdecl) == lang_c
3286	      && DECL_LANGUAGE (olddecl) == lang_c)
3287	    {
3288	      cp_error ("declaration of C function `%#D' conflicts with",
3289			newdecl);
3290	      cp_error_at ("previous declaration `%#D' here", olddecl);
3291	    }
3292	  else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3293			      TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3294	    {
3295	      cp_error ("new declaration `%#D'", newdecl);
3296	      cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3297	    }
3298	  else
3299	    return 0;
3300	}
3301
3302      /* Already complained about this, so don't do so again.  */
3303      else if (current_class_type == NULL_TREE
3304	  || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3305	{
3306	  cp_error ("conflicting types for `%#D'", newdecl);
3307	  cp_error_at ("previous declaration as `%#D'", olddecl);
3308	}
3309    }
3310  else if (TREE_CODE (newdecl) == FUNCTION_DECL
3311	    && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3312		 && (!DECL_TEMPLATE_INFO (newdecl)
3313		     || (DECL_TI_TEMPLATE (newdecl)
3314			 != DECL_TI_TEMPLATE (olddecl))))
3315		|| (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3316		    && (!DECL_TEMPLATE_INFO (olddecl)
3317			|| (DECL_TI_TEMPLATE (olddecl)
3318			    != DECL_TI_TEMPLATE (newdecl))))))
3319    /* It's OK to have a template specialization and a non-template
3320       with the same type, or to have specializations of two
3321       different templates with the same type.  Note that if one is a
3322       specialization, and the other is an instantiation of the same
3323       template, that we do not exit at this point.  That situation
3324       can occur if we instantiate a template class, and then
3325       specialize one of its methods.  This situation is legal, but
3326       the declarations must be merged in the usual way.  */
3327    return 0;
3328  else if (TREE_CODE (newdecl) == FUNCTION_DECL
3329	   && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3330		&& !DECL_USE_TEMPLATE (newdecl))
3331	       || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3332		   && !DECL_USE_TEMPLATE (olddecl))))
3333    /* One of the declarations is a template instantiation, and the
3334       other is not a template at all.  That's OK.  */
3335    return 0;
3336  else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3337           && DECL_NAMESPACE_ALIAS (newdecl)
3338           && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3339    /* Redeclaration of namespace alias, ignore it. */
3340    return 1;
3341  else
3342    {
3343      const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3344      if (errmsg)
3345	{
3346	  cp_error (errmsg, newdecl);
3347	  if (DECL_NAME (olddecl) != NULL_TREE)
3348	    cp_error_at ((DECL_INITIAL (olddecl)
3349			  && namespace_bindings_p ())
3350			 ? "`%#D' previously defined here"
3351			 : "`%#D' previously declared here", olddecl);
3352	}
3353      else if (TREE_CODE (olddecl) == FUNCTION_DECL
3354	       && DECL_INITIAL (olddecl) != NULL_TREE
3355	       && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3356	       && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3357	{
3358	  /* Prototype decl follows defn w/o prototype.  */
3359	  cp_warning_at ("prototype for `%#D'", newdecl);
3360	  cp_warning_at ("follows non-prototype definition here", olddecl);
3361	}
3362      else if (TREE_CODE (olddecl) == FUNCTION_DECL
3363	       && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3364	{
3365	  /* extern "C" int foo ();
3366	     int foo () { bar (); }
3367	     is OK.  */
3368	  if (current_lang_stack == current_lang_base)
3369	    DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3370	  else
3371	    {
3372	      cp_error_at ("previous declaration of `%#D' with %L linkage",
3373			   olddecl, DECL_LANGUAGE (olddecl));
3374	      cp_error ("conflicts with new declaration with %L linkage",
3375			DECL_LANGUAGE (newdecl));
3376	    }
3377	}
3378
3379      if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3380	;
3381      else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3382	{
3383	  tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3384	  tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3385	  int i = 1;
3386
3387	  if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3388	    t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3389
3390	  for (; t1 && t1 != void_list_node;
3391	       t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3392	    if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3393	      {
3394		if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3395					   TREE_PURPOSE (t2)))
3396		  {
3397		    if (pedantic)
3398		      {
3399			cp_pedwarn ("default argument given for parameter %d of `%#D'",
3400				    i, newdecl);
3401			cp_pedwarn_at ("after previous specification in `%#D'",
3402				       olddecl);
3403		      }
3404		  }
3405		else
3406		  {
3407		    cp_error ("default argument given for parameter %d of `%#D'",
3408			      i, newdecl);
3409		    cp_error_at ("after previous specification in `%#D'",
3410				 olddecl);
3411		  }
3412	      }
3413
3414	  if (DECL_THIS_INLINE (newdecl) && ! DECL_THIS_INLINE (olddecl)
3415	      && TREE_ADDRESSABLE (olddecl) && warn_inline)
3416	    {
3417	      cp_warning ("`%#D' was used before it was declared inline",
3418			  newdecl);
3419	      cp_warning_at ("previous non-inline declaration here",
3420			     olddecl);
3421	    }
3422	}
3423    }
3424
3425  /* If new decl is `static' and an `extern' was seen previously,
3426     warn about it.  */
3427  warn_extern_redeclared_static (newdecl, olddecl);
3428
3429  /* We have committed to returning 1 at this point.  */
3430  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3431    {
3432      /* Now that functions must hold information normally held
3433	 by field decls, there is extra work to do so that
3434	 declaration information does not get destroyed during
3435	 definition.  */
3436      if (DECL_VINDEX (olddecl))
3437	DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3438      if (DECL_CONTEXT (olddecl))
3439	DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3440      if (DECL_CLASS_CONTEXT (olddecl))
3441	DECL_CLASS_CONTEXT (newdecl) = DECL_CLASS_CONTEXT (olddecl);
3442      if (DECL_PENDING_INLINE_INFO (newdecl) == (struct pending_inline *)0)
3443	DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3444      DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3445      DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3446      DECL_ABSTRACT_VIRTUAL_P (newdecl) |= DECL_ABSTRACT_VIRTUAL_P (olddecl);
3447      DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3448      DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3449      new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3450
3451      /* Optionally warn about more than one declaration for the same
3452         name, but don't warn about a function declaration followed by a
3453         definition.  */
3454      if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3455	  && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3456	  /* Don't warn about extern decl followed by definition. */
3457	  && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3458	  /* Don't warn about friends, let add_friend take care of it. */
3459	  && ! DECL_FRIEND_P (newdecl))
3460	{
3461	  cp_warning ("redundant redeclaration of `%D' in same scope", newdecl);
3462	  cp_warning_at ("previous declaration of `%D'", olddecl);
3463	}
3464    }
3465
3466  /* Deal with C++: must preserve virtual function table size.  */
3467  if (TREE_CODE (olddecl) == TYPE_DECL)
3468    {
3469      register tree newtype = TREE_TYPE (newdecl);
3470      register tree oldtype = TREE_TYPE (olddecl);
3471
3472      if (newtype != error_mark_node && oldtype != error_mark_node
3473	  && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3474	{
3475	  CLASSTYPE_VSIZE (newtype) = CLASSTYPE_VSIZE (oldtype);
3476	  CLASSTYPE_FRIEND_CLASSES (newtype)
3477	    = CLASSTYPE_FRIEND_CLASSES (oldtype);
3478	}
3479    }
3480
3481  /* Copy all the DECL_... slots specified in the new decl
3482     except for any that we copy here from the old type.  */
3483  DECL_MACHINE_ATTRIBUTES (newdecl)
3484    = merge_machine_decl_attributes (olddecl, newdecl);
3485
3486  if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3487    {
3488      if (! duplicate_decls (DECL_TEMPLATE_RESULT (newdecl),
3489			     DECL_TEMPLATE_RESULT (olddecl)))
3490	cp_error ("invalid redeclaration of %D", newdecl);
3491      TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3492      DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3493	= chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3494		   DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3495
3496      return 1;
3497    }
3498
3499  if (types_match)
3500    {
3501      /* Automatically handles default parameters.  */
3502      tree oldtype = TREE_TYPE (olddecl);
3503      tree newtype;
3504
3505      /* Make sure we put the new type in the same obstack as the old one.  */
3506      if (oldtype)
3507	push_obstacks (TYPE_OBSTACK (oldtype), TYPE_OBSTACK (oldtype));
3508      else
3509	{
3510	  push_obstacks_nochange ();
3511	  end_temporary_allocation ();
3512	}
3513
3514      /* Merge the data types specified in the two decls.  */
3515      newtype = common_type (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3516
3517      if (TREE_CODE (newdecl) == VAR_DECL)
3518	DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3519      /* Do this after calling `common_type' so that default
3520	 parameters don't confuse us.  */
3521      else if (TREE_CODE (newdecl) == FUNCTION_DECL
3522	  && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3523	      != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3524	{
3525	  TREE_TYPE (newdecl) = build_exception_variant (newtype,
3526							 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3527	  TREE_TYPE (olddecl) = build_exception_variant (newtype,
3528							 TYPE_RAISES_EXCEPTIONS (oldtype));
3529
3530	  if ((pedantic || (! DECL_IN_SYSTEM_HEADER (olddecl)
3531	  		    && ! DECL_IN_SYSTEM_HEADER (newdecl)))
3532	      && DECL_SOURCE_LINE (olddecl) != 0
3533	      && flag_exceptions
3534	      && ! compexcepttypes (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
3535	    {
3536	      cp_pedwarn ("declaration of `%D' throws different exceptions",
3537			newdecl);
3538	      cp_pedwarn_at ("previous declaration here", olddecl);
3539	    }
3540	}
3541      TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3542
3543      /* Lay the type out, unless already done.  */
3544      if (! same_type_p (newtype, oldtype)
3545	  && TREE_TYPE (newdecl) != error_mark_node
3546	  && !(processing_template_decl && uses_template_parms (newdecl)))
3547	layout_type (TREE_TYPE (newdecl));
3548
3549      if ((TREE_CODE (newdecl) == VAR_DECL
3550	   || TREE_CODE (newdecl) == PARM_DECL
3551	   || TREE_CODE (newdecl) == RESULT_DECL
3552	   || TREE_CODE (newdecl) == FIELD_DECL
3553	   || TREE_CODE (newdecl) == TYPE_DECL)
3554	  && !(processing_template_decl && uses_template_parms (newdecl)))
3555	layout_decl (newdecl, 0);
3556
3557      /* Merge the type qualifiers.  */
3558      if (TREE_READONLY (newdecl))
3559	TREE_READONLY (olddecl) = 1;
3560      if (TREE_THIS_VOLATILE (newdecl))
3561	TREE_THIS_VOLATILE (olddecl) = 1;
3562
3563      /* Merge the initialization information.  */
3564      if (DECL_INITIAL (newdecl) == NULL_TREE
3565	  && DECL_INITIAL (olddecl) != NULL_TREE)
3566	{
3567	  DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3568	  DECL_SOURCE_FILE (newdecl) = DECL_SOURCE_FILE (olddecl);
3569	  DECL_SOURCE_LINE (newdecl) = DECL_SOURCE_LINE (olddecl);
3570	  if (DECL_LANG_SPECIFIC (newdecl)
3571	      && DECL_LANG_SPECIFIC (olddecl))
3572	    DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3573	}
3574
3575      /* Merge the section attribute.
3576         We want to issue an error if the sections conflict but that must be
3577	 done later in decl_attributes since we are called before attributes
3578	 are assigned.  */
3579      if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3580	DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3581
3582      /* Keep the old rtl since we can safely use it, unless it's the
3583	 call to abort() used for abstract virtuals.  */
3584      if ((DECL_LANG_SPECIFIC (olddecl)
3585	   && !DECL_ABSTRACT_VIRTUAL_P (olddecl))
3586	  || DECL_RTL (olddecl) != DECL_RTL (abort_fndecl))
3587	DECL_RTL (newdecl) = DECL_RTL (olddecl);
3588
3589      pop_obstacks ();
3590    }
3591  /* If cannot merge, then use the new type and qualifiers,
3592     and don't preserve the old rtl.  */
3593  else
3594    {
3595      /* Clean out any memory we had of the old declaration.  */
3596      tree oldstatic = value_member (olddecl, static_aggregates);
3597      if (oldstatic)
3598	TREE_VALUE (oldstatic) = error_mark_node;
3599
3600      TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3601      TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3602      TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3603      TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3604    }
3605
3606  /* Merge the storage class information.  */
3607  DECL_WEAK (newdecl) |= DECL_WEAK (olddecl);
3608  DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3609  TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3610  TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3611  if (! DECL_EXTERNAL (olddecl))
3612    DECL_EXTERNAL (newdecl) = 0;
3613
3614  if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3615    {
3616      DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3617      DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3618      DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3619      DECL_TEMPLATE_INSTANTIATED (newdecl)
3620	|= DECL_TEMPLATE_INSTANTIATED (olddecl);
3621      /* Don't really know how much of the language-specific
3622	 values we should copy from old to new.  */
3623      DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3624      DECL_ACCESS (newdecl) = DECL_ACCESS (olddecl);
3625      DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3626      DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3627      olddecl_friend = DECL_FRIEND_P (olddecl);
3628
3629      /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3630      if (TREE_CODE (newdecl) == FUNCTION_DECL
3631	  || DECL_FUNCTION_TEMPLATE_P (newdecl))
3632	DECL_BEFRIENDING_CLASSES (newdecl)
3633	  = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3634		     DECL_BEFRIENDING_CLASSES (olddecl));
3635    }
3636
3637  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3638    {
3639      if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3640	  && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3641	{
3642	  /* If newdecl is not a specialization, then it is not a
3643	     template-related function at all.  And that means that we
3644	     shoud have exited above, returning 0.  */
3645	  my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3646			      0);
3647
3648	  if (TREE_USED (olddecl))
3649	    /* From [temp.expl.spec]:
3650
3651	       If a template, a member template or the member of a class
3652	       template is explicitly specialized then that
3653	       specialization shall be declared before the first use of
3654	       that specialization that would cause an implicit
3655	       instantiation to take place, in every translation unit in
3656	       which such a use occurs.  */
3657	    cp_error ("explicit specialization of %D after first use",
3658		      olddecl);
3659
3660	  SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3661	}
3662      DECL_THIS_INLINE (newdecl) |= DECL_THIS_INLINE (olddecl);
3663
3664      /* If either decl says `inline', this fn is inline, unless its
3665         definition was passed already.  */
3666      if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3667	DECL_INLINE (olddecl) = 1;
3668      DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3669
3670      if (! types_match)
3671	{
3672	  DECL_LANGUAGE (olddecl) = DECL_LANGUAGE (newdecl);
3673	  DECL_ASSEMBLER_NAME (olddecl) = DECL_ASSEMBLER_NAME (newdecl);
3674	  DECL_RTL (olddecl) = DECL_RTL (newdecl);
3675	}
3676      if (! types_match || new_defines_function)
3677	{
3678	  /* These need to be copied so that the names are available.  */
3679	  DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3680	  DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3681	}
3682      if (new_defines_function)
3683	/* If defining a function declared with other language
3684	   linkage, use the previously declared language linkage.  */
3685	DECL_LANGUAGE (newdecl) = DECL_LANGUAGE (olddecl);
3686      else
3687	{
3688	  /* If redeclaring a builtin function, and not a definition,
3689	     it stays built in.  */
3690	  if (DECL_BUILT_IN (olddecl))
3691	    {
3692	      DECL_BUILT_IN (newdecl) = 1;
3693	      DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3694	      /* If we're keeping the built-in definition, keep the rtl,
3695		 regardless of declaration matches.  */
3696	      DECL_RTL (newdecl) = DECL_RTL (olddecl);
3697	    }
3698	  else
3699	    DECL_FRAME_SIZE (newdecl) = DECL_FRAME_SIZE (olddecl);
3700
3701	  DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3702	  if ((DECL_SAVED_INSNS (newdecl) = DECL_SAVED_INSNS (olddecl)))
3703	    /* Previously saved insns go together with
3704	       the function's previous definition.  */
3705	    DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3706	  /* Don't clear out the arguments if we're redefining a function.  */
3707	  if (DECL_ARGUMENTS (olddecl))
3708	    DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3709	}
3710      if (DECL_LANG_SPECIFIC (olddecl))
3711	DECL_MAIN_VARIANT (newdecl) = DECL_MAIN_VARIANT (olddecl);
3712    }
3713
3714  if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3715    {
3716      NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3717    }
3718
3719  /* Now preserve various other info from the definition.  */
3720  TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3721  TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3722  DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3723  DECL_ASSEMBLER_NAME (newdecl) = DECL_ASSEMBLER_NAME (olddecl);
3724
3725  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3726    {
3727      int function_size;
3728      struct lang_decl *ol = DECL_LANG_SPECIFIC (olddecl);
3729      struct lang_decl *nl = DECL_LANG_SPECIFIC (newdecl);
3730
3731      function_size = sizeof (struct tree_decl);
3732
3733      bcopy ((char *) newdecl + sizeof (struct tree_common),
3734	     (char *) olddecl + sizeof (struct tree_common),
3735	     function_size - sizeof (struct tree_common));
3736
3737      /* Can we safely free the storage used by newdecl?  */
3738
3739#define ROUND(x) ((x + obstack_alignment_mask (&permanent_obstack)) \
3740		  & ~ obstack_alignment_mask (&permanent_obstack))
3741
3742      if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3743	{
3744	  /* If newdecl is a template instantiation, it is possible that
3745	     the following sequence of events has occurred:
3746
3747	     o A friend function was declared in a class template.  The
3748	     class template was instantiated.
3749
3750	     o The instantiation of the friend declaration was
3751	     recorded on the instantiation list, and is newdecl.
3752
3753	     o Later, however, instantiate_class_template called pushdecl
3754	     on the newdecl to perform name injection.  But, pushdecl in
3755	     turn called duplicate_decls when it discovered that another
3756	     declaration of a global function with the same name already
3757	     existed.
3758
3759	     o Here, in duplicate_decls, we decided to clobber newdecl.
3760
3761	     If we're going to do that, we'd better make sure that
3762	     olddecl, and not newdecl, is on the list of
3763	     instantiations so that if we try to do the instantiation
3764	     again we won't get the clobbered declaration.  */
3765
3766	  tree tmpl = DECL_TI_TEMPLATE (newdecl);
3767	  tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3768
3769	  for (; decls; decls = TREE_CHAIN (decls))
3770	    if (TREE_VALUE (decls) == newdecl)
3771	      TREE_VALUE (decls) = olddecl;
3772	}
3773
3774      if (((char *)newdecl + ROUND (function_size) == (char *)nl
3775	   && ((char *)newdecl + ROUND (function_size)
3776	       + ROUND (sizeof (struct lang_decl))
3777	       == obstack_next_free (&permanent_obstack)))
3778	  || ((char *)newdecl + ROUND (function_size)
3779	      == obstack_next_free (&permanent_obstack)))
3780	{
3781	  DECL_MAIN_VARIANT (newdecl) = olddecl;
3782	  DECL_LANG_SPECIFIC (olddecl) = ol;
3783	  bcopy ((char *)nl, (char *)ol, sizeof (struct lang_decl));
3784
3785	  obstack_free (&permanent_obstack, newdecl);
3786	}
3787      else if (LANG_DECL_PERMANENT (ol) && ol != nl)
3788	{
3789	  if (DECL_MAIN_VARIANT (olddecl) == olddecl)
3790	    {
3791	      /* Save these lang_decls that would otherwise be lost.  */
3792	      extern tree free_lang_decl_chain;
3793	      tree free_lang_decl = (tree) ol;
3794
3795	      if (DECL_LANG_SPECIFIC (olddecl) == ol)
3796		abort ();
3797
3798	      TREE_CHAIN (free_lang_decl) = free_lang_decl_chain;
3799	      free_lang_decl_chain = free_lang_decl;
3800	    }
3801	  else
3802	    {
3803	      /* Storage leak.  */;
3804	    }
3805	}
3806    }
3807  else
3808    {
3809      bcopy ((char *) newdecl + sizeof (struct tree_common),
3810	     (char *) olddecl + sizeof (struct tree_common),
3811	     sizeof (struct tree_decl) - sizeof (struct tree_common)
3812	     + tree_code_length [(int)TREE_CODE (newdecl)] * sizeof (char *));
3813    }
3814
3815  DECL_UID (olddecl) = olddecl_uid;
3816  if (olddecl_friend)
3817    DECL_FRIEND_P (olddecl) = 1;
3818
3819  /* NEWDECL contains the merged attribute lists.
3820     Update OLDDECL to be the same.  */
3821  DECL_MACHINE_ATTRIBUTES (olddecl) = DECL_MACHINE_ATTRIBUTES (newdecl);
3822
3823  return 1;
3824}
3825
3826/* Record a decl-node X as belonging to the current lexical scope.
3827   Check for errors (such as an incompatible declaration for the same
3828   name already seen in the same scope).
3829
3830   Returns either X or an old decl for the same name.
3831   If an old decl is returned, it may have been smashed
3832   to agree with what X says.  */
3833
3834tree
3835pushdecl (x)
3836     tree x;
3837{
3838  register tree t;
3839  register tree name = DECL_ASSEMBLER_NAME (x);
3840  int need_new_binding = 1;
3841
3842  if (DECL_TEMPLATE_PARM_P (x))
3843    /* Template parameters have no context; they are not X::T even
3844       when declared within a class or namespace.  */
3845    ;
3846  else
3847    {
3848      if (current_function_decl && x != current_function_decl
3849	  /* A local declaration for a function doesn't constitute
3850             nesting.  */
3851	  && (TREE_CODE (x) != FUNCTION_DECL || DECL_INITIAL (x))
3852	  /* Don't change DECL_CONTEXT of virtual methods.  */
3853	  && (TREE_CODE (x) != FUNCTION_DECL || !DECL_VIRTUAL_P (x))
3854	  && !DECL_CONTEXT (x))
3855	DECL_CONTEXT (x) = current_function_decl;
3856      if (!DECL_CONTEXT (x))
3857	DECL_CONTEXT (x) = FROB_CONTEXT (current_namespace);
3858    }
3859
3860  /* Type are looked up using the DECL_NAME, as that is what the rest of the
3861     compiler wants to use.  */
3862  if (TREE_CODE (x) == TYPE_DECL || TREE_CODE (x) == VAR_DECL
3863      || TREE_CODE (x) == NAMESPACE_DECL)
3864    name = DECL_NAME (x);
3865
3866  if (name)
3867    {
3868#if 0
3869      /* Not needed...see below.  */
3870      char *file;
3871      int line;
3872#endif
3873      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3874	name = TREE_OPERAND (name, 0);
3875
3876      /* Namespace-scoped variables are not found in the current level. */
3877      if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x))
3878	t = namespace_binding (name, DECL_CONTEXT (x));
3879      else
3880	t = lookup_name_current_level (name);
3881      if (t == error_mark_node)
3882	{
3883	  /* error_mark_node is 0 for a while during initialization!  */
3884	  t = NULL_TREE;
3885	  cp_error_at ("`%#D' used prior to declaration", x);
3886	}
3887
3888      else if (t != NULL_TREE)
3889	{
3890#if 0
3891	  /* This is turned off until I have time to do it right (bpk).  */
3892	  /* With the code below that uses it...  */
3893	  file = DECL_SOURCE_FILE (t);
3894	  line = DECL_SOURCE_LINE (t);
3895#endif
3896	  if (TREE_CODE (t) == PARM_DECL)
3897	    {
3898	      if (DECL_CONTEXT (t) == NULL_TREE)
3899		fatal ("parse errors have confused me too much");
3900
3901	      /* Check for duplicate params.  */
3902	      if (duplicate_decls (x, t))
3903		return t;
3904	    }
3905	  else if (((TREE_CODE (x) == FUNCTION_DECL && DECL_LANGUAGE (x) == lang_c)
3906		    || DECL_FUNCTION_TEMPLATE_P (x))
3907		   && is_overloaded_fn (t))
3908	    /* Don't do anything just yet. */;
3909	  else if (t == wchar_decl_node)
3910	    {
3911	      if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3912		cp_pedwarn ("redeclaration of wchar_t as `%T'", TREE_TYPE (x));
3913
3914	      /* Throw away the redeclaration.  */
3915	      return t;
3916	    }
3917	  else if (TREE_CODE (t) != TREE_CODE (x))
3918	    {
3919	      if (duplicate_decls (x, t))
3920		return t;
3921	    }
3922	  else if (duplicate_decls (x, t))
3923	    {
3924#if 0
3925	      /* This is turned off until I have time to do it right (bpk).  */
3926
3927	      /* Also warn if they did a prototype with `static' on it, but
3928		 then later left the `static' off.  */
3929	      if (! TREE_PUBLIC (name) && TREE_PUBLIC (x))
3930		{
3931		  if (DECL_LANG_SPECIFIC (t) && DECL_FRIEND_P (t))
3932		    return t;
3933
3934		  if (extra_warnings)
3935		    {
3936		      cp_warning ("`static' missing from declaration of `%D'",
3937				  t);
3938		      warning_with_file_and_line (file, line,
3939						  "previous declaration of `%s'",
3940						  decl_as_string (t, 0));
3941		    }
3942
3943		  /* Now fix things so it'll do what they expect.  */
3944		  if (current_function_decl)
3945		    TREE_PUBLIC (current_function_decl) = 0;
3946		}
3947	      /* Due to interference in memory reclamation (X may be
3948		 obstack-deallocated at this point), we must guard against
3949		 one really special case.  [jason: This should be handled
3950		 by start_function]  */
3951	      if (current_function_decl == x)
3952		current_function_decl = t;
3953#endif
3954	      if (TREE_CODE (t) == TYPE_DECL)
3955		{
3956		  /* If declaring a type as a typedef, copy the type (unless
3957		     we're at line 0), and install this TYPE_DECL as the new
3958		     type's typedef name.  See the extensive comment in
3959		     ../c-decl.c (pushdecl). */
3960		  tree type = TREE_TYPE (t);
3961		  if (DECL_ORIGINAL_TYPE (t) == NULL
3962		      && DECL_SOURCE_LINE (t) != 0
3963		      && type != error_mark_node && TYPE_NAME (type) != t
3964		      /* We don't want to copy the type when all we're
3965			 doing is making a TYPE_DECL for the purposes of
3966			 inlining.  */
3967		      && (!TYPE_NAME (type)
3968			  || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (t)))
3969		    {
3970		      DECL_ORIGINAL_TYPE (t) = type;
3971		      type = build_type_copy (type);
3972		      TYPE_STUB_DECL (type) =
3973			TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (t));
3974		      TYPE_NAME (type) = t;
3975		      TREE_TYPE (t) = type;
3976		      if (TYPE_IDENTIFIER (type))
3977			set_identifier_type_value_with_scope (DECL_NAME (t),
3978						type, current_binding_level);
3979		    }
3980
3981		  SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3982		}
3983	      else if (TREE_CODE (t) == FUNCTION_DECL)
3984		check_default_args (t);
3985
3986	      return t;
3987	    }
3988	  else if (DECL_MAIN_P (x))
3989	    {
3990	      /* A redeclaration of main, but not a duplicate of the
3991		 previous one.
3992
3993		 [basic.start.main]
3994
3995	         This function shall not be overloaded.  */
3996	      cp_error_at ("invalid redeclaration of `%D'", t);
3997	      cp_error ("as `%D'", x);
3998	      /* We don't try to push this declaration since that
3999		 causes a crash.  */
4000	      return x;
4001	    }
4002	}
4003
4004      check_template_shadow (x);
4005
4006      /* If this is a function conjured up by the backend, massage it
4007	 so it looks friendly.  */
4008      if (TREE_CODE (x) == FUNCTION_DECL
4009	  && ! DECL_LANG_SPECIFIC (x))
4010	{
4011	  retrofit_lang_decl (x);
4012	  DECL_LANGUAGE (x) = lang_c;
4013	}
4014
4015      if (TREE_CODE (x) == FUNCTION_DECL && ! DECL_FUNCTION_MEMBER_P (x))
4016	{
4017	  t = push_overloaded_decl (x, PUSH_LOCAL);
4018	  if (t != x || DECL_LANGUAGE (x) == lang_c)
4019	    return t;
4020	  if (!namespace_bindings_p ())
4021	    /* We do not need to create a binding for this name;
4022	       push_overloaded_decl will have already done so if
4023	       necessary.  */
4024	    need_new_binding = 0;
4025	}
4026      else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
4027	return push_overloaded_decl (x, PUSH_GLOBAL);
4028
4029      /* If declaring a type as a typedef, copy the type (unless we're
4030	 at line 0), and install this TYPE_DECL as the new type's typedef
4031	 name.  See the extensive comment in ../c-decl.c (pushdecl). */
4032      if (TREE_CODE (x) == TYPE_DECL)
4033	{
4034	  tree type = TREE_TYPE (x);
4035          if (DECL_SOURCE_LINE (x) == 0)
4036            {
4037	      if (TYPE_NAME (type) == 0)
4038	        TYPE_NAME (type) = x;
4039            }
4040          else if (type != error_mark_node && TYPE_NAME (type) != x
4041		   /* We don't want to copy the type when all we're
4042		      doing is making a TYPE_DECL for the purposes of
4043		      inlining.  */
4044		   && (!TYPE_NAME (type)
4045		       || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
4046            {
4047	      push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
4048
4049	      DECL_ORIGINAL_TYPE (x) = type;
4050              type = build_type_copy (type);
4051	      TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
4052              TYPE_NAME (type) = x;
4053              TREE_TYPE (x) = type;
4054
4055	      pop_obstacks ();
4056            }
4057
4058	  if (type != error_mark_node
4059	      && TYPE_NAME (type)
4060	      && TYPE_IDENTIFIER (type))
4061            set_identifier_type_value_with_scope (DECL_NAME (x), type,
4062						  current_binding_level);
4063
4064	}
4065
4066      /* Multiple external decls of the same identifier ought to match.
4067
4068	 We get warnings about inline functions where they are defined.
4069	 We get warnings about other functions from push_overloaded_decl.
4070
4071	 Avoid duplicate warnings where they are used.  */
4072      if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
4073	{
4074	  tree decl;
4075
4076	  if (IDENTIFIER_NAMESPACE_VALUE (name) != NULL_TREE
4077	      && (DECL_EXTERNAL (IDENTIFIER_NAMESPACE_VALUE (name))
4078		  || TREE_PUBLIC (IDENTIFIER_NAMESPACE_VALUE (name))))
4079	    decl = IDENTIFIER_NAMESPACE_VALUE (name);
4080	  else
4081	    decl = NULL_TREE;
4082
4083	  if (decl
4084	      /* If different sort of thing, we already gave an error.  */
4085	      && TREE_CODE (decl) == TREE_CODE (x)
4086	      && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
4087	    {
4088	      cp_pedwarn ("type mismatch with previous external decl", x);
4089	      cp_pedwarn_at ("previous external decl of `%#D'", decl);
4090	    }
4091	}
4092
4093      /* This name is new in its binding level.
4094	 Install the new declaration and return it.  */
4095      if (namespace_bindings_p ())
4096	{
4097	  /* Install a global value.  */
4098
4099	  /* If the first global decl has external linkage,
4100	     warn if we later see static one.  */
4101	  if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
4102	    TREE_PUBLIC (name) = 1;
4103
4104	  if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
4105		&& t != NULL_TREE))
4106	    {
4107	      if (TREE_CODE (x) == FUNCTION_DECL)
4108		my_friendly_assert
4109		  ((IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE)
4110		  || (IDENTIFIER_GLOBAL_VALUE (name) == x), 378);
4111	      SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
4112	    }
4113
4114	  /* Don't forget if the function was used via an implicit decl.  */
4115	  if (IDENTIFIER_IMPLICIT_DECL (name)
4116	      && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4117	    TREE_USED (x) = 1;
4118
4119	  /* Don't forget if its address was taken in that way.  */
4120	  if (IDENTIFIER_IMPLICIT_DECL (name)
4121	      && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4122	    TREE_ADDRESSABLE (x) = 1;
4123
4124	  /* Warn about mismatches against previous implicit decl.  */
4125	  if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4126	      /* If this real decl matches the implicit, don't complain.  */
4127	      && ! (TREE_CODE (x) == FUNCTION_DECL
4128		    && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
4129	    cp_warning
4130	      ("`%D' was previously implicitly declared to return `int'", x);
4131
4132	  /* If new decl is `static' and an `extern' was seen previously,
4133	     warn about it.  */
4134	  if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4135	    warn_extern_redeclared_static (x, t);
4136	}
4137      else
4138	{
4139	  /* Here to install a non-global value.  */
4140	  tree oldlocal = IDENTIFIER_VALUE (name);
4141	  tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
4142
4143	  if (need_new_binding)
4144	    {
4145	      push_local_binding (name, x, 0);
4146	      /* Because push_local_binding will hook X on to the
4147		 current_binding_level's name list, we don't want to
4148		 do that again below.  */
4149	      need_new_binding = 0;
4150	    }
4151
4152	  /* If this is a TYPE_DECL, push it into the type value slot.  */
4153	  if (TREE_CODE (x) == TYPE_DECL)
4154	    set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4155						  current_binding_level);
4156
4157	  /* Clear out any TYPE_DECL shadowed by a namespace so that
4158	     we won't think this is a type.  The C struct hack doesn't
4159	     go through namespaces.  */
4160	  if (TREE_CODE (x) == NAMESPACE_DECL)
4161	    set_identifier_type_value_with_scope (name, NULL_TREE,
4162						  current_binding_level);
4163
4164	  /* If this is an extern function declaration, see if we
4165	     have a global definition or declaration for the function.  */
4166	  if (oldlocal == NULL_TREE
4167	      && DECL_EXTERNAL (x)
4168	      && oldglobal != NULL_TREE
4169	      && TREE_CODE (x) == FUNCTION_DECL
4170	      && TREE_CODE (oldglobal) == FUNCTION_DECL)
4171	    {
4172	      /* We have one.  Their types must agree.  */
4173	      if (decls_match (x, oldglobal))
4174		/* OK */;
4175	      else
4176		{
4177		  cp_warning ("extern declaration of `%#D' doesn't match", x);
4178		  cp_warning_at ("global declaration `%#D'", oldglobal);
4179		}
4180	    }
4181	  /* If we have a local external declaration,
4182	     and no file-scope declaration has yet been seen,
4183	     then if we later have a file-scope decl it must not be static.  */
4184	  if (oldlocal == NULL_TREE
4185	      && oldglobal == NULL_TREE
4186	      && DECL_EXTERNAL (x)
4187	      && TREE_PUBLIC (x))
4188	    TREE_PUBLIC (name) = 1;
4189
4190	  if (DECL_FROM_INLINE (x))
4191	    /* Inline decls shadow nothing.  */;
4192
4193	  /* Warn if shadowing an argument at the top level of the body.  */
4194	  else if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4195		   && TREE_CODE (oldlocal) == PARM_DECL
4196		   /* Don't complain if it's from an enclosing function.  */
4197		   && DECL_CONTEXT (oldlocal) == current_function_decl
4198		   && TREE_CODE (x) != PARM_DECL)
4199	    {
4200	      /* Go to where the parms should be and see if we
4201		 find them there.  */
4202	      struct binding_level *b = current_binding_level->level_chain;
4203
4204	      if (cleanup_label)
4205		b = b->level_chain;
4206
4207	      /* ARM $8.3 */
4208	      if (b->parm_flag == 1)
4209		cp_error ("declaration of `%#D' shadows a parameter", name);
4210	    }
4211	  else if (warn_shadow && oldlocal != NULL_TREE
4212		   && current_binding_level->is_for_scope
4213		   && !DECL_DEAD_FOR_LOCAL (oldlocal))
4214	    {
4215	      warning ("variable `%s' shadows local",
4216		       IDENTIFIER_POINTER (name));
4217	      cp_warning_at ("  this is the shadowed declaration", oldlocal);
4218	    }
4219	  /* Maybe warn if shadowing something else.  */
4220	  else if (warn_shadow && !DECL_EXTERNAL (x)
4221		   /* No shadow warnings for internally generated vars.  */
4222		   && ! DECL_ARTIFICIAL (x)
4223		   /* No shadow warnings for vars made for inlining.  */
4224		   && ! DECL_FROM_INLINE (x))
4225	    {
4226	      const char *warnstring = NULL;
4227
4228	      if (oldlocal != NULL_TREE && TREE_CODE (oldlocal) == PARM_DECL)
4229		warnstring = "declaration of `%s' shadows a parameter";
4230	      else if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4231		       && current_class_ptr
4232		       && !TREE_STATIC (name))
4233		warnstring = "declaration of `%s' shadows a member of `this'";
4234	      else if (oldlocal != NULL_TREE)
4235		warnstring = "declaration of `%s' shadows previous local";
4236	      else if (oldglobal != NULL_TREE)
4237		/* XXX shadow warnings in outer-more namespaces */
4238		warnstring = "declaration of `%s' shadows global declaration";
4239
4240	      if (warnstring)
4241		warning (warnstring, IDENTIFIER_POINTER (name));
4242	    }
4243	}
4244
4245      if (TREE_CODE (x) == FUNCTION_DECL)
4246	check_default_args (x);
4247
4248      /* Keep count of variables in this level with incomplete type.  */
4249      if (TREE_CODE (x) == VAR_DECL
4250	  && TREE_TYPE (x) != error_mark_node
4251	  && ((TYPE_SIZE (TREE_TYPE (x)) == NULL_TREE
4252	       && PROMOTES_TO_AGGR_TYPE (TREE_TYPE (x), ARRAY_TYPE))
4253	      /* RTTI TD entries are created while defining the type_info.  */
4254	      || (TYPE_LANG_SPECIFIC (TREE_TYPE (x))
4255		  && TYPE_BEING_DEFINED (TREE_TYPE (x)))))
4256	current_binding_level->incomplete
4257	  = tree_cons (NULL_TREE, x, current_binding_level->incomplete);
4258    }
4259
4260  if (need_new_binding)
4261    {
4262      /* Put decls on list in reverse order.
4263	 We will reverse them later if necessary.  */
4264      TREE_CHAIN (x) = current_binding_level->names;
4265      current_binding_level->names = x;
4266      if (current_binding_level == global_binding_level
4267	  && !TREE_PERMANENT (x))
4268	my_friendly_abort (124);
4269    }
4270
4271  return x;
4272}
4273
4274/* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4275   caller to set DECL_CONTEXT properly.  */
4276
4277static tree
4278pushdecl_with_scope (x, level)
4279     tree x;
4280     struct binding_level *level;
4281{
4282  register struct binding_level *b;
4283  tree function_decl = current_function_decl;
4284
4285  current_function_decl = NULL_TREE;
4286  if (level->parm_flag == 2)
4287    {
4288      b = class_binding_level;
4289      class_binding_level = level;
4290      pushdecl_class_level (x);
4291      class_binding_level = b;
4292    }
4293  else
4294    {
4295      b = current_binding_level;
4296      current_binding_level = level;
4297      x = pushdecl (x);
4298      current_binding_level = b;
4299    }
4300  current_function_decl = function_decl;
4301  return x;
4302}
4303
4304/* Like pushdecl, only it places X in the current namespace,
4305   if appropriate.  */
4306
4307tree
4308pushdecl_namespace_level (x)
4309     tree x;
4310{
4311  register struct binding_level *b = current_binding_level;
4312  register tree t;
4313
4314  t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4315
4316  /* Now, the type_shadowed stack may screw us.  Munge it so it does
4317     what we want.  */
4318  if (TREE_CODE (x) == TYPE_DECL)
4319    {
4320      tree name = DECL_NAME (x);
4321      tree newval;
4322      tree *ptr = (tree *)0;
4323      for (; b != global_binding_level; b = b->level_chain)
4324        {
4325          tree shadowed = b->type_shadowed;
4326          for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4327            if (TREE_PURPOSE (shadowed) == name)
4328              {
4329		ptr = &TREE_VALUE (shadowed);
4330		/* Can't break out of the loop here because sometimes
4331		   a binding level will have duplicate bindings for
4332		   PT names.  It's gross, but I haven't time to fix it.  */
4333              }
4334        }
4335      newval = TREE_TYPE (x);
4336      if (ptr == (tree *)0)
4337        {
4338          /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4339             up here if this is changed to an assertion.  --KR  */
4340	  SET_IDENTIFIER_TYPE_VALUE (name, newval);
4341	}
4342      else
4343        {
4344	  *ptr = newval;
4345        }
4346    }
4347  return t;
4348}
4349
4350/* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4351   if appropriate.  */
4352
4353tree
4354pushdecl_top_level (x)
4355     tree x;
4356{
4357  tree cur_namespace = current_namespace;
4358  current_namespace = global_namespace;
4359  x = pushdecl_namespace_level (x);
4360  current_namespace = cur_namespace;
4361  return x;
4362}
4363
4364/* Make the declaration of X appear in CLASS scope.  */
4365
4366void
4367pushdecl_class_level (x)
4368     tree x;
4369{
4370  /* Don't use DECL_ASSEMBLER_NAME here!  Everything that looks in class
4371     scope looks for the pre-mangled name.  */
4372  register tree name;
4373
4374  if (TREE_CODE (x) == OVERLOAD)
4375    x = OVL_CURRENT (x);
4376  name = DECL_NAME (x);
4377
4378  if (name)
4379    {
4380      push_class_level_binding (name, x);
4381      if (TREE_CODE (x) == TYPE_DECL)
4382	set_identifier_type_value (name, TREE_TYPE (x));
4383    }
4384  else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4385    {
4386      tree f;
4387
4388      for (f = TYPE_FIELDS (TREE_TYPE (x));
4389	   f;
4390	   f = TREE_CHAIN (f))
4391	pushdecl_class_level (f);
4392    }
4393}
4394
4395#if 0
4396/* This function is used to push the mangled decls for nested types into
4397   the appropriate scope.  Previously pushdecl_top_level was used, but that
4398   is incorrect for members of local classes.  */
4399
4400void
4401pushdecl_nonclass_level (x)
4402     tree x;
4403{
4404  struct binding_level *b = current_binding_level;
4405
4406  my_friendly_assert (b->parm_flag != 2, 180);
4407
4408#if 0
4409  /* Get out of template binding levels */
4410  while (b->pseudo_global)
4411    b = b->level_chain;
4412#endif
4413
4414  pushdecl_with_scope (x, b);
4415}
4416#endif
4417
4418/* Make the declaration(s) of X appear in CLASS scope
4419   under the name NAME.  */
4420
4421void
4422push_class_level_binding (name, x)
4423     tree name;
4424     tree x;
4425{
4426  tree binding;
4427  /* The class_binding_level will be NULL if x is a template
4428     parameter name in a member template.  */
4429  if (!class_binding_level)
4430    return;
4431
4432  /* Make sure that this new member does not have the same name
4433     as a template parameter.  */
4434  if (TYPE_BEING_DEFINED (current_class_type))
4435    check_template_shadow (x);
4436
4437  /* If this declaration shadows a declaration from an enclosing
4438     class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4439     we leave this class.  Record the shadowed declaration here.  */
4440  binding = IDENTIFIER_BINDING (name);
4441  if (binding
4442      && ((TREE_CODE (x) == OVERLOAD
4443	   && BINDING_VALUE (binding)
4444	   && is_overloaded_fn (BINDING_VALUE (binding)))
4445	  || INHERITED_VALUE_BINDING_P (binding)))
4446    {
4447      tree shadow;
4448      tree old_decl;
4449
4450      /* If the old binding was from a base class, and was for a tag
4451	 name, slide it over to make room for the new binding.  The
4452	 old binding is still visible if explicitly qualified with a
4453	 class-key.  */
4454      if (INHERITED_VALUE_BINDING_P (binding)
4455	  && BINDING_VALUE (binding)
4456	  && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4457	  && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4458	  && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4459	{
4460	  old_decl = BINDING_TYPE (binding);
4461	  BINDING_TYPE (binding) = BINDING_VALUE (binding);
4462	  BINDING_VALUE (binding) = NULL_TREE;
4463	  INHERITED_VALUE_BINDING_P (binding) = 0;
4464	}
4465      else
4466	old_decl = BINDING_VALUE (binding);
4467
4468      /* There was already a binding for X containing fewer
4469	 functions than are named in X.  Find the previous
4470	 declaration of X on the class-shadowed list, and update it.  */
4471      for (shadow = class_binding_level->class_shadowed;
4472	   shadow;
4473	   shadow = TREE_CHAIN (shadow))
4474	if (TREE_PURPOSE (shadow) == name
4475	    && TREE_TYPE (shadow) == old_decl)
4476	  {
4477	    BINDING_VALUE (binding) = x;
4478	    INHERITED_VALUE_BINDING_P (binding) = 0;
4479	    TREE_TYPE (shadow) = x;
4480	    return;
4481	  }
4482    }
4483
4484  /* If we didn't replace an existing binding, put the binding on the
4485     stack of bindings for the identifier, and update
4486     IDENTIFIER_CLASS_VALUE.  */
4487  if (push_class_binding (name, x))
4488    {
4489      push_cache_obstack ();
4490      class_binding_level->class_shadowed
4491	= tree_cons (name, IDENTIFIER_CLASS_VALUE (name),
4492		     class_binding_level->class_shadowed);
4493      pop_obstacks ();
4494      /* Record the value we are binding NAME to so that we can know
4495	 what to pop later.  */
4496      TREE_TYPE (class_binding_level->class_shadowed) = x;
4497    }
4498}
4499
4500/* Insert another USING_DECL into the current binding level,
4501   returning this declaration. If this is a redeclaration,
4502   do nothing and return NULL_TREE.  */
4503
4504tree
4505push_using_decl (scope, name)
4506     tree scope;
4507     tree name;
4508{
4509  tree decl;
4510
4511  my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4512  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4513  for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4514    if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4515      break;
4516  if (decl)
4517    return NULL_TREE;
4518  decl = build_lang_decl (USING_DECL, name, void_type_node);
4519  DECL_INITIAL (decl) = scope;
4520  TREE_CHAIN (decl) = current_binding_level->usings;
4521  current_binding_level->usings = decl;
4522  return decl;
4523}
4524
4525/* Add namespace to using_directives. Return NULL_TREE if nothing was
4526   changed (i.e. there was already a directive), or the fresh
4527   TREE_LIST otherwise.  */
4528
4529tree
4530push_using_directive (used)
4531     tree used;
4532{
4533  tree ud = current_binding_level->using_directives;
4534  tree iter, ancestor;
4535
4536  /* Check if we already have this. */
4537  if (purpose_member (used, ud) != NULL_TREE)
4538    return NULL_TREE;
4539
4540  /* Recursively add all namespaces used. */
4541  for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4542    push_using_directive (TREE_PURPOSE (iter));
4543
4544  ancestor = namespace_ancestor (current_decl_namespace (), used);
4545  ud = current_binding_level->using_directives;
4546  ud = perm_tree_cons (used, ancestor, ud);
4547  current_binding_level->using_directives = ud;
4548  return ud;
4549}
4550
4551/* DECL is a FUNCTION_DECL for a non-member function, which may have
4552   other definitions already in place.  We get around this by making
4553   the value of the identifier point to a list of all the things that
4554   want to be referenced by that name.  It is then up to the users of
4555   that name to decide what to do with that list.
4556
4557   DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its DECL_RESULT
4558   slot.  It is dealt with the same way.
4559
4560   FLAGS is a bitwise-or of the following values:
4561     PUSH_LOCAL: Bind DECL in the current scope, rather than at
4562                 namespace scope.
4563     PUSH_USING: DECL is being pushed as the result of a using
4564                 declaration.
4565
4566   The value returned may be a previous declaration if we guessed wrong
4567   about what language DECL should belong to (C or C++).  Otherwise,
4568   it's always DECL (and never something that's not a _DECL).  */
4569
4570tree
4571push_overloaded_decl (decl, flags)
4572     tree decl;
4573     int flags;
4574{
4575  tree name = DECL_NAME (decl);
4576  tree old;
4577  tree new_binding;
4578  int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4579
4580  if (doing_global)
4581    {
4582      old = namespace_binding (name, DECL_CONTEXT (decl));
4583      if (old && TREE_CODE (old) == FUNCTION_DECL
4584	  && DECL_ARTIFICIAL (old)
4585	  && (DECL_BUILT_IN (old) || DECL_BUILT_IN_NONANSI (old)))
4586	{
4587	  if (duplicate_decls (decl, old))
4588	    return old;
4589	  old = NULL_TREE;
4590	}
4591    }
4592  else
4593    old = lookup_name_current_level (name);
4594
4595  if (old)
4596    {
4597      if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4598	{
4599	  tree t = TREE_TYPE (old);
4600	  if (IS_AGGR_TYPE (t) && warn_shadow
4601	      && (! DECL_IN_SYSTEM_HEADER (decl)
4602		  || ! DECL_IN_SYSTEM_HEADER (old)))
4603	    cp_warning ("`%#D' hides constructor for `%#T'", decl, t);
4604	  old = NULL_TREE;
4605	}
4606      else if (is_overloaded_fn (old))
4607        {
4608          tree tmp;
4609
4610	  for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4611	    {
4612	      tree fn = OVL_CURRENT (tmp);
4613
4614	      if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4615		  && !(flags & PUSH_USING)
4616		  && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4617				TYPE_ARG_TYPES (TREE_TYPE (decl))))
4618		cp_error ("`%#D' conflicts with previous using declaration `%#D'",
4619			  decl, fn);
4620
4621	      if (duplicate_decls (decl, fn))
4622		return fn;
4623	    }
4624	}
4625      else
4626	{
4627	  cp_error_at ("previous non-function declaration `%#D'", old);
4628	  cp_error ("conflicts with function declaration `%#D'", decl);
4629	  return decl;
4630	}
4631    }
4632
4633  if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4634    {
4635      if (old && TREE_CODE (old) != OVERLOAD)
4636	new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4637      else
4638	new_binding = ovl_cons (decl, old);
4639      if (flags & PUSH_USING)
4640	OVL_USED (new_binding) = 1;
4641    }
4642  else
4643    /* NAME is not ambiguous.  */
4644    new_binding = decl;
4645
4646  if (doing_global)
4647    set_namespace_binding (name, current_namespace, new_binding);
4648  else
4649    {
4650      /* We only create an OVERLOAD if there was a previous binding at
4651	 this level, or if decl is a template. In the former case, we
4652	 need to remove the old binding and replace it with the new
4653	 binding.  We must also run through the NAMES on the binding
4654	 level where the name was bound to update the chain.  */
4655
4656      if (TREE_CODE (new_binding) == OVERLOAD && old)
4657	{
4658	  tree *d;
4659
4660	  for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4661	       *d;
4662	       d = &TREE_CHAIN (*d))
4663	    if (*d == old
4664		|| (TREE_CODE (*d) == TREE_LIST
4665		    && TREE_VALUE (*d) == old))
4666	      {
4667		if (TREE_CODE (*d) == TREE_LIST)
4668		  /* Just replace the old binding with the new.  */
4669		  TREE_VALUE (*d) = new_binding;
4670		else
4671		  /* Build a TREE_LIST to wrap the OVERLOAD.  */
4672		  *d = build_tree_list (NULL_TREE, new_binding);
4673
4674		/* And update the CPLUS_BINDING node.  */
4675		BINDING_VALUE (IDENTIFIER_BINDING (name))
4676		  = new_binding;
4677		return decl;
4678	      }
4679
4680	  /* We should always find a previous binding in this case.  */
4681	  my_friendly_abort (0);
4682	}
4683
4684      /* Install the new binding.  */
4685      push_local_binding (name, new_binding, flags);
4686    }
4687
4688  return decl;
4689}
4690
4691/* Generate an implicit declaration for identifier FUNCTIONID
4692   as a function of type int ().  Print a warning if appropriate.  */
4693
4694tree
4695implicitly_declare (functionid)
4696     tree functionid;
4697{
4698  register tree decl;
4699  int temp = allocation_temporary_p ();
4700
4701  push_obstacks_nochange ();
4702
4703  /* Save the decl permanently so we can warn if definition follows.
4704     In ANSI C, warn_implicit is usually false, so the saves little space.
4705     But in C++, it's usually true, hence the extra code.  */
4706  if (temp && (! warn_implicit || toplevel_bindings_p ()))
4707    end_temporary_allocation ();
4708
4709  /* We used to reuse an old implicit decl here,
4710     but this loses with inline functions because it can clobber
4711     the saved decl chains.  */
4712  decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4713
4714  DECL_EXTERNAL (decl) = 1;
4715  TREE_PUBLIC (decl) = 1;
4716
4717  /* ANSI standard says implicit declarations are in the innermost block.
4718     So we record the decl in the standard fashion.  */
4719  pushdecl (decl);
4720  rest_of_decl_compilation (decl, NULL_PTR, 0, 0);
4721
4722  if (warn_implicit
4723      /* Only one warning per identifier.  */
4724      && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4725    {
4726      cp_pedwarn ("implicit declaration of function `%#D'", decl);
4727    }
4728
4729  SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4730
4731  pop_obstacks ();
4732
4733  return decl;
4734}
4735
4736/* Return zero if the declaration NEWDECL is valid
4737   when the declaration OLDDECL (assumed to be for the same name)
4738   has already been seen.
4739   Otherwise return an error message format string with a %s
4740   where the identifier should go.  */
4741
4742static const char *
4743redeclaration_error_message (newdecl, olddecl)
4744     tree newdecl, olddecl;
4745{
4746  if (TREE_CODE (newdecl) == TYPE_DECL)
4747    {
4748      /* Because C++ can put things into name space for free,
4749	 constructs like "typedef struct foo { ... } foo"
4750	 would look like an erroneous redeclaration.  */
4751      if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4752	return 0;
4753      else
4754	return "redefinition of `%#D'";
4755    }
4756  else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4757    {
4758      /* If this is a pure function, its olddecl will actually be
4759	 the original initialization to `0' (which we force to call
4760	 abort()).  Don't complain about redefinition in this case.  */
4761      if (DECL_LANG_SPECIFIC (olddecl) && DECL_ABSTRACT_VIRTUAL_P (olddecl))
4762	return 0;
4763
4764      /* If both functions come from different namespaces, this is not
4765	 a redeclaration - this is a conflict with a used function. */
4766      if (DECL_NAMESPACE_SCOPE_P (olddecl)
4767	  && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4768	return "`%D' conflicts with used function";
4769
4770      /* We'll complain about linkage mismatches in
4771         warn_extern_redeclared_static.  */
4772
4773      /* Defining the same name twice is no good.  */
4774      if (DECL_INITIAL (olddecl) != NULL_TREE
4775	  && DECL_INITIAL (newdecl) != NULL_TREE)
4776	{
4777	  if (DECL_NAME (olddecl) == NULL_TREE)
4778	    return "`%#D' not declared in class";
4779	  else
4780	    return "redefinition of `%#D'";
4781	}
4782      return 0;
4783    }
4784  else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4785    {
4786      if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4787	   && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4788	   && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4789	  || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4790	      && TYPE_SIZE (TREE_TYPE (newdecl))
4791	      && TYPE_SIZE (TREE_TYPE (olddecl))))
4792	return "redefinition of `%#D'";
4793      return 0;
4794    }
4795  else if (toplevel_bindings_p ())
4796    {
4797      /* Objects declared at top level:  */
4798      /* If at least one is a reference, it's ok.  */
4799      if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4800	return 0;
4801      /* Reject two definitions.  */
4802      return "redefinition of `%#D'";
4803    }
4804  else
4805    {
4806      /* Objects declared with block scope:  */
4807      /* Reject two definitions, and reject a definition
4808	 together with an external reference.  */
4809      if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4810	return "redeclaration of `%#D'";
4811      return 0;
4812    }
4813}
4814
4815/* Get the LABEL_DECL corresponding to identifier ID as a label.
4816   Create one if none exists so far for the current function.
4817   This function is called for both label definitions and label references.  */
4818
4819tree
4820lookup_label (id)
4821     tree id;
4822{
4823  register tree decl = IDENTIFIER_LABEL_VALUE (id);
4824
4825  if (current_function_decl == NULL_TREE)
4826    {
4827      error ("label `%s' referenced outside of any function",
4828	     IDENTIFIER_POINTER (id));
4829      return NULL_TREE;
4830    }
4831
4832  if ((decl == NULL_TREE
4833      || DECL_SOURCE_LINE (decl) == 0)
4834      && (named_label_uses == NULL
4835	  || named_label_uses->names_in_scope != current_binding_level->names
4836	  || named_label_uses->label_decl != decl))
4837    {
4838      struct named_label_list *new_ent;
4839      new_ent
4840	= (struct named_label_list*)oballoc (sizeof (struct named_label_list));
4841      new_ent->label_decl = decl;
4842      new_ent->names_in_scope = current_binding_level->names;
4843      new_ent->binding_level = current_binding_level;
4844      new_ent->lineno_o_goto = lineno;
4845      new_ent->filename_o_goto = input_filename;
4846      new_ent->next = named_label_uses;
4847      named_label_uses = new_ent;
4848    }
4849
4850  /* Use a label already defined or ref'd with this name.  */
4851  if (decl != NULL_TREE)
4852    {
4853      /* But not if it is inherited and wasn't declared to be inheritable.  */
4854      if (DECL_CONTEXT (decl) != current_function_decl
4855	  && ! C_DECLARED_LABEL_FLAG (decl))
4856	return shadow_label (id);
4857      return decl;
4858    }
4859
4860  decl = build_decl (LABEL_DECL, id, void_type_node);
4861
4862  /* Make sure every label has an rtx.  */
4863  label_rtx (decl);
4864
4865  /* A label not explicitly declared must be local to where it's ref'd.  */
4866  DECL_CONTEXT (decl) = current_function_decl;
4867
4868  DECL_MODE (decl) = VOIDmode;
4869
4870  /* Say where one reference is to the label,
4871     for the sake of the error if it is not defined.  */
4872  DECL_SOURCE_LINE (decl) = lineno;
4873  DECL_SOURCE_FILE (decl) = input_filename;
4874
4875  SET_IDENTIFIER_LABEL_VALUE (id, decl);
4876
4877  named_labels = tree_cons (NULL_TREE, decl, named_labels);
4878  named_label_uses->label_decl = decl;
4879
4880  return decl;
4881}
4882
4883/* Make a label named NAME in the current function,
4884   shadowing silently any that may be inherited from containing functions
4885   or containing scopes.
4886
4887   Note that valid use, if the label being shadowed
4888   comes from another scope in the same function,
4889   requires calling declare_nonlocal_label right away.  */
4890
4891tree
4892shadow_label (name)
4893     tree name;
4894{
4895  register tree decl = IDENTIFIER_LABEL_VALUE (name);
4896
4897  if (decl != NULL_TREE)
4898    {
4899      shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4900      SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4901    }
4902
4903  return lookup_label (name);
4904}
4905
4906/* Define a label, specifying the location in the source file.
4907   Return the LABEL_DECL node for the label, if the definition is valid.
4908   Otherwise return 0.  */
4909
4910tree
4911define_label (filename, line, name)
4912     char *filename;
4913     int line;
4914     tree name;
4915{
4916  tree decl;
4917
4918  if (minimal_parse_mode)
4919    {
4920      push_obstacks (&permanent_obstack, &permanent_obstack);
4921      decl = build_decl (LABEL_DECL, name, void_type_node);
4922      pop_obstacks ();
4923      DECL_SOURCE_LINE (decl) = line;
4924      DECL_SOURCE_FILE (decl) = filename;
4925      add_tree (decl);
4926      return decl;
4927    }
4928
4929  decl = lookup_label (name);
4930
4931  /* After labels, make any new cleanups go into their
4932     own new (temporary) binding contour.  */
4933  current_binding_level->more_cleanups_ok = 0;
4934
4935  /* If label with this name is known from an outer context, shadow it.  */
4936  if (decl != NULL_TREE && DECL_CONTEXT (decl) != current_function_decl)
4937    {
4938      shadowed_labels = tree_cons (NULL_TREE, decl, shadowed_labels);
4939      SET_IDENTIFIER_LABEL_VALUE (name, NULL_TREE);
4940      decl = lookup_label (name);
4941    }
4942
4943  if (name == get_identifier ("wchar_t"))
4944    cp_pedwarn ("label named wchar_t");
4945
4946  if (DECL_INITIAL (decl) != NULL_TREE)
4947    {
4948      cp_error ("duplicate label `%D'", decl);
4949      return 0;
4950    }
4951  else
4952    {
4953      struct named_label_list *uses, *prev;
4954      int identified = 0;
4955
4956      /* Mark label as having been defined.  */
4957      DECL_INITIAL (decl) = error_mark_node;
4958      /* Say where in the source.  */
4959      DECL_SOURCE_FILE (decl) = filename;
4960      DECL_SOURCE_LINE (decl) = line;
4961
4962      prev = NULL;
4963      uses = named_label_uses;
4964      while (uses != NULL)
4965	if (uses->label_decl == decl)
4966	  {
4967	    struct binding_level *b = current_binding_level;
4968	    while (b)
4969	      {
4970		tree new_decls = b->names;
4971		tree old_decls = (b == uses->binding_level)
4972				  ? uses->names_in_scope : NULL_TREE;
4973		while (new_decls != old_decls)
4974		  {
4975		    if (TREE_CODE (new_decls) == VAR_DECL
4976			/* Don't complain about crossing initialization
4977			   of internal entities.  They can't be accessed,
4978			   and they should be cleaned up
4979			   by the time we get to the label.  */
4980			&& ! DECL_ARTIFICIAL (new_decls)
4981			&& !(DECL_INITIAL (new_decls) == NULL_TREE
4982			     && pod_type_p (TREE_TYPE (new_decls))))
4983		      {
4984			/* This is really only important if we're crossing
4985			   an initialization.  The POD stuff is just
4986			   pedantry; why should it matter if the class
4987			   contains a field of pointer to member type?  */
4988			int problem = (DECL_INITIAL (new_decls)
4989				       || (TYPE_NEEDS_CONSTRUCTING
4990					   (TREE_TYPE (new_decls))));
4991
4992			if (! identified)
4993			  {
4994			    if (problem)
4995			      {
4996				cp_error ("jump to label `%D'", decl);
4997				error_with_file_and_line
4998				  (uses->filename_o_goto,
4999				   uses->lineno_o_goto, "  from here");
5000			      }
5001			    else
5002			      {
5003				cp_pedwarn ("jump to label `%D'", decl);
5004				pedwarn_with_file_and_line
5005				  (uses->filename_o_goto,
5006				   uses->lineno_o_goto, "  from here");
5007			      }
5008			    identified = 1;
5009			}
5010
5011			if (problem)
5012			  cp_error_at ("  crosses initialization of `%#D'",
5013				       new_decls);
5014			else
5015			  cp_pedwarn_at ("  enters scope of non-POD `%#D'",
5016					 new_decls);
5017		      }
5018		    new_decls = TREE_CHAIN (new_decls);
5019		  }
5020		if (b == uses->binding_level)
5021		  break;
5022		b = b->level_chain;
5023	      }
5024
5025	    if (prev != NULL)
5026	      prev->next = uses->next;
5027	    else
5028	      named_label_uses = uses->next;
5029
5030	    uses = uses->next;
5031	  }
5032	else
5033	  {
5034	    prev = uses;
5035	    uses = uses->next;
5036	  }
5037      current_function_return_value = NULL_TREE;
5038      return decl;
5039    }
5040}
5041
5042struct cp_switch
5043{
5044  struct binding_level *level;
5045  struct cp_switch *next;
5046};
5047
5048static struct cp_switch *switch_stack;
5049
5050void
5051push_switch ()
5052{
5053  struct cp_switch *p
5054    = (struct cp_switch *) oballoc (sizeof (struct cp_switch));
5055  p->level = current_binding_level;
5056  p->next = switch_stack;
5057  switch_stack = p;
5058}
5059
5060void
5061pop_switch ()
5062{
5063  switch_stack = switch_stack->next;
5064}
5065
5066/* Same, but for CASE labels.  If DECL is NULL_TREE, it's the default.  */
5067/* XXX Note decl is never actually used. (bpk) */
5068
5069void
5070define_case_label ()
5071{
5072  tree cleanup = last_cleanup_this_contour ();
5073  struct binding_level *b = current_binding_level;
5074  int identified = 0;
5075
5076  if (cleanup)
5077    {
5078      static int explained = 0;
5079      cp_warning_at ("destructor needed for `%#D'", TREE_PURPOSE (cleanup));
5080      warning ("where case label appears here");
5081      if (!explained)
5082	{
5083	  warning ("(enclose actions of previous case statements requiring");
5084	  warning ("destructors in their own binding contours.)");
5085	  explained = 1;
5086	}
5087    }
5088
5089  for (; b && b != switch_stack->level; b = b->level_chain)
5090    {
5091      tree new_decls = b->names;
5092      for (; new_decls; new_decls = TREE_CHAIN (new_decls))
5093	{
5094	  if (TREE_CODE (new_decls) == VAR_DECL
5095	      /* Don't complain about crossing initialization
5096		 of internal entities.  They can't be accessed,
5097		 and they should be cleaned up
5098		 by the time we get to the label.  */
5099	      && ! DECL_ARTIFICIAL (new_decls)
5100	      && ((DECL_INITIAL (new_decls) != NULL_TREE
5101		   && DECL_INITIAL (new_decls) != error_mark_node)
5102		  || TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (new_decls))))
5103	    {
5104	      if (! identified)
5105		error ("jump to case label");
5106	      identified = 1;
5107	      cp_error_at ("  crosses initialization of `%#D'",
5108			   new_decls);
5109	    }
5110	}
5111    }
5112
5113  /* After labels, make any new cleanups go into their
5114     own new (temporary) binding contour.  */
5115
5116  current_binding_level->more_cleanups_ok = 0;
5117  current_function_return_value = NULL_TREE;
5118}
5119
5120/* Return the list of declarations of the current level.
5121   Note that this list is in reverse order unless/until
5122   you nreverse it; and when you do nreverse it, you must
5123   store the result back using `storedecls' or you will lose.  */
5124
5125tree
5126getdecls ()
5127{
5128  return current_binding_level->names;
5129}
5130
5131/* Return the list of type-tags (for structs, etc) of the current level.  */
5132
5133tree
5134gettags ()
5135{
5136  return current_binding_level->tags;
5137}
5138
5139/* Store the list of declarations of the current level.
5140   This is done for the parameter declarations of a function being defined,
5141   after they are modified in the light of any missing parameters.  */
5142
5143static void
5144storedecls (decls)
5145     tree decls;
5146{
5147  current_binding_level->names = decls;
5148}
5149
5150/* Similarly, store the list of tags of the current level.  */
5151
5152void
5153storetags (tags)
5154     tree tags;
5155{
5156  current_binding_level->tags = tags;
5157}
5158
5159/* Given NAME, an IDENTIFIER_NODE,
5160   return the structure (or union or enum) definition for that name.
5161   Searches binding levels from BINDING_LEVEL up to the global level.
5162   If THISLEVEL_ONLY is nonzero, searches only the specified context
5163   (but skips any tag-transparent contexts to find one that is
5164   meaningful for tags).
5165   FORM says which kind of type the caller wants;
5166   it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5167   If the wrong kind of type is found, and it's not a template, an error is
5168   reported.  */
5169
5170static tree
5171lookup_tag (form, name, binding_level, thislevel_only)
5172     enum tree_code form;
5173     tree name;
5174     struct binding_level *binding_level;
5175     int thislevel_only;
5176{
5177  register struct binding_level *level;
5178  /* Non-zero if, we should look past a pseudo-global level, even if
5179     THISLEVEL_ONLY.  */
5180  int allow_pseudo_global = 1;
5181
5182  for (level = binding_level; level; level = level->level_chain)
5183    {
5184      register tree tail;
5185      if (ANON_AGGRNAME_P (name))
5186	for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5187	  {
5188	    /* There's no need for error checking here, because
5189	       anon names are unique throughout the compilation.  */
5190	    if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5191	      return TREE_VALUE (tail);
5192	  }
5193      else if (level->namespace_p)
5194	/* Do namespace lookup. */
5195	for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5196	  {
5197	    tree old = binding_for_name (name, tail);
5198
5199	    /* If we just skipped past a pseudo global level, even
5200	       though THISLEVEL_ONLY, and we find a template class
5201	       declaration, then we use the _TYPE node for the
5202	       template.  See the example below.  */
5203	    if (thislevel_only && !allow_pseudo_global
5204		&& old && BINDING_VALUE (old)
5205		&& DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5206	      old = TREE_TYPE (BINDING_VALUE (old));
5207	    else
5208	      old = BINDING_TYPE (old);
5209
5210	    /* If it has an original type, it is a typedef, and we
5211	       should not return it.  */
5212	    if (old && DECL_ORIGINAL_TYPE (TYPE_NAME (old)))
5213	      old = NULL_TREE;
5214	    if (old && TREE_CODE (old) != form
5215		&& !(form != ENUMERAL_TYPE && TREE_CODE (old) == TEMPLATE_DECL))
5216	      {
5217		cp_error ("`%#D' redeclared as %C", old, form);
5218		return NULL_TREE;
5219	      }
5220	    if (old)
5221	      return old;
5222	    if (thislevel_only || tail == global_namespace)
5223	      return NULL_TREE;
5224	  }
5225      else
5226	for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5227	  {
5228	    if (TREE_PURPOSE (tail) == name)
5229	      {
5230		enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5231		/* Should tighten this up; it'll probably permit
5232		   UNION_TYPE and a struct template, for example.  */
5233		if (code != form
5234		    && !(form != ENUMERAL_TYPE && code == TEMPLATE_DECL))
5235		  {
5236		    /* Definition isn't the kind we were looking for.  */
5237		    cp_error ("`%#D' redeclared as %C", TREE_VALUE (tail),
5238			      form);
5239		    return NULL_TREE;
5240		  }
5241		return TREE_VALUE (tail);
5242	      }
5243	  }
5244      if (thislevel_only && ! level->tag_transparent)
5245	{
5246	  if (level->pseudo_global && allow_pseudo_global)
5247	    {
5248	      /* We must deal with cases like this:
5249
5250	           template <class T> struct S;
5251		   template <class T> struct S {};
5252
5253		 When looking up `S', for the second declaration, we
5254		 would like to find the first declaration.  But, we
5255		 are in the pseudo-global level created for the
5256		 template parameters, rather than the (surrounding)
5257		 namespace level.  Thus, we keep going one more level,
5258		 even though THISLEVEL_ONLY is non-zero.  */
5259	      allow_pseudo_global = 0;
5260	      continue;
5261	    }
5262	  else
5263	    return NULL_TREE;
5264	}
5265      if (current_class_type && level->level_chain->namespace_p)
5266	{
5267	  /* Try looking in this class's tags before heading into
5268	     global binding level.  */
5269	  tree context = current_class_type;
5270	  while (context)
5271	    {
5272	      switch (TREE_CODE_CLASS (TREE_CODE (context)))
5273		{
5274		tree these_tags;
5275		case 't':
5276		    these_tags = CLASSTYPE_TAGS (context);
5277		    if (ANON_AGGRNAME_P (name))
5278		      while (these_tags)
5279			{
5280			  if (TYPE_IDENTIFIER (TREE_VALUE (these_tags))
5281			      == name)
5282			    return TREE_VALUE (tail);
5283			  these_tags = TREE_CHAIN (these_tags);
5284			}
5285		    else
5286		      while (these_tags)
5287			{
5288			  if (TREE_PURPOSE (these_tags) == name)
5289			    {
5290			      if (TREE_CODE (TREE_VALUE (these_tags)) != form)
5291				{
5292				  cp_error ("`%#D' redeclared as %C in class scope",
5293					    TREE_VALUE (tail), form);
5294				  return NULL_TREE;
5295				}
5296			      return TREE_VALUE (tail);
5297			    }
5298			  these_tags = TREE_CHAIN (these_tags);
5299			}
5300		    /* If this type is not yet complete, then don't
5301		       look at its context.  */
5302		    if (TYPE_SIZE (context) == NULL_TREE)
5303		      goto no_context;
5304		    /* Go to next enclosing type, if any.  */
5305		    context = DECL_CONTEXT (TYPE_MAIN_DECL (context));
5306		    break;
5307	        case 'd':
5308		    context = DECL_CONTEXT (context);
5309		    break;
5310	        default:
5311		    my_friendly_abort (10);
5312		}
5313	      continue;
5314	      no_context:
5315	      break;
5316	    }
5317	}
5318    }
5319  return NULL_TREE;
5320}
5321
5322#if 0
5323void
5324set_current_level_tags_transparency (tags_transparent)
5325     int tags_transparent;
5326{
5327  current_binding_level->tag_transparent = tags_transparent;
5328}
5329#endif
5330
5331/* Given a type, find the tag that was defined for it and return the tag name.
5332   Otherwise return 0.  However, the value can never be 0
5333   in the cases in which this is used.
5334
5335   C++: If NAME is non-zero, this is the new name to install.  This is
5336   done when replacing anonymous tags with real tag names.  */
5337
5338static tree
5339lookup_tag_reverse (type, name)
5340     tree type;
5341     tree name;
5342{
5343  register struct binding_level *level;
5344
5345  for (level = current_binding_level; level; level = level->level_chain)
5346    {
5347      register tree tail;
5348      for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5349	{
5350	  if (TREE_VALUE (tail) == type)
5351	    {
5352	      if (name)
5353		TREE_PURPOSE (tail) = name;
5354	      return TREE_PURPOSE (tail);
5355	    }
5356	}
5357    }
5358  return NULL_TREE;
5359}
5360
5361/* Lookup TYPE in CONTEXT (a chain of nested types or a FUNCTION_DECL).
5362   Return the type value, or NULL_TREE if not found.  */
5363
5364static tree
5365lookup_nested_type (type, context)
5366     tree type;
5367     tree context;
5368{
5369  if (context == NULL_TREE)
5370    return NULL_TREE;
5371  while (context)
5372    {
5373      switch (TREE_CODE (context))
5374	{
5375	case TYPE_DECL:
5376	  {
5377	    tree ctype = TREE_TYPE (context);
5378	    tree match = value_member (type, CLASSTYPE_TAGS (ctype));
5379	    if (match)
5380	      return TREE_VALUE (match);
5381	    context = DECL_CONTEXT (context);
5382
5383	    /* When we have a nested class whose member functions have
5384	       local types (e.g., a set of enums), we'll arrive here
5385	       with the DECL_CONTEXT as the actual RECORD_TYPE node for
5386	       the enclosing class.  Instead, we want to make sure we
5387	       come back in here with the TYPE_DECL, not the RECORD_TYPE.  */
5388	    if (context && TREE_CODE (context) == RECORD_TYPE)
5389	      context = TREE_CHAIN (context);
5390	  }
5391	  break;
5392	case FUNCTION_DECL:
5393	  if (TYPE_NAME (type) && TYPE_IDENTIFIER (type))
5394	    return lookup_name (TYPE_IDENTIFIER (type), 1);
5395	  return NULL_TREE;
5396	default:
5397	  my_friendly_abort (12);
5398	}
5399    }
5400  return NULL_TREE;
5401}
5402
5403/* Look up NAME in the NAMESPACE.  */
5404
5405tree
5406lookup_namespace_name (namespace, name)
5407     tree namespace, name;
5408{
5409  struct tree_binding _b;
5410  tree val;
5411
5412  my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5413
5414  if (TREE_CODE (name) == NAMESPACE_DECL)
5415    /* This happens for A::B<int> when B is a namespace. */
5416    return name;
5417  else if (TREE_CODE (name) == TEMPLATE_DECL)
5418    {
5419      /* This happens for A::B where B is a template, and there are no
5420	 template arguments.  */
5421      cp_error ("invalid use of `%D'", name);
5422      return error_mark_node;
5423    }
5424
5425  namespace = ORIGINAL_NAMESPACE (namespace);
5426
5427  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5428
5429  val = binding_init (&_b);
5430  if (!qualified_lookup_using_namespace (name, namespace, val, 0))
5431    return error_mark_node;
5432
5433  if (BINDING_VALUE (val))
5434    {
5435      val = BINDING_VALUE (val);
5436
5437      /* If we have a single function from a using decl, pull it out.  */
5438      if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5439	val = OVL_FUNCTION (val);
5440      return val;
5441    }
5442
5443  cp_error ("`%D' undeclared in namespace `%D'", name, namespace);
5444  return error_mark_node;
5445}
5446
5447/* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5448
5449static unsigned long
5450typename_hash (k)
5451     hash_table_key k;
5452{
5453  unsigned long hash;
5454  tree t;
5455
5456  t = (tree) k;
5457  hash = (((unsigned long) TYPE_CONTEXT (t))
5458	  ^ ((unsigned long) DECL_NAME (TYPE_NAME (t))));
5459
5460  return hash;
5461}
5462
5463/* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5464
5465static boolean
5466typename_compare (k1, k2)
5467     hash_table_key k1;
5468     hash_table_key k2;
5469{
5470  tree t1;
5471  tree t2;
5472  tree d1;
5473  tree d2;
5474
5475  t1 = (tree) k1;
5476  t2 = (tree) k2;
5477  d1 = TYPE_NAME (t1);
5478  d2 = TYPE_NAME (t2);
5479
5480  return (DECL_NAME (d1) == DECL_NAME (d2)
5481	  && same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2))
5482	  && ((TREE_TYPE (t1) != NULL_TREE)
5483	      == (TREE_TYPE (t2) != NULL_TREE))
5484	  && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5485	  && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5486}
5487
5488/* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5489   the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5490   is non-NULL, this type is being created by the implicit typename
5491   extension, and BASE_TYPE is a type named `t' in some base class of
5492   `T' which depends on template parameters.
5493
5494   Returns the new TYPENAME_TYPE.  */
5495
5496tree
5497build_typename_type (context, name, fullname, base_type)
5498     tree context;
5499     tree name;
5500     tree fullname;
5501     tree base_type;
5502{
5503  tree t;
5504  tree d;
5505  struct hash_entry* e;
5506
5507  static struct hash_table ht;
5508
5509  push_obstacks (&permanent_obstack, &permanent_obstack);
5510
5511  if (!ht.table
5512      && !hash_table_init (&ht, &hash_newfunc, &typename_hash,
5513			   &typename_compare))
5514    fatal ("virtual memory exhausted");
5515
5516  /* The FULLNAME needs to exist for the life of the hash table, i.e.,
5517     for the entire compilation.  */
5518  if (!TREE_PERMANENT (fullname))
5519    fullname = copy_to_permanent (fullname);
5520
5521  /* Build the TYPENAME_TYPE.  */
5522  t = make_lang_type (TYPENAME_TYPE);
5523  TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5524  TYPENAME_TYPE_FULLNAME (t) = fullname;
5525  TREE_TYPE (t) = base_type;
5526
5527  /* Build the corresponding TYPE_DECL.  */
5528  d = build_decl (TYPE_DECL, name, t);
5529  TYPE_NAME (TREE_TYPE (d)) = d;
5530  TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5531  DECL_CONTEXT (d) = FROB_CONTEXT (context);
5532  DECL_ARTIFICIAL (d) = 1;
5533
5534  /* See if we already have this type.  */
5535  e = hash_lookup (&ht, t, /*create=*/false, /*copy=*/0);
5536  if (e)
5537    {
5538      /* This will free not only TREE_TYPE, but the lang-specific data
5539	 and the TYPE_DECL as well.  */
5540      obstack_free (&permanent_obstack, t);
5541      t = (tree) e->key;
5542    }
5543  else
5544    /* Insert the type into the table.  */
5545    hash_lookup (&ht, t, /*create=*/true, /*copy=*/0);
5546
5547  pop_obstacks ();
5548
5549  return t;
5550}
5551
5552tree
5553make_typename_type (context, name)
5554     tree context, name;
5555{
5556  tree t;
5557  tree fullname;
5558
5559  if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
5560    {
5561      if (!(TYPE_LANG_SPECIFIC (name)
5562	    && (CLASSTYPE_IS_TEMPLATE (name)
5563		|| CLASSTYPE_USE_TEMPLATE (name))))
5564	name = TYPE_IDENTIFIER (name);
5565      else
5566	/* Create a TEMPLATE_ID_EXPR for the type.  */
5567	name = build_nt (TEMPLATE_ID_EXPR,
5568			 CLASSTYPE_TI_TEMPLATE (name),
5569			 CLASSTYPE_TI_ARGS (name));
5570    }
5571  else if (TREE_CODE (name) == TYPE_DECL)
5572    name = DECL_NAME (name);
5573
5574  fullname = name;
5575
5576  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5577    {
5578      name = TREE_OPERAND (name, 0);
5579      if (TREE_CODE (name) == TEMPLATE_DECL)
5580	name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5581    }
5582  if (TREE_CODE (name) != IDENTIFIER_NODE)
5583    my_friendly_abort (2000);
5584
5585  if (TREE_CODE (context) == NAMESPACE_DECL)
5586    {
5587      /* We can get here from typename_sub0 in the explicit_template_type
5588	 expansion.  Just fail.  */
5589      cp_error ("no class template named `%#T' in `%#T'",
5590		name, context);
5591      return error_mark_node;
5592    }
5593
5594  if (! uses_template_parms (context)
5595      || currently_open_class (context))
5596    {
5597      if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5598	{
5599	  tree tmpl = NULL_TREE;
5600	  if (IS_AGGR_TYPE (context))
5601	    tmpl = lookup_field (context, name, 0, 0);
5602	  if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5603	    {
5604	      cp_error ("no class template named `%#T' in `%#T'",
5605			name, context);
5606	      return error_mark_node;
5607	    }
5608
5609	  return lookup_template_class (tmpl,
5610					TREE_OPERAND (fullname, 1),
5611					NULL_TREE, context,
5612					/*entering_scope=*/0);
5613	}
5614      else
5615	{
5616	  if (IS_AGGR_TYPE (context))
5617	    t = lookup_field (context, name, 0, 1);
5618	  else
5619	    {
5620	      cp_error ("no type named `%#T' in `%#T'", name, context);
5621	      return error_mark_node;
5622	    }
5623
5624	  if (t)
5625	    return TREE_TYPE (t);
5626	}
5627    }
5628
5629  /* If the CONTEXT is not a template type, then either the field is
5630     there now or its never going to be.  */
5631  if (!uses_template_parms (context) && !t)
5632    {
5633      cp_error ("no type named `%#T' in `%#T'", name, context);
5634      return error_mark_node;
5635    }
5636
5637
5638  return build_typename_type (context, name, fullname,  NULL_TREE);
5639}
5640
5641/* Select the right _DECL from multiple choices. */
5642
5643static tree
5644select_decl (binding, flags)
5645     tree binding;
5646     int flags;
5647{
5648  tree val;
5649  val = BINDING_VALUE (binding);
5650  if (LOOKUP_NAMESPACES_ONLY (flags))
5651    {
5652      /* We are not interested in types. */
5653      if (val && TREE_CODE (val) == NAMESPACE_DECL)
5654        return val;
5655      return NULL_TREE;
5656    }
5657
5658  /* If we could have a type and
5659     we have nothing or we need a type and have none.  */
5660  if (BINDING_TYPE (binding)
5661      && (!val || ((flags & LOOKUP_PREFER_TYPES)
5662                   && TREE_CODE (val) != TYPE_DECL)))
5663    val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5664  /* Don't return non-types if we really prefer types. */
5665  else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5666	   && (TREE_CODE (val) != TEMPLATE_DECL
5667	       || !DECL_CLASS_TEMPLATE_P (val)))
5668    val = NULL_TREE;
5669
5670  return val;
5671}
5672
5673/* Unscoped lookup of a global, iterate over namespaces, considering
5674   using namespace statements. */
5675
5676static tree
5677unqualified_namespace_lookup (name, flags)
5678     tree name;
5679     int flags;
5680{
5681  struct tree_binding _binding;
5682  tree b = binding_init (&_binding);
5683  tree initial = current_decl_namespace();
5684  tree scope = initial;
5685  tree siter;
5686  struct binding_level *level;
5687  tree val = NULL_TREE;
5688
5689  while (!val)
5690    {
5691      val = binding_for_name (name, scope);
5692
5693      /* Initialize binding for this context. */
5694      BINDING_VALUE (b) = BINDING_VALUE (val);
5695      BINDING_TYPE (b) = BINDING_TYPE (val);
5696
5697      /* Add all _DECLs seen through local using-directives. */
5698      for (level = current_binding_level;
5699	   !level->namespace_p;
5700	   level = level->level_chain)
5701	if (!lookup_using_namespace (name, b, level->using_directives,
5702                                     scope, flags))
5703	  /* Give up because of error. */
5704	  return error_mark_node;
5705
5706      /* Add all _DECLs seen through global using-directives. */
5707      /* XXX local and global using lists should work equally. */
5708      siter = initial;
5709      while (1)
5710	{
5711	  if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
5712				       scope, flags))
5713	    /* Give up because of error. */
5714	    return error_mark_node;
5715	  if (siter == scope) break;
5716	  siter = CP_DECL_CONTEXT (siter);
5717	}
5718
5719      val = select_decl (b, flags);
5720      if (scope == global_namespace)
5721	break;
5722      scope = CP_DECL_CONTEXT (scope);
5723    }
5724  return val;
5725}
5726
5727/* Combine prefer_type and namespaces_only into flags.  */
5728
5729static int
5730lookup_flags (prefer_type, namespaces_only)
5731  int prefer_type, namespaces_only;
5732{
5733  if (namespaces_only)
5734    return LOOKUP_PREFER_NAMESPACES;
5735  if (prefer_type > 1)
5736    return LOOKUP_PREFER_TYPES;
5737  if (prefer_type > 0)
5738    return LOOKUP_PREFER_BOTH;
5739  return 0;
5740}
5741
5742/* Given a lookup that returned VAL, use FLAGS to decide if we want to
5743   ignore it or not.  Subroutine of lookup_name_real.  */
5744
5745static tree
5746qualify_lookup (val, flags)
5747     tree val;
5748     int flags;
5749{
5750  if (val == NULL_TREE)
5751    return val;
5752  if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5753    return val;
5754  if ((flags & LOOKUP_PREFER_TYPES)
5755      && (TREE_CODE (val) == TYPE_DECL
5756	  || ((flags & LOOKUP_TEMPLATES_EXPECTED)
5757	      && DECL_CLASS_TEMPLATE_P (val))))
5758    return val;
5759  if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5760    return NULL_TREE;
5761  return val;
5762}
5763
5764/* Any other BINDING overrides an implicit TYPENAME.  Warn about
5765   that.  */
5766
5767static void
5768warn_about_implicit_typename_lookup (typename, binding)
5769     tree typename;
5770     tree binding;
5771{
5772  tree subtype = TREE_TYPE (TREE_TYPE (typename));
5773  tree name = DECL_NAME (typename);
5774
5775  if (! (TREE_CODE (binding) == TEMPLATE_DECL
5776	 && CLASSTYPE_TEMPLATE_INFO (subtype)
5777	 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5778      && ! (TREE_CODE (binding) == TYPE_DECL
5779	    && same_type_p (TREE_TYPE (binding), subtype)))
5780    {
5781      cp_warning ("lookup of `%D' finds `%#D'",
5782		  name, binding);
5783      cp_warning ("  instead of `%D' from dependent base class",
5784		  typename);
5785      cp_warning ("  (use `typename %T::%D' if that's what you meant)",
5786		  constructor_name (current_class_type), name);
5787    }
5788}
5789
5790/* Look up NAME in the current binding level and its superiors in the
5791   namespace of variables, functions and typedefs.  Return a ..._DECL
5792   node of some kind representing its definition if there is only one
5793   such declaration, or return a TREE_LIST with all the overloaded
5794   definitions if there are many, or return 0 if it is undefined.
5795
5796   If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5797   If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5798   If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
5799   Otherwise we prefer non-TYPE_DECLs.
5800
5801   If NONCLASS is non-zero, we don't look for the NAME in class scope,
5802   using IDENTIFIER_CLASS_VALUE.  */
5803
5804static tree
5805lookup_name_real (name, prefer_type, nonclass, namespaces_only)
5806     tree name;
5807     int prefer_type, nonclass, namespaces_only;
5808{
5809  tree t;
5810  tree val = NULL_TREE;
5811  int yylex = 0;
5812  tree from_obj = NULL_TREE;
5813  int flags;
5814  int val_is_implicit_typename = 0;
5815
5816  /* Hack: copy flag set by parser, if set. */
5817  if (only_namespace_names)
5818    namespaces_only = 1;
5819
5820  if (prefer_type == -2)
5821    {
5822      extern int looking_for_typename;
5823      tree type = NULL_TREE;
5824
5825      yylex = 1;
5826      prefer_type = looking_for_typename;
5827
5828      flags = lookup_flags (prefer_type, namespaces_only);
5829      /* If the next thing is '<', class templates are types. */
5830      if (looking_for_template)
5831        flags |= LOOKUP_TEMPLATES_EXPECTED;
5832
5833      /* std:: becomes :: for now.  */
5834      if (got_scope == std_node)
5835	got_scope = void_type_node;
5836
5837      if (got_scope)
5838	type = got_scope;
5839      else if (got_object != error_mark_node)
5840	type = got_object;
5841
5842      if (type)
5843	{
5844	  if (type == error_mark_node)
5845	    return error_mark_node;
5846	  if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
5847	    type = TREE_TYPE (type);
5848
5849	  if (TYPE_P (type))
5850	    type = complete_type (type);
5851
5852	  if (TREE_CODE (type) == VOID_TYPE)
5853	    type = global_namespace;
5854	  if (TREE_CODE (type) == NAMESPACE_DECL)
5855	    {
5856	      struct tree_binding b;
5857	      val = binding_init (&b);
5858	      flags |= LOOKUP_COMPLAIN;
5859	      if (!qualified_lookup_using_namespace (name, type, val, flags))
5860		return NULL_TREE;
5861	      val = select_decl (val, flags);
5862	    }
5863	  else if (! IS_AGGR_TYPE (type)
5864		   || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5865		   || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
5866		   || TREE_CODE (type) == TYPENAME_TYPE)
5867	    /* Someone else will give an error about this if needed.  */
5868	    val = NULL_TREE;
5869	  else if (type == current_class_type)
5870	    val = IDENTIFIER_CLASS_VALUE (name);
5871	  else
5872	    val = lookup_member (type, name, 0, prefer_type);
5873	}
5874      else
5875	val = NULL_TREE;
5876
5877      if (got_scope)
5878	goto done;
5879      else if (got_object && val)
5880	from_obj = val;
5881    }
5882  else
5883    {
5884      flags = lookup_flags (prefer_type, namespaces_only);
5885      /* If we're not parsing, we need to complain. */
5886      flags |= LOOKUP_COMPLAIN;
5887    }
5888
5889  /* First, look in non-namespace scopes.  */
5890  for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
5891    {
5892      tree binding;
5893
5894      if (!LOCAL_BINDING_P (t) && nonclass)
5895	/* We're not looking for class-scoped bindings, so keep going.  */
5896	continue;
5897
5898      /* If this is the kind of thing we're looking for, we're done.  */
5899      if (qualify_lookup (BINDING_VALUE (t), flags))
5900	binding = BINDING_VALUE (t);
5901      else if ((flags & LOOKUP_PREFER_TYPES)
5902	       && qualify_lookup (BINDING_TYPE (t), flags))
5903	binding = BINDING_TYPE (t);
5904      else
5905	binding = NULL_TREE;
5906
5907      if (binding
5908	  && (!val || !(TREE_CODE (binding) == TYPE_DECL
5909			&& IMPLICIT_TYPENAME_P (TREE_TYPE (binding)))))
5910	{
5911	  if (val_is_implicit_typename && !yylex)
5912	    warn_about_implicit_typename_lookup (val, binding);
5913	  val = binding;
5914	  val_is_implicit_typename
5915	    = (TREE_CODE (val) == TYPE_DECL
5916	       && IMPLICIT_TYPENAME_P (TREE_TYPE (val)));
5917	  if (!val_is_implicit_typename)
5918	    break;
5919	}
5920    }
5921
5922  /* Now lookup in namespace scopes.  */
5923  if (!val || val_is_implicit_typename)
5924    {
5925      t = unqualified_namespace_lookup (name, flags);
5926      if (t)
5927	{
5928	  if (val_is_implicit_typename && !yylex)
5929	    warn_about_implicit_typename_lookup (val, t);
5930	  val = t;
5931	}
5932    }
5933
5934 done:
5935  if (val)
5936    {
5937      /* This should only warn about types used in qualified-ids.  */
5938      if (from_obj && from_obj != val)
5939	{
5940	  if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
5941	      && TREE_CODE (val) == TYPE_DECL
5942	      && TREE_TYPE (from_obj) != TREE_TYPE (val))
5943	    {
5944	      cp_pedwarn ("lookup of `%D' in the scope of `%#T' (`%#T')",
5945			  name, got_object, TREE_TYPE (from_obj));
5946	      cp_pedwarn ("  does not match lookup in the current scope (`%#T')",
5947			  TREE_TYPE (val));
5948	    }
5949
5950	  /* We don't change val to from_obj if got_object depends on
5951	     template parms because that breaks implicit typename for
5952	     destructor calls.  */
5953	  if (! uses_template_parms (got_object))
5954	    val = from_obj;
5955	}
5956
5957      /* If we have a single function from a using decl, pull it out.  */
5958      if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5959	val = OVL_FUNCTION (val);
5960    }
5961  else if (from_obj)
5962    val = from_obj;
5963
5964  return val;
5965}
5966
5967tree
5968lookup_name_nonclass (name)
5969     tree name;
5970{
5971  return lookup_name_real (name, 0, 1, 0);
5972}
5973
5974tree
5975lookup_function_nonclass (name, args)
5976     tree name;
5977     tree args;
5978{
5979  return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5980}
5981
5982tree
5983lookup_name_namespace_only (name)
5984     tree name;
5985{
5986  /* type-or-namespace, nonclass, namespace_only */
5987  return lookup_name_real (name, 1, 1, 1);
5988}
5989
5990tree
5991lookup_name (name, prefer_type)
5992     tree name;
5993     int prefer_type;
5994{
5995  return lookup_name_real (name, prefer_type, 0, 0);
5996}
5997
5998/* Similar to `lookup_name' but look only in the innermost non-class
5999   binding level.  */
6000
6001tree
6002lookup_name_current_level (name)
6003     tree name;
6004{
6005  struct binding_level *b;
6006  tree t = NULL_TREE;
6007
6008  b = current_binding_level;
6009  while (b->parm_flag == 2)
6010    b = b->level_chain;
6011
6012  if (b->namespace_p)
6013    {
6014      t =  IDENTIFIER_NAMESPACE_VALUE (name);
6015
6016      /* extern "C" function() */
6017      if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6018	t = TREE_VALUE (t);
6019    }
6020  else if (IDENTIFIER_BINDING (name)
6021	   && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6022    {
6023      while (1)
6024	{
6025	  if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6026	    return IDENTIFIER_VALUE (name);
6027
6028	  if (b->keep == 2)
6029	    b = b->level_chain;
6030	  else
6031	    break;
6032	}
6033    }
6034
6035  return t;
6036}
6037
6038/* Like lookup_name_current_level, but for types.  */
6039
6040tree
6041lookup_type_current_level (name)
6042     tree name;
6043{
6044  register tree t = NULL_TREE;
6045
6046  my_friendly_assert (! current_binding_level->namespace_p, 980716);
6047
6048  if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6049      && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6050    {
6051      struct binding_level *b = current_binding_level;
6052      while (1)
6053	{
6054	  if (purpose_member (name, b->type_shadowed))
6055	    return REAL_IDENTIFIER_TYPE_VALUE (name);
6056	  if (b->keep == 2)
6057	    b = b->level_chain;
6058	  else
6059	    break;
6060	}
6061    }
6062
6063  return t;
6064}
6065
6066void
6067begin_only_namespace_names ()
6068{
6069  only_namespace_names = 1;
6070}
6071
6072void
6073end_only_namespace_names ()
6074{
6075  only_namespace_names = 0;
6076}
6077
6078/* Arrange for the user to get a source line number, even when the
6079   compiler is going down in flames, so that she at least has a
6080   chance of working around problems in the compiler.  We used to
6081   call error(), but that let the segmentation fault continue
6082   through; now, it's much more passive by asking them to send the
6083   maintainers mail about the problem.  */
6084
6085static void
6086signal_catch (sig)
6087     int sig ATTRIBUTE_UNUSED;
6088{
6089  signal (SIGSEGV, SIG_DFL);
6090#ifdef SIGIOT
6091  signal (SIGIOT, SIG_DFL);
6092#endif
6093#ifdef SIGILL
6094  signal (SIGILL, SIG_DFL);
6095#endif
6096#ifdef SIGABRT
6097  signal (SIGABRT, SIG_DFL);
6098#endif
6099#ifdef SIGBUS
6100  signal (SIGBUS, SIG_DFL);
6101#endif
6102  my_friendly_abort (0);
6103}
6104
6105#if 0
6106/* Unused -- brendan 970107 */
6107/* Array for holding types considered "built-in".  These types
6108   are output in the module in which `main' is defined.  */
6109static tree *builtin_type_tdescs_arr;
6110static int builtin_type_tdescs_len, builtin_type_tdescs_max;
6111#endif
6112
6113/* Push the declarations of builtin types into the namespace.
6114   RID_INDEX, if < RID_MAX is the index of the builtin type
6115   in the array RID_POINTERS.  NAME is the name used when looking
6116   up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6117
6118static void
6119record_builtin_type (rid_index, name, type)
6120     enum rid rid_index;
6121     const char *name;
6122     tree type;
6123{
6124  tree rname = NULL_TREE, tname = NULL_TREE;
6125  tree tdecl = NULL_TREE;
6126
6127  if ((int) rid_index < (int) RID_MAX)
6128    rname = ridpointers[(int) rid_index];
6129  if (name)
6130    tname = get_identifier (name);
6131
6132  TYPE_BUILT_IN (type) = 1;
6133
6134  if (tname)
6135    {
6136      tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6137      set_identifier_type_value (tname, NULL_TREE);
6138      if ((int) rid_index < (int) RID_MAX)
6139	/* Built-in types live in the global namespace. */
6140	SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6141    }
6142  if (rname != NULL_TREE)
6143    {
6144      if (tname != NULL_TREE)
6145	{
6146	  set_identifier_type_value (rname, NULL_TREE);
6147	  SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6148	}
6149      else
6150	{
6151	  tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6152	  set_identifier_type_value (rname, NULL_TREE);
6153	}
6154    }
6155}
6156
6157/* Record one of the standard Java types.
6158 * Declare it as having the given NAME.
6159 * If SIZE > 0, it is the size of one of the integral types;
6160 * otherwise it is the negative of the size of one of the other types.  */
6161
6162static tree
6163record_builtin_java_type (name, size)
6164     const char *name;
6165     int size;
6166{
6167  tree type, decl;
6168  if (size > 0)
6169    type = make_signed_type (size);
6170  else if (size > -32)
6171    { /* "__java_char" or ""__java_boolean". */
6172      type = make_unsigned_type (-size);
6173      /*if (size == -1)	TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6174    }
6175  else
6176    { /* "__java_float" or ""__java_double". */
6177      type = make_node (REAL_TYPE);
6178      TYPE_PRECISION (type) = - size;
6179      layout_type (type);
6180    }
6181  record_builtin_type (RID_MAX, name, type);
6182  decl = TYPE_NAME (type);
6183
6184  /* Suppress generate debug symbol entries for these types,
6185     since for normal C++ they are just clutter.
6186     However, push_lang_context undoes this if extern "Java" is seen. */
6187  DECL_IGNORED_P (decl) = 1;
6188
6189  TYPE_FOR_JAVA (type) = 1;
6190  return type;
6191}
6192
6193/* Push a type into the namespace so that the back-ends ignore it. */
6194
6195static void
6196record_unknown_type (type, name)
6197     tree type;
6198     const char *name;
6199{
6200  tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6201  /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6202  DECL_IGNORED_P (decl) = 1;
6203  TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6204  TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6205  TYPE_ALIGN (type) = 1;
6206  TYPE_MODE (type) = TYPE_MODE (void_type_node);
6207}
6208
6209/* Push overloaded decl, in global scope, with one argument so it
6210   can be used as a callback from define_function.  */
6211
6212static void
6213push_overloaded_decl_1 (x)
6214     tree x;
6215{
6216  push_overloaded_decl (x, PUSH_GLOBAL);
6217}
6218
6219#ifdef __GNUC__
6220__inline
6221#endif
6222tree
6223auto_function (name, type, code)
6224     tree name, type;
6225     enum built_in_function code;
6226{
6227  return define_function
6228    (IDENTIFIER_POINTER (name), type, code, push_overloaded_decl_1,
6229     IDENTIFIER_POINTER (build_decl_overload (name, TYPE_ARG_TYPES (type),
6230					      0)));
6231}
6232
6233/* Create the predefined scalar types of C,
6234   and some nodes representing standard constants (0, 1, (void *)0).
6235   Initialize the global binding level.
6236   Make definitions for built-in primitive functions.  */
6237
6238void
6239init_decl_processing ()
6240{
6241  register tree endlink, int_endlink, double_endlink, unsigned_endlink;
6242  tree fields[20];
6243  /* Data type of memcpy.  */
6244  tree memcpy_ftype, strlen_ftype;
6245  int wchar_type_size;
6246  tree temp;
6247  tree array_domain_type;
6248  tree vb_off_identifier = NULL_TREE;
6249  /* Function type `char *(char *, char *)' and similar ones */
6250  tree string_ftype_ptr_ptr, int_ftype_string_string;
6251  tree sizetype_endlink;
6252  tree ptr_ftype, ptr_ftype_unsigned, ptr_ftype_sizetype;
6253  tree void_ftype, void_ftype_int, void_ftype_ptr;
6254
6255  /* Have to make these distinct before we try using them.  */
6256  lang_name_cplusplus = get_identifier ("C++");
6257  lang_name_c = get_identifier ("C");
6258  lang_name_java = get_identifier ("Java");
6259
6260  /* Enter the global namespace. */
6261  my_friendly_assert (global_namespace == NULL_TREE, 375);
6262  my_friendly_assert (current_lang_name == NULL_TREE, 375);
6263  current_lang_name = lang_name_cplusplus;
6264  push_namespace (get_identifier ("::"));
6265  global_namespace = current_namespace;
6266  current_lang_name = NULL_TREE;
6267
6268  if (flag_strict_prototype == 2)
6269    flag_strict_prototype = pedantic;
6270  if (! flag_permissive && ! pedantic)
6271    flag_pedantic_errors = 1;
6272
6273  strict_prototypes_lang_c = flag_strict_prototype;
6274
6275  /* Initially, C.  */
6276  current_lang_name = lang_name_c;
6277
6278  current_function_decl = NULL_TREE;
6279  named_labels = NULL_TREE;
6280  named_label_uses = NULL;
6281  current_binding_level = NULL_BINDING_LEVEL;
6282  free_binding_level = NULL_BINDING_LEVEL;
6283
6284  /* Because most segmentation signals can be traced back into user
6285     code, catch them and at least give the user a chance of working
6286     around compiler bugs.  */
6287  signal (SIGSEGV, signal_catch);
6288
6289  /* We will also catch aborts in the back-end through signal_catch and
6290     give the user a chance to see where the error might be, and to defeat
6291     aborts in the back-end when there have been errors previously in their
6292     code.  */
6293#ifdef SIGIOT
6294  signal (SIGIOT, signal_catch);
6295#endif
6296#ifdef SIGILL
6297  signal (SIGILL, signal_catch);
6298#endif
6299#ifdef SIGABRT
6300  signal (SIGABRT, signal_catch);
6301#endif
6302#ifdef SIGBUS
6303  signal (SIGBUS, signal_catch);
6304#endif
6305
6306  gcc_obstack_init (&decl_obstack);
6307
6308  /* Must lay these out before anything else gets laid out.  */
6309  error_mark_node = make_node (ERROR_MARK);
6310  TREE_PERMANENT (error_mark_node) = 1;
6311  TREE_TYPE (error_mark_node) = error_mark_node;
6312  error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6313  TREE_TYPE (error_mark_list) = error_mark_node;
6314
6315  /* Make the binding_level structure for global names.  */
6316  pushlevel (0);
6317  global_binding_level = current_binding_level;
6318  /* The global level is the namespace level of ::.  */
6319  NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6320  declare_namespace_level ();
6321
6322  this_identifier = get_identifier (THIS_NAME);
6323  in_charge_identifier = get_identifier (IN_CHARGE_NAME);
6324  vlist_identifier = get_identifier (VLIST_NAME);
6325  ctor_identifier = get_identifier (CTOR_NAME);
6326  dtor_identifier = get_identifier (DTOR_NAME);
6327  pfn_identifier = get_identifier (VTABLE_PFN_NAME);
6328  index_identifier = get_identifier (VTABLE_INDEX_NAME);
6329  delta_identifier = get_identifier (VTABLE_DELTA_NAME);
6330  delta2_identifier = get_identifier (VTABLE_DELTA2_NAME);
6331  pfn_or_delta2_identifier = get_identifier ("__pfn_or_delta2");
6332  if (flag_handle_signatures)
6333    {
6334      tag_identifier = get_identifier (SIGTABLE_TAG_NAME);
6335      vb_off_identifier = get_identifier (SIGTABLE_VB_OFF_NAME);
6336      vt_off_identifier = get_identifier (SIGTABLE_VT_OFF_NAME);
6337    }
6338
6339  /* Define `int' and `char' first so that dbx will output them first.  */
6340
6341  integer_type_node = make_signed_type (INT_TYPE_SIZE);
6342  record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
6343
6344  /* Define `char', which is like either `signed char' or `unsigned char'
6345     but not the same as either.  */
6346
6347  char_type_node
6348    = (flag_signed_char
6349       ? make_signed_type (CHAR_TYPE_SIZE)
6350       : make_unsigned_type (CHAR_TYPE_SIZE));
6351  record_builtin_type (RID_CHAR, "char", char_type_node);
6352
6353  /* `signed' is the same as `int' */
6354  record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
6355
6356  long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
6357  record_builtin_type (RID_LONG, "long int", long_integer_type_node);
6358
6359  unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
6360  record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
6361
6362  long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
6363  record_builtin_type (RID_MAX, "long unsigned int", long_unsigned_type_node);
6364  record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
6365
6366  long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
6367  record_builtin_type (RID_MAX, "long long int", long_long_integer_type_node);
6368
6369  long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
6370  record_builtin_type (RID_MAX, "long long unsigned int",
6371		       long_long_unsigned_type_node);
6372  record_builtin_type (RID_MAX, "long long unsigned",
6373		       long_long_unsigned_type_node);
6374
6375  short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
6376  record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
6377  short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
6378  record_builtin_type (RID_MAX, "short unsigned int", short_unsigned_type_node);
6379  record_builtin_type (RID_MAX, "unsigned short", short_unsigned_type_node);
6380
6381  /* `unsigned long' is the standard type for sizeof.
6382     Note that stddef.h uses `unsigned long',
6383     and this must agree, even if long and int are the same size.  */
6384  set_sizetype
6385    (TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (SIZE_TYPE))));
6386
6387  ptrdiff_type_node
6388    = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (PTRDIFF_TYPE)));
6389
6390  /* Define both `signed char' and `unsigned char'.  */
6391  signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
6392  record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
6393  unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
6394  record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
6395
6396  /* These are types that type_for_size and type_for_mode use.  */
6397  intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
6398  pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
6399  intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
6400  pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
6401  intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
6402  pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
6403  intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
6404  pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
6405#if HOST_BITS_PER_WIDE_INT >= 64
6406  intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
6407  pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
6408#endif
6409  unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
6410  pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
6411  unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
6412  pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
6413  unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
6414  pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
6415  unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
6416  pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
6417#if HOST_BITS_PER_WIDE_INT >= 64
6418  unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
6419  pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
6420#endif
6421
6422  float_type_node = make_node (REAL_TYPE);
6423  TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
6424  record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
6425  layout_type (float_type_node);
6426
6427  double_type_node = make_node (REAL_TYPE);
6428  if (flag_short_double)
6429    TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
6430  else
6431    TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
6432  record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
6433  layout_type (double_type_node);
6434
6435  long_double_type_node = make_node (REAL_TYPE);
6436  TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
6437  record_builtin_type (RID_MAX, "long double", long_double_type_node);
6438  layout_type (long_double_type_node);
6439
6440  complex_integer_type_node = make_node (COMPLEX_TYPE);
6441  pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
6442			complex_integer_type_node));
6443  TREE_TYPE (complex_integer_type_node) = integer_type_node;
6444  layout_type (complex_integer_type_node);
6445
6446  complex_float_type_node = make_node (COMPLEX_TYPE);
6447  pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
6448			complex_float_type_node));
6449  TREE_TYPE (complex_float_type_node) = float_type_node;
6450  layout_type (complex_float_type_node);
6451
6452  complex_double_type_node = make_node (COMPLEX_TYPE);
6453  pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
6454			complex_double_type_node));
6455  TREE_TYPE (complex_double_type_node) = double_type_node;
6456  layout_type (complex_double_type_node);
6457
6458  complex_long_double_type_node = make_node (COMPLEX_TYPE);
6459  pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
6460			complex_long_double_type_node));
6461  TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
6462  layout_type (complex_long_double_type_node);
6463
6464  java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6465  java_short_type_node = record_builtin_java_type ("__java_short", 16);
6466  java_int_type_node = record_builtin_java_type ("__java_int", 32);
6467  java_long_type_node = record_builtin_java_type ("__java_long", 64);
6468  java_float_type_node = record_builtin_java_type ("__java_float", -32);
6469  java_double_type_node = record_builtin_java_type ("__java_double", -64);
6470  java_char_type_node = record_builtin_java_type ("__java_char", -16);
6471  java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6472
6473  integer_zero_node = build_int_2 (0, 0);
6474  TREE_TYPE (integer_zero_node) = integer_type_node;
6475  integer_one_node = build_int_2 (1, 0);
6476  TREE_TYPE (integer_one_node) = integer_type_node;
6477  integer_two_node = build_int_2 (2, 0);
6478  TREE_TYPE (integer_two_node) = integer_type_node;
6479  integer_three_node = build_int_2 (3, 0);
6480  TREE_TYPE (integer_three_node) = integer_type_node;
6481
6482  boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6483  TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6484  TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6485  TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6486  TYPE_PRECISION (boolean_type_node) = 1;
6487  record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6488  boolean_false_node = build_int_2 (0, 0);
6489  TREE_TYPE (boolean_false_node) = boolean_type_node;
6490  boolean_true_node = build_int_2 (1, 0);
6491  TREE_TYPE (boolean_true_node) = boolean_type_node;
6492
6493  /* These are needed by stor-layout.c.  */
6494  size_zero_node = size_int (0);
6495  size_one_node = size_int (1);
6496
6497  signed_size_zero_node = build_int_2 (0, 0);
6498  TREE_TYPE (signed_size_zero_node) = make_signed_type (TYPE_PRECISION (sizetype));
6499
6500  void_type_node = make_node (VOID_TYPE);
6501  record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
6502  layout_type (void_type_node); /* Uses integer_zero_node.  */
6503  void_list_node = build_tree_list (NULL_TREE, void_type_node);
6504  TREE_PARMLIST (void_list_node) = 1;
6505
6506  null_pointer_node = build_int_2 (0, 0);
6507  TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
6508  layout_type (TREE_TYPE (null_pointer_node));
6509
6510  /* Used for expressions that do nothing, but are not errors.  */
6511  void_zero_node = build_int_2 (0, 0);
6512  TREE_TYPE (void_zero_node) = void_type_node;
6513
6514  string_type_node = build_pointer_type (char_type_node);
6515  const_string_type_node
6516    = build_pointer_type (build_qualified_type (char_type_node,
6517						TYPE_QUAL_CONST));
6518#if 0
6519  record_builtin_type (RID_MAX, NULL_PTR, string_type_node);
6520#endif
6521
6522  /* Make a type to be the domain of a few array types
6523     whose domains don't really matter.
6524     200 is small enough that it always fits in size_t
6525     and large enough that it can hold most function names for the
6526     initializations of __FUNCTION__ and __PRETTY_FUNCTION__.  */
6527  array_domain_type = build_index_type (build_int_2 (200, 0));
6528
6529  /* Make a type for arrays of characters.
6530     With luck nothing will ever really depend on the length of this
6531     array type.  */
6532  char_array_type_node
6533    = build_array_type (char_type_node, array_domain_type);
6534  /* Likewise for arrays of ints.  */
6535  int_array_type_node
6536    = build_array_type (integer_type_node, array_domain_type);
6537
6538  /* This is just some anonymous class type.  Nobody should ever
6539     need to look inside this envelope.  */
6540  class_star_type_node = build_pointer_type (make_lang_type (RECORD_TYPE));
6541
6542  default_function_type
6543    = build_function_type (integer_type_node, NULL_TREE);
6544
6545  ptr_type_node = build_pointer_type (void_type_node);
6546  const_ptr_type_node
6547    = build_pointer_type (build_qualified_type (void_type_node,
6548						TYPE_QUAL_CONST));
6549#if 0
6550  record_builtin_type (RID_MAX, NULL_PTR, ptr_type_node);
6551#endif
6552
6553  endlink = void_list_node;
6554  int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
6555  double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
6556  unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
6557
6558  ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
6559  ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
6560  sizetype_endlink = tree_cons (NULL_TREE, sizetype, endlink);
6561  /* We realloc here because sizetype could be int or unsigned.  S'ok.  */
6562  ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
6563
6564  void_ftype = build_function_type (void_type_node, endlink);
6565  void_ftype_int = build_function_type (void_type_node, int_endlink);
6566  void_ftype_ptr
6567    = build_function_type (void_type_node,
6568 			   tree_cons (NULL_TREE, ptr_type_node, endlink));
6569  void_ftype_ptr
6570    = build_exception_variant (void_ftype_ptr,
6571			       tree_cons (NULL_TREE, NULL_TREE, NULL_TREE));
6572
6573  float_ftype_float
6574    = build_function_type (float_type_node,
6575			   tree_cons (NULL_TREE, float_type_node, endlink));
6576
6577  double_ftype_double
6578    = build_function_type (double_type_node, double_endlink);
6579
6580  ldouble_ftype_ldouble
6581    = build_function_type (long_double_type_node,
6582			   tree_cons (NULL_TREE, long_double_type_node,
6583				      endlink));
6584
6585  double_ftype_double_double
6586    = build_function_type (double_type_node,
6587			   tree_cons (NULL_TREE, double_type_node,
6588				      double_endlink));
6589
6590  int_ftype_int
6591    = build_function_type (integer_type_node, int_endlink);
6592
6593  long_ftype_long
6594    = build_function_type (long_integer_type_node,
6595			   tree_cons (NULL_TREE, long_integer_type_node,
6596				      endlink));
6597
6598  int_ftype_cptr_cptr_sizet
6599    = build_function_type (integer_type_node,
6600			   tree_cons (NULL_TREE, const_ptr_type_node,
6601				      tree_cons (NULL_TREE, const_ptr_type_node,
6602						 tree_cons (NULL_TREE,
6603							    sizetype,
6604							    endlink))));
6605
6606  string_ftype_ptr_ptr		/* strcpy prototype */
6607    = build_function_type (string_type_node,
6608			   tree_cons (NULL_TREE, string_type_node,
6609				      tree_cons (NULL_TREE,
6610						 const_string_type_node,
6611						 endlink)));
6612
6613  int_ftype_string_string	/* strcmp prototype */
6614    = build_function_type (integer_type_node,
6615			   tree_cons (NULL_TREE, const_string_type_node,
6616				      tree_cons (NULL_TREE,
6617						 const_string_type_node,
6618						 endlink)));
6619
6620  strlen_ftype		/* strlen prototype */
6621    = build_function_type (sizetype,
6622			   tree_cons (NULL_TREE, const_string_type_node,
6623				      endlink));
6624
6625  memcpy_ftype	/* memcpy prototype */
6626    = build_function_type (ptr_type_node,
6627			   tree_cons (NULL_TREE, ptr_type_node,
6628				      tree_cons (NULL_TREE, const_ptr_type_node,
6629						 sizetype_endlink)));
6630
6631  if (flag_huge_objects)
6632    delta_type_node = long_integer_type_node;
6633  else
6634    delta_type_node = short_integer_type_node;
6635
6636  builtin_function ("__builtin_constant_p", default_function_type,
6637		    BUILT_IN_CONSTANT_P, NULL_PTR);
6638
6639  builtin_return_address_fndecl
6640    = builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
6641			BUILT_IN_RETURN_ADDRESS, NULL_PTR);
6642
6643  builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
6644		    BUILT_IN_FRAME_ADDRESS, NULL_PTR);
6645
6646  builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
6647		    BUILT_IN_ALLOCA, "alloca");
6648  builtin_function ("__builtin_ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
6649  /* Define alloca, ffs as builtins.
6650     Declare _exit just to mark it as volatile.  */
6651  if (! flag_no_builtin && !flag_no_nonansi_builtin)
6652    {
6653      temp = builtin_function ("alloca", ptr_ftype_sizetype,
6654			       BUILT_IN_ALLOCA, NULL_PTR);
6655      /* Suppress error if redefined as a non-function.  */
6656      DECL_BUILT_IN_NONANSI (temp) = 1;
6657      temp = builtin_function ("ffs", int_ftype_int, BUILT_IN_FFS, NULL_PTR);
6658      /* Suppress error if redefined as a non-function.  */
6659      DECL_BUILT_IN_NONANSI (temp) = 1;
6660      temp = builtin_function ("_exit", void_ftype_int,
6661			       NOT_BUILT_IN, NULL_PTR);
6662      TREE_THIS_VOLATILE (temp) = 1;
6663      TREE_SIDE_EFFECTS (temp) = 1;
6664      /* Suppress error if redefined as a non-function.  */
6665      DECL_BUILT_IN_NONANSI (temp) = 1;
6666    }
6667
6668  builtin_function ("__builtin_abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
6669  builtin_function ("__builtin_fabsf", float_ftype_float, BUILT_IN_FABS,
6670		    NULL_PTR);
6671  builtin_function ("__builtin_fabs", double_ftype_double, BUILT_IN_FABS,
6672		    NULL_PTR);
6673  builtin_function ("__builtin_fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
6674		    NULL_PTR);
6675  builtin_function ("__builtin_labs", long_ftype_long,
6676		    BUILT_IN_LABS, NULL_PTR);
6677  builtin_function ("__builtin_saveregs", ptr_ftype,
6678		    BUILT_IN_SAVEREGS, NULL_PTR);
6679  builtin_function ("__builtin_classify_type", default_function_type,
6680		    BUILT_IN_CLASSIFY_TYPE, NULL_PTR);
6681  builtin_function ("__builtin_next_arg", ptr_ftype,
6682		    BUILT_IN_NEXT_ARG, NULL_PTR);
6683  builtin_function ("__builtin_args_info", int_ftype_int,
6684		    BUILT_IN_ARGS_INFO, NULL_PTR);
6685  builtin_function ("__builtin_setjmp",
6686		    build_function_type (integer_type_node,
6687					 tree_cons (NULL_TREE, ptr_type_node,
6688						    endlink)),
6689		    BUILT_IN_SETJMP, NULL_PTR);
6690  builtin_function ("__builtin_longjmp",
6691		    build_function_type (integer_type_node,
6692					 tree_cons (NULL_TREE, ptr_type_node,
6693						    tree_cons (NULL_TREE,
6694							       integer_type_node,
6695							       endlink))),
6696		    BUILT_IN_LONGJMP, NULL_PTR);
6697
6698  /* Untyped call and return.  */
6699  builtin_function ("__builtin_apply_args", ptr_ftype,
6700		    BUILT_IN_APPLY_ARGS, NULL_PTR);
6701
6702  temp = tree_cons (NULL_TREE,
6703		    build_pointer_type (build_function_type (void_type_node,
6704							     NULL_TREE)),
6705		    tree_cons (NULL_TREE, ptr_ftype_sizetype, NULL_TREE));
6706  builtin_function ("__builtin_apply",
6707		    build_function_type (ptr_type_node, temp),
6708		    BUILT_IN_APPLY, NULL_PTR);
6709  builtin_function ("__builtin_return", void_ftype_ptr,
6710		    BUILT_IN_RETURN, NULL_PTR);
6711
6712  /* Currently under experimentation.  */
6713  builtin_function ("__builtin_memcpy", memcpy_ftype,
6714		    BUILT_IN_MEMCPY, "memcpy");
6715  builtin_function ("__builtin_memcmp", int_ftype_cptr_cptr_sizet,
6716		    BUILT_IN_MEMCMP, "memcmp");
6717  builtin_function ("__builtin_strcmp", int_ftype_string_string,
6718		    BUILT_IN_STRCMP, "strcmp");
6719  builtin_function ("__builtin_strcpy", string_ftype_ptr_ptr,
6720		    BUILT_IN_STRCPY, "strcpy");
6721  builtin_function ("__builtin_strlen", strlen_ftype,
6722		    BUILT_IN_STRLEN, "strlen");
6723  builtin_function ("__builtin_sqrtf", float_ftype_float,
6724		    BUILT_IN_FSQRT, "sqrtf");
6725  builtin_function ("__builtin_fsqrt", double_ftype_double,
6726		    BUILT_IN_FSQRT, NULL_PTR);
6727  builtin_function ("__builtin_sqrtl", ldouble_ftype_ldouble,
6728		    BUILT_IN_FSQRT, "sqrtl");
6729  builtin_function ("__builtin_sinf", float_ftype_float,
6730		    BUILT_IN_SIN, "sinf");
6731  builtin_function ("__builtin_sin", double_ftype_double,
6732		    BUILT_IN_SIN, "sin");
6733  builtin_function ("__builtin_sinl", ldouble_ftype_ldouble,
6734		    BUILT_IN_SIN, "sinl");
6735  builtin_function ("__builtin_cosf", float_ftype_float,
6736		    BUILT_IN_COS, "cosf");
6737  builtin_function ("__builtin_cos", double_ftype_double,
6738		    BUILT_IN_COS, "cos");
6739  builtin_function ("__builtin_cosl", ldouble_ftype_ldouble,
6740		    BUILT_IN_COS, "cosl");
6741
6742  if (!flag_no_builtin)
6743    {
6744      builtin_function ("abs", int_ftype_int, BUILT_IN_ABS, NULL_PTR);
6745      builtin_function ("fabs", double_ftype_double, BUILT_IN_FABS, NULL_PTR);
6746      builtin_function ("labs", long_ftype_long, BUILT_IN_LABS, NULL_PTR);
6747      builtin_function ("fabsf", float_ftype_float, BUILT_IN_FABS, NULL_PTR);
6748      builtin_function ("fabsl", ldouble_ftype_ldouble, BUILT_IN_FABS,
6749			NULL_PTR);
6750      builtin_function ("memcpy", memcpy_ftype, BUILT_IN_MEMCPY, NULL_PTR);
6751      builtin_function ("memcmp", int_ftype_cptr_cptr_sizet, BUILT_IN_MEMCMP,
6752			NULL_PTR);
6753      builtin_function ("strcmp", int_ftype_string_string, BUILT_IN_STRCMP,
6754			NULL_PTR);
6755      builtin_function ("strcpy", string_ftype_ptr_ptr, BUILT_IN_STRCPY,
6756			NULL_PTR);
6757      builtin_function ("strlen", strlen_ftype, BUILT_IN_STRLEN, NULL_PTR);
6758      builtin_function ("sqrtf", float_ftype_float, BUILT_IN_FSQRT, NULL_PTR);
6759      builtin_function ("sqrt", double_ftype_double, BUILT_IN_FSQRT, NULL_PTR);
6760      builtin_function ("sqrtl", ldouble_ftype_ldouble, BUILT_IN_FSQRT,
6761			NULL_PTR);
6762      builtin_function ("sinf", float_ftype_float, BUILT_IN_SIN, NULL_PTR);
6763      builtin_function ("sin", double_ftype_double, BUILT_IN_SIN, NULL_PTR);
6764      builtin_function ("sinl", ldouble_ftype_ldouble, BUILT_IN_SIN, NULL_PTR);
6765      builtin_function ("cosf", float_ftype_float, BUILT_IN_COS, NULL_PTR);
6766      builtin_function ("cos", double_ftype_double, BUILT_IN_COS, NULL_PTR);
6767      builtin_function ("cosl", ldouble_ftype_ldouble, BUILT_IN_COS, NULL_PTR);
6768
6769      /* Declare these functions volatile
6770	 to avoid spurious "control drops through" warnings.  */
6771      temp = builtin_function ("abort", void_ftype,
6772			       NOT_BUILT_IN, NULL_PTR);
6773      TREE_THIS_VOLATILE (temp) = 1;
6774      TREE_SIDE_EFFECTS (temp) = 1;
6775      /* Well, these are actually ANSI, but we can't set DECL_BUILT_IN on
6776         them...  */
6777      DECL_BUILT_IN_NONANSI (temp) = 1;
6778      temp = builtin_function ("exit", void_ftype_int,
6779			       NOT_BUILT_IN, NULL_PTR);
6780      TREE_THIS_VOLATILE (temp) = 1;
6781      TREE_SIDE_EFFECTS (temp) = 1;
6782      DECL_BUILT_IN_NONANSI (temp) = 1;
6783    }
6784
6785#if 0
6786  /* Support for these has not been written in either expand_builtin
6787     or build_function_call.  */
6788  builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV, NULL_PTR);
6789  builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV, NULL_PTR);
6790  builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
6791		    NULL_PTR);
6792  builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
6793		    NULL_PTR);
6794  builtin_function ("__builtin_fmod", double_ftype_double_double,
6795		    BUILT_IN_FMOD, NULL_PTR);
6796  builtin_function ("__builtin_frem", double_ftype_double_double,
6797		    BUILT_IN_FREM, NULL_PTR);
6798  builtin_function ("__builtin_memset", ptr_ftype_ptr_int_int,
6799		    BUILT_IN_MEMSET, NULL_PTR);
6800  builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
6801		    NULL_PTR);
6802  builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
6803		    NULL_PTR);
6804#endif
6805
6806  /* C++ extensions */
6807
6808  unknown_type_node = make_node (UNKNOWN_TYPE);
6809  record_unknown_type (unknown_type_node, "unknown type");
6810
6811  /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6812  TREE_TYPE (unknown_type_node) = unknown_type_node;
6813
6814  TREE_TYPE (null_node) = type_for_size (POINTER_SIZE, 0);
6815
6816  /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6817     result.  */
6818  TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6819  TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6820
6821  /* This is for handling opaque types in signatures.  */
6822  opaque_type_node = copy_node (ptr_type_node);
6823  TYPE_MAIN_VARIANT (opaque_type_node) = opaque_type_node;
6824  record_builtin_type (RID_MAX, 0, opaque_type_node);
6825
6826  /* This is special for C++ so functions can be overloaded.  */
6827  wchar_type_node
6828    = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (get_identifier (WCHAR_TYPE)));
6829  wchar_type_size = TYPE_PRECISION (wchar_type_node);
6830  signed_wchar_type_node = make_signed_type (wchar_type_size);
6831  unsigned_wchar_type_node = make_unsigned_type (wchar_type_size);
6832  wchar_type_node
6833    = TREE_UNSIGNED (wchar_type_node)
6834      ? unsigned_wchar_type_node
6835      : signed_wchar_type_node;
6836  record_builtin_type (RID_WCHAR, "__wchar_t", wchar_type_node);
6837
6838  /* Artificial declaration of wchar_t -- can be bashed */
6839  wchar_decl_node = build_decl (TYPE_DECL, get_identifier ("wchar_t"),
6840				wchar_type_node);
6841  pushdecl (wchar_decl_node);
6842
6843  /* This is for wide string constants.  */
6844  wchar_array_type_node
6845    = build_array_type (wchar_type_node, array_domain_type);
6846
6847  if (flag_vtable_thunks)
6848    {
6849      /* Make sure we get a unique function type, so we can give
6850	 its pointer type a name.  (This wins for gdb.) */
6851      tree vfunc_type = make_node (FUNCTION_TYPE);
6852      TREE_TYPE (vfunc_type) = integer_type_node;
6853      TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6854      layout_type (vfunc_type);
6855
6856      vtable_entry_type = build_pointer_type (vfunc_type);
6857    }
6858  else
6859    {
6860      vtable_entry_type = make_lang_type (RECORD_TYPE);
6861      fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
6862					 delta_type_node);
6863      fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
6864					 delta_type_node);
6865      fields[2] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6866					 ptr_type_node);
6867      finish_builtin_type (vtable_entry_type, VTBL_PTR_TYPE, fields, 2,
6868			   double_type_node);
6869
6870      /* Make this part of an invisible union.  */
6871      fields[3] = copy_node (fields[2]);
6872      TREE_TYPE (fields[3]) = delta_type_node;
6873      DECL_NAME (fields[3]) = delta2_identifier;
6874      DECL_MODE (fields[3]) = TYPE_MODE (delta_type_node);
6875      DECL_SIZE (fields[3]) = TYPE_SIZE (delta_type_node);
6876      TREE_UNSIGNED (fields[3]) = 0;
6877      TREE_CHAIN (fields[2]) = fields[3];
6878      vtable_entry_type = build_qualified_type (vtable_entry_type,
6879						TYPE_QUAL_CONST);
6880    }
6881  record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6882
6883  vtbl_type_node
6884    = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6885  layout_type (vtbl_type_node);
6886  vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6887  record_builtin_type (RID_MAX, NULL_PTR, vtbl_type_node);
6888  vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6889  layout_type (vtbl_ptr_type_node);
6890  record_builtin_type (RID_MAX, NULL_PTR, vtbl_ptr_type_node);
6891
6892  if (flag_vtable_thunks)
6893    {
6894      /* We need vlists only when using thunks; otherwise leave them
6895	 as NULL_TREE. That way, it doesn't get into the way of the
6896	 mangling.  */
6897      vlist_type_node = build_pointer_type (vtbl_ptr_type_node);
6898      vlist_zero_node = build_int_2 (0, 0);
6899      TREE_TYPE (vlist_zero_node) = vlist_type_node;
6900    }
6901
6902  /* Simplify life by making a "sigtable_entry_type".  Give its
6903     fields names so that the debugger can use them.  */
6904
6905  if (flag_handle_signatures)
6906    {
6907      sigtable_entry_type = make_lang_type (RECORD_TYPE);
6908      fields[0] = build_lang_field_decl (FIELD_DECL, tag_identifier,
6909					 delta_type_node);
6910      fields[1] = build_lang_field_decl (FIELD_DECL, vb_off_identifier,
6911					 delta_type_node);
6912      fields[2] = build_lang_field_decl (FIELD_DECL, delta_identifier,
6913					 delta_type_node);
6914      fields[3] = build_lang_field_decl (FIELD_DECL, index_identifier,
6915					 delta_type_node);
6916      fields[4] = build_lang_field_decl (FIELD_DECL, pfn_identifier,
6917					 ptr_type_node);
6918
6919      /* Set the alignment to the max of the alignment of ptr_type_node and
6920	 delta_type_node.  Double alignment wastes a word on the Sparc.  */
6921      finish_builtin_type (sigtable_entry_type, SIGTABLE_PTR_TYPE, fields, 4,
6922			   (TYPE_ALIGN (ptr_type_node) > TYPE_ALIGN (delta_type_node))
6923			   ? ptr_type_node
6924			   : delta_type_node);
6925
6926      /* Make this part of an invisible union.  */
6927      fields[5] = copy_node (fields[4]);
6928      TREE_TYPE (fields[5]) = delta_type_node;
6929      DECL_NAME (fields[5]) = vt_off_identifier;
6930      DECL_MODE (fields[5]) = TYPE_MODE (delta_type_node);
6931      DECL_SIZE (fields[5]) = TYPE_SIZE (delta_type_node);
6932      TREE_UNSIGNED (fields[5]) = 0;
6933      TREE_CHAIN (fields[4]) = fields[5];
6934
6935      sigtable_entry_type = build_qualified_type (sigtable_entry_type,
6936						  TYPE_QUAL_CONST);
6937      record_builtin_type (RID_MAX, SIGTABLE_PTR_TYPE, sigtable_entry_type);
6938    }
6939
6940  std_node = build_decl (NAMESPACE_DECL,
6941			 get_identifier (flag_honor_std ? "fake std":"std"),
6942			 void_type_node);
6943  pushdecl (std_node);
6944
6945  global_type_node = make_node (LANG_TYPE);
6946  record_unknown_type (global_type_node, "global type");
6947
6948  /* Now, C++.  */
6949  current_lang_name = lang_name_cplusplus;
6950
6951  {
6952    tree bad_alloc_type_node, newtype, deltype;
6953    if (flag_honor_std)
6954      push_namespace (get_identifier ("std"));
6955    bad_alloc_type_node = xref_tag
6956      (class_type_node, get_identifier ("bad_alloc"), 1);
6957    if (flag_honor_std)
6958      pop_namespace ();
6959    newtype = build_exception_variant
6960      (ptr_ftype_sizetype, build_tree_list (NULL_TREE, bad_alloc_type_node));
6961    deltype = build_exception_variant
6962      (void_ftype_ptr, build_tree_list (NULL_TREE, NULL_TREE));
6963    auto_function (ansi_opname[(int) NEW_EXPR], newtype, NOT_BUILT_IN);
6964    auto_function (ansi_opname[(int) VEC_NEW_EXPR], newtype, NOT_BUILT_IN);
6965    global_delete_fndecl
6966      = auto_function (ansi_opname[(int) DELETE_EXPR], deltype, NOT_BUILT_IN);
6967    auto_function (ansi_opname[(int) VEC_DELETE_EXPR], deltype, NOT_BUILT_IN);
6968  }
6969
6970  abort_fndecl
6971    = define_function ("__pure_virtual", void_ftype,
6972		       NOT_BUILT_IN, 0, 0);
6973
6974  /* Perform other language dependent initializations.  */
6975  init_class_processing ();
6976  init_init_processing ();
6977  init_search_processing ();
6978  if (flag_rtti)
6979    init_rtti_processing ();
6980
6981  if (flag_exceptions)
6982    init_exception_processing ();
6983  if (flag_no_inline)
6984    {
6985      flag_inline_functions = 0;
6986    }
6987
6988  if (! supports_one_only ())
6989    flag_weak = 0;
6990
6991  /* Create the global bindings for __FUNCTION__ and __PRETTY_FUNCTION__.  */
6992  declare_function_name ();
6993
6994  /* Prepare to check format strings against argument lists.  */
6995  init_function_format_info ();
6996
6997  /* Show we use EH for cleanups.  */
6998  using_eh_for_cleanups ();
6999
7000  print_error_function = lang_print_error_function;
7001  lang_get_alias_set = &c_get_alias_set;
7002  valid_lang_attribute = cp_valid_lang_attribute;
7003
7004  /* Maintain consistency.  Perhaps we should just complain if they
7005     say -fwritable-strings?  */
7006  if (flag_writable_strings)
7007    flag_const_strings = 0;
7008}
7009
7010/* Function to print any language-specific context for an error message.  */
7011
7012static void
7013lang_print_error_function (file)
7014     char *file;
7015{
7016  default_print_error_function (file);
7017  maybe_print_template_context ();
7018}
7019
7020/* Make a definition for a builtin function named NAME and whose data type
7021   is TYPE.  TYPE should be a function type with argument types.
7022   FUNCTION_CODE tells later passes how to compile calls to this function.
7023   See tree.h for its possible values.
7024
7025   If LIBRARY_NAME is nonzero, use that for DECL_ASSEMBLER_NAME,
7026   the name to be called if we can't opencode the function.  */
7027
7028tree
7029define_function (name, type, function_code, pfn, library_name)
7030     const char *name;
7031     tree type;
7032     enum built_in_function function_code;
7033     void (*pfn) PROTO((tree));
7034     const char *library_name;
7035{
7036  tree decl = build_lang_decl (FUNCTION_DECL, get_identifier (name), type);
7037  DECL_EXTERNAL (decl) = 1;
7038  TREE_PUBLIC (decl) = 1;
7039  DECL_ARTIFICIAL (decl) = 1;
7040
7041  my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 392);
7042  DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
7043
7044  /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
7045     we cannot change DECL_ASSEMBLER_NAME until we have installed this
7046     function in the namespace.  */
7047  if (pfn) (*pfn) (decl);
7048  if (library_name)
7049    DECL_ASSEMBLER_NAME (decl) = get_identifier (library_name);
7050  make_function_rtl (decl);
7051  if (function_code != NOT_BUILT_IN)
7052    {
7053      DECL_BUILT_IN (decl) = 1;
7054      DECL_FUNCTION_CODE (decl) = function_code;
7055    }
7056  return decl;
7057}
7058
7059/* When we call finish_struct for an anonymous union, we create
7060   default copy constructors and such.  But, an anonymous union
7061   shouldn't have such things; this function undoes the damage to the
7062   anonymous union type T.
7063
7064   (The reason that we create the synthesized methods is that we don't
7065   distinguish `union { int i; }' from `typedef union { int i; } U'.
7066   The first is an anonymous union; the second is just an ordinary
7067   union type.)  */
7068
7069void
7070fixup_anonymous_aggr (t)
7071     tree t;
7072{
7073  tree *q;
7074
7075  /* Wipe out memory of synthesized methods */
7076  TYPE_HAS_CONSTRUCTOR (t) = 0;
7077  TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
7078  TYPE_HAS_INIT_REF (t) = 0;
7079  TYPE_HAS_CONST_INIT_REF (t) = 0;
7080  TYPE_HAS_ASSIGN_REF (t) = 0;
7081  TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
7082
7083  /* Splice the implicitly generated functions out of the TYPE_METHODS
7084     list.  */
7085  q = &TYPE_METHODS (t);
7086  while (*q)
7087    {
7088      if (DECL_ARTIFICIAL (*q))
7089	*q = TREE_CHAIN (*q);
7090      else
7091	q = &TREE_CHAIN (*q);
7092    }
7093
7094  /* ANSI C++ June 5 1992 WP 9.5.3.  Anonymous unions may not have
7095     function members.  */
7096  if (TYPE_METHODS (t))
7097    error ("an anonymous union cannot have function members");
7098}
7099
7100/* Make sure that a declaration with no declarator is well-formed, i.e.
7101   just defines a tagged type or anonymous union.
7102
7103   Returns the type defined, if any.  */
7104
7105tree
7106check_tag_decl (declspecs)
7107     tree declspecs;
7108{
7109  int found_type = 0;
7110  tree ob_modifier = NULL_TREE;
7111  register tree link;
7112  register tree t = NULL_TREE;
7113
7114  for (link = declspecs; link; link = TREE_CHAIN (link))
7115    {
7116      register tree value = TREE_VALUE (link);
7117
7118      if (TYPE_P (value))
7119	{
7120	  ++found_type;
7121
7122	  if ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
7123	      || TREE_CODE (value) == ENUMERAL_TYPE)
7124	    {
7125	      my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
7126	      t = value;
7127	    }
7128	}
7129      else if (value == ridpointers[(int) RID_FRIEND])
7130	{
7131	  if (current_class_type == NULL_TREE
7132	      || current_scope () != current_class_type)
7133	    ob_modifier = value;
7134	}
7135      else if (value == ridpointers[(int) RID_STATIC]
7136	       || value == ridpointers[(int) RID_EXTERN]
7137	       || value == ridpointers[(int) RID_AUTO]
7138	       || value == ridpointers[(int) RID_REGISTER]
7139	       || value == ridpointers[(int) RID_INLINE]
7140	       || value == ridpointers[(int) RID_VIRTUAL]
7141	       || value == ridpointers[(int) RID_CONST]
7142	       || value == ridpointers[(int) RID_VOLATILE]
7143	       || value == ridpointers[(int) RID_EXPLICIT])
7144	ob_modifier = value;
7145    }
7146
7147  if (found_type > 1)
7148    error ("multiple types in one declaration");
7149
7150  /* Inside a class, we might be in a friend or access declaration.
7151     Until we have a good way of detecting the latter, don't warn.  */
7152  if (t == NULL_TREE && ! current_class_type)
7153    pedwarn ("declaration does not declare anything");
7154
7155  /* Check for an anonymous union.  We're careful
7156     accessing TYPE_IDENTIFIER because some built-in types, like
7157     pointer-to-member types, do not have TYPE_NAME.  */
7158  else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
7159	   && TYPE_NAME (t)
7160	   && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
7161    {
7162      /* Anonymous unions are objects, so they can have specifiers.  */;
7163      SET_ANON_AGGR_TYPE_P (t);
7164
7165      if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
7166	pedwarn ("ISO C++ prohibits anonymous structs");
7167    }
7168
7169  else if (ob_modifier)
7170    {
7171      if (ob_modifier == ridpointers[(int) RID_INLINE]
7172	  || ob_modifier == ridpointers[(int) RID_VIRTUAL])
7173	cp_error ("`%D' can only be specified for functions", ob_modifier);
7174      else if (ob_modifier == ridpointers[(int) RID_FRIEND])
7175	cp_error ("`%D' can only be specified inside a class", ob_modifier);
7176      else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
7177	cp_error ("`%D' can only be specified for constructors",
7178		  ob_modifier);
7179      else
7180	cp_error ("`%D' can only be specified for objects and functions",
7181		  ob_modifier);
7182    }
7183
7184  return t;
7185}
7186
7187/* Called when a declaration is seen that contains no names to declare.
7188   If its type is a reference to a structure, union or enum inherited
7189   from a containing scope, shadow that tag name for the current scope
7190   with a forward reference.
7191   If its type defines a new named structure or union
7192   or defines an enum, it is valid but we need not do anything here.
7193   Otherwise, it is an error.
7194
7195   C++: may have to grok the declspecs to learn about static,
7196   complain for anonymous unions.  */
7197
7198void
7199shadow_tag (declspecs)
7200     tree declspecs;
7201{
7202  tree t = check_tag_decl (declspecs);
7203
7204  if (t)
7205    maybe_process_partial_specialization (t);
7206
7207  /* This is where the variables in an anonymous union are
7208     declared.  An anonymous union declaration looks like:
7209     union { ... } ;
7210     because there is no declarator after the union, the parser
7211     sends that declaration here.  */
7212  if (t && ANON_AGGR_TYPE_P (t))
7213    {
7214      fixup_anonymous_aggr (t);
7215
7216      if (TYPE_FIELDS (t))
7217	{
7218	  tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
7219				      NULL_TREE);
7220	  finish_anon_union (decl);
7221	}
7222    }
7223}
7224
7225/* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
7226
7227tree
7228groktypename (typename)
7229     tree typename;
7230{
7231  if (TREE_CODE (typename) != TREE_LIST)
7232    return typename;
7233  return grokdeclarator (TREE_VALUE (typename),
7234			 TREE_PURPOSE (typename),
7235			 TYPENAME, 0, NULL_TREE);
7236}
7237
7238/* Decode a declarator in an ordinary declaration or data definition.
7239   This is called as soon as the type information and variable name
7240   have been parsed, before parsing the initializer if any.
7241   Here we create the ..._DECL node, fill in its type,
7242   and put it on the list of decls for the current context.
7243   The ..._DECL node is returned as the value.
7244
7245   Exception: for arrays where the length is not specified,
7246   the type is left null, to be filled in by `cp_finish_decl'.
7247
7248   Function definitions do not come here; they go to start_function
7249   instead.  However, external and forward declarations of functions
7250   do go through here.  Structure field declarations are done by
7251   grokfield and not through here.  */
7252
7253/* Set this to zero to debug not using the temporary obstack
7254   to parse initializers.  */
7255int debug_temp_inits = 1;
7256
7257tree
7258start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
7259     tree declarator, declspecs;
7260     int initialized;
7261     tree attributes, prefix_attributes;
7262{
7263  register tree decl;
7264  register tree type, tem;
7265  tree context;
7266  extern int have_extern_spec;
7267  extern int used_extern_spec;
7268  tree attrlist;
7269
7270#if 0
7271  /* See code below that used this.  */
7272  int init_written = initialized;
7273#endif
7274
7275  /* This should only be done once on the top most decl.  */
7276  if (have_extern_spec && !used_extern_spec)
7277    {
7278      declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"),
7279				  declspecs);
7280      used_extern_spec = 1;
7281    }
7282
7283  if (attributes || prefix_attributes)
7284    attrlist = build_scratch_list (attributes, prefix_attributes);
7285  else
7286    attrlist = NULL_TREE;
7287
7288  decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7289			 attrlist);
7290
7291  if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7292    return NULL_TREE;
7293
7294  type = TREE_TYPE (decl);
7295
7296  if (type == error_mark_node)
7297    return NULL_TREE;
7298
7299  /* Don't lose if destructors must be executed at file-level.  */
7300  if (! processing_template_decl && TREE_STATIC (decl)
7301      && TYPE_NEEDS_DESTRUCTOR (complete_type (type))
7302      && !TREE_PERMANENT (decl))
7303    {
7304      push_obstacks (&permanent_obstack, &permanent_obstack);
7305      decl = copy_node (decl);
7306      if (TREE_CODE (type) == ARRAY_TYPE)
7307	{
7308	  tree itype = TYPE_DOMAIN (type);
7309	  if (itype && ! TREE_PERMANENT (itype))
7310	    {
7311	      itype = build_index_type (copy_to_permanent (TYPE_MAX_VALUE (itype)));
7312	      type = build_cplus_array_type (TREE_TYPE (type), itype);
7313	      TREE_TYPE (decl) = type;
7314	    }
7315	}
7316      pop_obstacks ();
7317    }
7318
7319  context
7320    = (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
7321      ? DECL_CLASS_CONTEXT (decl)
7322      : DECL_CONTEXT (decl);
7323
7324  if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7325      && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7326    {
7327      /* When parsing the initializer, lookup should use the object's
7328	 namespace. */
7329      push_decl_namespace (context);
7330    }
7331
7332  /* We are only interested in class contexts, later. */
7333  if (context && TREE_CODE (context) == NAMESPACE_DECL)
7334    context = NULL_TREE;
7335
7336  if (initialized)
7337    /* Is it valid for this decl to have an initializer at all?
7338       If not, set INITIALIZED to zero, which will indirectly
7339       tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7340    switch (TREE_CODE (decl))
7341      {
7342      case TYPE_DECL:
7343	/* typedef foo = bar  means give foo the same type as bar.
7344	   We haven't parsed bar yet, so `cp_finish_decl' will fix that up.
7345	   Any other case of an initialization in a TYPE_DECL is an error.  */
7346	if (pedantic || list_length (declspecs) > 1)
7347	  {
7348	    cp_error ("typedef `%D' is initialized", decl);
7349	    initialized = 0;
7350	  }
7351	break;
7352
7353      case FUNCTION_DECL:
7354	cp_error ("function `%#D' is initialized like a variable", decl);
7355	initialized = 0;
7356	break;
7357
7358      default:
7359	if (! processing_template_decl)
7360	  {
7361	    if (type != error_mark_node)
7362	      {
7363		if (TYPE_SIZE (type) != NULL_TREE
7364		    && ! TREE_CONSTANT (TYPE_SIZE (type)))
7365		  {
7366		    cp_error
7367		      ("variable-sized object `%D' may not be initialized",
7368		       decl);
7369		    initialized = 0;
7370		  }
7371
7372		if (TREE_CODE (type) == ARRAY_TYPE
7373		    && TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
7374		  {
7375		    cp_error
7376		      ("elements of array `%#D' have incomplete type", decl);
7377		    initialized = 0;
7378		  }
7379	      }
7380	  }
7381      }
7382
7383  if (initialized)
7384    {
7385      if (! toplevel_bindings_p ()
7386	  && DECL_EXTERNAL (decl))
7387	cp_warning ("declaration of `%#D' has `extern' and is initialized",
7388		    decl);
7389      DECL_EXTERNAL (decl) = 0;
7390      if (toplevel_bindings_p ())
7391	TREE_STATIC (decl) = 1;
7392
7393      /* Tell `pushdecl' this is an initialized decl
7394	 even though we don't yet have the initializer expression.
7395	 Also tell `cp_finish_decl' it may store the real initializer.  */
7396      DECL_INITIAL (decl) = error_mark_node;
7397    }
7398
7399#ifdef SET_DEFAULT_DECL_ATTRIBUTES
7400  SET_DEFAULT_DECL_ATTRIBUTES (decl, attributes);
7401#endif
7402
7403  /* Set attributes here so if duplicate decl, will have proper attributes.  */
7404  cplus_decl_attributes (decl, attributes, prefix_attributes);
7405
7406  if (context && TYPE_SIZE (complete_type (context)) != NULL_TREE)
7407    {
7408      push_nested_class (context, 2);
7409
7410      if (TREE_CODE (decl) == VAR_DECL)
7411	{
7412	  tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7413	  if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7414	    cp_error ("`%#D' is not a static member of `%#T'", decl, context);
7415	  else
7416	    {
7417	      if (DECL_CONTEXT (field) != context)
7418		{
7419		  cp_pedwarn ("ANSI C++ does not permit `%T::%D' to be defined as `%T::%D'",
7420			      DECL_CONTEXT (field), DECL_NAME (decl),
7421			      context, DECL_NAME (decl));
7422		  DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7423		}
7424	      /* Static data member are tricky; an in-class initialization
7425		 still doesn't provide a definition, so the in-class
7426		 declaration will have DECL_EXTERNAL set, but will have an
7427		 initialization.  Thus, duplicate_decls won't warn
7428		 about this situation, and so we check here.  */
7429	      if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7430		cp_error ("duplicate initialization of %D", decl);
7431	      if (duplicate_decls (decl, field))
7432		decl = field;
7433	    }
7434	}
7435      else
7436	{
7437	  tree field = check_classfn (context, decl);
7438	  if (field && duplicate_decls (decl, field))
7439	    decl = field;
7440	}
7441
7442      /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7443      DECL_IN_AGGR_P (decl) = 0;
7444      if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7445	  || CLASSTYPE_USE_TEMPLATE (context))
7446	{
7447	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7448	  /* [temp.expl.spec] An explicit specialization of a static data
7449	     member of a template is a definition if the declaration
7450	     includes an initializer; otherwise, it is a declaration.
7451
7452	     We check for processing_specialization so this only applies
7453	     to the new specialization syntax.  */
7454	  if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7455	    DECL_EXTERNAL (decl) = 1;
7456	}
7457
7458      if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7459	cp_pedwarn ("declaration of `%#D' outside of class is not definition",
7460		    decl);
7461    }
7462
7463  /* Add this decl to the current binding level, but not if it
7464     comes from another scope, e.g. a static member variable.
7465     TEM may equal DECL or it may be a previous decl of the same name.  */
7466
7467  if ((TREE_CODE (decl) != PARM_DECL && DECL_CONTEXT (decl) != NULL_TREE
7468       /* Definitions of namespace members outside their namespace are
7469	  possible. */
7470       && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
7471      || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
7472      || TREE_CODE (type) == LANG_TYPE
7473      /* The declaration of template specializations does not affect
7474	 the functions available for overload resolution, so we do not
7475	 call pushdecl.  */
7476      || (TREE_CODE (decl) == FUNCTION_DECL
7477	  && DECL_TEMPLATE_SPECIALIZATION (decl)))
7478    tem = decl;
7479  else
7480    tem = pushdecl (decl);
7481
7482  if (processing_template_decl)
7483    {
7484      if (! current_function_decl)
7485	tem = push_template_decl (tem);
7486      else if (minimal_parse_mode)
7487	DECL_VINDEX (tem)
7488	    = build_min_nt (DECL_STMT, copy_to_permanent (declarator),
7489			    copy_to_permanent (declspecs),
7490			    NULL_TREE);
7491    }
7492
7493
7494#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7495  /* Tell the back-end to use or not use .common as appropriate.  If we say
7496     -fconserve-space, we want this to save .data space, at the expense of
7497     wrong semantics.  If we say -fno-conserve-space, we want this to
7498     produce errors about redefs; to do this we force variables into the
7499     data segment.  */
7500  DECL_COMMON (tem) = flag_conserve_space || ! TREE_PUBLIC (tem);
7501#endif
7502
7503  if (! processing_template_decl)
7504    start_decl_1 (tem);
7505
7506  /* Corresponding pop_obstacks is done in `cp_finish_decl'.  */
7507  push_obstacks_nochange ();
7508
7509#if 0
7510  /* We have no way of knowing whether the initializer will need to be
7511     evaluated at run-time or not until we've parsed it, so let's just put
7512     it in the permanent obstack.  (jason) */
7513  if (init_written
7514      && ! (TREE_CODE (tem) == PARM_DECL
7515	    || (TREE_READONLY (tem)
7516		&& (TREE_CODE (tem) == VAR_DECL
7517		    || TREE_CODE (tem) == FIELD_DECL))))
7518    {
7519      /* When parsing and digesting the initializer,
7520	 use temporary storage.  Do this even if we will ignore the value.  */
7521      if (toplevel_bindings_p () && debug_temp_inits)
7522	{
7523	  if (processing_template_decl
7524	      || TYPE_NEEDS_CONSTRUCTING (type)
7525	      || TREE_CODE (type) == REFERENCE_TYPE)
7526	    /* In this case, the initializer must lay down in permanent
7527	       storage, since it will be saved until `finish_file' is run.   */
7528	    ;
7529	  else
7530	    temporary_allocation ();
7531	}
7532    }
7533#endif
7534
7535  return tem;
7536}
7537
7538void
7539start_decl_1 (decl)
7540     tree decl;
7541{
7542  tree type = TREE_TYPE (decl);
7543  int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7544
7545  if (type == error_mark_node)
7546    return;
7547
7548  /* If this type of object needs a cleanup, and control may
7549     jump past it, make a new binding level so that it is cleaned
7550     up only when it is initialized first.  */
7551  if (TYPE_NEEDS_DESTRUCTOR (type)
7552      && current_binding_level->more_cleanups_ok == 0)
7553    pushlevel_temporary (1);
7554
7555  if (initialized)
7556    /* Is it valid for this decl to have an initializer at all?
7557       If not, set INITIALIZED to zero, which will indirectly
7558       tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7559    {
7560      /* Don't allow initializations for incomplete types except for
7561	 arrays which might be completed by the initialization.  */
7562      if (TYPE_SIZE (complete_type (type)) != NULL_TREE)
7563	;			/* A complete type is ok.  */
7564      else if (TREE_CODE (type) != ARRAY_TYPE)
7565	{
7566	  cp_error ("variable `%#D' has initializer but incomplete type",
7567		    decl);
7568	  initialized = 0;
7569	  type = TREE_TYPE (decl) = error_mark_node;
7570	}
7571      else if (TYPE_SIZE (complete_type (TREE_TYPE (type))) == NULL_TREE)
7572	{
7573	  if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7574	    cp_error ("elements of array `%#D' have incomplete type", decl);
7575	  /* else we already gave an error in start_decl.  */
7576	  initialized = 0;
7577	}
7578    }
7579
7580  if (!initialized
7581      && TREE_CODE (decl) != TYPE_DECL
7582      && TREE_CODE (decl) != TEMPLATE_DECL
7583      && IS_AGGR_TYPE (type) && ! DECL_EXTERNAL (decl))
7584    {
7585      if ((! processing_template_decl || ! uses_template_parms (type))
7586	  && TYPE_SIZE (complete_type (type)) == NULL_TREE)
7587	{
7588	  cp_error ("aggregate `%#D' has incomplete type and cannot be initialized",
7589		 decl);
7590	  /* Change the type so that assemble_variable will give
7591	     DECL an rtl we can live with: (mem (const_int 0)).  */
7592	  type = TREE_TYPE (decl) = error_mark_node;
7593	}
7594      else
7595	{
7596	  /* If any base type in the hierarchy of TYPE needs a constructor,
7597	     then we set initialized to 1.  This way any nodes which are
7598	     created for the purposes of initializing this aggregate
7599	     will live as long as it does.  This is necessary for global
7600	     aggregates which do not have their initializers processed until
7601	     the end of the file.  */
7602	  initialized = TYPE_NEEDS_CONSTRUCTING (type);
7603	}
7604    }
7605
7606#if 0
7607  /* We don't do this yet for GNU C++.  */
7608  /* For a local variable, define the RTL now.  */
7609  if (! toplevel_bindings_p ()
7610      /* But not if this is a duplicate decl
7611	 and we preserved the rtl from the previous one
7612	 (which may or may not happen).  */
7613      && DECL_RTL (tem) == NULL_RTX)
7614    {
7615      if (TYPE_SIZE (TREE_TYPE (tem)) != NULL_TREE)
7616	expand_decl (tem);
7617      else if (TREE_CODE (TREE_TYPE (tem)) == ARRAY_TYPE
7618	       && DECL_INITIAL (tem) != NULL_TREE)
7619	expand_decl (tem);
7620    }
7621#endif
7622
7623  if (! initialized)
7624    DECL_INITIAL (decl) = NULL_TREE;
7625}
7626
7627/* Handle initialization of references.
7628   These three arguments are from `cp_finish_decl', and have the
7629   same meaning here that they do there.
7630
7631   Quotes on semantics can be found in ARM 8.4.3.  */
7632
7633static void
7634grok_reference_init (decl, type, init)
7635     tree decl, type, init;
7636{
7637  tree tmp;
7638
7639  if (init == NULL_TREE)
7640    {
7641      if ((DECL_LANG_SPECIFIC (decl) == 0
7642	   || DECL_IN_AGGR_P (decl) == 0)
7643	  && ! DECL_THIS_EXTERN (decl))
7644	{
7645	  cp_error ("`%D' declared as reference but not initialized", decl);
7646	  if (TREE_CODE (decl) == VAR_DECL)
7647	    SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7648	}
7649      return;
7650    }
7651
7652  if (init == error_mark_node)
7653    return;
7654
7655  if (TREE_CODE (type) == REFERENCE_TYPE
7656      && TREE_CODE (init) == CONSTRUCTOR)
7657    {
7658      cp_error ("ANSI C++ forbids use of initializer list to initialize reference `%D'", decl);
7659      return;
7660    }
7661
7662  if (TREE_CODE (init) == TREE_LIST)
7663    init = build_compound_expr (init);
7664
7665  if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7666    init = convert_from_reference (init);
7667
7668  if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7669      && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7670    {
7671      /* Note: default conversion is only called in very special cases.  */
7672      init = default_conversion (init);
7673    }
7674
7675  tmp = convert_to_reference
7676    (type, init, CONV_IMPLICIT,
7677     LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND, decl);
7678
7679  if (tmp == error_mark_node)
7680    goto fail;
7681  else if (tmp != NULL_TREE)
7682    {
7683      init = tmp;
7684      DECL_INITIAL (decl) = save_expr (init);
7685    }
7686  else
7687    {
7688      cp_error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7689      goto fail;
7690    }
7691
7692  /* ?? Can this be optimized in some cases to
7693     hand back the DECL_INITIAL slot??  */
7694  if (TYPE_SIZE (TREE_TYPE (type)))
7695    {
7696      init = convert_from_reference (decl);
7697      if (TREE_PERMANENT (decl))
7698	init = copy_to_permanent (init);
7699      SET_DECL_REFERENCE_SLOT (decl, init);
7700    }
7701
7702  if (TREE_STATIC (decl) && ! TREE_CONSTANT (DECL_INITIAL (decl)))
7703    {
7704      expand_static_init (decl, DECL_INITIAL (decl));
7705      DECL_INITIAL (decl) = NULL_TREE;
7706    }
7707  return;
7708
7709 fail:
7710  if (TREE_CODE (decl) == VAR_DECL)
7711    SET_DECL_REFERENCE_SLOT (decl, error_mark_node);
7712  return;
7713}
7714
7715/* Fill in DECL_INITIAL with some magical value to prevent expand_decl from
7716   mucking with forces it does not comprehend (i.e. initialization with a
7717   constructor).  If we are at global scope and won't go into COMMON, fill
7718   it in with a dummy CONSTRUCTOR to force the variable into .data;
7719   otherwise we can use error_mark_node.  */
7720
7721static tree
7722obscure_complex_init (decl, init)
7723     tree decl, init;
7724{
7725  if (! flag_no_inline && TREE_STATIC (decl))
7726    {
7727      if (extract_init (decl, init))
7728	return NULL_TREE;
7729    }
7730
7731#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7732  if (toplevel_bindings_p () && ! DECL_COMMON (decl))
7733    DECL_INITIAL (decl) = build (CONSTRUCTOR, TREE_TYPE (decl), NULL_TREE,
7734				 NULL_TREE);
7735  else
7736#endif
7737    DECL_INITIAL (decl) = error_mark_node;
7738
7739  return init;
7740}
7741
7742/* Issue an error message if DECL is an uninitialized const variable.  */
7743
7744static void
7745check_for_uninitialized_const_var (decl)
7746     tree decl;
7747{
7748  tree type = TREE_TYPE (decl);
7749
7750  /* ``Unless explicitly declared extern, a const object does not have
7751     external linkage and must be initialized. ($8.4; $12.1)'' ARM
7752     7.1.6 */
7753  if (TREE_CODE (decl) == VAR_DECL
7754      && TREE_CODE (type) != REFERENCE_TYPE
7755      && CP_TYPE_CONST_P (type)
7756      && !TYPE_NEEDS_CONSTRUCTING (type)
7757      && !DECL_INITIAL (decl))
7758    cp_error ("uninitialized const `%D'", decl);
7759}
7760
7761/* Finish processing of a declaration;
7762   install its line number and initial value.
7763   If the length of an array type is not known before,
7764   it must be determined now, from the initial value, or it is an error.
7765
7766   Call `pop_obstacks' iff NEED_POP is nonzero.
7767
7768   For C++, `cp_finish_decl' must be fairly evasive:  it must keep initializers
7769   for aggregates that have constructors alive on the permanent obstack,
7770   so that the global initializing functions can be written at the end.
7771
7772   INIT0 holds the value of an initializer that should be allowed to escape
7773   the normal rules.
7774
7775   FLAGS is LOOKUP_ONLYCONVERTING is the = init syntax was used, else 0
7776   if the (init) syntax was used.
7777
7778   For functions that take default parameters, DECL points to its
7779   "maximal" instantiation.  `cp_finish_decl' must then also declared its
7780   subsequently lower and lower forms of instantiation, checking for
7781   ambiguity as it goes.  This can be sped up later.  */
7782
7783void
7784cp_finish_decl (decl, init, asmspec_tree, need_pop, flags)
7785     tree decl, init;
7786     tree asmspec_tree;
7787     int need_pop;
7788     int flags;
7789{
7790  register tree type;
7791  tree cleanup = NULL_TREE, ttype = NULL_TREE;
7792  int was_incomplete;
7793  int temporary = allocation_temporary_p ();
7794  char *asmspec = NULL;
7795  int was_readonly = 0;
7796  int already_used = 0;
7797  tree core_type;
7798
7799  /* If this is 0, then we did not change obstacks.  */
7800  if (! decl)
7801    {
7802      if (init)
7803	error ("assignment (not initialization) in declaration");
7804      return;
7805    }
7806
7807  /* If a name was specified, get the string.  */
7808  if (asmspec_tree)
7809      asmspec = TREE_STRING_POINTER (asmspec_tree);
7810
7811  if (init && TREE_CODE (init) == NAMESPACE_DECL)
7812    {
7813      cp_error ("Cannot initialize `%D' to namespace `%D'",
7814		decl, init);
7815      init = NULL_TREE;
7816    }
7817
7818  if (current_class_type
7819      && DECL_REAL_CONTEXT (decl) == current_class_type
7820      && TYPE_BEING_DEFINED (current_class_type)
7821      && (DECL_INITIAL (decl) || init))
7822    DECL_DEFINED_IN_CLASS_P (decl) = 1;
7823
7824  if (TREE_CODE (decl) == VAR_DECL
7825      && DECL_CONTEXT (decl)
7826      && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
7827      && DECL_CONTEXT (decl) != current_namespace
7828      && init)
7829    {
7830      /* Leave the namespace of the object. */
7831      pop_decl_namespace ();
7832    }
7833
7834  /* If the type of the thing we are declaring either has
7835     a constructor, or has a virtual function table pointer,
7836     AND its initialization was accepted by `start_decl',
7837     then we stayed on the permanent obstack through the
7838     declaration, otherwise, changed obstacks as GCC would.  */
7839
7840  type = TREE_TYPE (decl);
7841
7842  if (type == error_mark_node)
7843    {
7844      if (toplevel_bindings_p () && temporary)
7845	end_temporary_allocation ();
7846
7847      return;
7848    }
7849
7850  if (TYPE_HAS_MUTABLE_P (type))
7851    TREE_READONLY (decl) = 0;
7852
7853  if (processing_template_decl)
7854    {
7855      if (init && DECL_INITIAL (decl))
7856	DECL_INITIAL (decl) = init;
7857      if (minimal_parse_mode && ! DECL_ARTIFICIAL (decl))
7858	{
7859	  tree stmt = DECL_VINDEX (decl);
7860	  /* If the decl is declaring a member of a local class (in a
7861	     template function), the DECL_VINDEX will either be NULL,
7862	     or it will be an actual virtual function index, not a
7863	     DECL_STMT.  */
7864	  if (stmt != NULL_TREE && TREE_CODE (stmt) == DECL_STMT)
7865	    {
7866	      DECL_VINDEX (decl) = NULL_TREE;
7867	      TREE_OPERAND (stmt, 2) = copy_to_permanent (init);
7868	      add_tree (stmt);
7869	    }
7870	}
7871
7872      goto finish_end0;
7873    }
7874  /* Take care of TYPE_DECLs up front.  */
7875  if (TREE_CODE (decl) == TYPE_DECL)
7876    {
7877      if (init && DECL_INITIAL (decl))
7878	{
7879	  /* typedef foo = bar; store the type of bar as the type of foo.  */
7880	  TREE_TYPE (decl) = type = TREE_TYPE (init);
7881	  DECL_INITIAL (decl) = init = NULL_TREE;
7882	}
7883      if (type != error_mark_node
7884	  && IS_AGGR_TYPE (type) && DECL_NAME (decl))
7885	{
7886	  if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
7887	    cp_warning ("shadowing previous type declaration of `%#D'", decl);
7888	  set_identifier_type_value (DECL_NAME (decl), type);
7889	  CLASSTYPE_GOT_SEMICOLON (type) = 1;
7890	}
7891      GNU_xref_decl (current_function_decl, decl);
7892
7893      /* If we have installed this as the canonical typedef for this
7894	 type, and that type has not been defined yet, delay emitting
7895	 the debug information for it, as we will emit it later.  */
7896      if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
7897	  && TYPE_SIZE (TREE_TYPE (decl)) == NULL_TREE)
7898	TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
7899
7900      rest_of_decl_compilation (decl, NULL_PTR,
7901				DECL_CONTEXT (decl) == NULL_TREE, at_eof);
7902      goto finish_end;
7903    }
7904  if (TREE_CODE (decl) != FUNCTION_DECL)
7905    {
7906      ttype = target_type (type);
7907    }
7908
7909  if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
7910      && TYPE_NEEDS_CONSTRUCTING (type))
7911    {
7912
7913      /* Currently, GNU C++ puts constants in text space, making them
7914	 impossible to initialize.  In the future, one would hope for
7915	 an operating system which understood the difference between
7916	 initialization and the running of a program.  */
7917      was_readonly = 1;
7918      TREE_READONLY (decl) = 0;
7919    }
7920
7921  if (TREE_CODE (decl) == FIELD_DECL)
7922    {
7923      if (init && init != error_mark_node)
7924	my_friendly_assert (TREE_PERMANENT (init), 147);
7925
7926      if (asmspec)
7927	{
7928	  /* This must override the asm specifier which was placed
7929	     by grokclassfn.  Lay this out fresh.  */
7930	  DECL_RTL (TREE_TYPE (decl)) = NULL_RTX;
7931	  DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
7932	  make_decl_rtl (decl, asmspec, 0);
7933	}
7934    }
7935  /* If `start_decl' didn't like having an initialization, ignore it now.  */
7936  else if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7937    init = NULL_TREE;
7938  else if (DECL_EXTERNAL (decl))
7939    ;
7940  else if (TREE_CODE (type) == REFERENCE_TYPE
7941	   || (TYPE_LANG_SPECIFIC (type) && IS_SIGNATURE_REFERENCE (type)))
7942    {
7943      if (TREE_STATIC (decl))
7944	make_decl_rtl (decl, NULL_PTR,
7945		       toplevel_bindings_p ()
7946		       || pseudo_global_level_p ());
7947      grok_reference_init (decl, type, init);
7948      init = NULL_TREE;
7949    }
7950
7951  GNU_xref_decl (current_function_decl, decl);
7952
7953  core_type = type;
7954  while (TREE_CODE (core_type) == ARRAY_TYPE)
7955    core_type = TREE_TYPE (core_type);
7956
7957  if (TREE_CODE (decl) == FIELD_DECL)
7958    ;
7959  else if (TREE_CODE (decl) == CONST_DECL)
7960    {
7961      my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7962
7963      DECL_INITIAL (decl) = init;
7964
7965      /* This will keep us from needing to worry about our obstacks.  */
7966      my_friendly_assert (init != NULL_TREE, 149);
7967      init = NULL_TREE;
7968    }
7969  else if (init)
7970    {
7971      if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7972	{
7973	  if (TREE_CODE (type) == ARRAY_TYPE)
7974	    init = digest_init (type, init, (tree *) 0);
7975	  else if (TREE_CODE (init) == CONSTRUCTOR
7976		   && TREE_HAS_CONSTRUCTOR (init))
7977	    {
7978	      if (TYPE_NON_AGGREGATE_CLASS (type))
7979		{
7980		  cp_error ("`%D' must be initialized by constructor, not by `{...}'",
7981			    decl);
7982		  init = error_mark_node;
7983		}
7984	      else
7985		goto dont_use_constructor;
7986	    }
7987	}
7988      else
7989	{
7990	dont_use_constructor:
7991	  if (TREE_CODE (init) != TREE_VEC)
7992	    init = store_init_value (decl, init);
7993	}
7994
7995      if (init)
7996	/* We must hide the initializer so that expand_decl
7997	   won't try to do something it does not understand.  */
7998	init = obscure_complex_init (decl, init);
7999    }
8000  else if (DECL_EXTERNAL (decl))
8001    ;
8002  else if (TREE_CODE_CLASS (TREE_CODE (type)) == 't'
8003	   && (IS_AGGR_TYPE (type) || TYPE_NEEDS_CONSTRUCTING (type)))
8004    {
8005      if (! TYPE_NEEDS_CONSTRUCTING (core_type))
8006	{
8007	  if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
8008	    cp_error ("structure `%D' with uninitialized const members", decl);
8009	  if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
8010	    cp_error ("structure `%D' with uninitialized reference members",
8011		      decl);
8012	}
8013
8014      check_for_uninitialized_const_var (decl);
8015
8016      if (TYPE_SIZE (type) != NULL_TREE
8017	  && TYPE_NEEDS_CONSTRUCTING (type))
8018	init = obscure_complex_init (decl, NULL_TREE);
8019
8020    }
8021  else
8022    check_for_uninitialized_const_var (decl);
8023
8024  /* For top-level declaration, the initial value was read in
8025     the temporary obstack.  MAXINDEX, rtl, etc. to be made below
8026     must go in the permanent obstack; but don't discard the
8027     temporary data yet.  */
8028
8029  if (toplevel_bindings_p () && temporary)
8030    end_temporary_allocation ();
8031
8032  /* Deduce size of array from initialization, if not already known.  */
8033
8034  if (TREE_CODE (type) == ARRAY_TYPE
8035      && TYPE_DOMAIN (type) == NULL_TREE
8036      && TREE_CODE (decl) != TYPE_DECL)
8037    {
8038      int do_default
8039	= (TREE_STATIC (decl)
8040	   /* Even if pedantic, an external linkage array
8041	      may have incomplete type at first.  */
8042	   ? pedantic && ! DECL_EXTERNAL (decl)
8043	   : !DECL_EXTERNAL (decl));
8044      tree initializer = init ? init : DECL_INITIAL (decl);
8045      int failure = complete_array_type (type, initializer, do_default);
8046
8047      if (failure == 1)
8048	cp_error ("initializer fails to determine size of `%D'", decl);
8049
8050      if (failure == 2)
8051	{
8052	  if (do_default)
8053	    cp_error ("array size missing in `%D'", decl);
8054	  /* If a `static' var's size isn't known, make it extern as
8055	     well as static, so it does not get allocated.  If it's not
8056	     `static', then don't mark it extern; finish_incomplete_decl
8057	     will give it a default size and it will get allocated.  */
8058	  else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
8059	    DECL_EXTERNAL (decl) = 1;
8060	}
8061
8062      if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
8063	  && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
8064			      integer_zero_node))
8065	cp_error ("zero-size array `%D'", decl);
8066
8067      layout_decl (decl, 0);
8068    }
8069
8070  if (TREE_CODE (decl) == VAR_DECL)
8071    {
8072      if (DECL_SIZE (decl) == NULL_TREE
8073	  && TYPE_SIZE (complete_type (TREE_TYPE (decl))) != NULL_TREE)
8074	layout_decl (decl, 0);
8075
8076      if (TREE_STATIC (decl) && DECL_SIZE (decl) == NULL_TREE)
8077	{
8078	  /* A static variable with an incomplete type:
8079	     that is an error if it is initialized.
8080	     Otherwise, let it through, but if it is not `extern'
8081	     then it may cause an error message later.  */
8082	  if (DECL_INITIAL (decl) != NULL_TREE)
8083	    cp_error ("storage size of `%D' isn't known", decl);
8084	  init = NULL_TREE;
8085	}
8086      else if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
8087	{
8088	  /* An automatic variable with an incomplete type: that is an error.
8089	     Don't talk about array types here, since we took care of that
8090	     message in grokdeclarator.  */
8091	  cp_error ("storage size of `%D' isn't known", decl);
8092	  TREE_TYPE (decl) = error_mark_node;
8093	}
8094      else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
8095	/* Let debugger know it should output info for this type.  */
8096	note_debug_info_needed (ttype);
8097
8098      if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
8099	note_debug_info_needed (DECL_CONTEXT (decl));
8100
8101      if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8102	  && DECL_SIZE (decl) != NULL_TREE
8103	  && ! TREE_CONSTANT (DECL_SIZE (decl)))
8104	{
8105	  if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
8106	    constant_expression_warning (DECL_SIZE (decl));
8107	  else
8108	    cp_error ("storage size of `%D' isn't constant", decl);
8109	}
8110
8111      if (! DECL_EXTERNAL (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8112	  /* Cleanups for static variables are handled by `finish_file'.  */
8113	  && ! TREE_STATIC (decl))
8114	{
8115	  int yes = suspend_momentary ();
8116	  cleanup = maybe_build_cleanup (decl);
8117	  resume_momentary (yes);
8118	}
8119    }
8120  /* PARM_DECLs get cleanups, too.  */
8121  else if (TREE_CODE (decl) == PARM_DECL && TYPE_NEEDS_DESTRUCTOR (type))
8122    {
8123      if (temporary)
8124	end_temporary_allocation ();
8125      cleanup = maybe_build_cleanup (decl);
8126      if (temporary)
8127	resume_temporary_allocation ();
8128    }
8129
8130  /* Output the assembler code and/or RTL code for variables and functions,
8131     unless the type is an undefined structure or union.
8132     If not, it will get done when the type is completed.  */
8133
8134  was_incomplete = (DECL_SIZE (decl) == NULL_TREE);
8135
8136  if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8137      || TREE_CODE (decl) == RESULT_DECL)
8138    {
8139      /* ??? FIXME: What about nested classes?  */
8140      int toplev = toplevel_bindings_p () || pseudo_global_level_p ();
8141      int was_temp
8142	= (TREE_STATIC (decl) && TYPE_NEEDS_DESTRUCTOR (type)
8143	   && allocation_temporary_p ());
8144
8145      if (was_temp)
8146	end_temporary_allocation ();
8147
8148      /* Static data in a function with comdat linkage also has comdat
8149         linkage.  */
8150      if (TREE_CODE (decl) == VAR_DECL
8151	  && TREE_STATIC (decl)
8152	  /* Don't mess with __FUNCTION__.  */
8153	  && ! TREE_ASM_WRITTEN (decl)
8154	  && current_function_decl
8155	  && DECL_CONTEXT (decl) == current_function_decl
8156	  && (DECL_THIS_INLINE (current_function_decl)
8157	      || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
8158	  && TREE_PUBLIC (current_function_decl))
8159	{
8160	  /* Rather than try to get this right with inlining, we suppress
8161	     inlining of such functions.  */
8162	  current_function_cannot_inline
8163	    = "function with static variable cannot be inline";
8164
8165	  /* If flag_weak, we don't need to mess with this, as we can just
8166	     make the function weak, and let it refer to its unique local
8167	     copy.  This works because we don't allow the function to be
8168	     inlined.  */
8169	  if (! flag_weak)
8170	    {
8171	      if (DECL_INTERFACE_KNOWN (current_function_decl))
8172		{
8173		  TREE_PUBLIC (decl) = 1;
8174		  DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
8175		}
8176	      else if (DECL_INITIAL (decl) == NULL_TREE
8177		       || DECL_INITIAL (decl) == error_mark_node)
8178		{
8179		  TREE_PUBLIC (decl) = 1;
8180		  DECL_COMMON (decl) = 1;
8181		}
8182	      /* else we lose. We can only do this if we can use common,
8183                 which we can't if it has been initialized.  */
8184
8185	      if (TREE_PUBLIC (decl))
8186		DECL_ASSEMBLER_NAME (decl)
8187		  = build_static_name (current_function_decl, DECL_NAME (decl));
8188	      else if (! DECL_ARTIFICIAL (decl))
8189		{
8190		  cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
8191		  cp_warning_at ("  you can work around this by removing the initializer", decl);
8192		}
8193	    }
8194	}
8195
8196      else if (TREE_CODE (decl) == VAR_DECL
8197	       && DECL_LANG_SPECIFIC (decl)
8198	       && DECL_COMDAT (decl))
8199	/* Set it up again; we might have set DECL_INITIAL since the
8200	   last time.  */
8201	comdat_linkage (decl);
8202
8203      if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
8204	make_decl_rtl (decl, NULL_PTR, toplev);
8205      else if (TREE_CODE (decl) == VAR_DECL
8206	       && TREE_READONLY (decl)
8207	       && DECL_INITIAL (decl) != NULL_TREE
8208	       && DECL_INITIAL (decl) != error_mark_node
8209	       && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl)))
8210	{
8211	  DECL_INITIAL (decl) = save_expr (DECL_INITIAL (decl));
8212
8213	  if (asmspec)
8214	    DECL_ASSEMBLER_NAME (decl) = get_identifier (asmspec);
8215
8216	  if (! toplev
8217	      && TREE_STATIC (decl)
8218	      && ! TREE_SIDE_EFFECTS (decl)
8219	      && ! TREE_PUBLIC (decl)
8220	      && ! DECL_EXTERNAL (decl)
8221	      && ! TYPE_NEEDS_DESTRUCTOR (type)
8222	      && DECL_MODE (decl) != BLKmode)
8223	    {
8224	      /* If this variable is really a constant, then fill its DECL_RTL
8225		 slot with something which won't take up storage.
8226		 If something later should take its address, we can always give
8227		 it legitimate RTL at that time.  */
8228	      DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
8229	      store_expr (DECL_INITIAL (decl), DECL_RTL (decl), 0);
8230	      TREE_ASM_WRITTEN (decl) = 1;
8231	    }
8232	  else if (toplev && ! TREE_PUBLIC (decl))
8233	    {
8234	      /* If this is a static const, change its apparent linkage
8235	         if it belongs to a #pragma interface.  */
8236	      if (!interface_unknown)
8237		{
8238		  TREE_PUBLIC (decl) = 1;
8239		  DECL_EXTERNAL (decl) = interface_only;
8240		}
8241	      make_decl_rtl (decl, asmspec, toplev);
8242	    }
8243	  else
8244	    rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8245	}
8246      else if (TREE_CODE (decl) == VAR_DECL
8247	       && DECL_LANG_SPECIFIC (decl)
8248	       && DECL_IN_AGGR_P (decl))
8249	{
8250	  if (TREE_STATIC (decl))
8251	    {
8252	      if (init == NULL_TREE
8253#ifdef DEFAULT_STATIC_DEFS
8254		  /* If this code is dead, then users must
8255		     explicitly declare static member variables
8256		     outside the class def'n as well.  */
8257		  && TYPE_NEEDS_CONSTRUCTING (type)
8258#endif
8259		  )
8260		{
8261		  DECL_EXTERNAL (decl) = 1;
8262		  make_decl_rtl (decl, asmspec, 1);
8263		}
8264	      else
8265		rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8266	    }
8267	  else
8268	    /* Just a constant field.  Should not need any rtl.  */
8269	    goto finish_end0;
8270	}
8271      else
8272	rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8273
8274      if (was_temp)
8275	resume_temporary_allocation ();
8276
8277      if (type != error_mark_node
8278	  && TYPE_LANG_SPECIFIC (core_type)
8279	  && CLASSTYPE_ABSTRACT_VIRTUALS (core_type))
8280	abstract_virtuals_error (decl, core_type);
8281      else if ((TREE_CODE (type) == FUNCTION_TYPE
8282		|| TREE_CODE (type) == METHOD_TYPE)
8283	       && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
8284	       && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (type)))
8285	abstract_virtuals_error (decl, TREE_TYPE (type));
8286
8287      if (TYPE_LANG_SPECIFIC (core_type) && IS_SIGNATURE (core_type))
8288	signature_error (decl, core_type);
8289      else if ((TREE_CODE (type) == FUNCTION_TYPE
8290		|| TREE_CODE (type) == METHOD_TYPE)
8291	       && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
8292	       && IS_SIGNATURE (TREE_TYPE (type)))
8293	signature_error (decl, TREE_TYPE (type));
8294
8295      if (TREE_CODE (decl) == FUNCTION_DECL)
8296	;
8297      else if (DECL_EXTERNAL (decl)
8298	       && ! (DECL_LANG_SPECIFIC (decl)
8299		     && DECL_NOT_REALLY_EXTERN (decl)))
8300	{
8301	  if (init)
8302	    DECL_INITIAL (decl) = init;
8303	}
8304      else if (TREE_STATIC (decl) && type != error_mark_node)
8305	{
8306	  /* Cleanups for static variables are handled by `finish_file'.  */
8307	  if (TYPE_NEEDS_CONSTRUCTING (type) || init != NULL_TREE
8308	      || TYPE_NEEDS_DESTRUCTOR (type))
8309	    expand_static_init (decl, init);
8310	}
8311      else if (! toplev)
8312	{
8313	  /* This is a declared decl which must live until the
8314	     end of the binding contour.  It may need a cleanup.  */
8315
8316	  /* Recompute the RTL of a local array now
8317	     if it used to be an incomplete type.  */
8318	  if (was_incomplete && ! TREE_STATIC (decl))
8319	    {
8320	      /* If we used it already as memory, it must stay in memory.  */
8321	      TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8322	      /* If it's still incomplete now, no init will save it.  */
8323	      if (DECL_SIZE (decl) == NULL_TREE)
8324		DECL_INITIAL (decl) = NULL_TREE;
8325	      expand_decl (decl);
8326	    }
8327	  else if (! TREE_ASM_WRITTEN (decl)
8328		   && (TYPE_SIZE (type) != NULL_TREE
8329		       || TREE_CODE (type) == ARRAY_TYPE))
8330	    {
8331	      /* Do this here, because we did not expand this decl's
8332		 rtl in start_decl.  */
8333	      if (DECL_RTL (decl) == NULL_RTX)
8334		expand_decl (decl);
8335	      else if (cleanup)
8336		{
8337		  /* XXX: Why don't we use decl here?  */
8338		  /* Ans: Because it was already expanded? */
8339		  if (! expand_decl_cleanup (NULL_TREE, cleanup))
8340		    cp_error ("parser lost in parsing declaration of `%D'",
8341			      decl);
8342		  /* Cleanup used up here.  */
8343		  cleanup = NULL_TREE;
8344		}
8345	    }
8346
8347	  if (current_binding_level->is_for_scope)
8348	    {
8349	      struct binding_level *outer
8350		= current_binding_level->level_chain;
8351
8352	      /* Check to see if the same name is already bound at
8353		 the outer level, either because it was directly declared,
8354		 or because a dead for-decl got preserved.  In either case,
8355		 the code would not have been valid under the ARM
8356		 scope rules, so clear is_for_scope for the
8357		 current_binding_level.
8358
8359		 Otherwise, we need to preserve the temp slot for decl
8360		 to last into the outer binding level.  */
8361
8362	      tree outer_binding
8363		= TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
8364
8365	      if (outer_binding && BINDING_LEVEL (outer_binding) == outer
8366		  && (TREE_CODE (BINDING_VALUE (outer_binding))
8367		      == VAR_DECL)
8368		  && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
8369		{
8370		  BINDING_VALUE (outer_binding)
8371		    = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
8372		  current_binding_level->is_for_scope = 0;
8373		}
8374	      else if (DECL_IN_MEMORY_P (decl))
8375		preserve_temp_slots (DECL_RTL (decl));
8376	    }
8377
8378	  expand_start_target_temps ();
8379
8380	  if (DECL_SIZE (decl) && type != error_mark_node)
8381	    {
8382	      /* Compute and store the initial value.  */
8383	      expand_decl_init (decl);
8384       	      already_used = TREE_USED (decl) || TREE_USED (type);
8385
8386	      if (init || TYPE_NEEDS_CONSTRUCTING (type))
8387		{
8388		  emit_line_note (DECL_SOURCE_FILE (decl),
8389				  DECL_SOURCE_LINE (decl));
8390		  expand_aggr_init (decl, init, flags);
8391		}
8392
8393	      /* Set this to 0 so we can tell whether an aggregate which
8394		 was initialized was ever used.  Don't do this if it has a
8395		 destructor, so we don't complain about the 'resource
8396		 allocation is initialization' idiom.  */
8397	      /* Now set attribute((unused)) on types so decls of
8398		 that type will be marked used. (see TREE_USED, above.)
8399		 This avoids the warning problems this particular code
8400		 tried to work around. */
8401
8402	      if (TYPE_NEEDS_CONSTRUCTING (type)
8403		  && ! already_used
8404		  && cleanup == NULL_TREE
8405		  && DECL_NAME (decl))
8406		TREE_USED (decl) = 0;
8407
8408	      if (already_used)
8409		TREE_USED (decl) = 1;
8410	    }
8411
8412	  /* Cleanup any temporaries needed for the initial value.  */
8413	  expand_end_target_temps ();
8414
8415	  if (DECL_SIZE (decl) && type != error_mark_node)
8416	    {
8417	      /* Store the cleanup, if there was one.  */
8418	      if (cleanup)
8419		{
8420		  if (! expand_decl_cleanup (decl, cleanup))
8421		    cp_error ("parser lost in parsing declaration of `%D'",
8422			      decl);
8423		}
8424	    }
8425	}
8426    finish_end0:
8427
8428      /* Undo call to `pushclass' that was done in `start_decl'
8429	 due to initialization of qualified member variable.
8430	 I.e., Foo::x = 10;  */
8431      {
8432	tree context = DECL_REAL_CONTEXT (decl);
8433	if (context
8434	    && TREE_CODE_CLASS (TREE_CODE (context)) == 't'
8435	    && (TREE_CODE (decl) == VAR_DECL
8436		/* We also have a pushclass done that we need to undo here
8437		   if we're at top level and declare a method.  */
8438		|| TREE_CODE (decl) == FUNCTION_DECL)
8439	    /* If size hasn't been set, we're still defining it,
8440	       and therefore inside the class body; don't pop
8441	       the binding level..  */
8442	    && TYPE_SIZE (context) != NULL_TREE
8443	    && context == current_class_type)
8444	  pop_nested_class ();
8445      }
8446    }
8447
8448 finish_end:
8449
8450  /* If requested, warn about definitions of large data objects.  */
8451
8452  if (warn_larger_than
8453      && ! processing_template_decl
8454      && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
8455      && !DECL_EXTERNAL (decl))
8456    {
8457      register tree decl_size = DECL_SIZE (decl);
8458
8459      if (decl_size && TREE_CODE (decl_size) == INTEGER_CST)
8460	{
8461	  unsigned units = TREE_INT_CST_LOW (decl_size) / BITS_PER_UNIT;
8462
8463	  if (units > larger_than_size)
8464	    warning_with_decl (decl, "size of `%s' is %u bytes", units);
8465	}
8466    }
8467
8468  if (need_pop)
8469    {
8470      /* Resume permanent allocation, if not within a function.  */
8471      /* The corresponding push_obstacks_nochange is in start_decl,
8472	 start_method, groktypename, and in grokfield.  */
8473      pop_obstacks ();
8474    }
8475
8476  if (was_readonly)
8477    TREE_READONLY (decl) = 1;
8478}
8479
8480/* This is here for a midend callback from c-common.c */
8481
8482void
8483finish_decl (decl, init, asmspec_tree)
8484     tree decl, init;
8485     tree asmspec_tree;
8486{
8487  cp_finish_decl (decl, init, asmspec_tree, 1, 0);
8488}
8489
8490void
8491expand_static_init (decl, init)
8492     tree decl;
8493     tree init;
8494{
8495  tree oldstatic = value_member (decl, static_aggregates);
8496
8497  if (oldstatic)
8498    {
8499      if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8500	cp_error ("multiple initializations given for `%D'", decl);
8501    }
8502  else if (! toplevel_bindings_p ())
8503    {
8504      /* Emit code to perform this initialization but once.  */
8505      tree temp;
8506
8507      /* Remember this information until end of file.  */
8508      push_obstacks (&permanent_obstack, &permanent_obstack);
8509
8510      /* Emit code to perform this initialization but once.  This code
8511	 looks like:
8512
8513           static int temp = 0;
8514           if (!temp) {
8515             // Do initialization.
8516	     temp = 1;
8517	     // Register variable for destruction at end of program.
8518	   }
8519
8520	 Note that the `temp' variable is only set to 1 *after* the
8521	 initialization is complete.  This ensures that an exception,
8522	 thrown during the construction, will cause the variable to
8523	 reinitialized when we pass through this code again, as per:
8524
8525	   [stmt.dcl]
8526
8527	   If the initialization exits by throwing an exception, the
8528	   initialization is not complete, so it will be tried again
8529	   the next time control enters the declaration.
8530
8531         In theory, this process should be thread-safe, too; multiple
8532	 threads should not be able to initialize the variable more
8533	 than once.  We don't yet attempt to ensure thread-safety.  */
8534      temp = get_temp_name (integer_type_node, 1);
8535      rest_of_decl_compilation (temp, NULL_PTR, 0, 0);
8536
8537      /* Begin the conditional initialization.  */
8538      expand_start_cond (build_binary_op (EQ_EXPR, temp,
8539					  integer_zero_node), 0);
8540      expand_start_target_temps ();
8541
8542      /* Do the initialization itself.  */
8543      if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8544	  || (init && TREE_CODE (init) == TREE_LIST))
8545	{
8546	  expand_aggr_init (decl, init, 0);
8547	  do_pending_stack_adjust ();
8548	}
8549      else if (init)
8550	expand_assignment (decl, init, 0, 0);
8551
8552      /* Set TEMP to 1.  */
8553      expand_assignment (temp, integer_one_node, 0, 0);
8554
8555      /* Cleanup any temporaries needed for the initial value.  If
8556	 destroying one of the temporaries causes an exception to be
8557	 thrown, then the object itself has still been fully
8558	 constructed.  */
8559      expand_end_target_temps ();
8560
8561      /* Use atexit to register a function for destroying this static
8562	 variable.  */
8563      if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (decl)))
8564	{
8565	  tree cleanup, fcall;
8566	  static tree Atexit = 0;
8567	  int saved_flag_access_control;
8568
8569	  if (Atexit == 0)
8570	    {
8571	      tree atexit_fndecl, PFV, pfvlist;
8572	      /* Remember this information until end of file.  */
8573	      push_obstacks (&permanent_obstack, &permanent_obstack);
8574	      PFV = build_pointer_type (build_function_type
8575					(void_type_node, void_list_node));
8576
8577	      pfvlist = tree_cons (NULL_TREE, PFV, void_list_node);
8578
8579	      push_lang_context (lang_name_c);
8580	      atexit_fndecl
8581		= builtin_function ("atexit",
8582				    build_function_type (void_type_node,
8583							 pfvlist),
8584				    NOT_BUILT_IN, NULL_PTR);
8585	      mark_used (atexit_fndecl);
8586	      Atexit = default_conversion (atexit_fndecl);
8587	      pop_lang_context ();
8588	      pop_obstacks ();
8589	    }
8590
8591	  /* Call build_cleanup before we enter the anonymous function
8592	     so that any access checks will be done relative to the
8593	     current scope, rather than the scope of the anonymous
8594	     function.  */
8595	  build_cleanup (decl);
8596
8597	  /* Now start the function.  */
8598	  cleanup = start_anon_func ();
8599
8600	  /* Now, recompute the cleanup.  It may contain SAVE_EXPRs
8601	     that refer to the original function, rather than the
8602	     anonymous one.  That will make the back-end think that
8603	     nested functions are in use, which causes confusion.  */
8604	  saved_flag_access_control = flag_access_control;
8605	  flag_access_control = 0;
8606	  fcall = build_cleanup (decl);
8607	  flag_access_control = saved_flag_access_control;
8608
8609	  /* Finish off the function.  */
8610	  expand_expr_stmt (fcall);
8611	  end_anon_func ();
8612
8613	  /* Call atexit with the cleanup function.  */
8614	  mark_addressable (cleanup);
8615	  cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8616	  fcall = build_function_call (Atexit,
8617				       expr_tree_cons (NULL_TREE,
8618						       cleanup,
8619						       NULL_TREE));
8620	  expand_expr_stmt (fcall);
8621	}
8622
8623      expand_end_cond ();
8624      /* Resume old (possibly temporary) allocation.  */
8625      pop_obstacks ();
8626    }
8627  else
8628    {
8629      /* This code takes into account memory allocation policy of
8630	 `start_decl'.  Namely, if TYPE_NEEDS_CONSTRUCTING does not
8631	 hold for this object, then we must make permanent the storage
8632	 currently in the temporary obstack.  */
8633      if (!TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl)))
8634	preserve_initializer ();
8635      static_aggregates = perm_tree_cons (init, decl, static_aggregates);
8636    }
8637}
8638
8639/* Make TYPE a complete type based on INITIAL_VALUE.
8640   Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8641   2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8642
8643int
8644complete_array_type (type, initial_value, do_default)
8645     tree type, initial_value;
8646     int do_default;
8647{
8648  register tree maxindex = NULL_TREE;
8649  int value = 0;
8650
8651  /* Allocate on the same obstack as TYPE.  */
8652  push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
8653
8654  if (initial_value)
8655    {
8656      /* Note MAXINDEX  is really the maximum index,
8657	 one less than the size.  */
8658      if (TREE_CODE (initial_value) == STRING_CST)
8659	{
8660	  int eltsize
8661	    = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8662	  maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8663				   / eltsize) - 1, 0);
8664	}
8665      else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8666	{
8667	  tree elts = CONSTRUCTOR_ELTS (initial_value);
8668	  maxindex = size_binop (MINUS_EXPR, integer_zero_node, size_one_node);
8669	  for (; elts; elts = TREE_CHAIN (elts))
8670	    {
8671	      if (TREE_PURPOSE (elts))
8672		maxindex = TREE_PURPOSE (elts);
8673	      else
8674		maxindex = size_binop (PLUS_EXPR, maxindex, size_one_node);
8675	    }
8676	  maxindex = copy_node (maxindex);
8677	}
8678      else
8679	{
8680	  /* Make an error message unless that happened already.  */
8681	  if (initial_value != error_mark_node)
8682	    value = 1;
8683	  else
8684	    initial_value = NULL_TREE;
8685
8686	  /* Prevent further error messages.  */
8687	  maxindex = build_int_2 (0, 0);
8688	}
8689    }
8690
8691  if (!maxindex)
8692    {
8693      if (do_default)
8694	maxindex = build_int_2 (0, 0);
8695      value = 2;
8696    }
8697
8698  if (maxindex)
8699    {
8700      tree itype;
8701      tree domain;
8702
8703      domain = build_index_type (maxindex);
8704      TYPE_DOMAIN (type) = domain;
8705
8706      if (! TREE_TYPE (maxindex))
8707	TREE_TYPE (maxindex) = domain;
8708      if (initial_value)
8709        itype = TREE_TYPE (initial_value);
8710      else
8711	itype = NULL;
8712      if (itype && !TYPE_DOMAIN (itype))
8713	TYPE_DOMAIN (itype) = domain;
8714      /* The type of the main variant should never be used for arrays
8715	 of different sizes.  It should only ever be completed with the
8716	 size of the array.  */
8717      if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8718	TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8719    }
8720
8721  pop_obstacks();
8722
8723  /* Lay out the type now that we can get the real answer.  */
8724
8725  layout_type (type);
8726
8727  return value;
8728}
8729
8730/* Return zero if something is declared to be a member of type
8731   CTYPE when in the context of CUR_TYPE.  STRING is the error
8732   message to print in that case.  Otherwise, quietly return 1.  */
8733
8734static int
8735member_function_or_else (ctype, cur_type, string)
8736     tree ctype, cur_type;
8737     const char *string;
8738{
8739  if (ctype && ctype != cur_type)
8740    {
8741      error (string, TYPE_NAME_STRING (ctype));
8742      return 0;
8743    }
8744  return 1;
8745}
8746
8747/* Subroutine of `grokdeclarator'.  */
8748
8749/* Generate errors possibly applicable for a given set of specifiers.
8750   This is for ARM $7.1.2.  */
8751
8752static void
8753bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
8754     tree object;
8755     const char *type;
8756     int virtualp, quals, friendp, raises, inlinep;
8757{
8758  if (virtualp)
8759    cp_error ("`%D' declared as a `virtual' %s", object, type);
8760  if (inlinep)
8761    cp_error ("`%D' declared as an `inline' %s", object, type);
8762  if (quals)
8763    cp_error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8764	      object, type);
8765  if (friendp)
8766    cp_error_at ("invalid friend declaration", object);
8767  if (raises)
8768    cp_error_at ("invalid exception specifications", object);
8769}
8770
8771/* CTYPE is class type, or null if non-class.
8772   TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8773   or METHOD_TYPE.
8774   DECLARATOR is the function's name.
8775   VIRTUALP is truthvalue of whether the function is virtual or not.
8776   FLAGS are to be passed through to `grokclassfn'.
8777   QUALS are qualifiers indicating whether the function is `const'
8778   or `volatile'.
8779   RAISES is a list of exceptions that this function can raise.
8780   CHECK is 1 if we must find this method in CTYPE, 0 if we should
8781   not look, and -1 if we should not call `grokclassfn' at all.
8782
8783   Returns `NULL_TREE' if something goes wrong, after issuing
8784   applicable error messages.  */
8785
8786static tree
8787grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
8788	    raises, check, friendp, publicp, inlinep, funcdef_flag,
8789	    template_count, in_namespace)
8790     tree ctype, type;
8791     tree declarator;
8792     tree orig_declarator;
8793     int virtualp;
8794     enum overload_flags flags;
8795     tree quals, raises;
8796     int check, friendp, publicp, inlinep, funcdef_flag, template_count;
8797     tree in_namespace;
8798{
8799  tree cname, decl;
8800  int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8801  int has_default_arg = 0;
8802  tree t;
8803
8804  if (ctype)
8805    cname = TREE_CODE (TYPE_NAME (ctype)) == TYPE_DECL
8806      ? TYPE_IDENTIFIER (ctype) : TYPE_NAME (ctype);
8807  else
8808    cname = NULL_TREE;
8809
8810  if (raises)
8811    {
8812      type = build_exception_variant (type, raises);
8813    }
8814
8815  decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8816  /* Propagate volatile out from type to decl. */
8817  if (TYPE_VOLATILE (type))
8818    TREE_THIS_VOLATILE (decl) = 1;
8819
8820  /* If this decl has namespace scope, set that up.  */
8821  if (in_namespace)
8822    set_decl_namespace (decl, in_namespace, friendp);
8823  else if (publicp && ! ctype)
8824    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8825
8826  /* `main' and builtins have implicit 'C' linkage.  */
8827  if ((MAIN_NAME_P (declarator)
8828       || (IDENTIFIER_LENGTH (declarator) > 10
8829	   && IDENTIFIER_POINTER (declarator)[0] == '_'
8830	   && IDENTIFIER_POINTER (declarator)[1] == '_'
8831	   && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8832      && current_lang_name == lang_name_cplusplus
8833      && ctype == NULL_TREE
8834      /* NULL_TREE means global namespace.  */
8835      && DECL_CONTEXT (decl) == NULL_TREE)
8836    DECL_LANGUAGE (decl) = lang_c;
8837
8838  /* Should probably propagate const out from type to decl I bet (mrs).  */
8839  if (staticp)
8840    {
8841      DECL_STATIC_FUNCTION_P (decl) = 1;
8842      DECL_CONTEXT (decl) = ctype;
8843    }
8844
8845  if (ctype)
8846    DECL_CLASS_CONTEXT (decl) = ctype;
8847
8848  if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8849    {
8850      if (processing_template_decl)
8851	error ("cannot declare `main' to be a template");
8852      if (inlinep)
8853	error ("cannot declare `main' to be inline");
8854      else if (! publicp)
8855	error ("cannot declare `main' to be static");
8856      inlinep = 0;
8857      publicp = 1;
8858    }
8859
8860  /* Members of anonymous types and local classes have no linkage; make
8861     them internal.  */
8862  if (ctype && (ANON_AGGRNAME_P (TYPE_IDENTIFIER (ctype))
8863		|| hack_decl_function_context (TYPE_MAIN_DECL (ctype))))
8864    publicp = 0;
8865
8866  if (publicp)
8867    {
8868      /* [basic.link]: A name with no linkage (notably, the name of a class
8869	 or enumeration declared in a local scope) shall not be used to
8870	 declare an entity with linkage.
8871
8872	 Only check this for public decls for now.  */
8873      t = no_linkage_check (TREE_TYPE (decl));
8874      if (t)
8875	{
8876	  if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
8877	    {
8878	      if (DECL_LANGUAGE (decl) == lang_c)
8879		/* Allow this; it's pretty common in C.  */;
8880	      else
8881		cp_pedwarn ("non-local function `%#D' uses anonymous type",
8882			    decl);
8883	    }
8884	  else
8885	    cp_pedwarn ("non-local function `%#D' uses local type `%T'",
8886			decl, t);
8887	}
8888    }
8889
8890  TREE_PUBLIC (decl) = publicp;
8891  if (! publicp)
8892    {
8893      DECL_INTERFACE_KNOWN (decl) = 1;
8894      DECL_NOT_REALLY_EXTERN (decl) = 1;
8895    }
8896
8897  if (inlinep)
8898    DECL_THIS_INLINE (decl) = DECL_INLINE (decl) = 1;
8899
8900  DECL_EXTERNAL (decl) = 1;
8901  if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8902    {
8903      cp_error ("%smember function `%D' cannot have `%T' method qualifier",
8904		(ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8905      quals = NULL_TREE;
8906    }
8907
8908  if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8909    grok_op_properties (decl, virtualp, check < 0);
8910
8911  if (ctype && hack_decl_function_context (decl))
8912    DECL_NO_STATIC_CHAIN (decl) = 1;
8913
8914  for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8915    if (TREE_PURPOSE (t)
8916	&& TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8917      {
8918	has_default_arg = 1;
8919	break;
8920      }
8921
8922  if (friendp
8923      && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8924    {
8925      if (funcdef_flag)
8926	cp_error
8927	  ("defining explicit specialization `%D' in friend declaration",
8928	   orig_declarator);
8929      else
8930	{
8931	  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8932	    {
8933	      /* Something like `template <class T> friend void f<T>()'.  */
8934	      cp_error ("template-id `%D' in declaration of primary template",
8935			orig_declarator);
8936	      return NULL_TREE;
8937	    }
8938
8939
8940	  /* A friend declaration of the form friend void f<>().  Record
8941	     the information in the TEMPLATE_ID_EXPR.  */
8942	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
8943	  DECL_TEMPLATE_INFO (decl)
8944	    = perm_tree_cons (TREE_OPERAND (orig_declarator, 0),
8945			      TREE_OPERAND (orig_declarator, 1),
8946			      NULL_TREE);
8947
8948	  if (has_default_arg)
8949	    {
8950	      cp_error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8951			decl);
8952	      return NULL_TREE;
8953	    }
8954
8955	  if (inlinep)
8956	    {
8957	      cp_error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8958			decl);
8959	      return NULL_TREE;
8960	    }
8961	}
8962    }
8963
8964  if (has_default_arg)
8965    add_defarg_fn (decl);
8966
8967  /* Plain overloading: will not be grok'd by grokclassfn.  */
8968  if (! ctype && ! processing_template_decl
8969      && DECL_LANGUAGE (decl) != lang_c
8970      && (! DECL_USE_TEMPLATE (decl) || name_mangling_version < 1))
8971    set_mangled_name_for_decl (decl);
8972
8973  if (funcdef_flag)
8974    /* Make the init_value nonzero so pushdecl knows this is not
8975       tentative.  error_mark_node is replaced later with the BLOCK.  */
8976    DECL_INITIAL (decl) = error_mark_node;
8977
8978  /* Caller will do the rest of this.  */
8979  if (check < 0)
8980    return decl;
8981
8982  if (check && funcdef_flag)
8983    DECL_INITIAL (decl) = error_mark_node;
8984
8985  if (flags == NO_SPECIAL && ctype && constructor_name (cname) == declarator)
8986    {
8987      tree tmp;
8988      /* Just handle constructors here.  We could do this
8989	 inside the following if stmt, but I think
8990	 that the code is more legible by breaking this
8991	 case out.  See comments below for what each of
8992	 the following calls is supposed to do.  */
8993      DECL_CONSTRUCTOR_P (decl) = 1;
8994
8995      grokclassfn (ctype, decl, flags, quals);
8996
8997      decl = check_explicit_specialization (orig_declarator, decl,
8998					    template_count,
8999					    2 * (funcdef_flag != 0) +
9000					    4 * (friendp != 0));
9001
9002      if (decl == error_mark_node)
9003	return NULL_TREE;
9004
9005      maybe_vlist_ctor_wrapper (decl, funcdef_flag);
9006
9007      if ((! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9008	  && check)
9009	{
9010	  tmp = check_classfn (ctype, decl);
9011
9012	  if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
9013	    tmp = DECL_TEMPLATE_RESULT(tmp);
9014
9015	  if (tmp && DECL_ARTIFICIAL (tmp))
9016	    cp_error ("definition of implicitly-declared `%D'", tmp);
9017	  if (tmp && duplicate_decls (decl, tmp))
9018	    return tmp;
9019	}
9020      if (! grok_ctor_properties (ctype, decl))
9021	return NULL_TREE;
9022    }
9023  else
9024    {
9025      tree tmp;
9026
9027      /* Function gets the ugly name, field gets the nice one.
9028	 This call may change the type of the function (because
9029	 of default parameters)!  */
9030      if (ctype != NULL_TREE)
9031	grokclassfn (ctype, decl, flags, quals);
9032
9033      decl = check_explicit_specialization (orig_declarator, decl,
9034					    template_count,
9035					    2 * (funcdef_flag != 0) +
9036					    4 * (friendp != 0));
9037      if (decl == error_mark_node)
9038	return NULL_TREE;
9039
9040      if (ctype != NULL_TREE
9041	  && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9042	  && check)
9043	{
9044	  tmp = check_classfn (ctype, decl);
9045
9046	  if (tmp && TREE_CODE (tmp) == TEMPLATE_DECL)
9047	    tmp = DECL_TEMPLATE_RESULT (tmp);
9048
9049	  if (tmp && DECL_STATIC_FUNCTION_P (tmp)
9050	      && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9051	    {
9052	      /* Remove the `this' parm added by grokclassfn.
9053	         XXX Isn't this done in start_function, too?  */
9054	      revert_static_member_fn (&decl, NULL, NULL);
9055	      last_function_parms = TREE_CHAIN (last_function_parms);
9056	    }
9057	  if (tmp && DECL_ARTIFICIAL (tmp))
9058	    cp_error ("definition of implicitly-declared `%D'", tmp);
9059	  if (tmp)
9060	    {
9061	      /* Attempt to merge the declarations.  This can fail, in
9062		 the case of some illegal specialization declarations.  */
9063	      if (!duplicate_decls (decl, tmp))
9064		cp_error ("no `%#D' member function declared in class `%T'",
9065			  decl, ctype);
9066	      return tmp;
9067	    }
9068	}
9069
9070      if (ctype == NULL_TREE || check)
9071	return decl;
9072
9073      if (virtualp)
9074	{
9075	  DECL_VIRTUAL_P (decl) = 1;
9076	  if (DECL_VINDEX (decl) == NULL_TREE)
9077	    DECL_VINDEX (decl) = error_mark_node;
9078	  IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
9079	}
9080    }
9081  return decl;
9082}
9083
9084static tree
9085grokvardecl (type, declarator, specbits_in, initialized, constp, in_namespace)
9086     tree type;
9087     tree declarator;
9088     RID_BIT_TYPE *specbits_in;
9089     int initialized;
9090     int constp;
9091     tree in_namespace;
9092{
9093  tree decl;
9094  RID_BIT_TYPE specbits;
9095
9096  specbits = *specbits_in;
9097
9098  if (TREE_CODE (type) == OFFSET_TYPE)
9099    {
9100      /* If you declare a static member so that it
9101	 can be initialized, the code will reach here.  */
9102      tree basetype = TYPE_OFFSET_BASETYPE (type);
9103      type = TREE_TYPE (type);
9104      decl = build_lang_field_decl (VAR_DECL, declarator, type);
9105      DECL_CONTEXT (decl) = basetype;
9106      DECL_CLASS_CONTEXT (decl) = basetype;
9107      DECL_ASSEMBLER_NAME (decl) = build_static_name (basetype, declarator);
9108    }
9109  else
9110    {
9111      tree context;
9112
9113      if (in_namespace)
9114	context = in_namespace;
9115      else if (namespace_bindings_p () || RIDBIT_SETP (RID_EXTERN, specbits))
9116	context = current_namespace;
9117      else
9118	context = NULL_TREE;
9119
9120      decl = build_decl (VAR_DECL, declarator, complete_type (type));
9121
9122      if (context)
9123	set_decl_namespace (decl, context, 0);
9124
9125      context = DECL_CONTEXT (decl);
9126      if (declarator && context && current_lang_name != lang_name_c)
9127	DECL_ASSEMBLER_NAME (decl) = build_static_name (context, declarator);
9128    }
9129
9130  if (in_namespace)
9131    set_decl_namespace (decl, in_namespace, 0);
9132
9133  if (RIDBIT_SETP (RID_EXTERN, specbits))
9134    {
9135      DECL_THIS_EXTERN (decl) = 1;
9136      DECL_EXTERNAL (decl) = !initialized;
9137    }
9138
9139  /* In class context, static means one per class,
9140     public access, and static storage.  */
9141  if (DECL_CLASS_SCOPE_P (decl))
9142    {
9143      TREE_PUBLIC (decl) = 1;
9144      TREE_STATIC (decl) = 1;
9145      DECL_EXTERNAL (decl) = 0;
9146    }
9147  /* At top level, either `static' or no s.c. makes a definition
9148     (perhaps tentative), and absence of `static' makes it public.  */
9149  else if (toplevel_bindings_p ())
9150    {
9151      TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9152			    && (DECL_THIS_EXTERN (decl) || ! constp));
9153      TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9154    }
9155  /* Not at top level, only `static' makes a static definition.  */
9156  else
9157    {
9158      TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9159      TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9160    }
9161
9162  if (TREE_PUBLIC (decl))
9163    {
9164      /* [basic.link]: A name with no linkage (notably, the name of a class
9165	 or enumeration declared in a local scope) shall not be used to
9166	 declare an entity with linkage.
9167
9168	 Only check this for public decls for now.  */
9169      tree t = no_linkage_check (TREE_TYPE (decl));
9170      if (t)
9171	{
9172	  if (ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
9173	    /* Ignore for now; `enum { foo } e' is pretty common.  */;
9174	  else
9175	    cp_pedwarn ("non-local variable `%#D' uses local type `%T'",
9176			decl, t);
9177	}
9178    }
9179
9180  return decl;
9181}
9182
9183/* Create and return a canonical pointer to member function type, for
9184   TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9185
9186tree
9187build_ptrmemfunc_type (type)
9188     tree type;
9189{
9190  tree fields[4];
9191  tree t;
9192  tree u;
9193
9194  /* If a canonical type already exists for this type, use it.  We use
9195     this method instead of type_hash_canon, because it only does a
9196     simple equality check on the list of field members.  */
9197
9198  if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9199    return t;
9200
9201  push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
9202
9203  u = make_lang_type (UNION_TYPE);
9204  SET_IS_AGGR_TYPE (u, 0);
9205  fields[0] = build_lang_field_decl (FIELD_DECL, pfn_identifier, type);
9206  fields[1] = build_lang_field_decl (FIELD_DECL, delta2_identifier,
9207				     delta_type_node);
9208  finish_builtin_type (u, "__ptrmemfunc_type", fields, 1, ptr_type_node);
9209  TYPE_NAME (u) = NULL_TREE;
9210
9211  t = make_lang_type (RECORD_TYPE);
9212
9213  /* Let the front-end know this is a pointer to member function...  */
9214  TYPE_PTRMEMFUNC_FLAG (t) = 1;
9215  /* ... and not really an aggregate.  */
9216  SET_IS_AGGR_TYPE (t, 0);
9217
9218  fields[0] = build_lang_field_decl (FIELD_DECL, delta_identifier,
9219				     delta_type_node);
9220  fields[1] = build_lang_field_decl (FIELD_DECL, index_identifier,
9221				     delta_type_node);
9222  fields[2] = build_lang_field_decl (FIELD_DECL, pfn_or_delta2_identifier, u);
9223  finish_builtin_type (t, "__ptrmemfunc_type", fields, 2, ptr_type_node);
9224
9225  pop_obstacks ();
9226
9227  /* Zap out the name so that the back-end will give us the debugging
9228     information for this anonymous RECORD_TYPE.  */
9229  TYPE_NAME (t) = NULL_TREE;
9230
9231  TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9232
9233  /* Seems to be wanted.  */
9234  CLASSTYPE_GOT_SEMICOLON (t) = 1;
9235  return t;
9236}
9237
9238/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9239   Check to see that the definition is valid.  Issue appropriate error
9240   messages.  Return 1 if the definition is particularly bad, or 0
9241   otherwise.  */
9242
9243int
9244check_static_variable_definition (decl, type)
9245     tree decl;
9246     tree type;
9247{
9248  /* Motion 10 at San Diego: If a static const integral data member is
9249     initialized with an integral constant expression, the initializer
9250     may appear either in the declaration (within the class), or in
9251     the definition, but not both.  If it appears in the class, the
9252     member is a member constant.  The file-scope definition is always
9253     required.  */
9254  if (CLASS_TYPE_P (type) || TREE_CODE (type) == REFERENCE_TYPE)
9255    {
9256      cp_error ("in-class initialization of static data member of non-integral type `%T'",
9257		type);
9258      /* If we just return the declaration, crashes will sometimes
9259	 occur.  We therefore return void_type_node, as if this was a
9260	 friend declaration, to cause callers to completely ignore
9261	 this declaration.  */
9262      return 1;
9263    }
9264  else if (!CP_TYPE_CONST_P (type))
9265    cp_error ("ANSI C++ forbids in-class initialization of non-const static member `%D'",
9266	      decl);
9267  else if (pedantic && !INTEGRAL_TYPE_P (type))
9268    cp_pedwarn ("ANSI C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9269
9270  return 0;
9271}
9272
9273/* Given declspecs and a declarator,
9274   determine the name and type of the object declared
9275   and construct a ..._DECL node for it.
9276   (In one case we can return a ..._TYPE node instead.
9277    For invalid input we sometimes return 0.)
9278
9279   DECLSPECS is a chain of tree_list nodes whose value fields
9280    are the storage classes and type specifiers.
9281
9282   DECL_CONTEXT says which syntactic context this declaration is in:
9283     NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9284     FUNCDEF for a function definition.  Like NORMAL but a few different
9285      error messages in each case.  Return value may be zero meaning
9286      this definition is too screwy to try to parse.
9287     MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
9288      handle member functions (which have FIELD context).
9289      Return value may be zero meaning this definition is too screwy to
9290      try to parse.
9291     PARM for a parameter declaration (either within a function prototype
9292      or before a function body).  Make a PARM_DECL, or return void_type_node.
9293     CATCHPARM for a parameter declaration before a catch clause.
9294     TYPENAME if for a typename (in a cast or sizeof).
9295      Don't make a DECL node; just return the ..._TYPE node.
9296     FIELD for a struct or union field; make a FIELD_DECL.
9297     BITFIELD for a field with specified width.
9298   INITIALIZED is 1 if the decl has an initializer.
9299
9300   ATTRLIST is a TREE_LIST node with prefix attributes in TREE_VALUE and
9301   normal attributes in TREE_PURPOSE, or NULL_TREE.
9302
9303   In the TYPENAME case, DECLARATOR is really an absolute declarator.
9304   It may also be so in the PARM case, for a prototype where the
9305   argument type is specified but not the name.
9306
9307   This function is where the complicated C meanings of `static'
9308   and `extern' are interpreted.
9309
9310   For C++, if there is any monkey business to do, the function which
9311   calls this one must do it, i.e., prepending instance variables,
9312   renaming overloaded function names, etc.
9313
9314   Note that for this C++, it is an error to define a method within a class
9315   which does not belong to that class.
9316
9317   Except in the case where SCOPE_REFs are implicitly known (such as
9318   methods within a class being redundantly qualified),
9319   declarations which involve SCOPE_REFs are returned as SCOPE_REFs
9320   (class_name::decl_name).  The caller must also deal with this.
9321
9322   If a constructor or destructor is seen, and the context is FIELD,
9323   then the type gains the attribute TREE_HAS_x.  If such a declaration
9324   is erroneous, NULL_TREE is returned.
9325
9326   QUALS is used only for FUNCDEF and MEMFUNCDEF cases.  For a member
9327   function, these are the qualifiers to give to the `this' pointer.
9328
9329   May return void_type_node if the declarator turned out to be a friend.
9330   See grokfield for details.  */
9331
9332enum return_types { return_normal, return_ctor, return_dtor, return_conversion };
9333
9334tree
9335grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
9336     tree declspecs;
9337     tree declarator;
9338     enum decl_context decl_context;
9339     int initialized;
9340     tree attrlist;
9341{
9342  RID_BIT_TYPE specbits;
9343  int nclasses = 0;
9344  tree spec;
9345  tree type = NULL_TREE;
9346  int longlong = 0;
9347  int constp;
9348  int restrictp;
9349  int volatilep;
9350  int type_quals;
9351  int virtualp, explicitp, friendp, inlinep, staticp;
9352  int explicit_int = 0;
9353  int explicit_char = 0;
9354  int defaulted_int = 0;
9355  int opaque_typedef = 0;
9356  tree typedef_decl = NULL_TREE;
9357  char *name;
9358  tree typedef_type = NULL_TREE;
9359  int funcdef_flag = 0;
9360  enum tree_code innermost_code = ERROR_MARK;
9361  int bitfield = 0;
9362#if 0
9363  /* See the code below that used this.  */
9364  tree decl_machine_attr = NULL_TREE;
9365#endif
9366  /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
9367     All FIELD_DECLs we build here have `init' put into their DECL_INITIAL.  */
9368  tree init = NULL_TREE;
9369
9370  /* Keep track of what sort of function is being processed
9371     so that we can warn about default return values, or explicit
9372     return values which do not match prescribed defaults.  */
9373  enum return_types return_type = return_normal;
9374
9375  tree dname = NULL_TREE;
9376  tree ctype = current_class_type;
9377  tree ctor_return_type = NULL_TREE;
9378  enum overload_flags flags = NO_SPECIAL;
9379  tree quals = NULL_TREE;
9380  tree raises = NULL_TREE;
9381  int template_count = 0;
9382  tree in_namespace = NULL_TREE;
9383  tree inner_attrs;
9384  int ignore_attrs;
9385
9386  RIDBIT_RESET_ALL (specbits);
9387  if (decl_context == FUNCDEF)
9388    funcdef_flag = 1, decl_context = NORMAL;
9389  else if (decl_context == MEMFUNCDEF)
9390    funcdef_flag = -1, decl_context = FIELD;
9391  else if (decl_context == BITFIELD)
9392    bitfield = 1, decl_context = FIELD;
9393
9394  /* Look inside a declarator for the name being declared
9395     and get it as a string, for an error message.  */
9396  {
9397    tree *next = &declarator;
9398    register tree decl;
9399    name = NULL;
9400
9401    while (next && *next)
9402      {
9403	decl = *next;
9404	switch (TREE_CODE (decl))
9405	  {
9406	  case TREE_LIST:
9407	    /* For attributes.  */
9408	    next = &TREE_VALUE (decl);
9409	    break;
9410
9411	  case COND_EXPR:
9412	    ctype = NULL_TREE;
9413	    next = &TREE_OPERAND (decl, 0);
9414	    break;
9415
9416	  case BIT_NOT_EXPR:	/* For C++ destructors!  */
9417	    {
9418	      tree name = TREE_OPERAND (decl, 0);
9419	      tree rename = NULL_TREE;
9420
9421	      my_friendly_assert (flags == NO_SPECIAL, 152);
9422	      flags = DTOR_FLAG;
9423	      return_type = return_dtor;
9424	      if (TREE_CODE (name) == TYPE_DECL)
9425		TREE_OPERAND (decl, 0) = name = constructor_name (name);
9426	      my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9427	      if (ctype == NULL_TREE)
9428		{
9429		  if (current_class_type == NULL_TREE)
9430		    {
9431		      error ("destructors must be member functions");
9432		      flags = NO_SPECIAL;
9433		    }
9434		  else
9435		    {
9436		      tree t = constructor_name (current_class_name);
9437		      if (t != name)
9438			rename = t;
9439		    }
9440		}
9441	      else
9442		{
9443		  tree t = constructor_name (ctype);
9444		  if (t != name)
9445		    rename = t;
9446		}
9447
9448	      if (rename)
9449		{
9450		  cp_error ("destructor `%T' must match class name `%T'",
9451			    name, rename);
9452		  TREE_OPERAND (decl, 0) = rename;
9453		}
9454	      next = &name;
9455	    }
9456	    break;
9457
9458	  case ADDR_EXPR:	/* C++ reference declaration */
9459	    /* Fall through. */
9460	  case ARRAY_REF:
9461	  case INDIRECT_REF:
9462	    ctype = NULL_TREE;
9463	    innermost_code = TREE_CODE (decl);
9464	    next = &TREE_OPERAND (decl, 0);
9465	    break;
9466
9467	  case CALL_EXPR:
9468	    if (parmlist_is_exprlist (TREE_OPERAND (decl, 1)))
9469	      {
9470		/* This is actually a variable declaration using
9471		   constructor syntax.  We need to call start_decl and
9472		   cp_finish_decl so we can get the variable
9473		   initialized...  */
9474
9475		tree attributes, prefix_attributes;
9476
9477		*next = TREE_OPERAND (decl, 0);
9478		init = TREE_OPERAND (decl, 1);
9479
9480		if (attrlist)
9481		  {
9482		    attributes = TREE_PURPOSE (attrlist);
9483		    prefix_attributes = TREE_VALUE (attrlist);
9484		  }
9485		else
9486		  {
9487		    attributes = NULL_TREE;
9488		    prefix_attributes = NULL_TREE;
9489		  }
9490
9491		decl = start_decl (declarator, declspecs, 1,
9492				   attributes, prefix_attributes);
9493		if (decl)
9494		  {
9495		    /* Look for __unused__ attribute */
9496		    if (TREE_USED (TREE_TYPE (decl)))
9497		      TREE_USED (decl) = 1;
9498		    finish_decl (decl, init, NULL_TREE);
9499		  }
9500		else
9501		  cp_error ("invalid declarator");
9502		return 0;
9503	      }
9504	    innermost_code = TREE_CODE (decl);
9505	    if (decl_context == FIELD && ctype == NULL_TREE)
9506	      ctype = current_class_type;
9507	    if (ctype
9508		&& TREE_OPERAND (decl, 0)
9509		&& (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9510		    && ((DECL_NAME (TREE_OPERAND (decl, 0))
9511			 == constructor_name_full (ctype))
9512			|| (DECL_NAME (TREE_OPERAND (decl, 0))
9513			    == constructor_name (ctype)))))
9514	      TREE_OPERAND (decl, 0) = constructor_name (ctype);
9515	    next = &TREE_OPERAND (decl, 0);
9516	    decl = *next;
9517	    if (ctype != NULL_TREE
9518		&& decl != NULL_TREE && flags != DTOR_FLAG
9519		&& decl == constructor_name (ctype))
9520	      {
9521		return_type = return_ctor;
9522		ctor_return_type = ctype;
9523	      }
9524	    ctype = NULL_TREE;
9525	    break;
9526
9527	  case TEMPLATE_ID_EXPR:
9528	      {
9529		tree fns = TREE_OPERAND (decl, 0);
9530
9531		if (TREE_CODE (fns) == LOOKUP_EXPR)
9532		  fns = TREE_OPERAND (fns, 0);
9533
9534		dname = fns;
9535		if (TREE_CODE (dname) == COMPONENT_REF)
9536		  dname = TREE_OPERAND (dname, 1);
9537		if (TREE_CODE (dname) != IDENTIFIER_NODE)
9538		  {
9539		    my_friendly_assert (is_overloaded_fn (dname),
9540					19990331);
9541		    dname = DECL_NAME (get_first_fn (dname));
9542		  }
9543	      }
9544	  /* Fall through. */
9545
9546	  case IDENTIFIER_NODE:
9547	    if (TREE_CODE (decl) == IDENTIFIER_NODE)
9548	      dname = decl;
9549
9550	    next = 0;
9551
9552	    if (is_rid (dname))
9553	      {
9554		cp_error ("declarator-id missing; using reserved word `%D'",
9555			  dname);
9556		name = IDENTIFIER_POINTER (dname);
9557	      }
9558	    if (! IDENTIFIER_OPNAME_P (dname)
9559		/* GNU/Linux headers use '__op'.  Arrgh.  */
9560		|| (IDENTIFIER_TYPENAME_P (dname) && ! TREE_TYPE (dname)))
9561	      name = IDENTIFIER_POINTER (dname);
9562	    else
9563	      {
9564		if (IDENTIFIER_TYPENAME_P (dname))
9565		  {
9566		    my_friendly_assert (flags == NO_SPECIAL, 154);
9567		    flags = TYPENAME_FLAG;
9568		    ctor_return_type = TREE_TYPE (dname);
9569		    return_type = return_conversion;
9570		  }
9571		name = operator_name_string (dname);
9572	      }
9573	    break;
9574
9575	    /* C++ extension */
9576	  case SCOPE_REF:
9577	    {
9578	      /* Perform error checking, and decide on a ctype.  */
9579	      tree cname = TREE_OPERAND (decl, 0);
9580	      if (cname == NULL_TREE)
9581		ctype = NULL_TREE;
9582	      else if (TREE_CODE (cname) == NAMESPACE_DECL)
9583		{
9584		  ctype = NULL_TREE;
9585		  in_namespace = TREE_OPERAND (decl, 0);
9586		  TREE_OPERAND (decl, 0) = NULL_TREE;
9587		}
9588	      else if (! is_aggr_type (cname, 1))
9589		TREE_OPERAND (decl, 0) = NULL_TREE;
9590	      /* Must test TREE_OPERAND (decl, 1), in case user gives
9591		 us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9592	      else if (TREE_OPERAND (decl, 1)
9593		       && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9594		ctype = cname;
9595	      else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9596		       || TREE_CODE (cname) == TEMPLATE_TEMPLATE_PARM)
9597		{
9598		  cp_error ("`%T::%D' is not a valid declarator", cname,
9599			    TREE_OPERAND (decl, 1));
9600		  cp_error ("  perhaps you want `typename %T::%D' to make it a type",
9601			    cname, TREE_OPERAND (decl, 1));
9602		  return void_type_node;
9603		}
9604	      else if (ctype == NULL_TREE)
9605		ctype = cname;
9606	      else if (TREE_COMPLEXITY (decl) == current_class_depth)
9607		TREE_OPERAND (decl, 0) = ctype;
9608	      else
9609		{
9610		  if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9611		    {
9612		      cp_error ("type `%T' is not derived from type `%T'",
9613				cname, ctype);
9614		      TREE_OPERAND (decl, 0) = NULL_TREE;
9615		    }
9616		  else
9617		    ctype = cname;
9618		}
9619
9620	      if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9621		  && ((DECL_NAME (TREE_OPERAND (decl, 1))
9622		       == constructor_name_full (ctype))
9623		      || (DECL_NAME (TREE_OPERAND (decl, 1))
9624			  == constructor_name (ctype))))
9625		TREE_OPERAND (decl, 1) = constructor_name (ctype);
9626	      next = &TREE_OPERAND (decl, 1);
9627	      decl = *next;
9628	      if (ctype)
9629		{
9630		  if (TREE_CODE (decl) == IDENTIFIER_NODE
9631		      && constructor_name (ctype) == decl)
9632		    {
9633		      return_type = return_ctor;
9634		      ctor_return_type = ctype;
9635		    }
9636		  else if (TREE_CODE (decl) == BIT_NOT_EXPR
9637			   && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9638			   && (constructor_name (ctype) == TREE_OPERAND (decl, 0)
9639			       || constructor_name_full (ctype) == TREE_OPERAND (decl, 0)))
9640		    {
9641		      return_type = return_dtor;
9642		      ctor_return_type = ctype;
9643		      flags = DTOR_FLAG;
9644		      TREE_OPERAND (decl, 0) = constructor_name (ctype);
9645		      next = &TREE_OPERAND (decl, 0);
9646		    }
9647		}
9648	    }
9649	    break;
9650
9651	  case ERROR_MARK:
9652	    next = 0;
9653	    break;
9654
9655	  case TYPE_DECL:
9656	    /* Parse error puts this typespec where
9657	       a declarator should go.  */
9658	    cp_error ("`%T' specified as declarator-id", DECL_NAME (decl));
9659	    if (TREE_TYPE (decl) == current_class_type)
9660	      cp_error ("  perhaps you want `%T' for a constructor",
9661			current_class_name);
9662	    dname = DECL_NAME (decl);
9663	    name = IDENTIFIER_POINTER (dname);
9664
9665	    /* Avoid giving two errors for this.  */
9666	    IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9667
9668	    declspecs = temp_tree_cons (NULL_TREE, integer_type_node,
9669					declspecs);
9670	    *next = dname;
9671	    next = 0;
9672	    break;
9673
9674	  default:
9675	    cp_compiler_error ("`%D' as declarator", decl);
9676	    return 0; /* We used to do a 155 abort here.  */
9677	  }
9678      }
9679    if (name == NULL)
9680      name = "type name";
9681  }
9682
9683  /* A function definition's declarator must have the form of
9684     a function declarator.  */
9685
9686  if (funcdef_flag && innermost_code != CALL_EXPR)
9687    return 0;
9688
9689  if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9690      && innermost_code != CALL_EXPR
9691      && ! (ctype && declspecs == NULL_TREE))
9692    {
9693      cp_error ("declaration of `%D' as non-function", dname);
9694      return void_type_node;
9695    }
9696
9697  /* Anything declared one level down from the top level
9698     must be one of the parameters of a function
9699     (because the body is at least two levels down).  */
9700
9701  /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9702     by not allowing C++ class definitions to specify their parameters
9703     with xdecls (must be spec.d in the parmlist).
9704
9705     Since we now wait to push a class scope until we are sure that
9706     we are in a legitimate method context, we must set oldcname
9707     explicitly (since current_class_name is not yet alive).
9708
9709     We also want to avoid calling this a PARM if it is in a namespace.  */
9710
9711  if (decl_context == NORMAL && ! namespace_bindings_p ()
9712      && ! pseudo_global_level_p ())
9713    {
9714      struct binding_level *b = current_binding_level;
9715      current_binding_level = b->level_chain;
9716      if (current_binding_level != 0 && toplevel_bindings_p ())
9717	decl_context = PARM;
9718      current_binding_level = b;
9719    }
9720
9721  /* Look through the decl specs and record which ones appear.
9722     Some typespecs are defined as built-in typenames.
9723     Others, the ones that are modifiers of other types,
9724     are represented by bits in SPECBITS: set the bits for
9725     the modifiers that appear.  Storage class keywords are also in SPECBITS.
9726
9727     If there is a typedef name or a type, store the type in TYPE.
9728     This includes builtin typedefs such as `int'.
9729
9730     Set EXPLICIT_INT if the type is `int' or `char' and did not
9731     come from a user typedef.
9732
9733     Set LONGLONG if `long' is mentioned twice.
9734
9735     For C++, constructors and destructors have their own fast treatment.  */
9736
9737  for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
9738    {
9739      register int i;
9740      register tree id;
9741
9742      /* Certain parse errors slip through.  For example,
9743	 `int class;' is not caught by the parser. Try
9744	 weakly to recover here.  */
9745      if (TREE_CODE (spec) != TREE_LIST)
9746	return 0;
9747
9748      id = TREE_VALUE (spec);
9749
9750      if (TREE_CODE (id) == IDENTIFIER_NODE)
9751	{
9752	  if (id == ridpointers[(int) RID_INT]
9753	      || id == ridpointers[(int) RID_CHAR]
9754	      || id == ridpointers[(int) RID_BOOL]
9755	      || id == ridpointers[(int) RID_WCHAR])
9756	    {
9757	      if (type)
9758		{
9759		  if (id == ridpointers[(int) RID_BOOL])
9760		    error ("`bool' is now a keyword");
9761		  else
9762		    cp_error ("extraneous `%T' ignored", id);
9763		}
9764	      else
9765		{
9766		  if (id == ridpointers[(int) RID_INT])
9767		    explicit_int = 1;
9768		  else if (id == ridpointers[(int) RID_CHAR])
9769		    explicit_char = 1;
9770		  type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
9771		}
9772	      goto found;
9773	    }
9774	  /* C++ aggregate types.  */
9775	  if (IDENTIFIER_HAS_TYPE_VALUE (id))
9776	    {
9777	      if (type)
9778		cp_error ("multiple declarations `%T' and `%T'", type, id);
9779	      else
9780		type = IDENTIFIER_TYPE_VALUE (id);
9781	      goto found;
9782	    }
9783
9784	  for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
9785	    {
9786	      if (ridpointers[i] == id)
9787		{
9788		  if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
9789		    {
9790		      if (pedantic && ! in_system_header && warn_long_long)
9791			pedwarn ("ANSI C++ does not support `long long'");
9792		      if (longlong)
9793			error ("`long long long' is too long for GCC");
9794		      else
9795			longlong = 1;
9796		    }
9797		  else if (RIDBIT_SETP (i, specbits))
9798		    pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
9799		  RIDBIT_SET (i, specbits);
9800		  goto found;
9801		}
9802	    }
9803	}
9804      /* C++ aggregate types.  */
9805      else if (TREE_CODE (id) == TYPE_DECL || TREE_CODE (id) == TEMPLATE_DECL)
9806	{
9807	  if (type)
9808	    cp_error ("multiple declarations `%T' and `%T'", type,
9809		      TREE_TYPE (id));
9810	  else
9811	    {
9812	      type = TREE_TYPE (id);
9813	      TREE_VALUE (spec) = type;
9814	    }
9815	  goto found;
9816	}
9817      if (type)
9818	error ("two or more data types in declaration of `%s'", name);
9819      else if (TREE_CODE (id) == IDENTIFIER_NODE)
9820	{
9821	  register tree t = lookup_name (id, 1);
9822	  if (!t || TREE_CODE (t) != TYPE_DECL)
9823	    error ("`%s' fails to be a typedef or built in type",
9824		   IDENTIFIER_POINTER (id));
9825	  else
9826	    {
9827	      type = TREE_TYPE (t);
9828#if 0
9829	      /* See the code below that used this.  */
9830	      decl_machine_attr = DECL_MACHINE_ATTRIBUTES (id);
9831#endif
9832	      typedef_decl = t;
9833	    }
9834	}
9835      else if (id != error_mark_node)
9836	/* Can't change CLASS nodes into RECORD nodes here!  */
9837	type = id;
9838
9839    found: ;
9840    }
9841
9842  typedef_type = type;
9843
9844  /* No type at all: default to `int', and set DEFAULTED_INT
9845     because it was not a user-defined typedef.
9846     Except when we have a `typedef' inside a signature, in
9847     which case the type defaults to `unknown type' and is
9848     instantiated when assigning to a signature pointer or ref.  */
9849
9850  if (type == NULL_TREE
9851      && (RIDBIT_SETP (RID_SIGNED, specbits)
9852	  || RIDBIT_SETP (RID_UNSIGNED, specbits)
9853	  || RIDBIT_SETP (RID_LONG, specbits)
9854	  || RIDBIT_SETP (RID_SHORT, specbits)))
9855    {
9856      /* These imply 'int'.  */
9857      type = integer_type_node;
9858      defaulted_int = 1;
9859    }
9860
9861  if (type == NULL_TREE)
9862    {
9863      explicit_int = -1;
9864      if (return_type == return_dtor)
9865	type = void_type_node;
9866      else if (return_type == return_ctor)
9867	type = build_pointer_type (ctor_return_type);
9868      else if (return_type == return_conversion)
9869	type = ctor_return_type;
9870      else if (current_class_type
9871	       && IS_SIGNATURE (current_class_type)
9872	       && RIDBIT_SETP (RID_TYPEDEF, specbits)
9873	       && (decl_context == FIELD || decl_context == NORMAL))
9874	{
9875	  explicit_int = 0;
9876	  opaque_typedef = 1;
9877	  type = copy_node (opaque_type_node);
9878	}
9879      else
9880	{
9881	  /* We handle `main' specially here, because 'main () { }' is so
9882	     common.  With no options, it is allowed.  With -Wreturn-type,
9883	     it is a warning.  It is only an error with -pedantic-errors.  */
9884	  int is_main = (funcdef_flag
9885			 && MAIN_NAME_P (dname)
9886			 && ctype == NULL_TREE
9887			 && in_namespace == NULL_TREE
9888			 && current_namespace == global_namespace);
9889
9890	  if (in_system_header)
9891	    /* Allow it, sigh.  */;
9892	  else if (pedantic || ! is_main)
9893	    cp_pedwarn ("ANSI C++ forbids declaration `%D' with no type",
9894			dname);
9895	  else if (warn_return_type)
9896	    cp_warning ("ANSI C++ forbids declaration `%D' with no type",
9897			dname);
9898
9899	  type = integer_type_node;
9900	}
9901    }
9902  else if (return_type == return_dtor)
9903    {
9904      error ("return type specification for destructor invalid");
9905      type = void_type_node;
9906    }
9907  else if (return_type == return_ctor)
9908    {
9909      error ("return type specification for constructor invalid");
9910      type = build_pointer_type (ctor_return_type);
9911    }
9912  else if (return_type == return_conversion)
9913    {
9914      if (!same_type_p (type, ctor_return_type))
9915	cp_error ("operator `%T' declared to return `%T'",
9916		  ctor_return_type, type);
9917      else
9918	cp_pedwarn ("return type specified for `operator %T'",
9919		    ctor_return_type);
9920
9921      type = ctor_return_type;
9922    }
9923
9924  ctype = NULL_TREE;
9925
9926  /* Now process the modifiers that were specified
9927     and check for invalid combinations.  */
9928
9929  /* Long double is a special combination.  */
9930
9931  if (RIDBIT_SETP (RID_LONG, specbits)
9932      && TYPE_MAIN_VARIANT (type) == double_type_node)
9933    {
9934      RIDBIT_RESET (RID_LONG, specbits);
9935      type = build_qualified_type (long_double_type_node,
9936				   CP_TYPE_QUALS (type));
9937    }
9938
9939  /* Check all other uses of type modifiers.  */
9940
9941  if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9942      || RIDBIT_SETP (RID_SIGNED, specbits)
9943      || RIDBIT_SETP (RID_LONG, specbits)
9944      || RIDBIT_SETP (RID_SHORT, specbits))
9945    {
9946      int ok = 0;
9947
9948      if (TREE_CODE (type) == REAL_TYPE)
9949	error ("short, signed or unsigned invalid for `%s'", name);
9950      else if (TREE_CODE (type) != INTEGER_TYPE)
9951	error ("long, short, signed or unsigned invalid for `%s'", name);
9952      else if (RIDBIT_SETP (RID_LONG, specbits)
9953	       && RIDBIT_SETP (RID_SHORT, specbits))
9954	error ("long and short specified together for `%s'", name);
9955      else if ((RIDBIT_SETP (RID_LONG, specbits)
9956		|| RIDBIT_SETP (RID_SHORT, specbits))
9957	       && explicit_char)
9958	error ("long or short specified with char for `%s'", name);
9959      else if ((RIDBIT_SETP (RID_LONG, specbits)
9960		|| RIDBIT_SETP (RID_SHORT, specbits))
9961	       && TREE_CODE (type) == REAL_TYPE)
9962	error ("long or short specified with floating type for `%s'", name);
9963      else if (RIDBIT_SETP (RID_SIGNED, specbits)
9964	       && RIDBIT_SETP (RID_UNSIGNED, specbits))
9965	error ("signed and unsigned given together for `%s'", name);
9966      else
9967	{
9968	  ok = 1;
9969	  if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
9970	    {
9971	      pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
9972		       name);
9973	      if (flag_pedantic_errors)
9974		ok = 0;
9975	    }
9976	}
9977
9978      /* Discard the type modifiers if they are invalid.  */
9979      if (! ok)
9980	{
9981	  RIDBIT_RESET (RID_UNSIGNED, specbits);
9982	  RIDBIT_RESET (RID_SIGNED, specbits);
9983	  RIDBIT_RESET (RID_LONG, specbits);
9984	  RIDBIT_RESET (RID_SHORT, specbits);
9985	  longlong = 0;
9986	}
9987    }
9988
9989  if (RIDBIT_SETP (RID_COMPLEX, specbits)
9990      && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
9991    {
9992      error ("complex invalid for `%s'", name);
9993      RIDBIT_RESET (RID_COMPLEX, specbits);
9994    }
9995
9996  /* Decide whether an integer type is signed or not.
9997     Optionally treat bitfields as signed by default.  */
9998  if (RIDBIT_SETP (RID_UNSIGNED, specbits)
9999      || (bitfield && ! flag_signed_bitfields
10000	  && (explicit_int || defaulted_int || explicit_char
10001	      /* A typedef for plain `int' without `signed'
10002		 can be controlled just like plain `int'.  */
10003	      || ! (typedef_decl != NULL_TREE
10004		    && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
10005	  && TREE_CODE (type) != ENUMERAL_TYPE
10006	  && RIDBIT_NOTSETP (RID_SIGNED, specbits)))
10007    {
10008      if (longlong)
10009	type = long_long_unsigned_type_node;
10010      else if (RIDBIT_SETP (RID_LONG, specbits))
10011	type = long_unsigned_type_node;
10012      else if (RIDBIT_SETP (RID_SHORT, specbits))
10013	type = short_unsigned_type_node;
10014      else if (type == char_type_node)
10015	type = unsigned_char_type_node;
10016      else if (typedef_decl)
10017	type = unsigned_type (type);
10018      else
10019	type = unsigned_type_node;
10020    }
10021  else if (RIDBIT_SETP (RID_SIGNED, specbits)
10022	   && type == char_type_node)
10023    type = signed_char_type_node;
10024  else if (longlong)
10025    type = long_long_integer_type_node;
10026  else if (RIDBIT_SETP (RID_LONG, specbits))
10027    type = long_integer_type_node;
10028  else if (RIDBIT_SETP (RID_SHORT, specbits))
10029    type = short_integer_type_node;
10030
10031  if (RIDBIT_SETP (RID_COMPLEX, specbits))
10032    {
10033      /* If we just have "complex", it is equivalent to
10034	 "complex double", but if any modifiers at all are specified it is
10035	 the complex form of TYPE.  E.g, "complex short" is
10036	 "complex short int".  */
10037
10038      if (defaulted_int && ! longlong
10039	  && ! (RIDBIT_SETP (RID_LONG, specbits)
10040		|| RIDBIT_SETP (RID_SHORT, specbits)
10041		|| RIDBIT_SETP (RID_SIGNED, specbits)
10042		|| RIDBIT_SETP (RID_UNSIGNED, specbits)))
10043	type = complex_double_type_node;
10044      else if (type == integer_type_node)
10045	type = complex_integer_type_node;
10046      else if (type == float_type_node)
10047	type = complex_float_type_node;
10048      else if (type == double_type_node)
10049	type = complex_double_type_node;
10050      else if (type == long_double_type_node)
10051	type = complex_long_double_type_node;
10052      else
10053	type = build_complex_type (type);
10054    }
10055
10056  if (return_type == return_conversion
10057      && (RIDBIT_SETP (RID_CONST, specbits)
10058	  || RIDBIT_SETP (RID_VOLATILE, specbits)
10059	  || RIDBIT_SETP (RID_RESTRICT, specbits)))
10060    cp_error ("qualifiers are not allowed on declaration of `operator %T'",
10061	      ctor_return_type);
10062
10063  /* Set CONSTP if this declaration is `const', whether by
10064     explicit specification or via a typedef.
10065     Likewise for VOLATILEP.  */
10066
10067  constp = !! RIDBIT_SETP (RID_CONST, specbits) + CP_TYPE_CONST_P (type);
10068  restrictp =
10069    !! RIDBIT_SETP (RID_RESTRICT, specbits) + CP_TYPE_RESTRICT_P (type);
10070  volatilep =
10071    !! RIDBIT_SETP (RID_VOLATILE, specbits) + CP_TYPE_VOLATILE_P (type);
10072  type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10073		| (restrictp ? TYPE_QUAL_RESTRICT : 0)
10074		| (volatilep ? TYPE_QUAL_VOLATILE : 0));
10075  type = cp_build_qualified_type (type, type_quals);
10076  staticp = 0;
10077  inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10078  virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10079  RIDBIT_RESET (RID_VIRTUAL, specbits);
10080  explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10081  RIDBIT_RESET (RID_EXPLICIT, specbits);
10082
10083  if (RIDBIT_SETP (RID_STATIC, specbits))
10084    staticp = 1 + (decl_context == FIELD);
10085
10086  if (virtualp && staticp == 2)
10087    {
10088      cp_error ("member `%D' cannot be declared both virtual and static",
10089		dname);
10090      staticp = 0;
10091    }
10092  friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10093  RIDBIT_RESET (RID_FRIEND, specbits);
10094
10095  /* $7.1.2, Function specifiers */
10096  if (friendp && explicitp)
10097    error ("only declarations of constructors can be `explicit'");
10098
10099  if (RIDBIT_SETP (RID_MUTABLE, specbits))
10100    {
10101      if (decl_context == PARM)
10102	{
10103	  error ("non-member `%s' cannot be declared `mutable'", name);
10104	  RIDBIT_RESET (RID_MUTABLE, specbits);
10105	}
10106      else if (friendp || decl_context == TYPENAME)
10107	{
10108	  error ("non-object member `%s' cannot be declared `mutable'", name);
10109	  RIDBIT_RESET (RID_MUTABLE, specbits);
10110	}
10111    }
10112
10113  /* Warn if two storage classes are given. Default to `auto'.  */
10114
10115  if (RIDBIT_ANY_SET (specbits))
10116    {
10117      if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10118      if (RIDBIT_SETP (RID_EXTERN, specbits)) nclasses++;
10119      if (decl_context == PARM && nclasses > 0)
10120	error ("storage class specifiers invalid in parameter declarations");
10121      if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10122	{
10123	  if (decl_context == PARM)
10124	    error ("typedef declaration invalid in parameter declaration");
10125	  nclasses++;
10126	}
10127      if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10128      if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10129    }
10130
10131  /* Give error if `virtual' is used outside of class declaration.  */
10132  if (virtualp
10133      && (current_class_name == NULL_TREE || decl_context != FIELD))
10134    {
10135      error ("virtual outside class declaration");
10136      virtualp = 0;
10137    }
10138  if (current_class_name == NULL_TREE && RIDBIT_SETP (RID_MUTABLE, specbits))
10139    {
10140      error ("only members can be declared mutable");
10141      RIDBIT_RESET (RID_MUTABLE, specbits);
10142    }
10143
10144  /* Static anonymous unions are dealt with here.  */
10145  if (staticp && decl_context == TYPENAME
10146      && TREE_CODE (declspecs) == TREE_LIST
10147      && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10148    decl_context = FIELD;
10149
10150  /* Give error if `const,' `volatile,' `inline,' `friend,' or `virtual'
10151     is used in a signature member function declaration.  */
10152  if (decl_context == FIELD
10153      && IS_SIGNATURE (current_class_type)
10154      && RIDBIT_NOTSETP (RID_TYPEDEF, specbits))
10155    {
10156      if (type_quals != TYPE_UNQUALIFIED)
10157	{
10158	  error ("type qualifiers specified for signature member function `%s'", name);
10159	  type_quals = TYPE_UNQUALIFIED;
10160	}
10161      if (inlinep)
10162	{
10163	  error ("`inline' specified for signature member function `%s'", name);
10164	  /* Later, we'll make signature member functions inline.  */
10165	  inlinep = 0;
10166	}
10167      if (friendp)
10168	{
10169	  error ("`friend' declaration in signature definition");
10170	  friendp = 0;
10171	}
10172      if (virtualp)
10173	{
10174	  error ("`virtual' specified for signature member function `%s'",
10175		 name);
10176	  /* Later, we'll make signature member functions virtual.  */
10177	  virtualp = 0;
10178	}
10179    }
10180
10181  /* Warn about storage classes that are invalid for certain
10182     kinds of declarations (parameters, typenames, etc.).  */
10183
10184  if (nclasses > 1)
10185    error ("multiple storage classes in declaration of `%s'", name);
10186  else if (decl_context != NORMAL && nclasses > 0)
10187    {
10188      if ((decl_context == PARM || decl_context == CATCHPARM)
10189	  && (RIDBIT_SETP (RID_REGISTER, specbits)
10190	      || RIDBIT_SETP (RID_AUTO, specbits)))
10191	;
10192      else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10193	;
10194      else if (decl_context == FIELD
10195	       && ! IS_SIGNATURE (current_class_type)
10196 	       /* C++ allows static class elements  */
10197 	       && RIDBIT_SETP (RID_STATIC, specbits))
10198 	/* C++ also allows inlines and signed and unsigned elements,
10199 	   but in those cases we don't come in here.  */
10200	;
10201      else
10202	{
10203	  if (decl_context == FIELD)
10204	    {
10205	      tree tmp = NULL_TREE;
10206	      register int op = 0;
10207
10208	      if (declarator)
10209		{
10210		  /* Avoid trying to get an operand off an identifier node.  */
10211		  if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10212		    tmp = declarator;
10213		  else
10214		    tmp = TREE_OPERAND (declarator, 0);
10215		  op = IDENTIFIER_OPNAME_P (tmp);
10216		}
10217	      error ("storage class specified for %s `%s'",
10218		     IS_SIGNATURE (current_class_type)
10219		     ? (op
10220			? "signature member operator"
10221			: "signature member function")
10222		     : (op ? "member operator" : "field"),
10223		     op ? operator_name_string (tmp) : name);
10224	    }
10225	  else
10226	    error (((decl_context == PARM || decl_context == CATCHPARM)
10227		    ? "storage class specified for parameter `%s'"
10228		    : "storage class specified for typename"), name);
10229	  RIDBIT_RESET (RID_REGISTER, specbits);
10230	  RIDBIT_RESET (RID_AUTO, specbits);
10231	  RIDBIT_RESET (RID_EXTERN, specbits);
10232
10233	  if (decl_context == FIELD && IS_SIGNATURE (current_class_type))
10234	    {
10235	      RIDBIT_RESET (RID_STATIC, specbits);
10236	      staticp = 0;
10237	    }
10238	}
10239    }
10240  else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10241    {
10242      if (toplevel_bindings_p ())
10243	{
10244	  /* It's common practice (and completely valid) to have a const
10245	     be initialized and declared extern.  */
10246	  if (!(type_quals & TYPE_QUAL_CONST))
10247	    warning ("`%s' initialized and declared `extern'", name);
10248	}
10249      else
10250	error ("`%s' has both `extern' and initializer", name);
10251    }
10252  else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10253	   && ! toplevel_bindings_p ())
10254    error ("nested function `%s' declared `extern'", name);
10255  else if (toplevel_bindings_p ())
10256    {
10257      if (RIDBIT_SETP (RID_AUTO, specbits))
10258	error ("top-level declaration of `%s' specifies `auto'", name);
10259    }
10260
10261  if (nclasses > 0 && friendp)
10262    error ("storage class specifiers invalid in friend function declarations");
10263
10264  /* Now figure out the structure of the declarator proper.
10265     Descend through it, creating more complex types, until we reach
10266     the declared identifier (or NULL_TREE, in an absolute declarator).  */
10267
10268  inner_attrs = NULL_TREE;
10269  ignore_attrs = 0;
10270
10271  while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10272	 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10273    {
10274      /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10275	 an INDIRECT_REF (for *...),
10276	 a CALL_EXPR (for ...(...)),
10277	 an identifier (for the name being declared)
10278	 or a null pointer (for the place in an absolute declarator
10279	 where the name was omitted).
10280	 For the last two cases, we have just exited the loop.
10281
10282	 For C++ it could also be
10283	 a SCOPE_REF (for class :: ...).  In this case, we have converted
10284	 sensible names to types, and those are the values we use to
10285	 qualify the member name.
10286	 an ADDR_EXPR (for &...),
10287	 a BIT_NOT_EXPR (for destructors)
10288
10289	 At this point, TYPE is the type of elements of an array,
10290	 or for a function to return, or for a pointer to point to.
10291	 After this sequence of ifs, TYPE is the type of the
10292	 array or function or pointer, and DECLARATOR has had its
10293	 outermost layer removed.  */
10294
10295      if (type == error_mark_node)
10296	{
10297	  if (TREE_CODE (declarator) == SCOPE_REF)
10298	    declarator = TREE_OPERAND (declarator, 1);
10299	  else
10300	    declarator = TREE_OPERAND (declarator, 0);
10301	  continue;
10302	}
10303      if (quals != NULL_TREE
10304	  && (declarator == NULL_TREE
10305	      || TREE_CODE (declarator) != SCOPE_REF))
10306	{
10307	  if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10308	    ctype = TYPE_METHOD_BASETYPE (type);
10309	  if (ctype != NULL_TREE)
10310	    {
10311	      tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10312	      ctype = grok_method_quals (ctype, dummy, quals);
10313	      type = TREE_TYPE (dummy);
10314	      quals = NULL_TREE;
10315	    }
10316	}
10317
10318      /* See the comment for the TREE_LIST case, below.  */
10319      if (ignore_attrs)
10320	ignore_attrs = 0;
10321      else if (inner_attrs)
10322	{
10323	  decl_attributes (type, inner_attrs, NULL_TREE);
10324	  inner_attrs = NULL_TREE;
10325	}
10326
10327      switch (TREE_CODE (declarator))
10328	{
10329	case TREE_LIST:
10330	  {
10331	    /* We encode a declarator with embedded attributes using
10332	       a TREE_LIST.  The attributes apply to the declarator
10333	       directly inside them, so we have to skip an iteration
10334	       before applying them to the type.  If the declarator just
10335	       inside is the declarator-id, we apply the attrs to the
10336	       decl itself.  */
10337	    inner_attrs = TREE_PURPOSE (declarator);
10338	    ignore_attrs = 1;
10339	    declarator = TREE_VALUE (declarator);
10340	  }
10341	  break;
10342
10343	case ARRAY_REF:
10344	  {
10345	    register tree itype = NULL_TREE;
10346	    register tree size = TREE_OPERAND (declarator, 1);
10347	    /* The index is a signed object `sizetype' bits wide.  */
10348	    tree index_type = signed_type (sizetype);
10349
10350	    declarator = TREE_OPERAND (declarator, 0);
10351
10352	    /* Check for some types that there cannot be arrays of.  */
10353
10354	    if (TREE_CODE (type) == VOID_TYPE)
10355	      {
10356		cp_error ("declaration of `%D' as array of voids", dname);
10357		type = error_mark_node;
10358	      }
10359
10360	    if (TREE_CODE (type) == FUNCTION_TYPE)
10361	      {
10362		cp_error ("declaration of `%D' as array of functions", dname);
10363		type = error_mark_node;
10364	      }
10365
10366	    /* ARM $8.4.3: Since you can't have a pointer to a reference,
10367	       you can't have arrays of references.  If we allowed them,
10368	       then we'd be saying x[i] is valid for an array x, but
10369	       then you'd have to ask: what does `*(x + i)' mean?  */
10370	    if (TREE_CODE (type) == REFERENCE_TYPE)
10371	      {
10372		if (decl_context == TYPENAME)
10373		  cp_error ("cannot make arrays of references");
10374		else
10375		  cp_error ("declaration of `%D' as array of references",
10376			    dname);
10377		type = error_mark_node;
10378	      }
10379
10380	    if (TREE_CODE (type) == OFFSET_TYPE)
10381	      {
10382		  cp_error ("declaration of `%D' as array of data members",
10383			    dname);
10384		type = error_mark_node;
10385	      }
10386
10387	    if (TREE_CODE (type) == METHOD_TYPE)
10388	      {
10389		cp_error ("declaration of `%D' as array of function members",
10390			  dname);
10391		type = error_mark_node;
10392	      }
10393
10394	    if (size == error_mark_node)
10395	      type = error_mark_node;
10396	    else if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10397	      {
10398		/* [dcl.array]
10399
10400		   the constant expressions that specify the bounds of
10401		   the arrays can be omitted only for the first member
10402		   of the sequence.  */
10403		cp_error ("declaration of `%D' as multidimensional array",
10404			  dname);
10405		cp_error ("must have bounds for all dimensions except the first");
10406		type = error_mark_node;
10407	      }
10408
10409	    if (type == error_mark_node)
10410	      continue;
10411
10412	    /* VC++ spells a zero-sized array with [].  */
10413	    if (size == NULL_TREE && decl_context == FIELD && !	staticp
10414		&& ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10415	      size = integer_zero_node;
10416
10417	    if (size)
10418	      {
10419		/* Must suspend_momentary here because the index
10420		   type may need to live until the end of the function.
10421		   For example, it is used in the declaration of a
10422		   variable which requires destructing at the end of
10423		   the function; then build_vec_delete will need this
10424		   value.  */
10425		int yes = suspend_momentary ();
10426		/* Might be a cast. */
10427		if (TREE_CODE (size) == NOP_EXPR
10428		    && TREE_TYPE (size) == TREE_TYPE (TREE_OPERAND (size, 0)))
10429		  size = TREE_OPERAND (size, 0);
10430
10431		/* If this involves a template parameter, it will be a
10432		   constant at instantiation time, but we don't know
10433		   what the value is yet.  Even if no template
10434		   parameters are involved, we may an expression that
10435		   is not a constant; we don't even simplify `1 + 2'
10436		   when processing a template.  */
10437		if (processing_template_decl)
10438		  {
10439		    /* Resolve a qualified reference to an enumerator or
10440		       static const data member of ours.  */
10441		    if (TREE_CODE (size) == SCOPE_REF
10442			&& TREE_OPERAND (size, 0) == current_class_type)
10443		      {
10444			tree t = lookup_field (current_class_type,
10445					       TREE_OPERAND (size, 1), 0, 0);
10446			if (t)
10447			  size = t;
10448		      }
10449
10450		    itype = build_index_type (build_min
10451		      (MINUS_EXPR, sizetype, size, integer_one_node));
10452		    goto dont_grok_size;
10453		  }
10454
10455		if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
10456		    && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
10457		    && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
10458		  {
10459		    cp_error ("size of array `%D' has non-integer type",
10460			      dname);
10461		    size = integer_one_node;
10462		  }
10463		if (TREE_READONLY_DECL_P (size))
10464		  size = decl_constant_value (size);
10465		if (pedantic && integer_zerop (size))
10466		  cp_pedwarn ("ANSI C++ forbids zero-size array `%D'", dname);
10467		if (TREE_CONSTANT (size))
10468		  {
10469		    int old_flag_pedantic_errors = flag_pedantic_errors;
10470		    int old_pedantic = pedantic;
10471		    pedantic = flag_pedantic_errors = 1;
10472		    /* Always give overflow errors on array subscripts.  */
10473		    constant_expression_warning (size);
10474		    pedantic = old_pedantic;
10475		    flag_pedantic_errors = old_flag_pedantic_errors;
10476		    if (INT_CST_LT (size, integer_zero_node))
10477		      {
10478			cp_error ("size of array `%D' is negative", dname);
10479			size = integer_one_node;
10480		      }
10481		  }
10482		else
10483		  {
10484		    if (pedantic)
10485		      {
10486			if (dname)
10487			  cp_pedwarn ("ANSI C++ forbids variable-size array `%D'",
10488				      dname);
10489			else
10490			  cp_pedwarn ("ANSI C++ forbids variable-size array");
10491		      }
10492		  }
10493
10494		itype
10495		  = fold (build_binary_op (MINUS_EXPR,
10496					   cp_convert (index_type, size),
10497					   cp_convert (index_type,
10498						       integer_one_node)));
10499		if (! TREE_CONSTANT (itype))
10500		  itype = variable_size (itype);
10501		else if (TREE_OVERFLOW (itype))
10502		  {
10503		    error ("overflow in array dimension");
10504		    TREE_OVERFLOW (itype) = 0;
10505		  }
10506
10507		/* If we're a parm, we need to have a permanent type so
10508                   mangling checks for re-use will work right.  If both the
10509                   element and index types are permanent, the array type
10510                   will be, too.  */
10511		if (decl_context == PARM
10512		    && allocation_temporary_p () && TREE_PERMANENT (type))
10513		  {
10514		    push_obstacks (&permanent_obstack, &permanent_obstack);
10515		    itype = build_index_type (itype);
10516		    pop_obstacks ();
10517		  }
10518		else
10519		  itype = build_index_type (itype);
10520
10521	      dont_grok_size:
10522		resume_momentary (yes);
10523	      }
10524
10525	    type = build_cplus_array_type (type, itype);
10526	    ctype = NULL_TREE;
10527	  }
10528	  break;
10529
10530	case CALL_EXPR:
10531	  {
10532	    tree arg_types;
10533	    int funcdecl_p;
10534	    tree inner_parms = TREE_OPERAND (declarator, 1);
10535	    tree inner_decl = TREE_OPERAND (declarator, 0);
10536
10537	    /* Declaring a function type.
10538	       Make sure we have a valid type for the function to return.  */
10539
10540	    /* We now know that the TYPE_QUALS don't apply to the
10541               decl, but to its return type.  */
10542	    type_quals = TYPE_UNQUALIFIED;
10543
10544	    /* Warn about some types functions can't return.  */
10545
10546	    if (TREE_CODE (type) == FUNCTION_TYPE)
10547	      {
10548		error ("`%s' declared as function returning a function", name);
10549		type = integer_type_node;
10550	      }
10551	    if (TREE_CODE (type) == ARRAY_TYPE)
10552	      {
10553		error ("`%s' declared as function returning an array", name);
10554		type = integer_type_node;
10555	      }
10556
10557	    if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10558	      inner_decl = TREE_OPERAND (inner_decl, 1);
10559
10560	    if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10561	      inner_decl = dname;
10562
10563	    /* Pick up type qualifiers which should be applied to `this'.  */
10564	    quals = TREE_OPERAND (declarator, 2);
10565
10566	    /* Pick up the exception specifications.  */
10567	    raises = TREE_TYPE (declarator);
10568
10569	    /* Say it's a definition only for the CALL_EXPR
10570	       closest to the identifier.  */
10571	    funcdecl_p
10572	      = inner_decl
10573	      && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10574		  || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10575		  || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10576
10577	    if (ctype == NULL_TREE
10578		&& decl_context == FIELD
10579		&& funcdecl_p
10580		&& (friendp == 0 || dname == current_class_name))
10581	      ctype = current_class_type;
10582
10583	    if (ctype && return_type == return_conversion)
10584	      TYPE_HAS_CONVERSION (ctype) = 1;
10585	    if (ctype && constructor_name (ctype) == dname)
10586	      {
10587		/* We are within a class's scope. If our declarator name
10588		   is the same as the class name, and we are defining
10589		   a function, then it is a constructor/destructor, and
10590		   therefore returns a void type.  */
10591
10592		if (flags == DTOR_FLAG)
10593		  {
10594		    /* ANSI C++ June 5 1992 WP 12.4.1.  A destructor may
10595		       not be declared const or volatile.  A destructor
10596		       may not be static.  */
10597		    if (staticp == 2)
10598		      error ("destructor cannot be static member function");
10599		    if (quals)
10600		      {
10601			cp_error ("destructors may not be `%s'",
10602				  IDENTIFIER_POINTER (TREE_VALUE (quals)));
10603			quals = NULL_TREE;
10604		      }
10605		    if (decl_context == FIELD)
10606		      {
10607			if (! member_function_or_else (ctype, current_class_type,
10608						       "destructor for alien class `%s' cannot be a member"))
10609			  return void_type_node;
10610		      }
10611		  }
10612		else            /* It's a constructor.  */
10613		  {
10614		    if (explicitp == 1)
10615		      explicitp = 2;
10616		    /* ANSI C++ June 5 1992 WP 12.1.2.  A constructor may
10617		       not be declared const or volatile.  A constructor may
10618		       not be virtual.  A constructor may not be static.  */
10619		    if (staticp == 2)
10620		      error ("constructor cannot be static member function");
10621		    if (virtualp)
10622		      {
10623			pedwarn ("constructors cannot be declared virtual");
10624			virtualp = 0;
10625		      }
10626		    if (quals)
10627		      {
10628			cp_error ("constructors may not be `%s'",
10629				  IDENTIFIER_POINTER (TREE_VALUE (quals)));
10630			quals = NULL_TREE;
10631 		      }
10632		    {
10633		      RID_BIT_TYPE tmp_bits;
10634		      bcopy ((void*)&specbits, (void*)&tmp_bits, sizeof (RID_BIT_TYPE));
10635		      RIDBIT_RESET (RID_INLINE, tmp_bits);
10636		      RIDBIT_RESET (RID_STATIC, tmp_bits);
10637		      if (RIDBIT_ANY_SET (tmp_bits))
10638			error ("return value type specifier for constructor ignored");
10639		    }
10640		    type = build_pointer_type (ctype);
10641		    if (decl_context == FIELD
10642			&& IS_SIGNATURE (current_class_type))
10643		      {
10644			error ("constructor not allowed in signature");
10645			return void_type_node;
10646		      }
10647		    else if (decl_context == FIELD)
10648		      {
10649			if (! member_function_or_else (ctype, current_class_type,
10650						       "constructor for alien class `%s' cannot be member"))
10651			  return void_type_node;
10652			TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10653			if (return_type != return_ctor)
10654			  return NULL_TREE;
10655		      }
10656		  }
10657		if (decl_context == FIELD)
10658		  staticp = 0;
10659	      }
10660	    else if (friendp)
10661	      {
10662		if (initialized)
10663		  error ("can't initialize friend function `%s'", name);
10664		if (virtualp)
10665		  {
10666		    /* Cannot be both friend and virtual.  */
10667		    error ("virtual functions cannot be friends");
10668		    RIDBIT_RESET (RID_FRIEND, specbits);
10669		    friendp = 0;
10670		  }
10671		if (decl_context == NORMAL)
10672		  error ("friend declaration not in class definition");
10673		if (current_function_decl && funcdef_flag)
10674		  cp_error ("can't define friend function `%s' in a local class definition",
10675			    name);
10676	      }
10677
10678	    /* Construct the function type and go to the next
10679	       inner layer of declarator.  */
10680
10681	    declarator = TREE_OPERAND (declarator, 0);
10682
10683	    /* FIXME: This is where default args should be fully
10684	       processed.  */
10685
10686	    arg_types = grokparms (inner_parms, funcdecl_p ? funcdef_flag : 0);
10687
10688	    if (declarator && flags == DTOR_FLAG)
10689	      {
10690		/* A destructor declared in the body of a class will
10691		   be represented as a BIT_NOT_EXPR.  But, we just
10692		   want the underlying IDENTIFIER.  */
10693		if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10694		  declarator = TREE_OPERAND (declarator, 0);
10695
10696		if (strict_prototype == 0 && arg_types == NULL_TREE)
10697		  arg_types = void_list_node;
10698		else if (arg_types == NULL_TREE
10699			 || arg_types != void_list_node)
10700		  {
10701		    cp_error ("destructors may not have parameters");
10702		    arg_types = void_list_node;
10703		    last_function_parms = NULL_TREE;
10704		  }
10705	      }
10706
10707	    /* ANSI says that `const int foo ();'
10708	       does not make the function foo const.  */
10709	    type = build_function_type (type, arg_types);
10710
10711	    {
10712	      tree t;
10713	      for (t = arg_types; t; t = TREE_CHAIN (t))
10714		if (TREE_PURPOSE (t)
10715		    && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
10716		  {
10717		    add_defarg_fn (type);
10718		    break;
10719		  }
10720	    }
10721	  }
10722	  break;
10723
10724	case ADDR_EXPR:
10725	case INDIRECT_REF:
10726	  /* Filter out pointers-to-references and references-to-references.
10727	     We can get these if a TYPE_DECL is used.  */
10728
10729	  if (TREE_CODE (type) == REFERENCE_TYPE)
10730	    {
10731	      error ("cannot declare %s to references",
10732		     TREE_CODE (declarator) == ADDR_EXPR
10733		     ? "references" : "pointers");
10734	      declarator = TREE_OPERAND (declarator, 0);
10735	      continue;
10736	    }
10737
10738	  if (TREE_CODE (type) == OFFSET_TYPE
10739	      && (TREE_CODE (TREE_TYPE (type)) == VOID_TYPE
10740		  || TREE_CODE (TREE_TYPE (type)) == REFERENCE_TYPE))
10741	    {
10742	      cp_error ("cannot declare pointer to `%#T' member",
10743			TREE_TYPE (type));
10744	      type = TREE_TYPE (type);
10745	    }
10746
10747	  /* Merge any constancy or volatility into the target type
10748	     for the pointer.  */
10749
10750	  /* We now know that the TYPE_QUALS don't apply to the decl,
10751	     but to the target of the pointer.  */
10752	  type_quals = TYPE_UNQUALIFIED;
10753
10754	  if (IS_SIGNATURE (type))
10755	    {
10756	      if (TREE_CODE (declarator) == ADDR_EXPR)
10757		{
10758		  if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10759		      && TYPE_SIZE (type))
10760		    cp_warning ("empty signature `%T' used in signature reference declaration",
10761				type);
10762#if 0
10763		  type = build_signature_reference_type (type);
10764#else
10765		  sorry ("signature reference");
10766		  return NULL_TREE;
10767#endif
10768		}
10769	      else
10770		{
10771		  if (CLASSTYPE_METHOD_VEC (type) == NULL_TREE
10772		      && TYPE_SIZE (type))
10773		    cp_warning ("empty signature `%T' used in signature pointer declaration",
10774				type);
10775		  type = build_signature_pointer_type (type);
10776		}
10777	    }
10778	  else if (TREE_CODE (declarator) == ADDR_EXPR)
10779	    {
10780	      if (TREE_CODE (type) == VOID_TYPE)
10781		error ("invalid type: `void &'");
10782	      else
10783		type = build_reference_type (type);
10784	    }
10785	  else if (TREE_CODE (type) == METHOD_TYPE)
10786	    type = build_ptrmemfunc_type (build_pointer_type (type));
10787	  else
10788	    type = build_pointer_type (type);
10789
10790	  /* Process a list of type modifier keywords (such as
10791	     const or volatile) that were given inside the `*' or `&'.  */
10792
10793	  if (TREE_TYPE (declarator))
10794	    {
10795	      register tree typemodlist;
10796	      int erred = 0;
10797
10798	      constp = 0;
10799	      volatilep = 0;
10800	      restrictp = 0;
10801	      for (typemodlist = TREE_TYPE (declarator); typemodlist;
10802		   typemodlist = TREE_CHAIN (typemodlist))
10803		{
10804		  tree qualifier = TREE_VALUE (typemodlist);
10805
10806		  if (qualifier == ridpointers[(int) RID_CONST])
10807		    constp++;
10808		  else if (qualifier == ridpointers[(int) RID_VOLATILE])
10809		    volatilep++;
10810		  else if (qualifier == ridpointers[(int) RID_RESTRICT])
10811		    restrictp++;
10812		  else if (!erred)
10813		    {
10814		      erred = 1;
10815		      error ("invalid type modifier within pointer declarator");
10816		    }
10817		}
10818	      if (constp > 1)
10819		pedwarn ("duplicate `const'");
10820	      if (volatilep > 1)
10821		pedwarn ("duplicate `volatile'");
10822	      if (restrictp > 1)
10823		pedwarn ("duplicate `restrict'");
10824
10825	      type_quals = ((constp ? TYPE_QUAL_CONST : 0)
10826			    | (restrictp ? TYPE_QUAL_RESTRICT : 0)
10827			    | (volatilep ? TYPE_QUAL_VOLATILE : 0));
10828	      if (TREE_CODE (declarator) == ADDR_EXPR
10829		  && (constp || volatilep))
10830		{
10831		  if (constp)
10832		    pedwarn ("discarding `const' applied to a reference");
10833		  if (volatilep)
10834		    pedwarn ("discarding `volatile' applied to a reference");
10835		  type_quals &= ~(TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE);
10836		}
10837	      type = cp_build_qualified_type (type, type_quals);
10838	    }
10839	  declarator = TREE_OPERAND (declarator, 0);
10840	  ctype = NULL_TREE;
10841	  break;
10842
10843	case SCOPE_REF:
10844	  {
10845	    /* We have converted type names to NULL_TREE if the
10846	       name was bogus, or to a _TYPE node, if not.
10847
10848	       The variable CTYPE holds the type we will ultimately
10849	       resolve to.  The code here just needs to build
10850	       up appropriate member types.  */
10851	    tree sname = TREE_OPERAND (declarator, 1);
10852	    tree t;
10853
10854	    /* Destructors can have their visibilities changed as well.  */
10855	    if (TREE_CODE (sname) == BIT_NOT_EXPR)
10856	      sname = TREE_OPERAND (sname, 0);
10857
10858	    if (TREE_COMPLEXITY (declarator) == 0)
10859	      /* This needs to be here, in case we are called
10860		 multiple times.  */ ;
10861	    else if (TREE_COMPLEXITY (declarator) == -1)
10862	      /* Namespace member. */
10863	      pop_decl_namespace ();
10864	    else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
10865	      /* Don't fall out into global scope. Hides real bug? --eichin */ ;
10866	    else if (! IS_AGGR_TYPE_CODE
10867		     (TREE_CODE (TREE_OPERAND (declarator, 0))))
10868	      ;
10869	    else if (TREE_COMPLEXITY (declarator) == current_class_depth)
10870	      {
10871		/* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
10872		   that refer to ctype.  They couldn't be resolved earlier
10873		   because we hadn't pushed into the class yet.
10874		   Example: resolve 'B<T>::type' in
10875		   'B<typename B<T>::type> B<T>::f () { }'.  */
10876		if (current_template_parms
10877		    && uses_template_parms (type)
10878		    && uses_template_parms (current_class_type))
10879		  {
10880		    tree args = current_template_args ();
10881		    type = tsubst (type, args, /*complain=*/1, NULL_TREE);
10882		  }
10883
10884		/* This pop_nested_class corresponds to the
10885                   push_nested_class used to push into class scope for
10886                   parsing the argument list of a function decl, in
10887                   qualified_id.  */
10888		pop_nested_class ();
10889		TREE_COMPLEXITY (declarator) = current_class_depth;
10890	      }
10891	    else
10892	      my_friendly_abort (16);
10893
10894	    if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10895	      {
10896		/* We had a reference to a global decl, or
10897		   perhaps we were given a non-aggregate typedef,
10898		   in which case we cleared this out, and should just
10899		   keep going as though it wasn't there.  */
10900		declarator = sname;
10901		continue;
10902	      }
10903	    ctype = TREE_OPERAND (declarator, 0);
10904
10905	    t = ctype;
10906	    while (t != NULL_TREE && CLASS_TYPE_P (t))
10907	      {
10908		if (CLASSTYPE_TEMPLATE_INFO (t) &&
10909		    !CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
10910		  template_count += 1;
10911		t = TYPE_MAIN_DECL (t);
10912		if (DECL_LANG_SPECIFIC (t))
10913		  t = DECL_CLASS_CONTEXT (t);
10914		else
10915		  t = NULL_TREE;
10916	      }
10917
10918	    if (sname == NULL_TREE)
10919	      goto done_scoping;
10920
10921	    if (TREE_CODE (sname) == IDENTIFIER_NODE)
10922	      {
10923		/* This is the `standard' use of the scoping operator:
10924		   basetype :: member .  */
10925
10926		if (ctype == current_class_type)
10927		  {
10928		    /* class A {
10929		         void A::f ();
10930		       };
10931
10932		       Is this ill-formed?  */
10933
10934		    if (pedantic)
10935		      cp_pedwarn ("extra qualification `%T::' on member `%s' ignored",
10936				  ctype, name);
10937		  }
10938		else if (TREE_CODE (type) == FUNCTION_TYPE)
10939		  {
10940		    if (current_class_type == NULL_TREE
10941			|| friendp)
10942		      type = build_cplus_method_type (ctype, TREE_TYPE (type),
10943						      TYPE_ARG_TYPES (type));
10944		    else
10945		      {
10946			cp_error ("cannot declare member function `%T::%s' within `%T'",
10947				  ctype, name, current_class_type);
10948			return void_type_node;
10949		      }
10950		  }
10951		else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10952			 || TYPE_SIZE (complete_type (ctype)) != NULL_TREE)
10953		  {
10954		    /* Have to move this code elsewhere in this function.
10955		       this code is used for i.e., typedef int A::M; M *pm;
10956
10957		       It is?  How? jason 10/2/94 */
10958
10959		    if (current_class_type)
10960		      {
10961			cp_error ("cannot declare member `%T::%s' within `%T'",
10962				  ctype, name, current_class_type);
10963			return void_type_node;
10964		      }
10965		    type = build_offset_type (ctype, type);
10966		  }
10967		else if (uses_template_parms (ctype))
10968		  {
10969                    if (TREE_CODE (type) == FUNCTION_TYPE)
10970		      type
10971			= build_cplus_method_type (ctype, TREE_TYPE (type),
10972						   TYPE_ARG_TYPES (type));
10973  		  }
10974		else
10975		  {
10976		    cp_error ("structure `%T' not yet defined", ctype);
10977		    return error_mark_node;
10978		  }
10979
10980		declarator = sname;
10981	      }
10982	    else if (TREE_CODE (sname) == SCOPE_REF)
10983	      my_friendly_abort (17);
10984	    else
10985	      {
10986	      done_scoping:
10987		declarator = TREE_OPERAND (declarator, 1);
10988		if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10989		  /* In this case, we will deal with it later.  */
10990		  ;
10991		else
10992		  {
10993		    if (TREE_CODE (type) == FUNCTION_TYPE)
10994		      type = build_cplus_method_type (ctype, TREE_TYPE (type),
10995						      TYPE_ARG_TYPES (type));
10996		    else
10997		      type = build_offset_type (ctype, type);
10998		  }
10999	      }
11000	  }
11001	  break;
11002
11003	case BIT_NOT_EXPR:
11004	  declarator = TREE_OPERAND (declarator, 0);
11005	  break;
11006
11007	case RECORD_TYPE:
11008	case UNION_TYPE:
11009	case ENUMERAL_TYPE:
11010	  declarator = NULL_TREE;
11011	  break;
11012
11013	case ERROR_MARK:
11014	  declarator = NULL_TREE;
11015	  break;
11016
11017	default:
11018	  my_friendly_abort (158);
11019	}
11020    }
11021
11022  /* See the comment for the TREE_LIST case, above.  */
11023  if (inner_attrs)
11024    {
11025      if (! ignore_attrs)
11026	decl_attributes (type, inner_attrs, NULL_TREE);
11027      else if (attrlist)
11028	TREE_VALUE (attrlist) = chainon (inner_attrs, TREE_VALUE (attrlist));
11029      else
11030	attrlist = build_decl_list (NULL_TREE, inner_attrs);
11031    }
11032
11033  /* Now TYPE has the actual type.  */
11034
11035  if (explicitp == 1)
11036    {
11037      error ("only constructors can be declared `explicit'");
11038      explicitp = 0;
11039    }
11040
11041  if (RIDBIT_SETP (RID_MUTABLE, specbits))
11042    {
11043      if (type_quals & TYPE_QUAL_CONST)
11044	{
11045	  error ("const `%s' cannot be declared `mutable'", name);
11046	  RIDBIT_RESET (RID_MUTABLE, specbits);
11047	}
11048      else if (staticp)
11049	{
11050	  error ("static `%s' cannot be declared `mutable'", name);
11051	  RIDBIT_RESET (RID_MUTABLE, specbits);
11052	}
11053    }
11054
11055  if (declarator == NULL_TREE
11056      || TREE_CODE (declarator) == IDENTIFIER_NODE
11057      || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11058	  && (TREE_CODE (type) == FUNCTION_TYPE
11059	      || TREE_CODE (type) == METHOD_TYPE)))
11060    /* OK */;
11061  else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11062    {
11063      cp_error ("template-id `%D' used as a declarator", declarator);
11064      declarator = dname;
11065    }
11066  else
11067    /* Unexpected declarator format.  */
11068    my_friendly_abort (990210);
11069
11070  /* If this is declaring a typedef name, return a TYPE_DECL.  */
11071
11072  if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11073    {
11074      tree decl;
11075
11076      /* Note that the grammar rejects storage classes
11077	 in typenames, fields or parameters.  */
11078      if (current_lang_name == lang_name_java)
11079	TYPE_FOR_JAVA (type) = 1;
11080
11081      if (decl_context == FIELD)
11082	{
11083	  if (declarator == constructor_name (current_class_type))
11084	    cp_pedwarn ("ANSI C++ forbids nested type `%D' with same name as enclosing class",
11085			declarator);
11086	  decl = build_lang_decl (TYPE_DECL, declarator, type);
11087	  if (IS_SIGNATURE (current_class_type) && opaque_typedef)
11088	    SIGNATURE_HAS_OPAQUE_TYPEDECLS (current_class_type) = 1;
11089	}
11090      else
11091	{
11092	  /* Make sure this typedef lives as long as its type,
11093	     since it might be used as a template parameter. */
11094	  if (type != error_mark_node)
11095	    push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
11096	  decl = build_decl (TYPE_DECL, declarator, type);
11097	  if (type != error_mark_node)
11098	    pop_obstacks ();
11099	}
11100
11101      /* If the user declares "struct {...} foo" then `foo' will have
11102	 an anonymous name.  Fill that name in now.  Nothing can
11103	 refer to it, so nothing needs know about the name change.
11104	 The TYPE_NAME field was filled in by build_struct_xref.  */
11105      if (type != error_mark_node
11106	  && TYPE_NAME (type)
11107	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11108	  && ANON_AGGRNAME_P (TYPE_IDENTIFIER (type)))
11109	{
11110	  tree oldname = TYPE_NAME (type);
11111	  tree t;
11112
11113	  /* FIXME: This is bogus; we should not be doing this for
11114	            cv-qualified types.  */
11115
11116	  /* Replace the anonymous name with the real name everywhere.  */
11117	  lookup_tag_reverse (type, declarator);
11118	  for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11119	    if (TYPE_NAME (t) == oldname)
11120	      TYPE_NAME (t) = decl;
11121
11122	  if (TYPE_LANG_SPECIFIC (type))
11123	    TYPE_WAS_ANONYMOUS (type) = 1;
11124
11125	  /* If this is a typedef within a template class, the nested
11126	     type is a (non-primary) template.  The name for the
11127	     template needs updating as well.  */
11128	  if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11129	    DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11130	      = TYPE_IDENTIFIER (type);
11131
11132	  /* XXX Temporarily set the scope.
11133	     When returning, start_decl expects it as NULL_TREE,
11134	     and will then then set it using pushdecl. */
11135	  my_friendly_assert (DECL_CONTEXT (decl) == NULL_TREE, 980404);
11136	  if (current_class_type)
11137	    DECL_CONTEXT (decl) = current_class_type;
11138	  else
11139	    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11140
11141	  DECL_ASSEMBLER_NAME (decl) = DECL_NAME (decl);
11142	  DECL_ASSEMBLER_NAME (decl)
11143	    = get_identifier (build_overload_name (type, 1, 1));
11144	  DECL_CONTEXT (decl) = NULL_TREE;
11145
11146	  /* FIXME remangle member functions; member functions of a
11147	     type with external linkage have external linkage.  */
11148	}
11149
11150      if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
11151	{
11152	  cp_error_at ("typedef name may not be class-qualified", decl);
11153	  return NULL_TREE;
11154	}
11155      else if (quals)
11156	{
11157	  if (ctype == NULL_TREE)
11158	    {
11159	      if (TREE_CODE (type) != METHOD_TYPE)
11160		cp_error_at ("invalid type qualifier for non-method type", decl);
11161	      else
11162		ctype = TYPE_METHOD_BASETYPE (type);
11163	    }
11164	  if (ctype != NULL_TREE)
11165	    grok_method_quals (ctype, decl, quals);
11166	}
11167
11168      if (RIDBIT_SETP (RID_SIGNED, specbits)
11169	  || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11170	C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11171
11172      if (RIDBIT_SETP (RID_MUTABLE, specbits))
11173	error ("non-object member `%s' cannot be declared mutable", name);
11174
11175      bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11176		      inlinep, friendp, raises != NULL_TREE);
11177
11178      if (initialized)
11179	error ("typedef declaration includes an initializer");
11180
11181      return decl;
11182    }
11183
11184  /* Detect the case of an array type of unspecified size
11185     which came, as such, direct from a typedef name.
11186     We must copy the type, so that each identifier gets
11187     a distinct type, so that each identifier's size can be
11188     controlled separately by its own initializer.  */
11189
11190  if (type == typedef_type && TREE_CODE (type) == ARRAY_TYPE
11191      && TYPE_DOMAIN (type) == NULL_TREE)
11192    {
11193      type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
11194    }
11195
11196  /* If this is a type name (such as, in a cast or sizeof),
11197     compute the type and return it now.  */
11198
11199  if (decl_context == TYPENAME)
11200    {
11201      /* Note that the grammar rejects storage classes
11202	 in typenames, fields or parameters.  */
11203      if (type_quals != TYPE_UNQUALIFIED)
11204	{
11205	  if (IS_SIGNATURE (type))
11206	    error ("type qualifiers specified for signature type");
11207	  type_quals = TYPE_UNQUALIFIED;
11208	}
11209
11210      /* Special case: "friend class foo" looks like a TYPENAME context.  */
11211      if (friendp)
11212	{
11213	  if (type_quals != TYPE_UNQUALIFIED)
11214	    {
11215	      cp_error ("type qualifiers specified for friend class declaration");
11216	      type_quals = TYPE_UNQUALIFIED;
11217	    }
11218	  if (inlinep)
11219	    {
11220	      cp_error ("`inline' specified for friend class declaration");
11221	      inlinep = 0;
11222	    }
11223
11224	  /* Only try to do this stuff if we didn't already give up.  */
11225	  if (type != integer_type_node)
11226	    {
11227	      /* A friendly class?  */
11228	      if (current_class_type)
11229		make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11230	      else
11231		error ("trying to make class `%s' a friend of global scope",
11232		       TYPE_NAME_STRING (type));
11233	      type = void_type_node;
11234	    }
11235	}
11236      else if (quals)
11237	{
11238	  tree dummy = build_decl (TYPE_DECL, declarator, type);
11239	  if (ctype == NULL_TREE)
11240	    {
11241	      my_friendly_assert (TREE_CODE (type) == METHOD_TYPE, 159);
11242	      ctype = TYPE_METHOD_BASETYPE (type);
11243	    }
11244	  grok_method_quals (ctype, dummy, quals);
11245	  type = TREE_TYPE (dummy);
11246	}
11247
11248      return type;
11249    }
11250  else if (declarator == NULL_TREE && decl_context != PARM
11251	   && decl_context != CATCHPARM
11252	   && TREE_CODE (type) != UNION_TYPE
11253	   && ! bitfield)
11254    {
11255      cp_error ("abstract declarator `%T' used as declaration", type);
11256      declarator = make_anon_name ();
11257    }
11258
11259  /* `void' at top level (not within pointer)
11260     is allowed only in typedefs or type names.
11261     We don't complain about parms either, but that is because
11262     a better error message can be made later.  */
11263
11264  if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11265    {
11266      if (! declarator)
11267	error ("unnamed variable or field declared void");
11268      else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11269	{
11270	  if (IDENTIFIER_OPNAME_P (declarator))
11271	    my_friendly_abort (356);
11272	  else
11273	    error ("variable or field `%s' declared void", name);
11274	}
11275      else
11276	error ("variable or field declared void");
11277      type = integer_type_node;
11278    }
11279
11280  /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11281     or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
11282
11283  if (decl_context == PARM || decl_context == CATCHPARM)
11284    {
11285      if (ctype || in_namespace)
11286	error ("cannot use `::' in parameter declaration");
11287
11288      /* A parameter declared as an array of T is really a pointer to T.
11289	 One declared as a function is really a pointer to a function.
11290	 One declared as a member is really a pointer to member.  */
11291
11292      if (TREE_CODE (type) == ARRAY_TYPE)
11293	{
11294	  /* Transfer const-ness of array into that of type pointed to.  */
11295	  type = build_pointer_type (TREE_TYPE (type));
11296	  type_quals = TYPE_UNQUALIFIED;
11297	}
11298      else if (TREE_CODE (type) == FUNCTION_TYPE)
11299	type = build_pointer_type (type);
11300      else if (TREE_CODE (type) == OFFSET_TYPE)
11301	type = build_pointer_type (type);
11302      else if (TREE_CODE (type) == VOID_TYPE && declarator)
11303	{
11304	  error ("declaration of `%s' as void", name);
11305	  return NULL_TREE;
11306	}
11307    }
11308
11309  {
11310    register tree decl;
11311
11312    if (decl_context == PARM)
11313      {
11314	decl = build_decl (PARM_DECL, declarator, type);
11315
11316	bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11317			inlinep, friendp, raises != NULL_TREE);
11318	if (current_class_type
11319	    && IS_SIGNATURE (current_class_type))
11320	  {
11321	    if (inlinep)
11322	      error ("parameter of signature member function declared `inline'");
11323	    if (RIDBIT_SETP (RID_AUTO, specbits))
11324	      error ("parameter of signature member function declared `auto'");
11325	    if (RIDBIT_SETP (RID_REGISTER, specbits))
11326	      error ("parameter of signature member function declared `register'");
11327	  }
11328
11329	/* Compute the type actually passed in the parmlist,
11330	   for the case where there is no prototype.
11331	   (For example, shorts and chars are passed as ints.)
11332	   When there is a prototype, this is overridden later.  */
11333
11334	DECL_ARG_TYPE (decl) = type_promotes_to (type);
11335      }
11336    else if (decl_context == FIELD)
11337      {
11338	if (type == error_mark_node)
11339	  {
11340	    /* Happens when declaring arrays of sizes which
11341	       are error_mark_node, for example.  */
11342	    decl = NULL_TREE;
11343	  }
11344	else if (in_namespace && !friendp)
11345	  {
11346	    /* Something like struct S { int N::j; };  */
11347	    cp_error ("invalid use of `::'");
11348	    decl = NULL_TREE;
11349	  }
11350	else if (TREE_CODE (type) == FUNCTION_TYPE)
11351	  {
11352	    int publicp = 0;
11353	    tree function_context;
11354
11355	    /* We catch the others as conflicts with the builtin
11356	       typedefs.  */
11357	    if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11358	      {
11359		cp_error ("function `%D' cannot be declared friend",
11360			  declarator);
11361		friendp = 0;
11362	      }
11363
11364	    if (friendp == 0)
11365	      {
11366		if (ctype == NULL_TREE)
11367		  ctype = current_class_type;
11368
11369		if (ctype == NULL_TREE)
11370		  {
11371		    cp_error ("can't make `%D' into a method -- not in a class",
11372			      declarator);
11373		    return void_type_node;
11374		  }
11375
11376		/* ``A union may [ ... ] not [ have ] virtual functions.''
11377		   ARM 9.5 */
11378		if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11379		  {
11380		    cp_error ("function `%D' declared virtual inside a union",
11381			      declarator);
11382		    return void_type_node;
11383		  }
11384
11385		if (declarator == ansi_opname[(int) NEW_EXPR]
11386		    || declarator == ansi_opname[(int) VEC_NEW_EXPR]
11387		    || declarator == ansi_opname[(int) DELETE_EXPR]
11388		    || declarator == ansi_opname[(int) VEC_DELETE_EXPR])
11389		  {
11390		    if (virtualp)
11391		      {
11392			cp_error ("`%D' cannot be declared virtual, since it is always static",
11393				  declarator);
11394			virtualp = 0;
11395		      }
11396		  }
11397		else if (staticp < 2)
11398		  type = build_cplus_method_type (ctype, TREE_TYPE (type),
11399						  TYPE_ARG_TYPES (type));
11400	      }
11401
11402	    /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
11403	    function_context = (ctype != NULL_TREE) ?
11404	      hack_decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11405	    publicp = (! friendp || ! staticp)
11406	      && function_context == NULL_TREE;
11407	    decl = grokfndecl (ctype, type,
11408			       TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11409			       ? declarator : dname,
11410			       declarator,
11411			       virtualp, flags, quals, raises,
11412			       friendp ? -1 : 0, friendp, publicp, inlinep,
11413			       funcdef_flag, template_count, in_namespace);
11414	    if (decl == NULL_TREE)
11415	      return decl;
11416#if 0
11417	    /* This clobbers the attrs stored in `decl' from `attrlist'.  */
11418	    /* The decl and setting of decl_machine_attr is also turned off.  */
11419	    decl = build_decl_attribute_variant (decl, decl_machine_attr);
11420#endif
11421
11422	    /* [class.conv.ctor]
11423
11424	       A constructor declared without the function-specifier
11425	       explicit that can be called with a single parameter
11426	       specifies a conversion from the type of its first
11427	       parameter to the type of its class.  Such a constructor
11428	       is called a converting constructor.  */
11429	    if (explicitp == 2)
11430	      DECL_NONCONVERTING_P (decl) = 1;
11431	    else if (DECL_CONSTRUCTOR_P (decl))
11432	      {
11433		/* The constructor can be called with exactly one
11434		   parameter if there is at least one parameter, and
11435		   any subsequent parameters have default arguments.
11436		   We don't look at the first parameter, which is
11437		   really just the `this' parameter for the new
11438		   object.  */
11439		tree arg_types =
11440		  TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)));
11441
11442		/* Skip the `in_chrg' argument too, if present.  */
11443		if (TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (decl)))
11444		  arg_types = TREE_CHAIN (arg_types);
11445
11446		/* And the `vlist' argument. */
11447		if (TYPE_USES_PVBASES (DECL_CONTEXT (decl)))
11448		  arg_types = TREE_CHAIN (arg_types);
11449
11450		if (arg_types == void_list_node
11451		    || (arg_types
11452			&& TREE_CHAIN (arg_types)
11453			&& TREE_CHAIN (arg_types) != void_list_node
11454			&& !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11455		  DECL_NONCONVERTING_P (decl) = 1;
11456	      }
11457	  }
11458	else if (TREE_CODE (type) == METHOD_TYPE)
11459	  {
11460	    /* We only get here for friend declarations of
11461	       members of other classes.  */
11462	    /* All method decls are public, so tell grokfndecl to set
11463	       TREE_PUBLIC, also.  */
11464	    decl = grokfndecl (ctype, type, declarator, declarator,
11465			       virtualp, flags, quals, raises,
11466			       friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11467			       template_count, in_namespace);
11468	    if (decl == NULL_TREE)
11469	      return NULL_TREE;
11470	  }
11471	else if (!staticp && ! processing_template_decl
11472		 && TYPE_SIZE (complete_type (type)) == NULL_TREE
11473		 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11474	  {
11475	    if (declarator)
11476	      cp_error ("field `%D' has incomplete type", declarator);
11477	    else
11478	      cp_error ("name `%T' has incomplete type", type);
11479
11480	    /* If we're instantiating a template, tell them which
11481	       instantiation made the field's type be incomplete.  */
11482	    if (current_class_type
11483		&& TYPE_NAME (current_class_type)
11484		&& IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11485		&& declspecs && TREE_VALUE (declspecs)
11486		&& TREE_TYPE (TREE_VALUE (declspecs)) == type)
11487	      cp_error ("  in instantiation of template `%T'",
11488			current_class_type);
11489
11490	    type = error_mark_node;
11491	    decl = NULL_TREE;
11492	  }
11493	else
11494	  {
11495	    if (friendp)
11496	      {
11497		error ("`%s' is neither function nor method; cannot be declared friend",
11498		       IDENTIFIER_POINTER (declarator));
11499		friendp = 0;
11500	      }
11501	    decl = NULL_TREE;
11502	  }
11503
11504	if (friendp)
11505	  {
11506	    /* Friends are treated specially.  */
11507	    if (ctype == current_class_type)
11508	      warning ("member functions are implicitly friends of their class");
11509	    else
11510	      {
11511		tree t = NULL_TREE;
11512		if (decl && DECL_NAME (decl))
11513		  {
11514		    if (template_class_depth (current_class_type) == 0)
11515		      {
11516			decl
11517			  = check_explicit_specialization
11518			  (declarator, decl,
11519			   template_count, 2 * (funcdef_flag != 0) + 4);
11520			if (decl == error_mark_node)
11521			  return error_mark_node;
11522		      }
11523
11524		    t = do_friend (ctype, declarator, decl,
11525				   last_function_parms, attrlist, flags, quals,
11526				   funcdef_flag);
11527		  }
11528		if (t && funcdef_flag)
11529		  return t;
11530
11531		return void_type_node;
11532	      }
11533	  }
11534
11535	/* Structure field.  It may not be a function, except for C++ */
11536
11537	if (decl == NULL_TREE)
11538	  {
11539	    if (initialized)
11540	      {
11541		if (!staticp)
11542		  {
11543		    /* An attempt is being made to initialize a non-static
11544		       member.  But, from [class.mem]:
11545
11546		       4 A member-declarator can contain a
11547		       constant-initializer only if it declares a static
11548		       member (_class.static_) of integral or enumeration
11549		       type, see _class.static.data_.
11550
11551		       This used to be relatively common practice, but
11552		       the rest of the compiler does not correctly
11553		       handle the initialization unless the member is
11554		       static so we make it static below.  */
11555		    cp_pedwarn ("ANSI C++ forbids initialization of member `%D'",
11556				declarator);
11557		    cp_pedwarn ("making `%D' static", declarator);
11558		    staticp = 1;
11559		  }
11560
11561		if (uses_template_parms (type))
11562		  /* We'll check at instantiation time.  */
11563		  ;
11564		else if (check_static_variable_definition (declarator,
11565							   type))
11566		  /* If we just return the declaration, crashes
11567		     will sometimes occur.  We therefore return
11568		     void_type_node, as if this was a friend
11569		     declaration, to cause callers to completely
11570		     ignore this declaration.  */
11571		  return void_type_node;
11572	      }
11573
11574	    /* 9.2p13 [class.mem] */
11575	    if (declarator == constructor_name (current_class_type)
11576		/* Divergence from the standard:  In extern "C", we
11577		   allow non-static data members here, because C does
11578		   and /usr/include/netinet/in.h uses that.  */
11579		&& (staticp || ! in_system_header))
11580	      cp_pedwarn ("ANSI C++ forbids data member `%D' with same name as enclosing class",
11581			  declarator);
11582
11583	    if (staticp)
11584	      {
11585		/* C++ allows static class members.
11586		   All other work for this is done by grokfield.
11587		   This VAR_DCL is built by build_lang_field_decl.
11588		   All other VAR_DECLs are built by build_decl.  */
11589		decl = build_lang_field_decl (VAR_DECL, declarator, type);
11590		TREE_STATIC (decl) = 1;
11591		/* In class context, 'static' means public access.  */
11592		TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11593	      }
11594	    else
11595	      {
11596		decl = build_lang_field_decl (FIELD_DECL, declarator, type);
11597		if (RIDBIT_SETP (RID_MUTABLE, specbits))
11598		  {
11599		    DECL_MUTABLE_P (decl) = 1;
11600		    RIDBIT_RESET (RID_MUTABLE, specbits);
11601		  }
11602	      }
11603
11604	    bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11605			    inlinep, friendp, raises != NULL_TREE);
11606	  }
11607      }
11608    else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11609      {
11610	tree original_name;
11611	int publicp = 0;
11612
11613	if (! declarator)
11614	  return NULL_TREE;
11615
11616	if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11617	  original_name = dname;
11618	else
11619	  original_name = declarator;
11620
11621	if (RIDBIT_SETP (RID_AUTO, specbits))
11622	  error ("storage class `auto' invalid for function `%s'", name);
11623	else if (RIDBIT_SETP (RID_REGISTER, specbits))
11624	  error ("storage class `register' invalid for function `%s'", name);
11625
11626	/* Function declaration not at top level.
11627	   Storage classes other than `extern' are not allowed
11628	   and `extern' makes no difference.  */
11629	if (! toplevel_bindings_p ()
11630	    && (RIDBIT_SETP (RID_STATIC, specbits)
11631		|| RIDBIT_SETP (RID_INLINE, specbits))
11632	    && pedantic)
11633	  {
11634	    if (RIDBIT_SETP (RID_STATIC, specbits))
11635	      pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11636	    else
11637	      pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11638	  }
11639
11640	if (ctype == NULL_TREE)
11641	  {
11642	    if (virtualp)
11643	      {
11644		error ("virtual non-class function `%s'", name);
11645		virtualp = 0;
11646	      }
11647	  }
11648	else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11649	  type = build_cplus_method_type (ctype, TREE_TYPE (type),
11650					  TYPE_ARG_TYPES (type));
11651
11652	/* Record presence of `static'.  */
11653	publicp = (ctype != NULL_TREE
11654		   || RIDBIT_SETP (RID_EXTERN, specbits)
11655		   || !RIDBIT_SETP (RID_STATIC, specbits));
11656
11657	decl = grokfndecl (ctype, type, original_name, declarator,
11658			   virtualp, flags, quals, raises,
11659			   1, friendp,
11660			   publicp, inlinep, funcdef_flag,
11661			   template_count, in_namespace);
11662	if (decl == NULL_TREE)
11663	  return NULL_TREE;
11664
11665	if (staticp == 1)
11666	  {
11667	    int illegal_static = 0;
11668
11669	    /* Don't allow a static member function in a class, and forbid
11670	       declaring main to be static.  */
11671	    if (TREE_CODE (type) == METHOD_TYPE)
11672	      {
11673		cp_pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11674		illegal_static = 1;
11675	      }
11676	    else if (current_function_decl)
11677	      {
11678		/* FIXME need arm citation */
11679		error ("cannot declare static function inside another function");
11680		illegal_static = 1;
11681	      }
11682
11683	    if (illegal_static)
11684	      {
11685		staticp = 0;
11686		RIDBIT_RESET (RID_STATIC, specbits);
11687	      }
11688	  }
11689      }
11690    else
11691      {
11692	/* It's a variable.  */
11693
11694	/* An uninitialized decl with `extern' is a reference.  */
11695	decl = grokvardecl (type, declarator, &specbits,
11696			    initialized,
11697			    (type_quals & TYPE_QUAL_CONST) != 0,
11698			    in_namespace);
11699	bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11700			inlinep, friendp, raises != NULL_TREE);
11701
11702	if (ctype)
11703	  {
11704	    DECL_CONTEXT (decl) = ctype;
11705	    if (staticp == 1)
11706	      {
11707	        cp_pedwarn ("static member `%D' re-declared as static", decl);
11708	        staticp = 0;
11709		RIDBIT_RESET (RID_STATIC, specbits);
11710	      }
11711	    if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11712	      {
11713		cp_error ("static member `%D' declared `register'", decl);
11714		RIDBIT_RESET (RID_REGISTER, specbits);
11715	      }
11716	    if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11717	      {
11718	        cp_pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11719			    decl);
11720		RIDBIT_RESET (RID_EXTERN, specbits);
11721	      }
11722	  }
11723      }
11724
11725    if (RIDBIT_SETP (RID_MUTABLE, specbits))
11726      {
11727	error ("`%s' cannot be declared mutable", name);
11728      }
11729
11730    /* Record `register' declaration for warnings on &
11731       and in case doing stupid register allocation.  */
11732
11733    if (RIDBIT_SETP (RID_REGISTER, specbits))
11734      DECL_REGISTER (decl) = 1;
11735
11736    if (RIDBIT_SETP (RID_EXTERN, specbits))
11737      DECL_THIS_EXTERN (decl) = 1;
11738
11739    if (RIDBIT_SETP (RID_STATIC, specbits))
11740      DECL_THIS_STATIC (decl) = 1;
11741
11742    /* Record constancy and volatility.  */
11743    /* FIXME: Disallow `restrict' pointer-to-member declarations.  */
11744    c_apply_type_quals_to_decl (type_quals, decl);
11745
11746    return decl;
11747  }
11748}
11749
11750/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
11751   An empty exprlist is a parmlist.  An exprlist which
11752   contains only identifiers at the global level
11753   is a parmlist.  Otherwise, it is an exprlist.  */
11754
11755int
11756parmlist_is_exprlist (exprs)
11757     tree exprs;
11758{
11759  if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
11760    return 0;
11761
11762  if (toplevel_bindings_p ())
11763    {
11764      /* At the global level, if these are all identifiers,
11765	 then it is a parmlist.  */
11766      while (exprs)
11767	{
11768	  if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
11769	    return 1;
11770	  exprs = TREE_CHAIN (exprs);
11771	}
11772      return 0;
11773    }
11774  return 1;
11775}
11776
11777/* Subroutine of start_function.  Ensure that each of the parameter
11778   types (as listed in PARMS) is complete, as is required for a
11779   function definition.  */
11780
11781static void
11782require_complete_types_for_parms (parms)
11783     tree parms;
11784{
11785  while (parms)
11786    {
11787      tree type = TREE_TYPE (parms);
11788      if (TYPE_SIZE (complete_type (type)) == NULL_TREE)
11789	{
11790	  if (DECL_NAME (parms))
11791	    error ("parameter `%s' has incomplete type",
11792		   IDENTIFIER_POINTER (DECL_NAME (parms)));
11793	  else
11794	    error ("parameter has incomplete type");
11795	  TREE_TYPE (parms) = error_mark_node;
11796	}
11797      else
11798	layout_decl (parms, 0);
11799
11800      parms = TREE_CHAIN (parms);
11801    }
11802}
11803
11804/* Returns DECL if DECL is a local variable (or parameter).  Returns
11805   NULL_TREE otherwise.  */
11806
11807static tree
11808local_variable_p (t)
11809     tree t;
11810{
11811  if ((TREE_CODE (t) == VAR_DECL
11812       /* A VAR_DECL with a context that is a _TYPE is a static data
11813	  member.  */
11814       && !TYPE_P (CP_DECL_CONTEXT (t))
11815       /* Any other non-local variable must be at namespace scope.  */
11816       && TREE_CODE (CP_DECL_CONTEXT (t)) != NAMESPACE_DECL)
11817      || (TREE_CODE (t) == PARM_DECL))
11818    return t;
11819
11820  return NULL_TREE;
11821}
11822
11823/* Check that ARG, which is a default-argument expression for a
11824   parameter DECL, is legal.  Returns ARG, or ERROR_MARK_NODE, if
11825   something goes wrong.  DECL may also be a _TYPE node, rather than a
11826   DECL, if there is no DECL available.  */
11827
11828tree
11829check_default_argument (decl, arg)
11830     tree decl;
11831     tree arg;
11832{
11833  tree var;
11834  tree decl_type;
11835
11836  if (TREE_CODE (arg) == DEFAULT_ARG)
11837    /* We get a DEFAULT_ARG when looking at an in-class declaration
11838       with a default argument.  Ignore the argument for now; we'll
11839       deal with it after the class is complete.  */
11840    return arg;
11841
11842  if (processing_template_decl || uses_template_parms (arg))
11843    /* We don't do anything checking until instantiation-time.  Note
11844       that there may be uninstantiated arguments even for an
11845       instantiated function, since default arguments are not
11846       instantiated until they are needed.  */
11847    return arg;
11848
11849  if (TYPE_P (decl))
11850    {
11851      decl_type = decl;
11852      decl = NULL_TREE;
11853    }
11854  else
11855    decl_type = TREE_TYPE (decl);
11856
11857  if (arg == error_mark_node
11858      || decl == error_mark_node
11859      || TREE_TYPE (arg) == error_mark_node
11860      || decl_type == error_mark_node)
11861    /* Something already went wrong.  There's no need to check
11862       further.  */
11863    return error_mark_node;
11864
11865  /* [dcl.fct.default]
11866
11867     A default argument expression is implicitly converted to the
11868     parameter type.  */
11869  if (!TREE_TYPE (arg)
11870      || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11871    {
11872      if (decl)
11873	cp_error ("default argument for `%#D' has type `%T'",
11874		  decl, TREE_TYPE (arg));
11875      else
11876	cp_error ("default argument for paramter of type `%T' has type `%T'",
11877		  decl_type, TREE_TYPE (arg));
11878
11879      return error_mark_node;
11880    }
11881
11882  /* [dcl.fct.default]
11883
11884     Local variables shall not be used in default argument
11885     expressions.
11886
11887     The keyword `this' shall not be used in a default argument of a
11888     member function.  */
11889  var = search_tree (arg, local_variable_p);
11890  if (var)
11891    {
11892      cp_error ("default argument `%E' uses local variable `%D'",
11893		arg, var);
11894      return error_mark_node;
11895    }
11896
11897  /* All is well.  */
11898  return arg;
11899}
11900
11901/* Decode the list of parameter types for a function type.
11902   Given the list of things declared inside the parens,
11903   return a list of types.
11904
11905   The list we receive can have three kinds of elements:
11906   an IDENTIFIER_NODE for names given without types,
11907   a TREE_LIST node for arguments given as typespecs or names with typespecs,
11908   or void_type_node, to mark the end of an argument list
11909   when additional arguments are not permitted (... was not used).
11910
11911   FUNCDEF_FLAG is nonzero for a function definition, 0 for
11912   a mere declaration.  A nonempty identifier-list gets an error message
11913   when FUNCDEF_FLAG is zero.
11914   If FUNCDEF_FLAG is 1, then parameter types must be complete.
11915   If FUNCDEF_FLAG is -1, then parameter types may be incomplete.
11916
11917   If all elements of the input list contain types,
11918   we return a list of the types.
11919   If all elements contain no type (except perhaps a void_type_node
11920   at the end), we return a null list.
11921   If some have types and some do not, it is an error, and we
11922   return a null list.
11923
11924   Also set last_function_parms to either
11925   a list of names (IDENTIFIER_NODEs) or a chain of PARM_DECLs.
11926   A list of names is converted to a chain of PARM_DECLs
11927   by store_parm_decls so that ultimately it is always a chain of decls.
11928
11929   Note that in C++, parameters can take default values.  These default
11930   values are in the TREE_PURPOSE field of the TREE_LIST.  It is
11931   an error to specify default values which are followed by parameters
11932   that have no default values, or an ELLIPSES.  For simplicities sake,
11933   only parameters which are specified with their types can take on
11934   default values.  */
11935
11936static tree
11937grokparms (first_parm, funcdef_flag)
11938     tree first_parm;
11939     int funcdef_flag;
11940{
11941  tree result = NULL_TREE;
11942  tree decls = NULL_TREE;
11943
11944  if (first_parm != NULL_TREE
11945      && TREE_CODE (TREE_VALUE (first_parm)) == IDENTIFIER_NODE)
11946    {
11947      if (! funcdef_flag)
11948	pedwarn ("parameter names (without types) in function declaration");
11949      last_function_parms = first_parm;
11950      return NULL_TREE;
11951    }
11952  else if (first_parm != NULL_TREE
11953	   && TREE_CODE (TREE_VALUE (first_parm)) != TREE_LIST
11954	   && TREE_CODE (TREE_VALUE (first_parm)) != VOID_TYPE)
11955    my_friendly_abort (145);
11956  else
11957    {
11958      /* Types were specified.  This is a list of declarators
11959	 each represented as a TREE_LIST node.  */
11960      register tree parm, chain;
11961      int any_init = 0, any_error = 0;
11962
11963      if (first_parm != NULL_TREE)
11964	{
11965	  tree last_result = NULL_TREE;
11966	  tree last_decl = NULL_TREE;
11967
11968	  for (parm = first_parm; parm != NULL_TREE; parm = chain)
11969	    {
11970	      tree type = NULL_TREE, list_node = parm;
11971	      register tree decl = TREE_VALUE (parm);
11972	      tree init = TREE_PURPOSE (parm);
11973
11974	      chain = TREE_CHAIN (parm);
11975	      /* @@ weak defense against parse errors.  */
11976	      if (TREE_CODE (decl) != VOID_TYPE
11977		  && TREE_CODE (decl) != TREE_LIST)
11978		{
11979		  /* Give various messages as the need arises.  */
11980		  if (TREE_CODE (decl) == STRING_CST)
11981		    cp_error ("invalid string constant `%E'", decl);
11982		  else if (TREE_CODE (decl) == INTEGER_CST)
11983		    error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11984		  continue;
11985		}
11986
11987	      if (TREE_CODE (decl) != VOID_TYPE)
11988		{
11989		  decl = grokdeclarator (TREE_VALUE (decl),
11990					 TREE_PURPOSE (decl),
11991					 PARM, init != NULL_TREE,
11992					 NULL_TREE);
11993		  if (! decl || TREE_TYPE (decl) == error_mark_node)
11994		    continue;
11995
11996		  /* Top-level qualifiers on the parameters are
11997		     ignored for function types.  */
11998		  type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
11999
12000		  if (TREE_CODE (type) == VOID_TYPE)
12001		    decl = void_type_node;
12002		  else if (TREE_CODE (type) == METHOD_TYPE)
12003		    {
12004		      if (DECL_NAME (decl))
12005			/* Cannot use the decl here because
12006			   we don't have DECL_CONTEXT set up yet.  */
12007			cp_error ("parameter `%D' invalidly declared method type",
12008				  DECL_NAME (decl));
12009		      else
12010			error ("parameter invalidly declared method type");
12011		      type = build_pointer_type (type);
12012		      TREE_TYPE (decl) = type;
12013		    }
12014		  else if (TREE_CODE (type) == OFFSET_TYPE)
12015		    {
12016		      if (DECL_NAME (decl))
12017			cp_error ("parameter `%D' invalidly declared offset type",
12018				  DECL_NAME (decl));
12019		      else
12020			error ("parameter invalidly declared offset type");
12021		      type = build_pointer_type (type);
12022		      TREE_TYPE (decl) = type;
12023		    }
12024                  else if (TREE_CODE (type) == RECORD_TYPE
12025                           && TYPE_LANG_SPECIFIC (type)
12026                           && CLASSTYPE_ABSTRACT_VIRTUALS (type))
12027                    {
12028                      abstract_virtuals_error (decl, type);
12029                      any_error = 1;  /* Seems like a good idea. */
12030                    }
12031                  else if (TREE_CODE (type) == RECORD_TYPE
12032                           && TYPE_LANG_SPECIFIC (type)
12033                           && IS_SIGNATURE (type))
12034                    {
12035                      signature_error (decl, type);
12036                      any_error = 1;  /* Seems like a good idea. */
12037                    }
12038		  else if (POINTER_TYPE_P (type))
12039		    {
12040		      tree t = type;
12041		      while (POINTER_TYPE_P (t)
12042			     || (TREE_CODE (t) == ARRAY_TYPE
12043				 && TYPE_DOMAIN (t) != NULL_TREE))
12044			t = TREE_TYPE (t);
12045		      if (TREE_CODE (t) == ARRAY_TYPE)
12046			cp_error ("parameter type `%T' includes %s to array of unknown bound",
12047				  type,
12048				  TYPE_PTR_P (type) ? "pointer" : "reference");
12049		    }
12050		}
12051
12052	      if (TREE_CODE (decl) == VOID_TYPE)
12053		{
12054		  if (result == NULL_TREE)
12055		    {
12056		      result = void_list_node;
12057		      last_result = result;
12058		    }
12059		  else
12060		    {
12061		      TREE_CHAIN (last_result) = void_list_node;
12062		      last_result = void_list_node;
12063		    }
12064		  if (chain
12065		      && (chain != void_list_node || TREE_CHAIN (chain)))
12066		    error ("`void' in parameter list must be entire list");
12067		  break;
12068		}
12069
12070	      /* Since there is a prototype, args are passed in their own types.  */
12071	      DECL_ARG_TYPE (decl) = TREE_TYPE (decl);
12072#ifdef PROMOTE_PROTOTYPES
12073	      if ((TREE_CODE (type) == INTEGER_TYPE
12074		   || TREE_CODE (type) == ENUMERAL_TYPE)
12075		  && TYPE_PRECISION (type) < TYPE_PRECISION (integer_type_node))
12076		DECL_ARG_TYPE (decl) = integer_type_node;
12077#endif
12078	      if (!any_error && init)
12079		{
12080		  any_init++;
12081		  init = check_default_argument (decl, init);
12082		}
12083	      else
12084		init = NULL_TREE;
12085
12086	      if (decls == NULL_TREE)
12087		{
12088		  decls = decl;
12089		  last_decl = decls;
12090		}
12091	      else
12092		{
12093		  TREE_CHAIN (last_decl) = decl;
12094		  last_decl = decl;
12095		}
12096	      if (! current_function_decl && TREE_PERMANENT (list_node))
12097		{
12098		  TREE_PURPOSE (list_node) = init;
12099		  TREE_VALUE (list_node) = type;
12100		  TREE_CHAIN (list_node) = NULL_TREE;
12101		}
12102	      else
12103		list_node = saveable_tree_cons (init, type, NULL_TREE);
12104	      if (result == NULL_TREE)
12105		{
12106		  result = list_node;
12107		  last_result = result;
12108		}
12109	      else
12110		{
12111		  TREE_CHAIN (last_result) = list_node;
12112		  last_result = list_node;
12113		}
12114	    }
12115	  if (last_result)
12116	    TREE_CHAIN (last_result) = NULL_TREE;
12117	  /* If there are no parameters, and the function does not end
12118	     with `...', then last_decl will be NULL_TREE.  */
12119	  if (last_decl != NULL_TREE)
12120	    TREE_CHAIN (last_decl) = NULL_TREE;
12121	}
12122    }
12123
12124  last_function_parms = decls;
12125
12126  return result;
12127}
12128
12129/* Called from the parser to update an element of TYPE_ARG_TYPES for some
12130   FUNCTION_TYPE with the newly parsed version of its default argument, which
12131   was previously digested as text.  See snarf_defarg et al in lex.c.  */
12132
12133void
12134replace_defarg (arg, init)
12135     tree arg, init;
12136{
12137  if (! processing_template_decl
12138      && ! can_convert_arg (TREE_VALUE (arg), TREE_TYPE (init), init))
12139    cp_pedwarn ("invalid type `%T' for default argument to `%T'",
12140		TREE_TYPE (init), TREE_VALUE (arg));
12141  TREE_PURPOSE (arg) = init;
12142}
12143
12144/* Return 1 if D copies its arguments. This is used to test for copy
12145   constructors and copy assignment operators.  */
12146
12147int
12148copy_args_p (d)
12149     tree d;
12150{
12151  tree t = FUNCTION_ARG_CHAIN (d);
12152  if (DECL_CONSTRUCTOR_P (d)
12153      && TYPE_USES_VIRTUAL_BASECLASSES (DECL_CONTEXT (d)))
12154    {
12155      t = TREE_CHAIN (t);
12156      if (TYPE_USES_PVBASES (DECL_CONTEXT (d)))
12157	t = TREE_CHAIN (t);
12158    }
12159
12160  if (t && TREE_CODE (TREE_VALUE (t)) == REFERENCE_TYPE
12161      && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_VALUE (t)))
12162	  == DECL_CLASS_CONTEXT (d))
12163      && (TREE_CHAIN (t) == NULL_TREE
12164	  || TREE_CHAIN (t) == void_list_node
12165	  || TREE_PURPOSE (TREE_CHAIN (t))))
12166    return 1;
12167  return 0;
12168}
12169
12170/* These memoizing functions keep track of special properties which
12171   a class may have.  `grok_ctor_properties' notices whether a class
12172   has a constructor of the form X(X&), and also complains
12173   if the class has a constructor of the form X(X).
12174   `grok_op_properties' takes notice of the various forms of
12175   operator= which are defined, as well as what sorts of type conversion
12176   may apply.  Both functions take a FUNCTION_DECL as an argument.  */
12177
12178int
12179grok_ctor_properties (ctype, decl)
12180     tree ctype, decl;
12181{
12182  tree parmtypes = FUNCTION_ARG_CHAIN (decl);
12183  tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
12184
12185  /* When a type has virtual baseclasses, a magical first int argument is
12186     added to any ctor so we can tell if the class has been initialized
12187     yet.  This could screw things up in this function, so we deliberately
12188     ignore the leading int if we're in that situation.  */
12189  if (TYPE_USES_VIRTUAL_BASECLASSES (ctype)
12190      && !CLASSTYPE_IS_TEMPLATE (ctype))
12191    {
12192      my_friendly_assert (parmtypes
12193			  && TREE_VALUE (parmtypes) == integer_type_node,
12194			  980529);
12195      parmtypes = TREE_CHAIN (parmtypes);
12196      parmtype = TREE_VALUE (parmtypes);
12197    }
12198
12199  if (TYPE_USES_PVBASES (ctype))
12200    {
12201      my_friendly_assert (parmtypes
12202			  && TREE_VALUE (parmtypes) == vlist_type_node,
12203			  980529);
12204      parmtypes = TREE_CHAIN (parmtypes);
12205      parmtype = TREE_VALUE (parmtypes);
12206    }
12207
12208  maybe_vlist_ctor_wrapper (decl, 0);
12209
12210  /* [class.copy]
12211
12212     A non-template constructor for class X is a copy constructor if
12213     its first parameter is of type X&, const X&, volatile X& or const
12214     volatile X&, and either there are no other parameters or else all
12215     other parameters have default arguments.  */
12216  if (TREE_CODE (parmtype) == REFERENCE_TYPE
12217      && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == ctype
12218      && (TREE_CHAIN (parmtypes) == NULL_TREE
12219	  || TREE_CHAIN (parmtypes) == void_list_node
12220	  || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
12221      && !(DECL_TEMPLATE_INSTANTIATION (decl)
12222	   && is_member_template (DECL_TI_TEMPLATE (decl))))
12223    {
12224      TYPE_HAS_INIT_REF (ctype) = 1;
12225      if (CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
12226	TYPE_HAS_CONST_INIT_REF (ctype) = 1;
12227    }
12228  /* [class.copy]
12229
12230     A declaration of a constructor for a class X is ill-formed if its
12231     first parameter is of type (optionally cv-qualified) X and either
12232     there are no other parameters or else all other parameters have
12233     default arguments.
12234
12235     We *don't* complain about member template instantiations that
12236     have this form, though; they can occur as we try to decide what
12237     constructor to use during overload resolution.  Since overload
12238     resolution will never prefer such a constructor to the
12239     non-template copy constructor (which is either explicitly or
12240     implicitly defined), there's no need to worry about their
12241     existence.  Theoretically, they should never even be
12242     instantiated, but that's hard to forestall.  */
12243  else if (TYPE_MAIN_VARIANT (parmtype) == ctype
12244	   && (TREE_CHAIN (parmtypes) == NULL_TREE
12245	       || TREE_CHAIN (parmtypes) == void_list_node
12246	       || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
12247	   && !(DECL_TEMPLATE_INSTANTIATION (decl)
12248		&& is_member_template (DECL_TI_TEMPLATE (decl))))
12249    {
12250      cp_error ("invalid constructor; you probably meant `%T (const %T&)'",
12251		ctype, ctype);
12252      SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12253      return 0;
12254    }
12255  else if (TREE_CODE (parmtype) == VOID_TYPE
12256	   || TREE_PURPOSE (parmtypes) != NULL_TREE)
12257    TYPE_HAS_DEFAULT_CONSTRUCTOR (ctype) = 1;
12258
12259  return 1;
12260}
12261
12262/* An operator with this name can be either unary or binary.  */
12263
12264static int
12265ambi_op_p (name)
12266     tree name;
12267{
12268  return (name == ansi_opname [(int) INDIRECT_REF]
12269	  || name == ansi_opname [(int) ADDR_EXPR]
12270	  || name == ansi_opname [(int) NEGATE_EXPR]
12271	  || name == ansi_opname[(int) POSTINCREMENT_EXPR]
12272	  || name == ansi_opname[(int) POSTDECREMENT_EXPR]
12273	  || name == ansi_opname [(int) CONVERT_EXPR]);
12274}
12275
12276/* An operator with this name can only be unary.  */
12277
12278static int
12279unary_op_p (name)
12280     tree name;
12281{
12282  return (name == ansi_opname [(int) TRUTH_NOT_EXPR]
12283	  || name == ansi_opname [(int) BIT_NOT_EXPR]
12284	  || name == ansi_opname [(int) COMPONENT_REF]
12285	  || IDENTIFIER_TYPENAME_P (name));
12286}
12287
12288/* Do a little sanity-checking on how they declared their operator.  */
12289
12290void
12291grok_op_properties (decl, virtualp, friendp)
12292     tree decl;
12293     int virtualp, friendp;
12294{
12295  tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12296  int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12297  tree name = DECL_NAME (decl);
12298
12299  if (current_class_type == NULL_TREE)
12300    friendp = 1;
12301
12302  if (! friendp)
12303    {
12304      /* [class.copy]
12305
12306	 A user-declared copy assignment operator X::operator= is a
12307	 non-static non-template member function of class X with
12308	 exactly one parameter of type X, X&, const X&, volatile X& or
12309	 const volatile X&.  */
12310      if (name == ansi_opname[(int) MODIFY_EXPR]
12311	  && !(DECL_TEMPLATE_INSTANTIATION (decl)
12312	       && is_member_template (DECL_TI_TEMPLATE (decl))))
12313	;
12314      else if (name == ansi_opname[(int) CALL_EXPR])
12315	TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12316      else if (name == ansi_opname[(int) ARRAY_REF])
12317	TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12318      else if (name == ansi_opname[(int) COMPONENT_REF]
12319	       || name == ansi_opname[(int) MEMBER_REF])
12320	TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12321      else if (name == ansi_opname[(int) NEW_EXPR])
12322	TYPE_GETS_NEW (current_class_type) |= 1;
12323      else if (name == ansi_opname[(int) DELETE_EXPR])
12324	TYPE_GETS_DELETE (current_class_type) |= 1;
12325      else if (name == ansi_opname[(int) VEC_NEW_EXPR])
12326	TYPE_GETS_NEW (current_class_type) |= 2;
12327      else if (name == ansi_opname[(int) VEC_DELETE_EXPR])
12328	TYPE_GETS_DELETE (current_class_type) |= 2;
12329    }
12330
12331  if (name == ansi_opname[(int) NEW_EXPR]
12332      || name == ansi_opname[(int) VEC_NEW_EXPR])
12333    {
12334      /* When the compiler encounters the definition of A::operator new, it
12335	 doesn't look at the class declaration to find out if it's static.  */
12336      if (methodp)
12337	revert_static_member_fn (&decl, NULL, NULL);
12338
12339      /* Take care of function decl if we had syntax errors.  */
12340      if (argtypes == NULL_TREE)
12341	TREE_TYPE (decl)
12342	  = build_function_type (ptr_type_node,
12343				 hash_tree_chain (integer_type_node,
12344						  void_list_node));
12345      else
12346	TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12347    }
12348  else if (name == ansi_opname[(int) DELETE_EXPR]
12349	   || name == ansi_opname[(int) VEC_DELETE_EXPR])
12350    {
12351      if (methodp)
12352	revert_static_member_fn (&decl, NULL, NULL);
12353
12354      if (argtypes == NULL_TREE)
12355	TREE_TYPE (decl)
12356	  = build_function_type (void_type_node,
12357				 hash_tree_chain (ptr_type_node,
12358						  void_list_node));
12359      else
12360	{
12361	  TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12362
12363	  if (! friendp && name == ansi_opname[(int) VEC_DELETE_EXPR]
12364	      && (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
12365		  != void_list_node))
12366	    TYPE_VEC_DELETE_TAKES_SIZE (current_class_type) = 1;
12367	}
12368    }
12369  else
12370    {
12371      /* An operator function must either be a non-static member function
12372	 or have at least one parameter of a class, a reference to a class,
12373	 an enumeration, or a reference to an enumeration.  13.4.0.6 */
12374      if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12375	{
12376	  if (IDENTIFIER_TYPENAME_P (name)
12377	      || name == ansi_opname[(int) CALL_EXPR]
12378	      || name == ansi_opname[(int) MODIFY_EXPR]
12379	      || name == ansi_opname[(int) COMPONENT_REF]
12380	      || name == ansi_opname[(int) ARRAY_REF])
12381	    cp_error ("`%D' must be a nonstatic member function", decl);
12382	  else
12383	    {
12384	      tree p = argtypes;
12385
12386	      if (DECL_STATIC_FUNCTION_P (decl))
12387		cp_error ("`%D' must be either a non-static member function or a non-member function", decl);
12388
12389	      if (p)
12390		for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12391		  {
12392		    tree arg = TREE_VALUE (p);
12393		    if (TREE_CODE (arg) == REFERENCE_TYPE)
12394		      arg = TREE_TYPE (arg);
12395
12396		    /* This lets bad template code slip through.  */
12397		    if (IS_AGGR_TYPE (arg)
12398			|| TREE_CODE (arg) == ENUMERAL_TYPE
12399			|| TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12400			|| TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
12401		      goto foundaggr;
12402		  }
12403	      cp_error
12404		("`%D' must have an argument of class or enumerated type",
12405		 decl);
12406	    foundaggr:
12407	      ;
12408	    }
12409	}
12410
12411      if (name == ansi_opname[(int) CALL_EXPR])
12412	return;			/* No restrictions on args. */
12413
12414      if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12415	{
12416	  tree t = TREE_TYPE (name);
12417	  if (TREE_CODE (t) == VOID_TYPE)
12418	    pedwarn ("void is not a valid type conversion operator");
12419	  else if (! friendp)
12420	    {
12421	      int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12422	      const char *what = 0;
12423	      if (ref)
12424		t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12425
12426	      if (t == current_class_type)
12427		what = "the same type";
12428	      /* Don't force t to be complete here.  */
12429	      else if (IS_AGGR_TYPE (t)
12430		       && TYPE_SIZE (t)
12431		       && DERIVED_FROM_P (t, current_class_type))
12432		what = "a base class";
12433
12434	      if (what)
12435		warning ("conversion to %s%s will never use a type conversion operator",
12436			 ref ? "a reference to " : "", what);
12437	    }
12438	}
12439
12440      if (name == ansi_opname[(int) MODIFY_EXPR])
12441	{
12442	  tree parmtype;
12443
12444	  if (list_length (argtypes) != 3 && methodp)
12445	    {
12446	      cp_error ("`%D' must take exactly one argument", decl);
12447	      return;
12448	    }
12449	  parmtype = TREE_VALUE (TREE_CHAIN (argtypes));
12450
12451	  if (copy_assignment_arg_p (parmtype, virtualp)
12452	      && ! friendp)
12453	    {
12454	      TYPE_HAS_ASSIGN_REF (current_class_type) = 1;
12455	      if (TREE_CODE (parmtype) != REFERENCE_TYPE
12456		  || CP_TYPE_CONST_P (TREE_TYPE (parmtype)))
12457		TYPE_HAS_CONST_ASSIGN_REF (current_class_type) = 1;
12458	    }
12459	}
12460      else if (name == ansi_opname[(int) COND_EXPR])
12461	{
12462	  /* 13.4.0.3 */
12463	  pedwarn ("ANSI C++ prohibits overloading operator ?:");
12464	  if (list_length (argtypes) != 4)
12465	    cp_error ("`%D' must take exactly three arguments", decl);
12466	}
12467      else if (ambi_op_p (name))
12468	{
12469	  if (list_length (argtypes) == 2)
12470	    /* prefix */;
12471	  else if (list_length (argtypes) == 3)
12472	    {
12473	      if ((name == ansi_opname[(int) POSTINCREMENT_EXPR]
12474		   || name == ansi_opname[(int) POSTDECREMENT_EXPR])
12475		  && ! processing_template_decl
12476		  && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12477		{
12478		  if (methodp)
12479		    cp_error ("postfix `%D' must take `int' as its argument",
12480			      decl);
12481		  else
12482		    cp_error
12483		      ("postfix `%D' must take `int' as its second argument",
12484		       decl);
12485		}
12486	    }
12487	  else
12488	    {
12489	      if (methodp)
12490		cp_error ("`%D' must take either zero or one argument", decl);
12491	      else
12492		cp_error ("`%D' must take either one or two arguments", decl);
12493	    }
12494
12495	  /* More Effective C++ rule 6.  */
12496	  if (warn_ecpp
12497	      && (name == ansi_opname[(int) POSTINCREMENT_EXPR]
12498		  || name == ansi_opname[(int) POSTDECREMENT_EXPR]))
12499	    {
12500	      tree arg = TREE_VALUE (argtypes);
12501	      tree ret = TREE_TYPE (TREE_TYPE (decl));
12502	      if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12503		arg = TREE_TYPE (arg);
12504	      arg = TYPE_MAIN_VARIANT (arg);
12505	      if (list_length (argtypes) == 2)
12506		{
12507		  if (TREE_CODE (ret) != REFERENCE_TYPE
12508		      || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12509				       arg))
12510		    cp_warning ("prefix `%D' should return `%T'", decl,
12511				build_reference_type (arg));
12512		}
12513	      else
12514		{
12515		  if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12516		    cp_warning ("postfix `%D' should return `%T'", decl, arg);
12517		}
12518	    }
12519	}
12520      else if (unary_op_p (name))
12521	{
12522	  if (list_length (argtypes) != 2)
12523	    {
12524	      if (methodp)
12525		cp_error ("`%D' must take `void'", decl);
12526	      else
12527		cp_error ("`%D' must take exactly one argument", decl);
12528	    }
12529	}
12530      else /* if (binary_op_p (name)) */
12531	{
12532	  if (list_length (argtypes) != 3)
12533	    {
12534	      if (methodp)
12535		cp_error ("`%D' must take exactly one argument", decl);
12536	      else
12537		cp_error ("`%D' must take exactly two arguments", decl);
12538	    }
12539
12540	  /* More Effective C++ rule 7.  */
12541	  if (warn_ecpp
12542	      && (name == ansi_opname [TRUTH_ANDIF_EXPR]
12543		  || name == ansi_opname [TRUTH_ORIF_EXPR]
12544		  || name == ansi_opname [COMPOUND_EXPR]))
12545	    cp_warning ("user-defined `%D' always evaluates both arguments",
12546			decl);
12547	}
12548
12549      /* Effective C++ rule 23.  */
12550      if (warn_ecpp
12551	  && list_length (argtypes) == 3
12552	  && (name == ansi_opname [PLUS_EXPR]
12553	      || name == ansi_opname [MINUS_EXPR]
12554	      || name == ansi_opname [TRUNC_DIV_EXPR]
12555	      || name == ansi_opname [MULT_EXPR])
12556	  && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12557	cp_warning ("`%D' should return by value", decl);
12558
12559      /* 13.4.0.8 */
12560      if (argtypes)
12561	for (; argtypes != void_list_node ; argtypes = TREE_CHAIN (argtypes))
12562	  if (TREE_PURPOSE (argtypes))
12563	    {
12564	      TREE_PURPOSE (argtypes) = NULL_TREE;
12565	      if (name == ansi_opname[(int) POSTINCREMENT_EXPR]
12566		  || name == ansi_opname[(int) POSTDECREMENT_EXPR])
12567		{
12568		  if (pedantic)
12569		    cp_pedwarn ("`%D' cannot have default arguments", decl);
12570		}
12571	      else
12572		cp_error ("`%D' cannot have default arguments", decl);
12573	    }
12574    }
12575}
12576
12577static const char *
12578tag_name (code)
12579     enum tag_types code;
12580{
12581  switch (code)
12582    {
12583    case record_type:
12584      return "struct";
12585    case class_type:
12586      return "class";
12587    case union_type:
12588      return "union ";
12589    case enum_type:
12590      return "enum";
12591    case signature_type:
12592      return "signature";
12593    default:
12594      my_friendly_abort (981122);
12595    }
12596}
12597
12598/* Get the struct, enum or union (CODE says which) with tag NAME.
12599   Define the tag as a forward-reference if it is not defined.
12600
12601   C++: If a class derivation is given, process it here, and report
12602   an error if multiple derivation declarations are not identical.
12603
12604   If this is a definition, come in through xref_tag and only look in
12605   the current frame for the name (since C++ allows new names in any
12606   scope.)  */
12607
12608tree
12609xref_tag (code_type_node, name, globalize)
12610     tree code_type_node;
12611     tree name;
12612     int globalize;
12613{
12614  enum tag_types tag_code;
12615  enum tree_code code;
12616  int temp = 0;
12617  register tree ref, t;
12618  struct binding_level *b = current_binding_level;
12619  int got_type = 0;
12620  tree attributes = NULL_TREE;
12621  tree context = NULL_TREE;
12622
12623  /* If we are called from the parser, code_type_node will sometimes be a
12624     TREE_LIST.  This indicates that the user wrote
12625     "class __attribute__ ((foo)) bar".  Extract the attributes so we can
12626     use them later.  */
12627  if (TREE_CODE (code_type_node) == TREE_LIST)
12628    {
12629      attributes = TREE_PURPOSE (code_type_node);
12630      code_type_node = TREE_VALUE (code_type_node);
12631    }
12632
12633  tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12634  switch (tag_code)
12635    {
12636    case record_type:
12637    case class_type:
12638    case signature_type:
12639      code = RECORD_TYPE;
12640      break;
12641    case union_type:
12642      code = UNION_TYPE;
12643      break;
12644    case enum_type:
12645      code = ENUMERAL_TYPE;
12646      break;
12647    default:
12648      my_friendly_abort (18);
12649    }
12650
12651  /* If a cross reference is requested, look up the type
12652     already defined for this tag and return it.  */
12653  if (TREE_CODE_CLASS (TREE_CODE (name)) == 't')
12654    {
12655      t = name;
12656      name = TYPE_IDENTIFIER (t);
12657      got_type = 1;
12658    }
12659  else
12660    t = IDENTIFIER_TYPE_VALUE (name);
12661
12662  if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12663      && TREE_CODE (t) != TEMPLATE_TEMPLATE_PARM)
12664    t = NULL_TREE;
12665
12666  if (! globalize)
12667    {
12668      /* If we know we are defining this tag, only look it up in
12669	 this scope and don't try to find it as a type.  */
12670      ref = lookup_tag (code, name, b, 1);
12671    }
12672  else
12673    {
12674      if (t)
12675	{
12676	  /* [dcl.type.elab] If the identifier resolves to a
12677	     typedef-name or a template type-parameter, the
12678	     elaborated-type-specifier is ill-formed.  */
12679	  if (t != TYPE_MAIN_VARIANT (t)
12680	      || (CLASS_TYPE_P (t) && TYPE_WAS_ANONYMOUS (t)))
12681	    cp_pedwarn ("using typedef-name `%D' after `%s'",
12682			TYPE_NAME (t), tag_name (tag_code));
12683	  else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12684	    cp_error ("using template type parameter `%T' after `%s'",
12685		      t, tag_name (tag_code));
12686
12687	  ref = t;
12688	}
12689      else
12690	ref = lookup_tag (code, name, b, 0);
12691
12692      if (! ref)
12693	{
12694	  /* Try finding it as a type declaration.  If that wins,
12695	     use it.  */
12696	  ref = lookup_name (name, 1);
12697
12698	  if (ref != NULL_TREE
12699	      && processing_template_decl
12700	      && DECL_CLASS_TEMPLATE_P (ref)
12701	      && template_class_depth (current_class_type) == 0)
12702	    /* Since GLOBALIZE is true, we're declaring a global
12703	       template, so we want this type.  */
12704	    ref = DECL_RESULT (ref);
12705
12706	  if (ref && TREE_CODE (ref) == TYPE_DECL
12707	      && TREE_CODE (TREE_TYPE (ref)) == code)
12708	    ref = TREE_TYPE (ref);
12709	  else
12710	    ref = NULL_TREE;
12711	}
12712
12713      if (ref && current_class_type
12714	  && template_class_depth (current_class_type)
12715	  && PROCESSING_REAL_TEMPLATE_DECL_P ())
12716	{
12717	  /* Since GLOBALIZE is non-zero, we are not looking at a
12718	     definition of this tag.  Since, in addition, we are currently
12719	     processing a (member) template declaration of a template
12720	     class, we must be very careful; consider:
12721
12722	       template <class X>
12723	       struct S1
12724
12725	       template <class U>
12726	       struct S2
12727	       { template <class V>
12728	       friend struct S1; };
12729
12730	     Here, the S2::S1 declaration should not be confused with the
12731	     outer declaration.  In particular, the inner version should
12732	     have a template parameter of level 2, not level 1.  This
12733	     would be particularly important if the member declaration
12734	     were instead:
12735
12736	       template <class V = U> friend struct S1;
12737
12738	     say, when we should tsubst into `U' when instantiating
12739	     S2.  On the other hand, when presented with:
12740
12741	         template <class T>
12742	         struct S1 {
12743		   template <class U>
12744	           struct S2 {};
12745		   template <class U>
12746		   friend struct S2;
12747		 };
12748
12749              we must find the inner binding eventually.  We
12750	      accomplish this by making sure that the new type we
12751	      create to represent this declaration has the right
12752	      TYPE_CONTEXT.  */
12753	  context = TYPE_CONTEXT (ref);
12754	  ref = NULL_TREE;
12755	}
12756    }
12757
12758  push_obstacks_nochange ();
12759
12760  if (! ref)
12761    {
12762      /* If no such tag is yet defined, create a forward-reference node
12763	 and record it as the "definition".
12764	 When a real declaration of this type is found,
12765	 the forward-reference will be altered into a real type.  */
12766
12767      /* In C++, since these migrate into the global scope, we must
12768	 build them on the permanent obstack.  */
12769
12770      temp = allocation_temporary_p ();
12771      if (temp)
12772	end_temporary_allocation ();
12773
12774      if (code == ENUMERAL_TYPE)
12775	{
12776	  cp_error ("use of enum `%#D' without previous declaration", name);
12777
12778	  ref = make_node (ENUMERAL_TYPE);
12779
12780	  /* Give the type a default layout like unsigned int
12781	     to avoid crashing if it does not get defined.  */
12782	  TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12783	  TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12784	  TREE_UNSIGNED (ref) = 1;
12785	  TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12786	  TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12787	  TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12788
12789	  /* Enable us to recognize when a type is created in class context.
12790	     To do nested classes correctly, this should probably be cleared
12791	     out when we leave this classes scope.  Currently this in only
12792	     done in `start_enum'.  */
12793
12794	  pushtag (name, ref, globalize);
12795	}
12796      else
12797	{
12798	  struct binding_level *old_b = class_binding_level;
12799
12800	  ref = make_lang_type (code);
12801	  TYPE_CONTEXT (ref) = context;
12802
12803	  if (tag_code == signature_type)
12804	    {
12805	      SET_SIGNATURE (ref);
12806	      /* Since a signature type will be turned into the type
12807		 of signature tables, it's not only an interface.  */
12808	      CLASSTYPE_INTERFACE_ONLY (ref) = 0;
12809	      SET_CLASSTYPE_INTERFACE_KNOWN (ref);
12810	      /* A signature doesn't have a vtable.  */
12811	      CLASSTYPE_VTABLE_NEEDS_WRITING (ref) = 0;
12812	    }
12813
12814#ifdef NONNESTED_CLASSES
12815	  /* Class types don't nest the way enums do.  */
12816	  class_binding_level = (struct binding_level *)0;
12817#endif
12818	  pushtag (name, ref, globalize);
12819	  class_binding_level = old_b;
12820	}
12821    }
12822  else
12823    {
12824      /* If it no longer looks like a nested type, make sure it's
12825	 in global scope.
12826         If it is not an IDENTIFIER, this is not a declaration */
12827      if (b->namespace_p && !class_binding_level
12828	  && TREE_CODE (name) == IDENTIFIER_NODE)
12829	{
12830	  if (IDENTIFIER_NAMESPACE_VALUE (name) == NULL_TREE)
12831	    SET_IDENTIFIER_NAMESPACE_VALUE (name, TYPE_NAME (ref));
12832	}
12833
12834      if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12835	redeclare_class_template (ref, current_template_parms);
12836    }
12837
12838  /* Until the type is defined, tentatively accept whatever
12839     structure tag the user hands us.  */
12840  if (TYPE_SIZE (ref) == NULL_TREE
12841      && ref != current_class_type
12842      /* Have to check this, in case we have contradictory tag info.  */
12843      && IS_AGGR_TYPE_CODE (TREE_CODE (ref)))
12844    {
12845      if (tag_code == class_type)
12846	CLASSTYPE_DECLARED_CLASS (ref) = 1;
12847      else if (tag_code == record_type || tag_code == signature_type)
12848	CLASSTYPE_DECLARED_CLASS (ref) = 0;
12849    }
12850
12851  pop_obstacks ();
12852
12853  TREE_TYPE (ref) = attributes;
12854
12855  return ref;
12856}
12857
12858tree
12859xref_tag_from_type (old, id, globalize)
12860     tree old, id;
12861     int globalize;
12862{
12863  tree code_type_node;
12864
12865  if (TREE_CODE (old) == RECORD_TYPE)
12866    code_type_node = (CLASSTYPE_DECLARED_CLASS (old)
12867		      ? class_type_node : record_type_node);
12868  else
12869    code_type_node = union_type_node;
12870
12871  if (id == NULL_TREE)
12872    id = TYPE_IDENTIFIER (old);
12873
12874  return xref_tag (code_type_node, id, globalize);
12875}
12876
12877/* REF is a type (named NAME), for which we have just seen some
12878   baseclasses.  BINFO is a list of those baseclasses; the
12879   TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12880   the base-class.  CODE_TYPE_NODE indicates whether REF is a class,
12881   struct, or union.  */
12882
12883void
12884xref_basetypes (code_type_node, name, ref, binfo)
12885     tree code_type_node;
12886     tree name, ref;
12887     tree binfo;
12888{
12889  /* In the declaration `A : X, Y, ... Z' we mark all the types
12890     (A, X, Y, ..., Z) so we can check for duplicates.  */
12891  tree binfos;
12892  tree base;
12893
12894  int i, len;
12895  enum tag_types tag_code = (enum tag_types) TREE_INT_CST_LOW (code_type_node);
12896
12897  if (tag_code == union_type)
12898    {
12899      cp_error ("derived union `%T' invalid", ref);
12900      return;
12901    }
12902
12903  len = list_length (binfo);
12904  push_obstacks (TYPE_OBSTACK (ref), TYPE_OBSTACK (ref));
12905
12906  /* First, make sure that any templates in base-classes are
12907     instantiated.  This ensures that if we call ourselves recursively
12908     we do not get confused about which classes are marked and which
12909     are not.  */
12910  for (base = binfo; base; base = TREE_CHAIN (base))
12911    complete_type (TREE_VALUE (base));
12912
12913  SET_CLASSTYPE_MARKED (ref);
12914  BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
12915
12916  for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12917    {
12918      /* The base of a derived struct is public by default.  */
12919      int via_public
12920	= (TREE_PURPOSE (binfo) == access_public_node
12921	   || TREE_PURPOSE (binfo) == access_public_virtual_node
12922	   || (tag_code != class_type
12923	       && (TREE_PURPOSE (binfo) == access_default_node
12924		   || TREE_PURPOSE (binfo) == access_default_virtual_node)));
12925      int via_protected
12926	= (TREE_PURPOSE (binfo) == access_protected_node
12927	   || TREE_PURPOSE (binfo) == access_protected_virtual_node);
12928      int via_virtual
12929	= (TREE_PURPOSE (binfo) == access_private_virtual_node
12930	   || TREE_PURPOSE (binfo) == access_protected_virtual_node
12931	   || TREE_PURPOSE (binfo) == access_public_virtual_node
12932	   || TREE_PURPOSE (binfo) == access_default_virtual_node);
12933      tree basetype = TREE_VALUE (binfo);
12934      tree base_binfo;
12935
12936      if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12937	basetype = TREE_TYPE (basetype);
12938      if (!basetype
12939	  || (TREE_CODE (basetype) != RECORD_TYPE
12940	      && TREE_CODE (basetype) != TYPENAME_TYPE
12941	      && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12942	      && TREE_CODE (basetype) != TEMPLATE_TEMPLATE_PARM))
12943	{
12944	  cp_error ("base type `%T' fails to be a struct or class type",
12945		    TREE_VALUE (binfo));
12946	  continue;
12947	}
12948
12949      GNU_xref_hier (name, basetype, via_public, via_virtual, 0);
12950
12951      /* This code replaces similar code in layout_basetypes.
12952         We put the complete_type first for implicit `typename'.  */
12953      if (TYPE_SIZE (basetype) == NULL_TREE
12954	  && ! (current_template_parms && uses_template_parms (basetype)))
12955	{
12956	  cp_error ("base class `%T' has incomplete type", basetype);
12957	  continue;
12958	}
12959      else
12960	{
12961	  if (CLASSTYPE_MARKED (basetype))
12962	    {
12963	      if (basetype == ref)
12964		cp_error ("recursive type `%T' undefined", basetype);
12965	      else
12966		cp_error ("duplicate base type `%T' invalid", basetype);
12967	      continue;
12968	    }
12969
12970	  if (TYPE_FOR_JAVA (basetype)
12971	      && current_lang_stack == current_lang_base)
12972	    TYPE_FOR_JAVA (ref) = 1;
12973
12974	  /* Note that the BINFO records which describe individual
12975	     inheritances are *not* shared in the lattice!  They
12976	     cannot be shared because a given baseclass may be
12977	     inherited with different `accessibility' by different
12978	     derived classes.  (Each BINFO record describing an
12979	     individual inheritance contains flags which say what
12980	     the `accessibility' of that particular inheritance is.)  */
12981
12982	  base_binfo
12983	    = make_binfo (integer_zero_node, basetype,
12984			  CLASS_TYPE_P (basetype)
12985			  ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12986			  CLASS_TYPE_P (basetype)
12987			  ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
12988
12989	  TREE_VEC_ELT (binfos, i) = base_binfo;
12990	  TREE_VIA_PUBLIC (base_binfo) = via_public;
12991	  TREE_VIA_PROTECTED (base_binfo) = via_protected;
12992	  TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12993	  BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12994
12995	  /* We need to unshare the binfos now so that lookups during class
12996	     definition work.  */
12997	  unshare_base_binfos (base_binfo);
12998
12999	  SET_CLASSTYPE_MARKED (basetype);
13000
13001	  /* We are free to modify these bits because they are meaningless
13002	     at top level, and BASETYPE is a top-level type.  */
13003	  if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
13004	    {
13005	      TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
13006	      TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
13007	      /* The PVBASES flag is never set for templates; we know
13008		 only for instantiations whether the virtual bases are
13009		 polymorphic. */
13010	      if (flag_vtable_thunks >= 2 && !CLASSTYPE_IS_TEMPLATE (ref))
13011		{
13012		  if (via_virtual && TYPE_VIRTUAL_P (basetype))
13013		    TYPE_USES_PVBASES (ref) = 1;
13014		  else if (TYPE_USES_PVBASES (basetype))
13015		    TYPE_USES_PVBASES (ref) = 1;
13016		}
13017	    }
13018
13019	  if (CLASS_TYPE_P (basetype))
13020	    {
13021	      TYPE_GETS_NEW (ref) |= TYPE_GETS_NEW (basetype);
13022	      TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13023	    }
13024
13025	  i += 1;
13026	}
13027    }
13028  if (i)
13029    TREE_VEC_LENGTH (binfos) = i;
13030  else
13031    BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
13032
13033  if (i > 1)
13034    TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
13035  else if (i == 1)
13036    {
13037      tree basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, 0));
13038
13039      if (CLASS_TYPE_P (basetype))
13040	TYPE_USES_MULTIPLE_INHERITANCE (ref)
13041	  = TYPE_USES_MULTIPLE_INHERITANCE (basetype);
13042    }
13043
13044  if (TYPE_USES_MULTIPLE_INHERITANCE (ref))
13045    TYPE_USES_COMPLEX_INHERITANCE (ref) = 1;
13046
13047  /* Unmark all the types.  */
13048  while (--i >= 0)
13049    CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
13050  CLEAR_CLASSTYPE_MARKED (ref);
13051
13052  /* Now that we know all the base-classes, set up the list of virtual
13053     bases.  */
13054  CLASSTYPE_VBASECLASSES (ref) = get_vbase_types (ref);
13055
13056  pop_obstacks ();
13057}
13058
13059
13060/* Begin compiling the definition of an enumeration type.
13061   NAME is its name (or null if anonymous).
13062   Returns the type object, as yet incomplete.
13063   Also records info about it so that build_enumerator
13064   may be used to declare the individual values as they are read.  */
13065
13066tree
13067start_enum (name)
13068     tree name;
13069{
13070  register tree enumtype = NULL_TREE;
13071  struct binding_level *b = current_binding_level;
13072
13073  /* We are wasting space here and putting these on the permanent_obstack so
13074     that typeid(local enum) will work correctly. */
13075  push_obstacks (&permanent_obstack, &permanent_obstack);
13076
13077  /* If this is the real definition for a previous forward reference,
13078     fill in the contents in the same object that used to be the
13079     forward reference.  */
13080
13081  if (name != NULL_TREE)
13082    enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13083
13084  if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13085    cp_error ("multiple definition of `%#T'", enumtype);
13086  else
13087    {
13088      enumtype = make_node (ENUMERAL_TYPE);
13089      pushtag (name, enumtype, 0);
13090    }
13091
13092  if (current_class_type)
13093    TREE_ADDRESSABLE (b->tags) = 1;
13094
13095  /* We don't copy this value because build_enumerator needs to do it.  */
13096  enum_next_value = integer_zero_node;
13097  enum_overflow = 0;
13098
13099  GNU_xref_decl (current_function_decl, enumtype);
13100  return enumtype;
13101}
13102
13103/* After processing and defining all the values of an enumeration type,
13104   install their decls in the enumeration type and finish it off.
13105   ENUMTYPE is the type object and VALUES a list of name-value pairs.
13106   Returns ENUMTYPE.  */
13107
13108tree
13109finish_enum (enumtype)
13110     tree enumtype;
13111{
13112  register tree minnode = NULL_TREE, maxnode = NULL_TREE;
13113  /* Calculate the maximum value of any enumerator in this type.  */
13114
13115  tree values = TYPE_VALUES (enumtype);
13116  if (values)
13117    {
13118      tree pair;
13119
13120      for (pair = values; pair; pair = TREE_CHAIN (pair))
13121	{
13122	  tree decl;
13123	  tree value;
13124
13125	  /* The TREE_VALUE is a CONST_DECL for this enumeration
13126	     constant.  */
13127	  decl = TREE_VALUE (pair);
13128
13129	  /* The DECL_INITIAL will be NULL if we are processing a
13130	     template declaration and this enumeration constant had no
13131	     explicit initializer.  */
13132	  value = DECL_INITIAL (decl);
13133	  if (value && !processing_template_decl)
13134	    {
13135	      /* Set the TREE_TYPE for the VALUE as well.  That's so
13136		 that when we call decl_constant_value we get an
13137		 entity of the right type (but with the constant
13138		 value).  Since we shouldn't ever call
13139		 decl_constant_value on a template type, there's no
13140		 reason to do that when processing_template_decl.
13141		 And, if the expression is something like a
13142		 TEMPLATE_PARM_INDEX or a CAST_EXPR doing so will
13143		 wreak havoc on the intended type of the expression.
13144
13145	         Of course, there's also no point in trying to compute
13146		 minimum or maximum values if we're in a template.  */
13147	      TREE_TYPE (value) = enumtype;
13148
13149	      if (!minnode)
13150		minnode = maxnode = value;
13151	      else if (tree_int_cst_lt (maxnode, value))
13152		maxnode = value;
13153	      else if (tree_int_cst_lt (value, minnode))
13154		minnode = value;
13155	    }
13156
13157	  if (processing_template_decl)
13158	    /* If this is just a template, leave the CONST_DECL
13159	       alone.  That way tsubst_copy will find CONST_DECLs for
13160	       CONST_DECLs, and not INTEGER_CSTs.  */
13161	    ;
13162	  else
13163	    /* In the list we're building up, we want the enumeration
13164	       values, not the CONST_DECLs.  */
13165	    TREE_VALUE (pair) = value;
13166	}
13167    }
13168  else
13169    maxnode = minnode = integer_zero_node;
13170
13171  TYPE_VALUES (enumtype) = nreverse (values);
13172
13173  if (processing_template_decl)
13174    {
13175      tree scope = current_scope ();
13176      if (scope && TREE_CODE (scope) == FUNCTION_DECL)
13177	add_tree (build_min (TAG_DEFN, enumtype));
13178    }
13179  else
13180    {
13181      int unsignedp = tree_int_cst_sgn (minnode) >= 0;
13182      int lowprec = min_precision (minnode, unsignedp);
13183      int highprec = min_precision (maxnode, unsignedp);
13184      int precision = MAX (lowprec, highprec);
13185      tree tem;
13186
13187      TYPE_SIZE (enumtype) = NULL_TREE;
13188
13189      /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
13190
13191      TYPE_PRECISION (enumtype) = precision;
13192      if (unsignedp)
13193	fixup_unsigned_type (enumtype);
13194      else
13195	fixup_signed_type (enumtype);
13196
13197      if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13198	/* Use the width of the narrowest normal C type which is wide
13199	   enough.  */
13200	TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
13201						    (precision, 1));
13202      else
13203	TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13204
13205      TYPE_SIZE (enumtype) = 0;
13206      layout_type (enumtype);
13207
13208      /* Fix up all variant types of this enum type.  */
13209      for (tem = TYPE_MAIN_VARIANT (enumtype); tem;
13210	   tem = TYPE_NEXT_VARIANT (tem))
13211	{
13212	  TYPE_VALUES (tem) = TYPE_VALUES (enumtype);
13213	  TYPE_MIN_VALUE (tem) = TYPE_MIN_VALUE (enumtype);
13214	  TYPE_MAX_VALUE (tem) = TYPE_MAX_VALUE (enumtype);
13215	  TYPE_SIZE (tem) = TYPE_SIZE (enumtype);
13216	  TYPE_SIZE_UNIT (tem) = TYPE_SIZE_UNIT (enumtype);
13217	  TYPE_MODE (tem) = TYPE_MODE (enumtype);
13218	  TYPE_PRECISION (tem) = TYPE_PRECISION (enumtype);
13219	  TYPE_ALIGN (tem) = TYPE_ALIGN (enumtype);
13220	  TREE_UNSIGNED (tem) = TREE_UNSIGNED (enumtype);
13221	}
13222
13223      /* Finish debugging output for this type.  */
13224      rest_of_type_compilation (enumtype, namespace_bindings_p ());
13225    }
13226
13227  /* In start_enum we pushed obstacks.  Here, we must pop them.  */
13228  pop_obstacks ();
13229
13230  return enumtype;
13231}
13232
13233/* Build and install a CONST_DECL for an enumeration constant of the
13234   enumeration type TYPE whose NAME and VALUE (if any) are provided.
13235   Assignment of sequential values by default is handled here.  */
13236
13237tree
13238build_enumerator (name, value, type)
13239     tree name;
13240     tree value;
13241     tree type;
13242{
13243  tree decl, result;
13244  tree context;
13245
13246  /* Remove no-op casts from the value.  */
13247  if (value)
13248    STRIP_TYPE_NOPS (value);
13249
13250 if (! processing_template_decl)
13251   {
13252     /* Validate and default VALUE.  */
13253     if (value != NULL_TREE)
13254       {
13255	 if (TREE_READONLY_DECL_P (value))
13256	   value = decl_constant_value (value);
13257
13258	 if (TREE_CODE (value) == INTEGER_CST)
13259	   {
13260	     value = default_conversion (value);
13261	     constant_expression_warning (value);
13262	   }
13263	 else
13264	   {
13265	     cp_error ("enumerator value for `%D' not integer constant", name);
13266	     value = NULL_TREE;
13267	   }
13268       }
13269
13270     /* Default based on previous value.  */
13271     if (value == NULL_TREE && ! processing_template_decl)
13272       {
13273	 value = enum_next_value;
13274	 if (enum_overflow)
13275	   cp_error ("overflow in enumeration values at `%D'", name);
13276       }
13277
13278     /* Remove no-op casts from the value.  */
13279     if (value)
13280       STRIP_TYPE_NOPS (value);
13281#if 0
13282     /* To fix MAX_VAL enum consts. (bkoz)  */
13283     TREE_TYPE (value) = integer_type_node;
13284#endif
13285   }
13286
13287 /* We always have to copy here; not all INTEGER_CSTs are unshared.
13288    Even in other cases, we will later (in finish_enum) be setting the
13289    type of VALUE.  */
13290 if (value != NULL_TREE)
13291   value = copy_node (value);
13292
13293  /* C++ associates enums with global, function, or class declarations.  */
13294
13295 context = current_scope ();
13296 if (context && context == current_class_type)
13297   /* This enum declaration is local to the class.  */
13298   decl = build_lang_field_decl (CONST_DECL, name, type);
13299 else
13300   /* It's a global enum, or it's local to a function.  (Note local to
13301      a function could mean local to a class method.  */
13302   decl = build_decl (CONST_DECL, name, type);
13303
13304 DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13305 DECL_INITIAL (decl) = value;
13306 TREE_READONLY (decl) = 1;
13307
13308 if (context && context == current_class_type)
13309   /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13310      on the TYPE_FIELDS list for `S'.  (That's so that you can say
13311      things like `S::i' later.)  */
13312   finish_member_declaration (decl);
13313 else
13314   {
13315     pushdecl (decl);
13316     GNU_xref_decl (current_function_decl, decl);
13317   }
13318
13319 if (! processing_template_decl)
13320   {
13321     /* Set basis for default for next value.  */
13322     enum_next_value = build_binary_op_nodefault (PLUS_EXPR, value,
13323						  integer_one_node, PLUS_EXPR);
13324     enum_overflow = tree_int_cst_lt (enum_next_value, value);
13325   }
13326
13327  result = saveable_tree_cons (name, decl, NULL_TREE);
13328  return result;
13329}
13330
13331
13332static int function_depth;
13333
13334/* Create the FUNCTION_DECL for a function definition.
13335   DECLSPECS and DECLARATOR are the parts of the declaration;
13336   they describe the function's name and the type it returns,
13337   but twisted together in a fashion that parallels the syntax of C.
13338
13339   If PRE_PARSED_P is non-zero then DECLARATOR is really the DECL for
13340   the function we are about to process; DECLSPECS are ignored.  For
13341   example, we set PRE_PARSED_P when processing the definition of
13342   inline function that was defined in-class; the definition is
13343   actually processed when the class is complete.  In this case,
13344   PRE_PARSED_P is 2.  We also set PRE_PARSED_P when instanting the
13345   body of a template function, and when constructing thunk functions
13346   and such; in these cases PRE_PARSED_P is 1.
13347
13348   This function creates a binding context for the function body
13349   as well as setting up the FUNCTION_DECL in current_function_decl.
13350
13351   Returns 1 on success.  If the DECLARATOR is not suitable for a function
13352   (it defines a datum instead), we return 0, which tells
13353   yyparse to report a parse error.
13354
13355   For C++, we must first check whether that datum makes any sense.
13356   For example, "class A local_a(1,2);" means that variable local_a
13357   is an aggregate of type A, which should have a constructor
13358   applied to it with the argument list [1, 2].
13359
13360   @@ There is currently no way to retrieve the storage
13361   @@ allocated to FUNCTION (or all of its parms) if we return
13362   @@ something we had previously.  */
13363
13364int
13365start_function (declspecs, declarator, attrs, pre_parsed_p)
13366     tree declspecs, declarator, attrs;
13367     int pre_parsed_p;
13368{
13369  tree decl1;
13370  tree ctype = NULL_TREE;
13371  tree fntype;
13372  tree restype;
13373  extern int have_extern_spec;
13374  extern int used_extern_spec;
13375  int doing_friend = 0;
13376
13377  /* Sanity check.  */
13378  my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13379  my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13380
13381  /* Assume, until we see it does.  */
13382  current_function_returns_value = 0;
13383  current_function_returns_null = 0;
13384  named_labels = 0;
13385  shadowed_labels = 0;
13386  current_function_assigns_this = 0;
13387  current_function_just_assigned_this = 0;
13388  current_function_parms_stored = 0;
13389  original_result_rtx = NULL_RTX;
13390  base_init_expr = NULL_TREE;
13391  current_base_init_list = NULL_TREE;
13392  current_member_init_list = NULL_TREE;
13393  ctor_label = dtor_label = NULL_TREE;
13394  static_labelno = 0;
13395
13396  clear_temp_name ();
13397
13398  /* This should only be done once on the top most decl.  */
13399  if (have_extern_spec && !used_extern_spec)
13400    {
13401      declspecs = decl_tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13402      used_extern_spec = 1;
13403    }
13404
13405  if (pre_parsed_p)
13406    {
13407      decl1 = declarator;
13408
13409#if 0
13410      /* What was this testing for, exactly?  */
13411      if (! DECL_ARGUMENTS (decl1)
13412	  && !DECL_STATIC_FUNCTION_P (decl1)
13413	  && !DECL_ARTIFICIAL (decl1)
13414	  && DECL_CLASS_SCOPE_P (decl1)
13415	  && TYPE_IDENTIFIER (DECL_CONTEXT (decl1))
13416	  && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (DECL_CONTEXT (decl1))))
13417	{
13418	  tree binding = binding_for_name (DECL_NAME (decl1),
13419					   current_namespace);
13420	  cp_error ("redeclaration of `%#D'", decl1);
13421	  if (IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)))
13422	    cp_error_at ("previous declaration here", IDENTIFIER_CLASS_VALUE (DECL_NAME (decl1)));
13423	  else if (BINDING_VALUE (binding))
13424	    cp_error_at ("previous declaration here", BINDING_VALUE (binding));
13425	}
13426#endif
13427
13428      fntype = TREE_TYPE (decl1);
13429      if (TREE_CODE (fntype) == METHOD_TYPE)
13430	ctype = TYPE_METHOD_BASETYPE (fntype);
13431
13432      /* ANSI C++ June 5 1992 WP 11.4.5.  A friend function defined in a
13433	 class is in the (lexical) scope of the class in which it is
13434	 defined.  */
13435      if (!ctype && DECL_FRIEND_P (decl1))
13436	{
13437	  ctype = DECL_CLASS_CONTEXT (decl1);
13438
13439	  /* CTYPE could be null here if we're dealing with a template;
13440	     for example, `inline friend float foo()' inside a template
13441	     will have no CTYPE set.  */
13442	  if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13443	    ctype = NULL_TREE;
13444	  else
13445	    doing_friend = 1;
13446	}
13447
13448      last_function_parms = DECL_ARGUMENTS (decl1);
13449      last_function_parm_tags = NULL_TREE;
13450    }
13451  else
13452    {
13453      decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL_TREE);
13454      /* If the declarator is not suitable for a function definition,
13455	 cause a syntax error.  */
13456      if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL) return 0;
13457
13458      fntype = TREE_TYPE (decl1);
13459
13460      restype = TREE_TYPE (fntype);
13461      if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13462	{
13463	  cp_error ("semicolon missing after declaration of `%#T'", restype);
13464	  shadow_tag (build_expr_list (NULL_TREE, restype));
13465	  CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13466	  if (TREE_CODE (fntype) == FUNCTION_TYPE)
13467	    fntype = build_function_type (integer_type_node,
13468					  TYPE_ARG_TYPES (fntype));
13469	  else
13470	    fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13471					      integer_type_node,
13472					      TYPE_ARG_TYPES (fntype));
13473	  TREE_TYPE (decl1) = fntype;
13474	}
13475
13476      if (TREE_CODE (fntype) == METHOD_TYPE)
13477	ctype = TYPE_METHOD_BASETYPE (fntype);
13478      else if (DECL_MAIN_P (decl1))
13479	{
13480	  /* If this doesn't return integer_type, complain.  */
13481	  if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13482	    {
13483	      if (pedantic || warn_return_type)
13484		pedwarn ("return type for `main' changed to `int'");
13485	      TREE_TYPE (decl1) = fntype = default_function_type;
13486	    }
13487	}
13488    }
13489
13490  /* Warn if function was previously implicitly declared
13491     (but not if we warned then).  */
13492  if (! warn_implicit
13493      && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13494    cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13495
13496  announce_function (decl1);
13497
13498  /* Set up current_class_type, and enter the scope of the class, if
13499     appropriate.  */
13500  if (ctype)
13501    push_nested_class (ctype, 1);
13502  else if (DECL_STATIC_FUNCTION_P (decl1))
13503    push_nested_class (DECL_CONTEXT (decl1), 2);
13504
13505  /* Now that we have entered the scope of the class, we must restore
13506     the bindings for any template parameters surrounding DECL1, if it
13507     is an inline member template.  (Order is important; consider the
13508     case where a template parameter has the same name as a field of
13509     the class.)  It is not until after this point that
13510     PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
13511  if (pre_parsed_p == 2)
13512    maybe_begin_member_template_processing (decl1);
13513
13514  /* We are now in the scope of the function being defined.  */
13515  current_function_decl = decl1;
13516
13517  /* Save the parm names or decls from this function's declarator
13518     where store_parm_decls will find them.  */
13519  current_function_parms = last_function_parms;
13520  current_function_parm_tags = last_function_parm_tags;
13521
13522  if (! processing_template_decl)
13523    {
13524      /* In a function definition, arg types must be complete.  */
13525      require_complete_types_for_parms (current_function_parms);
13526
13527      if (TYPE_SIZE (complete_type (TREE_TYPE (fntype))) == NULL_TREE)
13528	{
13529	  cp_error ("return-type `%#T' is an incomplete type",
13530		    TREE_TYPE (fntype));
13531
13532	  /* Make it return void instead, but don't change the
13533	     type of the DECL_RESULT, in case we have a named return value.  */
13534	  if (ctype)
13535	    TREE_TYPE (decl1)
13536	      = build_cplus_method_type (build_type_variant (ctype,
13537							     TREE_READONLY (decl1),
13538							     TREE_SIDE_EFFECTS (decl1)),
13539					 void_type_node,
13540					 FUNCTION_ARG_CHAIN (decl1));
13541	  else
13542	    TREE_TYPE (decl1)
13543	      = build_function_type (void_type_node,
13544				     TYPE_ARG_TYPES (TREE_TYPE (decl1)));
13545	  DECL_RESULT (decl1)
13546	    = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (TREE_TYPE (fntype)));
13547	  TREE_READONLY (DECL_RESULT (decl1))
13548	    = CP_TYPE_CONST_P (TREE_TYPE (fntype));
13549	  TREE_THIS_VOLATILE (DECL_RESULT (decl1))
13550	    = CP_TYPE_VOLATILE_P (TREE_TYPE (fntype));
13551	}
13552
13553      if (TYPE_LANG_SPECIFIC (TREE_TYPE (fntype))
13554	  && CLASSTYPE_ABSTRACT_VIRTUALS (TREE_TYPE (fntype)))
13555	abstract_virtuals_error (decl1, TREE_TYPE (fntype));
13556    }
13557
13558  /* Effective C++ rule 15.  See also c_expand_return.  */
13559  if (warn_ecpp
13560      && DECL_NAME (decl1) == ansi_opname[(int) MODIFY_EXPR]
13561      && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13562    cp_warning ("`operator=' should return a reference to `*this'");
13563
13564  /* Make the init_value nonzero so pushdecl knows this is not tentative.
13565     error_mark_node is replaced below (in poplevel) with the BLOCK.  */
13566  DECL_INITIAL (decl1) = error_mark_node;
13567
13568#ifdef SET_DEFAULT_DECL_ATTRIBUTES
13569  SET_DEFAULT_DECL_ATTRIBUTES (decl1, attrs);
13570#endif
13571
13572  /* This function exists in static storage.
13573     (This does not mean `static' in the C sense!)  */
13574  TREE_STATIC (decl1) = 1;
13575
13576  /* We must call push_template_decl after current_class_type is set
13577     up.  (If we are processing inline definitions after exiting a
13578     class scope, current_class_type will be NULL_TREE until set above
13579     by push_nested_class.)  */
13580  if (processing_template_decl)
13581    decl1 = push_template_decl (decl1);
13582
13583  /* Record the decl so that the function name is defined.
13584     If we already have a decl for this name, and it is a FUNCTION_DECL,
13585     use the old decl.  */
13586  if (!processing_template_decl && pre_parsed_p == 0)
13587    {
13588      /* A specialization is not used to guide overload resolution.  */
13589      if ((flag_guiding_decls
13590	   || !DECL_TEMPLATE_SPECIALIZATION (decl1))
13591	  && ! DECL_FUNCTION_MEMBER_P (decl1))
13592	decl1 = pushdecl (decl1);
13593      else
13594	{
13595	  /* We need to set the DECL_CONTEXT. */
13596	  if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13597	    DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13598	  /* And make sure we have enough default args.  */
13599	  check_default_args (decl1);
13600	}
13601      DECL_MAIN_VARIANT (decl1) = decl1;
13602      fntype = TREE_TYPE (decl1);
13603    }
13604
13605  current_function_decl = decl1;
13606
13607  if (DECL_INTERFACE_KNOWN (decl1))
13608    {
13609      tree ctx = hack_decl_function_context (decl1);
13610
13611      if (DECL_NOT_REALLY_EXTERN (decl1))
13612	DECL_EXTERNAL (decl1) = 0;
13613
13614      if (ctx != NULL_TREE && DECL_THIS_INLINE (ctx)
13615	  && TREE_PUBLIC (ctx))
13616	/* This is a function in a local class in an extern inline
13617	   function.  */
13618	comdat_linkage (decl1);
13619    }
13620  /* If this function belongs to an interface, it is public.
13621     If it belongs to someone else's interface, it is also external.
13622     This only affects inlines and template instantiations.  */
13623  else if (interface_unknown == 0
13624	   && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13625	       || flag_alt_external_templates))
13626    {
13627      if (DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1)
13628	  || processing_template_decl)
13629	{
13630	  DECL_EXTERNAL (decl1)
13631	    = (interface_only
13632	       || (DECL_THIS_INLINE (decl1) && ! flag_implement_inlines
13633		   && !DECL_VINDEX (decl1)));
13634
13635	  /* For WIN32 we also want to put these in linkonce sections.  */
13636	  maybe_make_one_only (decl1);
13637	}
13638      else
13639	DECL_EXTERNAL (decl1) = 0;
13640      DECL_NOT_REALLY_EXTERN (decl1) = 0;
13641      DECL_INTERFACE_KNOWN (decl1) = 1;
13642    }
13643  else if (interface_unknown && interface_only
13644	   && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13645	       || flag_alt_external_templates))
13646    {
13647      /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13648	 interface, we will have interface_only set but not
13649	 interface_known.  In that case, we don't want to use the normal
13650	 heuristics because someone will supply a #pragma implementation
13651	 elsewhere, and deducing it here would produce a conflict.  */
13652      comdat_linkage (decl1);
13653      DECL_EXTERNAL (decl1) = 0;
13654      DECL_INTERFACE_KNOWN (decl1) = 1;
13655      DECL_DEFER_OUTPUT (decl1) = 1;
13656    }
13657  else
13658    {
13659      /* This is a definition, not a reference.
13660	 So clear DECL_EXTERNAL.  */
13661      DECL_EXTERNAL (decl1) = 0;
13662
13663      if ((DECL_THIS_INLINE (decl1) || DECL_TEMPLATE_INSTANTIATION (decl1))
13664	  && ! DECL_INTERFACE_KNOWN (decl1)
13665	  /* Don't try to defer nested functions for now.  */
13666	  && ! hack_decl_function_context (decl1))
13667	DECL_DEFER_OUTPUT (decl1) = 1;
13668      else
13669	DECL_INTERFACE_KNOWN (decl1) = 1;
13670    }
13671
13672  if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1))
13673    {
13674      if (TREE_CODE (fntype) == METHOD_TYPE)
13675	TREE_TYPE (decl1) = fntype
13676	  = build_function_type (TREE_TYPE (fntype),
13677				 TREE_CHAIN (TYPE_ARG_TYPES (fntype)));
13678      current_function_parms = TREE_CHAIN (current_function_parms);
13679      DECL_ARGUMENTS (decl1) = current_function_parms;
13680      ctype = NULL_TREE;
13681    }
13682  restype = TREE_TYPE (fntype);
13683
13684  if (ctype)
13685    {
13686      /* If we're compiling a friend function, neither of the variables
13687	 current_class_ptr nor current_class_type will have values.  */
13688      if (! doing_friend)
13689	{
13690	  /* We know that this was set up by `grokclassfn'.
13691	     We do not wait until `store_parm_decls', since evil
13692	     parse errors may never get us to that point.  Here
13693	     we keep the consistency between `current_class_type'
13694	     and `current_class_ptr'.  */
13695	  tree t = current_function_parms;
13696
13697	  my_friendly_assert (t != NULL_TREE
13698			      && TREE_CODE (t) == PARM_DECL, 162);
13699
13700	  if (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE)
13701	    {
13702	      int i;
13703
13704	      if (! hack_decl_function_context (decl1))
13705		temporary_allocation ();
13706	      i = suspend_momentary ();
13707
13708	      /* Normally, build_indirect_ref returns
13709		 current_class_ref whenever current_class_ptr is
13710		 dereferenced.  This time, however, we want it to
13711		 *create* current_class_ref, so we temporarily clear
13712		 current_class_ptr to fool it.  */
13713	      current_class_ptr = NULL_TREE;
13714	      current_class_ref = build_indirect_ref (t, NULL_PTR);
13715	      current_class_ptr = t;
13716
13717	      resume_momentary (i);
13718	      if (! hack_decl_function_context (decl1))
13719		end_temporary_allocation ();
13720	    }
13721	  else
13722	    /* We're having a signature pointer here.  */
13723	    current_class_ref = current_class_ptr = t;
13724
13725	}
13726    }
13727  else
13728    current_class_ptr = current_class_ref = NULL_TREE;
13729
13730  pushlevel (0);
13731  current_binding_level->parm_flag = 1;
13732
13733  GNU_xref_function (decl1, current_function_parms);
13734
13735  if (attrs)
13736    cplus_decl_attributes (decl1, NULL_TREE, attrs);
13737
13738  make_function_rtl (decl1);
13739
13740  /* Promote the value to int before returning it.  */
13741  if (C_PROMOTING_INTEGER_TYPE_P (restype))
13742    restype = type_promotes_to (restype);
13743
13744  /* If this fcn was already referenced via a block-scope `extern' decl
13745     (or an implicit decl), propagate certain information about the usage.  */
13746  if (TREE_ADDRESSABLE (DECL_ASSEMBLER_NAME (decl1)))
13747    TREE_ADDRESSABLE (decl1) = 1;
13748
13749  if (DECL_RESULT (decl1) == NULL_TREE)
13750    {
13751      DECL_RESULT (decl1)
13752	= build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13753      TREE_READONLY (DECL_RESULT (decl1)) = CP_TYPE_CONST_P (restype);
13754      TREE_THIS_VOLATILE (DECL_RESULT (decl1)) = CP_TYPE_VOLATILE_P (restype);
13755    }
13756
13757  /* Allocate further tree nodes temporarily during compilation
13758     of this function only.  Tiemann moved up here from bottom of fn.  */
13759  /* If this is a nested function, then we must continue to allocate RTL
13760     on the permanent obstack in case we need to inline it later.  */
13761  if (! hack_decl_function_context (decl1))
13762    temporary_allocation ();
13763
13764  if (processing_template_decl)
13765    {
13766      ++minimal_parse_mode;
13767      last_tree = DECL_SAVED_TREE (decl1)
13768	= build_nt (EXPR_STMT, void_zero_node);
13769    }
13770
13771  ++function_depth;
13772
13773  if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl1))
13774      && DECL_LANGUAGE (decl1) == lang_cplusplus)
13775    {
13776      dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13777      ctor_label = NULL_TREE;
13778    }
13779  else
13780    {
13781      dtor_label = NULL_TREE;
13782      if (DECL_CONSTRUCTOR_P (decl1))
13783	ctor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13784    }
13785
13786  return 1;
13787}
13788
13789/* Called after store_parm_decls for a function-try-block.  We need to update
13790   last_parm_cleanup_insn so that the base initializers for a constructor
13791   are run within this block, not before it.  */
13792
13793void
13794expand_start_early_try_stmts ()
13795{
13796  expand_start_try_stmts ();
13797  last_parm_cleanup_insn = get_last_insn ();
13798}
13799
13800/* Store the parameter declarations into the current function declaration.
13801   This is called after parsing the parameter declarations, before
13802   digesting the body of the function.
13803
13804   Also install to binding contour return value identifier, if any.  */
13805
13806void
13807store_parm_decls ()
13808{
13809  register tree fndecl = current_function_decl;
13810  register tree parm;
13811  int parms_have_cleanups = 0;
13812  tree cleanups = NULL_TREE;
13813
13814  /* This is either a chain of PARM_DECLs (when a prototype is used).  */
13815  tree specparms = current_function_parms;
13816
13817  /* This is a list of types declared among parms in a prototype.  */
13818  tree parmtags = current_function_parm_tags;
13819
13820  /* This is a chain of any other decls that came in among the parm
13821     declarations.  If a parm is declared with  enum {foo, bar} x;
13822     then CONST_DECLs for foo and bar are put here.  */
13823  tree nonparms = NULL_TREE;
13824
13825  if (toplevel_bindings_p ())
13826    fatal ("parse errors have confused me too much");
13827
13828  /* Initialize RTL machinery.  */
13829  init_function_start (fndecl, input_filename, lineno);
13830
13831  /* Create a binding level for the parms.  */
13832  expand_start_bindings (0);
13833
13834  if (specparms != NULL_TREE)
13835    {
13836      /* This case is when the function was defined with an ANSI prototype.
13837	 The parms already have decls, so we need not do anything here
13838	 except record them as in effect
13839	 and complain if any redundant old-style parm decls were written.  */
13840
13841      register tree next;
13842
13843      /* Must clear this because it might contain TYPE_DECLs declared
13844	 at class level.  */
13845      storedecls (NULL_TREE);
13846
13847      for (parm = nreverse (specparms); parm; parm = next)
13848	{
13849	  next = TREE_CHAIN (parm);
13850	  if (TREE_CODE (parm) == PARM_DECL)
13851	    {
13852	      tree cleanup;
13853	      if (DECL_NAME (parm) == NULL_TREE)
13854		{
13855		  pushdecl (parm);
13856		}
13857	      else if (TREE_CODE (TREE_TYPE (parm)) == VOID_TYPE)
13858		cp_error ("parameter `%D' declared void", parm);
13859	      else
13860		{
13861		  /* Now fill in DECL_REFERENCE_SLOT for any of the parm decls.
13862		     A parameter is assumed not to have any side effects.
13863		     If this should change for any reason, then this
13864		     will have to wrap the bashed reference type in a save_expr.
13865
13866		     Also, if the parameter type is declared to be an X
13867		     and there is an X(X&) constructor, we cannot lay it
13868		     into the stack (any more), so we make this parameter
13869		     look like it is really of reference type.  Functions
13870		     which pass parameters to this function will know to
13871		     create a temporary in their frame, and pass a reference
13872		     to that.  */
13873
13874		  if (TREE_CODE (TREE_TYPE (parm)) == REFERENCE_TYPE
13875		      && TYPE_SIZE (TREE_TYPE (TREE_TYPE (parm))))
13876		    SET_DECL_REFERENCE_SLOT (parm, convert_from_reference (parm));
13877
13878		  pushdecl (parm);
13879		}
13880	      if (! processing_template_decl
13881		  && (cleanup = maybe_build_cleanup (parm), cleanup))
13882		{
13883		  expand_decl (parm);
13884		  parms_have_cleanups = 1;
13885
13886		  /* Keep track of the cleanups.  */
13887		  cleanups = tree_cons (parm, cleanup, cleanups);
13888		}
13889	    }
13890	  else
13891	    {
13892	      /* If we find an enum constant or a type tag,
13893		 put it aside for the moment.  */
13894	      TREE_CHAIN (parm) = NULL_TREE;
13895	      nonparms = chainon (nonparms, parm);
13896	    }
13897	}
13898
13899      /* Get the decls in their original chain order
13900	 and record in the function.  This is all and only the
13901	 PARM_DECLs that were pushed into scope by the loop above.  */
13902      DECL_ARGUMENTS (fndecl) = getdecls ();
13903
13904      storetags (chainon (parmtags, gettags ()));
13905    }
13906  else
13907    DECL_ARGUMENTS (fndecl) = NULL_TREE;
13908
13909  /* Now store the final chain of decls for the arguments
13910     as the decl-chain of the current lexical scope.
13911     Put the enumerators in as well, at the front so that
13912     DECL_ARGUMENTS is not modified.  */
13913
13914  storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13915
13916  /* Declare __FUNCTION__ and __PRETTY_FUNCTION__ for this function.  */
13917  declare_function_name ();
13918
13919  /* Initialize the RTL code for the function.  */
13920  DECL_SAVED_INSNS (fndecl) = NULL_RTX;
13921  if (! processing_template_decl)
13922    expand_function_start (fndecl, parms_have_cleanups);
13923
13924  current_function_parms_stored = 1;
13925
13926  /* If this function is `main', emit a call to `__main'
13927     to run global initializers, etc.  */
13928  if (DECL_MAIN_P (fndecl))
13929    expand_main_function ();
13930
13931  /* Now that we have initialized the parms, we can start their
13932     cleanups.  We cannot do this before, since expand_decl_cleanup
13933     should not be called before the parm can be used.  */
13934  if (cleanups
13935      && ! processing_template_decl)
13936    {
13937      for (cleanups = nreverse (cleanups); cleanups; cleanups = TREE_CHAIN (cleanups))
13938	{
13939	  if (! expand_decl_cleanup (TREE_PURPOSE (cleanups), TREE_VALUE (cleanups)))
13940	    cp_error ("parser lost in parsing declaration of `%D'",
13941		      TREE_PURPOSE (cleanups));
13942	}
13943    }
13944
13945  /* Create a binding contour which can be used to catch
13946     cleanup-generated temporaries.  Also, if the return value needs or
13947     has initialization, deal with that now.  */
13948  if (parms_have_cleanups)
13949    {
13950      pushlevel (0);
13951      expand_start_bindings (0);
13952    }
13953
13954  if (! processing_template_decl && flag_exceptions)
13955    {
13956      /* Do the starting of the exception specifications, if we have any.  */
13957      if (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13958	expand_start_eh_spec ();
13959    }
13960
13961  last_parm_cleanup_insn = get_last_insn ();
13962  last_dtor_insn = get_last_insn ();
13963}
13964
13965/* Bind a name and initialization to the return value of
13966   the current function.  */
13967
13968void
13969store_return_init (return_id, init)
13970     tree return_id, init;
13971{
13972  tree decl = DECL_RESULT (current_function_decl);
13973
13974  if (pedantic)
13975    /* Give this error as many times as there are occurrences,
13976       so that users can use Emacs compilation buffers to find
13977       and fix all such places.  */
13978    pedwarn ("ANSI C++ does not permit named return values");
13979
13980  if (return_id != NULL_TREE)
13981    {
13982      if (DECL_NAME (decl) == NULL_TREE)
13983	{
13984	  DECL_NAME (decl) = return_id;
13985	  DECL_ASSEMBLER_NAME (decl) = return_id;
13986	}
13987      else
13988	cp_error ("return identifier `%D' already in place", decl);
13989    }
13990
13991  /* Can't let this happen for constructors.  */
13992  if (DECL_CONSTRUCTOR_P (current_function_decl))
13993    {
13994      error ("can't redefine default return value for constructors");
13995      return;
13996    }
13997
13998  /* If we have a named return value, put that in our scope as well.  */
13999  if (DECL_NAME (decl) != NULL_TREE)
14000    {
14001      /* If this named return value comes in a register,
14002	 put it in a pseudo-register.  */
14003      if (DECL_REGISTER (decl))
14004	{
14005	  original_result_rtx = DECL_RTL (decl);
14006	  DECL_RTL (decl) = gen_reg_rtx (DECL_MODE (decl));
14007	}
14008
14009      /* Let `cp_finish_decl' know that this initializer is ok.  */
14010      DECL_INITIAL (decl) = init;
14011      pushdecl (decl);
14012
14013      if (minimal_parse_mode)
14014	add_tree (build_min_nt (RETURN_INIT, return_id,
14015				copy_to_permanent (init)));
14016      else
14017	cp_finish_decl (decl, init, NULL_TREE, 0, 0);
14018    }
14019}
14020
14021
14022/* Emit implicit code for a destructor. This is a subroutine of
14023   finish_function.  */
14024
14025static void
14026finish_dtor ()
14027{
14028  tree binfo = TYPE_BINFO (current_class_type);
14029  tree cond = integer_one_node;
14030  tree exprstmt;
14031  tree in_charge_node = lookup_name (in_charge_identifier, 0);
14032  tree virtual_size;
14033  int ok_to_optimize_dtor = 0;
14034  int empty_dtor = get_last_insn () == last_dtor_insn;
14035  rtx insns, last_parm_insn;
14036
14037  if (current_function_assigns_this)
14038    cond = build (NE_EXPR, boolean_type_node,
14039		  current_class_ptr, integer_zero_node);
14040  else
14041    {
14042      int n_baseclasses = CLASSTYPE_N_BASECLASSES (current_class_type);
14043
14044      /* If this destructor is empty, then we don't need to check
14045	 whether `this' is NULL in some cases.  */
14046      if ((flag_this_is_variable & 1) == 0)
14047	ok_to_optimize_dtor = 1;
14048      else if (empty_dtor)
14049	ok_to_optimize_dtor
14050	  = (n_baseclasses == 0
14051	     || (n_baseclasses == 1
14052		 && TYPE_HAS_DESTRUCTOR (TYPE_BINFO_BASETYPE (current_class_type, 0))));
14053    }
14054
14055  /* If this has a vlist1 parameter, allocate the corresponding vlist
14056     parameter.  */
14057  if (DECL_DESTRUCTOR_FOR_PVBASE_P (current_function_decl))
14058    {
14059      /* _Vlist __vlist; */
14060      tree vlist;
14061
14062      mark_all_temps_used();
14063      vlist = pushdecl (build_decl (VAR_DECL, vlist_identifier,
14064					 vlist_type_node));
14065      TREE_USED (vlist) = 1;
14066      DECL_ARTIFICIAL (vlist) = 1;
14067      expand_decl (vlist);
14068      expand_decl_init (vlist);
14069    }
14070
14071  /* These initializations might go inline.  Protect
14072     the binding level of the parms.  */
14073  pushlevel (0);
14074  expand_start_bindings (0);
14075
14076  if (current_function_assigns_this)
14077    {
14078      current_function_assigns_this = 0;
14079      current_function_just_assigned_this = 0;
14080    }
14081
14082  /* Generate the code to call destructor on base class.
14083     If this destructor belongs to a class with virtual
14084     functions, then set the virtual function table
14085     pointer to represent the type of our base class.  */
14086
14087  /* This side-effect makes call to `build_delete' generate the
14088     code we have to have at the end of this destructor.
14089     `build_delete' will set the flag again.  */
14090  TYPE_HAS_DESTRUCTOR (current_class_type) = 0;
14091
14092  /* These are two cases where we cannot delegate deletion.  */
14093  if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)
14094      || TYPE_GETS_REG_DELETE (current_class_type))
14095    exprstmt = build_delete
14096      (current_class_type, current_class_ref, integer_zero_node,
14097       LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
14098  else
14099    exprstmt = build_delete
14100      (current_class_type, current_class_ref, in_charge_node,
14101       LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL, 0);
14102
14103  /* If we did not assign to this, then `this' is non-zero at
14104     the end of a destructor.  As a special optimization, don't
14105     emit test if this is an empty destructor.  If it does nothing,
14106     it does nothing.  If it calls a base destructor, the base
14107     destructor will perform the test.  */
14108
14109  if (exprstmt != error_mark_node
14110      && (TREE_CODE (exprstmt) != NOP_EXPR
14111	  || TREE_OPERAND (exprstmt, 0) != integer_zero_node
14112	  || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type)))
14113    {
14114      expand_label (dtor_label);
14115      if (cond != integer_one_node)
14116	expand_start_cond (cond, 0);
14117      if (exprstmt != void_zero_node)
14118	/* Don't call `expand_expr_stmt' if we're not going to do
14119	   anything, since -Wall will give a diagnostic.  */
14120	expand_expr_stmt (exprstmt);
14121
14122      /* Run destructor on all virtual baseclasses.  */
14123      if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
14124	{
14125	  tree vbases = nreverse
14126	    (copy_list (CLASSTYPE_VBASECLASSES (current_class_type)));
14127	  expand_start_cond (build (BIT_AND_EXPR, integer_type_node,
14128				    in_charge_node, integer_two_node), 0);
14129	  while (vbases)
14130	    {
14131	      if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (vbases)))
14132		{
14133		  tree vb = get_vbase
14134		    (BINFO_TYPE (vbases),
14135		     TYPE_BINFO (current_class_type));
14136
14137		  expand_expr_stmt
14138		    (build_base_dtor_call (current_class_ref,
14139					   vb, integer_zero_node));
14140		}
14141	      vbases = TREE_CHAIN (vbases);
14142	    }
14143	  expand_end_cond ();
14144	}
14145
14146      do_pending_stack_adjust ();
14147      if (cond != integer_one_node)
14148	expand_end_cond ();
14149    }
14150
14151  virtual_size = c_sizeof (current_class_type);
14152
14153  /* At the end, call delete if that's what's requested.  */
14154
14155  /* FDIS sez: At the point of definition of a virtual destructor
14156     (including an implicit definition), non-placement operator
14157     delete shall be looked up in the scope of the destructor's
14158     class and if found shall be accessible and unambiguous.
14159
14160     This is somewhat unclear, but I take it to mean that if the
14161     class only defines placement deletes we don't do anything here.
14162     So we pass LOOKUP_SPECULATIVELY; delete_sanity will complain
14163     for us if they ever try to delete one of these.  */
14164
14165  if (TYPE_GETS_REG_DELETE (current_class_type)
14166      || TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
14167    exprstmt = build_op_delete_call
14168      (DELETE_EXPR, current_class_ptr, virtual_size,
14169       LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
14170  else
14171    exprstmt = NULL_TREE;
14172
14173  if (exprstmt)
14174    {
14175      cond = build (BIT_AND_EXPR, integer_type_node,
14176		    in_charge_node, integer_one_node);
14177      expand_start_cond (cond, 0);
14178      expand_expr_stmt (exprstmt);
14179      expand_end_cond ();
14180    }
14181
14182  /* End of destructor.  */
14183  expand_end_bindings (NULL_TREE, getdecls () != NULL_TREE, 0);
14184  poplevel (getdecls () != NULL_TREE, 0, 0);
14185
14186  /* Back to the top of destructor.  */
14187  /* Don't execute destructor code if `this' is NULL.  */
14188
14189  start_sequence ();
14190
14191  /* If we need thunk-style vlists, initialize them if the caller did
14192     not pass them. This requires a new temporary. The generated code
14193     looks like
14194       if (!(__in_charge & 4))
14195         __vlist = __vl.<type> + sizeof(__vl.<type>);
14196       else
14197         __vlist = __vlist1;
14198  */
14199  if (TYPE_USES_PVBASES (current_class_type))
14200    {
14201      tree vlist = lookup_name (vlist_identifier, 0);
14202      tree vlist1 = lookup_name (get_identifier (VLIST1_NAME), 0);
14203      cond = build (BIT_AND_EXPR, integer_type_node,
14204		    in_charge_node, build_int_2 (4, 0));
14205      cond = build1 (TRUTH_NOT_EXPR, boolean_type_node, cond);
14206      expand_start_cond (cond, 0);
14207      init_vlist (current_class_type);
14208      expand_start_else ();
14209      expand_expr_stmt (build_modify_expr (vlist, NOP_EXPR, vlist1));
14210      expand_end_cond ();
14211    }
14212
14213  /* If the dtor is empty, and we know there is not possible way we
14214     could use any vtable entries, before they are possibly set by
14215     a base class dtor, we don't have to setup the vtables, as we
14216     know that any base class dtoring will set up any vtables it
14217     needs.  We avoid MI, because one base class dtor can do a
14218     virtual dispatch to an overridden function that would need to
14219     have a non-related vtable set up, we cannot avoid setting up
14220     vtables in that case.  We could change this to see if there is
14221     just one vtable.  */
14222  if (! empty_dtor || TYPE_USES_COMPLEX_INHERITANCE (current_class_type))
14223    {
14224      /* Make all virtual function table pointers in non-virtual base
14225	 classes point to CURRENT_CLASS_TYPE's virtual function
14226	 tables.  */
14227      expand_direct_vtbls_init (binfo, binfo, 1, 0, current_class_ptr);
14228
14229      if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
14230	expand_indirect_vtbls_init (binfo, current_class_ref, current_class_ptr);
14231    }
14232
14233  if (! ok_to_optimize_dtor)
14234    {
14235      cond = build_binary_op (NE_EXPR,
14236			      current_class_ptr, integer_zero_node);
14237      expand_start_cond (cond, 0);
14238    }
14239
14240  insns = get_insns ();
14241  end_sequence ();
14242
14243  last_parm_insn = get_first_nonparm_insn ();
14244  if (last_parm_insn == NULL_RTX)
14245    last_parm_insn = get_last_insn ();
14246  else
14247    last_parm_insn = previous_insn (last_parm_insn);
14248
14249  emit_insns_after (insns, last_parm_insn);
14250
14251  if (! ok_to_optimize_dtor)
14252    expand_end_cond ();
14253}
14254
14255/* Emit implicit code for a constructor. This is a subroutine of
14256   finish_function. CALL_POPLEVEL is the same variable in
14257   finish_function.  */
14258
14259static void
14260finish_ctor (call_poplevel)
14261     int call_poplevel;
14262{
14263  register tree fndecl = current_function_decl;
14264  tree cond = NULL_TREE, thenclause = NULL_TREE;
14265  rtx insns;
14266  tree decls;
14267
14268  /* Allow constructor for a type to get a new instance of the object
14269     using `build_new'.  */
14270  tree abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type);
14271  CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = NULL_TREE;
14272
14273  if (flag_this_is_variable > 0)
14274    {
14275      cond = build_binary_op (EQ_EXPR, current_class_ptr, integer_zero_node);
14276      thenclause =
14277	build_modify_expr (current_class_ptr, NOP_EXPR,
14278			   build_new (NULL_TREE, current_class_type,
14279				      void_type_node, 0));
14280    }
14281
14282  CLASSTYPE_ABSTRACT_VIRTUALS (current_class_type) = abstract_virtuals;
14283
14284  start_sequence ();
14285
14286  if (flag_this_is_variable > 0)
14287    {
14288      expand_start_cond (cond, 0);
14289      expand_expr_stmt (thenclause);
14290      expand_end_cond ();
14291    }
14292
14293  /* Emit insns from `emit_base_init' which sets up virtual
14294     function table pointer(s).  */
14295  if (base_init_expr)
14296    {
14297      expand_expr_stmt (base_init_expr);
14298      base_init_expr = NULL_TREE;
14299    }
14300
14301  insns = get_insns ();
14302  end_sequence ();
14303
14304  /* This is where the body of the constructor begins.  */
14305
14306  emit_insns_after (insns, last_parm_cleanup_insn);
14307
14308  end_protect_partials ();
14309
14310  /* This is where the body of the constructor ends.  */
14311  expand_label (ctor_label);
14312  ctor_label = NULL_TREE;
14313
14314  if (call_poplevel)
14315    {
14316      decls = getdecls ();
14317      expand_end_bindings (decls, decls != NULL_TREE, 0);
14318      poplevel (decls != NULL_TREE, 1, 0);
14319    }
14320
14321  /* c_expand_return knows to return 'this' from a constructor.  */
14322  c_expand_return (NULL_TREE);
14323
14324  current_function_assigns_this = 0;
14325  current_function_just_assigned_this = 0;
14326}
14327
14328
14329/* Finish up a function declaration and compile that function
14330   all the way to assembler language output.  The free the storage
14331   for the function definition.
14332
14333   This is called after parsing the body of the function definition.
14334   LINENO is the current line number.
14335
14336   FLAGS is a bitwise or of the following values:
14337     1 - CALL_POPLEVEL
14338       An extra call to poplevel (and expand_end_bindings) must be
14339       made to take care of the binding contour for the base
14340       initializers.  This is only relevant for constructors.
14341     2 - INCLASS_INLINE
14342       We just finished processing the body of an in-class inline
14343       function definition.  (This processing will have taken place
14344       after the class definition is complete.)
14345
14346   NESTED is nonzero if we were in the middle of compiling another function
14347   when we started on this one.  */
14348
14349void
14350finish_function (lineno, flags, nested)
14351     int lineno;
14352     int flags;
14353     int nested;
14354{
14355  register tree fndecl = current_function_decl;
14356  tree fntype, ctype = NULL_TREE;
14357  /* Label to use if this function is supposed to return a value.  */
14358  tree no_return_label = NULL_TREE;
14359  tree decls = NULL_TREE;
14360  int call_poplevel = (flags & 1) != 0;
14361  int inclass_inline = (flags & 2) != 0;
14362  int in_template;
14363
14364  /* When we get some parse errors, we can end up without a
14365     current_function_decl, so cope.  */
14366  if (fndecl == NULL_TREE)
14367    return;
14368
14369  if (function_depth > 1)
14370    nested = 1;
14371
14372  fntype = TREE_TYPE (fndecl);
14373
14374/*  TREE_READONLY (fndecl) = 1;
14375    This caused &foo to be of type ptr-to-const-function
14376    which then got a warning when stored in a ptr-to-function variable.  */
14377
14378  /* This happens on strange parse errors.  */
14379  if (! current_function_parms_stored)
14380    {
14381      call_poplevel = 0;
14382      store_parm_decls ();
14383    }
14384
14385  if (processing_template_decl)
14386    {
14387      if (DECL_CONSTRUCTOR_P (fndecl) && call_poplevel)
14388	{
14389	  decls = getdecls ();
14390	  expand_end_bindings (decls, decls != NULL_TREE, 0);
14391	  poplevel (decls != NULL_TREE, 0, 0);
14392	}
14393    }
14394  else
14395    {
14396      if (write_symbols != NO_DEBUG /*&& TREE_CODE (fntype) != METHOD_TYPE*/)
14397	{
14398	  tree ttype = target_type (fntype);
14399	  tree parmdecl;
14400
14401	  if (IS_AGGR_TYPE (ttype))
14402	    /* Let debugger know it should output info for this type.  */
14403	    note_debug_info_needed (ttype);
14404
14405	  for (parmdecl = DECL_ARGUMENTS (fndecl); parmdecl; parmdecl = TREE_CHAIN (parmdecl))
14406	    {
14407	      ttype = target_type (TREE_TYPE (parmdecl));
14408	      if (IS_AGGR_TYPE (ttype))
14409		/* Let debugger know it should output info for this type.  */
14410		note_debug_info_needed (ttype);
14411	    }
14412	}
14413
14414      /* Clean house because we will need to reorder insns here.  */
14415      do_pending_stack_adjust ();
14416
14417      if (dtor_label)
14418	finish_dtor ();
14419      else if (current_function_assigns_this)
14420	{
14421	  /* Does not need to call emit_base_init, because
14422	     that is done (if needed) just after assignment to this
14423	     is seen.  */
14424
14425	  if (DECL_CONSTRUCTOR_P (current_function_decl))
14426	    {
14427	      end_protect_partials ();
14428	      expand_label (ctor_label);
14429	      ctor_label = NULL_TREE;
14430
14431	      if (call_poplevel)
14432		{
14433		  decls = getdecls ();
14434		  expand_end_bindings (decls, decls != NULL_TREE, 0);
14435		  poplevel (decls != NULL_TREE, 0, 0);
14436		}
14437	      /* c_expand_return knows to return 'this' from a constructor.  */
14438	      c_expand_return (NULL_TREE);
14439	    }
14440	  else if (TREE_CODE (TREE_TYPE (DECL_RESULT (current_function_decl))) != VOID_TYPE
14441		   && return_label != NULL_RTX)
14442	    no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
14443
14444	  current_function_assigns_this = 0;
14445	  current_function_just_assigned_this = 0;
14446	  base_init_expr = NULL_TREE;
14447	}
14448      else if (DECL_CONSTRUCTOR_P (fndecl)
14449	       && !DECL_VLIST_CTOR_WRAPPER_P (fndecl))
14450	finish_ctor (call_poplevel);
14451      else if (DECL_MAIN_P (fndecl))
14452	{
14453	  /* Make it so that `main' always returns 0 by default.  */
14454#ifdef VMS
14455	  c_expand_return (integer_one_node);
14456#else
14457	  c_expand_return (integer_zero_node);
14458#endif
14459	}
14460      else if (return_label != NULL_RTX
14461	       && current_function_return_value == NULL_TREE
14462	       && ! DECL_NAME (DECL_RESULT (current_function_decl)))
14463	no_return_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
14464
14465      if (flag_exceptions)
14466	expand_exception_blocks ();
14467
14468      /* If this function is supposed to return a value, ensure that
14469	 we do not fall into the cleanups by mistake.  The end of our
14470	 function will look like this:
14471
14472	 user code (may have return stmt somewhere)
14473	 goto no_return_label
14474	 cleanup_label:
14475	 cleanups
14476	 goto return_label
14477	 no_return_label:
14478	 NOTE_INSN_FUNCTION_END
14479	 return_label:
14480	 things for return
14481
14482	 If the user omits a return stmt in the USER CODE section, we
14483	 will have a control path which reaches NOTE_INSN_FUNCTION_END.
14484	 Otherwise, we won't.  */
14485      if (no_return_label)
14486	{
14487	  DECL_CONTEXT (no_return_label) = fndecl;
14488	  DECL_INITIAL (no_return_label) = error_mark_node;
14489	  DECL_SOURCE_FILE (no_return_label) = input_filename;
14490	  DECL_SOURCE_LINE (no_return_label) = lineno;
14491	  expand_goto (no_return_label);
14492	}
14493
14494      if (cleanup_label)
14495	{
14496	  /* Remove the binding contour which is used
14497	     to catch cleanup-generated temporaries.  */
14498	  expand_end_bindings (0, 0, 0);
14499	  poplevel (0, 0, 0);
14500
14501	  /* Emit label at beginning of cleanup code for parameters.  */
14502	  emit_label (cleanup_label);
14503	}
14504
14505      /* Get return value into register if that's where it's supposed to be.  */
14506      if (original_result_rtx)
14507	fixup_result_decl (DECL_RESULT (fndecl), original_result_rtx);
14508
14509      /* Finish building code that will trigger warnings if users forget
14510	 to make their functions return values.  */
14511      if (no_return_label || cleanup_label)
14512	emit_jump (return_label);
14513      if (no_return_label)
14514	{
14515	  /* We don't need to call `expand_*_return' here because we
14516	     don't need any cleanups here--this path of code is only
14517	     for error checking purposes.  */
14518	  expand_label (no_return_label);
14519	}
14520
14521      /* Generate rtl for function exit.  */
14522      expand_function_end (input_filename, lineno, 1);
14523    }
14524
14525  /* If we're processing a template, squirrel away the definition
14526     until we do an instantiation.  */
14527  if (processing_template_decl)
14528    {
14529      --minimal_parse_mode;
14530      DECL_SAVED_TREE (fndecl) = TREE_CHAIN (DECL_SAVED_TREE (fndecl));
14531      /* We have to save this value here in case
14532	 maybe_end_member_template_processing decides to pop all the
14533	 template parameters.  */
14534      in_template = 1;
14535    }
14536  else
14537    in_template = 0;
14538
14539  /* This must come after expand_function_end because cleanups might
14540     have declarations (from inline functions) that need to go into
14541     this function's blocks.  */
14542  if (current_binding_level->parm_flag != 1)
14543    my_friendly_abort (122);
14544  poplevel (1, 0, 1);
14545
14546  /* If this is a in-class inline definition, we may have to pop the
14547     bindings for the template parameters that we added in
14548     maybe_begin_member_template_processing when start_function was
14549     called.  */
14550  if (inclass_inline)
14551    maybe_end_member_template_processing ();
14552
14553  /* Reset scope for C++: if we were in the scope of a class,
14554     then when we finish this function, we are not longer so.
14555     This cannot be done until we know for sure that no more
14556     class members will ever be referenced in this function
14557     (i.e., calls to destructors).  */
14558  if (current_class_name)
14559    {
14560      ctype = current_class_type;
14561      pop_nested_class ();
14562    }
14563
14564  /* Must mark the RESULT_DECL as being in this function.  */
14565  DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14566
14567  /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14568     to the FUNCTION_DECL node itself.  */
14569  BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14570
14571  if (!in_template)
14572    {
14573      int saved_flag_keep_inline_functions =
14574	flag_keep_inline_functions;
14575
14576      /* So we can tell if jump_optimize sets it to 1.  */
14577      can_reach_end = 0;
14578
14579      if (DECL_CONTEXT (fndecl) != NULL_TREE
14580	  && hack_decl_function_context (fndecl))
14581	/* Trick rest_of_compilation into not deferring output of this
14582	   function, even if it is inline, since the rtl_obstack for
14583	   this function is the function_obstack of the enclosing
14584	   function and will be deallocated when the enclosing
14585	   function is gone.  See save_tree_status.  */
14586	flag_keep_inline_functions = 1;
14587
14588      /* Run the optimizers and output the assembler code for this
14589         function.  */
14590
14591      if (DECL_ARTIFICIAL (fndecl))
14592	{
14593	  /* Do we really *want* to inline this synthesized method?  */
14594
14595	  int save_fif = flag_inline_functions;
14596	  flag_inline_functions = 1;
14597
14598	  /* Turn off DECL_INLINE for the moment so function_cannot_inline_p
14599	     will check our size.  */
14600	  DECL_INLINE (fndecl) = 0;
14601
14602	  rest_of_compilation (fndecl);
14603	  flag_inline_functions = save_fif;
14604	}
14605      else
14606	rest_of_compilation (fndecl);
14607
14608      flag_keep_inline_functions = saved_flag_keep_inline_functions;
14609
14610      if (DECL_SAVED_INSNS (fndecl) && ! TREE_ASM_WRITTEN (fndecl))
14611	{
14612	  /* Set DECL_EXTERNAL so that assemble_external will be called as
14613	     necessary.  We'll clear it again in finish_file.  */
14614	  if (! DECL_EXTERNAL (fndecl))
14615	    DECL_NOT_REALLY_EXTERN (fndecl) = 1;
14616	  DECL_EXTERNAL (fndecl) = 1;
14617	  mark_inline_for_output (fndecl);
14618	}
14619
14620      if (ctype && TREE_ASM_WRITTEN (fndecl))
14621	note_debug_info_needed (ctype);
14622
14623      current_function_returns_null |= can_reach_end;
14624
14625      /* Since we don't normally go through c_expand_return for constructors,
14626	 this normally gets the wrong value.
14627	 Also, named return values have their return codes emitted after
14628	 NOTE_INSN_FUNCTION_END, confusing jump.c.  */
14629      if (DECL_CONSTRUCTOR_P (fndecl)
14630	  || DECL_NAME (DECL_RESULT (fndecl)) != NULL_TREE)
14631	current_function_returns_null = 0;
14632
14633      if (TREE_THIS_VOLATILE (fndecl) && current_function_returns_null)
14634	cp_warning ("`noreturn' function `%D' does return", fndecl);
14635      else if ((warn_return_type || pedantic)
14636	       && current_function_returns_null
14637	       && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE)
14638	{
14639	  /* If this function returns non-void and control can drop through,
14640	     complain.  */
14641	  cp_warning ("control reaches end of non-void function `%D'", fndecl);
14642	}
14643      /* With just -W, complain only if function returns both with
14644	 and without a value.  */
14645      else if (extra_warnings
14646	       && current_function_returns_value && current_function_returns_null)
14647	warning ("this function may return with or without a value");
14648    }
14649
14650  --function_depth;
14651
14652  /* Free all the tree nodes making up this function.  */
14653  /* Switch back to allocating nodes permanently
14654     until we start another function.  */
14655  if (! nested)
14656    permanent_allocation (1);
14657
14658  if (DECL_SAVED_INSNS (fndecl) == NULL_RTX)
14659    {
14660      tree t;
14661
14662      /* Stop pointing to the local nodes about to be freed.  */
14663      /* But DECL_INITIAL must remain nonzero so we know this
14664	 was an actual function definition.  */
14665      DECL_INITIAL (fndecl) = error_mark_node;
14666      for (t = DECL_ARGUMENTS (fndecl); t; t = TREE_CHAIN (t))
14667	DECL_RTL (t) = DECL_INCOMING_RTL (t) = NULL_RTX;
14668    }
14669
14670  if (DECL_STATIC_CONSTRUCTOR (fndecl))
14671    static_ctors = perm_tree_cons (NULL_TREE, fndecl, static_ctors);
14672  if (DECL_STATIC_DESTRUCTOR (fndecl))
14673    static_dtors = perm_tree_cons (NULL_TREE, fndecl, static_dtors);
14674
14675  if (! nested)
14676    {
14677      /* Let the error reporting routines know that we're outside a
14678         function.  For a nested function, this value is used in
14679         pop_cp_function_context and then reset via pop_function_context.  */
14680      current_function_decl = NULL_TREE;
14681    }
14682
14683  named_label_uses = NULL;
14684  current_class_ptr = NULL_TREE;
14685  current_class_ref = NULL_TREE;
14686}
14687
14688/* Create the FUNCTION_DECL for a function definition.
14689   DECLSPECS and DECLARATOR are the parts of the declaration;
14690   they describe the return type and the name of the function,
14691   but twisted together in a fashion that parallels the syntax of C.
14692
14693   This function creates a binding context for the function body
14694   as well as setting up the FUNCTION_DECL in current_function_decl.
14695
14696   Returns a FUNCTION_DECL on success.
14697
14698   If the DECLARATOR is not suitable for a function (it defines a datum
14699   instead), we return 0, which tells yyparse to report a parse error.
14700
14701   May return void_type_node indicating that this method is actually
14702   a friend.  See grokfield for more details.
14703
14704   Came here with a `.pushlevel' .
14705
14706   DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14707   CHANGES TO CODE IN `grokfield'.  */
14708
14709tree
14710start_method (declspecs, declarator, attrlist)
14711     tree declarator, declspecs, attrlist;
14712{
14713  tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14714				attrlist);
14715
14716  /* Something too ugly to handle.  */
14717  if (fndecl == NULL_TREE)
14718    return NULL_TREE;
14719
14720  /* Pass friends other than inline friend functions back.  */
14721  if (fndecl == void_type_node)
14722    return fndecl;
14723
14724  if (TREE_CODE (fndecl) != FUNCTION_DECL)
14725    /* Not a function, tell parser to report parse error.  */
14726    return NULL_TREE;
14727
14728  if (IS_SIGNATURE (current_class_type))
14729    IS_DEFAULT_IMPLEMENTATION (fndecl) = 1;
14730
14731  if (DECL_IN_AGGR_P (fndecl))
14732    {
14733      if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14734	{
14735	  if (DECL_CONTEXT (fndecl)
14736	      && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14737	    cp_error ("`%D' is already defined in class %s", fndecl,
14738			     TYPE_NAME_STRING (DECL_CONTEXT (fndecl)));
14739	}
14740      return void_type_node;
14741    }
14742
14743  check_template_shadow (fndecl);
14744
14745  DECL_THIS_INLINE (fndecl) = 1;
14746
14747  if (flag_default_inline)
14748    DECL_INLINE (fndecl) = 1;
14749
14750  /* We process method specializations in finish_struct_1.  */
14751  if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14752    fndecl = push_template_decl (fndecl);
14753
14754  /* We read in the parameters on the maybepermanent_obstack,
14755     but we won't be getting back to them until after we
14756     may have clobbered them.  So the call to preserve_data
14757     will keep them safe.  */
14758  preserve_data ();
14759
14760  if (! DECL_FRIEND_P (fndecl))
14761    {
14762      if (TREE_CHAIN (fndecl))
14763	{
14764	  fndecl = copy_node (fndecl);
14765	  TREE_CHAIN (fndecl) = NULL_TREE;
14766	}
14767
14768      if (DECL_CONSTRUCTOR_P (fndecl))
14769	{
14770	  if (! grok_ctor_properties (current_class_type, fndecl))
14771	    return void_type_node;
14772	}
14773      else if (IDENTIFIER_OPNAME_P (DECL_NAME (fndecl)))
14774	grok_op_properties (fndecl, DECL_VIRTUAL_P (fndecl), 0);
14775    }
14776
14777  cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0, 0);
14778
14779  /* Make a place for the parms */
14780  pushlevel (0);
14781  current_binding_level->parm_flag = 1;
14782
14783  DECL_IN_AGGR_P (fndecl) = 1;
14784  return fndecl;
14785}
14786
14787/* Go through the motions of finishing a function definition.
14788   We don't compile this method until after the whole class has
14789   been processed.
14790
14791   FINISH_METHOD must return something that looks as though it
14792   came from GROKFIELD (since we are defining a method, after all).
14793
14794   This is called after parsing the body of the function definition.
14795   STMTS is the chain of statements that makes up the function body.
14796
14797   DECL is the ..._DECL that `start_method' provided.  */
14798
14799tree
14800finish_method (decl)
14801     tree decl;
14802{
14803  register tree fndecl = decl;
14804  tree old_initial;
14805
14806  register tree link;
14807
14808  if (decl == void_type_node)
14809    return decl;
14810
14811  old_initial = DECL_INITIAL (fndecl);
14812
14813  /* Undo the level for the parms (from start_method).
14814     This is like poplevel, but it causes nothing to be
14815     saved.  Saving information here confuses symbol-table
14816     output routines.  Besides, this information will
14817     be correctly output when this method is actually
14818     compiled.  */
14819
14820  /* Clear out the meanings of the local variables of this level;
14821     also record in each decl which block it belongs to.  */
14822
14823  for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14824    {
14825      if (DECL_NAME (link) != NULL_TREE)
14826	pop_binding (DECL_NAME (link), link);
14827      my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14828      DECL_CONTEXT (link) = NULL_TREE;
14829    }
14830
14831  GNU_xref_end_scope ((HOST_WIDE_INT) current_binding_level,
14832		      (HOST_WIDE_INT) current_binding_level->level_chain,
14833		      current_binding_level->parm_flag,
14834		      current_binding_level->keep);
14835
14836  poplevel (0, 0, 0);
14837
14838  DECL_INITIAL (fndecl) = old_initial;
14839
14840  /* We used to check if the context of FNDECL was different from
14841     current_class_type as another way to get inside here.  This didn't work
14842     for String.cc in libg++.  */
14843  if (DECL_FRIEND_P (fndecl))
14844    {
14845      CLASSTYPE_INLINE_FRIENDS (current_class_type)
14846	= tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14847      decl = void_type_node;
14848    }
14849
14850  return decl;
14851}
14852
14853/* Called when a new struct TYPE is defined.
14854   If this structure or union completes the type of any previous
14855   variable declaration, lay it out and output its rtl.  */
14856
14857void
14858hack_incomplete_structures (type)
14859     tree type;
14860{
14861  tree *list;
14862
14863  if (current_binding_level->incomplete == NULL_TREE)
14864    return;
14865
14866  if (!type) /* Don't do this for class templates.  */
14867    return;
14868
14869  for (list = &current_binding_level->incomplete; *list; )
14870    {
14871      tree decl = TREE_VALUE (*list);
14872      if ((decl && TREE_TYPE (decl) == type)
14873	  || (TREE_TYPE (decl)
14874	      && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14875	      && TREE_TYPE (TREE_TYPE (decl)) == type))
14876	{
14877	  int toplevel = toplevel_bindings_p ();
14878	  if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
14879	      && TREE_TYPE (TREE_TYPE (decl)) == type)
14880	    layout_type (TREE_TYPE (decl));
14881	  layout_decl (decl, 0);
14882	  rest_of_decl_compilation (decl, NULL_PTR, toplevel, 0);
14883	  if (! toplevel)
14884	    {
14885	      tree cleanup;
14886	      expand_decl (decl);
14887	      cleanup = maybe_build_cleanup (decl);
14888	      expand_decl_init (decl);
14889	      if (! expand_decl_cleanup (decl, cleanup))
14890		cp_error ("parser lost in parsing declaration of `%D'",
14891			  decl);
14892	    }
14893	  *list = TREE_CHAIN (*list);
14894	}
14895      else
14896	list = &TREE_CHAIN (*list);
14897    }
14898}
14899
14900/* If DECL is of a type which needs a cleanup, build that cleanup here.
14901   See build_delete for information about AUTO_DELETE.
14902
14903   Don't build these on the momentary obstack; they must live
14904   the life of the binding contour.  */
14905
14906static tree
14907maybe_build_cleanup_1 (decl, auto_delete)
14908     tree decl, auto_delete;
14909{
14910  tree type = TREE_TYPE (decl);
14911  if (type != error_mark_node && TYPE_NEEDS_DESTRUCTOR (type))
14912    {
14913      int temp = 0, flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14914      tree rval;
14915
14916      if (TREE_CODE (decl) != PARM_DECL)
14917	temp = suspend_momentary ();
14918
14919      if (TREE_CODE (type) == ARRAY_TYPE)
14920	rval = decl;
14921      else
14922	{
14923	  mark_addressable (decl);
14924	  rval = build_unary_op (ADDR_EXPR, decl, 0);
14925	}
14926
14927      /* Optimize for space over speed here.  */
14928      if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14929	  || flag_expensive_optimizations)
14930	flags |= LOOKUP_NONVIRTUAL;
14931
14932      rval = build_delete (TREE_TYPE (rval), rval, auto_delete, flags, 0);
14933
14934      if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14935	  && ! TYPE_HAS_DESTRUCTOR (type))
14936	rval = build_compound_expr (expr_tree_cons (NULL_TREE, rval,
14937					       build_expr_list (NULL_TREE, build_vbase_delete (type, decl))));
14938
14939      if (TREE_CODE (decl) != PARM_DECL)
14940	resume_momentary (temp);
14941
14942      return rval;
14943    }
14944  return 0;
14945}
14946
14947/* If DECL is of a type which needs a cleanup, build that cleanup
14948   here.  The cleanup does free the storage with a call to delete.  */
14949
14950tree
14951maybe_build_cleanup_and_delete (decl)
14952     tree decl;
14953{
14954  return maybe_build_cleanup_1 (decl, integer_three_node);
14955}
14956
14957/* If DECL is of a type which needs a cleanup, build that cleanup
14958   here.  The cleanup does not free the storage with a call a delete.  */
14959
14960tree
14961maybe_build_cleanup (decl)
14962     tree decl;
14963{
14964  return maybe_build_cleanup_1 (decl, integer_two_node);
14965}
14966
14967/* Expand a C++ expression at the statement level.
14968   This is needed to ferret out nodes which have UNKNOWN_TYPE.
14969   The C++ type checker should get all of these out when
14970   expressions are combined with other, type-providing, expressions,
14971   leaving only orphan expressions, such as:
14972
14973   &class::bar;		/ / takes its address, but does nothing with it.  */
14974
14975void
14976cplus_expand_expr_stmt (exp)
14977     tree exp;
14978{
14979  if (processing_template_decl)
14980    {
14981      add_tree (build_min_nt (EXPR_STMT, exp));
14982      return;
14983    }
14984
14985  /* Arrange for all temps to disappear.  */
14986  expand_start_target_temps ();
14987
14988  exp = require_complete_type_in_void (exp);
14989
14990  if (TREE_CODE (exp) == FUNCTION_DECL)
14991    {
14992      cp_warning ("reference, not call, to function `%D'", exp);
14993      warning ("at this point in file");
14994    }
14995
14996#if 0
14997  /* We should do this eventually, but right now this causes regex.o from
14998     libg++ to miscompile, and tString to core dump.  */
14999  exp = build1 (CLEANUP_POINT_EXPR, TREE_TYPE (exp), exp);
15000#endif
15001
15002  /* Strip unused implicit INDIRECT_REFs of references.  */
15003  if (TREE_CODE (exp) == INDIRECT_REF
15004      && TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == REFERENCE_TYPE)
15005    exp = TREE_OPERAND (exp, 0);
15006
15007  /* If we don't do this, we end up down inside expand_expr
15008     trying to do TYPE_MODE on the ERROR_MARK, and really
15009     go outside the bounds of the type.  */
15010  if (exp != error_mark_node)
15011    expand_expr_stmt (break_out_cleanups (exp));
15012
15013  /* Clean up any pending cleanups.  This happens when a function call
15014     returns a cleanup-needing value that nobody uses.  */
15015  expand_end_target_temps ();
15016}
15017
15018/* When a stmt has been parsed, this function is called.
15019
15020   Currently, this function only does something within a
15021   constructor's scope: if a stmt has just assigned to this,
15022   and we are in a derived class, we call `emit_base_init'.  */
15023
15024void
15025finish_stmt ()
15026{
15027  extern struct nesting *cond_stack, *loop_stack, *case_stack;
15028
15029
15030  if (current_function_assigns_this
15031      || ! current_function_just_assigned_this)
15032    return;
15033  if (DECL_CONSTRUCTOR_P (current_function_decl))
15034    {
15035      /* Constructors must wait until we are out of control
15036	 zones before calling base constructors.  */
15037      if (cond_stack || loop_stack || case_stack)
15038	return;
15039      expand_expr_stmt (base_init_expr);
15040      check_base_init (current_class_type);
15041    }
15042  current_function_assigns_this = 1;
15043}
15044
15045/* Change a static member function definition into a FUNCTION_TYPE, instead
15046   of the METHOD_TYPE that we create when it's originally parsed.
15047
15048   WARNING: DO NOT pass &TREE_TYPE (decl) to FN or &TYPE_ARG_TYPES
15049   (TREE_TYPE (decl)) to ARGTYPES, as doing so will corrupt the types of
15050   other decls.  Either pass the addresses of local variables or NULL.  */
15051
15052void
15053revert_static_member_fn (decl, fn, argtypes)
15054     tree *decl, *fn, *argtypes;
15055{
15056  tree tmp;
15057  tree function = fn ? *fn : TREE_TYPE (*decl);
15058  tree args = argtypes ? *argtypes : TYPE_ARG_TYPES (function);
15059
15060  if (CP_TYPE_QUALS (TREE_TYPE (TREE_VALUE (args)))
15061      != TYPE_UNQUALIFIED)
15062    cp_error ("static member function `%#D' declared with type qualifiers",
15063	      *decl);
15064
15065  args = TREE_CHAIN (args);
15066  tmp = build_function_type (TREE_TYPE (function), args);
15067  tmp = build_qualified_type (tmp, CP_TYPE_QUALS (function));
15068  tmp = build_exception_variant (tmp,
15069				 TYPE_RAISES_EXCEPTIONS (function));
15070  TREE_TYPE (*decl) = tmp;
15071  if (DECL_ARGUMENTS (*decl))
15072    DECL_ARGUMENTS (*decl) = TREE_CHAIN (DECL_ARGUMENTS (*decl));
15073  DECL_STATIC_FUNCTION_P (*decl) = 1;
15074  if (fn)
15075    *fn = tmp;
15076  if (argtypes)
15077    *argtypes = args;
15078}
15079
15080struct cp_function
15081{
15082  int returns_value;
15083  int returns_null;
15084  int assigns_this;
15085  int just_assigned_this;
15086  int parms_stored;
15087  int temp_name_counter;
15088  tree named_labels;
15089  struct named_label_list *named_label_uses;
15090  tree shadowed_labels;
15091  tree ctor_label;
15092  tree dtor_label;
15093  rtx last_dtor_insn;
15094  rtx last_parm_cleanup_insn;
15095  tree base_init_list;
15096  tree member_init_list;
15097  tree base_init_expr;
15098  tree current_class_ptr;
15099  tree current_class_ref;
15100  rtx result_rtx;
15101  struct cp_function *next;
15102  struct binding_level *binding_level;
15103  int static_labelno;
15104};
15105
15106static struct cp_function *cp_function_chain;
15107
15108extern int temp_name_counter;
15109
15110/* Save and reinitialize the variables
15111   used during compilation of a C++ function.  */
15112
15113void
15114push_cp_function_context (context)
15115     tree context;
15116{
15117  struct cp_function *p
15118    = (struct cp_function *) xmalloc (sizeof (struct cp_function));
15119
15120  push_function_context_to (context);
15121
15122  p->next = cp_function_chain;
15123  cp_function_chain = p;
15124
15125  p->named_labels = named_labels;
15126  p->named_label_uses = named_label_uses;
15127  p->shadowed_labels = shadowed_labels;
15128  p->returns_value = current_function_returns_value;
15129  p->returns_null = current_function_returns_null;
15130  p->binding_level = current_binding_level;
15131  p->ctor_label = ctor_label;
15132  p->dtor_label = dtor_label;
15133  p->last_dtor_insn = last_dtor_insn;
15134  p->last_parm_cleanup_insn = last_parm_cleanup_insn;
15135  p->assigns_this = current_function_assigns_this;
15136  p->just_assigned_this = current_function_just_assigned_this;
15137  p->parms_stored = current_function_parms_stored;
15138  p->result_rtx = original_result_rtx;
15139  p->base_init_expr = base_init_expr;
15140  p->temp_name_counter = temp_name_counter;
15141  p->base_init_list = current_base_init_list;
15142  p->member_init_list = current_member_init_list;
15143  p->current_class_ptr = current_class_ptr;
15144  p->current_class_ref = current_class_ref;
15145  p->static_labelno = static_labelno;
15146}
15147
15148/* Restore the variables used during compilation of a C++ function.  */
15149
15150void
15151pop_cp_function_context (context)
15152     tree context;
15153{
15154  struct cp_function *p = cp_function_chain;
15155  tree link;
15156
15157  /* Bring back all the labels that were shadowed.  */
15158  for (link = shadowed_labels; link; link = TREE_CHAIN (link))
15159    if (DECL_NAME (TREE_VALUE (link)) != 0)
15160      SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (TREE_VALUE (link)),
15161				  TREE_VALUE (link));
15162
15163  pop_function_context_from (context);
15164
15165  cp_function_chain = p->next;
15166
15167  named_labels = p->named_labels;
15168  named_label_uses = p->named_label_uses;
15169  shadowed_labels = p->shadowed_labels;
15170  current_function_returns_value = p->returns_value;
15171  current_function_returns_null = p->returns_null;
15172  current_binding_level = p->binding_level;
15173  ctor_label = p->ctor_label;
15174  dtor_label = p->dtor_label;
15175  last_dtor_insn = p->last_dtor_insn;
15176  last_parm_cleanup_insn = p->last_parm_cleanup_insn;
15177  current_function_assigns_this = p->assigns_this;
15178  current_function_just_assigned_this = p->just_assigned_this;
15179  current_function_parms_stored = p->parms_stored;
15180  original_result_rtx = p->result_rtx;
15181  base_init_expr = p->base_init_expr;
15182  temp_name_counter = p->temp_name_counter;
15183  current_base_init_list = p->base_init_list;
15184  current_member_init_list = p->member_init_list;
15185  current_class_ptr = p->current_class_ptr;
15186  current_class_ref = p->current_class_ref;
15187  static_labelno = p->static_labelno;
15188
15189  free (p);
15190}
15191
15192int
15193in_function_p ()
15194{
15195  return function_depth != 0;
15196}
15197