decl.c revision 117410
1/* Process declarations and variables for C compiler.
2   Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3   2001, 2002, 2003  Free Software Foundation, Inc.
4   Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
13GNU CC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with GNU CC; see the file COPYING.  If not, write to
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA.  */
22
23/* $FreeBSD: head/contrib/gcc/cp/decl.c 117410 2003-07-11 04:34:52Z kan $ */
24
25
26/* Process declarations and symbol lookup for C front end.
27   Also constructs types; the standard scalar types at initialization,
28   and structure, union, array and enum types when they are declared.  */
29
30/* ??? not all decl nodes are given the most useful possible
31   line numbers.  For example, the CONST_DECLs for enum values.  */
32
33#include "config.h"
34#include "system.h"
35#include "tree.h"
36#include "rtl.h"
37#include "expr.h"
38#include "flags.h"
39#include "cp-tree.h"
40#include "tree-inline.h"
41#include "decl.h"
42#include "lex.h"
43#include "output.h"
44#include "except.h"
45#include "toplev.h"
46#include "hashtab.h"
47#include "ggc.h"
48#include "tm_p.h"
49#include "target.h"
50#include "c-common.h"
51#include "c-pragma.h"
52#include "diagnostic.h"
53#include "debug.h"
54#include "timevar.h"
55#include "input.h"
56
57#ifndef HOST_PTR_PRINTF_FORMAT
58#define HOST_PTR_PRINTF_FORMAT HOST_PTR_PRINTF
59#endif
60#ifndef HOST_PTR_PRINTF_TYPE
61#define HOST_PTR_PRINTF_TYPE (void *)
62#endif
63
64static tree grokparms				PARAMS ((tree));
65static const char *redeclaration_error_message	PARAMS ((tree, tree));
66
67static void push_binding_level PARAMS ((struct cp_binding_level *, int,
68				      int));
69static void pop_binding_level PARAMS ((void));
70static void suspend_binding_level PARAMS ((void));
71static void resume_binding_level PARAMS ((struct cp_binding_level *));
72static struct cp_binding_level *make_binding_level PARAMS ((void));
73static void declare_namespace_level PARAMS ((void));
74static int decl_jump_unsafe PARAMS ((tree));
75static void storedecls PARAMS ((tree));
76static void require_complete_types_for_parms PARAMS ((tree));
77static int ambi_op_p PARAMS ((enum tree_code));
78static int unary_op_p PARAMS ((enum tree_code));
79static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
80static tree lookup_tag_reverse PARAMS ((tree, tree));
81static tree lookup_name_real PARAMS ((tree, int, int, int));
82static void push_local_name PARAMS ((tree));
83static void warn_extern_redeclared_static PARAMS ((tree, tree));
84static tree grok_reference_init PARAMS ((tree, tree, tree));
85static tree grokfndecl PARAMS ((tree, tree, tree, tree, int,
86			      enum overload_flags, tree,
87			      tree, int, int, int, int, int, int, tree));
88static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree));
89static tree follow_tag_typedef PARAMS ((tree));
90static tree lookup_tag PARAMS ((enum tree_code, tree,
91			      struct cp_binding_level *, int));
92static void set_identifier_type_value_with_scope
93	PARAMS ((tree, tree, struct cp_binding_level *));
94static void record_unknown_type PARAMS ((tree, const char *));
95static tree builtin_function_1 PARAMS ((const char *, tree, tree, int,
96                                      enum built_in_class, const char *,
97				      tree));
98static tree build_library_fn_1 PARAMS ((tree, enum tree_code, tree));
99static int member_function_or_else PARAMS ((tree, tree, enum overload_flags));
100static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int,
101				  int));
102static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct cp_binding_level*));
103static void check_for_uninitialized_const_var PARAMS ((tree));
104static hashval_t typename_hash PARAMS ((const void *));
105static int typename_compare PARAMS ((const void *, const void *));
106static void push_binding PARAMS ((tree, tree, struct cp_binding_level*));
107static int add_binding PARAMS ((tree, tree));
108static void pop_binding PARAMS ((tree, tree));
109static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *));
110static cxx_binding *find_binding (tree, tree, cxx_binding *);
111static tree select_decl (cxx_binding *, int);
112static int lookup_flags PARAMS ((int, int));
113static tree qualify_lookup PARAMS ((tree, int));
114static tree record_builtin_java_type PARAMS ((const char *, int));
115static const char *tag_name PARAMS ((enum tag_types code));
116static void find_class_binding_level PARAMS ((void));
117static struct cp_binding_level *innermost_nonclass_level PARAMS ((void));
118static void warn_about_implicit_typename_lookup PARAMS ((tree, tree));
119static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *));
120static int walk_globals_r PARAMS ((tree, void *));
121static int walk_vtables_r PARAMS ((tree, void*));
122static void add_decl_to_level PARAMS ((tree, struct cp_binding_level *));
123static tree make_label_decl PARAMS ((tree, int));
124static void use_label PARAMS ((tree));
125static void check_previous_goto_1 PARAMS ((tree, struct cp_binding_level *, tree,
126					   const char *, int));
127static void check_previous_goto PARAMS ((struct named_label_use_list *));
128static void check_switch_goto PARAMS ((struct cp_binding_level *));
129static void check_previous_gotos PARAMS ((tree));
130static void pop_label PARAMS ((tree, tree));
131static void pop_labels PARAMS ((tree));
132static void maybe_deduce_size_from_array_init PARAMS ((tree, tree));
133static void layout_var_decl PARAMS ((tree));
134static void maybe_commonize_var PARAMS ((tree));
135static tree check_initializer (tree, tree, int);
136static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *));
137static void save_function_data PARAMS ((tree));
138static void check_function_type PARAMS ((tree, tree));
139static void begin_constructor_body PARAMS ((void));
140static void finish_constructor_body PARAMS ((void));
141static void begin_destructor_body PARAMS ((void));
142static void finish_destructor_body PARAMS ((void));
143static tree create_array_type_for_decl PARAMS ((tree, tree, tree));
144static tree get_atexit_node PARAMS ((void));
145static tree get_dso_handle_node PARAMS ((void));
146static tree start_cleanup_fn PARAMS ((void));
147static void end_cleanup_fn PARAMS ((void));
148static tree cp_make_fname_decl PARAMS ((tree, int));
149static void initialize_predefined_identifiers PARAMS ((void));
150static tree check_special_function_return_type
151  PARAMS ((special_function_kind, tree, tree));
152static tree push_cp_library_fn PARAMS ((enum tree_code, tree));
153static tree build_cp_library_fn PARAMS ((tree, enum tree_code, tree));
154static void store_parm_decls PARAMS ((tree));
155static int cp_missing_noreturn_ok_p PARAMS ((tree));
156static void initialize_local_var (tree, tree);
157static void expand_static_init (tree, tree);
158static tree next_initializable_field (tree);
159static tree reshape_init (tree, tree *);
160
161/* Erroneous argument lists can use this *IFF* they do not modify it.  */
162tree error_mark_list;
163
164/* The following symbols are subsumed in the cp_global_trees array, and
165   listed here individually for documentation purposes.
166
167   C++ extensions
168	tree wchar_decl_node;
169
170	tree vtable_entry_type;
171	tree delta_type_node;
172	tree __t_desc_type_node;
173        tree ti_desc_type_node;
174	tree bltn_desc_type_node, ptr_desc_type_node;
175	tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
176	tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
177	tree ptm_desc_type_node;
178	tree base_desc_type_node;
179
180	tree class_type_node, record_type_node, union_type_node, enum_type_node;
181	tree unknown_type_node;
182
183   Array type `vtable_entry_type[]'
184
185	tree vtbl_type_node;
186	tree vtbl_ptr_type_node;
187
188   Namespaces,
189
190	tree std_node;
191	tree abi_node;
192
193   A FUNCTION_DECL which can call `abort'.  Not necessarily the
194   one that the user will declare, but sufficient to be called
195   by routines that want to abort the program.
196
197	tree abort_fndecl;
198
199   The FUNCTION_DECL for the default `::operator delete'.
200
201	tree global_delete_fndecl;
202
203   Used by RTTI
204	tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
205	tree tinfo_var_id;
206
207*/
208
209tree cp_global_trees[CPTI_MAX];
210
211/* Indicates that there is a type value in some namespace, although
212   that is not necessarily in scope at the moment.  */
213
214static GTY(()) tree global_type_node;
215
216/* Expect only namespace names now.  */
217static int only_namespace_names;
218
219/* Used only for jumps to as-yet undefined labels, since jumps to
220   defined labels can have their validity checked immediately.  */
221
222struct named_label_use_list GTY(())
223{
224  struct cp_binding_level *binding_level;
225  tree names_in_scope;
226  tree label_decl;
227  const char *filename_o_goto;
228  int lineno_o_goto;
229  struct named_label_use_list *next;
230};
231
232#define named_label_uses cp_function_chain->x_named_label_uses
233
234#define local_names cp_function_chain->x_local_names
235
236/* A list of objects which have constructors or destructors
237   which reside in the global scope.  The decl is stored in
238   the TREE_VALUE slot and the initializer is stored
239   in the TREE_PURPOSE slot.  */
240tree static_aggregates;
241
242/* -- end of C++ */
243
244/* A node for the integer constants 2, and 3.  */
245
246tree integer_two_node, integer_three_node;
247
248/* Similar, for last_function_parm_tags.  */
249tree last_function_parms;
250
251/* A list of all LABEL_DECLs in the function that have names.  Here so
252   we can clear out their names' definitions at the end of the
253   function, and so we can check the validity of jumps to these labels.  */
254
255struct named_label_list GTY(())
256{
257  struct cp_binding_level *binding_level;
258  tree names_in_scope;
259  tree old_value;
260  tree label_decl;
261  tree bad_decls;
262  struct named_label_list *next;
263  unsigned int in_try_scope : 1;
264  unsigned int in_catch_scope : 1;
265};
266
267#define named_labels cp_function_chain->x_named_labels
268
269/* The name of the anonymous namespace, throughout this translation
270   unit.  */
271tree anonymous_namespace_name;
272
273/* The number of function bodies which we are currently processing.
274   (Zero if we are at namespace scope, one inside the body of a
275   function, two inside the body of a function in a local class, etc.)  */
276int function_depth;
277
278/* States indicating how grokdeclarator() should handle declspecs marked
279   with __attribute__((deprecated)).  An object declared as
280   __attribute__((deprecated)) suppresses warnings of uses of other
281   deprecated items.  */
282
283enum deprecated_states {
284  DEPRECATED_NORMAL,
285  DEPRECATED_SUPPRESS
286};
287
288static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
289
290/* Set by add_implicitly_declared_members() to keep those members from
291   being flagged as deprecated or reported as using deprecated
292   types.  */
293int adding_implicit_members = 0;
294
295/* True if a declaration with an `extern' linkage specifier is being
296   processed.  */
297bool have_extern_spec;
298
299
300/* Compute the chain index of a binding_entry given the HASH value of its
301   name and the total COUNT of chains.  COUNT is assumed to be a power
302   of 2.  */
303#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
304
305/* A free list of "binding_entry"s awaiting for re-use.  */
306static GTY((deletable("")))  binding_entry free_binding_entry;
307
308/* Create a binding_entry object for (NAME, TYPE).  */
309static inline binding_entry
310binding_entry_make (tree name, tree type)
311{
312  binding_entry entry;
313
314  if (free_binding_entry)
315    {
316      entry = free_binding_entry;
317      free_binding_entry = entry->chain;
318    }
319  else
320    entry = ggc_alloc (sizeof (struct binding_entry_s));
321
322  entry->name = name;
323  entry->type = type;
324
325  return entry;
326}
327
328/* Put ENTRY back on the free list.  */
329static inline void
330binding_entry_free (binding_entry entry)
331{
332  entry->chain = free_binding_entry;
333  free_binding_entry = entry;
334}
335
336/* The datatype used to implement the mapping from names to types at
337   a given scope.  */
338struct binding_table_s GTY(())
339{
340  /* Array of chains of "binding_entry"s  */
341  binding_entry * GTY((length ("%h.chain_count"))) chain;
342
343  /* The number of chains in this table.  This is the length of the
344     the member "chaiin" considered as an array.  */
345  size_t chain_count;
346
347  /* Number of "binding_entry"s in this table.  */
348  size_t entry_count;
349};
350
351/* These macros indicate the initial chains count for binding_table.  */
352#define SCOPE_DEFAULT_HT_SIZE                        (1 << 3)
353#define CLASS_SCOPE_HT_SIZE                          (1 << 3)
354#define NAMESPACE_ORDINARY_HT_SIZE                   (1 << 5)
355#define NAMESPACE_STD_HT_SIZE                        (1 << 8)
356#define GLOBAL_SCOPE_HT_SIZE                         (1 << 8)
357
358/* Construct TABLE with an initial CHAIN_COUNT.  */
359static inline void
360binding_table_construct (binding_table table, size_t chain_count)
361{
362  table->chain_count = chain_count;
363  table->entry_count = 0;
364  table->chain = ggc_alloc_cleared
365    (table->chain_count * sizeof (binding_entry));
366}
367
368/* Free TABLE by making its entries ready for reuse. */
369static inline void
370binding_table_free (binding_table table)
371{
372  size_t i;
373  if (table == NULL)
374    return;
375
376  for (i = 0; i < table->chain_count; ++i)
377    {
378      while (table->chain[i] != NULL)
379        {
380          binding_entry entry = table->chain[i];
381          table->chain[i] = entry->chain;
382          binding_entry_free (entry);
383        }
384    }
385  table->entry_count = 0;
386}
387
388/* Allocate a table with CHAIN_COUNT, assumed to be a power of two.  */
389static inline binding_table
390binding_table_new (size_t chain_count)
391{
392  binding_table table = ggc_alloc (sizeof (struct binding_table_s));
393  binding_table_construct (table, chain_count);
394  return table;
395}
396
397/* Expand TABLE to twice its current chain_count.  */
398static void
399binding_table_expand (binding_table table)
400{
401  const size_t old_chain_count = table->chain_count;
402  const size_t old_entry_count = table->entry_count;
403  const size_t new_chain_count = 2 * old_chain_count;
404  binding_entry *old_chains = table->chain;
405  size_t i;
406
407  binding_table_construct (table, new_chain_count);
408  for (i = 0; i < old_chain_count; ++i)
409    {
410      binding_entry entry = old_chains[i];
411      for (; entry != NULL; entry = old_chains[i])
412        {
413          const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
414          const size_t j = ENTRY_INDEX (hash, new_chain_count);
415
416          old_chains[i] = entry->chain;
417          entry->chain = table->chain[j];
418          table->chain[j] = entry;
419        }
420    }
421  table->entry_count = old_entry_count;
422}
423
424/* Insert a binding for NAME to TYPe into TABLE.  */
425static inline void
426binding_table_insert (binding_table table, tree name, tree type)
427{
428  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
429  const size_t i = ENTRY_INDEX (hash, table->chain_count);
430  binding_entry entry = binding_entry_make (name, type);
431
432  entry->chain = table->chain[i];
433  table->chain[i] = entry;
434  ++table->entry_count;
435
436  if (3 * table->chain_count < 5 * table->entry_count)
437    binding_table_expand (table);
438}
439
440/* Return the binding_entry, if any, that maps NAME.  */
441binding_entry
442binding_table_find (binding_table table, tree name)
443{
444  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
445  binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
446
447  while (entry != NULL && entry->name != name)
448    entry = entry->chain;
449
450  return entry;
451}
452
453/* Return the binding_entry, if any, that maps name to an anonymous type.  */
454static inline tree
455binding_table_find_anon_type (binding_table table, tree name)
456{
457  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
458  binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
459
460  while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
461    entry = entry->chain;
462
463  return entry ? entry->type : NULL;
464}
465
466/* Return the binding_entry, if any, that has TYPE as target.  If NAME
467   is non-null, then set the domain and rehash that entry.  */
468static inline binding_entry
469binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
470{
471  const size_t chain_count = table->chain_count;
472  binding_entry entry = NULL;
473  binding_entry *p;
474  size_t i;
475
476  for (i = 0; i < chain_count && entry == NULL; ++i)
477    {
478      p = &table->chain[i];
479      while (*p != NULL && entry == NULL)
480        if ((*p)->type == type)
481          entry = *p;
482        else
483          p = &(*p)->chain;
484    }
485
486  if (entry != NULL && name != NULL && entry->name != name)
487    {
488      /* Remove the bucket from the previous chain.  */
489      *p = (*p)->chain;
490
491      /* Remap the name type to type.  */
492      i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
493      entry->chain = table->chain[i];
494      entry->name = name;
495      table->chain[i] = entry;
496    }
497
498  return entry;
499}
500
501/* Remove from TABLE all entries that map to anonymous enums or
502   class-types.  */
503static void
504binding_table_remove_anonymous_types (binding_table table)
505{
506  const size_t chain_count = table->chain_count;
507  size_t i;
508
509  for (i = 0; i < chain_count; ++i)
510    {
511      binding_entry *p = &table->chain[i];
512
513      while (*p != NULL)
514        if (ANON_AGGRNAME_P ((*p)->name))
515          {
516            binding_entry e = *p;
517            *p = (*p)->chain;
518            --table->entry_count;
519            binding_entry_free (e);
520          }
521        else
522          p = &(*p)->chain;
523    }
524}
525
526/* Apply PROC -- with DATA -- to all entries in TABLE.  */
527void
528binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
529{
530  const size_t chain_count = table->chain_count;
531  size_t i;
532
533  for (i = 0; i < chain_count; ++i)
534    {
535      binding_entry entry = table->chain[i];
536      for (; entry != NULL; entry = entry->chain)
537        proc (entry, data);
538    }
539}
540
541
542/* For each binding contour we allocate a binding_level structure
543   which records the names defined in that contour.
544   Contours include:
545    0) the global one
546    1) one for each function definition,
547       where internal declarations of the parameters appear.
548    2) one for each compound statement,
549       to record its declarations.
550
551   The current meaning of a name can be found by searching the levels
552   from the current one out to the global one.
553
554   Off to the side, may be the class_binding_level.  This exists only
555   to catch class-local declarations.  It is otherwise nonexistent.
556
557   Also there may be binding levels that catch cleanups that must be
558   run when exceptions occur.  Thus, to see whether a name is bound in
559   the current scope, it is not enough to look in the
560   CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
561   instead.  */
562
563/* Note that the information in the `names' component of the global contour
564   is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
565
566struct cp_binding_level GTY(())
567  {
568    /* A chain of _DECL nodes for all variables, constants, functions,
569       and typedef types.  These are in the reverse of the order
570       supplied.  There may be OVERLOADs on this list, too, but they
571       are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
572    tree names;
573
574    /* Count of elements in names chain.  */
575    size_t names_size;
576
577    /* A chain of NAMESPACE_DECL nodes.  */
578    tree namespaces;
579
580    /* An array of static functions and variables (for namespaces only) */
581    varray_type static_decls;
582
583    /* A chain of VTABLE_DECL nodes.  */
584    tree vtables;
585
586    /* A dictionary for looking up enums or class-types names.  */
587    binding_table type_decls;
588
589    /* A list of USING_DECL nodes.  */
590    tree usings;
591
592    /* A list of used namespaces. PURPOSE is the namespace,
593       VALUE the common ancestor with this binding_level's namespace.  */
594    tree using_directives;
595
596    /* If this binding level is the binding level for a class, then
597       class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
598       is the name of an entity bound in the class.  The TREE_TYPE is
599       the DECL bound by this name in the class.  */
600    tree class_shadowed;
601
602    /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
603       is used for all binding levels. In addition the TREE_VALUE is the
604       IDENTIFIER_TYPE_VALUE before we entered the class.  */
605    tree type_shadowed;
606
607    /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
608       label in this scope.  The TREE_PURPOSE is the previous value of
609       the IDENTIFIER_LABEL VALUE.  */
610    tree shadowed_labels;
611
612    /* For each level (except not the global one),
613       a chain of BLOCK nodes for all the levels
614       that were entered and exited one level down.  */
615    tree blocks;
616
617    /* The _TYPE node for this level, if parm_flag == 2.  */
618    tree this_class;
619
620    /* The binding level which this one is contained in (inherits from).  */
621    struct cp_binding_level *level_chain;
622
623    /* List of VAR_DECLS saved from a previous for statement.
624       These would be dead in ISO-conforming code, but might
625       be referenced in ARM-era code.  These are stored in a
626       TREE_LIST; the TREE_VALUE is the actual declaration.  */
627    tree dead_vars_from_for;
628
629    /* 1 for the level that holds the parameters of a function.
630       2 for the level that holds a class declaration.  */
631    unsigned parm_flag : 2;
632
633    /* 1 means make a BLOCK for this level regardless of all else.
634       2 for temporary binding contours created by the compiler.  */
635    unsigned keep : 2;
636
637    /* Nonzero if this level "doesn't exist" for tags.  */
638    unsigned tag_transparent : 1;
639
640    /* Nonzero if this level can safely have additional
641       cleanup-needing variables added to it.  */
642    unsigned more_cleanups_ok : 1;
643    unsigned have_cleanups : 1;
644
645    /* Nonzero if this scope is for storing the decls for template
646       parameters and generic decls; these decls will be discarded and
647       replaced with a TEMPLATE_DECL.  */
648    unsigned template_parms_p : 1;
649
650    /* Nonzero if this scope corresponds to the `<>' in a
651       `template <>' clause.  Whenever this flag is set,
652       TEMPLATE_PARMS_P will be set as well.  */
653    unsigned template_spec_p : 1;
654
655    /* This is set for a namespace binding level.  */
656    unsigned namespace_p : 1;
657
658    /* True if this level is that of a for-statement where we need to
659       worry about ambiguous (ARM or ISO) scope rules.  */
660    unsigned is_for_scope : 1;
661
662    /* True if this level corresponds to a TRY block.  Currently this
663       information is only available while building the tree structure.  */
664    unsigned is_try_scope : 1;
665
666    /* True if this level corresponds to a CATCH block.  Currently this
667       information is only available while building the tree structure.  */
668    unsigned is_catch_scope : 1;
669
670    /* Three bits left for this word.  */
671
672    /* Binding depth at which this level began.  */
673    unsigned binding_depth;
674  };
675
676#define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
677
678/* True if SCOPE designates the global scope binding contour.  */
679#define global_scope_p(SCOPE)  \
680  ((SCOPE) == NAMESPACE_LEVEL (global_namespace))
681
682/* The binding level currently in effect.  */
683
684#define current_binding_level			\
685  (cfun && cp_function_chain->bindings		\
686   ? cp_function_chain->bindings		\
687   : scope_chain->bindings)
688
689/* The binding level of the current class, if any.  */
690
691#define class_binding_level scope_chain->class_bindings
692
693/* A chain of binding_level structures awaiting reuse.  */
694
695static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
696
697/* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
698
699static int keep_next_level_flag;
700
701/* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
702   UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
703   time the VAR_DECL was declared, the type was incomplete.  */
704
705static GTY(()) tree incomplete_vars;
706
707#ifndef ENABLE_SCOPE_CHECKING
708#  define ENABLE_SCOPE_CHECKING 0
709#else
710#  define ENABLE_SCOPE_CHECKING 1
711#endif
712
713static unsigned binding_depth = 0;
714static int is_class_level = 0;
715
716static void
717indent (unsigned depth)
718{
719  unsigned i;
720
721  for (i = 0; i < depth * 2; i++)
722    putc (' ', stderr);
723}
724
725static tree pushdecl_with_scope	PARAMS ((tree, struct cp_binding_level *));
726
727static void
728push_binding_level (newlevel, tag_transparent, keep)
729     struct cp_binding_level *newlevel;
730     int tag_transparent, keep;
731{
732  /* Add this level to the front of the chain (stack) of levels that
733     are active.  */
734  memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
735  newlevel->level_chain = current_binding_level;
736  current_binding_level = newlevel;
737  newlevel->tag_transparent = tag_transparent;
738  newlevel->more_cleanups_ok = 1;
739
740  newlevel->keep = keep;
741  if (ENABLE_SCOPE_CHECKING)
742    {
743      newlevel->binding_depth = binding_depth;
744      indent (binding_depth);
745      verbatim ("push %s level %p line %d\n",
746                (is_class_level) ? "class" : "block",
747                (void *) newlevel, lineno);
748      is_class_level = 0;
749      binding_depth++;
750    }
751}
752
753/* Find the innermost enclosing class scope, and reset
754   CLASS_BINDING_LEVEL appropriately.  */
755
756static void
757find_class_binding_level ()
758{
759  struct cp_binding_level *level = current_binding_level;
760
761  while (level && level->parm_flag != 2)
762    level = level->level_chain;
763  if (level && level->parm_flag == 2)
764    class_binding_level = level;
765  else
766    class_binding_level = 0;
767}
768
769static void
770pop_binding_level ()
771{
772  if (NAMESPACE_LEVEL (global_namespace))
773    /* Cannot pop a level, if there are none left to pop.  */
774    my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
775  /* Pop the current level, and free the structure for reuse.  */
776  if (ENABLE_SCOPE_CHECKING)
777    {
778      indent (--binding_depth);
779      verbatim ("pop  %s level %p line %d\n",
780                (is_class_level) ? "class" : "block",
781                (void *) current_binding_level, lineno);
782      if (is_class_level != (current_binding_level == class_binding_level))
783        {
784          indent (binding_depth);
785          verbatim ("XXX is_class_level != (current_binding_level "
786                    "== class_binding_level)\n");
787        }
788      is_class_level = 0;
789    }
790  {
791    register struct cp_binding_level *level = current_binding_level;
792    current_binding_level = current_binding_level->level_chain;
793    level->level_chain = free_binding_level;
794    if (level->parm_flag != 2)
795      binding_table_free (level->type_decls);
796    else
797      level->type_decls = NULL;
798    my_friendly_assert (!ENABLE_SCOPE_CHECKING
799                        || level->binding_depth == binding_depth, 20030529);
800    free_binding_level = level;
801    find_class_binding_level ();
802  }
803}
804
805static void
806suspend_binding_level ()
807{
808  if (class_binding_level)
809    current_binding_level = class_binding_level;
810
811  if (NAMESPACE_LEVEL (global_namespace))
812    my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
813  /* Suspend the current level.  */
814  if (ENABLE_SCOPE_CHECKING)
815    {
816      indent (--binding_depth);
817      verbatim("suspend  %s level %p line %d\n",
818               (is_class_level) ? "class" : "block",
819               (void *) current_binding_level, lineno);
820      if (is_class_level != (current_binding_level == class_binding_level))
821        {
822          indent (binding_depth);
823          verbatim ("XXX is_class_level != (current_binding_level "
824                    "== class_binding_level)\n");
825        }
826      is_class_level = 0;
827    }
828  current_binding_level = current_binding_level->level_chain;
829  find_class_binding_level ();
830}
831
832static void
833resume_binding_level (b)
834     struct cp_binding_level *b;
835{
836  /* Resuming binding levels is meant only for namespaces,
837     and those cannot nest into classes.  */
838  my_friendly_assert(!class_binding_level, 386);
839  /* Also, resuming a non-directly nested namespace is a no-no.  */
840  my_friendly_assert(b->level_chain == current_binding_level, 386);
841  current_binding_level = b;
842  if (ENABLE_SCOPE_CHECKING)
843    {
844      b->binding_depth = binding_depth;
845      indent (binding_depth);
846      verbatim ("resume %s level %p line %d\n",
847                (is_class_level) ? "class" : "block", (void *) b, lineno);
848      is_class_level = 0;
849      binding_depth++;
850    }
851}
852
853/* Create a new `struct cp_binding_level'.  */
854
855static
856struct cp_binding_level *
857make_binding_level ()
858{
859  /* NOSTRICT */
860  return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
861}
862
863/* Nonzero if we are currently in the global binding level.  */
864
865int
866global_bindings_p ()
867{
868  return global_scope_p (current_binding_level);
869}
870
871/* Return the innermost binding level that is not for a class scope.  */
872
873static struct cp_binding_level *
874innermost_nonclass_level ()
875{
876  struct cp_binding_level *b;
877
878  b = current_binding_level;
879  while (b->parm_flag == 2)
880    b = b->level_chain;
881
882  return b;
883}
884
885/* Nonzero if we are currently in a toplevel binding level.  This
886   means either the global binding level or a namespace in a toplevel
887   binding level.  Since there are no non-toplevel namespace levels,
888   this really means any namespace or template parameter level.  We
889   also include a class whose context is toplevel.  */
890
891int
892toplevel_bindings_p ()
893{
894  struct cp_binding_level *b = innermost_nonclass_level ();
895
896  return b->namespace_p || b->template_parms_p;
897}
898
899/* Nonzero if this is a namespace scope, or if we are defining a class
900   which is itself at namespace scope, or whose enclosing class is
901   such a class, etc.  */
902
903int
904namespace_bindings_p ()
905{
906  struct cp_binding_level *b = innermost_nonclass_level ();
907
908  return b->namespace_p;
909}
910
911/* If KEEP is nonzero, make a BLOCK node for the next binding level,
912   unconditionally.  Otherwise, use the normal logic to decide whether
913   or not to create a BLOCK.  */
914
915void
916keep_next_level (keep)
917     int keep;
918{
919  keep_next_level_flag = keep;
920}
921
922/* Nonzero if the current level needs to have a BLOCK made.  */
923
924int
925kept_level_p ()
926{
927  return (current_binding_level->blocks != NULL_TREE
928	  || current_binding_level->keep
929	  || current_binding_level->names != NULL_TREE
930	  || (current_binding_level->type_decls != NULL
931	      && !current_binding_level->tag_transparent));
932}
933
934static void
935declare_namespace_level ()
936{
937  current_binding_level->namespace_p = 1;
938}
939
940/* Returns nonzero if this scope was created to store template
941   parameters.  */
942
943int
944template_parm_scope_p ()
945{
946  return current_binding_level->template_parms_p;
947}
948
949/* Returns the kind of template specialization we are currently
950   processing, given that it's declaration contained N_CLASS_SCOPES
951   explicit scope qualifications.  */
952
953tmpl_spec_kind
954current_tmpl_spec_kind (n_class_scopes)
955     int n_class_scopes;
956{
957  int n_template_parm_scopes = 0;
958  int seen_specialization_p = 0;
959  int innermost_specialization_p = 0;
960  struct cp_binding_level *b;
961
962  /* Scan through the template parameter scopes.  */
963  for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
964    {
965      /* If we see a specialization scope inside a parameter scope,
966	 then something is wrong.  That corresponds to a declaration
967	 like:
968
969	    template <class T> template <> ...
970
971	 which is always invalid since [temp.expl.spec] forbids the
972	 specialization of a class member template if the enclosing
973	 class templates are not explicitly specialized as well.  */
974      if (b->template_spec_p)
975	{
976	  if (n_template_parm_scopes == 0)
977	    innermost_specialization_p = 1;
978	  else
979	    seen_specialization_p = 1;
980	}
981      else if (seen_specialization_p == 1)
982	return tsk_invalid_member_spec;
983
984      ++n_template_parm_scopes;
985    }
986
987  /* Handle explicit instantiations.  */
988  if (processing_explicit_instantiation)
989    {
990      if (n_template_parm_scopes != 0)
991	/* We've seen a template parameter list during an explicit
992	   instantiation.  For example:
993
994	     template <class T> template void f(int);
995
996	   This is erroneous.  */
997	return tsk_invalid_expl_inst;
998      else
999	return tsk_expl_inst;
1000    }
1001
1002  if (n_template_parm_scopes < n_class_scopes)
1003    /* We've not seen enough template headers to match all the
1004       specialized classes present.  For example:
1005
1006         template <class T> void R<T>::S<T>::f(int);
1007
1008       This is invalid; there needs to be one set of template
1009       parameters for each class.  */
1010    return tsk_insufficient_parms;
1011  else if (n_template_parm_scopes == n_class_scopes)
1012    /* We're processing a non-template declaration (even though it may
1013       be a member of a template class.)  For example:
1014
1015         template <class T> void S<T>::f(int);
1016
1017       The `class T' maches the `S<T>', leaving no template headers
1018       corresponding to the `f'.  */
1019    return tsk_none;
1020  else if (n_template_parm_scopes > n_class_scopes + 1)
1021    /* We've got too many template headers.  For example:
1022
1023         template <> template <class T> void f (T);
1024
1025       There need to be more enclosing classes.  */
1026    return tsk_excessive_parms;
1027  else
1028    /* This must be a template.  It's of the form:
1029
1030         template <class T> template <class U> void S<T>::f(U);
1031
1032       This is a specialization if the innermost level was a
1033       specialization; otherwise it's just a definition of the
1034       template.  */
1035    return innermost_specialization_p ? tsk_expl_spec : tsk_template;
1036}
1037
1038void
1039set_class_shadows (shadows)
1040     tree shadows;
1041{
1042  class_binding_level->class_shadowed = shadows;
1043}
1044
1045/* Enter a new binding level.
1046   If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
1047   not for that of tags.  */
1048
1049void
1050pushlevel (tag_transparent)
1051     int tag_transparent;
1052{
1053  struct cp_binding_level *newlevel;
1054
1055  if (cfun && !doing_semantic_analysis_p ())
1056    return;
1057
1058  /* Reuse or create a struct for this binding level.  */
1059  if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1060    {
1061      newlevel = free_binding_level;
1062      free_binding_level = free_binding_level->level_chain;
1063    }
1064  else
1065    newlevel = make_binding_level ();
1066
1067  push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
1068  keep_next_level_flag = 0;
1069}
1070
1071/* We're defining an object of type TYPE.  If it needs a cleanup, but
1072   we're not allowed to add any more objects with cleanups to the current
1073   scope, create a new binding level.  */
1074
1075void
1076maybe_push_cleanup_level (type)
1077     tree type;
1078{
1079  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1080      && current_binding_level->more_cleanups_ok == 0)
1081    {
1082      keep_next_level (2);
1083      pushlevel (1);
1084      clear_last_expr ();
1085      add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
1086    }
1087}
1088
1089/* Enter a new scope.  The KIND indicates what kind of scope is being
1090   created.  */
1091
1092void
1093begin_scope (sk)
1094     scope_kind sk;
1095{
1096  pushlevel (0);
1097
1098  switch (sk)
1099    {
1100    case sk_template_spec:
1101      current_binding_level->template_spec_p = 1;
1102      /* Fall through.  */
1103
1104    case sk_template_parms:
1105      current_binding_level->template_parms_p = 1;
1106      break;
1107
1108    default:
1109      abort ();
1110    }
1111}
1112
1113/* Exit the current scope.  */
1114
1115void
1116finish_scope ()
1117{
1118  poplevel (0, 0, 0);
1119}
1120
1121void
1122note_level_for_for ()
1123{
1124  current_binding_level->is_for_scope = 1;
1125}
1126
1127/* Record that the current binding level represents a try block.  */
1128
1129void
1130note_level_for_try ()
1131{
1132  current_binding_level->is_try_scope = 1;
1133}
1134
1135/* Record that the current binding level represents a catch block.  */
1136
1137void
1138note_level_for_catch ()
1139{
1140  current_binding_level->is_catch_scope = 1;
1141}
1142
1143/* For a binding between a name and an entity at a block scope,
1144   this is the `struct cp_binding_level' for the block.  */
1145#define BINDING_LEVEL(NODE) ((NODE)->scope.level)
1146
1147/* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
1148
1149static GTY((deletable (""))) cxx_binding *free_bindings;
1150
1151/* Make DECL the innermost binding for ID.  The LEVEL is the binding
1152   level at which this declaration is being bound.  */
1153
1154static void
1155push_binding (id, decl, level)
1156     tree id;
1157     tree decl;
1158     struct cp_binding_level* level;
1159{
1160  cxx_binding *binding;
1161
1162  if (free_bindings)
1163    {
1164      binding = free_bindings;
1165      free_bindings = binding->previous;
1166    }
1167  else
1168    binding = cxx_binding_make ();
1169
1170  /* Now, fill in the binding information.  */
1171  BINDING_VALUE (binding) = decl;
1172  BINDING_TYPE (binding) = NULL_TREE;
1173  BINDING_LEVEL (binding) = level;
1174  INHERITED_VALUE_BINDING_P (binding) = 0;
1175  LOCAL_BINDING_P (binding) = (level != class_binding_level);
1176  BINDING_HAS_LEVEL_P (binding) = 1;
1177
1178  /* And put it on the front of the list of bindings for ID.  */
1179  binding->previous = IDENTIFIER_BINDING (id);
1180  IDENTIFIER_BINDING (id) = binding;
1181}
1182
1183/* ID is already bound in the current scope.  But, DECL is an
1184   additional binding for ID in the same scope.  This is the `struct
1185   stat' hack whereby a non-typedef class-name or enum-name can be
1186   bound at the same level as some other kind of entity.  It's the
1187   responsibility of the caller to check that inserting this name is
1188   valid here.  Returns nonzero if the new binding was successful.  */
1189static int
1190add_binding (id, decl)
1191     tree id;
1192     tree decl;
1193{
1194  cxx_binding *binding = IDENTIFIER_BINDING (id);
1195  int ok = 1;
1196
1197  timevar_push (TV_NAME_LOOKUP);
1198  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1199    /* The new name is the type name.  */
1200    BINDING_TYPE (binding) = decl;
1201  else if (!BINDING_VALUE (binding))
1202    /* This situation arises when push_class_level_binding moves an
1203       inherited type-binding out of the way to make room for a new
1204       value binding.  */
1205    BINDING_VALUE (binding) = decl;
1206  else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1207	   && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
1208    {
1209      /* The old binding was a type name.  It was placed in
1210	 BINDING_VALUE because it was thought, at the point it was
1211	 declared, to be the only entity with such a name.  Move the
1212	 type name into the type slot; it is now hidden by the new
1213	 binding.  */
1214      BINDING_TYPE (binding) = BINDING_VALUE (binding);
1215      BINDING_VALUE (binding) = decl;
1216      INHERITED_VALUE_BINDING_P (binding) = 0;
1217    }
1218  else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1219	   && TREE_CODE (decl) == TYPE_DECL
1220	   && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
1221	   && (same_type_p (TREE_TYPE (decl),
1222			    TREE_TYPE (BINDING_VALUE (binding)))
1223	       /* If either type involves template parameters, we must
1224		  wait until instantiation.  */
1225	       || uses_template_parms (TREE_TYPE (decl))
1226	       || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
1227    /* We have two typedef-names, both naming the same type to have
1228       the same name.  This is OK because of:
1229
1230         [dcl.typedef]
1231
1232	 In a given scope, a typedef specifier can be used to redefine
1233	 the name of any type declared in that scope to refer to the
1234	 type to which it already refers.  */
1235    ok = 0;
1236  /* There can be two block-scope declarations of the same variable,
1237     so long as they are `extern' declarations.  However, there cannot
1238     be two declarations of the same static data member:
1239
1240       [class.mem]
1241
1242       A member shall not be declared twice in the
1243       member-specification.  */
1244  else if (TREE_CODE (decl) == VAR_DECL
1245	   && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
1246	   && DECL_EXTERNAL (decl)
1247	   && DECL_EXTERNAL (BINDING_VALUE (binding))
1248	   && !DECL_CLASS_SCOPE_P (decl))
1249    {
1250      duplicate_decls (decl, BINDING_VALUE (binding));
1251      ok = 0;
1252    }
1253  else
1254    {
1255      error ("declaration of `%#D'", decl);
1256      cp_error_at ("conflicts with previous declaration `%#D'",
1257		   BINDING_VALUE (binding));
1258      ok = 0;
1259    }
1260
1261  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
1262}
1263
1264/* Add DECL to the list of things declared in B.  */
1265
1266static void
1267add_decl_to_level (decl, b)
1268     tree decl;
1269     struct cp_binding_level *b;
1270{
1271  if (TREE_CODE (decl) == NAMESPACE_DECL
1272      && !DECL_NAMESPACE_ALIAS (decl))
1273    {
1274      TREE_CHAIN (decl) = b->namespaces;
1275      b->namespaces = decl;
1276    }
1277  else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1278    {
1279      TREE_CHAIN (decl) = b->vtables;
1280      b->vtables = decl;
1281    }
1282  else
1283    {
1284      /* We build up the list in reverse order, and reverse it later if
1285         necessary.  */
1286      TREE_CHAIN (decl) = b->names;
1287      b->names = decl;
1288      b->names_size++;
1289
1290      /* If appropriate, add decl to separate list of statics */
1291      if (b->namespace_p)
1292	if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1293	    || (TREE_CODE (decl) == FUNCTION_DECL
1294		&& (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1295	  VARRAY_PUSH_TREE (b->static_decls, decl);
1296    }
1297}
1298
1299/* Bind DECL to ID in the current_binding_level, assumed to be a local
1300   binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1301   doesn't really belong to this binding level, that it got here
1302   through a using-declaration.  */
1303
1304void
1305push_local_binding (id, decl, flags)
1306     tree id;
1307     tree decl;
1308     int flags;
1309{
1310  struct cp_binding_level *b;
1311
1312  /* Skip over any local classes.  This makes sense if we call
1313     push_local_binding with a friend decl of a local class.  */
1314  b = current_binding_level;
1315  while (b->parm_flag == 2)
1316    b = b->level_chain;
1317
1318  if (lookup_name_current_level (id))
1319    {
1320      /* Supplement the existing binding.  */
1321      if (!add_binding (id, decl))
1322	/* It didn't work.  Something else must be bound at this
1323	   level.  Do not add DECL to the list of things to pop
1324	   later.  */
1325	return;
1326    }
1327  else
1328    /* Create a new binding.  */
1329    push_binding (id, decl, b);
1330
1331  if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1332    /* We must put the OVERLOAD into a TREE_LIST since the
1333       TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1334       decls that got here through a using-declaration.  */
1335    decl = build_tree_list (NULL_TREE, decl);
1336
1337  /* And put DECL on the list of things declared by the current
1338     binding level.  */
1339  add_decl_to_level (decl, b);
1340}
1341
1342/* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1343   binding was successful.  */
1344
1345int
1346push_class_binding (id, decl)
1347     tree id;
1348     tree decl;
1349{
1350  int result = 1;
1351  cxx_binding *binding = IDENTIFIER_BINDING (id);
1352  tree context;
1353
1354  timevar_push (TV_NAME_LOOKUP);
1355  /* Note that we declared this value so that we can issue an error if
1356     this is an invalid redeclaration of a name already used for some
1357     other purpose.  */
1358  note_name_declared_in_class (id, decl);
1359
1360  if (binding && BINDING_LEVEL (binding) == class_binding_level)
1361    /* Supplement the existing binding.  */
1362    result = add_binding (id, decl);
1363  else
1364    /* Create a new binding.  */
1365    push_binding (id, decl, class_binding_level);
1366
1367  /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1368     class-level declaration.  Note that we do not use DECL here
1369     because of the possibility of the `struct stat' hack; if DECL is
1370     a class-name or enum-name we might prefer a field-name, or some
1371     such.  */
1372  IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1373
1374  /* If this is a binding from a base class, mark it as such.  */
1375  binding = IDENTIFIER_BINDING (id);
1376  if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1377    {
1378      /* Any implicit typename must be from a base-class.  The
1379	 context for an implicit typename declaration is always
1380	 the derived class in which the lookup was done, so the checks
1381	 based on the context of DECL below will not trigger.  */
1382      if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
1383	INHERITED_VALUE_BINDING_P (binding) = 1;
1384      else
1385	{
1386	  if (TREE_CODE (decl) == OVERLOAD)
1387	    context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1388	  else
1389	    {
1390	      my_friendly_assert (DECL_P (decl), 0);
1391	      context = context_for_name_lookup (decl);
1392	    }
1393
1394	  if (is_properly_derived_from (current_class_type, context))
1395	    INHERITED_VALUE_BINDING_P (binding) = 1;
1396	  else
1397	    INHERITED_VALUE_BINDING_P (binding) = 0;
1398	}
1399    }
1400  else if (BINDING_VALUE (binding) == decl)
1401    /* We only encounter a TREE_LIST when push_class_decls detects an
1402       ambiguity.  Such an ambiguity can be overridden by a definition
1403       in this class.  */
1404    INHERITED_VALUE_BINDING_P (binding) = 1;
1405
1406  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1407}
1408
1409/* Remove the binding for DECL which should be the innermost binding
1410   for ID.  */
1411
1412static void
1413pop_binding (id, decl)
1414     tree id;
1415     tree decl;
1416{
1417  cxx_binding *binding;
1418
1419  if (id == NULL_TREE)
1420    /* It's easiest to write the loops that call this function without
1421       checking whether or not the entities involved have names.  We
1422       get here for such an entity.  */
1423    return;
1424
1425  /* Get the innermost binding for ID.  */
1426  binding = IDENTIFIER_BINDING (id);
1427
1428  /* The name should be bound.  */
1429  my_friendly_assert (binding != NULL, 0);
1430
1431  /* The DECL will be either the ordinary binding or the type
1432     binding for this identifier.  Remove that binding.  */
1433  if (BINDING_VALUE (binding) == decl)
1434    BINDING_VALUE (binding) = NULL_TREE;
1435  else if (BINDING_TYPE (binding) == decl)
1436    BINDING_TYPE (binding) = NULL_TREE;
1437  else
1438    abort ();
1439
1440  if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1441    {
1442      /* We're completely done with the innermost binding for this
1443	 identifier.  Unhook it from the list of bindings.  */
1444      IDENTIFIER_BINDING (id) = binding->previous;
1445
1446      /* Add it to the free list.  */
1447      binding->previous = free_bindings;
1448      free_bindings = binding;
1449
1450      /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1451	 it.  */
1452      BINDING_LEVEL (binding) = NULL;
1453    }
1454}
1455
1456/* When a label goes out of scope, check to see if that label was used
1457   in a valid manner, and issue any appropriate warnings or errors.  */
1458
1459static void
1460pop_label (label, old_value)
1461     tree label;
1462     tree old_value;
1463{
1464  if (!processing_template_decl && doing_semantic_analysis_p ())
1465    {
1466      if (DECL_INITIAL (label) == NULL_TREE)
1467	{
1468	  cp_error_at ("label `%D' used but not defined", label);
1469	  /* Avoid crashing later.  */
1470	  define_label (input_filename, 1, DECL_NAME (label));
1471	}
1472      else if (warn_unused_label && !TREE_USED (label))
1473	cp_warning_at ("label `%D' defined but not used", label);
1474    }
1475
1476  SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1477}
1478
1479/* At the end of a function, all labels declared within the function
1480   go out of scope.  BLOCK is the top-level block for the
1481   function.  */
1482
1483static void
1484pop_labels (block)
1485     tree block;
1486{
1487  struct named_label_list *link;
1488
1489  /* Clear out the definitions of all label names, since their scopes
1490     end here.  */
1491  for (link = named_labels; link; link = link->next)
1492    {
1493      pop_label (link->label_decl, link->old_value);
1494      /* Put the labels into the "variables" of the top-level block,
1495	 so debugger can see them.  */
1496      TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1497      BLOCK_VARS (block) = link->label_decl;
1498    }
1499
1500  named_labels = NULL;
1501}
1502
1503/* Exit a binding level.
1504   Pop the level off, and restore the state of the identifier-decl mappings
1505   that were in effect when this level was entered.
1506
1507   If KEEP == 1, this level had explicit declarations, so
1508   and create a "block" (a BLOCK node) for the level
1509   to record its declarations and subblocks for symbol table output.
1510
1511   If FUNCTIONBODY is nonzero, this level is the body of a function,
1512   so create a block as if KEEP were set and also clear out all
1513   label names.
1514
1515   If REVERSE is nonzero, reverse the order of decls before putting
1516   them into the BLOCK.  */
1517
1518tree
1519poplevel (keep, reverse, functionbody)
1520     int keep;
1521     int reverse;
1522     int functionbody;
1523{
1524  register tree link;
1525  /* The chain of decls was accumulated in reverse order.
1526     Put it into forward order, just for cleanliness.  */
1527  tree decls;
1528  int tmp = functionbody;
1529  int real_functionbody;
1530  tree subblocks;
1531  tree block = NULL_TREE;
1532  tree decl;
1533  int leaving_for_scope;
1534
1535  timevar_push (TV_NAME_LOOKUP);
1536
1537  if (cfun && !doing_semantic_analysis_p ())
1538    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1539
1540  my_friendly_assert (current_binding_level->parm_flag != 2,
1541		      19990916);
1542
1543  real_functionbody = (current_binding_level->keep == 2
1544		       ? ((functionbody = 0), tmp) : functionbody);
1545  subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1546
1547  my_friendly_assert (!current_binding_level->class_shadowed,
1548		      19990414);
1549
1550  /* We used to use KEEP == 2 to indicate that the new block should go
1551     at the beginning of the list of blocks at this binding level,
1552     rather than the end.  This hack is no longer used.  */
1553  my_friendly_assert (keep == 0 || keep == 1, 0);
1554
1555  if (current_binding_level->keep == 1)
1556    keep = 1;
1557
1558  /* Any uses of undefined labels, and any defined labels, now operate
1559     under constraints of next binding contour.  */
1560  if (cfun && !functionbody)
1561    {
1562      struct cp_binding_level *level_chain;
1563      level_chain = current_binding_level->level_chain;
1564      if (level_chain)
1565	{
1566	  struct named_label_use_list *uses;
1567	  struct named_label_list *labels;
1568	  for (labels = named_labels; labels; labels = labels->next)
1569	    if (labels->binding_level == current_binding_level)
1570	      {
1571		tree decl;
1572		if (current_binding_level->is_try_scope)
1573		  labels->in_try_scope = 1;
1574		if (current_binding_level->is_catch_scope)
1575		  labels->in_catch_scope = 1;
1576		for (decl = labels->names_in_scope; decl;
1577		     decl = TREE_CHAIN (decl))
1578		  if (decl_jump_unsafe (decl))
1579		    labels->bad_decls = tree_cons (NULL_TREE, decl,
1580						   labels->bad_decls);
1581		labels->binding_level = level_chain;
1582		labels->names_in_scope = level_chain->names;
1583	      }
1584
1585	  for (uses = named_label_uses; uses; uses = uses->next)
1586	    if (uses->binding_level == current_binding_level)
1587	      {
1588		uses->binding_level = level_chain;
1589		uses->names_in_scope = level_chain->names;
1590	      }
1591	}
1592    }
1593
1594  /* Get the decls in the order they were written.
1595     Usually current_binding_level->names is in reverse order.
1596     But parameter decls were previously put in forward order.  */
1597
1598  if (reverse)
1599    current_binding_level->names
1600      = decls = nreverse (current_binding_level->names);
1601  else
1602    decls = current_binding_level->names;
1603
1604  /* Output any nested inline functions within this block
1605     if they weren't already output.  */
1606  for (decl = decls; decl; decl = TREE_CHAIN (decl))
1607    if (TREE_CODE (decl) == FUNCTION_DECL
1608	&& ! TREE_ASM_WRITTEN (decl)
1609	&& DECL_INITIAL (decl) != NULL_TREE
1610	&& TREE_ADDRESSABLE (decl)
1611	&& decl_function_context (decl) == current_function_decl)
1612      {
1613	/* If this decl was copied from a file-scope decl
1614	   on account of a block-scope extern decl,
1615	   propagate TREE_ADDRESSABLE to the file-scope decl.  */
1616	if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1617	  TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1618	else
1619	  {
1620	    push_function_context ();
1621	    output_inline_function (decl);
1622	    pop_function_context ();
1623	  }
1624      }
1625
1626  /* When not in function-at-a-time mode, expand_end_bindings will
1627     warn about unused variables.  But, in function-at-a-time mode
1628     expand_end_bindings is not passed the list of variables in the
1629     current scope, and therefore no warning is emitted.  So, we
1630     explicitly warn here.  */
1631  if (!processing_template_decl)
1632    warn_about_unused_variables (getdecls ());
1633
1634  /* If there were any declarations or structure tags in that level,
1635     or if this level is a function body,
1636     create a BLOCK to record them for the life of this function.  */
1637  block = NULL_TREE;
1638  if (keep == 1 || functionbody)
1639    block = make_node (BLOCK);
1640  if (block != NULL_TREE)
1641    {
1642      BLOCK_VARS (block) = decls;
1643      BLOCK_SUBBLOCKS (block) = subblocks;
1644    }
1645
1646  /* In each subblock, record that this is its superior.  */
1647  if (keep >= 0)
1648    for (link = subblocks; link; link = TREE_CHAIN (link))
1649      BLOCK_SUPERCONTEXT (link) = block;
1650
1651  /* We still support the old for-scope rules, whereby the variables
1652     in a for-init statement were in scope after the for-statement
1653     ended.  We only use the new rules in flag_new_for_scope is
1654     nonzero.  */
1655  leaving_for_scope
1656    = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1657
1658  /* Remove declarations for all the DECLs in this level.  */
1659  for (link = decls; link; link = TREE_CHAIN (link))
1660    {
1661      if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1662          && DECL_NAME (link))
1663	{
1664	  cxx_binding *outer_binding
1665	    = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1666	  tree ns_binding;
1667
1668	  if (!outer_binding)
1669	    ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1670	  else
1671	    ns_binding = NULL_TREE;
1672
1673	  if (outer_binding
1674	      && (BINDING_LEVEL (outer_binding)
1675		  == current_binding_level->level_chain))
1676	    /* We have something like:
1677
1678	         int i;
1679	         for (int i; ;);
1680
1681	       and we are leaving the `for' scope.  There's no reason to
1682	       keep the binding of the inner `i' in this case.  */
1683	    pop_binding (DECL_NAME (link), link);
1684	  else if ((outer_binding
1685		    && (TREE_CODE (BINDING_VALUE (outer_binding))
1686			== TYPE_DECL))
1687		   || (ns_binding
1688		       && TREE_CODE (ns_binding) == TYPE_DECL))
1689	    /* Here, we have something like:
1690
1691		 typedef int I;
1692
1693		 void f () {
1694		   for (int I; ;);
1695		 }
1696
1697	       We must pop the for-scope binding so we know what's a
1698	       type and what isn't.  */
1699	    pop_binding (DECL_NAME (link), link);
1700	  else
1701	    {
1702	      /* Mark this VAR_DECL as dead so that we can tell we left it
1703		 there only for backward compatibility.  */
1704	      DECL_DEAD_FOR_LOCAL (link) = 1;
1705
1706	      /* Keep track of what should of have happenned when we
1707		 popped the binding.  */
1708	      if (outer_binding && BINDING_VALUE (outer_binding))
1709		DECL_SHADOWED_FOR_VAR (link)
1710		  = BINDING_VALUE (outer_binding);
1711
1712	      /* Add it to the list of dead variables in the next
1713		 outermost binding to that we can remove these when we
1714		 leave that binding.  */
1715	      current_binding_level->level_chain->dead_vars_from_for
1716		= tree_cons (NULL_TREE, link,
1717			     current_binding_level->level_chain->
1718			     dead_vars_from_for);
1719
1720	      /* Although we don't pop the cxx_binding, we do clear
1721		 its BINDING_LEVEL since the level is going away now.  */
1722	      BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1723		= 0;
1724	    }
1725	}
1726      else
1727	{
1728	  /* Remove the binding.  */
1729	  decl = link;
1730	  if (TREE_CODE (decl) == TREE_LIST)
1731	    decl = TREE_VALUE (decl);
1732	  if (DECL_P (decl))
1733	    pop_binding (DECL_NAME (decl), decl);
1734	  else if (TREE_CODE (decl) == OVERLOAD)
1735	    pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1736	  else
1737	    abort ();
1738	}
1739    }
1740
1741  /* Remove declarations for any `for' variables from inner scopes
1742     that we kept around.  */
1743  for (link = current_binding_level->dead_vars_from_for;
1744       link; link = TREE_CHAIN (link))
1745    pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1746
1747  /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1748  for (link = current_binding_level->type_shadowed;
1749       link; link = TREE_CHAIN (link))
1750    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1751
1752  /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1753  for (link = current_binding_level->shadowed_labels;
1754       link;
1755       link = TREE_CHAIN (link))
1756    pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1757
1758  /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1759     list if a `using' declaration put them there.  The debugging
1760     back-ends won't understand OVERLOAD, so we remove them here.
1761     Because the BLOCK_VARS are (temporarily) shared with
1762     CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1763     popped all the bindings.  */
1764  if (block)
1765    {
1766      tree* d;
1767
1768      for (d = &BLOCK_VARS (block); *d; )
1769	{
1770	  if (TREE_CODE (*d) == TREE_LIST)
1771	    *d = TREE_CHAIN (*d);
1772	  else
1773	    d = &TREE_CHAIN (*d);
1774	}
1775    }
1776
1777  /* If the level being exited is the top level of a function,
1778     check over all the labels.  */
1779  if (functionbody)
1780    {
1781      /* Since this is the top level block of a function, the vars are
1782	 the function's parameters.  Don't leave them in the BLOCK
1783	 because they are found in the FUNCTION_DECL instead.  */
1784      BLOCK_VARS (block) = 0;
1785      pop_labels (block);
1786    }
1787
1788  tmp = current_binding_level->keep;
1789
1790  pop_binding_level ();
1791  if (functionbody)
1792    DECL_INITIAL (current_function_decl) = block;
1793  else if (block)
1794    current_binding_level->blocks
1795      = chainon (current_binding_level->blocks, block);
1796
1797  /* If we did not make a block for the level just exited,
1798     any blocks made for inner levels
1799     (since they cannot be recorded as subblocks in that level)
1800     must be carried forward so they will later become subblocks
1801     of something else.  */
1802  else if (subblocks)
1803    current_binding_level->blocks
1804      = chainon (current_binding_level->blocks, subblocks);
1805
1806  /* Each and every BLOCK node created here in `poplevel' is important
1807     (e.g. for proper debugging information) so if we created one
1808     earlier, mark it as "used".  */
1809  if (block)
1810    TREE_USED (block) = 1;
1811
1812  /* Take care of compiler's internal binding structures.  */
1813  if (tmp == 2)
1814    {
1815      tree scope_stmts;
1816
1817      scope_stmts
1818	= add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1819      if (block)
1820	{
1821	  SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1822	  SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1823	}
1824
1825      block = poplevel (keep, reverse, functionbody);
1826    }
1827
1828  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1829}
1830
1831/* Delete the node BLOCK from the current binding level.
1832   This is used for the block inside a stmt expr ({...})
1833   so that the block can be reinserted where appropriate.  */
1834
1835void
1836delete_block (block)
1837     tree block;
1838{
1839  tree t;
1840  if (current_binding_level->blocks == block)
1841    current_binding_level->blocks = TREE_CHAIN (block);
1842  for (t = current_binding_level->blocks; t;)
1843    {
1844      if (TREE_CHAIN (t) == block)
1845	TREE_CHAIN (t) = TREE_CHAIN (block);
1846      else
1847	t = TREE_CHAIN (t);
1848    }
1849  TREE_CHAIN (block) = NULL_TREE;
1850  /* Clear TREE_USED which is always set by poplevel.
1851     The flag is set again if insert_block is called.  */
1852  TREE_USED (block) = 0;
1853}
1854
1855/* Insert BLOCK at the end of the list of subblocks of the
1856   current binding level.  This is used when a BIND_EXPR is expanded,
1857   to handle the BLOCK node inside the BIND_EXPR.  */
1858
1859void
1860insert_block (block)
1861     tree block;
1862{
1863  TREE_USED (block) = 1;
1864  current_binding_level->blocks
1865    = chainon (current_binding_level->blocks, block);
1866}
1867
1868/* Set the BLOCK node for the innermost scope
1869   (the one we are currently in).  */
1870
1871void
1872set_block (block)
1873    tree block ATTRIBUTE_UNUSED;
1874{
1875  /* The RTL expansion machinery requires us to provide this callback,
1876     but it is not applicable in function-at-a-time mode.  */
1877  my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1878}
1879
1880/* Do a pushlevel for class declarations.  */
1881
1882void
1883pushlevel_class ()
1884{
1885  register struct cp_binding_level *newlevel;
1886
1887  /* Reuse or create a struct for this binding level.  */
1888  if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1889    {
1890      newlevel = free_binding_level;
1891      free_binding_level = free_binding_level->level_chain;
1892    }
1893  else
1894    newlevel = make_binding_level ();
1895
1896  if (ENABLE_SCOPE_CHECKING)
1897    is_class_level = 1;
1898
1899  push_binding_level (newlevel, 0, 0);
1900
1901  class_binding_level = current_binding_level;
1902  class_binding_level->parm_flag = 2;
1903  class_binding_level->this_class = current_class_type;
1904}
1905
1906/* ...and a poplevel for class declarations.  */
1907
1908void
1909poplevel_class ()
1910{
1911  register struct cp_binding_level *level = class_binding_level;
1912  tree shadowed;
1913
1914  timevar_push (TV_NAME_LOOKUP);
1915
1916  my_friendly_assert (level != 0, 354);
1917
1918  /* If we're leaving a toplevel class, don't bother to do the setting
1919     of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1920     shouldn't even be used when current_class_type isn't set, and second,
1921     if we don't touch it here, we're able to use the cache effect if the
1922     next time we're entering a class scope, it is the same class.  */
1923  if (current_class_depth != 1)
1924    {
1925      struct cp_binding_level* b;
1926
1927      /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1928      for (shadowed = level->class_shadowed;
1929	   shadowed;
1930	   shadowed = TREE_CHAIN (shadowed))
1931	IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1932
1933      /* Find the next enclosing class, and recreate
1934	 IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1935      b = level->level_chain;
1936      while (b && b->parm_flag != 2)
1937	b = b->level_chain;
1938
1939      if (b)
1940	for (shadowed = b->class_shadowed;
1941	     shadowed;
1942	     shadowed = TREE_CHAIN (shadowed))
1943	  {
1944	    cxx_binding *binding;
1945
1946	    binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1947	    while (binding && BINDING_LEVEL (binding) != b)
1948	      binding = binding->previous;
1949
1950	    if (binding)
1951	      IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1952		= BINDING_VALUE (binding);
1953	  }
1954    }
1955  else
1956    /* Remember to save what IDENTIFIER's were bound in this scope so we
1957       can recover from cache misses.  */
1958    {
1959      previous_class_type = current_class_type;
1960      previous_class_values = class_binding_level->class_shadowed;
1961    }
1962  for (shadowed = level->type_shadowed;
1963       shadowed;
1964       shadowed = TREE_CHAIN (shadowed))
1965    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1966
1967  /* Remove the bindings for all of the class-level declarations.  */
1968  for (shadowed = level->class_shadowed;
1969       shadowed;
1970       shadowed = TREE_CHAIN (shadowed))
1971    pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1972
1973  /* Now, pop out of the binding level which we created up in the
1974     `pushlevel_class' routine.  */
1975  if (ENABLE_SCOPE_CHECKING)
1976    is_class_level = 1;
1977
1978  pop_binding_level ();
1979
1980  timevar_pop (TV_NAME_LOOKUP);
1981}
1982
1983/* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1984   for any names in enclosing classes.  */
1985
1986void
1987clear_identifier_class_values ()
1988{
1989  tree t;
1990
1991  if (!class_binding_level)
1992    return;
1993
1994  for (t = class_binding_level->class_shadowed;
1995       t;
1996       t = TREE_CHAIN (t))
1997    IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1998}
1999
2000/* Returns nonzero if T is a virtual function table.  */
2001
2002int
2003vtable_decl_p (t, data)
2004     tree t;
2005     void *data ATTRIBUTE_UNUSED;
2006{
2007  return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
2008}
2009
2010/* Returns nonzero if T is a TYPE_DECL for a type with virtual
2011   functions.  */
2012
2013int
2014vtype_decl_p (t, data)
2015     tree t;
2016     void *data ATTRIBUTE_UNUSED;
2017{
2018  return (TREE_CODE (t) == TYPE_DECL
2019	  && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
2020	  && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
2021}
2022
2023/* Return the declarations that are members of the namespace NS.  */
2024
2025tree
2026cp_namespace_decls (ns)
2027     tree ns;
2028{
2029  return NAMESPACE_LEVEL (ns)->names;
2030}
2031
2032struct walk_globals_data {
2033  walk_globals_pred p;
2034  walk_globals_fn f;
2035  void *data;
2036};
2037
2038/* Walk the vtable declarations in NAMESPACE.  Whenever one is found
2039   for which P returns nonzero, call F with its address.  If any call
2040   to F returns a nonzero value, return a nonzero value.  */
2041
2042static int
2043walk_vtables_r (namespace, data)
2044     tree namespace;
2045     void *data;
2046{
2047  struct walk_globals_data* wgd = (struct walk_globals_data *) data;
2048  walk_globals_fn f = wgd->f;
2049  void *d = wgd->data;
2050  tree decl = NAMESPACE_LEVEL (namespace)->vtables;
2051  int result = 0;
2052
2053  for (; decl ; decl = TREE_CHAIN (decl))
2054    result |= (*f) (&decl, d);
2055
2056  return result;
2057}
2058
2059/* Walk the vtable declarations.  Whenever one is found for which P
2060   returns nonzero, call F with its address.  If any call to F
2061   returns a nonzero value, return a nonzero value.  */
2062int
2063walk_vtables (p, f, data)
2064     walk_globals_pred p;
2065     walk_globals_fn f;
2066     void *data;
2067{
2068  struct walk_globals_data wgd;
2069  wgd.p = p;
2070  wgd.f = f;
2071  wgd.data = data;
2072
2073  return walk_namespaces (walk_vtables_r, &wgd);
2074}
2075
2076/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
2077   itself, calling F for each.  The DATA is passed to F as well.  */
2078
2079static int
2080walk_namespaces_r (namespace, f, data)
2081     tree namespace;
2082     walk_namespaces_fn f;
2083     void *data;
2084{
2085  int result = 0;
2086  tree current = NAMESPACE_LEVEL (namespace)->namespaces;
2087
2088  result |= (*f) (namespace, data);
2089
2090  for (; current; current = TREE_CHAIN (current))
2091    result |= walk_namespaces_r (current, f, data);
2092
2093  return result;
2094}
2095
2096/* Walk all the namespaces, calling F for each.  The DATA is passed to
2097   F as well.  */
2098
2099int
2100walk_namespaces (f, data)
2101     walk_namespaces_fn f;
2102     void *data;
2103{
2104  return walk_namespaces_r (global_namespace, f, data);
2105}
2106
2107/* Walk the global declarations in NAMESPACE.  Whenever one is found
2108   for which P returns nonzero, call F with its address.  If any call
2109   to F returns a nonzero value, return a nonzero value.  */
2110
2111static int
2112walk_globals_r (namespace, data)
2113     tree namespace;
2114     void *data;
2115{
2116  struct walk_globals_data* wgd = (struct walk_globals_data *) data;
2117  walk_globals_pred p = wgd->p;
2118  walk_globals_fn f = wgd->f;
2119  void *d = wgd->data;
2120  tree *t;
2121  int result = 0;
2122
2123  t = &NAMESPACE_LEVEL (namespace)->names;
2124
2125  while (*t)
2126    {
2127      tree glbl = *t;
2128
2129      if ((*p) (glbl, d))
2130	result |= (*f) (t, d);
2131
2132      /* If F changed *T, then *T still points at the next item to
2133	 examine.  */
2134      if (*t == glbl)
2135	t = &TREE_CHAIN (*t);
2136    }
2137
2138  return result;
2139}
2140
2141/* Walk the global declarations.  Whenever one is found for which P
2142   returns nonzero, call F with its address.  If any call to F
2143   returns a nonzero value, return a nonzero value.  */
2144
2145int
2146walk_globals (p, f, data)
2147     walk_globals_pred p;
2148     walk_globals_fn f;
2149     void *data;
2150{
2151  struct walk_globals_data wgd;
2152  wgd.p = p;
2153  wgd.f = f;
2154  wgd.data = data;
2155
2156  return walk_namespaces (walk_globals_r, &wgd);
2157}
2158
2159/* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
2160   DATA is non-NULL, this is the last time we will call
2161   wrapup_global_declarations for this NAMESPACE.  */
2162
2163int
2164wrapup_globals_for_namespace (namespace, data)
2165     tree namespace;
2166     void *data;
2167{
2168  struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
2169  varray_type statics = level->static_decls;
2170  tree *vec = &VARRAY_TREE (statics, 0);
2171  int len = VARRAY_ACTIVE_SIZE (statics);
2172  int last_time = (data != 0);
2173
2174  if (last_time)
2175    {
2176      check_global_declarations (vec, len);
2177      return 0;
2178    }
2179
2180  /* Write out any globals that need to be output.  */
2181  return wrapup_global_declarations (vec, len);
2182}
2183
2184
2185/* For debugging.  */
2186static int no_print_functions = 0;
2187static int no_print_builtins = 0;
2188
2189/* Called from print_binding_level through binding_table_foreach to
2190   print the content of binding ENTRY.  DATA is a pointer to line offset
2191   marker.  */
2192static void
2193bt_print_entry (binding_entry entry, void *data)
2194{
2195  int *p = (int *) data;
2196  int len;
2197
2198  if (entry->name == NULL)
2199    len = 3;
2200  else if (entry->name == TYPE_IDENTIFIER (entry->type))
2201    len = 2;
2202  else
2203    len = 4;
2204
2205  *p += len;
2206
2207  if (*p > 5)
2208    {
2209      fprintf (stderr, "\n\t");
2210      *p = len;
2211    }
2212  if (entry->name == NULL)
2213    {
2214      print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
2215      fprintf (stderr, ">");
2216    }
2217  else if (entry->name == TYPE_IDENTIFIER (entry->type))
2218    print_node_brief (stderr, "", entry->type, 0);
2219  else
2220    {
2221      print_node_brief (stderr, "<typedef", entry->name, 0);
2222      print_node_brief (stderr, "", entry->type, 0);
2223      fprintf (stderr, ">");
2224    }
2225}
2226
2227void
2228print_binding_level (lvl)
2229     struct cp_binding_level *lvl;
2230{
2231  tree t;
2232  int i = 0, len;
2233  fprintf (stderr, " blocks=");
2234  fprintf (stderr, HOST_PTR_PRINTF_FORMAT, HOST_PTR_PRINTF_TYPE lvl->blocks);
2235  if (lvl->tag_transparent)
2236    fprintf (stderr, " tag-transparent");
2237  if (lvl->more_cleanups_ok)
2238    fprintf (stderr, " more-cleanups-ok");
2239  if (lvl->have_cleanups)
2240    fprintf (stderr, " have-cleanups");
2241  fprintf (stderr, "\n");
2242  if (lvl->names)
2243    {
2244      fprintf (stderr, " names:\t");
2245      /* We can probably fit 3 names to a line?  */
2246      for (t = lvl->names; t; t = TREE_CHAIN (t))
2247	{
2248	  if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
2249	    continue;
2250	  if (no_print_builtins
2251	      && (TREE_CODE (t) == TYPE_DECL)
2252	      && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
2253	    continue;
2254
2255	  /* Function decls tend to have longer names.  */
2256	  if (TREE_CODE (t) == FUNCTION_DECL)
2257	    len = 3;
2258	  else
2259	    len = 2;
2260	  i += len;
2261	  if (i > 6)
2262	    {
2263	      fprintf (stderr, "\n\t");
2264	      i = len;
2265	    }
2266	  print_node_brief (stderr, "", t, 0);
2267	  if (t == error_mark_node)
2268	    break;
2269	}
2270      if (i)
2271        fprintf (stderr, "\n");
2272    }
2273  if (lvl->type_decls)
2274    {
2275      fprintf (stderr, " tags:\t");
2276      i = 0;
2277      binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
2278      if (i)
2279	fprintf (stderr, "\n");
2280    }
2281  if (lvl->class_shadowed)
2282    {
2283      fprintf (stderr, " class-shadowed:");
2284      for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
2285	{
2286	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2287	}
2288      fprintf (stderr, "\n");
2289    }
2290  if (lvl->type_shadowed)
2291    {
2292      fprintf (stderr, " type-shadowed:");
2293      for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2294        {
2295	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2296        }
2297      fprintf (stderr, "\n");
2298    }
2299}
2300
2301void
2302print_other_binding_stack (stack)
2303     struct cp_binding_level *stack;
2304{
2305  struct cp_binding_level *level;
2306  for (level = stack; !global_scope_p (level); level = level->level_chain)
2307    {
2308      fprintf (stderr, "binding level ");
2309      fprintf (stderr, HOST_PTR_PRINTF_FORMAT, HOST_PTR_PRINTF_TYPE level);
2310      fprintf (stderr, "\n");
2311      print_binding_level (level);
2312    }
2313}
2314
2315void
2316print_binding_stack ()
2317{
2318  struct cp_binding_level *b;
2319  fprintf (stderr, "current_binding_level=");
2320  fprintf (stderr, HOST_PTR_PRINTF_FORMAT, HOST_PTR_PRINTF_TYPE current_binding_level);
2321  fprintf (stderr, "\nclass_binding_level=");
2322  fprintf (stderr, HOST_PTR_PRINTF_FORMAT, HOST_PTR_PRINTF_TYPE class_binding_level);
2323  fprintf (stderr, "\nNAMESPACE_LEVEL (global_namespace)=");
2324  fprintf (stderr, HOST_PTR_PRINTF_FORMAT,
2325           HOST_PTR_PRINTF_TYPE NAMESPACE_LEVEL (global_namespace));
2326  fprintf (stderr, "\n");
2327  if (class_binding_level)
2328    {
2329      for (b = class_binding_level; b; b = b->level_chain)
2330	if (b == current_binding_level)
2331	  break;
2332      if (b)
2333	b = class_binding_level;
2334      else
2335	b = current_binding_level;
2336    }
2337  else
2338    b = current_binding_level;
2339  print_other_binding_stack (b);
2340  fprintf (stderr, "global:\n");
2341  print_binding_level (NAMESPACE_LEVEL (global_namespace));
2342}
2343
2344/* Namespace binding access routines.   */
2345
2346/* Check whether the a binding for the name to scope is known.
2347   Returns the binding found, or NULL.  */
2348
2349static inline cxx_binding *
2350find_binding (tree name, tree scope, cxx_binding *front)
2351{
2352  cxx_binding *iter;
2353  cxx_binding *prev = NULL;
2354
2355  timevar_push (TV_NAME_LOOKUP);
2356
2357  for (iter = front; iter; iter = iter->previous)
2358    {
2359      if (BINDING_SCOPE (iter) == scope)
2360	{
2361	  /* Move binding found to the front of the list, so
2362             subsequent lookups will find it faster.  */
2363	  if (prev)
2364	    {
2365	      prev->previous = iter->previous;
2366	      iter->previous = front;
2367	      IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2368	    }
2369	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, iter);
2370	}
2371      prev = iter;
2372    }
2373  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL);
2374}
2375
2376/* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
2377cxx_binding *
2378cxx_scope_find_binding_for_name (tree scope, tree name)
2379{
2380  cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2381  if (b)
2382    {
2383      scope = ORIGINAL_NAMESPACE (scope);
2384      /* Fold-in case where NAME is used only once.  */
2385      if (scope == BINDING_SCOPE (b) && b->previous == NULL)
2386        return b;
2387      return find_binding (name, scope, b);
2388    }
2389  return b;
2390}
2391
2392
2393/* Always returns a binding for name in scope.
2394   If no binding is found, make a new one.  */
2395
2396cxx_binding *
2397binding_for_name (tree name, tree scope)
2398{
2399  cxx_binding *result;
2400
2401  scope = ORIGINAL_NAMESPACE (scope);
2402  result = cxx_scope_find_binding_for_name (scope, name);
2403  if (result)
2404    return result;
2405  /* Not found, make a new one.  */
2406  result = cxx_binding_make ();
2407  result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2408  BINDING_TYPE (result) = NULL_TREE;
2409  BINDING_VALUE (result) = NULL_TREE;
2410  BINDING_SCOPE (result) = scope;
2411  result->is_local = false;
2412  result->value_is_inherited = false;
2413  result->has_level = false;
2414  IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2415  return result;
2416}
2417
2418/* Return the binding value for name in scope.  */
2419
2420tree
2421namespace_binding (tree name, tree scope)
2422{
2423  cxx_binding *b =
2424    cxx_scope_find_binding_for_name (scope ? scope : global_namespace, name);
2425
2426  return b ? b->value : NULL_TREE;
2427}
2428
2429/* Set the binding value for name in scope.  */
2430
2431void
2432set_namespace_binding (name, scope, val)
2433     tree name;
2434     tree scope;
2435     tree val;
2436{
2437  cxx_binding *b;
2438
2439  timevar_push (TV_NAME_LOOKUP);
2440  if (scope == NULL_TREE)
2441    scope = global_namespace;
2442
2443  b = binding_for_name (name, scope);
2444  BINDING_VALUE (b) = val;
2445  timevar_pop (TV_NAME_LOOKUP);
2446}
2447
2448/* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2449   select a name that is unique to this compilation unit.  */
2450
2451void
2452push_namespace (name)
2453     tree name;
2454{
2455  tree d = NULL_TREE;
2456  int need_new = 1;
2457  int implicit_use = 0;
2458  int global = 0;
2459
2460  timevar_push (TV_NAME_LOOKUP);
2461
2462  if (!global_namespace)
2463    {
2464      /* This must be ::.  */
2465      my_friendly_assert (name == get_identifier ("::"), 377);
2466      global = 1;
2467    }
2468  else if (!name)
2469    {
2470      /* The name of anonymous namespace is unique for the translation
2471         unit.  */
2472      if (!anonymous_namespace_name)
2473        anonymous_namespace_name = get_file_function_name ('N');
2474      name = anonymous_namespace_name;
2475      d = IDENTIFIER_NAMESPACE_VALUE (name);
2476      if (d)
2477        /* Reopening anonymous namespace.  */
2478        need_new = 0;
2479      implicit_use = 1;
2480    }
2481  else
2482    {
2483      /* Check whether this is an extended namespace definition.  */
2484      d = IDENTIFIER_NAMESPACE_VALUE (name);
2485      if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2486        {
2487          need_new = 0;
2488          if (DECL_NAMESPACE_ALIAS (d))
2489            {
2490              error ("namespace alias `%D' not allowed here, assuming `%D'",
2491                        d, DECL_NAMESPACE_ALIAS (d));
2492              d = DECL_NAMESPACE_ALIAS (d);
2493            }
2494        }
2495    }
2496
2497  if (need_new)
2498    {
2499      /* Make a new namespace, binding the name to it.  */
2500      d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2501      /* The global namespace is not pushed, and the global binding
2502	 level is set elsewhere.  */
2503      if (!global)
2504	{
2505	  DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2506	  d = pushdecl (d);
2507	  pushlevel (0);
2508	  declare_namespace_level ();
2509	  NAMESPACE_LEVEL (d) = current_binding_level;
2510          current_binding_level->type_decls =
2511            binding_table_new (name == std_identifier
2512                               ? NAMESPACE_STD_HT_SIZE
2513                               : NAMESPACE_ORDINARY_HT_SIZE);
2514	  VARRAY_TREE_INIT (current_binding_level->static_decls,
2515			    name != std_identifier ? 10 : 200,
2516			    "Static declarations");
2517	}
2518    }
2519  else
2520    resume_binding_level (NAMESPACE_LEVEL (d));
2521
2522  if (implicit_use)
2523    do_using_directive (d);
2524  /* Enter the name space.  */
2525  current_namespace = d;
2526
2527  timevar_pop (TV_NAME_LOOKUP);
2528}
2529
2530/* Pop from the scope of the current namespace.  */
2531
2532void
2533pop_namespace ()
2534{
2535  my_friendly_assert (current_namespace != global_namespace, 20010801);
2536  current_namespace = CP_DECL_CONTEXT (current_namespace);
2537  /* The binding level is not popped, as it might be re-opened later.  */
2538  suspend_binding_level ();
2539}
2540
2541/* Push into the scope of the namespace NS, even if it is deeply
2542   nested within another namespace.  */
2543
2544void
2545push_nested_namespace (ns)
2546     tree ns;
2547{
2548  if (ns == global_namespace)
2549    push_to_top_level ();
2550  else
2551    {
2552      push_nested_namespace (CP_DECL_CONTEXT (ns));
2553      push_namespace (DECL_NAME (ns));
2554    }
2555}
2556
2557/* Pop back from the scope of the namespace NS, which was previously
2558   entered with push_nested_namespace.  */
2559
2560void
2561pop_nested_namespace (ns)
2562     tree ns;
2563{
2564  timevar_push (TV_NAME_LOOKUP);
2565  while (ns != global_namespace)
2566    {
2567      pop_namespace ();
2568      ns = CP_DECL_CONTEXT (ns);
2569    }
2570
2571  pop_from_top_level ();
2572  timevar_pop (TV_NAME_LOOKUP);
2573}
2574
2575
2576/* Allocate storage for saving a C++ binding.  */
2577#define cxx_saved_binding_make() \
2578  (ggc_alloc (sizeof (cxx_saved_binding)))
2579
2580struct cxx_saved_binding GTY(())
2581{
2582  /* Link that chains saved C++ bindings for a given name into a stack.  */
2583  cxx_saved_binding *previous;
2584  /* The name of the current binding.  */
2585  tree identifier;
2586  /* The binding we're saving.  */
2587  cxx_binding *binding;
2588  tree class_value;
2589  tree real_type_value;
2590};
2591
2592/* Subroutines for reverting temporarily to top-level for instantiation
2593   of templates and such.  We actually need to clear out the class- and
2594   local-value slots of all identifiers, so that only the global values
2595   are at all visible.  Simply setting current_binding_level to the global
2596   scope isn't enough, because more binding levels may be pushed.  */
2597struct saved_scope *scope_chain;
2598
2599static cxx_saved_binding *
2600store_bindings (tree names, cxx_saved_binding *old_bindings)
2601{
2602  tree t;
2603  cxx_saved_binding *search_bindings = old_bindings;
2604
2605  timevar_push (TV_NAME_LOOKUP);
2606  for (t = names; t; t = TREE_CHAIN (t))
2607    {
2608      tree id;
2609      cxx_saved_binding *saved;
2610      cxx_saved_binding *t1;
2611
2612      if (TREE_CODE (t) == TREE_LIST)
2613	id = TREE_PURPOSE (t);
2614      else
2615	id = DECL_NAME (t);
2616
2617      if (!id
2618	  /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2619	     we have no IDENTIFIER_BINDING if we have left the class
2620	     scope, but cached the class-level declarations.  */
2621	  || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2622	continue;
2623
2624      for (t1 = search_bindings; t1; t1 = t1->previous)
2625	if (t1->identifier == id)
2626	  goto skip_it;
2627
2628      my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2629      saved = cxx_saved_binding_make ();
2630      saved->previous = old_bindings;
2631      saved->identifier = id;
2632      saved->binding = IDENTIFIER_BINDING (id);
2633      saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2634      saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2635      IDENTIFIER_BINDING (id) = NULL;
2636      IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2637      old_bindings = saved;
2638    skip_it:
2639      ;
2640    }
2641  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2642}
2643
2644void
2645maybe_push_to_top_level (pseudo)
2646     int pseudo;
2647{
2648  struct saved_scope *s;
2649  struct cp_binding_level *b;
2650  cxx_saved_binding *old_bindings;
2651  int need_pop;
2652
2653  timevar_push (TV_NAME_LOOKUP);
2654
2655  s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2656
2657  b = scope_chain ? current_binding_level : 0;
2658
2659  /* If we're in the middle of some function, save our state.  */
2660  if (cfun)
2661    {
2662      need_pop = 1;
2663      push_function_context_to (NULL_TREE);
2664    }
2665  else
2666    need_pop = 0;
2667
2668  old_bindings = NULL;
2669  if (scope_chain && previous_class_type)
2670    old_bindings = store_bindings (previous_class_values, old_bindings);
2671
2672  /* Have to include the global scope, because class-scope decls
2673     aren't listed anywhere useful.  */
2674  for (; b; b = b->level_chain)
2675    {
2676      tree t;
2677
2678      /* Template IDs are inserted into the global level. If they were
2679	 inserted into namespace level, finish_file wouldn't find them
2680	 when doing pending instantiations. Therefore, don't stop at
2681	 namespace level, but continue until :: .  */
2682      if (global_scope_p (b) || (pseudo && b->template_parms_p))
2683	break;
2684
2685      old_bindings = store_bindings (b->names, old_bindings);
2686      /* We also need to check class_shadowed to save class-level type
2687	 bindings, since pushclass doesn't fill in b->names.  */
2688      if (b->parm_flag == 2)
2689	old_bindings = store_bindings (b->class_shadowed, old_bindings);
2690
2691      /* Unwind type-value slots back to top level.  */
2692      for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2693	SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2694    }
2695  s->prev = scope_chain;
2696  s->old_bindings = old_bindings;
2697  s->bindings = b;
2698  s->need_pop_function_context = need_pop;
2699  s->function_decl = current_function_decl;
2700  s->last_parms = last_function_parms;
2701
2702  scope_chain = s;
2703  current_function_decl = NULL_TREE;
2704  VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2705  current_lang_name = lang_name_cplusplus;
2706  current_namespace = global_namespace;
2707  timevar_pop (TV_NAME_LOOKUP);
2708}
2709
2710void
2711push_to_top_level ()
2712{
2713  maybe_push_to_top_level (0);
2714}
2715
2716void
2717pop_from_top_level ()
2718{
2719  struct saved_scope *s = scope_chain;
2720  cxx_saved_binding *saved;
2721
2722  timevar_push (TV_NAME_LOOKUP);
2723
2724  /* Clear out class-level bindings cache.  */
2725  if (previous_class_type)
2726    invalidate_class_lookup_cache ();
2727
2728  current_lang_base = 0;
2729
2730  scope_chain = s->prev;
2731  for (saved = s->old_bindings; saved; saved = saved->previous)
2732    {
2733      tree id = saved->identifier;
2734
2735      IDENTIFIER_BINDING (id) = saved->binding;
2736      IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2737      SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2738    }
2739
2740  /* If we were in the middle of compiling a function, restore our
2741     state.  */
2742  if (s->need_pop_function_context)
2743    pop_function_context_from (NULL_TREE);
2744  current_function_decl = s->function_decl;
2745  last_function_parms = s->last_parms;
2746
2747  timevar_pop (TV_NAME_LOOKUP);
2748}
2749
2750/* Push a definition of struct, union or enum tag "name".
2751   into binding_level "b".   "type" should be the type node,
2752   We assume that the tag "name" is not already defined.
2753
2754   Note that the definition may really be just a forward reference.
2755   In that case, the TYPE_SIZE will be a NULL_TREE.
2756
2757   C++ gratuitously puts all these tags in the name space.  */
2758
2759/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2760   record the shadowed value for this binding contour.  TYPE is
2761   the type that ID maps to.  */
2762
2763static void
2764set_identifier_type_value_with_scope (id, type, b)
2765     tree id;
2766     tree type;
2767     struct cp_binding_level *b;
2768{
2769  if (!b->namespace_p)
2770    {
2771      /* Shadow the marker, not the real thing, so that the marker
2772	 gets restored later.  */
2773      tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2774      b->type_shadowed
2775	= tree_cons (id, old_type_value, b->type_shadowed);
2776    }
2777  else
2778    {
2779      cxx_binding *binding = binding_for_name (id, current_namespace);
2780      BINDING_TYPE (binding) = type;
2781      /* Store marker instead of real type.  */
2782      type = global_type_node;
2783    }
2784  SET_IDENTIFIER_TYPE_VALUE (id, type);
2785}
2786
2787/* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2788
2789void
2790set_identifier_type_value (id, type)
2791     tree id;
2792     tree type;
2793{
2794  set_identifier_type_value_with_scope (id, type, current_binding_level);
2795}
2796
2797/* Return the type associated with id.  */
2798
2799tree
2800identifier_type_value (id)
2801     tree id;
2802{
2803  timevar_push (TV_NAME_LOOKUP);
2804  /* There is no type with that name, anywhere.  */
2805  if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2806    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2807  /* This is not the type marker, but the real thing.  */
2808  if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2809    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2810  /* Have to search for it. It must be on the global level, now.
2811     Ask lookup_name not to return non-types.  */
2812  id = lookup_name_real (id, 2, 1, 0);
2813  if (id)
2814    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2815  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2816}
2817
2818/* Pop off extraneous binding levels left over due to syntax errors.
2819
2820   We don't pop past namespaces, as they might be valid.  */
2821
2822void
2823pop_everything ()
2824{
2825  if (ENABLE_SCOPE_CHECKING)
2826    verbatim ("XXX entering pop_everything ()\n");
2827  while (!toplevel_bindings_p ())
2828    {
2829      if (current_binding_level->parm_flag == 2)
2830	pop_nested_class ();
2831      else
2832	poplevel (0, 0, 0);
2833    }
2834  if (ENABLE_SCOPE_CHECKING)
2835    verbatim ("XXX leaving pop_everything ()\n");
2836}
2837
2838/* The type TYPE is being declared.  If it is a class template, or a
2839   specialization of a class template, do any processing required and
2840   perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2841   being declared a friend.  B is the binding level at which this TYPE
2842   should be bound.
2843
2844   Returns the TYPE_DECL for TYPE, which may have been altered by this
2845   processing.  */
2846
2847static tree
2848maybe_process_template_type_declaration (type, globalize, b)
2849     tree type;
2850     int globalize;
2851     struct cp_binding_level* b;
2852{
2853  tree decl = TYPE_NAME (type);
2854
2855  if (processing_template_parmlist)
2856    /* You can't declare a new template type in a template parameter
2857       list.  But, you can declare a non-template type:
2858
2859         template <class A*> struct S;
2860
2861       is a forward-declaration of `A'.  */
2862    ;
2863  else
2864    {
2865      maybe_check_template_type (type);
2866
2867      my_friendly_assert (IS_AGGR_TYPE (type)
2868			  || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2869
2870
2871      if (processing_template_decl)
2872	{
2873	  /* This may change after the call to
2874	     push_template_decl_real, but we want the original value.  */
2875	  tree name = DECL_NAME (decl);
2876
2877	  decl = push_template_decl_real (decl, globalize);
2878	  /* If the current binding level is the binding level for the
2879	     template parameters (see the comment in
2880	     begin_template_parm_list) and the enclosing level is a class
2881	     scope, and we're not looking at a friend, push the
2882	     declaration of the member class into the class scope.  In the
2883	     friend case, push_template_decl will already have put the
2884	     friend into global scope, if appropriate.  */
2885	  if (TREE_CODE (type) != ENUMERAL_TYPE
2886	      && !globalize && b->template_parms_p
2887	      && b->level_chain->parm_flag == 2)
2888	    {
2889	      finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2890	      /* Put this tag on the list of tags for the class, since
2891		 that won't happen below because B is not the class
2892		 binding level, but is instead the pseudo-global level.  */
2893              if (b->level_chain->type_decls == NULL)
2894                b->level_chain->type_decls =
2895                  binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2896              binding_table_insert (b->level_chain->type_decls, name, type);
2897	      if (!COMPLETE_TYPE_P (current_class_type))
2898		{
2899		  maybe_add_class_template_decl_list (current_class_type,
2900						      type, /*friend_p=*/0);
2901		  CLASSTYPE_NESTED_UDTS (current_class_type) =
2902                    b->level_chain->type_decls;
2903		}
2904	    }
2905	}
2906    }
2907
2908  return decl;
2909}
2910
2911/* In C++, you don't have to write `struct S' to refer to `S'; you
2912   can just use `S'.  We accomplish this by creating a TYPE_DECL as
2913   if the user had written `typedef struct S S'.  Create and return
2914   the TYPE_DECL for TYPE.  */
2915
2916tree
2917create_implicit_typedef (name, type)
2918     tree name;
2919     tree type;
2920{
2921  tree decl;
2922
2923  decl = build_decl (TYPE_DECL, name, type);
2924  DECL_ARTIFICIAL (decl) = 1;
2925  /* There are other implicit type declarations, like the one *within*
2926     a class that allows you to write `S::S'.  We must distinguish
2927     amongst these.  */
2928  SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2929  TYPE_NAME (type) = decl;
2930
2931  return decl;
2932}
2933
2934/* Remember a local name for name-mangling purposes.  */
2935
2936static void
2937push_local_name (decl)
2938     tree decl;
2939{
2940  size_t i, nelts;
2941  tree t, name;
2942
2943  timevar_push (TV_NAME_LOOKUP);
2944
2945  if (!local_names)
2946    VARRAY_TREE_INIT (local_names, 8, "local_names");
2947
2948  name = DECL_NAME (decl);
2949
2950  nelts = VARRAY_ACTIVE_SIZE (local_names);
2951  for (i = 0; i < nelts; i++)
2952    {
2953      t = VARRAY_TREE (local_names, i);
2954      if (DECL_NAME (t) == name)
2955	{
2956	  if (!DECL_LANG_SPECIFIC (decl))
2957	    retrofit_lang_decl (decl);
2958	  DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2959	  if (DECL_LANG_SPECIFIC (t))
2960	    DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2961	  else
2962	    DECL_DISCRIMINATOR (decl) = 1;
2963
2964	  VARRAY_TREE (local_names, i) = decl;
2965	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
2966	}
2967    }
2968
2969  VARRAY_PUSH_TREE (local_names, decl);
2970  timevar_pop (TV_NAME_LOOKUP);
2971}
2972
2973/* Push a tag name NAME for struct/class/union/enum type TYPE.
2974   Normally put it into the inner-most non-tag-transparent scope,
2975   but if GLOBALIZE is true, put it in the inner-most non-class scope.
2976   The latter is needed for implicit declarations.  */
2977
2978void
2979pushtag (name, type, globalize)
2980     tree name, type;
2981     int globalize;
2982{
2983  register struct cp_binding_level *b;
2984
2985  timevar_push (TV_NAME_LOOKUP);
2986
2987  b = current_binding_level;
2988  while (b->tag_transparent
2989	 || (b->parm_flag == 2
2990	     && (globalize
2991		 /* We may be defining a new type in the initializer
2992		    of a static member variable. We allow this when
2993		    not pedantic, and it is particularly useful for
2994		    type punning via an anonymous union.  */
2995		 || COMPLETE_TYPE_P (b->this_class))))
2996    b = b->level_chain;
2997
2998  if (b->type_decls == NULL)
2999    b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
3000  binding_table_insert (b->type_decls, name, type);
3001
3002  if (name)
3003    {
3004      /* Do C++ gratuitous typedefing.  */
3005      if (IDENTIFIER_TYPE_VALUE (name) != type)
3006        {
3007          register tree d = NULL_TREE;
3008	  int in_class = 0;
3009	  tree context = TYPE_CONTEXT (type);
3010
3011	  if (! context)
3012	    {
3013	      tree cs = current_scope ();
3014
3015	      if (! globalize)
3016		context = cs;
3017	      else if (cs != NULL_TREE && TYPE_P (cs))
3018		/* When declaring a friend class of a local class, we want
3019		   to inject the newly named class into the scope
3020		   containing the local class, not the namespace scope.  */
3021		context = decl_function_context (get_type_decl (cs));
3022	    }
3023	  if (!context)
3024	    context = current_namespace;
3025
3026	  if ((b->template_parms_p && b->level_chain->parm_flag == 2)
3027	      || b->parm_flag == 2)
3028	    in_class = 1;
3029
3030	  if (current_lang_name == lang_name_java)
3031	    TYPE_FOR_JAVA (type) = 1;
3032
3033	  d = create_implicit_typedef (name, type);
3034	  DECL_CONTEXT (d) = FROB_CONTEXT (context);
3035	  if (! in_class)
3036	    set_identifier_type_value_with_scope (name, type, b);
3037
3038	  d = maybe_process_template_type_declaration (type,
3039						       globalize, b);
3040
3041	  if (b->parm_flag == 2)
3042	    {
3043	      if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
3044		/* Put this TYPE_DECL on the TYPE_FIELDS list for the
3045		   class.  But if it's a member template class, we
3046		   want the TEMPLATE_DECL, not the TYPE_DECL, so this
3047		   is done later.  */
3048		finish_member_declaration (d);
3049	      else
3050		pushdecl_class_level (d);
3051	    }
3052	  else
3053	    d = pushdecl_with_scope (d, b);
3054
3055	  /* FIXME what if it gets a name from typedef?  */
3056	  if (ANON_AGGRNAME_P (name))
3057	    DECL_IGNORED_P (d) = 1;
3058
3059	  TYPE_CONTEXT (type) = DECL_CONTEXT (d);
3060
3061	  /* If this is a local class, keep track of it.  We need this
3062	     information for name-mangling, and so that it is possible to find
3063	     all function definitions in a translation unit in a convenient
3064	     way.  (It's otherwise tricky to find a member function definition
3065	     it's only pointed to from within a local class.)  */
3066	  if (TYPE_CONTEXT (type)
3067	      && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
3068	      && !processing_template_decl)
3069	    VARRAY_PUSH_TREE (local_classes, type);
3070        }
3071      if (b->parm_flag == 2)
3072	{
3073	  if (!COMPLETE_TYPE_P (current_class_type))
3074	    {
3075	      maybe_add_class_template_decl_list (current_class_type,
3076						  type, /*friend_p=*/0);
3077	      CLASSTYPE_NESTED_UDTS (current_class_type) = b->type_decls;
3078	    }
3079	}
3080    }
3081
3082  if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
3083    /* Use the canonical TYPE_DECL for this node.  */
3084    TYPE_STUB_DECL (type) = TYPE_NAME (type);
3085  else
3086    {
3087      /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
3088	 will be the tagged type we just added to the current
3089	 binding level.  This fake NULL-named TYPE_DECL node helps
3090	 dwarfout.c to know when it needs to output a
3091	 representation of a tagged type, and it also gives us a
3092	 convenient place to record the "scope start" address for
3093	 the tagged type.  */
3094
3095      tree d = build_decl (TYPE_DECL, NULL_TREE, type);
3096      TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
3097    }
3098
3099  timevar_pop (TV_NAME_LOOKUP);
3100}
3101
3102/* Counter used to create anonymous type names.  */
3103
3104static int anon_cnt = 0;
3105
3106/* Return an IDENTIFIER which can be used as a name for
3107   anonymous structs and unions.  */
3108
3109tree
3110make_anon_name ()
3111{
3112  char buf[32];
3113
3114  sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
3115  return get_identifier (buf);
3116}
3117
3118/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
3119   This keeps dbxout from getting confused.  */
3120
3121void
3122clear_anon_tags ()
3123{
3124  register struct cp_binding_level *b;
3125  static int last_cnt = 0;
3126
3127  /* Fast out if no new anon names were declared.  */
3128  if (last_cnt == anon_cnt)
3129    return;
3130
3131  b = current_binding_level;
3132  while (b->tag_transparent)
3133    b = b->level_chain;
3134  if (b->type_decls != NULL)
3135    binding_table_remove_anonymous_types (b->type_decls);
3136  last_cnt = anon_cnt;
3137}
3138
3139/* Subroutine of duplicate_decls: return truthvalue of whether
3140   or not types of these decls match.
3141
3142   For C++, we must compare the parameter list so that `int' can match
3143   `int&' in a parameter position, but `int&' is not confused with
3144   `const int&'.  */
3145
3146int
3147decls_match (newdecl, olddecl)
3148     tree newdecl, olddecl;
3149{
3150  int types_match;
3151
3152  if (newdecl == olddecl)
3153    return 1;
3154
3155  if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
3156    /* If the two DECLs are not even the same kind of thing, we're not
3157       interested in their types.  */
3158    return 0;
3159
3160  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3161    {
3162      tree f1 = TREE_TYPE (newdecl);
3163      tree f2 = TREE_TYPE (olddecl);
3164      tree p1 = TYPE_ARG_TYPES (f1);
3165      tree p2 = TYPE_ARG_TYPES (f2);
3166
3167      if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
3168	  && ! (DECL_EXTERN_C_P (newdecl)
3169		&& DECL_EXTERN_C_P (olddecl)))
3170	return 0;
3171
3172      if (TREE_CODE (f1) != TREE_CODE (f2))
3173        return 0;
3174
3175      if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
3176	{
3177	  if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
3178	      && (DECL_BUILT_IN (olddecl)
3179#ifndef NO_IMPLICIT_EXTERN_C
3180	          || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
3181	          || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
3182#endif
3183	      ))
3184	    {
3185	      types_match = self_promoting_args_p (p1);
3186	      if (p1 == void_list_node)
3187		TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3188	    }
3189#ifndef NO_IMPLICIT_EXTERN_C
3190	  else if (p1 == NULL_TREE
3191		   && (DECL_EXTERN_C_P (olddecl)
3192	               && DECL_IN_SYSTEM_HEADER (olddecl)
3193	               && !DECL_CLASS_SCOPE_P (olddecl))
3194		   && (DECL_EXTERN_C_P (newdecl)
3195	               && DECL_IN_SYSTEM_HEADER (newdecl)
3196	               && !DECL_CLASS_SCOPE_P (newdecl)))
3197	    {
3198	      types_match = self_promoting_args_p (p2);
3199	      TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3200	    }
3201#endif
3202	  else
3203	    types_match = compparms (p1, p2);
3204	}
3205      else
3206	types_match = 0;
3207    }
3208  else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3209    {
3210      if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3211				DECL_TEMPLATE_PARMS (olddecl)))
3212	return 0;
3213
3214      if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
3215	  != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
3216	return 0;
3217
3218      if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3219	types_match = 1;
3220      else
3221	types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
3222				   DECL_TEMPLATE_RESULT (newdecl));
3223    }
3224  else
3225    {
3226      if (TREE_TYPE (newdecl) == error_mark_node)
3227	types_match = TREE_TYPE (olddecl) == error_mark_node;
3228      else if (TREE_TYPE (olddecl) == NULL_TREE)
3229	types_match = TREE_TYPE (newdecl) == NULL_TREE;
3230      else if (TREE_TYPE (newdecl) == NULL_TREE)
3231	types_match = 0;
3232      else
3233	types_match = comptypes (TREE_TYPE (newdecl),
3234				 TREE_TYPE (olddecl),
3235				 COMPARE_REDECLARATION);
3236    }
3237
3238  return types_match;
3239}
3240
3241/* If NEWDECL is `static' and an `extern' was seen previously,
3242   warn about it.  OLDDECL is the previous declaration.
3243
3244   Note that this does not apply to the C++ case of declaring
3245   a variable `extern const' and then later `const'.
3246
3247   Don't complain about built-in functions, since they are beyond
3248   the user's control.  */
3249
3250static void
3251warn_extern_redeclared_static (newdecl, olddecl)
3252     tree newdecl, olddecl;
3253{
3254  static const char *const explicit_extern_static_warning
3255    = "`%D' was declared `extern' and later `static'";
3256  static const char *const implicit_extern_static_warning
3257    = "`%D' was declared implicitly `extern' and later `static'";
3258
3259  tree name;
3260
3261  if (TREE_CODE (newdecl) == TYPE_DECL
3262      || TREE_CODE (newdecl) == TEMPLATE_DECL
3263      || TREE_CODE (newdecl) == CONST_DECL)
3264    return;
3265
3266  /* Don't get confused by static member functions; that's a different
3267     use of `static'.  */
3268  if (TREE_CODE (newdecl) == FUNCTION_DECL
3269      && DECL_STATIC_FUNCTION_P (newdecl))
3270    return;
3271
3272  /* If the old declaration was `static', or the new one isn't, then
3273     then everything is OK.  */
3274  if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
3275    return;
3276
3277  /* It's OK to declare a builtin function as `static'.  */
3278  if (TREE_CODE (olddecl) == FUNCTION_DECL
3279      && DECL_ARTIFICIAL (olddecl))
3280    return;
3281
3282  name = DECL_ASSEMBLER_NAME (newdecl);
3283  pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
3284	      ? implicit_extern_static_warning
3285	      : explicit_extern_static_warning, newdecl);
3286  cp_pedwarn_at ("previous declaration of `%D'", olddecl);
3287}
3288
3289/* Handle when a new declaration NEWDECL has the same name as an old
3290   one OLDDECL in the same binding contour.  Prints an error message
3291   if appropriate.
3292
3293   If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
3294   Otherwise, return 0.  */
3295
3296int
3297duplicate_decls (newdecl, olddecl)
3298     tree newdecl, olddecl;
3299{
3300  unsigned olddecl_uid = DECL_UID (olddecl);
3301  int olddecl_friend = 0, types_match = 0;
3302  int new_defines_function = 0;
3303
3304  if (newdecl == olddecl)
3305    return 1;
3306
3307  types_match = decls_match (newdecl, olddecl);
3308
3309  /* If either the type of the new decl or the type of the old decl is an
3310     error_mark_node, then that implies that we have already issued an
3311     error (earlier) for some bogus type specification, and in that case,
3312     it is rather pointless to harass the user with yet more error message
3313     about the same declaration, so just pretend the types match here.  */
3314  if (TREE_TYPE (newdecl) == error_mark_node
3315      || TREE_TYPE (olddecl) == error_mark_node)
3316    types_match = 1;
3317
3318  if (DECL_P (olddecl)
3319      && TREE_CODE (newdecl) == FUNCTION_DECL
3320      && TREE_CODE (olddecl) == FUNCTION_DECL
3321      && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
3322    {
3323      if (DECL_DECLARED_INLINE_P (newdecl)
3324	  && DECL_UNINLINABLE (newdecl)
3325	  && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3326	/* Already warned elsewhere.  */;
3327      else if (DECL_DECLARED_INLINE_P (olddecl)
3328	       && DECL_UNINLINABLE (olddecl)
3329	       && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3330	/* Already warned.  */;
3331      else if (DECL_DECLARED_INLINE_P (newdecl)
3332	       && DECL_UNINLINABLE (olddecl)
3333	       && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3334	{
3335	  warning_with_decl (newdecl,
3336			     "function `%s' redeclared as inline");
3337	  warning_with_decl (olddecl,
3338			     "previous declaration of function `%s' with attribute noinline");
3339	}
3340      else if (DECL_DECLARED_INLINE_P (olddecl)
3341	       && DECL_UNINLINABLE (newdecl)
3342	       && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3343	{
3344	  warning_with_decl (newdecl,
3345			     "function `%s' redeclared with attribute noinline");
3346	  warning_with_decl (olddecl,
3347			     "previous declaration of function `%s' was inline");
3348	}
3349    }
3350
3351  /* Check for redeclaration and other discrepancies.  */
3352  if (TREE_CODE (olddecl) == FUNCTION_DECL
3353      && DECL_ARTIFICIAL (olddecl))
3354    {
3355      if (TREE_CODE (newdecl) != FUNCTION_DECL)
3356	{
3357          /* Avoid warnings redeclaring anticipated built-ins.  */
3358          if (DECL_ANTICIPATED (olddecl))
3359            return 0;
3360
3361	  /* If you declare a built-in or predefined function name as static,
3362	     the old definition is overridden, but optionally warn this was a
3363	     bad choice of name.  */
3364	  if (! TREE_PUBLIC (newdecl))
3365	    {
3366	      if (warn_shadow)
3367		warning ("shadowing %s function `%#D'",
3368			    DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3369			    olddecl);
3370	      /* Discard the old built-in function.  */
3371	      return 0;
3372	    }
3373	  /* If the built-in is not ansi, then programs can override
3374	     it even globally without an error.  */
3375	  else if (! DECL_BUILT_IN (olddecl))
3376	    warning ("library function `%#D' redeclared as non-function `%#D'",
3377			olddecl, newdecl);
3378	  else
3379	    {
3380	      error ("declaration of `%#D'", newdecl);
3381	      error ("conflicts with built-in declaration `%#D'",
3382			olddecl);
3383	    }
3384	  return 0;
3385	}
3386      else if (!types_match)
3387	{
3388          /* Avoid warnings redeclaring anticipated built-ins.  */
3389          if (DECL_ANTICIPATED (olddecl))
3390            ;  /* Do nothing yet.  */
3391	  else if ((DECL_EXTERN_C_P (newdecl)
3392	       && DECL_EXTERN_C_P (olddecl))
3393	      || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3394			    TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3395	    {
3396	      /* A near match; override the builtin.  */
3397
3398	      if (TREE_PUBLIC (newdecl))
3399		{
3400		  warning ("new declaration `%#D'", newdecl);
3401		  warning ("ambiguates built-in declaration `%#D'",
3402			      olddecl);
3403		}
3404	      else if (warn_shadow)
3405		warning ("shadowing %s function `%#D'",
3406			    DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3407			    olddecl);
3408	    }
3409	  else
3410	    /* Discard the old built-in function.  */
3411	    return 0;
3412
3413	  /* Replace the old RTL to avoid problems with inlining.  */
3414	  SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3415	}
3416      /* Even if the types match, prefer the new declarations type
3417	 for anitipated built-ins, for exception lists, etc...  */
3418      else if (DECL_ANTICIPATED (olddecl))
3419	TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3420
3421      if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3422	{
3423	  /* If a builtin function is redeclared as `static', merge
3424	     the declarations, but make the original one static.  */
3425	  DECL_THIS_STATIC (olddecl) = 1;
3426	  TREE_PUBLIC (olddecl) = 0;
3427
3428	  /* Make the old declaration consistent with the new one so
3429	     that all remnants of the builtin-ness of this function
3430	     will be banished.  */
3431	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3432	  SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3433	}
3434    }
3435  else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3436    {
3437      if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3438	   && TREE_CODE (newdecl) != TYPE_DECL
3439	   && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3440		 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3441	  || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3442	      && TREE_CODE (olddecl) != TYPE_DECL
3443	      && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3444		    && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3445			== TYPE_DECL))))
3446	{
3447	  /* We do nothing special here, because C++ does such nasty
3448	     things with TYPE_DECLs.  Instead, just let the TYPE_DECL
3449	     get shadowed, and know that if we need to find a TYPE_DECL
3450	     for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3451	     slot of the identifier.  */
3452	  return 0;
3453	}
3454
3455      if ((TREE_CODE (newdecl) == FUNCTION_DECL
3456	   && DECL_FUNCTION_TEMPLATE_P (olddecl))
3457	  || (TREE_CODE (olddecl) == FUNCTION_DECL
3458	      && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3459	return 0;
3460
3461      error ("`%#D' redeclared as different kind of symbol", newdecl);
3462      if (TREE_CODE (olddecl) == TREE_LIST)
3463	olddecl = TREE_VALUE (olddecl);
3464      cp_error_at ("previous declaration of `%#D'", olddecl);
3465
3466      /* New decl is completely inconsistent with the old one =>
3467	 tell caller to replace the old one.  */
3468
3469      return 0;
3470    }
3471  else if (!types_match)
3472    {
3473      if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3474	/* These are certainly not duplicate declarations; they're
3475	   from different scopes.  */
3476	return 0;
3477
3478      if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3479	{
3480	  /* The name of a class template may not be declared to refer to
3481	     any other template, class, function, object, namespace, value,
3482	     or type in the same scope.  */
3483	  if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3484	      || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3485	    {
3486	      error ("declaration of template `%#D'", newdecl);
3487	      cp_error_at ("conflicts with previous declaration `%#D'",
3488			   olddecl);
3489	    }
3490	  else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3491		   && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3492		   && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3493				 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3494		   && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3495					   DECL_TEMPLATE_PARMS (olddecl))
3496		   /* Template functions can be disambiguated by
3497		      return type.  */
3498		   && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3499				   TREE_TYPE (TREE_TYPE (olddecl))))
3500	    {
3501	      error ("new declaration `%#D'", newdecl);
3502	      cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3503	    }
3504	  return 0;
3505	}
3506      if (TREE_CODE (newdecl) == FUNCTION_DECL)
3507	{
3508	  if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3509	    {
3510	      error ("declaration of C function `%#D' conflicts with",
3511			newdecl);
3512	      cp_error_at ("previous declaration `%#D' here", olddecl);
3513	    }
3514	  else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3515			      TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3516	    {
3517	      error ("new declaration `%#D'", newdecl);
3518	      cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3519	    }
3520	  else
3521	    return 0;
3522	}
3523
3524      /* Already complained about this, so don't do so again.  */
3525      else if (current_class_type == NULL_TREE
3526	  || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3527	{
3528	  error ("conflicting types for `%#D'", newdecl);
3529	  cp_error_at ("previous declaration as `%#D'", olddecl);
3530	}
3531    }
3532  else if (TREE_CODE (newdecl) == FUNCTION_DECL
3533	    && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3534		 && (!DECL_TEMPLATE_INFO (newdecl)
3535		     || (DECL_TI_TEMPLATE (newdecl)
3536			 != DECL_TI_TEMPLATE (olddecl))))
3537		|| (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3538		    && (!DECL_TEMPLATE_INFO (olddecl)
3539			|| (DECL_TI_TEMPLATE (olddecl)
3540			    != DECL_TI_TEMPLATE (newdecl))))))
3541    /* It's OK to have a template specialization and a non-template
3542       with the same type, or to have specializations of two
3543       different templates with the same type.  Note that if one is a
3544       specialization, and the other is an instantiation of the same
3545       template, that we do not exit at this point.  That situation
3546       can occur if we instantiate a template class, and then
3547       specialize one of its methods.  This situation is valid, but
3548       the declarations must be merged in the usual way.  */
3549    return 0;
3550  else if (TREE_CODE (newdecl) == FUNCTION_DECL
3551	   && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3552		&& !DECL_USE_TEMPLATE (newdecl))
3553	       || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3554		   && !DECL_USE_TEMPLATE (olddecl))))
3555    /* One of the declarations is a template instantiation, and the
3556       other is not a template at all.  That's OK.  */
3557    return 0;
3558  else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3559           && DECL_NAMESPACE_ALIAS (newdecl)
3560           && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3561    /* Redeclaration of namespace alias, ignore it.  */
3562    return 1;
3563  else
3564    {
3565      const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3566      if (errmsg)
3567	{
3568	  error (errmsg, newdecl);
3569	  if (DECL_NAME (olddecl) != NULL_TREE)
3570	    cp_error_at ((DECL_INITIAL (olddecl)
3571			  && namespace_bindings_p ())
3572			 ? "`%#D' previously defined here"
3573			 : "`%#D' previously declared here", olddecl);
3574	  return 0;
3575	}
3576      else if (TREE_CODE (olddecl) == FUNCTION_DECL
3577	       && DECL_INITIAL (olddecl) != NULL_TREE
3578	       && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3579	       && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3580	{
3581	  /* Prototype decl follows defn w/o prototype.  */
3582	  cp_warning_at ("prototype for `%#D'", newdecl);
3583	  cp_warning_at ("follows non-prototype definition here", olddecl);
3584	}
3585      else if (TREE_CODE (olddecl) == FUNCTION_DECL
3586	       && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3587	{
3588	  /* extern "C" int foo ();
3589	     int foo () { bar (); }
3590	     is OK.  */
3591	  if (current_lang_depth () == 0)
3592	    SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3593	  else
3594	    {
3595	      cp_error_at ("previous declaration of `%#D' with %L linkage",
3596			   olddecl, DECL_LANGUAGE (olddecl));
3597	      error ("conflicts with new declaration with %L linkage",
3598			DECL_LANGUAGE (newdecl));
3599	    }
3600	}
3601
3602      if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3603	;
3604      else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3605	{
3606	  tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3607	  tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3608	  int i = 1;
3609
3610	  if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3611	    t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3612
3613	  for (; t1 && t1 != void_list_node;
3614	       t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3615	    if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3616	      {
3617		if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3618					   TREE_PURPOSE (t2)))
3619		  {
3620		    pedwarn ("default argument given for parameter %d of `%#D'",
3621			     i, newdecl);
3622		    cp_pedwarn_at ("after previous specification in `%#D'",
3623			           olddecl);
3624		  }
3625		else
3626		  {
3627		    error ("default argument given for parameter %d of `%#D'",
3628			      i, newdecl);
3629		    cp_error_at ("after previous specification in `%#D'",
3630				 olddecl);
3631		  }
3632	      }
3633
3634	  if (DECL_DECLARED_INLINE_P (newdecl)
3635	      && ! DECL_DECLARED_INLINE_P (olddecl)
3636	      && TREE_ADDRESSABLE (olddecl) && warn_inline)
3637	    {
3638	      warning ("`%#D' was used before it was declared inline",
3639			  newdecl);
3640	      cp_warning_at ("previous non-inline declaration here",
3641			     olddecl);
3642	    }
3643	}
3644    }
3645
3646  /* Do not merge an implicit typedef with an explicit one.  In:
3647
3648       class A;
3649       ...
3650       typedef class A A __attribute__ ((foo));
3651
3652     the attribute should apply only to the typedef.  */
3653  if (TREE_CODE (olddecl) == TYPE_DECL
3654      && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3655	  || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3656    return 0;
3657
3658  /* If new decl is `static' and an `extern' was seen previously,
3659     warn about it.  */
3660  warn_extern_redeclared_static (newdecl, olddecl);
3661
3662  /* We have committed to returning 1 at this point.  */
3663  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3664    {
3665      /* Now that functions must hold information normally held
3666	 by field decls, there is extra work to do so that
3667	 declaration information does not get destroyed during
3668	 definition.  */
3669      if (DECL_VINDEX (olddecl))
3670	DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3671      if (DECL_CONTEXT (olddecl))
3672	DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3673      DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3674      DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3675      DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3676      DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3677      DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3678      DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3679      if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3680	SET_OVERLOADED_OPERATOR_CODE
3681	  (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3682      new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3683
3684      /* Optionally warn about more than one declaration for the same
3685         name, but don't warn about a function declaration followed by a
3686         definition.  */
3687      if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3688	  && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3689	  /* Don't warn about extern decl followed by definition.  */
3690	  && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3691	  /* Don't warn about friends, let add_friend take care of it.  */
3692	  && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3693	{
3694	  warning ("redundant redeclaration of `%D' in same scope", newdecl);
3695	  cp_warning_at ("previous declaration of `%D'", olddecl);
3696	}
3697    }
3698
3699  /* Deal with C++: must preserve virtual function table size.  */
3700  if (TREE_CODE (olddecl) == TYPE_DECL)
3701    {
3702      register tree newtype = TREE_TYPE (newdecl);
3703      register tree oldtype = TREE_TYPE (olddecl);
3704
3705      if (newtype != error_mark_node && oldtype != error_mark_node
3706	  && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3707	CLASSTYPE_FRIEND_CLASSES (newtype)
3708	  = CLASSTYPE_FRIEND_CLASSES (oldtype);
3709\
3710      DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3711    }
3712
3713  /* Copy all the DECL_... slots specified in the new decl
3714     except for any that we copy here from the old type.  */
3715  DECL_ATTRIBUTES (newdecl)
3716    = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3717
3718  if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3719    {
3720      TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3721      DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3722	= chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3723		   DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3724
3725      /* If the new declaration is a definition, update the file and
3726	 line information on the declaration.  */
3727      if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3728	  && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3729	{
3730	  DECL_SOURCE_LOCATION (olddecl)
3731	    = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3732	    = DECL_SOURCE_LOCATION (newdecl);
3733	}
3734
3735      return 1;
3736    }
3737
3738  if (types_match)
3739    {
3740      /* Automatically handles default parameters.  */
3741      tree oldtype = TREE_TYPE (olddecl);
3742      tree newtype;
3743
3744      /* Merge the data types specified in the two decls.  */
3745      newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3746
3747      /* If merge_types produces a non-typedef type, just use the old type.  */
3748      if (TREE_CODE (newdecl) == TYPE_DECL
3749	  && newtype == DECL_ORIGINAL_TYPE (newdecl))
3750	newtype = oldtype;
3751
3752      if (TREE_CODE (newdecl) == VAR_DECL)
3753	{
3754	  DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3755	  DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3756	}
3757
3758      /* Do this after calling `merge_types' so that default
3759	 parameters don't confuse us.  */
3760      else if (TREE_CODE (newdecl) == FUNCTION_DECL
3761	  && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3762	      != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3763	{
3764	  TREE_TYPE (newdecl) = build_exception_variant (newtype,
3765							 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3766	  TREE_TYPE (olddecl) = build_exception_variant (newtype,
3767							 TYPE_RAISES_EXCEPTIONS (oldtype));
3768
3769	  if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3770	      && DECL_SOURCE_LINE (olddecl) != 0
3771	      && flag_exceptions
3772	      && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3773	                             TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3774	    {
3775	      error ("declaration of `%F' throws different exceptions",
3776			newdecl);
3777	      cp_error_at ("than previous declaration `%F'", olddecl);
3778	    }
3779	}
3780      TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3781
3782      /* Lay the type out, unless already done.  */
3783      if (! same_type_p (newtype, oldtype)
3784	  && TREE_TYPE (newdecl) != error_mark_node
3785	  && !(processing_template_decl && uses_template_parms (newdecl)))
3786	layout_type (TREE_TYPE (newdecl));
3787
3788      if ((TREE_CODE (newdecl) == VAR_DECL
3789	   || TREE_CODE (newdecl) == PARM_DECL
3790	   || TREE_CODE (newdecl) == RESULT_DECL
3791	   || TREE_CODE (newdecl) == FIELD_DECL
3792	   || TREE_CODE (newdecl) == TYPE_DECL)
3793	  && !(processing_template_decl && uses_template_parms (newdecl)))
3794	layout_decl (newdecl, 0);
3795
3796      /* Merge the type qualifiers.  */
3797      if (TREE_READONLY (newdecl))
3798	TREE_READONLY (olddecl) = 1;
3799      if (TREE_THIS_VOLATILE (newdecl))
3800	TREE_THIS_VOLATILE (olddecl) = 1;
3801
3802      /* Merge the initialization information.  */
3803      if (DECL_INITIAL (newdecl) == NULL_TREE
3804	  && DECL_INITIAL (olddecl) != NULL_TREE)
3805	{
3806	  DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3807	  DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3808	  if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3809	      && DECL_LANG_SPECIFIC (newdecl)
3810	      && DECL_LANG_SPECIFIC (olddecl))
3811	    DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3812	}
3813
3814      /* Merge the section attribute.
3815         We want to issue an error if the sections conflict but that must be
3816	 done later in decl_attributes since we are called before attributes
3817	 are assigned.  */
3818      if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3819	DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3820
3821      if (TREE_CODE (newdecl) == FUNCTION_DECL)
3822	{
3823	  DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3824	    |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3825	  DECL_NO_LIMIT_STACK (newdecl)
3826	    |= DECL_NO_LIMIT_STACK (olddecl);
3827	  /* Keep the old RTL.  */
3828	  COPY_DECL_RTL (olddecl, newdecl);
3829	}
3830      else if (TREE_CODE (newdecl) == VAR_DECL
3831	       && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3832	{
3833	  /* Keep the old RTL.  We cannot keep the old RTL if the old
3834	     declaration was for an incomplete object and the new
3835	     declaration is not since many attributes of the RTL will
3836	     change.  */
3837	  COPY_DECL_RTL (olddecl, newdecl);
3838	}
3839    }
3840  /* If cannot merge, then use the new type and qualifiers,
3841     and don't preserve the old rtl.  */
3842  else
3843    {
3844      /* Clean out any memory we had of the old declaration.  */
3845      tree oldstatic = value_member (olddecl, static_aggregates);
3846      if (oldstatic)
3847	TREE_VALUE (oldstatic) = error_mark_node;
3848
3849      TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3850      TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3851      TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3852      TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3853    }
3854
3855  /* Merge the storage class information.  */
3856  merge_weak (newdecl, olddecl);
3857
3858  DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3859  DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3860  TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3861  TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3862  if (! DECL_EXTERNAL (olddecl))
3863    DECL_EXTERNAL (newdecl) = 0;
3864
3865  if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3866    {
3867      DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3868      DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3869      DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3870      DECL_TEMPLATE_INSTANTIATED (newdecl)
3871	|= DECL_TEMPLATE_INSTANTIATED (olddecl);
3872      /* Don't really know how much of the language-specific
3873	 values we should copy from old to new.  */
3874      DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3875      DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3876	DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3877      DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3878      DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3879      DECL_INITIALIZED_IN_CLASS_P (newdecl)
3880        |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3881      olddecl_friend = DECL_FRIEND_P (olddecl);
3882
3883      /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3884      if (TREE_CODE (newdecl) == FUNCTION_DECL
3885	  || DECL_FUNCTION_TEMPLATE_P (newdecl))
3886	{
3887	  DECL_BEFRIENDING_CLASSES (newdecl)
3888	    = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3889		       DECL_BEFRIENDING_CLASSES (olddecl));
3890	  /* DECL_THUNKS is only valid for virtual functions,
3891	     otherwise it is a DECL_FRIEND_CONTEXT.  */
3892	  if (DECL_VIRTUAL_P (newdecl))
3893	    DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3894	}
3895    }
3896
3897  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3898    {
3899      if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3900	  && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3901	{
3902	  /* If newdecl is not a specialization, then it is not a
3903	     template-related function at all.  And that means that we
3904	     shoud have exited above, returning 0.  */
3905	  my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3906			      0);
3907
3908	  if (TREE_USED (olddecl))
3909	    /* From [temp.expl.spec]:
3910
3911	       If a template, a member template or the member of a class
3912	       template is explicitly specialized then that
3913	       specialization shall be declared before the first use of
3914	       that specialization that would cause an implicit
3915	       instantiation to take place, in every translation unit in
3916	       which such a use occurs.  */
3917	    error ("explicit specialization of %D after first use",
3918		      olddecl);
3919
3920	  SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3921
3922	  /* [temp.expl.spec/14] We don't inline explicit specialization
3923	     just because the primary template says so.  */
3924	}
3925      else
3926	{
3927	  if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3928	    DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3929
3930	  DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3931
3932	  /* If either decl says `inline', this fn is inline, unless
3933	     its definition was passed already.  */
3934	  if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3935	    DECL_INLINE (olddecl) = 1;
3936	  DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3937
3938	  DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3939	    = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3940	}
3941
3942      /* Preserve abstractness on cloned [cd]tors.  */
3943      DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3944
3945      if (! types_match)
3946	{
3947	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3948	  COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3949	  SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3950	}
3951      if (! types_match || new_defines_function)
3952	{
3953	  /* These need to be copied so that the names are available.
3954	     Note that if the types do match, we'll preserve inline
3955	     info and other bits, but if not, we won't.  */
3956	  DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3957	  DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3958	}
3959      if (new_defines_function)
3960	/* If defining a function declared with other language
3961	   linkage, use the previously declared language linkage.  */
3962	SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3963      else if (types_match)
3964	{
3965	  /* If redeclaring a builtin function, and not a definition,
3966	     it stays built in.  */
3967	  if (DECL_BUILT_IN (olddecl))
3968	    {
3969	      DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3970	      DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3971	      /* If we're keeping the built-in definition, keep the rtl,
3972		 regardless of declaration matches.  */
3973	      SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3974	    }
3975	  else
3976	    DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3977
3978	  DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3979	  /* Don't clear out the arguments if we're redefining a function.  */
3980	  if (DECL_ARGUMENTS (olddecl))
3981	    DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3982	}
3983    }
3984  else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3985    NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3986
3987  /* Now preserve various other info from the definition.  */
3988  TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3989  TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3990  DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3991  COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3992
3993  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3994    {
3995      int function_size;
3996
3997      function_size = sizeof (struct tree_decl);
3998
3999      memcpy ((char *) olddecl + sizeof (struct tree_common),
4000	      (char *) newdecl + sizeof (struct tree_common),
4001	      function_size - sizeof (struct tree_common));
4002
4003      if (DECL_TEMPLATE_INSTANTIATION (newdecl))
4004	{
4005	  /* If newdecl is a template instantiation, it is possible that
4006	     the following sequence of events has occurred:
4007
4008	     o A friend function was declared in a class template.  The
4009	     class template was instantiated.
4010
4011	     o The instantiation of the friend declaration was
4012	     recorded on the instantiation list, and is newdecl.
4013
4014	     o Later, however, instantiate_class_template called pushdecl
4015	     on the newdecl to perform name injection.  But, pushdecl in
4016	     turn called duplicate_decls when it discovered that another
4017	     declaration of a global function with the same name already
4018	     existed.
4019
4020	     o Here, in duplicate_decls, we decided to clobber newdecl.
4021
4022	     If we're going to do that, we'd better make sure that
4023	     olddecl, and not newdecl, is on the list of
4024	     instantiations so that if we try to do the instantiation
4025	     again we won't get the clobbered declaration.  */
4026
4027	  tree tmpl = DECL_TI_TEMPLATE (newdecl);
4028	  tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4029
4030	  for (; decls; decls = TREE_CHAIN (decls))
4031	    if (TREE_VALUE (decls) == newdecl)
4032	      TREE_VALUE (decls) = olddecl;
4033	}
4034    }
4035  else
4036    {
4037      memcpy ((char *) olddecl + sizeof (struct tree_common),
4038	      (char *) newdecl + sizeof (struct tree_common),
4039	      sizeof (struct tree_decl) - sizeof (struct tree_common)
4040	      + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
4041    }
4042
4043  DECL_UID (olddecl) = olddecl_uid;
4044  if (olddecl_friend)
4045    DECL_FRIEND_P (olddecl) = 1;
4046
4047  /* NEWDECL contains the merged attribute lists.
4048     Update OLDDECL to be the same.  */
4049  DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
4050
4051  return 1;
4052}
4053
4054/* Record a decl-node X as belonging to the current lexical scope.
4055   Check for errors (such as an incompatible declaration for the same
4056   name already seen in the same scope).
4057
4058   Returns either X or an old decl for the same name.
4059   If an old decl is returned, it may have been smashed
4060   to agree with what X says.  */
4061
4062tree
4063pushdecl (x)
4064     tree x;
4065{
4066  register tree t;
4067  register tree name;
4068  int need_new_binding;
4069
4070  timevar_push (TV_NAME_LOOKUP);
4071
4072  /* We shouldn't be calling pushdecl when we're generating RTL for a
4073     function that we already did semantic analysis on previously.  */
4074  my_friendly_assert (!cfun || doing_semantic_analysis_p (),
4075		      19990913);
4076
4077  need_new_binding = 1;
4078
4079  if (DECL_TEMPLATE_PARM_P (x))
4080    /* Template parameters have no context; they are not X::T even
4081       when declared within a class or namespace.  */
4082    ;
4083  else
4084    {
4085      if (current_function_decl && x != current_function_decl
4086	  /* A local declaration for a function doesn't constitute
4087             nesting.  */
4088	  && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
4089	  /* A local declaration for an `extern' variable is in the
4090	     scope of the current namespace, not the current
4091	     function.  */
4092	  && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
4093	  && !DECL_CONTEXT (x))
4094	DECL_CONTEXT (x) = current_function_decl;
4095
4096      /* If this is the declaration for a namespace-scope function,
4097	 but the declaration itself is in a local scope, mark the
4098	 declaration.  */
4099      if (TREE_CODE (x) == FUNCTION_DECL
4100	  && DECL_NAMESPACE_SCOPE_P (x)
4101	  && current_function_decl
4102	  && x != current_function_decl)
4103	DECL_LOCAL_FUNCTION_P (x) = 1;
4104    }
4105
4106  name = DECL_NAME (x);
4107  if (name)
4108    {
4109      int different_binding_level = 0;
4110
4111      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4112	name = TREE_OPERAND (name, 0);
4113
4114      /* In case this decl was explicitly namespace-qualified, look it
4115	 up in its namespace context.  */
4116      if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
4117	  && namespace_bindings_p ())
4118	t = namespace_binding (name, DECL_CONTEXT (x));
4119      else
4120	t = lookup_name_current_level (name);
4121
4122      /* [basic.link] If there is a visible declaration of an entity
4123	 with linkage having the same name and type, ignoring entities
4124	 declared outside the innermost enclosing namespace scope, the
4125	 block scope declaration declares that same entity and
4126	 receives the linkage of the previous declaration.  */
4127      if (! t && current_function_decl && x != current_function_decl
4128	  && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
4129	  && DECL_EXTERNAL (x))
4130	{
4131	  /* Look in block scope.  */
4132	  t = IDENTIFIER_VALUE (name);
4133	  /* Or in the innermost namespace.  */
4134	  if (! t)
4135	    t = namespace_binding (name, DECL_CONTEXT (x));
4136	  /* Does it have linkage?  Note that if this isn't a DECL, it's an
4137	     OVERLOAD, which is OK.  */
4138	  if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
4139	    t = NULL_TREE;
4140	  if (t)
4141	    different_binding_level = 1;
4142	}
4143
4144      /* If we are declaring a function, and the result of name-lookup
4145	 was an OVERLOAD, look for an overloaded instance that is
4146	 actually the same as the function we are declaring.  (If
4147	 there is one, we have to merge our declaration with the
4148	 previous declaration.)  */
4149      if (t && TREE_CODE (t) == OVERLOAD)
4150	{
4151	  tree match;
4152
4153	  if (TREE_CODE (x) == FUNCTION_DECL)
4154	    for (match = t; match; match = OVL_NEXT (match))
4155	      {
4156		if (decls_match (OVL_CURRENT (match), x))
4157		  break;
4158	      }
4159	  else
4160	    /* Just choose one.  */
4161	    match = t;
4162
4163	  if (match)
4164	    t = OVL_CURRENT (match);
4165	  else
4166	    t = NULL_TREE;
4167	}
4168
4169      if (t == error_mark_node)
4170	{
4171	  /* error_mark_node is 0 for a while during initialization!  */
4172	  t = NULL_TREE;
4173	  cp_error_at ("`%#D' used prior to declaration", x);
4174	}
4175      else if (t != NULL_TREE)
4176	{
4177	  if (different_binding_level)
4178	    {
4179	      if (decls_match (x, t))
4180		/* The standard only says that the local extern
4181		   inherits linkage from the previous decl; in
4182		   particular, default args are not shared.  It would
4183		   be nice to propagate inlining info, though.  FIXME.  */
4184		TREE_PUBLIC (x) = TREE_PUBLIC (t);
4185	    }
4186	  else if (TREE_CODE (t) == PARM_DECL)
4187	    {
4188	      if (DECL_CONTEXT (t) == NULL_TREE)
4189		/* This is probaby caused by too many errors, but calling
4190		   abort will say that if errors have occurred.  */
4191		abort ();
4192
4193	      /* Check for duplicate params.  */
4194	      if (duplicate_decls (x, t))
4195		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4196	    }
4197	  else if ((DECL_EXTERN_C_FUNCTION_P (x)
4198		    || DECL_FUNCTION_TEMPLATE_P (x))
4199		   && is_overloaded_fn (t))
4200	    /* Don't do anything just yet.  */;
4201	  else if (t == wchar_decl_node)
4202	    {
4203	      if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
4204		pedwarn ("redeclaration of `wchar_t' as `%T'",
4205			    TREE_TYPE (x));
4206
4207	      /* Throw away the redeclaration.  */
4208	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4209	    }
4210	  else if (TREE_CODE (t) != TREE_CODE (x))
4211	    {
4212	      if (duplicate_decls (x, t))
4213		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4214	    }
4215	  else if (duplicate_decls (x, t))
4216	    {
4217	      if (TREE_CODE (t) == TYPE_DECL)
4218		SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
4219	      else if (TREE_CODE (t) == FUNCTION_DECL)
4220		check_default_args (t);
4221
4222	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4223	    }
4224	  else if (DECL_MAIN_P (x))
4225	    {
4226	      /* A redeclaration of main, but not a duplicate of the
4227		 previous one.
4228
4229		 [basic.start.main]
4230
4231	         This function shall not be overloaded.  */
4232	      cp_error_at ("invalid redeclaration of `%D'", t);
4233	      error ("as `%D'", x);
4234	      /* We don't try to push this declaration since that
4235		 causes a crash.  */
4236	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4237	    }
4238	}
4239
4240      check_template_shadow (x);
4241
4242      /* If this is a function conjured up by the backend, massage it
4243	 so it looks friendly.  */
4244      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
4245	{
4246	  retrofit_lang_decl (x);
4247	  SET_DECL_LANGUAGE (x, lang_c);
4248	}
4249
4250      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
4251	{
4252	  t = push_overloaded_decl (x, PUSH_LOCAL);
4253	  if (t != x)
4254	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4255	  if (!namespace_bindings_p ())
4256	    /* We do not need to create a binding for this name;
4257	       push_overloaded_decl will have already done so if
4258	       necessary.  */
4259	    need_new_binding = 0;
4260	}
4261      else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
4262	{
4263	  t = push_overloaded_decl (x, PUSH_GLOBAL);
4264	  if (t == x)
4265	    add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
4266	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4267	}
4268
4269      /* If declaring a type as a typedef, copy the type (unless we're
4270	 at line 0), and install this TYPE_DECL as the new type's typedef
4271	 name.  See the extensive comment in ../c-decl.c (pushdecl).  */
4272      if (TREE_CODE (x) == TYPE_DECL)
4273	{
4274	  tree type = TREE_TYPE (x);
4275	  if (DECL_SOURCE_LINE (x) == 0)
4276            {
4277	      if (TYPE_NAME (type) == 0)
4278	        TYPE_NAME (type) = x;
4279            }
4280          else if (type != error_mark_node && TYPE_NAME (type) != x
4281		   /* We don't want to copy the type when all we're
4282		      doing is making a TYPE_DECL for the purposes of
4283		      inlining.  */
4284		   && (!TYPE_NAME (type)
4285		       || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
4286            {
4287	      DECL_ORIGINAL_TYPE (x) = type;
4288              type = build_type_copy (type);
4289	      TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
4290              TYPE_NAME (type) = x;
4291              TREE_TYPE (x) = type;
4292            }
4293
4294	  if (type != error_mark_node
4295	      && TYPE_NAME (type)
4296	      && TYPE_IDENTIFIER (type))
4297            set_identifier_type_value_with_scope (DECL_NAME (x), type,
4298						  current_binding_level);
4299
4300	}
4301
4302      /* Multiple external decls of the same identifier ought to match.
4303
4304	 We get warnings about inline functions where they are defined.
4305	 We get warnings about other functions from push_overloaded_decl.
4306
4307	 Avoid duplicate warnings where they are used.  */
4308      if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
4309	{
4310	  tree decl;
4311
4312	  decl = IDENTIFIER_NAMESPACE_VALUE (name);
4313	  if (decl && TREE_CODE (decl) == OVERLOAD)
4314	    decl = OVL_FUNCTION (decl);
4315
4316	  if (decl && decl != error_mark_node
4317	      && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
4318	      /* If different sort of thing, we already gave an error.  */
4319	      && TREE_CODE (decl) == TREE_CODE (x)
4320	      && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
4321	    {
4322	      pedwarn ("type mismatch with previous external decl", x);
4323	      cp_pedwarn_at ("previous external decl of `%#D'", decl);
4324	    }
4325	}
4326
4327      /* This name is new in its binding level.
4328	 Install the new declaration and return it.  */
4329      if (namespace_bindings_p ())
4330	{
4331	  /* Install a global value.  */
4332
4333	  /* If the first global decl has external linkage,
4334	     warn if we later see static one.  */
4335	  if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
4336	    TREE_PUBLIC (name) = 1;
4337
4338 	  /* Bind the name for the entity.  */
4339 	  if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
4340  		&& t != NULL_TREE)
4341 	      && (TREE_CODE (x) == TYPE_DECL
4342 		  || TREE_CODE (x) == VAR_DECL
4343 		  || TREE_CODE (x) == NAMESPACE_DECL
4344 		  || TREE_CODE (x) == CONST_DECL
4345 		  || TREE_CODE (x) == TEMPLATE_DECL))
4346 	    SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
4347
4348	  /* Don't forget if the function was used via an implicit decl.  */
4349	  if (IDENTIFIER_IMPLICIT_DECL (name)
4350	      && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4351	    TREE_USED (x) = 1;
4352
4353	  /* Don't forget if its address was taken in that way.  */
4354	  if (IDENTIFIER_IMPLICIT_DECL (name)
4355	      && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4356	    TREE_ADDRESSABLE (x) = 1;
4357
4358	  /* Warn about mismatches against previous implicit decl.  */
4359	  if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4360	      /* If this real decl matches the implicit, don't complain.  */
4361	      && ! (TREE_CODE (x) == FUNCTION_DECL
4362		    && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
4363	    warning
4364	      ("`%D' was previously implicitly declared to return `int'", x);
4365
4366	  /* If new decl is `static' and an `extern' was seen previously,
4367	     warn about it.  */
4368	  if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4369	    warn_extern_redeclared_static (x, t);
4370	}
4371      else
4372	{
4373	  /* Here to install a non-global value.  */
4374	  tree oldlocal = IDENTIFIER_VALUE (name);
4375	  tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
4376
4377	  if (need_new_binding)
4378	    {
4379	      push_local_binding (name, x, 0);
4380	      /* Because push_local_binding will hook X on to the
4381		 current_binding_level's name list, we don't want to
4382		 do that again below.  */
4383	      need_new_binding = 0;
4384	    }
4385
4386	  /* If this is a TYPE_DECL, push it into the type value slot.  */
4387	  if (TREE_CODE (x) == TYPE_DECL)
4388	    set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4389						  current_binding_level);
4390
4391	  /* Clear out any TYPE_DECL shadowed by a namespace so that
4392	     we won't think this is a type.  The C struct hack doesn't
4393	     go through namespaces.  */
4394	  if (TREE_CODE (x) == NAMESPACE_DECL)
4395	    set_identifier_type_value_with_scope (name, NULL_TREE,
4396						  current_binding_level);
4397
4398	  if (oldlocal)
4399	    {
4400	      tree d = oldlocal;
4401
4402	      while (oldlocal
4403		     && TREE_CODE (oldlocal) == VAR_DECL
4404		     && DECL_DEAD_FOR_LOCAL (oldlocal))
4405		oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4406
4407	      if (oldlocal == NULL_TREE)
4408		oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4409	    }
4410
4411	  /* If this is an extern function declaration, see if we
4412	     have a global definition or declaration for the function.  */
4413	  if (oldlocal == NULL_TREE
4414	      && DECL_EXTERNAL (x)
4415	      && oldglobal != NULL_TREE
4416	      && TREE_CODE (x) == FUNCTION_DECL
4417	      && TREE_CODE (oldglobal) == FUNCTION_DECL)
4418	    {
4419	      /* We have one.  Their types must agree.  */
4420	      if (decls_match (x, oldglobal))
4421		/* OK */;
4422	      else
4423		{
4424		  warning ("extern declaration of `%#D' doesn't match", x);
4425		  cp_warning_at ("global declaration `%#D'", oldglobal);
4426		}
4427	    }
4428	  /* If we have a local external declaration,
4429	     and no file-scope declaration has yet been seen,
4430	     then if we later have a file-scope decl it must not be static.  */
4431	  if (oldlocal == NULL_TREE
4432	      && oldglobal == NULL_TREE
4433	      && DECL_EXTERNAL (x)
4434	      && TREE_PUBLIC (x))
4435	    TREE_PUBLIC (name) = 1;
4436
4437	  /* Warn if shadowing an argument at the top level of the body.  */
4438	  if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4439	      /* Inline decls shadow nothing.  */
4440	      && !DECL_FROM_INLINE (x)
4441	      && TREE_CODE (oldlocal) == PARM_DECL
4442	      /* Don't check the `this' parameter.  */
4443	      && !DECL_ARTIFICIAL (oldlocal))
4444	    {
4445	      bool err = false;
4446
4447	      /* Don't complain if it's from an enclosing function.  */
4448	      if (DECL_CONTEXT (oldlocal) == current_function_decl
4449		  && TREE_CODE (x) != PARM_DECL)
4450		{
4451		  /* Go to where the parms should be and see if we find
4452		     them there.  */
4453		  struct cp_binding_level *b = current_binding_level->level_chain;
4454
4455		  /* ARM $8.3 */
4456		  if (b->parm_flag == 1)
4457		    {
4458		      error ("declaration of `%#D' shadows a parameter",
4459				name);
4460		      err = true;
4461		    }
4462		}
4463
4464	      if (warn_shadow && !err)
4465		shadow_warning ("a parameter", name, oldlocal);
4466	    }
4467
4468	  /* Maybe warn if shadowing something else.  */
4469	  else if (warn_shadow && !DECL_EXTERNAL (x)
4470	      /* No shadow warnings for internally generated vars.  */
4471	      && ! DECL_ARTIFICIAL (x)
4472	      /* No shadow warnings for vars made for inlining.  */
4473	      && ! DECL_FROM_INLINE (x))
4474	    {
4475	      if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4476		       && current_class_ptr
4477		       && !TREE_STATIC (name))
4478		warning ("declaration of `%s' shadows a member of `this'",
4479			    IDENTIFIER_POINTER (name));
4480	      else if (oldlocal != NULL_TREE
4481		       && TREE_CODE (oldlocal) == VAR_DECL)
4482		shadow_warning ("a previous local", name, oldlocal);
4483	      else if (oldglobal != NULL_TREE
4484		       && TREE_CODE (oldglobal) == VAR_DECL)
4485		/* XXX shadow warnings in outer-more namespaces */
4486		shadow_warning ("a global declaration", name, oldglobal);
4487	    }
4488	}
4489
4490      if (TREE_CODE (x) == FUNCTION_DECL)
4491	check_default_args (x);
4492
4493      if (TREE_CODE (x) == VAR_DECL)
4494	maybe_register_incomplete_var (x);
4495    }
4496
4497  if (need_new_binding)
4498    add_decl_to_level (x,
4499		       DECL_NAMESPACE_SCOPE_P (x)
4500		       ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4501		       : current_binding_level);
4502
4503  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4504}
4505
4506/* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4507   caller to set DECL_CONTEXT properly.  */
4508
4509static tree
4510pushdecl_with_scope (x, level)
4511     tree x;
4512     struct cp_binding_level *level;
4513{
4514  register struct cp_binding_level *b;
4515  tree function_decl = current_function_decl;
4516
4517  timevar_push (TV_NAME_LOOKUP);
4518
4519  current_function_decl = NULL_TREE;
4520  if (level->parm_flag == 2)
4521    {
4522      b = class_binding_level;
4523      class_binding_level = level;
4524      pushdecl_class_level (x);
4525      class_binding_level = b;
4526    }
4527  else
4528    {
4529      b = current_binding_level;
4530      current_binding_level = level;
4531      x = pushdecl (x);
4532      current_binding_level = b;
4533    }
4534  current_function_decl = function_decl;
4535  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4536}
4537
4538/* Like pushdecl, only it places X in the current namespace,
4539   if appropriate.  */
4540
4541tree
4542pushdecl_namespace_level (x)
4543     tree x;
4544{
4545  register struct cp_binding_level *b = current_binding_level;
4546  register tree t;
4547
4548  timevar_push (TV_NAME_LOOKUP);
4549  t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4550
4551  /* Now, the type_shadowed stack may screw us.  Munge it so it does
4552     what we want.  */
4553  if (TREE_CODE (x) == TYPE_DECL)
4554    {
4555      tree name = DECL_NAME (x);
4556      tree newval;
4557      tree *ptr = (tree *)0;
4558      for (; !global_scope_p (b); b = b->level_chain)
4559        {
4560          tree shadowed = b->type_shadowed;
4561          for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4562            if (TREE_PURPOSE (shadowed) == name)
4563              {
4564		ptr = &TREE_VALUE (shadowed);
4565		/* Can't break out of the loop here because sometimes
4566		   a binding level will have duplicate bindings for
4567		   PT names.  It's gross, but I haven't time to fix it.  */
4568              }
4569        }
4570      newval = TREE_TYPE (x);
4571      if (ptr == (tree *)0)
4572        {
4573          /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4574             up here if this is changed to an assertion.  --KR  */
4575	  SET_IDENTIFIER_TYPE_VALUE (name, newval);
4576	}
4577      else
4578        {
4579	  *ptr = newval;
4580        }
4581    }
4582  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4583}
4584
4585/* Like pushdecl, only it places X in the global scope if appropriate.
4586   Calls cp_finish_decl to register the variable, initializing it with
4587   *INIT, if INIT is non-NULL.  */
4588
4589static tree
4590pushdecl_top_level_1 (tree x, tree *init)
4591{
4592  timevar_push (TV_NAME_LOOKUP);
4593  push_to_top_level ();
4594  x = pushdecl_namespace_level (x);
4595  if (init)
4596    cp_finish_decl (x, *init, NULL_TREE, 0);
4597  pop_from_top_level ();
4598  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4599}
4600
4601/* Like pushdecl, only it places X in the global scope if appropriate.  */
4602
4603tree
4604pushdecl_top_level (tree x)
4605{
4606  return pushdecl_top_level_1 (x, NULL);
4607}
4608
4609/* Like pushdecl, only it places X in the global scope if
4610   appropriate.  Calls cp_finish_decl to register the variable,
4611   initializing it with INIT.  */
4612
4613tree
4614pushdecl_top_level_and_finish (tree x, tree init)
4615{
4616  return pushdecl_top_level_1 (x, &init);
4617}
4618
4619/* Make the declaration of X appear in CLASS scope.  */
4620
4621bool
4622pushdecl_class_level (x)
4623     tree x;
4624{
4625  tree name;
4626  bool is_valid = true;
4627
4628  timevar_push (TV_NAME_LOOKUP);
4629  /* Get the name of X.  */
4630  if (TREE_CODE (x) == OVERLOAD)
4631    name = DECL_NAME (get_first_fn (x));
4632  else
4633    name = DECL_NAME (x);
4634
4635  if (name)
4636    {
4637      is_valid = push_class_level_binding (name, x);
4638      if (TREE_CODE (x) == TYPE_DECL)
4639	set_identifier_type_value (name, TREE_TYPE (x));
4640    }
4641  else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4642    {
4643      /* If X is an anonymous aggregate, all of its members are
4644	 treated as if they were members of the class containing the
4645	 aggregate, for naming purposes.  */
4646      tree f;
4647
4648      for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4649	{
4650	  push_srcloc (DECL_SOURCE_FILE (f), DECL_SOURCE_LINE (f));
4651	  if (!pushdecl_class_level (f))
4652	    is_valid = false;
4653	  pop_srcloc ();
4654	}
4655    }
4656  timevar_pop (TV_NAME_LOOKUP);
4657
4658  return is_valid;
4659}
4660
4661/* Enter DECL into the symbol table, if that's appropriate.  Returns
4662   DECL, or a modified version thereof.  */
4663
4664tree
4665maybe_push_decl (decl)
4666     tree decl;
4667{
4668  tree type = TREE_TYPE (decl);
4669
4670  /* Add this decl to the current binding level, but not if it comes
4671     from another scope, e.g. a static member variable.  TEM may equal
4672     DECL or it may be a previous decl of the same name.  */
4673  if (decl == error_mark_node
4674      || (TREE_CODE (decl) != PARM_DECL
4675	  && DECL_CONTEXT (decl) != NULL_TREE
4676	  /* Definitions of namespace members outside their namespace are
4677	     possible.  */
4678	  && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4679      || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4680      || TREE_CODE (type) == UNKNOWN_TYPE
4681      /* The declaration of a template specialization does not affect
4682	 the functions available for overload resolution, so we do not
4683	 call pushdecl.  */
4684      || (TREE_CODE (decl) == FUNCTION_DECL
4685	  && DECL_TEMPLATE_SPECIALIZATION (decl)))
4686    return decl;
4687  else
4688    return pushdecl (decl);
4689}
4690
4691/* Make the declaration(s) of X appear in CLASS scope under the name
4692   NAME.  Returns true if the binding is valid.  */
4693
4694bool
4695push_class_level_binding (tree name, tree x)
4696{
4697  cxx_binding *binding;
4698
4699  timevar_push (TV_NAME_LOOKUP);
4700  /* The class_binding_level will be NULL if x is a template
4701     parameter name in a member template.  */
4702  if (!class_binding_level)
4703    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4704
4705  /* Make sure that this new member does not have the same name
4706     as a template parameter.  */
4707  if (TYPE_BEING_DEFINED (current_class_type))
4708    check_template_shadow (x);
4709
4710  /* If this declaration shadows a declaration from an enclosing
4711     class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4712     we leave this class.  Record the shadowed declaration here.  */
4713  binding = IDENTIFIER_BINDING (name);
4714  if (binding
4715      && ((TREE_CODE (x) == OVERLOAD
4716	   && BINDING_VALUE (binding)
4717	   && is_overloaded_fn (BINDING_VALUE (binding)))
4718	  || INHERITED_VALUE_BINDING_P (binding)))
4719    {
4720      tree shadow;
4721      tree old_decl;
4722
4723      /* If the old binding was from a base class, and was for a tag
4724	 name, slide it over to make room for the new binding.  The
4725	 old binding is still visible if explicitly qualified with a
4726	 class-key.  */
4727      if (INHERITED_VALUE_BINDING_P (binding)
4728	  && BINDING_VALUE (binding)
4729	  && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4730	  && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4731	  && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4732	{
4733	  old_decl = BINDING_TYPE (binding);
4734	  BINDING_TYPE (binding) = BINDING_VALUE (binding);
4735	  BINDING_VALUE (binding) = NULL_TREE;
4736	  INHERITED_VALUE_BINDING_P (binding) = 0;
4737	}
4738      else
4739	old_decl = BINDING_VALUE (binding);
4740
4741      /* Find the previous binding of name on the class-shadowed
4742         list, and update it.  */
4743      for (shadow = class_binding_level->class_shadowed;
4744	   shadow;
4745	   shadow = TREE_CHAIN (shadow))
4746	if (TREE_PURPOSE (shadow) == name
4747	    && TREE_TYPE (shadow) == old_decl)
4748	  {
4749	    BINDING_VALUE (binding) = x;
4750	    INHERITED_VALUE_BINDING_P (binding) = 0;
4751	    TREE_TYPE (shadow) = x;
4752	    IDENTIFIER_CLASS_VALUE (name) = x;
4753	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4754	  }
4755    }
4756
4757  /* If we didn't replace an existing binding, put the binding on the
4758     stack of bindings for the identifier, and update the shadowed list.  */
4759  if (push_class_binding (name, x))
4760    {
4761      class_binding_level->class_shadowed
4762	= tree_cons (name, NULL,
4763		     class_binding_level->class_shadowed);
4764      /* Record the value we are binding NAME to so that we can know
4765	 what to pop later.  */
4766      TREE_TYPE (class_binding_level->class_shadowed) = x;
4767      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4768    }
4769
4770  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
4771}
4772
4773/* Insert another USING_DECL into the current binding level, returning
4774   this declaration. If this is a redeclaration, do nothing, and
4775   return NULL_TREE if this not in namespace scope (in namespace
4776   scope, a using decl might extend any previous bindings).  */
4777
4778tree
4779push_using_decl (scope, name)
4780     tree scope;
4781     tree name;
4782{
4783  tree decl;
4784
4785  timevar_push (TV_NAME_LOOKUP);
4786
4787  my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4788  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4789  for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4790    if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4791      break;
4792  if (decl)
4793    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4794			    namespace_bindings_p () ? decl : NULL_TREE);
4795  decl = build_lang_decl (USING_DECL, name, void_type_node);
4796  DECL_INITIAL (decl) = scope;
4797  TREE_CHAIN (decl) = current_binding_level->usings;
4798  current_binding_level->usings = decl;
4799  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4800}
4801
4802/* Add namespace to using_directives. Return NULL_TREE if nothing was
4803   changed (i.e. there was already a directive), or the fresh
4804   TREE_LIST otherwise.  */
4805
4806tree
4807push_using_directive (used)
4808     tree used;
4809{
4810  tree ud = current_binding_level->using_directives;
4811  tree iter, ancestor;
4812
4813  timevar_push (TV_NAME_LOOKUP);
4814
4815  /* Check if we already have this.  */
4816  if (purpose_member (used, ud) != NULL_TREE)
4817    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4818
4819  ancestor = namespace_ancestor (current_decl_namespace (), used);
4820  ud = current_binding_level->using_directives;
4821  ud = tree_cons (used, ancestor, ud);
4822  current_binding_level->using_directives = ud;
4823
4824  /* Recursively add all namespaces used.  */
4825  for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4826    push_using_directive (TREE_PURPOSE (iter));
4827
4828  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4829}
4830
4831/* DECL is a FUNCTION_DECL for a non-member function, which may have
4832   other definitions already in place.  We get around this by making
4833   the value of the identifier point to a list of all the things that
4834   want to be referenced by that name.  It is then up to the users of
4835   that name to decide what to do with that list.
4836
4837   DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4838   DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4839
4840   FLAGS is a bitwise-or of the following values:
4841     PUSH_LOCAL: Bind DECL in the current scope, rather than at
4842                 namespace scope.
4843     PUSH_USING: DECL is being pushed as the result of a using
4844                 declaration.
4845
4846   The value returned may be a previous declaration if we guessed wrong
4847   about what language DECL should belong to (C or C++).  Otherwise,
4848   it's always DECL (and never something that's not a _DECL).  */
4849
4850tree
4851push_overloaded_decl (decl, flags)
4852     tree decl;
4853     int flags;
4854{
4855  tree name = DECL_NAME (decl);
4856  tree old;
4857  tree new_binding;
4858  int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4859
4860  timevar_push (TV_NAME_LOOKUP);
4861
4862  if (doing_global)
4863    old = namespace_binding (name, DECL_CONTEXT (decl));
4864  else
4865    old = lookup_name_current_level (name);
4866
4867  if (old)
4868    {
4869      if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4870	{
4871	  tree t = TREE_TYPE (old);
4872	  if (IS_AGGR_TYPE (t) && warn_shadow
4873	      && (! DECL_IN_SYSTEM_HEADER (decl)
4874		  || ! DECL_IN_SYSTEM_HEADER (old)))
4875	    warning ("`%#D' hides constructor for `%#T'", decl, t);
4876	  old = NULL_TREE;
4877	}
4878      else if (is_overloaded_fn (old))
4879        {
4880          tree tmp;
4881
4882	  for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4883	    {
4884	      tree fn = OVL_CURRENT (tmp);
4885
4886	      if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4887		  && !(flags & PUSH_USING)
4888		  && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4889				TYPE_ARG_TYPES (TREE_TYPE (decl))))
4890		error ("`%#D' conflicts with previous using declaration `%#D'",
4891			  decl, fn);
4892
4893	      if (duplicate_decls (decl, fn))
4894		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4895	    }
4896	}
4897      else if (old == error_mark_node)
4898	/* Ignore the undefined symbol marker.  */
4899	old = NULL_TREE;
4900      else
4901	{
4902	  cp_error_at ("previous non-function declaration `%#D'", old);
4903	  error ("conflicts with function declaration `%#D'", decl);
4904	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4905	}
4906    }
4907
4908  if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4909    {
4910      if (old && TREE_CODE (old) != OVERLOAD)
4911	new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4912      else
4913	new_binding = ovl_cons (decl, old);
4914      if (flags & PUSH_USING)
4915	OVL_USED (new_binding) = 1;
4916    }
4917  else
4918    /* NAME is not ambiguous.  */
4919    new_binding = decl;
4920
4921  if (doing_global)
4922    set_namespace_binding (name, current_namespace, new_binding);
4923  else
4924    {
4925      /* We only create an OVERLOAD if there was a previous binding at
4926	 this level, or if decl is a template. In the former case, we
4927	 need to remove the old binding and replace it with the new
4928	 binding.  We must also run through the NAMES on the binding
4929	 level where the name was bound to update the chain.  */
4930
4931      if (TREE_CODE (new_binding) == OVERLOAD && old)
4932	{
4933	  tree *d;
4934
4935	  for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4936	       *d;
4937	       d = &TREE_CHAIN (*d))
4938	    if (*d == old
4939		|| (TREE_CODE (*d) == TREE_LIST
4940		    && TREE_VALUE (*d) == old))
4941	      {
4942		if (TREE_CODE (*d) == TREE_LIST)
4943		  /* Just replace the old binding with the new.  */
4944		  TREE_VALUE (*d) = new_binding;
4945		else
4946		  /* Build a TREE_LIST to wrap the OVERLOAD.  */
4947		  *d = tree_cons (NULL_TREE, new_binding,
4948				  TREE_CHAIN (*d));
4949
4950		/* And update the cxx_binding node.  */
4951		BINDING_VALUE (IDENTIFIER_BINDING (name))
4952		  = new_binding;
4953		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4954	      }
4955
4956	  /* We should always find a previous binding in this case.  */
4957	  abort ();
4958	}
4959
4960      /* Install the new binding.  */
4961      push_local_binding (name, new_binding, flags);
4962    }
4963
4964  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4965}
4966
4967/* Generate an implicit declaration for identifier FUNCTIONID
4968   as a function of type int ().  Print a warning if appropriate.  */
4969
4970tree
4971implicitly_declare (functionid)
4972     tree functionid;
4973{
4974  register tree decl;
4975
4976  /* We used to reuse an old implicit decl here,
4977     but this loses with inline functions because it can clobber
4978     the saved decl chains.  */
4979  decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4980
4981  DECL_EXTERNAL (decl) = 1;
4982  TREE_PUBLIC (decl) = 1;
4983
4984  /* ISO standard says implicit declarations are in the innermost block.
4985     So we record the decl in the standard fashion.  */
4986  pushdecl (decl);
4987  rest_of_decl_compilation (decl, NULL, 0, 0);
4988
4989  if (warn_implicit
4990      /* Only one warning per identifier.  */
4991      && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4992    {
4993      pedwarn ("implicit declaration of function `%#D'", decl);
4994    }
4995
4996  SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4997
4998  return decl;
4999}
5000
5001/* Return zero if the declaration NEWDECL is valid
5002   when the declaration OLDDECL (assumed to be for the same name)
5003   has already been seen.
5004   Otherwise return an error message format string with a %s
5005   where the identifier should go.  */
5006
5007static const char *
5008redeclaration_error_message (newdecl, olddecl)
5009     tree newdecl, olddecl;
5010{
5011  if (TREE_CODE (newdecl) == TYPE_DECL)
5012    {
5013      /* Because C++ can put things into name space for free,
5014	 constructs like "typedef struct foo { ... } foo"
5015	 would look like an erroneous redeclaration.  */
5016      if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
5017	return 0;
5018      else
5019	return "redefinition of `%#D'";
5020    }
5021  else if (TREE_CODE (newdecl) == FUNCTION_DECL)
5022    {
5023      /* If this is a pure function, its olddecl will actually be
5024	 the original initialization to `0' (which we force to call
5025	 abort()).  Don't complain about redefinition in this case.  */
5026      if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
5027	return 0;
5028
5029      /* If both functions come from different namespaces, this is not
5030	 a redeclaration - this is a conflict with a used function.  */
5031      if (DECL_NAMESPACE_SCOPE_P (olddecl)
5032	  && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
5033	return "`%D' conflicts with used function";
5034
5035      /* We'll complain about linkage mismatches in
5036         warn_extern_redeclared_static.  */
5037
5038      /* Defining the same name twice is no good.  */
5039      if (DECL_INITIAL (olddecl) != NULL_TREE
5040	  && DECL_INITIAL (newdecl) != NULL_TREE)
5041	{
5042	  if (DECL_NAME (olddecl) == NULL_TREE)
5043	    return "`%#D' not declared in class";
5044	  else
5045	    return "redefinition of `%#D'";
5046	}
5047      return 0;
5048    }
5049  else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
5050    {
5051      if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
5052	   && (DECL_TEMPLATE_RESULT (newdecl)
5053	       != DECL_TEMPLATE_RESULT (olddecl))
5054	   && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
5055	   && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
5056	  || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
5057	      && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
5058	      && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
5059	return "redefinition of `%#D'";
5060      return 0;
5061    }
5062  else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
5063    {
5064      /* Objects declared at top level:  */
5065      /* If at least one is a reference, it's ok.  */
5066      if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
5067	return 0;
5068      /* Reject two definitions.  */
5069      return "redefinition of `%#D'";
5070    }
5071  else
5072    {
5073      /* Objects declared with block scope:  */
5074      /* Reject two definitions, and reject a definition
5075	 together with an external reference.  */
5076      if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
5077	return "redeclaration of `%#D'";
5078      return 0;
5079    }
5080}
5081
5082/* Create a new label, named ID.  */
5083
5084static tree
5085make_label_decl (id, local_p)
5086     tree id;
5087     int local_p;
5088{
5089  tree decl;
5090
5091  decl = build_decl (LABEL_DECL, id, void_type_node);
5092  if (expanding_p)
5093    /* Make sure every label has an rtx.  */
5094    label_rtx (decl);
5095
5096  DECL_CONTEXT (decl) = current_function_decl;
5097  DECL_MODE (decl) = VOIDmode;
5098  C_DECLARED_LABEL_FLAG (decl) = local_p;
5099
5100  /* Say where one reference is to the label, for the sake of the
5101     error if it is not defined.  */
5102  DECL_SOURCE_LINE (decl) = lineno;
5103  DECL_SOURCE_FILE (decl) = input_filename;
5104
5105  /* Record the fact that this identifier is bound to this label.  */
5106  SET_IDENTIFIER_LABEL_VALUE (id, decl);
5107
5108  return decl;
5109}
5110
5111/* Record this label on the list of used labels so that we can check
5112   at the end of the function to see whether or not the label was
5113   actually defined, and so we can check when the label is defined whether
5114   this use is valid.  */
5115
5116static void
5117use_label (decl)
5118     tree decl;
5119{
5120  if (named_label_uses == NULL
5121      || named_label_uses->names_in_scope != current_binding_level->names
5122      || named_label_uses->label_decl != decl)
5123    {
5124      struct named_label_use_list *new_ent;
5125      new_ent = ((struct named_label_use_list *)
5126		 ggc_alloc (sizeof (struct named_label_use_list)));
5127      new_ent->label_decl = decl;
5128      new_ent->names_in_scope = current_binding_level->names;
5129      new_ent->binding_level = current_binding_level;
5130      new_ent->lineno_o_goto = lineno;
5131      new_ent->filename_o_goto = input_filename;
5132      new_ent->next = named_label_uses;
5133      named_label_uses = new_ent;
5134    }
5135}
5136
5137/* Look for a label named ID in the current function.  If one cannot
5138   be found, create one.  (We keep track of used, but undefined,
5139   labels, and complain about them at the end of a function.)  */
5140
5141tree
5142lookup_label (id)
5143     tree id;
5144{
5145  tree decl;
5146  struct named_label_list *ent;
5147
5148  timevar_push (TV_NAME_LOOKUP);
5149
5150  /* You can't use labels at global scope.  */
5151  if (current_function_decl == NULL_TREE)
5152    {
5153      error ("label `%s' referenced outside of any function",
5154	     IDENTIFIER_POINTER (id));
5155      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5156    }
5157
5158  /* See if we've already got this label.  */
5159  decl = IDENTIFIER_LABEL_VALUE (id);
5160  if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
5161    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5162
5163  /* Record this label on the list of labels used in this function.
5164     We do this before calling make_label_decl so that we get the
5165     IDENTIFIER_LABEL_VALUE before the new label is declared.  */
5166  ent = ((struct named_label_list *)
5167	 ggc_alloc_cleared (sizeof (struct named_label_list)));
5168  ent->old_value = IDENTIFIER_LABEL_VALUE (id);
5169  ent->next = named_labels;
5170  named_labels = ent;
5171
5172  /* We need a new label.  */
5173  decl = make_label_decl (id, /*local_p=*/0);
5174
5175  /* Now fill in the information we didn't have before.  */
5176  ent->label_decl = decl;
5177
5178  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5179}
5180
5181/* Declare a local label named ID.  */
5182
5183tree
5184declare_local_label (id)
5185     tree id;
5186{
5187  tree decl;
5188
5189  /* Add a new entry to the SHADOWED_LABELS list so that when we leave
5190     this scope we can restore the old value of
5191     IDENTIFIER_TYPE_VALUE.  */
5192  current_binding_level->shadowed_labels
5193    = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
5194		 current_binding_level->shadowed_labels);
5195  /* Look for the label.  */
5196  decl = make_label_decl (id, /*local_p=*/1);
5197  /* Now fill in the information we didn't have before.  */
5198  TREE_VALUE (current_binding_level->shadowed_labels) = decl;
5199
5200  return decl;
5201}
5202
5203/* Returns nonzero if it is ill-formed to jump past the declaration of
5204   DECL.  Returns 2 if it's also a real problem.  */
5205
5206static int
5207decl_jump_unsafe (decl)
5208     tree decl;
5209{
5210  if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
5211    return 0;
5212
5213  if (DECL_INITIAL (decl) == NULL_TREE
5214      && pod_type_p (TREE_TYPE (decl)))
5215    return 0;
5216
5217  /* This is really only important if we're crossing an initialization.
5218     The POD stuff is just pedantry; why should it matter if the class
5219     contains a field of pointer to member type?  */
5220  if (DECL_INITIAL (decl)
5221      || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
5222    return 2;
5223  return 1;
5224}
5225
5226/* Check that a single previously seen jump to a newly defined label
5227   is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
5228   the jump context; NAMES are the names in scope in LEVEL at the jump
5229   context; FILE and LINE are the source position of the jump or 0.  */
5230
5231static void
5232check_previous_goto_1 (decl, level, names, file, line)
5233     tree decl;
5234     struct cp_binding_level *level;
5235     tree names;
5236     const char *file;
5237     int line;
5238{
5239  int identified = 0;
5240  int saw_eh = 0;
5241  struct cp_binding_level *b = current_binding_level;
5242  for (; b; b = b->level_chain)
5243    {
5244      tree new_decls = b->names;
5245      tree old_decls = (b == level ? names : NULL_TREE);
5246      for (; new_decls != old_decls;
5247	   new_decls = TREE_CHAIN (new_decls))
5248	{
5249	  int problem = decl_jump_unsafe (new_decls);
5250	  if (! problem)
5251	    continue;
5252
5253	  if (! identified)
5254	    {
5255	      if (decl)
5256		pedwarn ("jump to label `%D'", decl);
5257	      else
5258		pedwarn ("jump to case label");
5259
5260	      if (file)
5261		pedwarn_with_file_and_line (file, line, "  from here");
5262	      identified = 1;
5263	    }
5264
5265	  if (problem > 1)
5266	    cp_error_at ("  crosses initialization of `%#D'",
5267			 new_decls);
5268	  else
5269	    cp_pedwarn_at ("  enters scope of non-POD `%#D'",
5270			   new_decls);
5271	}
5272
5273      if (b == level)
5274	break;
5275      if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
5276	{
5277	  if (! identified)
5278	    {
5279	      if (decl)
5280		pedwarn ("jump to label `%D'", decl);
5281	      else
5282		pedwarn ("jump to case label");
5283
5284	      if (file)
5285		pedwarn_with_file_and_line (file, line, "  from here");
5286	      identified = 1;
5287	    }
5288	  if (b->is_try_scope)
5289	    error ("  enters try block");
5290	  else
5291	    error ("  enters catch block");
5292	  saw_eh = 1;
5293	}
5294    }
5295}
5296
5297static void
5298check_previous_goto (use)
5299     struct named_label_use_list *use;
5300{
5301  check_previous_goto_1 (use->label_decl, use->binding_level,
5302			 use->names_in_scope, use->filename_o_goto,
5303			 use->lineno_o_goto);
5304}
5305
5306static void
5307check_switch_goto (level)
5308     struct cp_binding_level *level;
5309{
5310  check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
5311}
5312
5313/* Check that any previously seen jumps to a newly defined label DECL
5314   are OK.  Called by define_label.  */
5315
5316static void
5317check_previous_gotos (decl)
5318     tree decl;
5319{
5320  struct named_label_use_list **usep;
5321
5322  if (! TREE_USED (decl))
5323    return;
5324
5325  for (usep = &named_label_uses; *usep; )
5326    {
5327      struct named_label_use_list *use = *usep;
5328      if (use->label_decl == decl)
5329	{
5330	  check_previous_goto (use);
5331	  *usep = use->next;
5332	}
5333      else
5334	usep = &(use->next);
5335    }
5336}
5337
5338/* Check that a new jump to a label DECL is OK.  Called by
5339   finish_goto_stmt.  */
5340
5341void
5342check_goto (decl)
5343     tree decl;
5344{
5345  int identified = 0;
5346  tree bad;
5347  struct named_label_list *lab;
5348
5349  /* We can't know where a computed goto is jumping.  So we assume
5350     that it's OK.  */
5351  if (! DECL_P (decl))
5352    return;
5353
5354  /* If the label hasn't been defined yet, defer checking.  */
5355  if (! DECL_INITIAL (decl))
5356    {
5357      use_label (decl);
5358      return;
5359    }
5360
5361  for (lab = named_labels; lab; lab = lab->next)
5362    if (decl == lab->label_decl)
5363      break;
5364
5365  /* If the label is not on named_labels it's a gcc local label, so
5366     it must be in an outer scope, so jumping to it is always OK.  */
5367  if (lab == 0)
5368    return;
5369
5370  if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
5371      && !identified)
5372    {
5373      cp_pedwarn_at ("jump to label `%D'", decl);
5374      pedwarn ("  from here");
5375      identified = 1;
5376    }
5377
5378  for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
5379    {
5380      tree b = TREE_VALUE (bad);
5381      int u = decl_jump_unsafe (b);
5382
5383      if (u > 1 && DECL_ARTIFICIAL (b))
5384	/* Can't skip init of __exception_info.  */
5385	cp_error_at ("  enters catch block", b);
5386      else if (u > 1)
5387	cp_error_at ("  skips initialization of `%#D'", b);
5388      else
5389	cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
5390    }
5391
5392  if (lab->in_try_scope)
5393    error ("  enters try block");
5394  else if (lab->in_catch_scope)
5395    error ("  enters catch block");
5396}
5397
5398/* Define a label, specifying the location in the source file.
5399   Return the LABEL_DECL node for the label, if the definition is valid.
5400   Otherwise return 0.  */
5401
5402tree
5403define_label (filename, line, name)
5404     const char *filename;
5405     int line;
5406     tree name;
5407{
5408  tree decl = lookup_label (name);
5409  struct named_label_list *ent;
5410  register struct cp_binding_level *p;
5411
5412  timevar_push (TV_NAME_LOOKUP);
5413
5414  for (ent = named_labels; ent; ent = ent->next)
5415    if (ent->label_decl == decl)
5416      break;
5417
5418  /* After labels, make any new cleanups in the function go into their
5419     own new (temporary) binding contour.  */
5420  for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5421    p->more_cleanups_ok = 0;
5422
5423  if (name == get_identifier ("wchar_t"))
5424    pedwarn ("label named wchar_t");
5425
5426  if (DECL_INITIAL (decl) != NULL_TREE)
5427    {
5428      error ("duplicate label `%D'", decl);
5429      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5430    }
5431  else
5432    {
5433      /* Mark label as having been defined.  */
5434      DECL_INITIAL (decl) = error_mark_node;
5435      /* Say where in the source.  */
5436      DECL_SOURCE_FILE (decl) = filename;
5437      DECL_SOURCE_LINE (decl) = line;
5438      if (ent)
5439	{
5440	  ent->names_in_scope = current_binding_level->names;
5441	  ent->binding_level = current_binding_level;
5442	}
5443      check_previous_gotos (decl);
5444      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5445    }
5446  timevar_pop (TV_NAME_LOOKUP);
5447}
5448
5449struct cp_switch
5450{
5451  struct cp_binding_level *level;
5452  struct cp_switch *next;
5453  /* The SWITCH_STMT being built.  */
5454  tree switch_stmt;
5455  /* A splay-tree mapping the low element of a case range to the high
5456     element, or NULL_TREE if there is no high element.  Used to
5457     determine whether or not a new case label duplicates an old case
5458     label.  We need a tree, rather than simply a hash table, because
5459     of the GNU case range extension.  */
5460  splay_tree cases;
5461};
5462
5463/* A stack of the currently active switch statements.  The innermost
5464   switch statement is on the top of the stack.  There is no need to
5465   mark the stack for garbage collection because it is only active
5466   during the processing of the body of a function, and we never
5467   collect at that point.  */
5468
5469static struct cp_switch *switch_stack;
5470
5471/* Called right after a switch-statement condition is parsed.
5472   SWITCH_STMT is the switch statement being parsed.  */
5473
5474void
5475push_switch (switch_stmt)
5476     tree switch_stmt;
5477{
5478  struct cp_switch *p
5479    = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5480  p->level = current_binding_level;
5481  p->next = switch_stack;
5482  p->switch_stmt = switch_stmt;
5483  p->cases = splay_tree_new (case_compare, NULL, NULL);
5484  switch_stack = p;
5485}
5486
5487void
5488pop_switch ()
5489{
5490  struct cp_switch *cs;
5491
5492  cs = switch_stack;
5493  splay_tree_delete (cs->cases);
5494  switch_stack = switch_stack->next;
5495  free (cs);
5496}
5497
5498/* Note that we've seen a definition of a case label, and complain if this
5499   is a bad place for one.  */
5500
5501tree
5502finish_case_label (low_value, high_value)
5503     tree low_value;
5504     tree high_value;
5505{
5506  tree cond, r;
5507  register struct cp_binding_level *p;
5508
5509  if (! switch_stack)
5510    {
5511      if (high_value)
5512	error ("case label not within a switch statement");
5513      else if (low_value)
5514	error ("case label `%E' not within a switch statement",
5515		  low_value);
5516      else
5517	error ("`default' label not within a switch statement");
5518      return NULL_TREE;
5519    }
5520
5521  if (processing_template_decl)
5522    {
5523      tree label;
5524
5525      /* For templates, just add the case label; we'll do semantic
5526	 analysis at instantiation-time.  */
5527      label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5528      return add_stmt (build_case_label (low_value, high_value, label));
5529    }
5530
5531  /* Find the condition on which this switch statement depends.  */
5532  cond = SWITCH_COND (switch_stack->switch_stmt);
5533  if (cond && TREE_CODE (cond) == TREE_LIST)
5534    cond = TREE_VALUE (cond);
5535
5536  r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5537  if (r == error_mark_node)
5538    r = NULL_TREE;
5539
5540  check_switch_goto (switch_stack->level);
5541
5542  /* After labels, make any new cleanups in the function go into their
5543     own new (temporary) binding contour.  */
5544  for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5545    p->more_cleanups_ok = 0;
5546
5547  return r;
5548}
5549
5550/* Return the list of declarations of the current level.
5551   Note that this list is in reverse order unless/until
5552   you nreverse it; and when you do nreverse it, you must
5553   store the result back using `storedecls' or you will lose.  */
5554
5555tree
5556getdecls ()
5557{
5558  return current_binding_level->names;
5559}
5560
5561/* Store the list of declarations of the current level.
5562   This is done for the parameter declarations of a function being defined,
5563   after they are modified in the light of any missing parameters.  */
5564
5565static void
5566storedecls (decls)
5567     tree decls;
5568{
5569  current_binding_level->names = decls;
5570}
5571
5572/* Set the current binding TABLE for type declarations..  This is a
5573   temporary workaround of the fact that the data structure classtypes
5574   does not currently carry its allocated cxx_scope structure.  */
5575void
5576cxx_remember_type_decls (binding_table table)
5577{
5578  current_binding_level->type_decls = table;
5579}
5580
5581
5582/* Return the type that should be used when TYPE's name is preceded
5583   by a tag such as 'struct' or 'union', or null if the name cannot
5584   be used in this way.
5585
5586   For example, when processing the third line of:
5587
5588	struct A;
5589	typedef struct A A;
5590	struct A;
5591
5592   lookup of A will find the typedef.  Given A's typedef, this function
5593   will return the type associated with "struct A".  For the tag to be
5594   anything other than TYPE, TYPE must be a typedef whose original type
5595   has the same name and context as TYPE itself.
5596
5597   It is not valid for a typedef of an anonymous type to be used with
5598   an explicit tag:
5599
5600       typedef struct { ... } B;
5601       struct B;
5602
5603   Return null for this case.  */
5604
5605static tree
5606follow_tag_typedef (type)
5607     tree type;
5608{
5609  tree original;
5610
5611  original = original_type (type);
5612  if (! TYPE_NAME (original))
5613    return NULL_TREE;
5614  if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5615      && (CP_DECL_CONTEXT (TYPE_NAME (original))
5616	  == CP_DECL_CONTEXT (TYPE_NAME (type)))
5617      && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5618    return original;
5619  else
5620    return NULL_TREE;
5621}
5622
5623/* Given NAME, an IDENTIFIER_NODE,
5624   return the structure (or union or enum) definition for that name.
5625   Searches binding levels from BINDING_LEVEL up to the global level.
5626   If THISLEVEL_ONLY is nonzero, searches only the specified context
5627   (but skips any tag-transparent contexts to find one that is
5628   meaningful for tags).
5629   FORM says which kind of type the caller wants;
5630   it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5631   If the wrong kind of type is found, and it's not a template, an error is
5632   reported.  */
5633
5634static tree
5635lookup_tag (form, name, binding_level, thislevel_only)
5636     enum tree_code form;
5637     tree name;
5638     struct cp_binding_level *binding_level;
5639     int thislevel_only;
5640{
5641  register struct cp_binding_level *level;
5642  /* Nonzero if, we should look past a template parameter level, even
5643     if THISLEVEL_ONLY.  */
5644  int allow_template_parms_p = 1;
5645  bool type_is_anonymous = ANON_AGGRNAME_P (name);
5646
5647  timevar_push (TV_NAME_LOOKUP);
5648
5649  for (level = binding_level; level; level = level->level_chain)
5650    {
5651      register tree tail;
5652      if (type_is_anonymous && level->type_decls != NULL)
5653        {
5654          tree type = binding_table_find_anon_type (level->type_decls, name);
5655          /* There's no need for error checking here, because
5656             anon names are unique throughout the compilation.  */
5657          if (type != NULL)
5658            POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5659        }
5660      else if (level->namespace_p)
5661	/* Do namespace lookup.  */
5662	for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5663	  {
5664	    cxx_binding *binding =
5665              cxx_scope_find_binding_for_name (tail, name);
5666            tree old;
5667
5668	    /* If we just skipped past a template parameter level,
5669	       even though THISLEVEL_ONLY, and we find a template
5670	       class declaration, then we use the _TYPE node for the
5671	       template.  See the example below.  */
5672	    if (thislevel_only && !allow_template_parms_p
5673		&& binding && BINDING_VALUE (binding)
5674		&& DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5675	      old = TREE_TYPE (BINDING_VALUE (binding));
5676	    else if (binding)
5677	      old = BINDING_TYPE (binding);
5678            else
5679              old = NULL;
5680
5681	    if (old)
5682	      {
5683		/* We've found something at this binding level.  If it is
5684		   a typedef, extract the tag it refers to.  Lookup fails
5685		   if the typedef doesn't refer to a taggable type.  */
5686		old = follow_tag_typedef (old);
5687		if (!old)
5688		  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5689		if (TREE_CODE (old) != form
5690		    && (form == ENUMERAL_TYPE
5691			|| TREE_CODE (old) == ENUMERAL_TYPE))
5692		  {
5693		    error ("`%#D' redeclared as %C", old, form);
5694		    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5695		  }
5696		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5697	      }
5698	    if (thislevel_only || tail == global_namespace)
5699	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5700	  }
5701      else if (level->type_decls != NULL)
5702        {
5703          binding_entry entry = binding_table_find (level->type_decls, name);
5704          if (entry != NULL)
5705            {
5706              enum tree_code code = TREE_CODE (entry->type);
5707
5708              if (code != form
5709                  && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5710                {
5711                  /* Definition isn't the kind we were looking for.  */
5712                  error ("`%#D' redeclared as %C", entry->type, form);
5713                  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5714                }
5715              POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
5716            }
5717        }
5718      if (thislevel_only && ! level->tag_transparent)
5719	{
5720	  if (level->template_parms_p && allow_template_parms_p)
5721	    {
5722	      /* We must deal with cases like this:
5723
5724	           template <class T> struct S;
5725		   template <class T> struct S {};
5726
5727		 When looking up `S', for the second declaration, we
5728		 would like to find the first declaration.  But, we
5729		 are in the pseudo-global level created for the
5730		 template parameters, rather than the (surrounding)
5731		 namespace level.  Thus, we keep going one more level,
5732		 even though THISLEVEL_ONLY is nonzero.  */
5733	      allow_template_parms_p = 0;
5734	      continue;
5735	    }
5736	  else
5737	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5738	}
5739    }
5740  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5741}
5742
5743#if 0
5744void
5745set_current_level_tags_transparency (tags_transparent)
5746     int tags_transparent;
5747{
5748  current_binding_level->tag_transparent = tags_transparent;
5749}
5750#endif
5751
5752/* Given a type, find the tag that was defined for it and return the tag name.
5753   Otherwise return 0.  However, the value can never be 0
5754   in the cases in which this is used.
5755
5756   C++: If NAME is nonzero, this is the new name to install.  This is
5757   done when replacing anonymous tags with real tag names.  */
5758
5759static tree
5760lookup_tag_reverse (type, name)
5761     tree type;
5762     tree name;
5763{
5764  register struct cp_binding_level *level;
5765
5766  timevar_push (TV_NAME_LOOKUP);
5767
5768  for (level = current_binding_level; level; level = level->level_chain)
5769    {
5770      binding_entry entry = level->type_decls == NULL
5771        ? NULL
5772        : binding_table_reverse_maybe_remap (level->type_decls, type, name);
5773      if (entry)
5774        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
5775    }
5776  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5777}
5778
5779/* Look up NAME in the NAMESPACE.  */
5780
5781tree
5782lookup_namespace_name (namespace, name)
5783     tree namespace, name;
5784{
5785  tree val;
5786  tree template_id = NULL_TREE;
5787  cxx_binding binding;
5788
5789  timevar_push (TV_NAME_LOOKUP);
5790
5791  my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5792
5793  if (TREE_CODE (name) == NAMESPACE_DECL)
5794    /* This happens for A::B<int> when B is a namespace.  */
5795    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5796  else if (TREE_CODE (name) == TEMPLATE_DECL)
5797    {
5798      /* This happens for A::B where B is a template, and there are no
5799	 template arguments.  */
5800      error ("invalid use of `%D'", name);
5801      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5802    }
5803
5804  namespace = ORIGINAL_NAMESPACE (namespace);
5805
5806  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5807    {
5808      template_id = name;
5809      name = TREE_OPERAND (name, 0);
5810      if (TREE_CODE (name) == OVERLOAD)
5811	name = DECL_NAME (OVL_CURRENT (name));
5812      else if (DECL_P (name))
5813	name = DECL_NAME (name);
5814    }
5815
5816  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5817
5818  cxx_binding_clear (&binding);
5819  if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5820    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5821
5822  if (binding.value)
5823    {
5824      val = binding.value;
5825
5826      if (template_id)
5827	{
5828	  if (DECL_CLASS_TEMPLATE_P (val))
5829	    val = lookup_template_class (val,
5830					 TREE_OPERAND (template_id, 1),
5831					 /*in_decl=*/NULL_TREE,
5832					 /*context=*/NULL_TREE,
5833					 /*entering_scope=*/0,
5834	                                 tf_error | tf_warning);
5835	  else if (DECL_FUNCTION_TEMPLATE_P (val)
5836		   || TREE_CODE (val) == OVERLOAD)
5837	    val = lookup_template_function (val,
5838					    TREE_OPERAND (template_id, 1));
5839	  else
5840	    {
5841	      error ("`%D::%D' is not a template",
5842			namespace, name);
5843	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5844	    }
5845	}
5846
5847      /* If we have a single function from a using decl, pull it out.  */
5848      if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5849	val = OVL_FUNCTION (val);
5850
5851      /* Ignore built-in functions that haven't been prototyped yet.  */
5852      if (!val || !DECL_P(val)
5853          || !DECL_LANG_SPECIFIC(val)
5854          || !DECL_ANTICIPATED (val))
5855        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5856    }
5857
5858  error ("`%D' undeclared in namespace `%D'", name, namespace);
5859  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5860}
5861
5862/* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5863
5864static hashval_t
5865typename_hash (k)
5866     const void * k;
5867{
5868  hashval_t hash;
5869  tree t = (tree) k;
5870
5871  hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5872	  ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5873
5874  return hash;
5875}
5876
5877/* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5878
5879static int
5880typename_compare (k1, k2)
5881     const void * k1;
5882     const void * k2;
5883{
5884  tree t1;
5885  tree t2;
5886  tree d1;
5887  tree d2;
5888
5889  t1 = (tree) k1;
5890  t2 = (tree) k2;
5891  d1 = TYPE_NAME (t1);
5892  d2 = TYPE_NAME (t2);
5893
5894  return (DECL_NAME (d1) == DECL_NAME (d2)
5895	  && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5896	  && ((TREE_TYPE (t1) != NULL_TREE)
5897	      == (TREE_TYPE (t2) != NULL_TREE))
5898	  && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5899	  && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5900}
5901
5902/* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5903   the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5904   is non-NULL, this type is being created by the implicit typename
5905   extension, and BASE_TYPE is a type named `t' in some base class of
5906   `T' which depends on template parameters.
5907
5908   Returns the new TYPENAME_TYPE.  */
5909
5910static GTY ((param_is (union tree_node))) htab_t typename_htab;
5911
5912tree
5913build_typename_type (context, name, fullname, base_type)
5914     tree context;
5915     tree name;
5916     tree fullname;
5917     tree base_type;
5918{
5919  tree t;
5920  tree d;
5921  PTR *e;
5922
5923  if (typename_htab == NULL)
5924    {
5925      typename_htab = htab_create_ggc (61, &typename_hash,
5926				       &typename_compare, NULL);
5927    }
5928
5929  /* Build the TYPENAME_TYPE.  */
5930  t = make_aggr_type (TYPENAME_TYPE);
5931  TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5932  TYPENAME_TYPE_FULLNAME (t) = fullname;
5933  TREE_TYPE (t) = base_type;
5934
5935  /* Build the corresponding TYPE_DECL.  */
5936  d = build_decl (TYPE_DECL, name, t);
5937  TYPE_NAME (TREE_TYPE (d)) = d;
5938  TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5939  DECL_CONTEXT (d) = FROB_CONTEXT (context);
5940  DECL_ARTIFICIAL (d) = 1;
5941
5942  /* See if we already have this type.  */
5943  e = htab_find_slot (typename_htab, t, INSERT);
5944  if (*e)
5945    t = (tree) *e;
5946  else
5947    *e = t;
5948
5949  return t;
5950}
5951
5952/* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5953   unless an error occurs, in which case error_mark_node is returned.
5954   If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5955   set, we return that, rather than the _TYPE it corresponds to, in
5956   other cases we look through the type decl.  If TF_ERROR is set,
5957   complain about errors, otherwise be quiet.  */
5958
5959tree
5960make_typename_type (context, name, complain)
5961     tree context, name;
5962     tsubst_flags_t complain;
5963{
5964  tree fullname;
5965
5966  if (TYPE_P (name))
5967    {
5968      if (!(TYPE_LANG_SPECIFIC (name)
5969	    && (CLASSTYPE_IS_TEMPLATE (name)
5970		|| CLASSTYPE_USE_TEMPLATE (name))))
5971	name = TYPE_IDENTIFIER (name);
5972      else
5973	/* Create a TEMPLATE_ID_EXPR for the type.  */
5974	name = build_nt (TEMPLATE_ID_EXPR,
5975			 CLASSTYPE_TI_TEMPLATE (name),
5976			 CLASSTYPE_TI_ARGS (name));
5977    }
5978  else if (TREE_CODE (name) == TYPE_DECL)
5979    name = DECL_NAME (name);
5980
5981  fullname = name;
5982
5983  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5984    {
5985      name = TREE_OPERAND (name, 0);
5986      if (TREE_CODE (name) == TEMPLATE_DECL)
5987	name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5988    }
5989  if (TREE_CODE (name) == TEMPLATE_DECL)
5990    {
5991      error ("`%D' used without template parameters", name);
5992      return error_mark_node;
5993    }
5994  if (TREE_CODE (name) != IDENTIFIER_NODE)
5995    abort ();
5996
5997  if (TREE_CODE (context) == NAMESPACE_DECL)
5998    {
5999      /* We can get here from typename_sub0 in the explicit_template_type
6000	 expansion.  Just fail.  */
6001      if (complain & tf_error)
6002	error ("no class template named `%#T' in `%#T'",
6003		  name, context);
6004      return error_mark_node;
6005    }
6006
6007  if (! uses_template_parms (context)
6008      || currently_open_class (context))
6009    {
6010      if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
6011	{
6012	  tree tmpl = NULL_TREE;
6013	  if (IS_AGGR_TYPE (context))
6014	    tmpl = lookup_field (context, name, 0, 0);
6015	  if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6016	    {
6017	      if (complain & tf_error)
6018		error ("no class template named `%#T' in `%#T'",
6019			  name, context);
6020	      return error_mark_node;
6021	    }
6022
6023	  if (complain & tf_error)
6024	    {
6025	      if (complain & tf_parsing)
6026		type_access_control (context, tmpl);
6027	      else
6028		enforce_access (context, tmpl);
6029	    }
6030
6031	  return lookup_template_class (tmpl,
6032					TREE_OPERAND (fullname, 1),
6033					NULL_TREE, context,
6034					/*entering_scope=*/0,
6035	                                tf_error | tf_warning);
6036	}
6037      else
6038	{
6039          tree t;
6040
6041	  if (!IS_AGGR_TYPE (context))
6042	    {
6043	      if (complain & tf_error)
6044		error ("no type named `%#T' in `%#T'", name, context);
6045	      return error_mark_node;
6046	    }
6047
6048	  t = lookup_field (context, name, 0, 1);
6049	  if (t)
6050	    {
6051	      if (TREE_CODE (t) != TYPE_DECL)
6052		{
6053		  if (complain & tf_error)
6054		    error ("no type named `%#T' in `%#T'", name, context);
6055		  return error_mark_node;
6056		}
6057
6058	      if (complain & tf_error)
6059		{
6060	      	  if (complain & tf_parsing)
6061		    type_access_control (context, t);
6062		  else
6063		    enforce_access (context, t);
6064		}
6065
6066	      if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
6067		t = TREE_TYPE (t);
6068	      if (IMPLICIT_TYPENAME_P (t))
6069		{
6070		  /* Lookup found an implicit typename that we had
6071		     injected into the current scope. Doing things
6072		     properly would have located the exact same type,
6073		     so there is no error here.  We must remove the
6074		     implicitness so that we do not warn about it.  */
6075		  t = copy_node (t);
6076		  TREE_TYPE (t) = NULL_TREE;
6077		}
6078
6079	      return t;
6080	    }
6081	}
6082    }
6083
6084  /* If the CONTEXT is not a template type, then either the field is
6085     there now or its never going to be.  */
6086  if (!uses_template_parms (context))
6087    {
6088      if (complain & tf_error)
6089	error ("no type named `%#T' in `%#T'", name, context);
6090      return error_mark_node;
6091    }
6092
6093  return build_typename_type (context, name, fullname,  NULL_TREE);
6094}
6095
6096/* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
6097   unless an error occurs, in which case error_mark_node is returned.
6098   If we locate a TYPE_DECL, we return that, rather than the _TYPE it
6099   corresponds to.  If COMPLAIN zero, don't complain about any errors
6100   that occur.  */
6101
6102tree
6103make_unbound_class_template (context, name, complain)
6104     tree context, name;
6105     tsubst_flags_t complain;
6106{
6107  tree t;
6108  tree d;
6109
6110  if (TYPE_P (name))
6111    name = TYPE_IDENTIFIER (name);
6112  else if (DECL_P (name))
6113    name = DECL_NAME (name);
6114  if (TREE_CODE (name) != IDENTIFIER_NODE)
6115    abort ();
6116
6117  if (!uses_template_parms (context)
6118      || currently_open_class (context))
6119    {
6120      tree tmpl = NULL_TREE;
6121
6122      if (IS_AGGR_TYPE (context))
6123	tmpl = lookup_field (context, name, 0, 0);
6124
6125      if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6126	{
6127	  if (complain & tf_error)
6128	    error ("no class template named `%#T' in `%#T'", name, context);
6129	  return error_mark_node;
6130	}
6131
6132      if (complain & tf_error)
6133	{
6134	  if (complain & tf_parsing)
6135	    type_access_control (context, tmpl);
6136	  else
6137	    enforce_access (context, tmpl);
6138	}
6139
6140      return tmpl;
6141    }
6142
6143  /* Build the UNBOUND_CLASS_TEMPLATE.  */
6144  t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
6145  TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6146  TREE_TYPE (t) = NULL_TREE;
6147
6148  /* Build the corresponding TEMPLATE_DECL.  */
6149  d = build_decl (TEMPLATE_DECL, name, t);
6150  TYPE_NAME (TREE_TYPE (d)) = d;
6151  TYPE_STUB_DECL (TREE_TYPE (d)) = d;
6152  DECL_CONTEXT (d) = FROB_CONTEXT (context);
6153  DECL_ARTIFICIAL (d) = 1;
6154
6155  return t;
6156}
6157
6158/* Select the right _DECL from multiple choices.  */
6159
6160static tree
6161select_decl (cxx_binding *binding, int flags)
6162{
6163  tree val;
6164
6165  timevar_push (TV_NAME_LOOKUP);
6166
6167  val = BINDING_VALUE (binding);
6168
6169  if (LOOKUP_NAMESPACES_ONLY (flags))
6170    {
6171      /* We are not interested in types.  */
6172      if (val && TREE_CODE (val) == NAMESPACE_DECL)
6173        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6174      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
6175    }
6176
6177  /* If we could have a type and
6178     we have nothing or we need a type and have none.  */
6179  if (BINDING_TYPE (binding)
6180      && (!val || ((flags & LOOKUP_PREFER_TYPES)
6181                   && TREE_CODE (val) != TYPE_DECL)))
6182    val = TYPE_STUB_DECL (BINDING_TYPE (binding));
6183  /* Don't return non-types if we really prefer types.  */
6184  else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
6185	   && (TREE_CODE (val) != TEMPLATE_DECL
6186	       || !DECL_CLASS_TEMPLATE_P (val)))
6187    val = NULL_TREE;
6188
6189  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6190}
6191
6192/* Unscoped lookup of a global: iterate over current namespaces,
6193   considering using-directives.  If SPACESP is non-NULL, store a list
6194   of the namespaces we've considered in it.  */
6195
6196tree
6197unqualified_namespace_lookup (name, flags, spacesp)
6198     tree name;
6199     int flags;
6200     tree *spacesp;
6201{
6202  tree initial = current_decl_namespace ();
6203  tree scope = initial;
6204  tree siter;
6205  struct cp_binding_level *level;
6206  tree val = NULL_TREE;
6207  cxx_binding binding;
6208
6209  timevar_push (TV_NAME_LOOKUP);
6210  cxx_binding_clear (&binding);
6211  if (spacesp)
6212    *spacesp = NULL_TREE;
6213
6214  for (; !val; scope = CP_DECL_CONTEXT (scope))
6215    {
6216      cxx_binding *b;
6217      if (spacesp)
6218	*spacesp = tree_cons (scope, NULL_TREE, *spacesp);
6219      b = cxx_scope_find_binding_for_name (scope, name);
6220
6221      /* Ignore anticipated built-in functions.  */
6222      if (b && BINDING_VALUE (b) && DECL_P (BINDING_VALUE (b))
6223          && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
6224          && DECL_ANTICIPATED (BINDING_VALUE (b)))
6225        /* Keep binding cleared.  */;
6226      else if (b)
6227        {
6228          /* Initialize binding for this context.  */
6229          binding.value = BINDING_VALUE (b);
6230          binding.type = BINDING_TYPE (b);
6231        }
6232
6233      /* Add all _DECLs seen through local using-directives.  */
6234      for (level = current_binding_level;
6235	   !level->namespace_p;
6236	   level = level->level_chain)
6237	if (!lookup_using_namespace (name, &binding, level->using_directives,
6238                                     scope, flags, spacesp))
6239	  /* Give up because of error.  */
6240	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6241
6242      /* Add all _DECLs seen through global using-directives.  */
6243      /* XXX local and global using lists should work equally.  */
6244      siter = initial;
6245      while (1)
6246	{
6247	  if (!lookup_using_namespace (name, &binding,
6248                                       DECL_NAMESPACE_USING (siter),
6249				       scope, flags, spacesp))
6250	    /* Give up because of error.  */
6251	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6252	  if (siter == scope) break;
6253	  siter = CP_DECL_CONTEXT (siter);
6254	}
6255
6256      val = select_decl (&binding, flags);
6257      if (scope == global_namespace)
6258	break;
6259    }
6260  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6261}
6262
6263/* Combine prefer_type and namespaces_only into flags.  */
6264
6265static int
6266lookup_flags (prefer_type, namespaces_only)
6267  int prefer_type, namespaces_only;
6268{
6269  if (namespaces_only)
6270    return LOOKUP_PREFER_NAMESPACES;
6271  if (prefer_type > 1)
6272    return LOOKUP_PREFER_TYPES;
6273  if (prefer_type > 0)
6274    return LOOKUP_PREFER_BOTH;
6275  return 0;
6276}
6277
6278/* Given a lookup that returned VAL, use FLAGS to decide if we want to
6279   ignore it or not.  Subroutine of lookup_name_real.  */
6280
6281static tree
6282qualify_lookup (val, flags)
6283     tree val;
6284     int flags;
6285{
6286  if (val == NULL_TREE)
6287    return val;
6288  if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
6289    return val;
6290  if ((flags & LOOKUP_PREFER_TYPES)
6291      && (TREE_CODE (val) == TYPE_DECL
6292	  || ((flags & LOOKUP_TEMPLATES_EXPECTED)
6293	      && DECL_CLASS_TEMPLATE_P (val))))
6294    return val;
6295  if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
6296    return NULL_TREE;
6297  return val;
6298}
6299
6300/* Any other BINDING overrides an implicit TYPENAME.  Warn about
6301   that.  */
6302
6303static void
6304warn_about_implicit_typename_lookup (typename, binding)
6305     tree typename;
6306     tree binding;
6307{
6308  tree subtype = TREE_TYPE (TREE_TYPE (typename));
6309  tree name = DECL_NAME (typename);
6310
6311  if (! (TREE_CODE (binding) == TEMPLATE_DECL
6312	 && CLASS_TYPE_P (subtype)
6313	 && CLASSTYPE_TEMPLATE_INFO (subtype)
6314	 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
6315      && ! (TREE_CODE (binding) == TYPE_DECL
6316	    && same_type_p (TREE_TYPE (binding), subtype)))
6317    {
6318      warning ("lookup of `%D' finds `%#D'",
6319		  name, binding);
6320      warning ("  instead of `%D' from dependent base class",
6321		  typename);
6322      warning ("  (use `typename %T::%D' if that's what you meant)",
6323		  constructor_name (current_class_type), name);
6324    }
6325}
6326
6327/* Check to see whether or not DECL is a variable that would have been
6328   in scope under the ARM, but is not in scope under the ANSI/ISO
6329   standard.  If so, issue an error message.  If name lookup would
6330   work in both cases, but return a different result, this function
6331   returns the result of ANSI/ISO lookup.  Otherwise, it returns
6332   DECL.  */
6333
6334tree
6335check_for_out_of_scope_variable (tree decl)
6336{
6337  tree shadowed;
6338
6339  /* We only care about out of scope variables.  */
6340  if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
6341    return decl;
6342
6343  shadowed = DECL_SHADOWED_FOR_VAR (decl);
6344  while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
6345	 && DECL_DEAD_FOR_LOCAL (shadowed))
6346    shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
6347  if (!shadowed)
6348    shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
6349  if (shadowed)
6350    {
6351      if (!DECL_ERROR_REPORTED (decl))
6352	{
6353	  warning ("name lookup of `%D' changed",
6354		      DECL_NAME (decl));
6355	  cp_warning_at ("  matches this `%D' under ISO standard rules",
6356			 shadowed);
6357	  cp_warning_at ("  matches this `%D' under old rules", decl);
6358	  DECL_ERROR_REPORTED (decl) = 1;
6359	}
6360      return shadowed;
6361    }
6362
6363  /* If we have already complained about this declaration, there's no
6364     need to do it again.  */
6365  if (DECL_ERROR_REPORTED (decl))
6366    return decl;
6367
6368  DECL_ERROR_REPORTED (decl) = 1;
6369  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
6370    {
6371      error ("name lookup of `%D' changed for new ISO `for' scoping",
6372	     DECL_NAME (decl));
6373      cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
6374      return error_mark_node;
6375    }
6376  else
6377    {
6378      pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
6379	       DECL_NAME (decl));
6380      cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
6381    }
6382
6383  return decl;
6384}
6385
6386/* Look up NAME in the current binding level and its superiors in the
6387   namespace of variables, functions and typedefs.  Return a ..._DECL
6388   node of some kind representing its definition if there is only one
6389   such declaration, or return a TREE_LIST with all the overloaded
6390   definitions if there are many, or return 0 if it is undefined.
6391
6392   If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6393   If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6394   If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
6395   Otherwise we prefer non-TYPE_DECLs.
6396
6397   If NONCLASS is nonzero, we don't look for the NAME in class scope,
6398   using IDENTIFIER_CLASS_VALUE.  */
6399
6400static tree
6401lookup_name_real (name, prefer_type, nonclass, namespaces_only)
6402     tree name;
6403     int prefer_type, nonclass, namespaces_only;
6404{
6405  tree t;
6406  tree val = NULL_TREE;
6407  int yylex = 0;
6408  tree from_obj = NULL_TREE;
6409  int flags;
6410  int val_is_implicit_typename = 0;
6411  cxx_binding *iter;
6412
6413  timevar_push (TV_NAME_LOOKUP);
6414
6415  /* Hack: copy flag set by parser, if set.  */
6416  if (only_namespace_names)
6417    namespaces_only = 1;
6418
6419  if (prefer_type == -2)
6420    {
6421      extern int looking_for_typename;
6422      tree type = NULL_TREE;
6423
6424      yylex = 1;
6425      prefer_type = looking_for_typename;
6426
6427      flags = lookup_flags (prefer_type, namespaces_only);
6428      /* If the next thing is '<', class templates are types.  */
6429      if (looking_for_template)
6430        flags |= LOOKUP_TEMPLATES_EXPECTED;
6431
6432      if (got_scope)
6433	type = got_scope;
6434      else if (got_object != error_mark_node)
6435	type = got_object;
6436
6437      if (type)
6438	{
6439	  if (type == error_mark_node)
6440	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6441	  if (IMPLICIT_TYPENAME_P (type))
6442	    type = TREE_TYPE (type);
6443
6444	  if (TYPE_P (type))
6445	    type = complete_type (type);
6446
6447	  if (TREE_CODE (type) == VOID_TYPE)
6448	    type = global_namespace;
6449	  if (TREE_CODE (type) == NAMESPACE_DECL)
6450	    {
6451              cxx_binding b;
6452              cxx_binding_clear (&b);
6453	      flags |= LOOKUP_COMPLAIN;
6454	      if (!qualified_lookup_using_namespace (name, type, &b, flags))
6455		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
6456	      val = select_decl (&b, flags);
6457	    }
6458	  else if (! IS_AGGR_TYPE (type)
6459		   || TREE_CODE (type) == TEMPLATE_TYPE_PARM
6460		   || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
6461		   || TREE_CODE (type) == TYPENAME_TYPE)
6462	    /* Someone else will give an error about this if needed.  */
6463	    val = NULL_TREE;
6464	  else if (type == current_class_type)
6465	    val = IDENTIFIER_CLASS_VALUE (name);
6466	  else
6467	    {
6468	      val = lookup_member (type, name, 0, prefer_type);
6469	      if (!uses_template_parms (type))
6470		type_access_control (type, val);
6471
6472	      /* Restore the containing TYPENAME_TYPE if we looked
6473		 through it before.  */
6474	      if (got_scope && got_scope != type
6475		  && val && TREE_CODE (val) == TYPE_DECL
6476		  && TREE_CODE (TREE_TYPE (val)) == TYPENAME_TYPE)
6477		{
6478		  val = TREE_TYPE (val);
6479		  val = build_typename_type (got_scope, name,
6480					     TYPENAME_TYPE_FULLNAME (val),
6481					     TREE_TYPE (val));
6482		  val = TYPE_STUB_DECL (val);
6483		}
6484	    }
6485	}
6486      else
6487	val = NULL_TREE;
6488
6489      if (got_scope)
6490	goto done;
6491      else if (got_object && val)
6492	{
6493	  from_obj = val;
6494	  val = NULL_TREE;
6495	}
6496    }
6497  else
6498    {
6499      flags = lookup_flags (prefer_type, namespaces_only);
6500      /* If we're not parsing, we need to complain.  */
6501      flags |= LOOKUP_COMPLAIN;
6502    }
6503
6504  /* Conversion operators are handled specially because ordinary
6505     unqualified name lookup will not find template conversion
6506     operators.  */
6507  if (IDENTIFIER_TYPENAME_P (name))
6508    {
6509      struct cp_binding_level *level;
6510
6511      for (level = current_binding_level;
6512	   level && !level->namespace_p;
6513	   level = level->level_chain)
6514	{
6515	  tree class_type;
6516	  tree operators;
6517
6518	  /* A conversion operator can only be declared in a class
6519	     scope.  */
6520	  if (level->parm_flag != 2)
6521	    continue;
6522
6523	  /* Lookup the conversion operator in the class.  */
6524	  class_type = level->this_class;
6525	  operators = lookup_fnfields (class_type, name, /*protect=*/0);
6526	  if (operators)
6527	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
6528	}
6529
6530      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
6531    }
6532
6533  /* First, look in non-namespace scopes.  */
6534
6535  if (current_class_type == NULL_TREE)
6536    nonclass = 1;
6537
6538  for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
6539    {
6540      tree binding;
6541
6542      if (!LOCAL_BINDING_P (iter) && nonclass)
6543	/* We're not looking for class-scoped bindings, so keep going.  */
6544	continue;
6545
6546      /* If this is the kind of thing we're looking for, we're done.  */
6547      if (qualify_lookup (BINDING_VALUE (iter), flags))
6548	binding = BINDING_VALUE (iter);
6549      else if ((flags & LOOKUP_PREFER_TYPES)
6550	       && qualify_lookup (BINDING_TYPE (iter), flags))
6551	binding = BINDING_TYPE (iter);
6552      else
6553	binding = NULL_TREE;
6554
6555      /* Handle access control on types from enclosing or base classes.  */
6556      if (binding && ! yylex
6557	  && BINDING_LEVEL (iter) && BINDING_LEVEL (iter)->parm_flag == 2)
6558	type_access_control (BINDING_LEVEL (iter)->this_class, binding);
6559
6560      if (binding
6561	  && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
6562	{
6563	  if (val_is_implicit_typename && !yylex)
6564	    warn_about_implicit_typename_lookup (val, binding);
6565	  val = binding;
6566	  val_is_implicit_typename
6567	    = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
6568	  if (!val_is_implicit_typename)
6569	    break;
6570	}
6571    }
6572
6573  /* The name might be from an enclosing class of the current scope.  */
6574  if (!val && !nonclass && current_class_type)
6575    val = qualify_lookup (lookup_nested_field (name, !yylex), flags);
6576
6577  /* Now lookup in namespace scopes.  */
6578  if (!val || val_is_implicit_typename)
6579    {
6580      t = unqualified_namespace_lookup (name, flags, 0);
6581      if (t)
6582	{
6583	  if (val_is_implicit_typename && !yylex)
6584	    warn_about_implicit_typename_lookup (val, t);
6585	  val = t;
6586	}
6587    }
6588
6589 done:
6590  if (val)
6591    {
6592      /* This should only warn about types used in qualified-ids.  */
6593      if (from_obj && from_obj != val)
6594	{
6595	  if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
6596	      && TREE_CODE (val) == TYPE_DECL
6597	      && ! same_type_p (TREE_TYPE (from_obj), TREE_TYPE (val)))
6598	    pedwarn ("\
6599lookup of `%D' in the scope of `%#T' (`%#D') \
6600does not match lookup in the current scope (`%#D')",
6601			name, got_object, from_obj, val);
6602
6603	  /* We don't change val to from_obj if got_object depends on
6604	     template parms because that breaks implicit typename for
6605	     destructor calls.  */
6606	  if (! uses_template_parms (got_object))
6607	    val = from_obj;
6608	}
6609
6610      /* If we have a single function from a using decl, pull it out.  */
6611      if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
6612	val = OVL_FUNCTION (val);
6613    }
6614  else if (from_obj)
6615    val = from_obj;
6616
6617  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6618}
6619
6620tree
6621lookup_name_nonclass (name)
6622     tree name;
6623{
6624  return lookup_name_real (name, 0, 1, 0);
6625}
6626
6627tree
6628lookup_function_nonclass (name, args)
6629     tree name;
6630     tree args;
6631{
6632  return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6633}
6634
6635tree
6636lookup_name_namespace_only (name)
6637     tree name;
6638{
6639  /* type-or-namespace, nonclass, namespace_only */
6640  return lookup_name_real (name, 1, 1, 1);
6641}
6642
6643tree
6644lookup_name (name, prefer_type)
6645     tree name;
6646     int prefer_type;
6647{
6648  return lookup_name_real (name, prefer_type, 0, 0);
6649}
6650
6651/* Similar to `lookup_name' but look only in the innermost non-class
6652   binding level.  */
6653
6654tree
6655lookup_name_current_level (name)
6656     tree name;
6657{
6658  struct cp_binding_level *b;
6659  tree t = NULL_TREE;
6660
6661  timevar_push (TV_NAME_LOOKUP);
6662
6663  b = current_binding_level;
6664  while (b->parm_flag == 2)
6665    b = b->level_chain;
6666
6667  if (b->namespace_p)
6668    {
6669      t = IDENTIFIER_NAMESPACE_VALUE (name);
6670
6671      /* extern "C" function() */
6672      if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6673	t = TREE_VALUE (t);
6674    }
6675  else if (IDENTIFIER_BINDING (name)
6676	   && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6677    {
6678      while (1)
6679	{
6680	  if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6681	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
6682
6683	  if (b->keep == 2)
6684	    b = b->level_chain;
6685	  else
6686	    break;
6687	}
6688    }
6689
6690  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6691}
6692
6693/* Like lookup_name_current_level, but for types.  */
6694
6695tree
6696lookup_type_current_level (name)
6697     tree name;
6698{
6699  register tree t = NULL_TREE;
6700
6701  timevar_push (TV_NAME_LOOKUP);
6702
6703  my_friendly_assert (! current_binding_level->namespace_p, 980716);
6704
6705  if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6706      && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6707    {
6708      struct cp_binding_level *b = current_binding_level;
6709      while (1)
6710	{
6711	  if (purpose_member (name, b->type_shadowed))
6712	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
6713				    REAL_IDENTIFIER_TYPE_VALUE (name));
6714	  if (b->keep == 2)
6715	    b = b->level_chain;
6716	  else
6717	    break;
6718	}
6719    }
6720
6721  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6722}
6723
6724void
6725begin_only_namespace_names ()
6726{
6727  only_namespace_names = 1;
6728}
6729
6730void
6731end_only_namespace_names ()
6732{
6733  only_namespace_names = 0;
6734}
6735
6736/* Push the declarations of builtin types into the namespace.
6737   RID_INDEX is the index of the builtin type
6738   in the array RID_POINTERS.  NAME is the name used when looking
6739   up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6740
6741void
6742record_builtin_type (rid_index, name, type)
6743     enum rid rid_index;
6744     const char *name;
6745     tree type;
6746{
6747  tree rname = NULL_TREE, tname = NULL_TREE;
6748  tree tdecl = NULL_TREE;
6749
6750  if ((int) rid_index < (int) RID_MAX)
6751    rname = ridpointers[(int) rid_index];
6752  if (name)
6753    tname = get_identifier (name);
6754
6755  TYPE_BUILT_IN (type) = 1;
6756
6757  if (tname)
6758    {
6759      tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6760      set_identifier_type_value (tname, NULL_TREE);
6761      if ((int) rid_index < (int) RID_MAX)
6762	/* Built-in types live in the global namespace.  */
6763	SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6764    }
6765  if (rname != NULL_TREE)
6766    {
6767      if (tname != NULL_TREE)
6768	{
6769	  set_identifier_type_value (rname, NULL_TREE);
6770	  SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6771	}
6772      else
6773	{
6774	  tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6775	  set_identifier_type_value (rname, NULL_TREE);
6776	}
6777    }
6778}
6779
6780/* Record one of the standard Java types.
6781 * Declare it as having the given NAME.
6782 * If SIZE > 0, it is the size of one of the integral types;
6783 * otherwise it is the negative of the size of one of the other types.  */
6784
6785static tree
6786record_builtin_java_type (name, size)
6787     const char *name;
6788     int size;
6789{
6790  tree type, decl;
6791  if (size > 0)
6792    type = make_signed_type (size);
6793  else if (size > -32)
6794    { /* "__java_char" or ""__java_boolean".  */
6795      type = make_unsigned_type (-size);
6796      /*if (size == -1)	TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6797    }
6798  else
6799    { /* "__java_float" or ""__java_double".  */
6800      type = make_node (REAL_TYPE);
6801      TYPE_PRECISION (type) = - size;
6802      layout_type (type);
6803    }
6804  record_builtin_type (RID_MAX, name, type);
6805  decl = TYPE_NAME (type);
6806
6807  /* Suppress generate debug symbol entries for these types,
6808     since for normal C++ they are just clutter.
6809     However, push_lang_context undoes this if extern "Java" is seen.  */
6810  DECL_IGNORED_P (decl) = 1;
6811
6812  TYPE_FOR_JAVA (type) = 1;
6813  return type;
6814}
6815
6816/* Push a type into the namespace so that the back-ends ignore it.  */
6817
6818static void
6819record_unknown_type (type, name)
6820     tree type;
6821     const char *name;
6822{
6823  tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6824  /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6825  DECL_IGNORED_P (decl) = 1;
6826  TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6827  TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6828  TYPE_ALIGN (type) = 1;
6829  TYPE_USER_ALIGN (type) = 0;
6830  TYPE_MODE (type) = TYPE_MODE (void_type_node);
6831}
6832
6833/* An string for which we should create an IDENTIFIER_NODE at
6834   startup.  */
6835
6836typedef struct predefined_identifier
6837{
6838  /* The name of the identifier.  */
6839  const char *const name;
6840  /* The place where the IDENTIFIER_NODE should be stored.  */
6841  tree *const node;
6842  /* Nonzero if this is the name of a constructor or destructor.  */
6843  const int ctor_or_dtor_p;
6844} predefined_identifier;
6845
6846/* Create all the predefined identifiers.  */
6847
6848static void
6849initialize_predefined_identifiers ()
6850{
6851  const predefined_identifier *pid;
6852
6853  /* A table of identifiers to create at startup.  */
6854  static const predefined_identifier predefined_identifiers[] = {
6855    { "C++", &lang_name_cplusplus, 0 },
6856    { "C", &lang_name_c, 0 },
6857    { "Java", &lang_name_java, 0 },
6858    { CTOR_NAME, &ctor_identifier, 1 },
6859    { "__base_ctor", &base_ctor_identifier, 1 },
6860    { "__comp_ctor", &complete_ctor_identifier, 1 },
6861    { DTOR_NAME, &dtor_identifier, 1 },
6862    { "__comp_dtor", &complete_dtor_identifier, 1 },
6863    { "__base_dtor", &base_dtor_identifier, 1 },
6864    { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6865    { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6866    { "nelts", &nelts_identifier, 0 },
6867    { THIS_NAME, &this_identifier, 0 },
6868    { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6869    { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6870    { "_vptr", &vptr_identifier, 0 },
6871    { "__vtt_parm", &vtt_parm_identifier, 0 },
6872    { "std", &std_identifier, 0 },
6873    { NULL, NULL, 0 }
6874  };
6875
6876  for (pid = predefined_identifiers; pid->name; ++pid)
6877    {
6878      *pid->node = get_identifier (pid->name);
6879      if (pid->ctor_or_dtor_p)
6880	IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6881    }
6882}
6883
6884/* Create the predefined scalar types of C,
6885   and some nodes representing standard constants (0, 1, (void *)0).
6886   Initialize the global binding level.
6887   Make definitions for built-in primitive functions.  */
6888
6889void
6890cxx_init_decl_processing ()
6891{
6892  tree void_ftype;
6893  tree void_ftype_ptr;
6894
6895  /* Create all the identifiers we need.  */
6896  initialize_predefined_identifiers ();
6897
6898  /* Fill in back-end hooks.  */
6899  lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6900
6901  /* Create the global variables.  */
6902  push_to_top_level ();
6903
6904  /* Enter the global namespace.  */
6905  my_friendly_assert (global_namespace == NULL_TREE, 375);
6906  push_namespace (get_identifier ("::"));
6907  global_namespace = current_namespace;
6908  current_lang_name = NULL_TREE;
6909
6910  /* Adjust various flags based on command-line settings.  */
6911  if (!flag_permissive)
6912    flag_pedantic_errors = 1;
6913  if (!flag_no_inline)
6914    {
6915      flag_inline_trees = 1;
6916      flag_no_inline = 1;
6917    }
6918  if (flag_inline_functions)
6919    {
6920      flag_inline_trees = 2;
6921      flag_inline_functions = 0;
6922    }
6923
6924  /* Force minimum function alignment if using the least significant
6925     bit of function pointers to store the virtual bit.  */
6926  if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6927      && force_align_functions_log < 1)
6928    force_align_functions_log = 1;
6929
6930  /* Initially, C.  */
6931  current_lang_name = lang_name_c;
6932
6933  current_function_decl = NULL_TREE;
6934  current_binding_level = NULL_BINDING_LEVEL;
6935  free_binding_level = NULL_BINDING_LEVEL;
6936
6937  build_common_tree_nodes (flag_signed_char);
6938
6939  error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6940  TREE_TYPE (error_mark_list) = error_mark_node;
6941
6942  /* Make the binding_level structure for global names.  */
6943  pushlevel (0);
6944  current_binding_level->type_decls = binding_table_new (GLOBAL_SCOPE_HT_SIZE);
6945  /* The global level is the namespace level of ::.  */
6946  NAMESPACE_LEVEL (global_namespace) = current_binding_level;
6947  declare_namespace_level ();
6948
6949  VARRAY_TREE_INIT (current_binding_level->static_decls,
6950		    200,
6951		    "Static declarations");
6952
6953  /* Create the `std' namespace.  */
6954  push_namespace (std_identifier);
6955  std_node = current_namespace;
6956  pop_namespace ();
6957
6958  c_common_nodes_and_builtins ();
6959
6960  java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6961  java_short_type_node = record_builtin_java_type ("__java_short", 16);
6962  java_int_type_node = record_builtin_java_type ("__java_int", 32);
6963  java_long_type_node = record_builtin_java_type ("__java_long", 64);
6964  java_float_type_node = record_builtin_java_type ("__java_float", -32);
6965  java_double_type_node = record_builtin_java_type ("__java_double", -64);
6966  java_char_type_node = record_builtin_java_type ("__java_char", -16);
6967  java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6968
6969  integer_two_node = build_int_2 (2, 0);
6970  TREE_TYPE (integer_two_node) = integer_type_node;
6971  integer_three_node = build_int_2 (3, 0);
6972  TREE_TYPE (integer_three_node) = integer_type_node;
6973
6974  boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6975  TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6976  TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6977  TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6978  TYPE_PRECISION (boolean_type_node) = 1;
6979  record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6980  boolean_false_node = build_int_2 (0, 0);
6981  TREE_TYPE (boolean_false_node) = boolean_type_node;
6982  boolean_true_node = build_int_2 (1, 0);
6983  TREE_TYPE (boolean_true_node) = boolean_type_node;
6984
6985  empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6986
6987#if 0
6988  record_builtin_type (RID_MAX, NULL, string_type_node);
6989#endif
6990
6991  delta_type_node = ptrdiff_type_node;
6992  vtable_index_type = ptrdiff_type_node;
6993
6994  vtt_parm_type = build_pointer_type (const_ptr_type_node);
6995  void_ftype = build_function_type (void_type_node, void_list_node);
6996  void_ftype_ptr = build_function_type (void_type_node,
6997					tree_cons (NULL_TREE,
6998						   ptr_type_node,
6999						   void_list_node));
7000  void_ftype_ptr
7001    = build_exception_variant (void_ftype_ptr, empty_except_spec);
7002
7003  /* C++ extensions */
7004
7005  unknown_type_node = make_node (UNKNOWN_TYPE);
7006  record_unknown_type (unknown_type_node, "unknown type");
7007
7008  /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
7009  TREE_TYPE (unknown_type_node) = unknown_type_node;
7010
7011  /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
7012     result.  */
7013  TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
7014  TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
7015
7016  {
7017    /* Make sure we get a unique function type, so we can give
7018       its pointer type a name.  (This wins for gdb.) */
7019    tree vfunc_type = make_node (FUNCTION_TYPE);
7020    TREE_TYPE (vfunc_type) = integer_type_node;
7021    TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
7022    layout_type (vfunc_type);
7023
7024    vtable_entry_type = build_pointer_type (vfunc_type);
7025  }
7026  record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
7027
7028  vtbl_type_node
7029    = build_cplus_array_type (vtable_entry_type, NULL_TREE);
7030  layout_type (vtbl_type_node);
7031  vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
7032  record_builtin_type (RID_MAX, NULL, vtbl_type_node);
7033  vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
7034  layout_type (vtbl_ptr_type_node);
7035  record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
7036
7037  push_namespace (get_identifier ("__cxxabiv1"));
7038  abi_node = current_namespace;
7039  pop_namespace ();
7040
7041  global_type_node = make_node (LANG_TYPE);
7042  record_unknown_type (global_type_node, "global type");
7043
7044  /* Now, C++.  */
7045  current_lang_name = lang_name_cplusplus;
7046
7047  {
7048    tree bad_alloc_type_node, newtype, deltype;
7049    tree ptr_ftype_sizetype;
7050
7051    push_namespace (std_identifier);
7052    bad_alloc_type_node
7053      = xref_tag (class_type, get_identifier ("bad_alloc"),
7054		  /*attributes=*/NULL_TREE, 1);
7055    pop_namespace ();
7056    ptr_ftype_sizetype
7057      = build_function_type (ptr_type_node,
7058			     tree_cons (NULL_TREE,
7059					size_type_node,
7060					void_list_node));
7061    newtype = build_exception_variant
7062      (ptr_ftype_sizetype, add_exception_specifier
7063       (NULL_TREE, bad_alloc_type_node, -1));
7064    deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
7065    push_cp_library_fn (NEW_EXPR, newtype);
7066    push_cp_library_fn (VEC_NEW_EXPR, newtype);
7067    global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
7068    push_cp_library_fn (VEC_DELETE_EXPR, deltype);
7069  }
7070
7071  abort_fndecl
7072    = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
7073
7074  /* Perform other language dependent initializations.  */
7075  init_class_processing ();
7076  init_search_processing ();
7077  init_rtti_processing ();
7078
7079  if (flag_exceptions)
7080    init_exception_processing ();
7081
7082  if (! supports_one_only ())
7083    flag_weak = 0;
7084
7085  make_fname_decl = cp_make_fname_decl;
7086  start_fname_decls ();
7087
7088  /* Show we use EH for cleanups.  */
7089  using_eh_for_cleanups ();
7090
7091  /* Maintain consistency.  Perhaps we should just complain if they
7092     say -fwritable-strings?  */
7093  if (flag_writable_strings)
7094    flag_const_strings = 0;
7095}
7096
7097/* Generate an initializer for a function naming variable from
7098   NAME. NAME may be NULL, in which case we generate a special
7099   ERROR_MARK node which should be replaced later.  */
7100
7101tree
7102cp_fname_init (name)
7103     const char *name;
7104{
7105  tree domain = NULL_TREE;
7106  tree type;
7107  tree init = NULL_TREE;
7108  size_t length = 0;
7109
7110  if (name)
7111    {
7112      length = strlen (name);
7113      domain = build_index_type (size_int (length));
7114      init = build_string (length + 1, name);
7115    }
7116
7117  type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
7118  type = build_cplus_array_type (type, domain);
7119
7120  if (init)
7121    TREE_TYPE (init) = type;
7122  else
7123    /* We don't know the value until instantiation time. Make
7124       something which will be digested now, but replaced later.  */
7125    init = build (ERROR_MARK, type);
7126
7127  return init;
7128}
7129
7130/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
7131   decl, NAME is the initialization string and TYPE_DEP indicates whether
7132   NAME depended on the type of the function. We make use of that to detect
7133   __PRETTY_FUNCTION__ inside a template fn. This is being done
7134   lazily at the point of first use, so we musn't push the decl now.  */
7135
7136static tree
7137cp_make_fname_decl (id, type_dep)
7138     tree id;
7139     int type_dep;
7140{
7141  const char *const name = (type_dep && processing_template_decl
7142		      ? NULL : fname_as_string (type_dep));
7143  tree init = cp_fname_init (name);
7144  tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
7145
7146  /* As we don't push the decl here, we must set the context.  */
7147  DECL_CONTEXT (decl) = current_function_decl;
7148  DECL_PRETTY_FUNCTION_P (decl) = type_dep;
7149
7150  TREE_STATIC (decl) = 1;
7151  TREE_READONLY (decl) = 1;
7152  DECL_ARTIFICIAL (decl) = 1;
7153  DECL_INITIAL (decl) = init;
7154
7155  TREE_USED (decl) = 1;
7156
7157  cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
7158
7159  return decl;
7160}
7161
7162/* Make a definition for a builtin function named NAME in the current
7163   namespace, whose data type is TYPE and whose context is CONTEXT.
7164   TYPE should be a function type with argument types.
7165
7166   CLASS and CODE tell later passes how to compile calls to this function.
7167   See tree.h for possible values.
7168
7169   If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
7170   the name to be called if we can't opencode the function.
7171   If ATTRS is nonzero, use that for the function's attribute
7172   list.  */
7173
7174static tree
7175builtin_function_1 (name, type, context, code, class, libname, attrs)
7176     const char *name;
7177     tree type;
7178     tree context;
7179     int code;
7180     enum built_in_class class;
7181     const char *libname;
7182     tree attrs;
7183{
7184  tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
7185  DECL_BUILT_IN_CLASS (decl) = class;
7186  DECL_FUNCTION_CODE (decl) = code;
7187  DECL_CONTEXT (decl) = context;
7188
7189  pushdecl (decl);
7190
7191  /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
7192     we cannot change DECL_ASSEMBLER_NAME until we have installed this
7193     function in the namespace.  */
7194  if (libname)
7195    SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
7196  make_decl_rtl (decl, NULL);
7197
7198  /* Warn if a function in the namespace for users
7199     is used without an occasion to consider it declared.  */
7200  if (name[0] != '_' || name[1] != '_')
7201    DECL_ANTICIPATED (decl) = 1;
7202
7203  /* Possibly apply some default attributes to this built-in function.  */
7204  if (attrs)
7205    decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
7206  else
7207    decl_attributes (&decl, NULL_TREE, 0);
7208
7209  return decl;
7210}
7211
7212/* Entry point for the benefit of c_common_nodes_and_builtins.
7213
7214   Make a defintion for a builtin function named NAME and whose data type
7215   is TYPE.  TYPE should be a function type with argument types.  This
7216   function places the anticipated declaration in the global namespace
7217   and additionally in the std namespace if appropriate.
7218
7219   CLASS and CODE tell later passes how to compile calls to this function.
7220   See tree.h for possible values.
7221
7222   If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
7223   the name to be called if we can't opencode the function.
7224
7225   If ATTRS is nonzero, use that for the function's attribute
7226   list.  */
7227
7228tree
7229builtin_function (name, type, code, class, libname, attrs)
7230     const char *name;
7231     tree type;
7232     int code;
7233     enum built_in_class class;
7234     const char *libname;
7235     tree attrs;
7236{
7237  /* All builtins that don't begin with an '_' should additionally
7238     go in the 'std' namespace.  */
7239  if (name[0] != '_')
7240    {
7241      push_namespace (std_identifier);
7242      builtin_function_1 (name, type, std_node, code, class, libname, attrs);
7243      pop_namespace ();
7244    }
7245
7246  return builtin_function_1 (name, type, NULL_TREE, code,
7247			     class, libname, attrs);
7248}
7249
7250/* Generate a FUNCTION_DECL with the typical flags for a runtime library
7251   function.  Not called directly.  */
7252
7253static tree
7254build_library_fn_1 (name, operator_code, type)
7255     tree name;
7256     enum tree_code operator_code;
7257     tree type;
7258{
7259  tree fn = build_lang_decl (FUNCTION_DECL, name, type);
7260  DECL_EXTERNAL (fn) = 1;
7261  TREE_PUBLIC (fn) = 1;
7262  DECL_ARTIFICIAL (fn) = 1;
7263  TREE_NOTHROW (fn) = 1;
7264  SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
7265  SET_DECL_LANGUAGE (fn, lang_c);
7266  return fn;
7267}
7268
7269/* Returns the _DECL for a library function with C linkage.
7270   We assume that such functions never throw; if this is incorrect,
7271   callers should unset TREE_NOTHROW.  */
7272
7273tree
7274build_library_fn (name, type)
7275     tree name;
7276     tree type;
7277{
7278  return build_library_fn_1 (name, ERROR_MARK, type);
7279}
7280
7281/* Returns the _DECL for a library function with C++ linkage.  */
7282
7283static tree
7284build_cp_library_fn (name, operator_code, type)
7285     tree name;
7286     enum tree_code operator_code;
7287     tree type;
7288{
7289  tree fn = build_library_fn_1 (name, operator_code, type);
7290  TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
7291  DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
7292  SET_DECL_LANGUAGE (fn, lang_cplusplus);
7293  set_mangled_name_for_decl (fn);
7294  return fn;
7295}
7296
7297/* Like build_library_fn, but takes a C string instead of an
7298   IDENTIFIER_NODE.  */
7299
7300tree
7301build_library_fn_ptr (name, type)
7302     const char *name;
7303     tree type;
7304{
7305  return build_library_fn (get_identifier (name), type);
7306}
7307
7308/* Like build_cp_library_fn, but takes a C string instead of an
7309   IDENTIFIER_NODE.  */
7310
7311tree
7312build_cp_library_fn_ptr (name, type)
7313     const char *name;
7314     tree type;
7315{
7316  return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
7317}
7318
7319/* Like build_library_fn, but also pushes the function so that we will
7320   be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
7321
7322tree
7323push_library_fn (name, type)
7324     tree name, type;
7325{
7326  tree fn = build_library_fn (name, type);
7327  pushdecl_top_level (fn);
7328  return fn;
7329}
7330
7331/* Like build_cp_library_fn, but also pushes the function so that it
7332   will be found by normal lookup.  */
7333
7334static tree
7335push_cp_library_fn (operator_code, type)
7336     enum tree_code operator_code;
7337     tree type;
7338{
7339  tree fn = build_cp_library_fn (ansi_opname (operator_code),
7340				 operator_code,
7341				 type);
7342  pushdecl (fn);
7343  return fn;
7344}
7345
7346/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
7347   a FUNCTION_TYPE.  */
7348
7349tree
7350push_void_library_fn (name, parmtypes)
7351     tree name, parmtypes;
7352{
7353  tree type = build_function_type (void_type_node, parmtypes);
7354  return push_library_fn (name, type);
7355}
7356
7357/* Like push_library_fn, but also note that this function throws
7358   and does not return.  Used for __throw_foo and the like.  */
7359
7360tree
7361push_throw_library_fn (name, type)
7362     tree name, type;
7363{
7364  tree fn = push_library_fn (name, type);
7365  TREE_THIS_VOLATILE (fn) = 1;
7366  TREE_NOTHROW (fn) = 0;
7367  return fn;
7368}
7369
7370/* Apply default attributes to a function, if a system function with default
7371   attributes.  */
7372
7373void
7374cxx_insert_default_attributes (decl)
7375     tree decl;
7376{
7377  if (!DECL_EXTERN_C_FUNCTION_P (decl))
7378    return;
7379  if (!TREE_PUBLIC (decl))
7380    return;
7381  c_common_insert_default_attributes (decl);
7382}
7383
7384/* When we call finish_struct for an anonymous union, we create
7385   default copy constructors and such.  But, an anonymous union
7386   shouldn't have such things; this function undoes the damage to the
7387   anonymous union type T.
7388
7389   (The reason that we create the synthesized methods is that we don't
7390   distinguish `union { int i; }' from `typedef union { int i; } U'.
7391   The first is an anonymous union; the second is just an ordinary
7392   union type.)  */
7393
7394void
7395fixup_anonymous_aggr (t)
7396     tree t;
7397{
7398  tree *q;
7399
7400  /* Wipe out memory of synthesized methods */
7401  TYPE_HAS_CONSTRUCTOR (t) = 0;
7402  TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
7403  TYPE_HAS_INIT_REF (t) = 0;
7404  TYPE_HAS_CONST_INIT_REF (t) = 0;
7405  TYPE_HAS_ASSIGN_REF (t) = 0;
7406  TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
7407
7408  /* Splice the implicitly generated functions out of the TYPE_METHODS
7409     list.  */
7410  q = &TYPE_METHODS (t);
7411  while (*q)
7412    {
7413      if (DECL_ARTIFICIAL (*q))
7414	*q = TREE_CHAIN (*q);
7415      else
7416	q = &TREE_CHAIN (*q);
7417    }
7418
7419  /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
7420  if (TYPE_METHODS (t))
7421    cp_error_at ("an anonymous union cannot have function members", t);
7422
7423  /* Anonymous aggregates cannot have fields with ctors, dtors or complex
7424     assignment operators (because they cannot have these methods themselves).
7425     For anonymous unions this is already checked because they are not allowed
7426     in any union, otherwise we have to check it.  */
7427  if (TREE_CODE (t) != UNION_TYPE)
7428    {
7429      tree field, type;
7430
7431      for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7432	if (TREE_CODE (field) == FIELD_DECL)
7433	  {
7434	    type = TREE_TYPE (field);
7435	    if (CLASS_TYPE_P (type))
7436	      {
7437	        if (TYPE_NEEDS_CONSTRUCTING (type))
7438		  cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
7439			       field);
7440		if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7441		  cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
7442			       field);
7443		if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
7444		  cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
7445			       field);
7446	      }
7447	  }
7448    }
7449}
7450
7451/* Make sure that a declaration with no declarator is well-formed, i.e.
7452   just defines a tagged type or anonymous union.
7453
7454   Returns the type defined, if any.  */
7455
7456tree
7457check_tag_decl (declspecs)
7458     tree declspecs;
7459{
7460  int found_type = 0;
7461  int saw_friend = 0;
7462  int saw_typedef = 0;
7463  tree ob_modifier = NULL_TREE;
7464  register tree link;
7465  register tree t = NULL_TREE;
7466
7467  for (link = declspecs; link; link = TREE_CHAIN (link))
7468    {
7469      register tree value = TREE_VALUE (link);
7470
7471      if (TYPE_P (value)
7472	  || TREE_CODE (value) == TYPE_DECL
7473	  || (TREE_CODE (value) == IDENTIFIER_NODE
7474	      && IDENTIFIER_GLOBAL_VALUE (value)
7475	      && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
7476	{
7477	  ++found_type;
7478
7479	  if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
7480	    {
7481	      if (! in_system_header)
7482		pedwarn ("redeclaration of C++ built-in type `%T'", value);
7483	      return NULL_TREE;
7484	    }
7485
7486	  if (TYPE_P (value)
7487	      && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
7488		  || TREE_CODE (value) == ENUMERAL_TYPE))
7489	    {
7490	      my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
7491	      t = value;
7492	    }
7493	}
7494      else if (value == ridpointers[(int) RID_TYPEDEF])
7495        saw_typedef = 1;
7496      else if (value == ridpointers[(int) RID_FRIEND])
7497	{
7498	  if (current_class_type == NULL_TREE
7499	      || current_scope () != current_class_type)
7500	    ob_modifier = value;
7501	  else
7502	    saw_friend = 1;
7503	}
7504      else if (value == ridpointers[(int) RID_STATIC]
7505	       || value == ridpointers[(int) RID_EXTERN]
7506	       || value == ridpointers[(int) RID_AUTO]
7507	       || value == ridpointers[(int) RID_REGISTER]
7508	       || value == ridpointers[(int) RID_INLINE]
7509	       || value == ridpointers[(int) RID_VIRTUAL]
7510	       || value == ridpointers[(int) RID_CONST]
7511	       || value == ridpointers[(int) RID_VOLATILE]
7512	       || value == ridpointers[(int) RID_EXPLICIT]
7513	       || value == ridpointers[(int) RID_THREAD])
7514	ob_modifier = value;
7515    }
7516
7517  if (found_type > 1)
7518    error ("multiple types in one declaration");
7519
7520  if (t == NULL_TREE && ! saw_friend)
7521    pedwarn ("declaration does not declare anything");
7522
7523  /* Check for an anonymous union.  */
7524  else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
7525	   && TYPE_ANONYMOUS_P (t))
7526    {
7527      /* 7/3 In a simple-declaration, the optional init-declarator-list
7528         can be omitted only when declaring a class (clause 9) or
7529         enumeration (7.2), that is, when the decl-specifier-seq contains
7530         either a class-specifier, an elaborated-type-specifier with
7531         a class-key (9.1), or an enum-specifier.  In these cases and
7532         whenever a class-specifier or enum-specifier is present in the
7533         decl-specifier-seq, the identifiers in these specifiers are among
7534         the names being declared by the declaration (as class-name,
7535         enum-names, or enumerators, depending on the syntax).  In such
7536         cases, and except for the declaration of an unnamed bit-field (9.6),
7537         the decl-specifier-seq shall introduce one or more names into the
7538         program, or shall redeclare a name introduced by a previous
7539         declaration.  [Example:
7540             enum { };            // ill-formed
7541             typedef class { };   // ill-formed
7542         --end example]  */
7543      if (saw_typedef)
7544        {
7545          error ("missing type-name in typedef-declaration");
7546          return NULL_TREE;
7547        }
7548      /* Anonymous unions are objects, so they can have specifiers.  */;
7549      SET_ANON_AGGR_TYPE_P (t);
7550
7551      if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
7552	pedwarn ("ISO C++ prohibits anonymous structs");
7553    }
7554
7555  else if (ob_modifier)
7556    {
7557      if (ob_modifier == ridpointers[(int) RID_INLINE]
7558	  || ob_modifier == ridpointers[(int) RID_VIRTUAL])
7559	error ("`%D' can only be specified for functions", ob_modifier);
7560      else if (ob_modifier == ridpointers[(int) RID_FRIEND])
7561	error ("`%D' can only be specified inside a class", ob_modifier);
7562      else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
7563	error ("`%D' can only be specified for constructors",
7564		  ob_modifier);
7565      else
7566	error ("`%D' can only be specified for objects and functions",
7567		  ob_modifier);
7568    }
7569
7570  return t;
7571}
7572
7573/* Called when a declaration is seen that contains no names to declare.
7574   If its type is a reference to a structure, union or enum inherited
7575   from a containing scope, shadow that tag name for the current scope
7576   with a forward reference.
7577   If its type defines a new named structure or union
7578   or defines an enum, it is valid but we need not do anything here.
7579   Otherwise, it is an error.
7580
7581   C++: may have to grok the declspecs to learn about static,
7582   complain for anonymous unions.  */
7583
7584void
7585shadow_tag (declspecs)
7586     tree declspecs;
7587{
7588  tree t = check_tag_decl (declspecs);
7589
7590  if (t)
7591    maybe_process_partial_specialization (t);
7592
7593  /* This is where the variables in an anonymous union are
7594     declared.  An anonymous union declaration looks like:
7595     union { ... } ;
7596     because there is no declarator after the union, the parser
7597     sends that declaration here.  */
7598  if (t && ANON_AGGR_TYPE_P (t))
7599    {
7600      fixup_anonymous_aggr (t);
7601
7602      if (TYPE_FIELDS (t))
7603	{
7604	  tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
7605				      NULL);
7606	  finish_anon_union (decl);
7607	}
7608    }
7609}
7610
7611/* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
7612
7613tree
7614groktypename (typename)
7615     tree typename;
7616{
7617  tree specs, attrs;
7618  tree type;
7619  if (TREE_CODE (typename) != TREE_LIST)
7620    return typename;
7621  split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
7622  type = grokdeclarator (TREE_VALUE (typename), specs,
7623			 TYPENAME, 0, &attrs);
7624  if (attrs)
7625    cplus_decl_attributes (&type, attrs, 0);
7626  return type;
7627}
7628
7629/* Decode a declarator in an ordinary declaration or data definition.
7630   This is called as soon as the type information and variable name
7631   have been parsed, before parsing the initializer if any.
7632   Here we create the ..._DECL node, fill in its type,
7633   and put it on the list of decls for the current context.
7634   The ..._DECL node is returned as the value.
7635
7636   Exception: for arrays where the length is not specified,
7637   the type is left null, to be filled in by `cp_finish_decl'.
7638
7639   Function definitions do not come here; they go to start_function
7640   instead.  However, external and forward declarations of functions
7641   do go through here.  Structure field declarations are done by
7642   grokfield and not through here.  */
7643
7644tree
7645start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
7646     tree declarator, declspecs;
7647     int initialized;
7648     tree attributes, prefix_attributes;
7649{
7650  tree decl;
7651  register tree type, tem;
7652  tree context;
7653
7654#if 0
7655  /* See code below that used this.  */
7656  int init_written = initialized;
7657#endif
7658
7659  /* This should only be done once on the top most decl.  */
7660  if (have_extern_spec)
7661    {
7662      declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
7663			     declspecs);
7664      have_extern_spec = false;
7665    }
7666
7667  /* An object declared as __attribute__((deprecated)) suppresses
7668     warnings of uses of other deprecated items.  */
7669  if (lookup_attribute ("deprecated", attributes))
7670    deprecated_state = DEPRECATED_SUPPRESS;
7671
7672  attributes = chainon (attributes, prefix_attributes);
7673
7674  decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7675			 &attributes);
7676
7677  deprecated_state = DEPRECATED_NORMAL;
7678
7679  if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7680    return NULL_TREE;
7681
7682  type = TREE_TYPE (decl);
7683
7684  if (type == error_mark_node)
7685    return NULL_TREE;
7686
7687  context = DECL_CONTEXT (decl);
7688
7689  if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7690      && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7691    {
7692      /* When parsing the initializer, lookup should use the object's
7693	 namespace.  */
7694      push_decl_namespace (context);
7695    }
7696
7697  /* We are only interested in class contexts, later.  */
7698  if (context && TREE_CODE (context) == NAMESPACE_DECL)
7699    context = NULL_TREE;
7700
7701  if (initialized)
7702    /* Is it valid for this decl to have an initializer at all?
7703       If not, set INITIALIZED to zero, which will indirectly
7704       tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7705    switch (TREE_CODE (decl))
7706      {
7707      case TYPE_DECL:
7708	error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7709	initialized = 0;
7710	break;
7711
7712      case FUNCTION_DECL:
7713	error ("function `%#D' is initialized like a variable", decl);
7714	initialized = 0;
7715	break;
7716
7717      default:
7718	break;
7719      }
7720
7721  if (initialized)
7722    {
7723      if (! toplevel_bindings_p ()
7724	  && DECL_EXTERNAL (decl))
7725	warning ("declaration of `%#D' has `extern' and is initialized",
7726		    decl);
7727      DECL_EXTERNAL (decl) = 0;
7728      if (toplevel_bindings_p ())
7729	TREE_STATIC (decl) = 1;
7730
7731      /* Tell `pushdecl' this is an initialized decl
7732	 even though we don't yet have the initializer expression.
7733	 Also tell `cp_finish_decl' it may store the real initializer.  */
7734      DECL_INITIAL (decl) = error_mark_node;
7735    }
7736
7737  /* Set attributes here so if duplicate decl, will have proper attributes.  */
7738  cplus_decl_attributes (&decl, attributes, 0);
7739
7740  /* If #pragma weak was used, mark the decl weak now.  */
7741  if (global_scope_p (current_binding_level))
7742    maybe_apply_pragma_weak (decl);
7743
7744  if (TREE_CODE (decl) == FUNCTION_DECL
7745      && DECL_DECLARED_INLINE_P (decl)
7746      && DECL_UNINLINABLE (decl)
7747      && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7748    warning_with_decl (decl,
7749		       "inline function `%s' given attribute noinline");
7750
7751  if (context && COMPLETE_TYPE_P (complete_type (context)))
7752    {
7753      push_nested_class (context, 2);
7754
7755      if (TREE_CODE (decl) == VAR_DECL)
7756	{
7757	  tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7758	  if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7759	    error ("`%#D' is not a static member of `%#T'", decl, context);
7760	  else
7761	    {
7762	      if (DECL_CONTEXT (field) != context)
7763		{
7764		  pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7765			      DECL_CONTEXT (field), DECL_NAME (decl),
7766			      context, DECL_NAME (decl));
7767		  DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7768		}
7769	      /* Static data member are tricky; an in-class initialization
7770		 still doesn't provide a definition, so the in-class
7771		 declaration will have DECL_EXTERNAL set, but will have an
7772		 initialization.  Thus, duplicate_decls won't warn
7773		 about this situation, and so we check here.  */
7774	      if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7775		error ("duplicate initialization of %D", decl);
7776	      if (duplicate_decls (decl, field))
7777		decl = field;
7778	    }
7779	}
7780      else
7781	{
7782	  tree field = check_classfn (context, decl);
7783	  if (field && duplicate_decls (decl, field))
7784	    decl = field;
7785	}
7786
7787      /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7788      DECL_IN_AGGR_P (decl) = 0;
7789      if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7790	  || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7791	{
7792	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7793	  /* [temp.expl.spec] An explicit specialization of a static data
7794	     member of a template is a definition if the declaration
7795	     includes an initializer; otherwise, it is a declaration.
7796
7797	     We check for processing_specialization so this only applies
7798	     to the new specialization syntax.  */
7799	  if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7800	    DECL_EXTERNAL (decl) = 1;
7801	}
7802
7803      if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7804	pedwarn ("declaration of `%#D' outside of class is not definition",
7805		    decl);
7806    }
7807
7808  /* Enter this declaration into the symbol table.  */
7809  tem = maybe_push_decl (decl);
7810
7811  if (processing_template_decl)
7812    tem = push_template_decl (tem);
7813
7814#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7815  /* Tell the back-end to use or not use .common as appropriate.  If we say
7816     -fconserve-space, we want this to save .data space, at the expense of
7817     wrong semantics.  If we say -fno-conserve-space, we want this to
7818     produce errors about redefs; to do this we force variables into the
7819     data segment.  */
7820  DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7821			|| !DECL_THREAD_LOCAL (tem))
7822		       && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7823#endif
7824
7825  if (! processing_template_decl)
7826    start_decl_1 (tem);
7827
7828  return tem;
7829}
7830
7831void
7832start_decl_1 (decl)
7833     tree decl;
7834{
7835  tree type = TREE_TYPE (decl);
7836  int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7837
7838  if (type == error_mark_node)
7839    return;
7840
7841  maybe_push_cleanup_level (type);
7842
7843  if (initialized)
7844    /* Is it valid for this decl to have an initializer at all?
7845       If not, set INITIALIZED to zero, which will indirectly
7846       tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7847    {
7848      /* Don't allow initializations for incomplete types except for
7849	 arrays which might be completed by the initialization.  */
7850      if (COMPLETE_TYPE_P (complete_type (type)))
7851	;			/* A complete type is ok.  */
7852      else if (TREE_CODE (type) != ARRAY_TYPE)
7853	{
7854	  error ("variable `%#D' has initializer but incomplete type",
7855		    decl);
7856	  initialized = 0;
7857	  type = TREE_TYPE (decl) = error_mark_node;
7858	}
7859      else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7860	{
7861	  if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7862	    error ("elements of array `%#D' have incomplete type", decl);
7863	  /* else we already gave an error in start_decl.  */
7864	  initialized = 0;
7865	}
7866    }
7867
7868  if (!initialized
7869      && TREE_CODE (decl) != TYPE_DECL
7870      && TREE_CODE (decl) != TEMPLATE_DECL
7871      && type != error_mark_node
7872      && IS_AGGR_TYPE (type)
7873      && ! DECL_EXTERNAL (decl))
7874    {
7875      if ((! processing_template_decl || ! uses_template_parms (type))
7876	  && !COMPLETE_TYPE_P (complete_type (type)))
7877	{
7878	  error ("aggregate `%#D' has incomplete type and cannot be defined",
7879		 decl);
7880	  /* Change the type so that assemble_variable will give
7881	     DECL an rtl we can live with: (mem (const_int 0)).  */
7882	  type = TREE_TYPE (decl) = error_mark_node;
7883	}
7884      else
7885	{
7886	  /* If any base type in the hierarchy of TYPE needs a constructor,
7887	     then we set initialized to 1.  This way any nodes which are
7888	     created for the purposes of initializing this aggregate
7889	     will live as long as it does.  This is necessary for global
7890	     aggregates which do not have their initializers processed until
7891	     the end of the file.  */
7892	  initialized = TYPE_NEEDS_CONSTRUCTING (type);
7893	}
7894    }
7895
7896  if (! initialized)
7897    DECL_INITIAL (decl) = NULL_TREE;
7898}
7899
7900/* Handle initialization of references.
7901   These three arguments are from `cp_finish_decl', and have the
7902   same meaning here that they do there.
7903
7904   Quotes on semantics can be found in ARM 8.4.3.  */
7905
7906static tree
7907grok_reference_init (decl, type, init)
7908     tree decl, type, init;
7909{
7910  tree tmp;
7911
7912  if (init == NULL_TREE)
7913    {
7914      if ((DECL_LANG_SPECIFIC (decl) == 0
7915	   || DECL_IN_AGGR_P (decl) == 0)
7916	  && ! DECL_THIS_EXTERN (decl))
7917	error ("`%D' declared as reference but not initialized", decl);
7918      return NULL_TREE;
7919    }
7920
7921  if (init == error_mark_node)
7922    return NULL_TREE;
7923
7924  if (TREE_CODE (init) == CONSTRUCTOR)
7925    {
7926      error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7927      return NULL_TREE;
7928    }
7929
7930  if (TREE_CODE (init) == TREE_LIST)
7931    init = build_compound_expr (init);
7932
7933  if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7934    init = convert_from_reference (init);
7935
7936  if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7937      && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7938    {
7939      /* Note: default conversion is only called in very special cases.  */
7940      init = default_conversion (init);
7941    }
7942
7943  /* Convert INIT to the reference type TYPE.  This may involve the
7944     creation of a temporary, whose lifetime must be the same as that
7945     of the reference.  If so, a DECL_STMT for the temporary will be
7946     added just after the DECL_STMT for DECL.  That's why we don't set
7947     DECL_INITIAL for local references (instead assigning to them
7948     explicitly); we need to allow the temporary to be initialized
7949     first.  */
7950  tmp = initialize_reference (type, init, decl);
7951
7952  if (tmp == error_mark_node)
7953    return NULL_TREE;
7954  else if (tmp == NULL_TREE)
7955    {
7956      error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7957      return NULL_TREE;
7958    }
7959
7960  if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7961    return tmp;
7962
7963  DECL_INITIAL (decl) = tmp;
7964
7965  return NULL_TREE;
7966}
7967
7968/* When parsing `int a[] = {1, 2};' we don't know the size of the
7969   array until we finish parsing the initializer.  If that's the
7970   situation we're in, update DECL accordingly.  */
7971
7972static void
7973maybe_deduce_size_from_array_init (decl, init)
7974     tree decl;
7975     tree init;
7976{
7977  tree type = TREE_TYPE (decl);
7978
7979  if (TREE_CODE (type) == ARRAY_TYPE
7980      && TYPE_DOMAIN (type) == NULL_TREE
7981      && TREE_CODE (decl) != TYPE_DECL)
7982    {
7983      /* do_default is really a C-ism to deal with tentative definitions.
7984	 But let's leave it here to ease the eventual merge.  */
7985      int do_default = !DECL_EXTERNAL (decl);
7986      tree initializer = init ? init : DECL_INITIAL (decl);
7987      int failure = complete_array_type (type, initializer, do_default);
7988
7989      if (failure == 1)
7990	error ("initializer fails to determine size of `%D'", decl);
7991
7992      if (failure == 2)
7993	{
7994	  if (do_default)
7995	    error ("array size missing in `%D'", decl);
7996	  /* If a `static' var's size isn't known, make it extern as
7997	     well as static, so it does not get allocated.  If it's not
7998	     `static', then don't mark it extern; finish_incomplete_decl
7999	     will give it a default size and it will get allocated.  */
8000	  else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
8001	    DECL_EXTERNAL (decl) = 1;
8002	}
8003
8004      if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
8005	  && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
8006			      integer_zero_node))
8007	error ("zero-size array `%D'", decl);
8008
8009      layout_decl (decl, 0);
8010    }
8011}
8012
8013/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
8014   any appropriate error messages regarding the layout.  */
8015
8016static void
8017layout_var_decl (decl)
8018     tree decl;
8019{
8020  tree type = TREE_TYPE (decl);
8021#if 0
8022  tree ttype = target_type (type);
8023#endif
8024
8025  /* If we haven't already layed out this declaration, do so now.
8026     Note that we must not call complete type for an external object
8027     because it's type might involve templates that we are not
8028     supposed to isntantiate yet.  (And it's perfectly valid to say
8029     `extern X x' for some incomplete type `X'.)  */
8030  if (!DECL_EXTERNAL (decl))
8031    complete_type (type);
8032  if (!DECL_SIZE (decl)
8033      && TREE_TYPE (decl) != error_mark_node
8034      && (COMPLETE_TYPE_P (type)
8035	  || (TREE_CODE (type) == ARRAY_TYPE
8036	      && !TYPE_DOMAIN (type)
8037	      && COMPLETE_TYPE_P (TREE_TYPE (type)))))
8038    layout_decl (decl, 0);
8039
8040  if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
8041    {
8042      /* An automatic variable with an incomplete type: that is an error.
8043	 Don't talk about array types here, since we took care of that
8044	 message in grokdeclarator.  */
8045      error ("storage size of `%D' isn't known", decl);
8046      TREE_TYPE (decl) = error_mark_node;
8047    }
8048#if 0
8049  /* Keep this code around in case we later want to control debug info
8050     based on whether a type is "used".  (jason 1999-11-11) */
8051
8052  else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
8053    /* Let debugger know it should output info for this type.  */
8054    note_debug_info_needed (ttype);
8055
8056  if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
8057    note_debug_info_needed (DECL_CONTEXT (decl));
8058#endif
8059
8060  if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8061      && DECL_SIZE (decl) != NULL_TREE
8062      && ! TREE_CONSTANT (DECL_SIZE (decl)))
8063    {
8064      if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
8065	constant_expression_warning (DECL_SIZE (decl));
8066      else
8067	error ("storage size of `%D' isn't constant", decl);
8068    }
8069
8070  if (TREE_STATIC (decl)
8071      && !DECL_ARTIFICIAL (decl)
8072      && current_function_decl
8073      && DECL_CONTEXT (decl) == current_function_decl)
8074    push_local_name (decl);
8075}
8076
8077/* If a local static variable is declared in an inline function, or if
8078   we have a weak definition, we must endeavor to create only one
8079   instance of the variable at link-time.  */
8080
8081static void
8082maybe_commonize_var (decl)
8083     tree decl;
8084{
8085  /* Static data in a function with comdat linkage also has comdat
8086     linkage.  */
8087  if (TREE_STATIC (decl)
8088      /* Don't mess with __FUNCTION__.  */
8089      && ! DECL_ARTIFICIAL (decl)
8090      && DECL_FUNCTION_SCOPE_P (decl)
8091      /* Unfortunately, import_export_decl has not always been called
8092	 before the function is processed, so we cannot simply check
8093	 DECL_COMDAT.  */
8094      && (DECL_COMDAT (DECL_CONTEXT (decl))
8095	  || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
8096	       || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
8097	      && TREE_PUBLIC (DECL_CONTEXT (decl)))))
8098    {
8099      if (flag_weak)
8100	{
8101	  /* With weak symbols, we simply make the variable COMDAT;
8102	     that will cause copies in multiple translations units to
8103	     be merged.  */
8104	  comdat_linkage (decl);
8105	}
8106      else
8107	{
8108	  if (DECL_INITIAL (decl) == NULL_TREE
8109	      || DECL_INITIAL (decl) == error_mark_node)
8110	    {
8111	      /* Without weak symbols, we can use COMMON to merge
8112		 uninitialized variables.  */
8113	      TREE_PUBLIC (decl) = 1;
8114	      DECL_COMMON (decl) = 1;
8115	    }
8116	  else
8117	    {
8118	      /* While for initialized variables, we must use internal
8119		 linkage -- which means that multiple copies will not
8120		 be merged.  */
8121	      TREE_PUBLIC (decl) = 0;
8122	      DECL_COMMON (decl) = 0;
8123	      cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
8124	      cp_warning_at ("  you can work around this by removing the initializer", decl);
8125	    }
8126	}
8127    }
8128  else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
8129    /* Set it up again; we might have set DECL_INITIAL since the last
8130       time.  */
8131    comdat_linkage (decl);
8132}
8133
8134/* Issue an error message if DECL is an uninitialized const variable.  */
8135
8136static void
8137check_for_uninitialized_const_var (decl)
8138     tree decl;
8139{
8140  tree type = TREE_TYPE (decl);
8141
8142  /* ``Unless explicitly declared extern, a const object does not have
8143     external linkage and must be initialized. ($8.4; $12.1)'' ARM
8144     7.1.6 */
8145  if (TREE_CODE (decl) == VAR_DECL
8146      && TREE_CODE (type) != REFERENCE_TYPE
8147      && CP_TYPE_CONST_P (type)
8148      && !TYPE_NEEDS_CONSTRUCTING (type)
8149      && !DECL_INITIAL (decl))
8150    error ("uninitialized const `%D'", decl);
8151}
8152
8153/* FIELD is a FIELD_DECL or NULL.  In the former case, the value
8154   returned is the next FIELD_DECL (possibly FIELD itself) that can be
8155   initialized.  If there are no more such fields, the return value
8156   will be NULL.  */
8157
8158static tree
8159next_initializable_field (tree field)
8160{
8161  while (field
8162	 && (TREE_CODE (field) != FIELD_DECL
8163	     || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
8164	     || DECL_ARTIFICIAL (field)))
8165    field = TREE_CHAIN (field);
8166
8167  return field;
8168}
8169
8170/* Undo the brace-elision allowed by [dcl.init.aggr] in a
8171   brace-enclosed aggregate initializer.
8172
8173   *INITP is one of a list of initializers describing a brace-enclosed
8174   initializer for an entity of the indicated aggregate TYPE.  It may
8175   not presently match the shape of the TYPE; for example:
8176
8177     struct S { int a; int b; };
8178     struct S a[] = { 1, 2, 3, 4 };
8179
8180   Here *INITP will point to TREE_LIST of four elements, rather than a
8181   list of two elements, each itself a list of two elements.  This
8182   routine transforms INIT from the former form into the latter.  The
8183   revised initializer is returned.  */
8184
8185static tree
8186reshape_init (tree type, tree *initp)
8187{
8188  tree inits;
8189  tree old_init;
8190  tree old_init_value;
8191  tree new_init;
8192  bool brace_enclosed_p;
8193
8194  old_init = *initp;
8195  old_init_value = (TREE_CODE (*initp) == TREE_LIST
8196		    ? TREE_VALUE (*initp) : old_init);
8197
8198  /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
8199  if (!old_init_value)
8200    {
8201      my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
8202      TREE_VALUE (old_init) = error_mark_node;
8203      return old_init;
8204    }
8205
8206  /* If the initializer is brace-enclosed, pull initializers from the
8207     enclosed elements.  Advance past the brace-enclosed initializer
8208     now.  */
8209  if (TREE_CODE (old_init_value) == CONSTRUCTOR
8210      && TREE_HAS_CONSTRUCTOR (old_init_value))
8211    {
8212      *initp = TREE_CHAIN (old_init);
8213      TREE_CHAIN (old_init) = NULL_TREE;
8214      inits = CONSTRUCTOR_ELTS (old_init_value);
8215      initp = &inits;
8216      brace_enclosed_p = true;
8217    }
8218  else
8219    {
8220      inits = NULL_TREE;
8221      brace_enclosed_p = false;
8222    }
8223
8224  /* A non-aggregate type is always initialized with a single
8225     initializer.  */
8226  if (!CP_AGGREGATE_TYPE_P (type))
8227      {
8228	*initp = TREE_CHAIN (old_init);
8229	TREE_CHAIN (old_init) = NULL_TREE;
8230	/* It is invalid to initialize a non-aggregate type with a
8231	   brace-enclosed initializer.  */
8232	if (brace_enclosed_p)
8233	  {
8234	    error ("brace-enclosed initializer used to initialize `%T'",
8235		   type);
8236	    if (TREE_CODE (old_init) == TREE_LIST)
8237	      TREE_VALUE (old_init) = error_mark_node;
8238	    else
8239	      old_init = error_mark_node;
8240	  }
8241
8242	return old_init;
8243      }
8244
8245  /* [dcl.init.aggr]
8246
8247     All implicit type conversions (clause _conv_) are considered when
8248     initializing the aggregate member with an initializer from an
8249     initializer-list.  If the initializer can initialize a member,
8250     the member is initialized.  Otherwise, if the member is itself a
8251     non-empty subaggregate, brace elision is assumed and the
8252     initializer is considered for the initialization of the first
8253     member of the subaggregate.  */
8254  if (CLASS_TYPE_P (type)
8255      && !brace_enclosed_p
8256      && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
8257    {
8258      *initp = TREE_CHAIN (old_init);
8259      TREE_CHAIN (old_init) = NULL_TREE;
8260      return old_init;
8261    }
8262
8263  if (TREE_CODE (old_init_value) == STRING_CST
8264      && TREE_CODE (type) == ARRAY_TYPE
8265      && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8266    {
8267      /* [dcl.init.string]
8268
8269	 A char array (whether plain char, signed char, or unsigned char)
8270	 can be initialized by a string-literal (optionally enclosed in
8271	 braces); a wchar_t array can be initialized by a wide
8272	 string-literal (optionally enclosed in braces).  */
8273      new_init = old_init;
8274      /* Move past the initializer.  */
8275      *initp = TREE_CHAIN (old_init);
8276      TREE_CHAIN (old_init) = NULL_TREE;
8277    }
8278  else
8279    {
8280      /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */
8281      new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
8282      TREE_HAS_CONSTRUCTOR (new_init) = 1;
8283
8284      if (CLASS_TYPE_P (type))
8285	{
8286	  tree field;
8287
8288	  field = next_initializable_field (TYPE_FIELDS (type));
8289
8290	  if (!field)
8291	    {
8292	      /* [dcl.init.aggr]
8293
8294		 An initializer for an aggregate member that is an
8295		 empty class shall have the form of an empty
8296		 initializer-list {}.  */
8297	      if (!brace_enclosed_p)
8298		error ("initializer for `%T' must be brace-enclosed",
8299		       type);
8300	    }
8301	  else
8302	    {
8303	      /* Loop through the initializable fields, gathering
8304		 initializers.  */
8305	      while (*initp)
8306		{
8307		  tree field_init;
8308
8309		  /* Handle designated initializers, as an extension.  */
8310		  if (TREE_PURPOSE (*initp))
8311		    {
8312		      if (pedantic)
8313			pedwarn ("ISO C++ does not allow designated initializers");
8314		      field = lookup_field_1 (type, TREE_PURPOSE (*initp),
8315					      /*want_type=*/false);
8316		      if (!field || TREE_CODE (field) != FIELD_DECL)
8317			error ("`%T' has no non-static data member named `%D'",
8318			       type, TREE_PURPOSE (*initp));
8319		    }
8320		  if (!field)
8321		    break;
8322
8323		  field_init = reshape_init (TREE_TYPE (field), initp);
8324		  TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
8325		  CONSTRUCTOR_ELTS (new_init) = field_init;
8326		  /* [dcl.init.aggr]
8327
8328		     When a union  is  initialized with a brace-enclosed
8329		     initializer, the braces shall only contain an
8330		     initializer for the first member of the union.  */
8331		  if (TREE_CODE (type) == UNION_TYPE)
8332		    break;
8333		  field = next_initializable_field (TREE_CHAIN (field));
8334		}
8335	    }
8336	}
8337      else if (TREE_CODE (type) == ARRAY_TYPE)
8338	{
8339	  tree index;
8340	  tree max_index;
8341
8342	  /* If the bound of the array is known, take no more initializers
8343	     than are allowed.  */
8344	  max_index = (TYPE_DOMAIN (type)
8345		       ? array_type_nelts (type) : NULL_TREE);
8346	  /* Loop through the array elements, gathering initializers.  */
8347	  for (index = size_zero_node;
8348	       *initp && (!max_index || !tree_int_cst_lt (max_index, index));
8349	       index = size_binop (PLUS_EXPR, index, size_one_node))
8350	    {
8351	      tree element_init;
8352
8353	      element_init = reshape_init (TREE_TYPE (type), initp);
8354	      TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
8355	      CONSTRUCTOR_ELTS (new_init) = element_init;
8356	      if (TREE_PURPOSE (element_init))
8357		index = TREE_PURPOSE (element_init);
8358	    }
8359	}
8360      else
8361	abort ();
8362
8363      /* The initializers were placed in reverse order in the
8364	 CONSTRUCTOR.  */
8365      CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
8366
8367      if (TREE_CODE (old_init) == TREE_LIST)
8368	new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
8369    }
8370
8371  /* If this was a brace-enclosed initializer and all of the
8372     initializers were not used up, there is a problem.  */
8373  if (brace_enclosed_p && *initp)
8374    error ("too many initializers for `%T'", type);
8375
8376  return new_init;
8377}
8378
8379/* Verify INIT (the initializer for DECL), and record the
8380   initialization in DECL_INITIAL, if appropriate.
8381
8382   If the return value is non-NULL, it is an expression that must be
8383   evaluated dynamically to initialize DECL.  */
8384
8385static tree
8386check_initializer (tree decl, tree init, int flags)
8387{
8388  tree type = TREE_TYPE (decl);
8389
8390  /* If `start_decl' didn't like having an initialization, ignore it now.  */
8391  if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
8392    init = NULL_TREE;
8393
8394  /* If an initializer is present, DECL_INITIAL has been
8395     error_mark_node, to indicate that an as-of-yet unevaluated
8396     initialization will occur.  From now on, DECL_INITIAL reflects
8397     the static initialization -- if any -- of DECL.  */
8398  DECL_INITIAL (decl) = NULL_TREE;
8399
8400  /* Things that are going to be initialized need to have complete
8401     type.  */
8402  TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8403
8404  if (type == error_mark_node)
8405    /* We will have already complained.  */
8406    init = NULL_TREE;
8407  else if (init && COMPLETE_TYPE_P (type)
8408	   && !TREE_CONSTANT (TYPE_SIZE (type)))
8409    {
8410      error ("variable-sized object `%D' may not be initialized", decl);
8411      init = NULL_TREE;
8412    }
8413  else if (TREE_CODE (type) == ARRAY_TYPE
8414	   && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
8415    {
8416      error ("elements of array `%#D' have incomplete type", decl);
8417      init = NULL_TREE;
8418    }
8419  else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
8420    {
8421      error ("`%D' has incomplete type", decl);
8422      TREE_TYPE (decl) = error_mark_node;
8423      init = NULL_TREE;
8424    }
8425
8426  if (TREE_CODE (decl) == CONST_DECL)
8427    {
8428      my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
8429
8430      DECL_INITIAL (decl) = init;
8431
8432      my_friendly_assert (init != NULL_TREE, 149);
8433      init = NULL_TREE;
8434    }
8435  else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
8436    init = grok_reference_init (decl, type, init);
8437  else if (init)
8438    {
8439      if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
8440	{
8441	  /* [dcl.init] paragraph 13,
8442	     If T is a scalar type, then a declaration of the form
8443	     T x = { a };
8444	     is equivalent to
8445	     T x = a;
8446
8447	     reshape_init will complain about the extra braces,
8448	     and doesn't do anything useful in the case where TYPE is
8449	     scalar, so just don't call it.  */
8450	  if (CP_AGGREGATE_TYPE_P (type))
8451	    init = reshape_init (type, &init);
8452	}
8453
8454      /* If DECL has an array type without a specific bound, deduce the
8455	 array size from the initializer.  */
8456      maybe_deduce_size_from_array_init (decl, init);
8457      type = TREE_TYPE (decl);
8458      if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
8459	TREE_TYPE (init) = type;
8460
8461      if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
8462	{
8463	  if (TREE_CODE (type) == ARRAY_TYPE)
8464	    goto initialize_aggr;
8465	  else if (TREE_CODE (init) == CONSTRUCTOR
8466		   && TREE_HAS_CONSTRUCTOR (init))
8467	    {
8468	      if (TYPE_NON_AGGREGATE_CLASS (type))
8469		{
8470		  error ("`%D' must be initialized by constructor, not by `{...}'",
8471			 decl);
8472		  init = error_mark_node;
8473		}
8474	      else
8475		goto dont_use_constructor;
8476	    }
8477	  else
8478	    {
8479	      int saved_stmts_are_full_exprs_p;
8480
8481	    initialize_aggr:
8482	      saved_stmts_are_full_exprs_p = 0;
8483	      if (building_stmt_tree ())
8484		{
8485		  saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8486		  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8487		}
8488	      init = build_aggr_init (decl, init, flags);
8489	      if (building_stmt_tree ())
8490		current_stmt_tree ()->stmts_are_full_exprs_p =
8491		  saved_stmts_are_full_exprs_p;
8492	      return init;
8493	    }
8494	}
8495      else
8496	{
8497	dont_use_constructor:
8498	  if (TREE_CODE (init) != TREE_VEC)
8499	    init = store_init_value (decl, init);
8500	}
8501    }
8502  else if (DECL_EXTERNAL (decl))
8503    ;
8504  else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
8505    goto initialize_aggr;
8506  else if (IS_AGGR_TYPE (type))
8507    {
8508      tree core_type = strip_array_types (type);
8509
8510      if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
8511	error ("structure `%D' with uninitialized const members", decl);
8512      if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
8513	error ("structure `%D' with uninitialized reference members",
8514	       decl);
8515
8516      check_for_uninitialized_const_var (decl);
8517    }
8518  else
8519    check_for_uninitialized_const_var (decl);
8520
8521  if (init && init != error_mark_node)
8522    init = build (INIT_EXPR, type, decl, init);
8523
8524  return init;
8525}
8526
8527/* If DECL is not a local variable, give it RTL.  */
8528
8529static void
8530make_rtl_for_nonlocal_decl (decl, init, asmspec)
8531     tree decl;
8532     tree init;
8533     const char *asmspec;
8534{
8535  int toplev = toplevel_bindings_p ();
8536  int defer_p;
8537
8538  /* Handle non-variables up front.  */
8539  if (TREE_CODE (decl) != VAR_DECL)
8540    {
8541      rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8542      return;
8543    }
8544
8545  /* If we see a class member here, it should be a static data
8546     member.  */
8547  if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8548    {
8549      my_friendly_assert (TREE_STATIC (decl), 19990828);
8550      /* An in-class declaration of a static data member should be
8551	 external; it is only a declaration, and not a definition.  */
8552      if (init == NULL_TREE)
8553	my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
8554    }
8555
8556  /* Set the DECL_ASSEMBLER_NAME for the variable.  */
8557  if (asmspec)
8558    {
8559      SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8560      /* The `register' keyword, when used together with an
8561	 asm-specification, indicates that the variable should be
8562	 placed in a particular register.  */
8563      if (DECL_REGISTER (decl))
8564	DECL_C_HARD_REGISTER (decl) = 1;
8565    }
8566
8567  /* We don't create any RTL for local variables.  */
8568  if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8569    return;
8570
8571  /* We defer emission of local statics until the corresponding
8572     DECL_STMT is expanded.  */
8573  defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
8574
8575  /* We try to defer namespace-scope static constants so that they are
8576     not emitted into the object file unnecessarily.  */
8577  if (!DECL_VIRTUAL_P (decl)
8578      && TREE_READONLY (decl)
8579      && DECL_INITIAL (decl) != NULL_TREE
8580      && DECL_INITIAL (decl) != error_mark_node
8581      && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
8582      && toplev
8583      && !TREE_PUBLIC (decl))
8584    {
8585      /* Fool with the linkage of static consts according to #pragma
8586	 interface.  */
8587      if (!interface_unknown && !TREE_PUBLIC (decl))
8588	{
8589	  TREE_PUBLIC (decl) = 1;
8590	  DECL_EXTERNAL (decl) = interface_only;
8591	}
8592
8593      defer_p = 1;
8594    }
8595  /* Likewise for template instantiations.  */
8596  else if (DECL_COMDAT (decl))
8597    defer_p = 1;
8598
8599  /* If we're deferring the variable, we only need to make RTL if
8600     there's an ASMSPEC.  Otherwise, we'll lazily create it later when
8601     we need it.  (There's no way to lazily create RTL for things that
8602     have assembly specs because the information about the specifier
8603     isn't stored in the tree, yet)  */
8604  if (defer_p && asmspec)
8605    make_decl_rtl (decl, asmspec);
8606  /* If we're not deferring, go ahead and assemble the variable.  */
8607  else if (!defer_p)
8608    rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8609}
8610
8611/* The old ARM scoping rules injected variables declared in the
8612   initialization statement of a for-statement into the surrounding
8613   scope.  We support this usage, in order to be backward-compatible.
8614   DECL is a just-declared VAR_DECL; if necessary inject its
8615   declaration into the surrounding scope.  */
8616
8617void
8618maybe_inject_for_scope_var (decl)
8619     tree decl;
8620{
8621  timevar_push (TV_NAME_LOOKUP);
8622
8623  if (!DECL_NAME (decl))
8624    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
8625
8626  /* Declarations of __FUNCTION__ and its ilk appear magically when
8627     the variable is first used.  If that happens to be inside a
8628     for-loop, we don't want to do anything special.  */
8629  if (DECL_PRETTY_FUNCTION_P (decl))
8630    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
8631
8632  if (current_binding_level->is_for_scope)
8633    {
8634      struct cp_binding_level *outer
8635	= current_binding_level->level_chain;
8636
8637      /* Check to see if the same name is already bound at the outer
8638	 level, either because it was directly declared, or because a
8639	 dead for-decl got preserved.  In either case, the code would
8640	 not have been valid under the ARM scope rules, so clear
8641	 is_for_scope for the current_binding_level.
8642
8643	 Otherwise, we need to preserve the temp slot for decl to last
8644	 into the outer binding level.  */
8645
8646      cxx_binding *outer_binding
8647	= IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
8648
8649      if (outer_binding && BINDING_LEVEL (outer_binding) == outer
8650	  && (TREE_CODE (BINDING_VALUE (outer_binding))
8651	      == VAR_DECL)
8652	  && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
8653	{
8654	  BINDING_VALUE (outer_binding)
8655	    = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
8656	  current_binding_level->is_for_scope = 0;
8657	}
8658      else if (DECL_IN_MEMORY_P (decl))
8659	preserve_temp_slots (DECL_RTL (decl));
8660    }
8661
8662  timevar_pop (TV_NAME_LOOKUP);
8663}
8664
8665/* Generate code to initialize DECL (a local variable).  */
8666
8667static void
8668initialize_local_var (decl, init)
8669     tree decl;
8670     tree init;
8671{
8672  tree type = TREE_TYPE (decl);
8673  tree cleanup;
8674
8675  my_friendly_assert (TREE_CODE (decl) == VAR_DECL
8676		      || TREE_CODE (decl) == RESULT_DECL,
8677		      20021010);
8678  my_friendly_assert (!TREE_STATIC (decl), 20021010);
8679
8680  if (DECL_SIZE (decl) == NULL_TREE)
8681    {
8682      /* If we used it already as memory, it must stay in memory.  */
8683      DECL_INITIAL (decl) = NULL_TREE;
8684      TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8685    }
8686
8687  if (DECL_SIZE (decl) && type != error_mark_node)
8688    {
8689      int already_used;
8690
8691      /* Compute and store the initial value.  */
8692      already_used = TREE_USED (decl) || TREE_USED (type);
8693
8694      /* Perform the initialization.  */
8695      if (init)
8696	{
8697	  int saved_stmts_are_full_exprs_p;
8698
8699	  my_friendly_assert (building_stmt_tree (), 20000906);
8700	  saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8701	  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8702	  finish_expr_stmt (init);
8703	  current_stmt_tree ()->stmts_are_full_exprs_p =
8704	    saved_stmts_are_full_exprs_p;
8705	}
8706
8707      /* Set this to 0 so we can tell whether an aggregate which was
8708	 initialized was ever used.  Don't do this if it has a
8709	 destructor, so we don't complain about the 'resource
8710	 allocation is initialization' idiom.  Now set
8711	 attribute((unused)) on types so decls of that type will be
8712	 marked used. (see TREE_USED, above.)  */
8713      if (TYPE_NEEDS_CONSTRUCTING (type)
8714	  && ! already_used
8715	  && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8716	  && DECL_NAME (decl))
8717	TREE_USED (decl) = 0;
8718      else if (already_used)
8719	TREE_USED (decl) = 1;
8720    }
8721
8722  /* Generate a cleanup, if necessary.  */
8723  cleanup = cxx_maybe_build_cleanup (decl);
8724  if (DECL_SIZE (decl) && cleanup)
8725    finish_decl_cleanup (decl, cleanup);
8726}
8727
8728/* Finish processing of a declaration;
8729   install its line number and initial value.
8730   If the length of an array type is not known before,
8731   it must be determined now, from the initial value, or it is an error.
8732
8733   INIT holds the value of an initializer that should be allowed to escape
8734   the normal rules.
8735
8736   FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8737   if the (init) syntax was used.  */
8738
8739void
8740cp_finish_decl (decl, init, asmspec_tree, flags)
8741     tree decl, init;
8742     tree asmspec_tree;
8743     int flags;
8744{
8745  register tree type;
8746  tree ttype = NULL_TREE;
8747  const char *asmspec = NULL;
8748  int was_readonly = 0;
8749
8750  if (! decl)
8751    {
8752      if (init)
8753	error ("assignment (not initialization) in declaration");
8754      return;
8755    }
8756
8757  /* If a name was specified, get the string.  */
8758  if (global_scope_p (current_binding_level))
8759    asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8760  if (asmspec_tree)
8761    asmspec = TREE_STRING_POINTER (asmspec_tree);
8762
8763  if (init && TREE_CODE (init) == NAMESPACE_DECL)
8764    {
8765      error ("cannot initialize `%D' to namespace `%D'",
8766		decl, init);
8767      init = NULL_TREE;
8768    }
8769
8770  if (current_class_type
8771      && CP_DECL_CONTEXT (decl) == current_class_type
8772      && TYPE_BEING_DEFINED (current_class_type)
8773      && (DECL_INITIAL (decl) || init))
8774    DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8775
8776  if (TREE_CODE (decl) == VAR_DECL
8777      && DECL_CONTEXT (decl)
8778      && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8779      && DECL_CONTEXT (decl) != current_namespace
8780      && init)
8781    {
8782      /* Leave the namespace of the object.  */
8783      pop_decl_namespace ();
8784    }
8785
8786  type = TREE_TYPE (decl);
8787
8788  if (type == error_mark_node)
8789    return;
8790
8791  if (TYPE_HAS_MUTABLE_P (type))
8792    TREE_READONLY (decl) = 0;
8793
8794  if (processing_template_decl)
8795    {
8796      /* Add this declaration to the statement-tree.  */
8797      if (at_function_scope_p ()
8798	  && TREE_CODE (decl) != RESULT_DECL)
8799	add_decl_stmt (decl);
8800
8801      if (init && DECL_INITIAL (decl))
8802	DECL_INITIAL (decl) = init;
8803      goto finish_end0;
8804    }
8805
8806  /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8807  my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8808
8809  /* Take care of TYPE_DECLs up front.  */
8810  if (TREE_CODE (decl) == TYPE_DECL)
8811    {
8812      if (type != error_mark_node
8813	  && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8814	{
8815	  if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8816	    warning ("shadowing previous type declaration of `%#D'", decl);
8817	  set_identifier_type_value (DECL_NAME (decl), type);
8818	  CLASSTYPE_GOT_SEMICOLON (type) = 1;
8819	}
8820
8821      /* If we have installed this as the canonical typedef for this
8822	 type, and that type has not been defined yet, delay emitting
8823	 the debug information for it, as we will emit it later.  */
8824      if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8825	  && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8826	TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8827
8828      rest_of_decl_compilation (decl, NULL,
8829				DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8830      goto finish_end;
8831    }
8832
8833  if (TREE_CODE (decl) != FUNCTION_DECL)
8834    ttype = target_type (type);
8835
8836  if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8837      && (TYPE_NEEDS_CONSTRUCTING (type)
8838	  || TREE_CODE (type) == REFERENCE_TYPE))
8839    {
8840      /* Currently, GNU C++ puts constants in text space, making them
8841	 impossible to initialize.  In the future, one would hope for
8842	 an operating system which understood the difference between
8843	 initialization and the running of a program.  */
8844      was_readonly = 1;
8845      TREE_READONLY (decl) = 0;
8846    }
8847
8848  if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8849    {
8850      /* This must override the asm specifier which was placed by
8851	 grokclassfn.  Lay this out fresh.  */
8852      SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8853      SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8854      make_decl_rtl (decl, asmspec);
8855    }
8856  else if (TREE_CODE (decl) == RESULT_DECL)
8857    init = check_initializer (decl, init, flags);
8858  else if (TREE_CODE (decl) == VAR_DECL)
8859    {
8860      /* Only PODs can have thread-local storage.  Other types may require
8861	 various kinds of non-trivial initialization.  */
8862      if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8863	error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8864	       decl, TREE_TYPE (decl));
8865      /* Convert the initializer to the type of DECL, if we have not
8866	 already initialized DECL.  */
8867      if (!DECL_INITIALIZED_P (decl)
8868	  /* If !DECL_EXTERNAL then DECL is being defined.  In the
8869	     case of a static data member initialized inside the
8870	     class-specifier, there can be an initializer even if DECL
8871	     is *not* defined.  */
8872	  && (!DECL_EXTERNAL (decl) || init))
8873	{
8874	  init = check_initializer (decl, init, flags);
8875	  /* Thread-local storage cannot be dynamically initialized.  */
8876	  if (DECL_THREAD_LOCAL (decl) && init)
8877	    {
8878	      error ("`%D' is thread-local and so cannot be dynamically "
8879		     "initialized", decl);
8880	      init = NULL_TREE;
8881	    }
8882	  /* Handle:
8883
8884	     [dcl.init]
8885
8886	     The memory occupied by any object of static storage
8887	     duration is zero-initialized at program startup before
8888	     any other initialization takes place.
8889
8890	     We cannot create an appropriate initializer until after
8891	     the type of DECL is finalized.  If DECL_INITIAL is set,
8892	     then the DECL is statically initialized, and any
8893	     necessary zero-initialization has already been performed.  */
8894	  if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8895	    DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8896						   /*nelts=*/NULL_TREE,
8897						   /*static_storage_p=*/true);
8898	  /* Remember that the initialization for this variable has
8899	     taken place.  */
8900	  DECL_INITIALIZED_P (decl) = 1;
8901	}
8902      /* If the variable has an array type, lay out the type, even if
8903	 there is no initializer.  It is valid to index through the
8904	 array, and we must get TYPE_ALIGN set correctly on the array
8905	 type.  */
8906      else if (TREE_CODE (type) == ARRAY_TYPE)
8907	layout_type (type);
8908    }
8909
8910  /* Add this declaration to the statement-tree.  This needs to happen
8911     after the call to check_initializer so that the DECL_STMT for a
8912     reference temp is added before the DECL_STMT for the reference itself.  */
8913  if (building_stmt_tree ()
8914      && at_function_scope_p ()
8915      && TREE_CODE (decl) != RESULT_DECL)
8916    add_decl_stmt (decl);
8917
8918  if (TREE_CODE (decl) == VAR_DECL)
8919    layout_var_decl (decl);
8920
8921  /* Output the assembler code and/or RTL code for variables and functions,
8922     unless the type is an undefined structure or union.
8923     If not, it will get done when the type is completed.  */
8924  if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8925      || TREE_CODE (decl) == RESULT_DECL)
8926    {
8927      if (TREE_CODE (decl) == VAR_DECL)
8928	maybe_commonize_var (decl);
8929
8930      make_rtl_for_nonlocal_decl (decl, init, asmspec);
8931
8932      if (TREE_CODE (type) == FUNCTION_TYPE
8933	  || TREE_CODE (type) == METHOD_TYPE)
8934	abstract_virtuals_error (decl,
8935				 strip_array_types (TREE_TYPE (type)));
8936      else
8937	abstract_virtuals_error (decl, strip_array_types (type));
8938
8939      if (TREE_CODE (decl) == FUNCTION_DECL
8940	  || TREE_TYPE (decl) == error_mark_node)
8941	/* No initialization required.  */
8942	;
8943      else if (DECL_EXTERNAL (decl)
8944	       && ! (DECL_LANG_SPECIFIC (decl)
8945		     && DECL_NOT_REALLY_EXTERN (decl)))
8946	{
8947	  if (init)
8948	    DECL_INITIAL (decl) = init;
8949	}
8950      else
8951	{
8952	  /* A variable definition.  */
8953	  if (DECL_FUNCTION_SCOPE_P (decl))
8954	    {
8955	      /* This is a local declaration.  */
8956	      if (doing_semantic_analysis_p ())
8957		maybe_inject_for_scope_var (decl);
8958	      /* Initialize the local variable.  */
8959	      if (processing_template_decl)
8960		{
8961		  if (init || DECL_INITIAL (decl) == error_mark_node)
8962		    DECL_INITIAL (decl) = init;
8963		}
8964	      else if (!TREE_STATIC (decl))
8965		initialize_local_var (decl, init);
8966	    }
8967
8968	  if (TREE_STATIC (decl))
8969	    expand_static_init (decl, init);
8970	}
8971    finish_end0:
8972
8973      /* Undo call to `pushclass' that was done in `start_decl'
8974	 due to initialization of qualified member variable.
8975	 I.e., Foo::x = 10;  */
8976      {
8977	tree context = CP_DECL_CONTEXT (decl);
8978	if (context
8979	    && TYPE_P (context)
8980	    && (TREE_CODE (decl) == VAR_DECL
8981		/* We also have a pushclass done that we need to undo here
8982		   if we're at top level and declare a method.  */
8983		|| TREE_CODE (decl) == FUNCTION_DECL)
8984	    /* If size hasn't been set, we're still defining it,
8985	       and therefore inside the class body; don't pop
8986	       the binding level..  */
8987	    && COMPLETE_TYPE_P (context)
8988	    && context == current_class_type)
8989	  pop_nested_class ();
8990      }
8991    }
8992
8993 finish_end:
8994
8995  if (was_readonly)
8996    TREE_READONLY (decl) = 1;
8997}
8998
8999/* This is here for a midend callback from c-common.c */
9000
9001void
9002finish_decl (decl, init, asmspec_tree)
9003     tree decl, init;
9004     tree asmspec_tree;
9005{
9006  cp_finish_decl (decl, init, asmspec_tree, 0);
9007}
9008
9009/* Returns a declaration for a VAR_DECL as if:
9010
9011     extern "C" TYPE NAME;
9012
9013   had been seen.  Used to create compiler-generated global
9014   variables.  */
9015
9016tree
9017declare_global_var (name, type)
9018     tree name;
9019     tree type;
9020{
9021  tree decl;
9022
9023  push_to_top_level ();
9024  decl = build_decl (VAR_DECL, name, type);
9025  TREE_PUBLIC (decl) = 1;
9026  DECL_EXTERNAL (decl) = 1;
9027  DECL_ARTIFICIAL (decl) = 1;
9028  pushdecl (decl);
9029  cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
9030  pop_from_top_level ();
9031
9032  return decl;
9033}
9034
9035/* Returns a pointer to the `atexit' function.  Note that if
9036   FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9037   `__cxa_atexit' function specified in the IA64 C++ ABI.  */
9038
9039static tree
9040get_atexit_node ()
9041{
9042  tree atexit_fndecl;
9043  tree arg_types;
9044  tree fn_type;
9045  tree fn_ptr_type;
9046  const char *name;
9047
9048  if (atexit_node)
9049    return atexit_node;
9050
9051  if (flag_use_cxa_atexit)
9052    {
9053      /* The declaration for `__cxa_atexit' is:
9054
9055	   int __cxa_atexit (void (*)(void *), void *, void *)
9056
9057	 We build up the argument types and then then function type
9058	 itself.  */
9059
9060      /* First, build the pointer-to-function type for the first
9061	 argument.  */
9062      arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
9063      fn_type = build_function_type (void_type_node, arg_types);
9064      fn_ptr_type = build_pointer_type (fn_type);
9065      /* Then, build the rest of the argument types.  */
9066      arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
9067      arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
9068      arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
9069      /* And the final __cxa_atexit type.  */
9070      fn_type = build_function_type (integer_type_node, arg_types);
9071      fn_ptr_type = build_pointer_type (fn_type);
9072      name = "__cxa_atexit";
9073    }
9074  else
9075    {
9076      /* The declaration for `atexit' is:
9077
9078           int atexit (void (*)());
9079
9080	 We build up the argument types and then then function type
9081	 itself.  */
9082      fn_type = build_function_type (void_type_node, void_list_node);
9083      fn_ptr_type = build_pointer_type (fn_type);
9084      arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
9085      /* Build the final atexit type.  */
9086      fn_type = build_function_type (integer_type_node, arg_types);
9087      name = "atexit";
9088    }
9089
9090  /* Now, build the function declaration.  */
9091  push_lang_context (lang_name_c);
9092  atexit_fndecl = build_library_fn_ptr (name, fn_type);
9093  mark_used (atexit_fndecl);
9094  pop_lang_context ();
9095  atexit_node = default_conversion (atexit_fndecl);
9096
9097  return atexit_node;
9098}
9099
9100/* Returns the __dso_handle VAR_DECL.  */
9101
9102static tree
9103get_dso_handle_node ()
9104{
9105  if (dso_handle_node)
9106    return dso_handle_node;
9107
9108  /* Declare the variable.  */
9109  dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9110					ptr_type_node);
9111
9112  return dso_handle_node;
9113}
9114
9115/* Begin a new function with internal linkage whose job will be simply
9116   to destroy some particular variable.  */
9117
9118static tree
9119start_cleanup_fn ()
9120{
9121  static int counter = 0;
9122  int old_interface_only = interface_only;
9123  int old_interface_unknown = interface_unknown;
9124  char name[32];
9125  tree parmtypes;
9126  tree fntype;
9127  tree fndecl;
9128
9129  push_to_top_level ();
9130
9131  /* No need to mangle this.  */
9132  push_lang_context (lang_name_c);
9133
9134  interface_only = 0;
9135  interface_unknown = 1;
9136
9137  /* Build the parameter-types.  */
9138  parmtypes = void_list_node;
9139  /* Functions passed to __cxa_atexit take an additional parameter.
9140     We'll just ignore it.  After we implement the new calling
9141     convention for destructors, we can eliminate the use of
9142     additional cleanup functions entirely in the -fnew-abi case.  */
9143  if (flag_use_cxa_atexit)
9144    parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
9145  /* Build the function type itself.  */
9146  fntype = build_function_type (void_type_node, parmtypes);
9147  /* Build the name of the function.  */
9148  sprintf (name, "__tcf_%d", counter++);
9149  /* Build the function declaration.  */
9150  fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9151  /* It's a function with internal linkage, generated by the
9152     compiler.  */
9153  TREE_PUBLIC (fndecl) = 0;
9154  DECL_ARTIFICIAL (fndecl) = 1;
9155  /* Make the function `inline' so that it is only emitted if it is
9156     actually needed.  It is unlikely that it will be inlined, since
9157     it is only called via a function pointer, but we avoid unnecessary
9158     emissions this way.  */
9159  DECL_INLINE (fndecl) = 1;
9160  /* Build the parameter.  */
9161  if (flag_use_cxa_atexit)
9162    {
9163      tree parmdecl;
9164
9165      parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
9166      DECL_CONTEXT (parmdecl) = fndecl;
9167      TREE_USED (parmdecl) = 1;
9168      DECL_ARGUMENTS (fndecl) = parmdecl;
9169    }
9170
9171  pushdecl (fndecl);
9172  start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
9173
9174  interface_unknown = old_interface_unknown;
9175  interface_only = old_interface_only;
9176
9177  pop_lang_context ();
9178
9179  return current_function_decl;
9180}
9181
9182/* Finish the cleanup function begun by start_cleanup_fn.  */
9183
9184static void
9185end_cleanup_fn ()
9186{
9187  expand_body (finish_function (0));
9188
9189  pop_from_top_level ();
9190}
9191
9192/* Generate code to handle the destruction of DECL, an object with
9193   static storage duration.  */
9194
9195void
9196register_dtor_fn (decl)
9197     tree decl;
9198{
9199  tree cleanup;
9200  tree compound_stmt;
9201  tree args;
9202  tree fcall;
9203
9204  int saved_flag_access_control;
9205
9206  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
9207    return;
9208
9209  /* Call build_cleanup before we enter the anonymous function so that
9210     any access checks will be done relative to the current scope,
9211     rather than the scope of the anonymous function.  */
9212  build_cleanup (decl);
9213
9214  /* Now start the function.  */
9215  cleanup = start_cleanup_fn ();
9216
9217  /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
9218     to the original function, rather than the anonymous one.  That
9219     will make the back-end think that nested functions are in use,
9220     which causes confusion.  */
9221  saved_flag_access_control = flag_access_control;
9222  flag_access_control = 0;
9223  fcall = build_cleanup (decl);
9224  flag_access_control = saved_flag_access_control;
9225
9226  /* Create the body of the anonymous function.  */
9227  compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
9228  finish_expr_stmt (fcall);
9229  finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
9230  end_cleanup_fn ();
9231
9232  /* Call atexit with the cleanup function.  */
9233  cxx_mark_addressable (cleanup);
9234  cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
9235  if (flag_use_cxa_atexit)
9236    {
9237      args = tree_cons (NULL_TREE,
9238			build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
9239			NULL_TREE);
9240      args = tree_cons (NULL_TREE, null_pointer_node, args);
9241      args = tree_cons (NULL_TREE, cleanup, args);
9242    }
9243  else
9244    args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
9245  finish_expr_stmt (build_function_call (get_atexit_node (), args));
9246}
9247
9248/* DECL is a VAR_DECL with static storage duration.  INIT, if present,
9249   is its initializer.  Generate code to handle the construction
9250   and destruction of DECL.  */
9251
9252static void
9253expand_static_init (decl, init)
9254     tree decl;
9255     tree init;
9256{
9257  tree oldstatic;
9258
9259  my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
9260  my_friendly_assert (TREE_STATIC (decl), 20021010);
9261
9262  /* Some variables require no initialization.  */
9263  if (!init
9264      && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
9265      && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
9266    return;
9267
9268  oldstatic = value_member (decl, static_aggregates);
9269
9270  if (oldstatic)
9271    {
9272      if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
9273	error ("multiple initializations given for `%D'", decl);
9274    }
9275  else if (! toplevel_bindings_p ())
9276    {
9277      /* Emit code to perform this initialization but once.  */
9278      tree if_stmt;
9279      tree then_clause;
9280      tree assignment;
9281      tree guard;
9282      tree guard_init;
9283
9284      /* Emit code to perform this initialization but once.  This code
9285	 looks like:
9286
9287           static int guard = 0;
9288           if (!guard) {
9289             // Do initialization.
9290	     guard = 1;
9291	     // Register variable for destruction at end of program.
9292	   }
9293
9294	 Note that the `temp' variable is only set to 1 *after* the
9295	 initialization is complete.  This ensures that an exception,
9296	 thrown during the construction, will cause the variable to
9297	 reinitialized when we pass through this code again, as per:
9298
9299	   [stmt.dcl]
9300
9301	   If the initialization exits by throwing an exception, the
9302	   initialization is not complete, so it will be tried again
9303	   the next time control enters the declaration.
9304
9305         In theory, this process should be thread-safe, too; multiple
9306	 threads should not be able to initialize the variable more
9307	 than once.  We don't yet attempt to ensure thread-safety.  */
9308
9309      /* Create the guard variable.  */
9310      guard = get_guard (decl);
9311
9312      /* Begin the conditional initialization.  */
9313      if_stmt = begin_if_stmt ();
9314      finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
9315      then_clause = begin_compound_stmt (/*has_no_scope=*/0);
9316
9317      /* Do the initialization itself.  */
9318      assignment = init ? init : NULL_TREE;
9319
9320      /* Once the assignment is complete, set TEMP to 1.  Since the
9321	 construction of the static object is complete at this point,
9322	 we want to make sure TEMP is set to 1 even if a temporary
9323	 constructed during the initialization throws an exception
9324	 when it is destroyed.  So, we combine the initialization and
9325	 the assignment to TEMP into a single expression, ensuring
9326	 that when we call finish_expr_stmt the cleanups will not be
9327	 run until after TEMP is set to 1.  */
9328      guard_init = set_guard (guard);
9329      if (assignment)
9330	{
9331	  assignment = tree_cons (NULL_TREE, assignment,
9332				  build_tree_list (NULL_TREE,
9333						   guard_init));
9334	  assignment = build_compound_expr (assignment);
9335	}
9336      else
9337	assignment = guard_init;
9338      finish_expr_stmt (assignment);
9339
9340      /* Use atexit to register a function for destroying this static
9341	 variable.  */
9342      register_dtor_fn (decl);
9343
9344      finish_compound_stmt (/*has_no_scope=*/0, then_clause);
9345      finish_then_clause (if_stmt);
9346      finish_if_stmt ();
9347    }
9348  else
9349    static_aggregates = tree_cons (init, decl, static_aggregates);
9350}
9351
9352/* Finish the declaration of a catch-parameter.  */
9353
9354tree
9355start_handler_parms (declspecs, declarator)
9356     tree declspecs;
9357     tree declarator;
9358{
9359  tree decl;
9360  if (declspecs)
9361    {
9362      decl = grokdeclarator (declarator, declspecs, CATCHPARM,
9363			     1, NULL);
9364      if (decl == NULL_TREE)
9365	error ("invalid catch parameter");
9366    }
9367  else
9368    decl = NULL_TREE;
9369
9370  return decl;
9371}
9372
9373
9374/* Make TYPE a complete type based on INITIAL_VALUE.
9375   Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9376   2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
9377
9378int
9379complete_array_type (type, initial_value, do_default)
9380     tree type, initial_value;
9381     int do_default;
9382{
9383  register tree maxindex = NULL_TREE;
9384  int value = 0;
9385
9386  if (initial_value)
9387    {
9388      /* An array of character type can be initialized from a
9389	 brace-enclosed string constant.  */
9390      if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
9391	  && TREE_CODE (initial_value) == CONSTRUCTOR
9392	  && CONSTRUCTOR_ELTS (initial_value)
9393	  && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
9394	      == STRING_CST)
9395	  && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
9396	initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
9397
9398      /* Note MAXINDEX is really the maximum index, one less than the
9399	 size.  */
9400      if (TREE_CODE (initial_value) == STRING_CST)
9401	{
9402	  int eltsize
9403	    = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9404	  maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
9405				   / eltsize) - 1, 0);
9406	}
9407      else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9408	{
9409	  tree elts = CONSTRUCTOR_ELTS (initial_value);
9410
9411	  maxindex = ssize_int (-1);
9412	  for (; elts; elts = TREE_CHAIN (elts))
9413	    {
9414	      if (TREE_PURPOSE (elts))
9415		maxindex = TREE_PURPOSE (elts);
9416	      else
9417		maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
9418	    }
9419	  maxindex = copy_node (maxindex);
9420	}
9421      else
9422	{
9423	  /* Make an error message unless that happened already.  */
9424	  if (initial_value != error_mark_node)
9425	    value = 1;
9426	  else
9427	    initial_value = NULL_TREE;
9428
9429	  /* Prevent further error messages.  */
9430	  maxindex = build_int_2 (0, 0);
9431	}
9432    }
9433
9434  if (!maxindex)
9435    {
9436      if (do_default)
9437	maxindex = build_int_2 (0, 0);
9438      value = 2;
9439    }
9440
9441  if (maxindex)
9442    {
9443      tree itype;
9444      tree domain;
9445
9446      domain = build_index_type (maxindex);
9447      TYPE_DOMAIN (type) = domain;
9448
9449      if (! TREE_TYPE (maxindex))
9450	TREE_TYPE (maxindex) = domain;
9451      if (initial_value)
9452        itype = TREE_TYPE (initial_value);
9453      else
9454	itype = NULL;
9455      if (itype && !TYPE_DOMAIN (itype))
9456	TYPE_DOMAIN (itype) = domain;
9457      /* The type of the main variant should never be used for arrays
9458	 of different sizes.  It should only ever be completed with the
9459	 size of the array.  */
9460      if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
9461	TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
9462    }
9463
9464  /* Lay out the type now that we can get the real answer.  */
9465
9466  layout_type (type);
9467
9468  return value;
9469}
9470
9471/* Return zero if something is declared to be a member of type
9472   CTYPE when in the context of CUR_TYPE.  STRING is the error
9473   message to print in that case.  Otherwise, quietly return 1.  */
9474
9475static int
9476member_function_or_else (ctype, cur_type, flags)
9477     tree ctype, cur_type;
9478     enum overload_flags flags;
9479{
9480  if (ctype && ctype != cur_type)
9481    {
9482      if (flags == DTOR_FLAG)
9483	error ("destructor for alien class `%T' cannot be a member",
9484	          ctype);
9485      else
9486	error ("constructor for alien class `%T' cannot be a member",
9487	          ctype);
9488      return 0;
9489    }
9490  return 1;
9491}
9492
9493/* Subroutine of `grokdeclarator'.  */
9494
9495/* Generate errors possibly applicable for a given set of specifiers.
9496   This is for ARM $7.1.2.  */
9497
9498static void
9499bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
9500     tree object;
9501     const char *type;
9502     int virtualp, quals, friendp, raises, inlinep;
9503{
9504  if (virtualp)
9505    error ("`%D' declared as a `virtual' %s", object, type);
9506  if (inlinep)
9507    error ("`%D' declared as an `inline' %s", object, type);
9508  if (quals)
9509    error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
9510	      object, type);
9511  if (friendp)
9512    cp_error_at ("`%D' declared as a friend", object);
9513  if (raises
9514      && (TREE_CODE (object) == TYPE_DECL
9515	  || (!TYPE_PTRFN_P (TREE_TYPE (object))
9516	      && !TYPE_REFFN_P (TREE_TYPE (object))
9517	      && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9518    cp_error_at ("`%D' declared with an exception specification", object);
9519}
9520
9521/* CTYPE is class type, or null if non-class.
9522   TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9523   or METHOD_TYPE.
9524   DECLARATOR is the function's name.
9525   VIRTUALP is truthvalue of whether the function is virtual or not.
9526   FLAGS are to be passed through to `grokclassfn'.
9527   QUALS are qualifiers indicating whether the function is `const'
9528   or `volatile'.
9529   RAISES is a list of exceptions that this function can raise.
9530   CHECK is 1 if we must find this method in CTYPE, 0 if we should
9531   not look, and -1 if we should not call `grokclassfn' at all.
9532
9533   Returns `NULL_TREE' if something goes wrong, after issuing
9534   applicable error messages.  */
9535
9536static tree
9537grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
9538	    raises, check, friendp, publicp, inlinep, funcdef_flag,
9539	    template_count, in_namespace)
9540     tree ctype, type;
9541     tree declarator;
9542     tree orig_declarator;
9543     int virtualp;
9544     enum overload_flags flags;
9545     tree quals, raises;
9546     int check, friendp, publicp, inlinep, funcdef_flag, template_count;
9547     tree in_namespace;
9548{
9549  tree decl;
9550  int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9551  int has_default_arg = 0;
9552  tree t;
9553
9554  if (raises)
9555    type = build_exception_variant (type, raises);
9556
9557  decl = build_lang_decl (FUNCTION_DECL, declarator, type);
9558  /* Propagate volatile out from type to decl.  */
9559  if (TYPE_VOLATILE (type))
9560    TREE_THIS_VOLATILE (decl) = 1;
9561
9562  /* If this decl has namespace scope, set that up.  */
9563  if (in_namespace)
9564    set_decl_namespace (decl, in_namespace, friendp);
9565  else if (!ctype)
9566    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9567
9568  /* `main' and builtins have implicit 'C' linkage.  */
9569  if ((MAIN_NAME_P (declarator)
9570       || (IDENTIFIER_LENGTH (declarator) > 10
9571	   && IDENTIFIER_POINTER (declarator)[0] == '_'
9572	   && IDENTIFIER_POINTER (declarator)[1] == '_'
9573	   && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
9574      && current_lang_name == lang_name_cplusplus
9575      && ctype == NULL_TREE
9576      /* NULL_TREE means global namespace.  */
9577      && DECL_CONTEXT (decl) == NULL_TREE)
9578    SET_DECL_LANGUAGE (decl, lang_c);
9579
9580  /* Should probably propagate const out from type to decl I bet (mrs).  */
9581  if (staticp)
9582    {
9583      DECL_STATIC_FUNCTION_P (decl) = 1;
9584      DECL_CONTEXT (decl) = ctype;
9585    }
9586
9587  if (ctype)
9588    DECL_CONTEXT (decl) = ctype;
9589
9590  if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9591    {
9592      if (processing_template_decl)
9593	error ("cannot declare `::main' to be a template");
9594      if (inlinep)
9595	error ("cannot declare `::main' to be inline");
9596      if (!publicp)
9597	error ("cannot declare `::main' to be static");
9598      if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9599			integer_type_node))
9600	error ("`main' must return `int'");
9601      inlinep = 0;
9602      publicp = 1;
9603    }
9604
9605  /* Members of anonymous types and local classes have no linkage; make
9606     them internal.  */
9607  /* FIXME what if it gets a name from typedef?  */
9608  if (ctype && (TYPE_ANONYMOUS_P (ctype)
9609		|| decl_function_context (TYPE_MAIN_DECL (ctype))))
9610    publicp = 0;
9611
9612  if (publicp)
9613    {
9614      /* [basic.link]: A name with no linkage (notably, the name of a class
9615	 or enumeration declared in a local scope) shall not be used to
9616	 declare an entity with linkage.
9617
9618	 Only check this for public decls for now.  */
9619      t = no_linkage_check (TREE_TYPE (decl));
9620      if (t)
9621	{
9622	  if (TYPE_ANONYMOUS_P (t))
9623	    {
9624	      if (DECL_EXTERN_C_P (decl))
9625		/* Allow this; it's pretty common in C.  */;
9626	      else
9627		{
9628		  pedwarn ("non-local function `%#D' uses anonymous type",
9629			      decl);
9630		  if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
9631		    cp_pedwarn_at ("\
9632`%#D' does not refer to the unqualified type, so it is not used for linkage",
9633				TYPE_NAME (t));
9634		}
9635	    }
9636	  else
9637	    pedwarn ("non-local function `%#D' uses local type `%T'",
9638			decl, t);
9639	}
9640    }
9641
9642  TREE_PUBLIC (decl) = publicp;
9643  if (! publicp)
9644    {
9645      DECL_INTERFACE_KNOWN (decl) = 1;
9646      DECL_NOT_REALLY_EXTERN (decl) = 1;
9647    }
9648
9649  DID_INLINE_FUNC (decl) = 0;
9650  /* If the declaration was declared inline, mark it as such.  */
9651  if (inlinep)
9652    DECL_DECLARED_INLINE_P (decl) = 1;
9653  /* We inline functions that are explicitly declared inline, or, when
9654     the user explicitly asks us to, all functions.  */
9655  if (DECL_DECLARED_INLINE_P (decl))
9656    DECL_INLINE (decl) = 1;
9657  if (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag)
9658    {
9659      DID_INLINE_FUNC (decl) = 1;
9660      DECL_INLINE (decl) = 1;
9661    }
9662
9663  DECL_EXTERNAL (decl) = 1;
9664  if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
9665    {
9666      error ("%smember function `%D' cannot have `%T' method qualifier",
9667		(ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
9668      quals = NULL_TREE;
9669    }
9670
9671  if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
9672    grok_op_properties (decl, friendp);
9673
9674  if (ctype && decl_function_context (decl))
9675    DECL_NO_STATIC_CHAIN (decl) = 1;
9676
9677  for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9678    if (TREE_PURPOSE (t)
9679	&& TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
9680      {
9681	has_default_arg = 1;
9682	break;
9683      }
9684
9685  if (friendp
9686      && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9687    {
9688      if (funcdef_flag)
9689	error
9690	  ("defining explicit specialization `%D' in friend declaration",
9691	   orig_declarator);
9692      else
9693	{
9694	  tree fns = TREE_OPERAND (orig_declarator, 0);
9695	  tree args = TREE_OPERAND (orig_declarator, 1);
9696
9697	  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9698	    {
9699	      /* Something like `template <class T> friend void f<T>()'.  */
9700	      error ("invalid use of template-id `%D' in declaration of primary template",
9701			orig_declarator);
9702	      return NULL_TREE;
9703	    }
9704
9705
9706	  /* A friend declaration of the form friend void f<>().  Record
9707	     the information in the TEMPLATE_ID_EXPR.  */
9708	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
9709
9710          if (TREE_CODE (fns) == COMPONENT_REF)
9711            {
9712              /* Due to bison parser ickiness, we will have already looked
9713                 up an operator_name or PFUNCNAME within the current class
9714                 (see template_id in parse.y). If the current class contains
9715                 such a name, we'll get a COMPONENT_REF here. Undo that.  */
9716
9717              my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
9718                                  == current_class_type, 20001120);
9719              fns = TREE_OPERAND (fns, 1);
9720            }
9721	  my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
9722	                      || TREE_CODE (fns) == LOOKUP_EXPR
9723	                      || TREE_CODE (fns) == OVERLOAD, 20001120);
9724	  DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
9725
9726	  if (has_default_arg)
9727	    {
9728	      error ("default arguments are not allowed in declaration of friend template specialization `%D'",
9729			decl);
9730	      return NULL_TREE;
9731	    }
9732
9733	  if (inlinep)
9734	    {
9735	      error ("`inline' is not allowed in declaration of friend template specialization `%D'",
9736			decl);
9737	      return NULL_TREE;
9738	    }
9739	}
9740    }
9741
9742  if (has_default_arg)
9743    add_defarg_fn (decl);
9744
9745  if (funcdef_flag)
9746    /* Make the init_value nonzero so pushdecl knows this is not
9747       tentative.  error_mark_node is replaced later with the BLOCK.  */
9748    DECL_INITIAL (decl) = error_mark_node;
9749
9750  if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9751    TREE_NOTHROW (decl) = 1;
9752
9753  /* Caller will do the rest of this.  */
9754  if (check < 0)
9755    return decl;
9756
9757  if (flags == NO_SPECIAL && ctype && constructor_name (ctype) == declarator)
9758    DECL_CONSTRUCTOR_P (decl) = 1;
9759
9760  /* Function gets the ugly name, field gets the nice one.  This call
9761     may change the type of the function (because of default
9762     parameters)!  */
9763  if (ctype != NULL_TREE)
9764    grokclassfn (ctype, decl, flags, quals);
9765
9766  decl = check_explicit_specialization (orig_declarator, decl,
9767					template_count,
9768					2 * (funcdef_flag != 0) +
9769					4 * (friendp != 0));
9770  if (decl == error_mark_node)
9771    return NULL_TREE;
9772
9773  if (ctype != NULL_TREE
9774      && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9775      && check)
9776    {
9777      tree old_decl;
9778
9779      old_decl = check_classfn (ctype, decl);
9780
9781      if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9782	/* Because grokfndecl is always supposed to return a
9783	   FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9784	   here.  We depend on our callers to figure out that its
9785	   really a template that's being returned.  */
9786	old_decl = DECL_TEMPLATE_RESULT (old_decl);
9787
9788      if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9789	  && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9790	{
9791	  /* Remove the `this' parm added by grokclassfn.
9792	     XXX Isn't this done in start_function, too?  */
9793	  revert_static_member_fn (decl);
9794	  last_function_parms = TREE_CHAIN (last_function_parms);
9795	}
9796      if (old_decl && DECL_ARTIFICIAL (old_decl))
9797	error ("definition of implicitly-declared `%D'", old_decl);
9798
9799      if (old_decl)
9800	{
9801	  /* Since we've smashed OLD_DECL to its
9802	     DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9803	  if (TREE_CODE (decl) == TEMPLATE_DECL)
9804	    decl = DECL_TEMPLATE_RESULT (decl);
9805
9806	  /* Attempt to merge the declarations.  This can fail, in
9807	     the case of some invalid specialization declarations.  */
9808	  if (!duplicate_decls (decl, old_decl))
9809	    error ("no `%#D' member function declared in class `%T'",
9810		      decl, ctype);
9811	  return old_decl;
9812	}
9813    }
9814
9815  if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9816    return NULL_TREE;
9817
9818  if (ctype == NULL_TREE || check)
9819    return decl;
9820
9821  if (virtualp)
9822    DECL_VIRTUAL_P (decl) = 1;
9823
9824  return decl;
9825}
9826
9827/* Create a VAR_DECL named NAME with the indicated TYPE.
9828
9829   If SCOPE is non-NULL, it is the class type or namespace containing
9830   the variable.  If SCOPE is NULL, the variable should is created in
9831   the innermost enclosings scope.  */
9832
9833static tree
9834grokvardecl (type, name, specbits_in, initialized, constp, scope)
9835     tree type;
9836     tree name;
9837     RID_BIT_TYPE *specbits_in;
9838     int initialized;
9839     int constp;
9840     tree scope;
9841{
9842  tree decl;
9843  RID_BIT_TYPE specbits;
9844
9845  my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9846		      20020808);
9847
9848  specbits = *specbits_in;
9849
9850  /* Compute the scope in which to place the variable.  */
9851  if (!scope)
9852    {
9853      /* An explicit "extern" specifier indicates a namespace-scope
9854	 variable.  */
9855      if (RIDBIT_SETP (RID_EXTERN, specbits))
9856	scope = current_namespace;
9857      else if (!at_function_scope_p ())
9858	{
9859	  scope = current_scope ();
9860	  if (!scope)
9861	    scope = current_namespace;
9862	}
9863    }
9864
9865  if (scope
9866      && (/* If the variable is a namespace-scope variable declared in a
9867	     template, we need DECL_LANG_SPECIFIC.  */
9868	  (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9869	  /* Similarly for namespace-scope variables with language linkage
9870	     other than C++.  */
9871	  || (TREE_CODE (scope) == NAMESPACE_DECL
9872	      && current_lang_name != lang_name_cplusplus)
9873	  /* Similarly for static data members.  */
9874	  || TYPE_P (scope)))
9875    decl = build_lang_decl (VAR_DECL, name, type);
9876  else
9877    decl = build_decl (VAR_DECL, name, type);
9878
9879  if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9880    set_decl_namespace (decl, scope, 0);
9881  else
9882    DECL_CONTEXT (decl) = scope;
9883
9884  if (name && scope && current_lang_name != lang_name_c)
9885    /* We can't mangle lazily here because we don't have any
9886       way to recover whether or not a variable was `extern
9887       "C"' later.  */
9888    mangle_decl (decl);
9889
9890  if (RIDBIT_SETP (RID_EXTERN, specbits))
9891    {
9892      DECL_THIS_EXTERN (decl) = 1;
9893      DECL_EXTERNAL (decl) = !initialized;
9894    }
9895
9896  /* In class context, static means one per class,
9897     public access, and static storage.  */
9898  if (DECL_CLASS_SCOPE_P (decl))
9899    {
9900      TREE_PUBLIC (decl) = 1;
9901      TREE_STATIC (decl) = 1;
9902      DECL_EXTERNAL (decl) = 0;
9903    }
9904  /* At top level, either `static' or no s.c. makes a definition
9905     (perhaps tentative), and absence of `static' makes it public.  */
9906  else if (toplevel_bindings_p ())
9907    {
9908      TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9909			    && (DECL_THIS_EXTERN (decl) || ! constp));
9910      TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9911    }
9912  /* Not at top level, only `static' makes a static definition.  */
9913  else
9914    {
9915      TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9916      TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9917    }
9918
9919  if (RIDBIT_SETP (RID_THREAD, specbits))
9920    {
9921      if (targetm.have_tls)
9922	DECL_THREAD_LOCAL (decl) = 1;
9923      else
9924	/* A mere warning is sure to result in improper semantics
9925	   at runtime.  Don't bother to allow this to compile.  */
9926	error ("thread-local storage not supported for this target");
9927    }
9928
9929  if (TREE_PUBLIC (decl))
9930    {
9931      /* [basic.link]: A name with no linkage (notably, the name of a class
9932	 or enumeration declared in a local scope) shall not be used to
9933	 declare an entity with linkage.
9934
9935	 Only check this for public decls for now.  */
9936      tree t = no_linkage_check (TREE_TYPE (decl));
9937      if (t)
9938	{
9939	  if (TYPE_ANONYMOUS_P (t))
9940	    /* Ignore for now; `enum { foo } e' is pretty common.  */;
9941	  else
9942	    pedwarn ("non-local variable `%#D' uses local type `%T'",
9943			decl, t);
9944	}
9945    }
9946
9947  return decl;
9948}
9949
9950/* Create and return a canonical pointer to member function type, for
9951   TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9952
9953tree
9954build_ptrmemfunc_type (tree type)
9955{
9956  tree fields[4];
9957  tree t;
9958  tree unqualified_variant = NULL_TREE;
9959
9960  if (type == error_mark_node)
9961    return type;
9962
9963  /* If a canonical type already exists for this type, use it.  We use
9964     this method instead of type_hash_canon, because it only does a
9965     simple equality check on the list of field members.  */
9966
9967  if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9968    return t;
9969
9970  /* Make sure that we always have the unqualified pointer-to-member
9971     type first.  */
9972  if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9973    unqualified_variant
9974      = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9975
9976  t = make_aggr_type (RECORD_TYPE);
9977  /* Let the front-end know this is a pointer to member function...  */
9978  TYPE_PTRMEMFUNC_FLAG (t) = 1;
9979  /* ... and not really an aggregate.  */
9980  SET_IS_AGGR_TYPE (t, 0);
9981
9982  fields[0] = build_decl (FIELD_DECL, pfn_identifier, type);
9983  fields[1] = build_decl (FIELD_DECL, delta_identifier,
9984			  delta_type_node);
9985  finish_builtin_type (t, "__ptrmemfunc_type", fields, 1, ptr_type_node);
9986
9987  /* Zap out the name so that the back-end will give us the debugging
9988     information for this anonymous RECORD_TYPE.  */
9989  TYPE_NAME (t) = NULL_TREE;
9990
9991  /* If this is not the unqualified form of this pointer-to-member
9992     type, set the TYPE_MAIN_VARIANT for this type to be the
9993     unqualified type.  Since they are actually RECORD_TYPEs that are
9994     not variants of each other, we must do this manually.  */
9995  if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9996    {
9997      t = build_qualified_type (t, cp_type_quals (type));
9998      TYPE_MAIN_VARIANT (t) = unqualified_variant;
9999      TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
10000      TYPE_NEXT_VARIANT (unqualified_variant) = t;
10001    }
10002
10003  /* Cache this pointer-to-member type so that we can find it again
10004     later.  */
10005  TYPE_SET_PTRMEMFUNC_TYPE (type, t);
10006
10007  /* Seems to be wanted.  */
10008  CLASSTYPE_GOT_SEMICOLON (t) = 1;
10009
10010  return t;
10011}
10012
10013/* Create and return a pointer to data member type.  */
10014
10015tree
10016build_ptrmem_type (tree class_type, tree member_type)
10017{
10018  return build_pointer_type (build_offset_type (class_type, member_type));
10019}
10020
10021/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10022   Check to see that the definition is valid.  Issue appropriate error
10023   messages.  Return 1 if the definition is particularly bad, or 0
10024   otherwise.  */
10025
10026int
10027check_static_variable_definition (decl, type)
10028     tree decl;
10029     tree type;
10030{
10031  /* Motion 10 at San Diego: If a static const integral data member is
10032     initialized with an integral constant expression, the initializer
10033     may appear either in the declaration (within the class), or in
10034     the definition, but not both.  If it appears in the class, the
10035     member is a member constant.  The file-scope definition is always
10036     required.  */
10037  if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10038    {
10039      error ("invalid in-class initialization of static data member of non-integral type `%T'",
10040	     type);
10041      /* If we just return the declaration, crashes will sometimes
10042	 occur.  We therefore return void_type_node, as if this was a
10043	 friend declaration, to cause callers to completely ignore
10044	 this declaration.  */
10045      return 1;
10046    }
10047  else if (!CP_TYPE_CONST_P (type))
10048    error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
10049	      decl);
10050  else if (pedantic && !INTEGRAL_TYPE_P (type))
10051    pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
10052
10053  return 0;
10054}
10055
10056/* Given the SIZE (i.e., number of elements) in an array, compute an
10057   appropriate index type for the array.  If non-NULL, NAME is the
10058   name of the thing being declared.  */
10059
10060tree
10061compute_array_index_type (name, size)
10062     tree name;
10063     tree size;
10064{
10065  tree itype;
10066
10067  /* If this involves a template parameter, it will be a constant at
10068     instantiation time, but we don't know what the value is yet.
10069     Even if no template parameters are involved, we may an expression
10070     that is not a constant; we don't even simplify `1 + 2' when
10071     processing a template.  */
10072  if (processing_template_decl)
10073    {
10074      /* Resolve a qualified reference to an enumerator or static
10075	 const data member of ours.  */
10076      if (TREE_CODE (size) == SCOPE_REF
10077	  && TREE_OPERAND (size, 0) == current_class_type)
10078	{
10079	  tree t = lookup_field (current_class_type,
10080				 TREE_OPERAND (size, 1), 0, 0);
10081	  if (t)
10082	    size = t;
10083	}
10084
10085      return build_index_type (build_min (MINUS_EXPR, sizetype,
10086					  size, integer_one_node));
10087    }
10088
10089  /* The size might be the result of a cast.  */
10090  STRIP_TYPE_NOPS (size);
10091
10092  /* It might be a const variable or enumeration constant.  */
10093  size = decl_constant_value (size);
10094
10095  /* The array bound must be an integer type.  */
10096  if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
10097      && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
10098      && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
10099    {
10100      if (name)
10101	error ("size of array `%D' has non-integer type", name);
10102      else
10103	error ("size of array has non-integer type");
10104      size = integer_one_node;
10105    }
10106
10107  /* Normally, the array-bound will be a constant.  */
10108  if (TREE_CODE (size) == INTEGER_CST)
10109    {
10110      /* Check to see if the array bound overflowed.  Make that an
10111	 error, no matter how generous we're being.  */
10112      int old_flag_pedantic_errors = flag_pedantic_errors;
10113      int old_pedantic = pedantic;
10114      pedantic = flag_pedantic_errors = 1;
10115      constant_expression_warning (size);
10116      pedantic = old_pedantic;
10117      flag_pedantic_errors = old_flag_pedantic_errors;
10118
10119      /* An array must have a positive number of elements.  */
10120      if (INT_CST_LT (size, integer_zero_node))
10121	{
10122	  if (name)
10123	    error ("size of array `%D' is negative", name);
10124	  else
10125	    error ("size of array is negative");
10126	  size = integer_one_node;
10127	}
10128      /* Except that an extension we allow zero-sized arrays.  We
10129	 always allow them in system headers because glibc uses
10130	 them.  */
10131      else if (integer_zerop (size) && pedantic && !in_system_header)
10132	{
10133	  if (name)
10134	    pedwarn ("ISO C++ forbids zero-size array `%D'", name);
10135	  else
10136	    pedwarn ("ISO C++ forbids zero-size array");
10137	}
10138    }
10139  else if (TREE_CONSTANT (size))
10140    {
10141      /* `(int) &fn' is not a valid array bound.  */
10142      if (name)
10143	error ("size of array `%D' is not an integral constant-expression",
10144		  name);
10145      else
10146	error ("size of array is not an integral constant-expression");
10147    }
10148
10149  /* Compute the index of the largest element in the array.  It is
10150     one less than the number of elements in the array.  */
10151  itype
10152    = fold (cp_build_binary_op (MINUS_EXPR,
10153				cp_convert (ssizetype, size),
10154				cp_convert (ssizetype,
10155					    integer_one_node)));
10156
10157  /* Check for variable-sized arrays.  We allow such things as an
10158     extension, even though they are not allowed in ANSI/ISO C++.  */
10159  if (!TREE_CONSTANT (itype))
10160    {
10161      if (pedantic)
10162	{
10163	  if (name)
10164	    pedwarn ("ISO C++ forbids variable-size array `%D'",
10165			name);
10166	  else
10167	    pedwarn ("ISO C++ forbids variable-size array");
10168	}
10169
10170      /* Create a variable-sized array index type.  */
10171      itype = variable_size (itype);
10172    }
10173  /* Make sure that there was no overflow when creating to a signed
10174     index type.  (For example, on a 32-bit machine, an array with
10175     size 2^32 - 1 is too big.)  */
10176  else if (TREE_OVERFLOW (itype))
10177    {
10178      error ("overflow in array dimension");
10179      TREE_OVERFLOW (itype) = 0;
10180    }
10181
10182  /* Create and return the appropriate index type.  */
10183  return build_index_type (itype);
10184}
10185
10186/* Returns an ARRAY_TYPE for an array with SIZE elements of the
10187   indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
10188   with this type.  */
10189
10190static tree
10191create_array_type_for_decl (name, type, size)
10192     tree name;
10193     tree type;
10194     tree size;
10195{
10196  tree itype = NULL_TREE;
10197  const char* error_msg;
10198
10199  /* If things have already gone awry, bail now.  */
10200  if (type == error_mark_node || size == error_mark_node)
10201    return error_mark_node;
10202
10203  /* Assume that everything will go OK.  */
10204  error_msg = NULL;
10205
10206  /* There are some types which cannot be array elements.  */
10207  switch (TREE_CODE (type))
10208    {
10209    case VOID_TYPE:
10210      error_msg = "array of void";
10211      break;
10212
10213    case FUNCTION_TYPE:
10214      error_msg = "array of functions";
10215      break;
10216
10217    case REFERENCE_TYPE:
10218      error_msg = "array of references";
10219      break;
10220
10221    case OFFSET_TYPE:
10222      error_msg = "array of data members";
10223      break;
10224
10225    case METHOD_TYPE:
10226      error_msg = "array of function members";
10227      break;
10228
10229    default:
10230      break;
10231    }
10232
10233  /* If something went wrong, issue an error-message and return.  */
10234  if (error_msg)
10235    {
10236      if (name)
10237	error ("declaration of `%D' as %s", name, error_msg);
10238      else
10239	error ("creating %s", error_msg);
10240
10241      return error_mark_node;
10242    }
10243
10244  /* [dcl.array]
10245
10246     The constant expressions that specify the bounds of the arrays
10247     can be omitted only for the first member of the sequence.  */
10248  if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10249    {
10250      if (name)
10251	error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
10252		  name);
10253      else
10254	error ("multidimensional array must have bounds for all dimensions except the first");
10255
10256      return error_mark_node;
10257    }
10258
10259  /* Figure out the index type for the array.  */
10260  if (size)
10261    itype = compute_array_index_type (name, size);
10262
10263  return build_cplus_array_type (type, itype);
10264}
10265
10266/* Check that it's OK to declare a function with the indicated TYPE.
10267   SFK indicates the kind of special function (if any) that this
10268   function is.  OPTYPE is the type given in a conversion operator
10269   declaration.  Returns the actual return type of the function; that
10270   may be different than TYPE if an error occurs, or for certain
10271   special functions.  */
10272
10273static tree
10274check_special_function_return_type (sfk, type, optype)
10275     special_function_kind sfk;
10276     tree type;
10277     tree optype;
10278{
10279  switch (sfk)
10280    {
10281    case sfk_constructor:
10282      if (type)
10283	error ("return type specification for constructor invalid");
10284
10285      type = void_type_node;
10286      break;
10287
10288    case sfk_destructor:
10289      if (type)
10290	error ("return type specification for destructor invalid");
10291      type = void_type_node;
10292      break;
10293
10294    case sfk_conversion:
10295      if (type && !same_type_p (type, optype))
10296	error ("operator `%T' declared to return `%T'", optype, type);
10297      else if (type)
10298	pedwarn ("return type specified for `operator %T'",  optype);
10299      type = optype;
10300      break;
10301
10302    default:
10303      abort ();
10304      break;
10305    }
10306
10307  return type;
10308}
10309
10310/* Given declspecs and a declarator,
10311   determine the name and type of the object declared
10312   and construct a ..._DECL node for it.
10313   (In one case we can return a ..._TYPE node instead.
10314    For invalid input we sometimes return 0.)
10315
10316   DECLSPECS is a chain of tree_list nodes whose value fields
10317    are the storage classes and type specifiers.
10318
10319   DECL_CONTEXT says which syntactic context this declaration is in:
10320     NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10321     FUNCDEF for a function definition.  Like NORMAL but a few different
10322      error messages in each case.  Return value may be zero meaning
10323      this definition is too screwy to try to parse.
10324     MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
10325      handle member functions (which have FIELD context).
10326      Return value may be zero meaning this definition is too screwy to
10327      try to parse.
10328     PARM for a parameter declaration (either within a function prototype
10329      or before a function body).  Make a PARM_DECL, or return void_type_node.
10330     CATCHPARM for a parameter declaration before a catch clause.
10331     TYPENAME if for a typename (in a cast or sizeof).
10332      Don't make a DECL node; just return the ..._TYPE node.
10333     FIELD for a struct or union field; make a FIELD_DECL.
10334     BITFIELD for a field with specified width.
10335   INITIALIZED is 1 if the decl has an initializer.
10336
10337   ATTRLIST is a pointer to the list of attributes, which may be NULL
10338   if there are none; *ATTRLIST may be modified if attributes from inside
10339   the declarator should be applied to the declaration.
10340
10341   In the TYPENAME case, DECLARATOR is really an abstract declarator.
10342   It may also be so in the PARM case, for a prototype where the
10343   argument type is specified but not the name.
10344
10345   This function is where the complicated C meanings of `static'
10346   and `extern' are interpreted.
10347
10348   For C++, if there is any monkey business to do, the function which
10349   calls this one must do it, i.e., prepending instance variables,
10350   renaming overloaded function names, etc.
10351
10352   Note that for this C++, it is an error to define a method within a class
10353   which does not belong to that class.
10354
10355   Except in the case where SCOPE_REFs are implicitly known (such as
10356   methods within a class being redundantly qualified),
10357   declarations which involve SCOPE_REFs are returned as SCOPE_REFs
10358   (class_name::decl_name).  The caller must also deal with this.
10359
10360   If a constructor or destructor is seen, and the context is FIELD,
10361   then the type gains the attribute TREE_HAS_x.  If such a declaration
10362   is erroneous, NULL_TREE is returned.
10363
10364   QUALS is used only for FUNCDEF and MEMFUNCDEF cases.  For a member
10365   function, these are the qualifiers to give to the `this' pointer. We
10366   apply TYPE_QUAL_RESTRICT to the this ptr, not the object.
10367
10368   May return void_type_node if the declarator turned out to be a friend.
10369   See grokfield for details.  */
10370
10371tree
10372grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
10373     tree declspecs;
10374     tree declarator;
10375     enum decl_context decl_context;
10376     int initialized;
10377     tree *attrlist;
10378{
10379  RID_BIT_TYPE specbits;
10380  int nclasses = 0;
10381  tree spec;
10382  tree type = NULL_TREE;
10383  int longlong = 0;
10384  int type_quals;
10385  int virtualp, explicitp, friendp, inlinep, staticp;
10386  int explicit_int = 0;
10387  int explicit_char = 0;
10388  int defaulted_int = 0;
10389  int extern_langp = 0;
10390  tree dependant_name = NULL_TREE;
10391
10392  tree typedef_decl = NULL_TREE;
10393  const char *name;
10394  tree typedef_type = NULL_TREE;
10395  int funcdef_flag = 0;
10396  enum tree_code innermost_code = ERROR_MARK;
10397  int bitfield = 0;
10398#if 0
10399  /* See the code below that used this.  */
10400  tree decl_attr = NULL_TREE;
10401#endif
10402  /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
10403     All FIELD_DECLs we build here have `init' put into their DECL_INITIAL.  */
10404  tree init = NULL_TREE;
10405
10406  /* Keep track of what sort of function is being processed
10407     so that we can warn about default return values, or explicit
10408     return values which do not match prescribed defaults.  */
10409  special_function_kind sfk = sfk_none;
10410
10411  tree dname = NULL_TREE;
10412  tree ctype = current_class_type;
10413  tree ctor_return_type = NULL_TREE;
10414  enum overload_flags flags = NO_SPECIAL;
10415  tree quals = NULL_TREE;
10416  tree raises = NULL_TREE;
10417  int template_count = 0;
10418  tree in_namespace = NULL_TREE;
10419  tree returned_attrs = NULL_TREE;
10420
10421  RIDBIT_RESET_ALL (specbits);
10422  if (decl_context == FUNCDEF)
10423    funcdef_flag = 1, decl_context = NORMAL;
10424  else if (decl_context == MEMFUNCDEF)
10425    funcdef_flag = -1, decl_context = FIELD;
10426  else if (decl_context == BITFIELD)
10427    bitfield = 1, decl_context = FIELD;
10428
10429  /* Look inside a declarator for the name being declared
10430     and get it as a string, for an error message.  */
10431  {
10432    tree *next = &declarator;
10433    register tree decl;
10434    name = NULL;
10435
10436    while (next && *next)
10437      {
10438	decl = *next;
10439	switch (TREE_CODE (decl))
10440	  {
10441	  case TREE_LIST:
10442	    /* For attributes.  */
10443	    next = &TREE_VALUE (decl);
10444	    break;
10445
10446	  case COND_EXPR:
10447	    ctype = NULL_TREE;
10448	    next = &TREE_OPERAND (decl, 0);
10449	    break;
10450
10451	  case BIT_NOT_EXPR:	/* For C++ destructors!  */
10452	    {
10453	      tree name = TREE_OPERAND (decl, 0);
10454	      tree rename = NULL_TREE;
10455
10456	      my_friendly_assert (flags == NO_SPECIAL, 152);
10457	      flags = DTOR_FLAG;
10458	      sfk = sfk_destructor;
10459	      if (TREE_CODE (name) == TYPE_DECL)
10460		TREE_OPERAND (decl, 0) = name = constructor_name (name);
10461	      my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
10462	      if (ctype == NULL_TREE)
10463		{
10464		  if (current_class_type == NULL_TREE)
10465		    {
10466		      error ("destructors must be member functions");
10467		      flags = NO_SPECIAL;
10468		    }
10469		  else
10470		    {
10471		      tree t = constructor_name (current_class_name);
10472		      if (t != name)
10473			rename = t;
10474		    }
10475		}
10476	      else
10477		{
10478		  tree t = constructor_name (ctype);
10479		  if (t != name)
10480		    rename = t;
10481		}
10482
10483	      if (rename)
10484		{
10485		  error ("destructor `%T' must match class name `%T'",
10486			    name, rename);
10487		  TREE_OPERAND (decl, 0) = rename;
10488		}
10489	      next = &name;
10490	    }
10491	    break;
10492
10493	  case ADDR_EXPR:	/* C++ reference declaration */
10494	    /* Fall through.  */
10495	  case ARRAY_REF:
10496	  case INDIRECT_REF:
10497	    ctype = NULL_TREE;
10498	    innermost_code = TREE_CODE (decl);
10499	    next = &TREE_OPERAND (decl, 0);
10500	    break;
10501
10502	  case CALL_EXPR:
10503	    if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl)))
10504	      {
10505		/* This is actually a variable declaration using
10506		   constructor syntax.  We need to call start_decl and
10507		   cp_finish_decl so we can get the variable
10508		   initialized...  */
10509
10510		tree attributes;
10511
10512		if (decl_context != NORMAL)
10513		  {
10514		    error ("variable declaration is not allowed here");
10515		    return error_mark_node;
10516		  }
10517
10518		*next = TREE_OPERAND (decl, 0);
10519		init = CALL_DECLARATOR_PARMS (decl);
10520
10521		if (attrlist)
10522		  {
10523		    attributes = *attrlist;
10524		  }
10525		else
10526		  {
10527		    attributes = NULL_TREE;
10528		  }
10529
10530		decl = start_decl (declarator, declspecs, 1,
10531				   attributes, NULL_TREE);
10532		decl_type_access_control (decl);
10533		if (decl)
10534		  {
10535		    /* Look for __unused__ attribute */
10536		    if (TREE_USED (TREE_TYPE (decl)))
10537		      TREE_USED (decl) = 1;
10538		    finish_decl (decl, init, NULL_TREE);
10539		  }
10540		else
10541		  error ("invalid declarator");
10542		return NULL_TREE;
10543	      }
10544	    innermost_code = TREE_CODE (decl);
10545	    if (decl_context == FIELD && ctype == NULL_TREE)
10546	      ctype = current_class_type;
10547	    if (ctype
10548		&& TREE_OPERAND (decl, 0)
10549		&& (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
10550		    && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
10551					   ctype)))
10552	      TREE_OPERAND (decl, 0) = constructor_name (ctype);
10553	    next = &TREE_OPERAND (decl, 0);
10554	    decl = *next;
10555	    if (ctype != NULL_TREE
10556		&& decl != NULL_TREE && flags != DTOR_FLAG
10557		&& decl == constructor_name (ctype))
10558	      {
10559		sfk = sfk_constructor;
10560		ctor_return_type = ctype;
10561	      }
10562	    ctype = NULL_TREE;
10563	    break;
10564
10565	  case TEMPLATE_ID_EXPR:
10566	      {
10567		tree fns = TREE_OPERAND (decl, 0);
10568
10569		if (TREE_CODE (fns) == LOOKUP_EXPR)
10570		  fns = TREE_OPERAND (fns, 0);
10571
10572		dname = fns;
10573		if (TREE_CODE (dname) == COMPONENT_REF)
10574		  dname = TREE_OPERAND (dname, 1);
10575		if (TREE_CODE (dname) != IDENTIFIER_NODE)
10576		  {
10577		    my_friendly_assert (is_overloaded_fn (dname),
10578					19990331);
10579		    dname = DECL_NAME (get_first_fn (dname));
10580		  }
10581	      }
10582	  /* Fall through.  */
10583
10584	  case IDENTIFIER_NODE:
10585	    if (TREE_CODE (decl) == IDENTIFIER_NODE)
10586	      dname = decl;
10587
10588	    next = 0;
10589
10590	    if (C_IS_RESERVED_WORD (dname))
10591	      {
10592		error ("declarator-id missing; using reserved word `%D'",
10593			  dname);
10594		name = IDENTIFIER_POINTER (dname);
10595	      }
10596	    else if (!IDENTIFIER_TYPENAME_P (dname))
10597	      name = IDENTIFIER_POINTER (dname);
10598	    else
10599	      {
10600		my_friendly_assert (flags == NO_SPECIAL, 154);
10601		flags = TYPENAME_FLAG;
10602		ctor_return_type = TREE_TYPE (dname);
10603		sfk = sfk_conversion;
10604		if (IDENTIFIER_GLOBAL_VALUE (dname)
10605		    && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
10606			== TYPE_DECL))
10607		  name = IDENTIFIER_POINTER (dname);
10608		else
10609		  name = "<invalid operator>";
10610	      }
10611	    break;
10612
10613	    /* C++ extension */
10614	  case SCOPE_REF:
10615	    {
10616	      /* Perform error checking, and decide on a ctype.  */
10617	      tree cname = TREE_OPERAND (decl, 0);
10618	      if (cname == NULL_TREE)
10619		ctype = NULL_TREE;
10620	      else if (TREE_CODE (cname) == NAMESPACE_DECL)
10621		{
10622		  ctype = NULL_TREE;
10623		  in_namespace = TREE_OPERAND (decl, 0);
10624		  TREE_OPERAND (decl, 0) = NULL_TREE;
10625		}
10626	      else if (! is_aggr_type (cname, 1))
10627		TREE_OPERAND (decl, 0) = NULL_TREE;
10628	      /* Must test TREE_OPERAND (decl, 1), in case user gives
10629		 us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
10630	      else if (TREE_OPERAND (decl, 1)
10631		       && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
10632		ctype = cname;
10633	      else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
10634		       || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
10635		{
10636	  	  /* This might be declaring a member of a template
10637		     parm to be a friend.  */
10638		  ctype = cname;
10639		  dependant_name = TREE_OPERAND (decl, 1);
10640		}
10641	      else if (ctype == NULL_TREE)
10642		ctype = cname;
10643	      else if (TREE_COMPLEXITY (decl) == current_class_depth)
10644		TREE_OPERAND (decl, 0) = ctype;
10645	      else
10646		{
10647		  if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
10648		    {
10649		      error ("type `%T' is not derived from type `%T'",
10650				cname, ctype);
10651		      TREE_OPERAND (decl, 0) = NULL_TREE;
10652		    }
10653		  else
10654		    ctype = cname;
10655		}
10656
10657	      /* If the parser sees something like "void a::b" where
10658		 "a::b" is a namespace, it will build a SCOPE_REF with
10659		 a NAMESPACE_DECL, rather than an IDENTIFIER_NODE, as
10660		 the second operand.  Since the SCOPE_REF is being
10661		 used as a declarator, we recover from that here.  */
10662	      if (TREE_CODE (TREE_OPERAND (decl, 1)) == NAMESPACE_DECL)
10663		TREE_OPERAND (decl, 1) = DECL_NAME (TREE_OPERAND (decl, 1));
10664
10665	      if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
10666		  && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
10667					 ctype))
10668		TREE_OPERAND (decl, 1) = constructor_name (ctype);
10669	      next = &TREE_OPERAND (decl, 1);
10670	      decl = *next;
10671	      if (ctype)
10672		{
10673		  if (TREE_CODE (decl) == IDENTIFIER_NODE
10674		      && constructor_name (ctype) == decl)
10675		    {
10676		      sfk = sfk_constructor;
10677		      ctor_return_type = ctype;
10678		    }
10679		  else if (TREE_CODE (decl) == BIT_NOT_EXPR
10680			   && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
10681			   && constructor_name_p (TREE_OPERAND (decl, 0),
10682						  ctype))
10683		    {
10684		      sfk = sfk_destructor;
10685		      ctor_return_type = ctype;
10686		      flags = DTOR_FLAG;
10687		      TREE_OPERAND (decl, 0) = constructor_name (ctype);
10688		      next = &TREE_OPERAND (decl, 0);
10689		    }
10690		}
10691	    }
10692	    break;
10693
10694	  case ERROR_MARK:
10695	    next = 0;
10696	    break;
10697
10698	  case TYPE_DECL:
10699	    /* Parse error puts this typespec where
10700	       a declarator should go.  */
10701	    error ("`%T' specified as declarator-id", DECL_NAME (decl));
10702	    if (TREE_TYPE (decl) == current_class_type)
10703	      error ("  perhaps you want `%T' for a constructor",
10704			current_class_name);
10705	    dname = DECL_NAME (decl);
10706	    name = IDENTIFIER_POINTER (dname);
10707
10708	    /* Avoid giving two errors for this.  */
10709	    IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
10710
10711	    declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
10712	    *next = dname;
10713	    next = 0;
10714	    break;
10715
10716	  case BASELINK:
10717	    next = &BASELINK_FUNCTIONS (decl);
10718	    break;
10719
10720	  case TEMPLATE_DECL:
10721	    /* Sometimes, we see a template-name used as part of a
10722	       decl-specifier like in
10723	          std::allocator alloc;
10724	       Handle that gracefully.  */
10725	    error ("invalid use of template-name '%E' in a declarator", decl);
10726	    return error_mark_node;
10727	    break;
10728
10729	  default:
10730	    my_friendly_assert (0, 20020917);
10731	  }
10732      }
10733  }
10734
10735  /* A function definition's declarator must have the form of
10736     a function declarator.  */
10737
10738  if (funcdef_flag && innermost_code != CALL_EXPR)
10739    return 0;
10740
10741  if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
10742      && innermost_code != CALL_EXPR
10743      && ! (ctype && declspecs == NULL_TREE))
10744    {
10745      error ("declaration of `%D' as non-function", dname);
10746      return void_type_node;
10747    }
10748
10749  /* Anything declared one level down from the top level
10750     must be one of the parameters of a function
10751     (because the body is at least two levels down).  */
10752
10753  /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10754     by not allowing C++ class definitions to specify their parameters
10755     with xdecls (must be spec.d in the parmlist).
10756
10757     Since we now wait to push a class scope until we are sure that
10758     we are in a legitimate method context, we must set oldcname
10759     explicitly (since current_class_name is not yet alive).
10760
10761     We also want to avoid calling this a PARM if it is in a namespace.  */
10762
10763  if (decl_context == NORMAL && !toplevel_bindings_p ())
10764    {
10765      struct cp_binding_level *b = current_binding_level;
10766      current_binding_level = b->level_chain;
10767      if (current_binding_level != 0 && toplevel_bindings_p ())
10768	decl_context = PARM;
10769      current_binding_level = b;
10770    }
10771
10772  if (name == NULL)
10773    name = decl_context == PARM ? "parameter" : "type name";
10774
10775  /* Look through the decl specs and record which ones appear.
10776     Some typespecs are defined as built-in typenames.
10777     Others, the ones that are modifiers of other types,
10778     are represented by bits in SPECBITS: set the bits for
10779     the modifiers that appear.  Storage class keywords are also in SPECBITS.
10780
10781     If there is a typedef name or a type, store the type in TYPE.
10782     This includes builtin typedefs such as `int'.
10783
10784     Set EXPLICIT_INT if the type is `int' or `char' and did not
10785     come from a user typedef.
10786
10787     Set LONGLONG if `long' is mentioned twice.
10788
10789     For C++, constructors and destructors have their own fast treatment.  */
10790
10791  for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10792    {
10793      register int i;
10794      register tree id;
10795
10796      /* Certain parse errors slip through.  For example,
10797	 `int class;' is not caught by the parser. Try
10798	 weakly to recover here.  */
10799      if (TREE_CODE (spec) != TREE_LIST)
10800	return 0;
10801
10802      id = TREE_VALUE (spec);
10803
10804      /* If the entire declaration is itself tagged as deprecated then
10805         suppress reports of deprecated items.  */
10806      if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10807        {
10808	  if (deprecated_state != DEPRECATED_SUPPRESS)
10809	    warn_deprecated_use (id);
10810        }
10811
10812      if (TREE_CODE (id) == IDENTIFIER_NODE)
10813	{
10814	  if (id == ridpointers[(int) RID_INT]
10815	      || id == ridpointers[(int) RID_CHAR]
10816	      || id == ridpointers[(int) RID_BOOL]
10817	      || id == ridpointers[(int) RID_WCHAR])
10818	    {
10819	      if (type)
10820		{
10821		  if (id == ridpointers[(int) RID_BOOL])
10822		    error ("`bool' is now a keyword");
10823		  else
10824		    error ("extraneous `%T' ignored", id);
10825		}
10826	      else
10827		{
10828		  if (id == ridpointers[(int) RID_INT])
10829		    explicit_int = 1;
10830		  else if (id == ridpointers[(int) RID_CHAR])
10831		    explicit_char = 1;
10832		  type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10833		}
10834	      goto found;
10835	    }
10836	  /* C++ aggregate types.  */
10837	  if (IDENTIFIER_HAS_TYPE_VALUE (id))
10838	    {
10839	      if (type)
10840		error ("multiple declarations `%T' and `%T'", type, id);
10841	      else
10842		type = IDENTIFIER_TYPE_VALUE (id);
10843	      goto found;
10844	    }
10845
10846	  for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10847	    {
10848	      if (ridpointers[i] == id)
10849		{
10850		  if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10851		    {
10852		      if (pedantic && ! in_system_header && warn_long_long)
10853			pedwarn ("ISO C++ does not support `long long'");
10854		      if (longlong)
10855			error ("`long long long' is too long for GCC");
10856		      else
10857			longlong = 1;
10858		    }
10859		  else if (RIDBIT_SETP (i, specbits))
10860		    pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10861
10862		  /* Diagnose "__thread extern".  Recall that this list
10863		     is in the reverse order seen in the text.  */
10864		  if (i == (int)RID_THREAD)
10865		    {
10866		      if (RIDBIT_SETP (RID_EXTERN, specbits))
10867			error ("`__thread' before `extern'");
10868		      if (RIDBIT_SETP (RID_STATIC, specbits))
10869			error ("`__thread' before `static'");
10870		    }
10871
10872		  if (i == (int)RID_EXTERN
10873		      && TREE_PURPOSE (spec) == error_mark_node)
10874		    /* This extern was part of a language linkage.  */
10875		    extern_langp = 1;
10876
10877		  RIDBIT_SET (i, specbits);
10878		  goto found;
10879		}
10880	    }
10881	}
10882      else if (TREE_CODE (id) == TYPE_DECL)
10883	{
10884	  if (type)
10885	    error ("multiple declarations `%T' and `%T'", type,
10886		      TREE_TYPE (id));
10887	  else
10888	    {
10889	      type = TREE_TYPE (id);
10890	      TREE_VALUE (spec) = type;
10891	      typedef_decl = id;
10892	    }
10893	  goto found;
10894	}
10895      if (type)
10896	error ("two or more data types in declaration of `%s'", name);
10897      else if (TREE_CODE (id) == IDENTIFIER_NODE)
10898	{
10899	  register tree t = lookup_name (id, 1);
10900	  if (!t || TREE_CODE (t) != TYPE_DECL)
10901	    error ("`%s' fails to be a typedef or built in type",
10902		   IDENTIFIER_POINTER (id));
10903	  else
10904	    {
10905	      type = TREE_TYPE (t);
10906	      typedef_decl = t;
10907	    }
10908	}
10909      else if (id != error_mark_node)
10910	/* Can't change CLASS nodes into RECORD nodes here!  */
10911	type = id;
10912
10913    found: ;
10914    }
10915
10916#if 0
10917  /* See the code below that used this.  */
10918  if (typedef_decl)
10919    decl_attr = DECL_ATTRIBUTES (typedef_decl);
10920#endif
10921  typedef_type = type;
10922
10923  /* No type at all: default to `int', and set DEFAULTED_INT
10924     because it was not a user-defined typedef.  */
10925
10926  if (type == NULL_TREE
10927      && (RIDBIT_SETP (RID_SIGNED, specbits)
10928	  || RIDBIT_SETP (RID_UNSIGNED, specbits)
10929	  || RIDBIT_SETP (RID_LONG, specbits)
10930	  || RIDBIT_SETP (RID_SHORT, specbits)))
10931    {
10932      /* These imply 'int'.  */
10933      type = integer_type_node;
10934      defaulted_int = 1;
10935    }
10936
10937  if (sfk != sfk_none)
10938    type = check_special_function_return_type (sfk, type,
10939					       ctor_return_type);
10940  else if (type == NULL_TREE)
10941    {
10942      int is_main;
10943
10944      explicit_int = -1;
10945
10946      /* We handle `main' specially here, because 'main () { }' is so
10947	 common.  With no options, it is allowed.  With -Wreturn-type,
10948	 it is a warning.  It is only an error with -pedantic-errors.  */
10949      is_main = (funcdef_flag
10950		 && dname && MAIN_NAME_P (dname)
10951		 && ctype == NULL_TREE
10952		 && in_namespace == NULL_TREE
10953		 && current_namespace == global_namespace);
10954
10955      if (in_system_header || flag_ms_extensions)
10956	/* Allow it, sigh.  */;
10957      else if (pedantic || ! is_main)
10958	pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10959		    name);
10960      else if (warn_return_type)
10961	warning ("ISO C++ forbids declaration of `%s' with no type",
10962		    name);
10963
10964      type = integer_type_node;
10965    }
10966
10967  if (type && IMPLICIT_TYPENAME_P (type))
10968    {
10969      /* The implicit typename extension is deprecated and will be
10970	 removed.  Warn about its use now.  */
10971      warning ("`%T' is implicitly a typename", type);
10972      cp_deprecated ("implicit typename");
10973
10974      /* Now remove its implicitness, so that we don't warn again.
10975         For instance this might be a typedef, and we do not want to
10976         warn on uses of the typedef itself.  Simply clearing the
10977         TREE_TYPE is insufficient.  */
10978      type = copy_node (type);
10979      TREE_TYPE (type) = NULL_TREE;
10980    }
10981
10982  ctype = NULL_TREE;
10983
10984  /* Now process the modifiers that were specified
10985     and check for invalid combinations.  */
10986
10987  /* Long double is a special combination.  */
10988
10989  if (RIDBIT_SETP (RID_LONG, specbits)
10990      && TYPE_MAIN_VARIANT (type) == double_type_node)
10991    {
10992      RIDBIT_RESET (RID_LONG, specbits);
10993      type = build_qualified_type (long_double_type_node,
10994				   cp_type_quals (type));
10995    }
10996
10997  /* Check all other uses of type modifiers.  */
10998
10999  if (RIDBIT_SETP (RID_UNSIGNED, specbits)
11000      || RIDBIT_SETP (RID_SIGNED, specbits)
11001      || RIDBIT_SETP (RID_LONG, specbits)
11002      || RIDBIT_SETP (RID_SHORT, specbits))
11003    {
11004      int ok = 0;
11005
11006      if (TREE_CODE (type) == REAL_TYPE)
11007	error ("short, signed or unsigned invalid for `%s'", name);
11008      else if (TREE_CODE (type) != INTEGER_TYPE)
11009	error ("long, short, signed or unsigned invalid for `%s'", name);
11010      else if (RIDBIT_SETP (RID_LONG, specbits)
11011	       && RIDBIT_SETP (RID_SHORT, specbits))
11012	error ("long and short specified together for `%s'", name);
11013      else if ((RIDBIT_SETP (RID_LONG, specbits)
11014		|| RIDBIT_SETP (RID_SHORT, specbits))
11015	       && explicit_char)
11016	error ("long or short specified with char for `%s'", name);
11017      else if ((RIDBIT_SETP (RID_LONG, specbits)
11018		|| RIDBIT_SETP (RID_SHORT, specbits))
11019	       && TREE_CODE (type) == REAL_TYPE)
11020	error ("long or short specified with floating type for `%s'", name);
11021      else if (RIDBIT_SETP (RID_SIGNED, specbits)
11022	       && RIDBIT_SETP (RID_UNSIGNED, specbits))
11023	error ("signed and unsigned given together for `%s'", name);
11024      else
11025	{
11026	  ok = 1;
11027	  if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
11028	    {
11029	      pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
11030		       name);
11031	      if (flag_pedantic_errors)
11032		ok = 0;
11033	    }
11034	}
11035
11036      /* Discard the type modifiers if they are invalid.  */
11037      if (! ok)
11038	{
11039	  RIDBIT_RESET (RID_UNSIGNED, specbits);
11040	  RIDBIT_RESET (RID_SIGNED, specbits);
11041	  RIDBIT_RESET (RID_LONG, specbits);
11042	  RIDBIT_RESET (RID_SHORT, specbits);
11043	  longlong = 0;
11044	}
11045    }
11046
11047  if (RIDBIT_SETP (RID_COMPLEX, specbits)
11048      && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11049    {
11050      error ("complex invalid for `%s'", name);
11051      RIDBIT_RESET (RID_COMPLEX, specbits);
11052    }
11053
11054  /* Decide whether an integer type is signed or not.
11055     Optionally treat bitfields as signed by default.  */
11056  if (RIDBIT_SETP (RID_UNSIGNED, specbits)
11057      /* [class.bit]
11058
11059	 It is implementation-defined whether a plain (neither
11060	 explicitly signed or unsigned) char, short, int, or long
11061	 bit-field is signed or unsigned.
11062
11063	 Naturally, we extend this to long long as well.  Note that
11064	 this does not include wchar_t.  */
11065      || (bitfield && !flag_signed_bitfields
11066	  && RIDBIT_NOTSETP (RID_SIGNED, specbits)
11067	  /* A typedef for plain `int' without `signed' can be
11068	     controlled just like plain `int', but a typedef for
11069	     `signed int' cannot be so controlled.  */
11070	  && !(typedef_decl
11071	       && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11072	  && (TREE_CODE (type) == INTEGER_TYPE
11073	      || TREE_CODE (type) == CHAR_TYPE)
11074	  && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11075    {
11076      if (longlong)
11077	type = long_long_unsigned_type_node;
11078      else if (RIDBIT_SETP (RID_LONG, specbits))
11079	type = long_unsigned_type_node;
11080      else if (RIDBIT_SETP (RID_SHORT, specbits))
11081	type = short_unsigned_type_node;
11082      else if (type == char_type_node)
11083	type = unsigned_char_type_node;
11084      else if (typedef_decl)
11085	type = c_common_unsigned_type (type);
11086      else
11087	type = unsigned_type_node;
11088    }
11089  else if (RIDBIT_SETP (RID_SIGNED, specbits)
11090	   && type == char_type_node)
11091    type = signed_char_type_node;
11092  else if (longlong)
11093    type = long_long_integer_type_node;
11094  else if (RIDBIT_SETP (RID_LONG, specbits))
11095    type = long_integer_type_node;
11096  else if (RIDBIT_SETP (RID_SHORT, specbits))
11097    type = short_integer_type_node;
11098
11099  if (RIDBIT_SETP (RID_COMPLEX, specbits))
11100    {
11101      /* If we just have "complex", it is equivalent to
11102	 "complex double", but if any modifiers at all are specified it is
11103	 the complex form of TYPE.  E.g, "complex short" is
11104	 "complex short int".  */
11105
11106      if (defaulted_int && ! longlong
11107	  && ! (RIDBIT_SETP (RID_LONG, specbits)
11108		|| RIDBIT_SETP (RID_SHORT, specbits)
11109		|| RIDBIT_SETP (RID_SIGNED, specbits)
11110		|| RIDBIT_SETP (RID_UNSIGNED, specbits)))
11111	type = complex_double_type_node;
11112      else if (type == integer_type_node)
11113	type = complex_integer_type_node;
11114      else if (type == float_type_node)
11115	type = complex_float_type_node;
11116      else if (type == double_type_node)
11117	type = complex_double_type_node;
11118      else if (type == long_double_type_node)
11119	type = complex_long_double_type_node;
11120      else
11121	type = build_complex_type (type);
11122    }
11123
11124  type_quals = TYPE_UNQUALIFIED;
11125  if (RIDBIT_SETP (RID_CONST, specbits))
11126    type_quals |= TYPE_QUAL_CONST;
11127  if (RIDBIT_SETP (RID_VOLATILE, specbits))
11128    type_quals |= TYPE_QUAL_VOLATILE;
11129  if (RIDBIT_SETP (RID_RESTRICT, specbits))
11130    type_quals |= TYPE_QUAL_RESTRICT;
11131  if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
11132    error ("qualifiers are not allowed on declaration of `operator %T'",
11133	      ctor_return_type);
11134
11135  type_quals |= cp_type_quals (type);
11136  type = cp_build_qualified_type_real
11137    (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
11138 			 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
11139  /* We might have ignored or rejected some of the qualifiers.  */
11140  type_quals = cp_type_quals (type);
11141
11142  staticp = 0;
11143  inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
11144  virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
11145  RIDBIT_RESET (RID_VIRTUAL, specbits);
11146  explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
11147  RIDBIT_RESET (RID_EXPLICIT, specbits);
11148
11149  if (RIDBIT_SETP (RID_STATIC, specbits))
11150    staticp = 1 + (decl_context == FIELD);
11151
11152  if (virtualp && staticp == 2)
11153    {
11154      error ("member `%D' cannot be declared both virtual and static",
11155		dname);
11156      staticp = 0;
11157    }
11158  friendp = RIDBIT_SETP (RID_FRIEND, specbits);
11159  RIDBIT_RESET (RID_FRIEND, specbits);
11160
11161  if (dependant_name && !friendp)
11162    {
11163      error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
11164      return void_type_node;
11165    }
11166
11167  /* Warn if two storage classes are given. Default to `auto'.  */
11168
11169  if (RIDBIT_ANY_SET (specbits))
11170    {
11171      if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
11172      if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
11173      if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
11174      if (decl_context == PARM && nclasses > 0)
11175	error ("storage class specifiers invalid in parameter declarations");
11176      if (RIDBIT_SETP (RID_TYPEDEF, specbits))
11177	{
11178	  if (decl_context == PARM)
11179	    error ("typedef declaration invalid in parameter declaration");
11180	  nclasses++;
11181	}
11182      if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
11183      if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
11184      if (!nclasses && !friendp && extern_langp)
11185	nclasses++;
11186    }
11187
11188  /* Give error if `virtual' is used outside of class declaration.  */
11189  if (virtualp
11190      && (current_class_name == NULL_TREE || decl_context != FIELD))
11191    {
11192      error ("virtual outside class declaration");
11193      virtualp = 0;
11194    }
11195
11196  /* Static anonymous unions are dealt with here.  */
11197  if (staticp && decl_context == TYPENAME
11198      && TREE_CODE (declspecs) == TREE_LIST
11199      && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
11200    decl_context = FIELD;
11201
11202  /* Warn about storage classes that are invalid for certain
11203     kinds of declarations (parameters, typenames, etc.).  */
11204
11205  /* "static __thread" and "extern __thread" are allowed.  */
11206  if (nclasses == 2
11207      && RIDBIT_SETP (RID_THREAD, specbits)
11208      && (RIDBIT_SETP (RID_EXTERN, specbits)
11209	  || RIDBIT_SETP (RID_STATIC, specbits)))
11210    nclasses = 1;
11211
11212  if (nclasses > 1)
11213    error ("multiple storage classes in declaration of `%s'", name);
11214  else if (decl_context != NORMAL && nclasses > 0)
11215    {
11216      if ((decl_context == PARM || decl_context == CATCHPARM)
11217	  && (RIDBIT_SETP (RID_REGISTER, specbits)
11218	      || RIDBIT_SETP (RID_AUTO, specbits)))
11219	;
11220      else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
11221	;
11222      else if (decl_context == FIELD
11223	       /* C++ allows static class elements  */
11224	       && RIDBIT_SETP (RID_STATIC, specbits))
11225	/* C++ also allows inlines and signed and unsigned elements,
11226	   but in those cases we don't come in here.  */
11227	;
11228      else
11229	{
11230	  if (decl_context == FIELD)
11231	    {
11232	      tree tmp = NULL_TREE;
11233	      register int op = 0;
11234
11235	      if (declarator)
11236		{
11237		  /* Avoid trying to get an operand off an identifier node.  */
11238		  if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11239		    tmp = declarator;
11240		  else
11241		    tmp = TREE_OPERAND (declarator, 0);
11242		  op = IDENTIFIER_OPNAME_P (tmp);
11243		  if (IDENTIFIER_TYPENAME_P (tmp))
11244		    {
11245		      if (IDENTIFIER_GLOBAL_VALUE (tmp)
11246			  && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
11247			      == TYPE_DECL))
11248			name = IDENTIFIER_POINTER (tmp);
11249		      else
11250			name = "<invalid operator>";
11251		    }
11252		}
11253	      error ("storage class specified for %s `%s'",
11254		     op ? "member operator" : "field",
11255		     name);
11256	    }
11257	  else
11258	    {
11259	      if (decl_context == PARM || decl_context == CATCHPARM)
11260		error ("storage class specified for parameter `%s'", name);
11261	      else
11262		error ("storage class specified for typename");
11263	    }
11264	  RIDBIT_RESET (RID_REGISTER, specbits);
11265	  RIDBIT_RESET (RID_AUTO, specbits);
11266	  RIDBIT_RESET (RID_EXTERN, specbits);
11267	  RIDBIT_RESET (RID_THREAD, specbits);
11268	}
11269    }
11270  else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
11271    {
11272      if (toplevel_bindings_p ())
11273	{
11274	  /* It's common practice (and completely valid) to have a const
11275	     be initialized and declared extern.  */
11276	  if (!(type_quals & TYPE_QUAL_CONST))
11277	    warning ("`%s' initialized and declared `extern'", name);
11278	}
11279      else
11280	error ("`%s' has both `extern' and initializer", name);
11281    }
11282  else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
11283	   && ! toplevel_bindings_p ())
11284    error ("nested function `%s' declared `extern'", name);
11285  else if (toplevel_bindings_p ())
11286    {
11287      if (RIDBIT_SETP (RID_AUTO, specbits))
11288	error ("top-level declaration of `%s' specifies `auto'", name);
11289    }
11290  else if (RIDBIT_SETP (RID_THREAD, specbits)
11291	   && !RIDBIT_SETP (RID_EXTERN, specbits)
11292	   && !RIDBIT_SETP (RID_STATIC, specbits))
11293    {
11294      error ("function-scope `%s' implicitly auto and declared `__thread'",
11295	     name);
11296      RIDBIT_RESET (RID_THREAD, specbits);
11297    }
11298
11299  if (nclasses > 0 && friendp)
11300    error ("storage class specifiers invalid in friend function declarations");
11301
11302  /* Now figure out the structure of the declarator proper.
11303     Descend through it, creating more complex types, until we reach
11304     the declared identifier (or NULL_TREE, in an abstract declarator).  */
11305
11306  while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
11307	 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
11308    {
11309      /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
11310	 an INDIRECT_REF (for *...),
11311	 a CALL_EXPR (for ...(...)),
11312	 an identifier (for the name being declared)
11313	 or a null pointer (for the place in an absolute declarator
11314	 where the name was omitted).
11315	 For the last two cases, we have just exited the loop.
11316
11317	 For C++ it could also be
11318	 a SCOPE_REF (for class :: ...).  In this case, we have converted
11319	 sensible names to types, and those are the values we use to
11320	 qualify the member name.
11321	 an ADDR_EXPR (for &...),
11322	 a BIT_NOT_EXPR (for destructors)
11323
11324	 At this point, TYPE is the type of elements of an array,
11325	 or for a function to return, or for a pointer to point to.
11326	 After this sequence of ifs, TYPE is the type of the
11327	 array or function or pointer, and DECLARATOR has had its
11328	 outermost layer removed.  */
11329
11330      if (type == error_mark_node)
11331	{
11332	  if (TREE_CODE (declarator) == SCOPE_REF)
11333	    declarator = TREE_OPERAND (declarator, 1);
11334	  else
11335	    declarator = TREE_OPERAND (declarator, 0);
11336	  continue;
11337	}
11338      if (quals != NULL_TREE
11339	  && (declarator == NULL_TREE
11340	      || TREE_CODE (declarator) != SCOPE_REF))
11341	{
11342	  if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
11343	    ctype = TYPE_METHOD_BASETYPE (type);
11344	  if (ctype != NULL_TREE)
11345	    {
11346	      tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
11347	      grok_method_quals (ctype, dummy, quals);
11348	      type = TREE_TYPE (dummy);
11349	      ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
11350	      quals = NULL_TREE;
11351	    }
11352	}
11353
11354      switch (TREE_CODE (declarator))
11355	{
11356	case TREE_LIST:
11357	  {
11358	    /* We encode a declarator with embedded attributes using
11359	       a TREE_LIST.  */
11360	    tree attrs = TREE_PURPOSE (declarator);
11361	    tree inner_decl;
11362	    int attr_flags;
11363
11364	    declarator = TREE_VALUE (declarator);
11365	    inner_decl = declarator;
11366	    while (inner_decl != NULL_TREE
11367		   && TREE_CODE (inner_decl) == TREE_LIST)
11368	      inner_decl = TREE_VALUE (inner_decl);
11369	    attr_flags = 0;
11370	    if (inner_decl == NULL_TREE
11371		|| TREE_CODE (inner_decl) == IDENTIFIER_NODE)
11372	      attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
11373	    if (TREE_CODE (inner_decl) == CALL_EXPR)
11374	      attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
11375	    if (TREE_CODE (inner_decl) == ARRAY_REF)
11376	      attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
11377	    returned_attrs = decl_attributes (&type,
11378					      chainon (returned_attrs, attrs),
11379					      attr_flags);
11380	  }
11381	  break;
11382
11383	case ARRAY_REF:
11384	  {
11385	    register tree size;
11386
11387	    size = TREE_OPERAND (declarator, 1);
11388
11389	    /* VC++ spells a zero-sized array with [].  */
11390	    if (size == NULL_TREE && decl_context == FIELD && !	staticp
11391		&& ! RIDBIT_SETP (RID_TYPEDEF, specbits))
11392	      size = integer_zero_node;
11393
11394	    declarator = TREE_OPERAND (declarator, 0);
11395
11396	    type = create_array_type_for_decl (dname, type, size);
11397
11398	    ctype = NULL_TREE;
11399	  }
11400	  break;
11401
11402	case CALL_EXPR:
11403	  {
11404	    tree arg_types;
11405	    int funcdecl_p;
11406	    tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
11407	    tree inner_decl = TREE_OPERAND (declarator, 0);
11408
11409	    /* Declaring a function type.
11410	       Make sure we have a valid type for the function to return.  */
11411
11412	    /* We now know that the TYPE_QUALS don't apply to the
11413               decl, but to its return type.  */
11414	    type_quals = TYPE_UNQUALIFIED;
11415
11416	    /* Warn about some types functions can't return.  */
11417
11418	    if (TREE_CODE (type) == FUNCTION_TYPE)
11419	      {
11420		error ("`%s' declared as function returning a function", name);
11421		type = integer_type_node;
11422	      }
11423	    if (TREE_CODE (type) == ARRAY_TYPE)
11424	      {
11425		error ("`%s' declared as function returning an array", name);
11426		type = integer_type_node;
11427	      }
11428
11429	    if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
11430	      inner_decl = TREE_OPERAND (inner_decl, 1);
11431
11432	    if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
11433	      inner_decl = dname;
11434
11435	    /* Pick up type qualifiers which should be applied to `this'.  */
11436	    quals = CALL_DECLARATOR_QUALS (declarator);
11437
11438	    /* Pick up the exception specifications.  */
11439	    raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
11440
11441	    /* Say it's a definition only for the CALL_EXPR
11442	       closest to the identifier.  */
11443	    funcdecl_p
11444	      = inner_decl
11445	      && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
11446		  || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
11447		  || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
11448
11449	    if (ctype == NULL_TREE
11450		&& decl_context == FIELD
11451		&& funcdecl_p
11452		&& (friendp == 0 || dname == current_class_name))
11453	      ctype = current_class_type;
11454
11455	    if (ctype && sfk == sfk_conversion)
11456	      TYPE_HAS_CONVERSION (ctype) = 1;
11457	    if (ctype && constructor_name (ctype) == dname)
11458	      {
11459		/* We are within a class's scope. If our declarator name
11460		   is the same as the class name, and we are defining
11461		   a function, then it is a constructor/destructor, and
11462		   therefore returns a void type.  */
11463
11464		if (flags == DTOR_FLAG)
11465		  {
11466		    /* ISO C++ 12.4/2.  A destructor may not be
11467		       declared const or volatile.  A destructor may
11468		       not be static.  */
11469		    if (staticp == 2)
11470		      error ("destructor cannot be static member function");
11471		    if (quals)
11472		      {
11473			error ("destructors may not be `%s'",
11474				  IDENTIFIER_POINTER (TREE_VALUE (quals)));
11475			quals = NULL_TREE;
11476		      }
11477		    if (decl_context == FIELD)
11478		      {
11479			if (! member_function_or_else (ctype,
11480						       current_class_type,
11481						       flags))
11482			  return void_type_node;
11483		      }
11484		  }
11485		else            /* It's a constructor.  */
11486		  {
11487		    if (explicitp == 1)
11488		      explicitp = 2;
11489		    /* ISO C++ 12.1.  A constructor may not be
11490		       declared const or volatile.  A constructor may
11491		       not be virtual.  A constructor may not be
11492		       static.  */
11493		    if (staticp == 2)
11494		      error ("constructor cannot be static member function");
11495		    if (virtualp)
11496		      {
11497			pedwarn ("constructors cannot be declared virtual");
11498			virtualp = 0;
11499		      }
11500		    if (quals)
11501		      {
11502			error ("constructors may not be `%s'",
11503				  IDENTIFIER_POINTER (TREE_VALUE (quals)));
11504			quals = NULL_TREE;
11505		      }
11506		    {
11507		      RID_BIT_TYPE tmp_bits;
11508		      memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
11509		      RIDBIT_RESET (RID_INLINE, tmp_bits);
11510		      RIDBIT_RESET (RID_STATIC, tmp_bits);
11511		      if (RIDBIT_ANY_SET (tmp_bits))
11512			error ("return value type specifier for constructor ignored");
11513		    }
11514		    if (decl_context == FIELD)
11515		      {
11516			if (! member_function_or_else (ctype,
11517						       current_class_type,
11518						       flags))
11519			  return void_type_node;
11520			TYPE_HAS_CONSTRUCTOR (ctype) = 1;
11521			if (sfk != sfk_constructor)
11522			  return NULL_TREE;
11523		      }
11524		  }
11525		if (decl_context == FIELD)
11526		  staticp = 0;
11527	      }
11528	    else if (friendp)
11529	      {
11530		if (initialized)
11531		  error ("can't initialize friend function `%s'", name);
11532		if (virtualp)
11533		  {
11534		    /* Cannot be both friend and virtual.  */
11535		    error ("virtual functions cannot be friends");
11536		    RIDBIT_RESET (RID_FRIEND, specbits);
11537		    friendp = 0;
11538		  }
11539		if (decl_context == NORMAL)
11540		  error ("friend declaration not in class definition");
11541		if (current_function_decl && funcdef_flag)
11542		  error ("can't define friend function `%s' in a local class definition",
11543			    name);
11544	      }
11545
11546	    /* Construct the function type and go to the next
11547	       inner layer of declarator.  */
11548
11549	    declarator = TREE_OPERAND (declarator, 0);
11550
11551	    /* FIXME: This is where default args should be fully
11552	       processed.  */
11553
11554	    arg_types = grokparms (inner_parms);
11555
11556	    if (declarator && flags == DTOR_FLAG)
11557	      {
11558		/* A destructor declared in the body of a class will
11559		   be represented as a BIT_NOT_EXPR.  But, we just
11560		   want the underlying IDENTIFIER.  */
11561		if (TREE_CODE (declarator) == BIT_NOT_EXPR)
11562		  declarator = TREE_OPERAND (declarator, 0);
11563
11564                if (arg_types != void_list_node)
11565		  {
11566		    error ("destructors may not have parameters");
11567		    arg_types = void_list_node;
11568		    last_function_parms = NULL_TREE;
11569		  }
11570	      }
11571
11572	    /* ANSI says that `const int foo ();'
11573	       does not make the function foo const.  */
11574	    type = build_function_type (type, arg_types);
11575
11576	    {
11577	      tree t;
11578	      for (t = arg_types; t; t = TREE_CHAIN (t))
11579		if (TREE_PURPOSE (t)
11580		    && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
11581		  {
11582		    add_defarg_fn (type);
11583		    break;
11584		  }
11585	    }
11586	  }
11587	  break;
11588
11589	case ADDR_EXPR:
11590	case INDIRECT_REF:
11591	  /* Filter out pointers-to-references and references-to-references.
11592	     We can get these if a TYPE_DECL is used.  */
11593
11594	  if (TREE_CODE (type) == REFERENCE_TYPE)
11595	    {
11596	      error (TREE_CODE (declarator) == ADDR_EXPR
11597		     ? "cannot declare reference to `%#T'"
11598		     : "cannot declare pointer to `%#T'", type);
11599	      type = TREE_TYPE (type);
11600	    }
11601	  else if (VOID_TYPE_P (type)
11602		   && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
11603	    error (ctype ? "cannot declare pointer to `%#T' member"
11604		     : "cannot declare reference to `%#T'", type);
11605
11606	  /* Merge any constancy or volatility into the target type
11607	     for the pointer.  */
11608
11609	  /* We now know that the TYPE_QUALS don't apply to the decl,
11610	     but to the target of the pointer.  */
11611	  type_quals = TYPE_UNQUALIFIED;
11612
11613	  if (TREE_CODE (declarator) == ADDR_EXPR)
11614	    {
11615	      if (!VOID_TYPE_P (type))
11616		type = build_reference_type (type);
11617	    }
11618	  else if (TREE_CODE (type) == METHOD_TYPE)
11619	    type = build_ptrmemfunc_type (build_pointer_type (type));
11620	  else if (ctype)
11621	    type = build_ptrmem_type (ctype, type);
11622	  else
11623	    type = build_pointer_type (type);
11624
11625	  /* Process a list of type modifier keywords (such as
11626	     const or volatile) that were given inside the `*' or `&'.  */
11627
11628	  if (TREE_TYPE (declarator))
11629	    {
11630	      register tree typemodlist;
11631	      int erred = 0;
11632	      int constp = 0;
11633	      int volatilep = 0;
11634	      int restrictp = 0;
11635
11636	      for (typemodlist = TREE_TYPE (declarator); typemodlist;
11637		   typemodlist = TREE_CHAIN (typemodlist))
11638		{
11639		  tree qualifier = TREE_VALUE (typemodlist);
11640
11641		  if (qualifier == ridpointers[(int) RID_CONST])
11642		    {
11643		      constp++;
11644		      type_quals |= TYPE_QUAL_CONST;
11645		    }
11646		  else if (qualifier == ridpointers[(int) RID_VOLATILE])
11647		    {
11648		      volatilep++;
11649		      type_quals |= TYPE_QUAL_VOLATILE;
11650		    }
11651		  else if (qualifier == ridpointers[(int) RID_RESTRICT])
11652		    {
11653		      restrictp++;
11654		      type_quals |= TYPE_QUAL_RESTRICT;
11655		    }
11656		  else if (!erred)
11657		    {
11658		      erred = 1;
11659		      error ("invalid type modifier within pointer declarator");
11660		    }
11661		}
11662	      if (constp > 1)
11663		pedwarn ("duplicate `const'");
11664	      if (volatilep > 1)
11665		pedwarn ("duplicate `volatile'");
11666	      if (restrictp > 1)
11667		pedwarn ("duplicate `restrict'");
11668	      type = cp_build_qualified_type (type, type_quals);
11669	      type_quals = cp_type_quals (type);
11670	    }
11671	  declarator = TREE_OPERAND (declarator, 0);
11672	  ctype = NULL_TREE;
11673	  break;
11674
11675	case SCOPE_REF:
11676	  {
11677	    /* We have converted type names to NULL_TREE if the
11678	       name was bogus, or to a _TYPE node, if not.
11679
11680	       The variable CTYPE holds the type we will ultimately
11681	       resolve to.  The code here just needs to build
11682	       up appropriate member types.  */
11683	    tree sname = TREE_OPERAND (declarator, 1);
11684	    tree t;
11685
11686	    /* Destructors can have their visibilities changed as well.  */
11687	    if (TREE_CODE (sname) == BIT_NOT_EXPR)
11688	      sname = TREE_OPERAND (sname, 0);
11689
11690	    if (TREE_COMPLEXITY (declarator) == 0)
11691	      /* This needs to be here, in case we are called
11692		 multiple times.  */ ;
11693	    else if (TREE_COMPLEXITY (declarator) == -1)
11694	      /* Namespace member.  */
11695	      pop_decl_namespace ();
11696	    else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
11697	      /* Don't fall out into global scope. Hides real bug? --eichin */ ;
11698	    else if (!TREE_OPERAND (declarator, 0)
11699		     || !IS_AGGR_TYPE_CODE
11700		          (TREE_CODE (TREE_OPERAND (declarator, 0))))
11701	      ;
11702	    else if (TREE_COMPLEXITY (declarator) == current_class_depth)
11703	      {
11704		/* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
11705		   that refer to ctype.  They couldn't be resolved earlier
11706		   because we hadn't pushed into the class yet.
11707		   Example: resolve 'B<T>::type' in
11708		   'B<typename B<T>::type> B<T>::f () { }'.  */
11709		if (current_template_parms
11710		    && uses_template_parms (type)
11711		    && uses_template_parms (current_class_type))
11712		  {
11713		    tree args = current_template_args ();
11714		    type = tsubst (type, args, tf_error | tf_warning,
11715				   NULL_TREE);
11716		  }
11717
11718		/* This pop_nested_class corresponds to the
11719                   push_nested_class used to push into class scope for
11720                   parsing the argument list of a function decl, in
11721                   qualified_id.  */
11722		pop_nested_class ();
11723		TREE_COMPLEXITY (declarator) = current_class_depth;
11724	      }
11725	    else
11726	      abort ();
11727
11728	    if (TREE_OPERAND (declarator, 0) == NULL_TREE)
11729	      {
11730		/* We had a reference to a global decl, or
11731		   perhaps we were given a non-aggregate typedef,
11732		   in which case we cleared this out, and should just
11733		   keep going as though it wasn't there.  */
11734		declarator = sname;
11735		continue;
11736	      }
11737	    ctype = TREE_OPERAND (declarator, 0);
11738
11739	    t = ctype;
11740	    while (t != NULL_TREE && CLASS_TYPE_P (t))
11741	      {
11742		/* You're supposed to have one `template <...>'
11743		   for every template class, but you don't need one
11744		   for a full specialization.  For example:
11745
11746		     template <class T> struct S{};
11747		     template <> struct S<int> { void f(); };
11748		     void S<int>::f () {}
11749
11750		   is correct; there shouldn't be a `template <>' for
11751		   the definition of `S<int>::f'.  */
11752		if (CLASSTYPE_TEMPLATE_INFO (t)
11753		    && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
11754			|| uses_template_parms (CLASSTYPE_TI_ARGS (t)))
11755	            && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
11756		  template_count += 1;
11757
11758		t = TYPE_MAIN_DECL (t);
11759		t = DECL_CONTEXT (t);
11760	      }
11761
11762	    if (sname == NULL_TREE)
11763	      goto done_scoping;
11764
11765	    if (TREE_CODE (sname) == IDENTIFIER_NODE)
11766	      {
11767		/* This is the `standard' use of the scoping operator:
11768		   basetype :: member .  */
11769
11770		if (ctype == current_class_type)
11771		  {
11772		    /* class A {
11773		         void A::f ();
11774		       };
11775
11776		       Is this ill-formed?  */
11777
11778		    if (pedantic)
11779		      pedwarn ("extra qualification `%T::' on member `%s' ignored",
11780				  ctype, name);
11781		  }
11782		else if (TREE_CODE (type) == FUNCTION_TYPE)
11783		  {
11784		    if (current_class_type == NULL_TREE || friendp)
11785		      type = build_cplus_method_type (ctype, TREE_TYPE (type),
11786						      TYPE_ARG_TYPES (type));
11787		    else
11788		      {
11789			error ("cannot declare member function `%T::%s' within `%T'",
11790				  ctype, name, current_class_type);
11791			return void_type_node;
11792		      }
11793		  }
11794		else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
11795			 || COMPLETE_TYPE_P (complete_type (ctype)))
11796		  {
11797		    /* Have to move this code elsewhere in this function.
11798		       this code is used for i.e., typedef int A::M; M *pm;
11799
11800		       It is?  How? jason 10/2/94 */
11801
11802		    if (current_class_type)
11803		      {
11804			error ("cannot declare member `%T::%s' within `%T'",
11805				  ctype, name, current_class_type);
11806			return void_type_node;
11807		      }
11808		  }
11809		else
11810	          {
11811	            cxx_incomplete_type_error (NULL_TREE, ctype);
11812	            return error_mark_node;
11813		  }
11814
11815		declarator = sname;
11816	      }
11817	    else if (TREE_CODE (sname) == SCOPE_REF)
11818	      abort ();
11819	    else
11820	      {
11821	      done_scoping:
11822		declarator = TREE_OPERAND (declarator, 1);
11823		if (declarator && TREE_CODE (declarator) == CALL_EXPR)
11824		  /* In this case, we will deal with it later.  */
11825		  ;
11826		else if (TREE_CODE (type) == FUNCTION_TYPE)
11827		  type = build_cplus_method_type (ctype, TREE_TYPE (type),
11828						  TYPE_ARG_TYPES (type));
11829	      }
11830	  }
11831	  break;
11832
11833	case BIT_NOT_EXPR:
11834	  declarator = TREE_OPERAND (declarator, 0);
11835	  break;
11836
11837	case BASELINK:
11838	  declarator = BASELINK_FUNCTIONS (declarator);
11839	  break;
11840
11841	case RECORD_TYPE:
11842	case UNION_TYPE:
11843	case ENUMERAL_TYPE:
11844	  declarator = NULL_TREE;
11845	  break;
11846
11847	case ERROR_MARK:
11848	  declarator = NULL_TREE;
11849	  break;
11850
11851	default:
11852	  abort ();
11853	}
11854    }
11855
11856  if (returned_attrs)
11857    {
11858      if (attrlist)
11859	*attrlist = chainon (returned_attrs, *attrlist);
11860      else
11861	attrlist = &returned_attrs;
11862    }
11863
11864  /* Now TYPE has the actual type.  */
11865
11866  /* Did array size calculations overflow?  */
11867
11868  if (TREE_CODE (type) == ARRAY_TYPE
11869      && COMPLETE_TYPE_P (type)
11870      && TREE_OVERFLOW (TYPE_SIZE (type)))
11871    {
11872      error ("size of array `%s' is too large", name);
11873      /* If we proceed with the array type as it is, we'll eventually
11874	 crash in tree_low_cst().  */
11875      type = error_mark_node;
11876    }
11877
11878  if ((decl_context == FIELD || decl_context == PARM)
11879      && !processing_template_decl
11880      && variably_modified_type_p (type))
11881    {
11882      if (decl_context == FIELD)
11883	error ("data member may not have variably modified type `%T'", type);
11884      else
11885	error ("parameter may not have variably modified type `%T'", type);
11886      type = error_mark_node;
11887    }
11888
11889  if (explicitp == 1 || (explicitp && friendp))
11890    {
11891      /* [dcl.fct.spec] The explicit specifier shall only be used in
11892         declarations of constructors within a class definition.  */
11893      error ("only declarations of constructors can be `explicit'");
11894      explicitp = 0;
11895    }
11896
11897  if (RIDBIT_SETP (RID_MUTABLE, specbits))
11898    {
11899      if (decl_context != FIELD || friendp)
11900        {
11901	  error ("non-member `%s' cannot be declared `mutable'", name);
11902          RIDBIT_RESET (RID_MUTABLE, specbits);
11903        }
11904      else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11905	{
11906	  error ("non-object member `%s' cannot be declared `mutable'", name);
11907	  RIDBIT_RESET (RID_MUTABLE, specbits);
11908	}
11909      else if (TREE_CODE (type) == FUNCTION_TYPE
11910               || TREE_CODE (type) == METHOD_TYPE)
11911        {
11912	  error ("function `%s' cannot be declared `mutable'", name);
11913	  RIDBIT_RESET (RID_MUTABLE, specbits);
11914        }
11915      else if (staticp)
11916	{
11917	  error ("static `%s' cannot be declared `mutable'", name);
11918	  RIDBIT_RESET (RID_MUTABLE, specbits);
11919	}
11920      else if (type_quals & TYPE_QUAL_CONST)
11921	{
11922	  error ("const `%s' cannot be declared `mutable'", name);
11923 	  RIDBIT_RESET (RID_MUTABLE, specbits);
11924	}
11925    }
11926
11927  if (declarator == NULL_TREE
11928      || TREE_CODE (declarator) == IDENTIFIER_NODE
11929      || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11930	  && (TREE_CODE (type) == FUNCTION_TYPE
11931	      || TREE_CODE (type) == METHOD_TYPE)))
11932    /* OK */;
11933  else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11934    {
11935      error ("template-id `%D' used as a declarator", declarator);
11936      declarator = dname;
11937    }
11938  else
11939    /* Unexpected declarator format.  */
11940    abort ();
11941
11942  /* If this is declaring a typedef name, return a TYPE_DECL.  */
11943
11944  if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11945    {
11946      tree decl;
11947
11948      /* Note that the grammar rejects storage classes
11949	 in typenames, fields or parameters.  */
11950      if (current_lang_name == lang_name_java)
11951	TYPE_FOR_JAVA (type) = 1;
11952
11953      if (decl_context == FIELD)
11954	{
11955	  if (declarator == constructor_name (current_class_type))
11956	    pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11957			declarator);
11958	  decl = build_lang_decl (TYPE_DECL, declarator, type);
11959	}
11960      else
11961	{
11962	  decl = build_decl (TYPE_DECL, declarator, type);
11963	  if (!current_function_decl)
11964	    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11965	}
11966
11967      /* If the user declares "typedef struct {...} foo" then the
11968	 struct will have an anonymous name.  Fill that name in now.
11969	 Nothing can refer to it, so nothing needs know about the name
11970	 change.  */
11971      if (type != error_mark_node
11972	  && declarator
11973	  && TYPE_NAME (type)
11974	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11975	  && TYPE_ANONYMOUS_P (type)
11976	  /* Don't do this if there are attributes.  */
11977	  && (!attrlist || !*attrlist)
11978	  && cp_type_quals (type) == TYPE_UNQUALIFIED)
11979	{
11980	  tree oldname = TYPE_NAME (type);
11981	  tree t;
11982
11983	  /* Replace the anonymous name with the real name everywhere.  */
11984	  lookup_tag_reverse (type, declarator);
11985	  for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11986	    if (TYPE_NAME (t) == oldname)
11987	      TYPE_NAME (t) = decl;
11988
11989	  if (TYPE_LANG_SPECIFIC (type))
11990	    TYPE_WAS_ANONYMOUS (type) = 1;
11991
11992	  /* If this is a typedef within a template class, the nested
11993	     type is a (non-primary) template.  The name for the
11994	     template needs updating as well.  */
11995	  if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11996	    DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11997	      = TYPE_IDENTIFIER (type);
11998
11999	  /* FIXME remangle member functions; member functions of a
12000	     type with external linkage have external linkage.  */
12001	}
12002
12003      if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
12004	{
12005	  cp_error_at ("typedef name may not be class-qualified", decl);
12006	  return NULL_TREE;
12007	}
12008      else if (quals)
12009	{
12010	  if (ctype == NULL_TREE)
12011	    {
12012	      if (TREE_CODE (type) != METHOD_TYPE)
12013		cp_error_at ("invalid type qualifier for non-member function type", decl);
12014	      else
12015		ctype = TYPE_METHOD_BASETYPE (type);
12016	    }
12017	  if (ctype != NULL_TREE)
12018	    grok_method_quals (ctype, decl, quals);
12019	}
12020
12021      if (RIDBIT_SETP (RID_SIGNED, specbits)
12022	  || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12023	C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12024
12025      bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
12026		      inlinep, friendp, raises != NULL_TREE);
12027
12028      return decl;
12029    }
12030
12031  /* Detect the case of an array type of unspecified size
12032     which came, as such, direct from a typedef name.
12033     We must copy the type, so that the array's domain can be
12034     individually set by the object's initializer.  */
12035
12036  if (type && typedef_type
12037      && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12038      && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12039    type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12040
12041  /* Detect where we're using a typedef of function type to declare a
12042     function. last_function_parms will not be set, so we must create
12043     it now.  */
12044
12045  if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12046    {
12047      tree decls = NULL_TREE;
12048      tree args;
12049
12050      for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
12051	{
12052	  tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
12053
12054	  TREE_CHAIN (decl) = decls;
12055	  decls = decl;
12056	}
12057
12058      last_function_parms = nreverse (decls);
12059    }
12060
12061  /* If this is a type name (such as, in a cast or sizeof),
12062     compute the type and return it now.  */
12063
12064  if (decl_context == TYPENAME)
12065    {
12066      /* Note that the grammar rejects storage classes
12067	 in typenames, fields or parameters.  */
12068      if (type_quals != TYPE_UNQUALIFIED)
12069	type_quals = TYPE_UNQUALIFIED;
12070
12071      /* Special case: "friend class foo" looks like a TYPENAME context.  */
12072      if (friendp)
12073	{
12074	  if (type_quals != TYPE_UNQUALIFIED)
12075	    {
12076	      error ("type qualifiers specified for friend class declaration");
12077	      type_quals = TYPE_UNQUALIFIED;
12078	    }
12079	  if (inlinep)
12080	    {
12081	      error ("`inline' specified for friend class declaration");
12082	      inlinep = 0;
12083	    }
12084
12085	  if (!current_aggr)
12086	    {
12087	      /* Don't allow friend declaration without a class-key.  */
12088	      if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12089		pedwarn ("template parameters cannot be friends");
12090	      else if (TREE_CODE (type) == TYPENAME_TYPE)
12091	        pedwarn ("friend declaration requires class-key, "
12092			 "i.e. `friend class %T::%D'",
12093			 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
12094	      else
12095	        pedwarn ("friend declaration requires class-key, "
12096			 "i.e. `friend %#T'",
12097			 type);
12098	    }
12099
12100	  /* Only try to do this stuff if we didn't already give up.  */
12101	  if (type != integer_type_node)
12102	    {
12103	      decl_type_access_control (TYPE_NAME (type));
12104
12105	      /* A friendly class?  */
12106	      if (current_class_type)
12107		make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
12108	      else
12109		error ("trying to make class `%T' a friend of global scope",
12110		          type);
12111
12112	      type = void_type_node;
12113	    }
12114	}
12115      else if (quals)
12116	{
12117	  if (ctype == NULL_TREE)
12118	    {
12119	      if (TREE_CODE (type) != METHOD_TYPE)
12120	        error ("invalid qualifiers on non-member function type");
12121	      else
12122	        ctype = TYPE_METHOD_BASETYPE (type);
12123	    }
12124	  if (ctype)
12125	    {
12126	      tree dummy = build_decl (TYPE_DECL, declarator, type);
12127	      grok_method_quals (ctype, dummy, quals);
12128	      type = TREE_TYPE (dummy);
12129	    }
12130	}
12131
12132      return type;
12133    }
12134  else if (declarator == NULL_TREE && decl_context != PARM
12135	   && decl_context != CATCHPARM
12136	   && TREE_CODE (type) != UNION_TYPE
12137	   && ! bitfield)
12138    {
12139      error ("abstract declarator `%T' used as declaration", type);
12140      declarator = make_anon_name ();
12141    }
12142
12143  /* `void' at top level (not within pointer)
12144     is allowed only in typedefs or type names.
12145     We don't complain about parms either, but that is because
12146     a better error message can be made later.  */
12147
12148  if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
12149    {
12150      if (! declarator)
12151	error ("unnamed variable or field declared void");
12152      else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
12153	{
12154	  if (IDENTIFIER_OPNAME_P (declarator))
12155	    abort ();
12156	  else
12157	    error ("variable or field `%s' declared void", name);
12158	}
12159      else
12160	error ("variable or field declared void");
12161      type = integer_type_node;
12162    }
12163
12164  /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12165     or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
12166
12167  if (decl_context == PARM || decl_context == CATCHPARM)
12168    {
12169      if (ctype || in_namespace)
12170	error ("cannot use `::' in parameter declaration");
12171
12172      /* A parameter declared as an array of T is really a pointer to T.
12173	 One declared as a function is really a pointer to a function.
12174	 One declared as a member is really a pointer to member.  */
12175
12176      if (TREE_CODE (type) == ARRAY_TYPE)
12177	{
12178	  /* Transfer const-ness of array into that of type pointed to.  */
12179	  type = build_pointer_type (TREE_TYPE (type));
12180	  type_quals = TYPE_UNQUALIFIED;
12181	}
12182      else if (TREE_CODE (type) == FUNCTION_TYPE)
12183	type = build_pointer_type (type);
12184      else if (TREE_CODE (type) == OFFSET_TYPE)
12185	type = build_pointer_type (type);
12186    }
12187
12188  {
12189    register tree decl;
12190
12191    if (decl_context == PARM)
12192      {
12193	decl = cp_build_parm_decl (declarator, type);
12194
12195	bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
12196			inlinep, friendp, raises != NULL_TREE);
12197      }
12198    else if (decl_context == FIELD)
12199      {
12200	if (type == error_mark_node)
12201	  {
12202	    /* Happens when declaring arrays of sizes which
12203	       are error_mark_node, for example.  */
12204	    decl = NULL_TREE;
12205	  }
12206	else if (in_namespace && !friendp)
12207	  {
12208	    /* Something like struct S { int N::j; };  */
12209	    error ("invalid use of `::'");
12210	    decl = NULL_TREE;
12211	  }
12212	else if (TREE_CODE (type) == FUNCTION_TYPE)
12213	  {
12214	    int publicp = 0;
12215	    tree function_context;
12216
12217	    /* We catch the others as conflicts with the builtin
12218	       typedefs.  */
12219	    if (friendp && declarator == ridpointers[(int) RID_SIGNED])
12220	      {
12221		error ("function `%D' cannot be declared friend",
12222			  declarator);
12223		friendp = 0;
12224	      }
12225
12226	    if (friendp == 0)
12227	      {
12228		if (ctype == NULL_TREE)
12229		  ctype = current_class_type;
12230
12231		if (ctype == NULL_TREE)
12232		  {
12233		    error ("can't make `%D' into a method -- not in a class",
12234			      declarator);
12235		    return void_type_node;
12236		  }
12237
12238		/* ``A union may [ ... ] not [ have ] virtual functions.''
12239		   ARM 9.5 */
12240		if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
12241		  {
12242		    error ("function `%D' declared virtual inside a union",
12243			      declarator);
12244		    return void_type_node;
12245		  }
12246
12247		if (declarator == ansi_opname (NEW_EXPR)
12248		    || declarator == ansi_opname (VEC_NEW_EXPR)
12249		    || declarator == ansi_opname (DELETE_EXPR)
12250		    || declarator == ansi_opname (VEC_DELETE_EXPR))
12251		  {
12252		    if (virtualp)
12253		      {
12254			error ("`%D' cannot be declared virtual, since it is always static",
12255				  declarator);
12256			virtualp = 0;
12257		      }
12258		  }
12259		else if (staticp < 2)
12260		  type = build_cplus_method_type (ctype, TREE_TYPE (type),
12261						  TYPE_ARG_TYPES (type));
12262	      }
12263
12264	    /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
12265	    function_context = (ctype != NULL_TREE) ?
12266	      decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
12267	    publicp = (! friendp || ! staticp)
12268	      && function_context == NULL_TREE;
12269	    decl = grokfndecl (ctype, type,
12270			       TREE_CODE (declarator) != TEMPLATE_ID_EXPR
12271			       ? declarator : dname,
12272			       declarator,
12273			       virtualp, flags, quals, raises,
12274			       friendp ? -1 : 0, friendp, publicp, inlinep,
12275			       funcdef_flag, template_count, in_namespace);
12276	    if (decl == NULL_TREE)
12277	      return decl;
12278#if 0
12279	    /* This clobbers the attrs stored in `decl' from `attrlist'.  */
12280	    /* The decl and setting of decl_attr is also turned off.  */
12281	    decl = build_decl_attribute_variant (decl, decl_attr);
12282#endif
12283
12284	    /* [class.conv.ctor]
12285
12286	       A constructor declared without the function-specifier
12287	       explicit that can be called with a single parameter
12288	       specifies a conversion from the type of its first
12289	       parameter to the type of its class.  Such a constructor
12290	       is called a converting constructor.  */
12291	    if (explicitp == 2)
12292	      DECL_NONCONVERTING_P (decl) = 1;
12293	    else if (DECL_CONSTRUCTOR_P (decl))
12294	      {
12295		/* The constructor can be called with exactly one
12296		   parameter if there is at least one parameter, and
12297		   any subsequent parameters have default arguments.
12298		   Ignore any compiler-added parms.  */
12299		tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
12300
12301		if (arg_types == void_list_node
12302		    || (arg_types
12303			&& TREE_CHAIN (arg_types)
12304			&& TREE_CHAIN (arg_types) != void_list_node
12305			&& !TREE_PURPOSE (TREE_CHAIN (arg_types))))
12306		  DECL_NONCONVERTING_P (decl) = 1;
12307	      }
12308	  }
12309	else if (TREE_CODE (type) == METHOD_TYPE)
12310	  {
12311	    /* We only get here for friend declarations of
12312	       members of other classes.  */
12313	    /* All method decls are public, so tell grokfndecl to set
12314	       TREE_PUBLIC, also.  */
12315	    decl = grokfndecl (ctype, type, declarator, declarator,
12316			       virtualp, flags, quals, raises,
12317			       friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
12318			       template_count, in_namespace);
12319	    if (decl == NULL_TREE)
12320	      return NULL_TREE;
12321	  }
12322	else if (!staticp && ! processing_template_decl
12323		 && !COMPLETE_TYPE_P (complete_type (type))
12324		 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
12325	  {
12326	    if (declarator)
12327	      error ("field `%D' has incomplete type", declarator);
12328	    else
12329	      error ("name `%T' has incomplete type", type);
12330
12331	    /* If we're instantiating a template, tell them which
12332	       instantiation made the field's type be incomplete.  */
12333	    if (current_class_type
12334		&& TYPE_NAME (current_class_type)
12335		&& IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
12336		&& declspecs && TREE_VALUE (declspecs)
12337		&& TREE_TYPE (TREE_VALUE (declspecs)) == type)
12338	      error ("  in instantiation of template `%T'",
12339			current_class_type);
12340
12341	    type = error_mark_node;
12342	    decl = NULL_TREE;
12343	  }
12344	else
12345	  {
12346	    if (friendp)
12347	      {
12348		error ("`%s' is neither function nor member function; cannot be declared friend",
12349		       IDENTIFIER_POINTER (declarator));
12350		friendp = 0;
12351	      }
12352	    decl = NULL_TREE;
12353	  }
12354
12355	if (friendp)
12356	  {
12357	    /* Friends are treated specially.  */
12358	    if (ctype == current_class_type)
12359	      warning ("member functions are implicitly friends of their class");
12360 	    else
12361 	      {
12362 		tree t = NULL_TREE;
12363 		if (decl && DECL_NAME (decl))
12364 		  {
12365 		    if (template_class_depth (current_class_type) == 0)
12366 		      {
12367 			decl
12368 			  = check_explicit_specialization
12369 			  (declarator, decl,
12370 			   template_count, 2 * (funcdef_flag != 0) + 4);
12371 			if (decl == error_mark_node)
12372 			  return error_mark_node;
12373 		      }
12374
12375 		    t = do_friend (ctype, declarator, decl,
12376 				   last_function_parms, *attrlist,
12377				   flags, quals, funcdef_flag);
12378 		  }
12379 		if (t && funcdef_flag)
12380 		  return t;
12381
12382 		return void_type_node;
12383 	      }
12384	  }
12385
12386	/* Structure field.  It may not be a function, except for C++ */
12387
12388	if (decl == NULL_TREE)
12389	  {
12390	    if (initialized)
12391	      {
12392		if (!staticp)
12393		  {
12394		    /* An attempt is being made to initialize a non-static
12395		       member.  But, from [class.mem]:
12396
12397		       4 A member-declarator can contain a
12398		       constant-initializer only if it declares a static
12399		       member (_class.static_) of integral or enumeration
12400		       type, see _class.static.data_.
12401
12402		       This used to be relatively common practice, but
12403		       the rest of the compiler does not correctly
12404		       handle the initialization unless the member is
12405		       static so we make it static below.  */
12406		    pedwarn ("ISO C++ forbids initialization of member `%D'",
12407				declarator);
12408		    pedwarn ("making `%D' static", declarator);
12409		    staticp = 1;
12410		  }
12411
12412		if (uses_template_parms (type))
12413		  /* We'll check at instantiation time.  */
12414		  ;
12415		else if (check_static_variable_definition (declarator,
12416							   type))
12417		  /* If we just return the declaration, crashes
12418		     will sometimes occur.  We therefore return
12419		     void_type_node, as if this was a friend
12420		     declaration, to cause callers to completely
12421		     ignore this declaration.  */
12422		  return void_type_node;
12423	      }
12424
12425	    /* 9.2p13 [class.mem] */
12426	    if (declarator == constructor_name (current_class_type)
12427		/* The standard does not allow non-static data members
12428		   here either, but we agreed at the 10/99 meeting
12429		   to change that in TC 1 so that they are allowed in
12430		   classes with no user-defined constructors.  */
12431		&& staticp)
12432	      pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
12433			  declarator);
12434
12435	    if (staticp)
12436	      {
12437		/* C++ allows static class members.  All other work
12438		   for this is done by grokfield.  */
12439		decl = build_lang_decl (VAR_DECL, declarator, type);
12440		TREE_STATIC (decl) = 1;
12441		/* In class context, 'static' means public access.  */
12442		TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
12443	      }
12444	    else
12445	      {
12446		decl = build_decl (FIELD_DECL, declarator, type);
12447		DECL_NONADDRESSABLE_P (decl) = bitfield;
12448		if (RIDBIT_SETP (RID_MUTABLE, specbits))
12449		  {
12450		    DECL_MUTABLE_P (decl) = 1;
12451		    RIDBIT_RESET (RID_MUTABLE, specbits);
12452		  }
12453	      }
12454
12455	    bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
12456			    inlinep, friendp, raises != NULL_TREE);
12457	  }
12458      }
12459    else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
12460      {
12461	tree original_name;
12462	int publicp = 0;
12463
12464	if (! declarator)
12465	  return NULL_TREE;
12466
12467	if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
12468	  original_name = dname;
12469	else
12470	  original_name = declarator;
12471
12472	if (RIDBIT_SETP (RID_AUTO, specbits))
12473	  error ("storage class `auto' invalid for function `%s'", name);
12474	else if (RIDBIT_SETP (RID_REGISTER, specbits))
12475	  error ("storage class `register' invalid for function `%s'", name);
12476	else if (RIDBIT_SETP (RID_THREAD, specbits))
12477	  error ("storage class `__thread' invalid for function `%s'", name);
12478
12479	/* Function declaration not at top level.
12480	   Storage classes other than `extern' are not allowed
12481	   and `extern' makes no difference.  */
12482	if (! toplevel_bindings_p ()
12483	    && (RIDBIT_SETP (RID_STATIC, specbits)
12484		|| RIDBIT_SETP (RID_INLINE, specbits))
12485	    && pedantic)
12486	  {
12487	    if (RIDBIT_SETP (RID_STATIC, specbits))
12488	      pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
12489	    else
12490	      pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
12491	  }
12492
12493	if (ctype == NULL_TREE)
12494	  {
12495	    if (virtualp)
12496	      {
12497		error ("virtual non-class function `%s'", name);
12498		virtualp = 0;
12499	      }
12500	  }
12501	else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
12502	  type = build_cplus_method_type (ctype, TREE_TYPE (type),
12503					  TYPE_ARG_TYPES (type));
12504
12505	/* Record presence of `static'.  */
12506	publicp = (ctype != NULL_TREE
12507		   || RIDBIT_SETP (RID_EXTERN, specbits)
12508		   || !RIDBIT_SETP (RID_STATIC, specbits));
12509
12510	decl = grokfndecl (ctype, type, original_name, declarator,
12511			   virtualp, flags, quals, raises,
12512			   1, friendp,
12513			   publicp, inlinep, funcdef_flag,
12514			   template_count, in_namespace);
12515	if (decl == NULL_TREE)
12516	  return NULL_TREE;
12517
12518	if (staticp == 1)
12519	  {
12520	    int invalid_static = 0;
12521
12522	    /* Don't allow a static member function in a class, and forbid
12523	       declaring main to be static.  */
12524	    if (TREE_CODE (type) == METHOD_TYPE)
12525	      {
12526		pedwarn ("cannot declare member function `%D' to have static linkage", decl);
12527		invalid_static = 1;
12528	      }
12529	    else if (current_function_decl)
12530	      {
12531		/* FIXME need arm citation */
12532		error ("cannot declare static function inside another function");
12533		invalid_static = 1;
12534	      }
12535
12536	    if (invalid_static)
12537	      {
12538		staticp = 0;
12539		RIDBIT_RESET (RID_STATIC, specbits);
12540	      }
12541	  }
12542      }
12543    else
12544      {
12545	/* It's a variable.  */
12546
12547	/* An uninitialized decl with `extern' is a reference.  */
12548	decl = grokvardecl (type, declarator, &specbits,
12549			    initialized,
12550			    (type_quals & TYPE_QUAL_CONST) != 0,
12551			    ctype ? ctype : in_namespace);
12552	bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
12553			inlinep, friendp, raises != NULL_TREE);
12554
12555	if (ctype)
12556	  {
12557	    DECL_CONTEXT (decl) = ctype;
12558	    if (staticp == 1)
12559	      {
12560                pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
12561	        staticp = 0;
12562		RIDBIT_RESET (RID_STATIC, specbits);
12563	      }
12564	    if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
12565	      {
12566		error ("static member `%D' declared `register'", decl);
12567		RIDBIT_RESET (RID_REGISTER, specbits);
12568	      }
12569	    if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
12570	      {
12571	        pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
12572			    decl);
12573		RIDBIT_RESET (RID_EXTERN, specbits);
12574	      }
12575	  }
12576      }
12577
12578    my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
12579
12580    /* Record `register' declaration for warnings on &
12581       and in case doing stupid register allocation.  */
12582
12583    if (RIDBIT_SETP (RID_REGISTER, specbits))
12584      DECL_REGISTER (decl) = 1;
12585
12586    if (RIDBIT_SETP (RID_EXTERN, specbits))
12587      DECL_THIS_EXTERN (decl) = 1;
12588
12589    if (RIDBIT_SETP (RID_STATIC, specbits))
12590      DECL_THIS_STATIC (decl) = 1;
12591
12592    /* Record constancy and volatility.  There's no need to do this
12593       when processing a template; we'll do this for the instantiated
12594       declaration based on the type of DECL.  */
12595    if (!processing_template_decl)
12596      c_apply_type_quals_to_decl (type_quals, decl);
12597
12598    return decl;
12599  }
12600}
12601
12602/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
12603   An empty exprlist is a parmlist.  An exprlist which
12604   contains only identifiers at the global level
12605   is a parmlist.  Otherwise, it is an exprlist.  */
12606
12607int
12608parmlist_is_exprlist (exprs)
12609     tree exprs;
12610{
12611  if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
12612    return 0;
12613
12614  if (toplevel_bindings_p ())
12615    {
12616      /* At the global level, if these are all identifiers,
12617	 then it is a parmlist.  */
12618      while (exprs)
12619	{
12620	  if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
12621	    return 1;
12622	  exprs = TREE_CHAIN (exprs);
12623	}
12624      return 0;
12625    }
12626  return 1;
12627}
12628
12629/* Subroutine of start_function.  Ensure that each of the parameter
12630   types (as listed in PARMS) is complete, as is required for a
12631   function definition.  */
12632
12633static void
12634require_complete_types_for_parms (parms)
12635     tree parms;
12636{
12637  for (; parms; parms = TREE_CHAIN (parms))
12638    {
12639      if (VOID_TYPE_P (TREE_TYPE (parms)))
12640        /* grokparms will have already issued an error */
12641        TREE_TYPE (parms) = error_mark_node;
12642      else if (complete_type_or_else (TREE_TYPE (parms), parms))
12643	{
12644	  layout_decl (parms, 0);
12645	  DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12646	}
12647      else
12648        TREE_TYPE (parms) = error_mark_node;
12649    }
12650}
12651
12652/* Returns nonzero if T is a local variable.  */
12653
12654int
12655local_variable_p (t)
12656     tree t;
12657{
12658  if ((TREE_CODE (t) == VAR_DECL
12659       /* A VAR_DECL with a context that is a _TYPE is a static data
12660	  member.  */
12661       && !TYPE_P (CP_DECL_CONTEXT (t))
12662       /* Any other non-local variable must be at namespace scope.  */
12663       && !DECL_NAMESPACE_SCOPE_P (t))
12664      || (TREE_CODE (t) == PARM_DECL))
12665    return 1;
12666
12667  return 0;
12668}
12669
12670/* Returns nonzero if T is an automatic local variable or a label.
12671   (These are the declarations that need to be remapped when the code
12672   containing them is duplicated.)  */
12673
12674int
12675nonstatic_local_decl_p (t)
12676     tree t;
12677{
12678  return ((local_variable_p (t) && !TREE_STATIC (t))
12679	  || TREE_CODE (t) == LABEL_DECL
12680	  || TREE_CODE (t) == RESULT_DECL);
12681}
12682
12683/* Like local_variable_p, but suitable for use as a tree-walking
12684   function.  */
12685
12686static tree
12687local_variable_p_walkfn (tp, walk_subtrees, data)
12688     tree *tp;
12689     int *walk_subtrees ATTRIBUTE_UNUSED;
12690     void *data ATTRIBUTE_UNUSED;
12691{
12692  return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
12693	  ? *tp : NULL_TREE);
12694}
12695
12696/* Check that ARG, which is a default-argument expression for a
12697   parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
12698   something goes wrong.  DECL may also be a _TYPE node, rather than a
12699   DECL, if there is no DECL available.  */
12700
12701tree
12702check_default_argument (decl, arg)
12703     tree decl;
12704     tree arg;
12705{
12706  tree var;
12707  tree decl_type;
12708
12709  if (TREE_CODE (arg) == DEFAULT_ARG)
12710    /* We get a DEFAULT_ARG when looking at an in-class declaration
12711       with a default argument.  Ignore the argument for now; we'll
12712       deal with it after the class is complete.  */
12713    return arg;
12714
12715  if (processing_template_decl || uses_template_parms (arg))
12716    /* We don't do anything checking until instantiation-time.  Note
12717       that there may be uninstantiated arguments even for an
12718       instantiated function, since default arguments are not
12719       instantiated until they are needed.  */
12720    return arg;
12721
12722  if (TYPE_P (decl))
12723    {
12724      decl_type = decl;
12725      decl = NULL_TREE;
12726    }
12727  else
12728    decl_type = TREE_TYPE (decl);
12729
12730  if (arg == error_mark_node
12731      || decl == error_mark_node
12732      || TREE_TYPE (arg) == error_mark_node
12733      || decl_type == error_mark_node)
12734    /* Something already went wrong.  There's no need to check
12735       further.  */
12736    return error_mark_node;
12737
12738  /* [dcl.fct.default]
12739
12740     A default argument expression is implicitly converted to the
12741     parameter type.  */
12742  if (!TREE_TYPE (arg)
12743      || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
12744    {
12745      if (decl)
12746	error ("default argument for `%#D' has type `%T'",
12747		  decl, TREE_TYPE (arg));
12748      else
12749	error ("default argument for parameter of type `%T' has type `%T'",
12750		  decl_type, TREE_TYPE (arg));
12751
12752      return error_mark_node;
12753    }
12754
12755  /* [dcl.fct.default]
12756
12757     Local variables shall not be used in default argument
12758     expressions.
12759
12760     The keyword `this' shall not be used in a default argument of a
12761     member function.  */
12762  var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
12763				      NULL);
12764  if (var)
12765    {
12766      error ("default argument `%E' uses local variable `%D'",
12767		arg, var);
12768      return error_mark_node;
12769    }
12770
12771  /* All is well.  */
12772  return arg;
12773}
12774
12775/* Decode the list of parameter types for a function type.
12776   Given the list of things declared inside the parens,
12777   return a list of types.
12778
12779   We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
12780   flag. If unset, we append void_list_node. A parmlist declared
12781   as `(void)' is accepted as the empty parmlist.
12782
12783   Also set last_function_parms to the chain of PARM_DECLs.  */
12784
12785static tree
12786grokparms (first_parm)
12787     tree first_parm;
12788{
12789  tree result = NULL_TREE;
12790  tree decls = NULL_TREE;
12791  int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
12792  tree parm, chain;
12793  int any_error = 0;
12794
12795  my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
12796
12797  for (parm = first_parm; parm != NULL_TREE; parm = chain)
12798    {
12799      tree type = NULL_TREE;
12800      tree decl = TREE_VALUE (parm);
12801      tree init = TREE_PURPOSE (parm);
12802      tree specs, attrs;
12803
12804      chain = TREE_CHAIN (parm);
12805      /* @@ weak defense against parse errors.  */
12806      if (TREE_CODE (decl) != VOID_TYPE
12807	  && TREE_CODE (decl) != TREE_LIST)
12808	{
12809	  /* Give various messages as the need arises.  */
12810	  if (TREE_CODE (decl) == STRING_CST)
12811	    error ("invalid string constant `%E'", decl);
12812	  else if (TREE_CODE (decl) == INTEGER_CST)
12813	    error ("invalid integer constant in parameter list, did you forget to give parameter name?");
12814	  continue;
12815	}
12816
12817      if (parm == void_list_node)
12818        break;
12819
12820      split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
12821      decl = grokdeclarator (TREE_VALUE (decl), specs,
12822			     PARM, init != NULL_TREE, &attrs);
12823      if (! decl || TREE_TYPE (decl) == error_mark_node)
12824        continue;
12825
12826      if (attrs)
12827	cplus_decl_attributes (&decl, attrs, 0);
12828
12829      type = TREE_TYPE (decl);
12830      if (VOID_TYPE_P (type))
12831        {
12832          if (same_type_p (type, void_type_node)
12833              && !DECL_NAME (decl) && !result && !chain && !ellipsis)
12834            /* this is a parmlist of `(void)', which is ok.  */
12835            break;
12836          cxx_incomplete_type_error (decl, type);
12837	  /* It's not a good idea to actually create parameters of
12838	     type `void'; other parts of the compiler assume that a
12839	     void type terminates the parameter list.  */
12840	  type = error_mark_node;
12841	  TREE_TYPE (decl) = error_mark_node;
12842        }
12843
12844      if (type != error_mark_node)
12845	{
12846	  /* Top-level qualifiers on the parameters are
12847	     ignored for function types.  */
12848	  type = TYPE_MAIN_VARIANT (type);
12849	  if (TREE_CODE (type) == METHOD_TYPE)
12850	    {
12851	      error ("parameter `%D' invalidly declared method type", decl);
12852	      type = build_pointer_type (type);
12853	      TREE_TYPE (decl) = type;
12854	    }
12855	  else if (TREE_CODE (type) == OFFSET_TYPE)
12856	    {
12857	      error ("parameter `%D' invalidly declared offset type", decl);
12858	      type = build_pointer_type (type);
12859	      TREE_TYPE (decl) = type;
12860	    }
12861	  else if (abstract_virtuals_error (decl, type))
12862	    any_error = 1;  /* Seems like a good idea.  */
12863	  else if (POINTER_TYPE_P (type))
12864	    {
12865	      /* [dcl.fct]/6, parameter types cannot contain pointers
12866		 (references) to arrays of unknown bound.  */
12867	      tree t = TREE_TYPE (type);
12868	      int ptr = TYPE_PTR_P (type);
12869
12870              while (1)
12871                {
12872                  if (TYPE_PTR_P (t))
12873                    ptr = 1;
12874                  else if (TREE_CODE (t) != ARRAY_TYPE)
12875                    break;
12876                  else if (!TYPE_DOMAIN (t))
12877	            break;
12878	          t = TREE_TYPE (t);
12879	        }
12880	      if (TREE_CODE (t) == ARRAY_TYPE)
12881		error ("parameter `%D' includes %s to array of unknown bound `%T'",
12882			  decl, ptr ? "pointer" : "reference", t);
12883	    }
12884
12885	  if (!any_error && init)
12886	    init = check_default_argument (decl, init);
12887	  else
12888	    init = NULL_TREE;
12889	}
12890
12891      TREE_CHAIN (decl) = decls;
12892      decls = decl;
12893      result = tree_cons (init, type, result);
12894    }
12895  decls = nreverse (decls);
12896  result = nreverse (result);
12897  if (!ellipsis)
12898    result = chainon (result, void_list_node);
12899  last_function_parms = decls;
12900
12901  return result;
12902}
12903
12904
12905/* D is a constructor or overloaded `operator='.
12906
12907   Let T be the class in which D is declared. Then, this function
12908   returns:
12909
12910   -1 if D's is an ill-formed constructor or copy assignment operator
12911      whose first parameter is of type `T'.
12912   0  if D is not a copy constructor or copy assignment
12913      operator.
12914   1  if D is a copy constructor or copy assignment operator whose
12915      first parameter is a reference to const qualified T.
12916   2  if D is a copy constructor or copy assignment operator whose
12917      first parameter is a reference to non-const qualified T.
12918
12919   This function can be used as a predicate. Positive values indicate
12920   a copy constructor and nonzero values indicate a copy assignment
12921   operator.  */
12922
12923int
12924copy_fn_p (d)
12925     tree d;
12926{
12927  tree args;
12928  tree arg_type;
12929  int result = 1;
12930
12931  my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12932
12933  if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12934    /* Instantiations of template member functions are never copy
12935       functions.  Note that member functions of templated classes are
12936       represented as template functions internally, and we must
12937       accept those as copy functions.  */
12938    return 0;
12939
12940  args = FUNCTION_FIRST_USER_PARMTYPE (d);
12941  if (!args)
12942    return 0;
12943
12944  arg_type = TREE_VALUE (args);
12945
12946  if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12947    {
12948      /* Pass by value copy assignment operator.  */
12949      result = -1;
12950    }
12951  else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12952	   && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12953    {
12954      if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12955	result = 2;
12956    }
12957  else
12958    return 0;
12959
12960  args = TREE_CHAIN (args);
12961
12962  if (args && args != void_list_node && !TREE_PURPOSE (args))
12963    /* There are more non-optional args.  */
12964    return 0;
12965
12966  return result;
12967}
12968
12969/* Remember any special properties of member function DECL.  */
12970
12971void grok_special_member_properties (decl)
12972     tree decl;
12973{
12974  if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12975    ; /* Not special.  */
12976  else if (DECL_CONSTRUCTOR_P (decl))
12977    {
12978      int ctor = copy_fn_p (decl);
12979
12980      if (ctor > 0)
12981	{
12982	  /* [class.copy]
12983
12984     	     A non-template constructor for class X is a copy
12985     	     constructor if its first parameter is of type X&, const
12986     	     X&, volatile X& or const volatile X&, and either there
12987     	     are no other parameters or else all other parameters have
12988     	     default arguments.  */
12989	  TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12990	  if (ctor > 1)
12991	    TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12992	}
12993      else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12994	TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12995    }
12996  else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12997    {
12998      /* [class.copy]
12999
13000     	 A non-template assignment operator for class X is a copy
13001     	 assignment operator if its parameter is of type X, X&, const
13002     	 X&, volatile X& or const volatile X&.  */
13003
13004      int assop = copy_fn_p (decl);
13005
13006      if (assop)
13007	{
13008	  TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
13009	  if (assop != 1)
13010	    TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
13011	  if (DECL_PURE_VIRTUAL_P (decl))
13012	    TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
13013	}
13014    }
13015}
13016
13017/* Check a constructor DECL has the correct form.  Complains
13018   if the class has a constructor of the form X(X).  */
13019
13020int
13021grok_ctor_properties (ctype, decl)
13022     tree ctype, decl;
13023{
13024  int ctor_parm = copy_fn_p (decl);
13025
13026  if (ctor_parm < 0)
13027    {
13028      /* [class.copy]
13029
13030     	 A declaration of a constructor for a class X is ill-formed if
13031     	 its first parameter is of type (optionally cv-qualified) X
13032     	 and either there are no other parameters or else all other
13033     	 parameters have default arguments.
13034
13035     	 We *don't* complain about member template instantiations that
13036     	 have this form, though; they can occur as we try to decide
13037     	 what constructor to use during overload resolution.  Since
13038     	 overload resolution will never prefer such a constructor to
13039     	 the non-template copy constructor (which is either explicitly
13040     	 or implicitly defined), there's no need to worry about their
13041     	 existence.  Theoretically, they should never even be
13042     	 instantiated, but that's hard to forestall.  */
13043      error ("invalid constructor; you probably meant `%T (const %T&)'",
13044		ctype, ctype);
13045      SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
13046      return 0;
13047    }
13048
13049  return 1;
13050}
13051
13052/* An operator with this code is unary, but can also be binary.  */
13053
13054static int
13055ambi_op_p (code)
13056     enum tree_code code;
13057{
13058  return (code == INDIRECT_REF
13059	  || code == ADDR_EXPR
13060	  || code == CONVERT_EXPR
13061	  || code == NEGATE_EXPR
13062	  || code == PREINCREMENT_EXPR
13063	  || code == PREDECREMENT_EXPR);
13064}
13065
13066/* An operator with this name can only be unary.  */
13067
13068static int
13069unary_op_p (code)
13070     enum tree_code code;
13071{
13072  return (code == TRUTH_NOT_EXPR
13073	  || code == BIT_NOT_EXPR
13074	  || code == COMPONENT_REF
13075	  || code == TYPE_EXPR);
13076}
13077
13078/* Do a little sanity-checking on how they declared their operator.  */
13079
13080void
13081grok_op_properties (decl, friendp)
13082     tree decl;
13083     int friendp;
13084{
13085  tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
13086  tree argtype;
13087  int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
13088  tree name = DECL_NAME (decl);
13089  enum tree_code operator_code;
13090  int arity;
13091
13092  /* Count the number of arguments.  */
13093  for (argtype = argtypes, arity = 0;
13094       argtype && argtype != void_list_node;
13095       argtype = TREE_CHAIN (argtype))
13096    ++arity;
13097
13098  if (current_class_type == NULL_TREE)
13099    friendp = 1;
13100
13101  if (DECL_CONV_FN_P (decl))
13102    operator_code = TYPE_EXPR;
13103  else
13104    do
13105      {
13106#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)	\
13107	if (ansi_opname (CODE) == name)				\
13108	  {							\
13109	    operator_code = (CODE);				\
13110	    break;						\
13111	  }							\
13112	else if (ansi_assopname (CODE) == name)			\
13113	  {							\
13114	    operator_code = (CODE);				\
13115	    DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;		\
13116	    break;						\
13117	  }
13118
13119#include "operators.def"
13120#undef DEF_OPERATOR
13121
13122	abort ();
13123      }
13124    while (0);
13125  my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
13126  SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13127
13128  if (! friendp)
13129    {
13130      switch (operator_code)
13131	{
13132	case CALL_EXPR:
13133	  TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
13134	  break;
13135
13136	case ARRAY_REF:
13137	  TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
13138	  break;
13139
13140	case COMPONENT_REF:
13141	case MEMBER_REF:
13142	  TYPE_OVERLOADS_ARROW (current_class_type) = 1;
13143	  break;
13144
13145	case NEW_EXPR:
13146	  TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
13147	  break;
13148
13149	case DELETE_EXPR:
13150	  TYPE_GETS_DELETE (current_class_type) |= 1;
13151	  break;
13152
13153	case VEC_NEW_EXPR:
13154	  TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
13155	  break;
13156
13157	case VEC_DELETE_EXPR:
13158	  TYPE_GETS_DELETE (current_class_type) |= 2;
13159	  break;
13160
13161	default:
13162	  break;
13163	}
13164    }
13165
13166  if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
13167    {
13168      /* When the compiler encounters the definition of A::operator new, it
13169	 doesn't look at the class declaration to find out if it's static.  */
13170      if (methodp)
13171	revert_static_member_fn (decl);
13172
13173      TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
13174    }
13175  else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13176    {
13177      if (methodp)
13178	revert_static_member_fn (decl);
13179
13180      TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13181    }
13182  else
13183    {
13184      /* An operator function must either be a non-static member function
13185	 or have at least one parameter of a class, a reference to a class,
13186	 an enumeration, or a reference to an enumeration.  13.4.0.6 */
13187      if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13188	{
13189	  if (operator_code == TYPE_EXPR
13190	      || operator_code == CALL_EXPR
13191	      || operator_code == COMPONENT_REF
13192	      || operator_code == ARRAY_REF
13193	      || operator_code == NOP_EXPR)
13194	    error ("`%D' must be a nonstatic member function", decl);
13195	  else
13196	    {
13197	      tree p = argtypes;
13198
13199	      if (DECL_STATIC_FUNCTION_P (decl))
13200		error ("`%D' must be either a non-static member function or a non-member function", decl);
13201
13202	      if (p)
13203		for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
13204		  {
13205		    tree arg = TREE_VALUE (p);
13206		    if (TREE_CODE (arg) == REFERENCE_TYPE)
13207		      arg = TREE_TYPE (arg);
13208
13209		    /* This lets bad template code slip through.  */
13210		    if (IS_AGGR_TYPE (arg)
13211			|| TREE_CODE (arg) == ENUMERAL_TYPE
13212			|| TREE_CODE (arg) == TEMPLATE_TYPE_PARM
13213			|| TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
13214		      goto foundaggr;
13215		  }
13216	      error
13217		("`%D' must have an argument of class or enumerated type",
13218		 decl);
13219	    foundaggr:
13220	      ;
13221	    }
13222	}
13223
13224      if (operator_code == CALL_EXPR)
13225	return;			/* No restrictions on args.  */
13226
13227      if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
13228	{
13229	  tree t = TREE_TYPE (name);
13230	  if (! friendp)
13231	    {
13232	      int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13233	      const char *what = 0;
13234
13235	      if (ref)
13236		t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13237
13238	      if (TREE_CODE (t) == VOID_TYPE)
13239	        what = "void";
13240	      else if (t == current_class_type)
13241		what = "the same type";
13242	      /* Don't force t to be complete here.  */
13243	      else if (IS_AGGR_TYPE (t)
13244		       && COMPLETE_TYPE_P (t)
13245		       && DERIVED_FROM_P (t, current_class_type))
13246		what = "a base class";
13247
13248	      if (what && warn_conversion)
13249		warning ("conversion to %s%s will never use a type conversion operator",
13250			 ref ? "a reference to " : "", what);
13251	    }
13252	}
13253      if (operator_code == COND_EXPR)
13254	{
13255	  /* 13.4.0.3 */
13256	  error ("ISO C++ prohibits overloading operator ?:");
13257	}
13258      else if (ambi_op_p (operator_code))
13259	{
13260	  if (arity == 1)
13261	    /* We pick the one-argument operator codes by default, so
13262	       we don't have to change anything.  */
13263	    ;
13264	  else if (arity == 2)
13265	    {
13266	      /* If we thought this was a unary operator, we now know
13267		 it to be a binary operator.  */
13268	      switch (operator_code)
13269		{
13270		case INDIRECT_REF:
13271		  operator_code = MULT_EXPR;
13272		  break;
13273
13274		case ADDR_EXPR:
13275		  operator_code = BIT_AND_EXPR;
13276		  break;
13277
13278		case CONVERT_EXPR:
13279		  operator_code = PLUS_EXPR;
13280		  break;
13281
13282		case NEGATE_EXPR:
13283		  operator_code = MINUS_EXPR;
13284		  break;
13285
13286		case PREINCREMENT_EXPR:
13287		  operator_code = POSTINCREMENT_EXPR;
13288		  break;
13289
13290		case PREDECREMENT_EXPR:
13291		  operator_code = POSTDECREMENT_EXPR;
13292		  break;
13293
13294		default:
13295		  abort ();
13296		}
13297
13298	      SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13299
13300	      if ((operator_code == POSTINCREMENT_EXPR
13301		   || operator_code == POSTDECREMENT_EXPR)
13302		  && ! processing_template_decl
13303		  && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13304		{
13305		  if (methodp)
13306		    error ("postfix `%D' must take `int' as its argument",
13307			      decl);
13308		  else
13309		    error
13310		      ("postfix `%D' must take `int' as its second argument",
13311		       decl);
13312		}
13313	    }
13314	  else
13315	    {
13316	      if (methodp)
13317		error ("`%D' must take either zero or one argument", decl);
13318	      else
13319		error ("`%D' must take either one or two arguments", decl);
13320	    }
13321
13322	  /* More Effective C++ rule 6.  */
13323	  if (warn_ecpp
13324	      && (operator_code == POSTINCREMENT_EXPR
13325		  || operator_code == POSTDECREMENT_EXPR
13326		  || operator_code == PREINCREMENT_EXPR
13327		  || operator_code == PREDECREMENT_EXPR))
13328	    {
13329	      tree arg = TREE_VALUE (argtypes);
13330	      tree ret = TREE_TYPE (TREE_TYPE (decl));
13331	      if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13332		arg = TREE_TYPE (arg);
13333	      arg = TYPE_MAIN_VARIANT (arg);
13334	      if (operator_code == PREINCREMENT_EXPR
13335		  || operator_code == PREDECREMENT_EXPR)
13336		{
13337		  if (TREE_CODE (ret) != REFERENCE_TYPE
13338		      || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13339				       arg))
13340		    warning ("prefix `%D' should return `%T'", decl,
13341				build_reference_type (arg));
13342		}
13343	      else
13344		{
13345		  if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13346		    warning ("postfix `%D' should return `%T'", decl, arg);
13347		}
13348	    }
13349	}
13350      else if (unary_op_p (operator_code))
13351	{
13352	  if (arity != 1)
13353	    {
13354	      if (methodp)
13355		error ("`%D' must take `void'", decl);
13356	      else
13357		error ("`%D' must take exactly one argument", decl);
13358	    }
13359	}
13360      else /* if (binary_op_p (operator_code)) */
13361	{
13362	  if (arity != 2)
13363	    {
13364	      if (methodp)
13365		error ("`%D' must take exactly one argument", decl);
13366	      else
13367		error ("`%D' must take exactly two arguments", decl);
13368	    }
13369
13370	  /* More Effective C++ rule 7.  */
13371	  if (warn_ecpp
13372	      && (operator_code == TRUTH_ANDIF_EXPR
13373		  || operator_code == TRUTH_ORIF_EXPR
13374		  || operator_code == COMPOUND_EXPR))
13375	    warning ("user-defined `%D' always evaluates both arguments",
13376			decl);
13377	}
13378
13379      /* Effective C++ rule 23.  */
13380      if (warn_ecpp
13381	  && arity == 2
13382	  && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13383	  && (operator_code == PLUS_EXPR
13384	      || operator_code == MINUS_EXPR
13385	      || operator_code == TRUNC_DIV_EXPR
13386	      || operator_code == MULT_EXPR
13387	      || operator_code == TRUNC_MOD_EXPR)
13388	  && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13389	warning ("`%D' should return by value", decl);
13390
13391      /* [over.oper]/8 */
13392      for (; argtypes && argtypes != void_list_node;
13393          argtypes = TREE_CHAIN (argtypes))
13394        if (TREE_PURPOSE (argtypes))
13395          {
13396            TREE_PURPOSE (argtypes) = NULL_TREE;
13397            if (operator_code == POSTINCREMENT_EXPR
13398		|| operator_code == POSTDECREMENT_EXPR)
13399              {
13400                if (pedantic)
13401                  pedwarn ("`%D' cannot have default arguments", decl);
13402              }
13403            else
13404              error ("`%D' cannot have default arguments", decl);
13405          }
13406
13407    }
13408}
13409
13410static const char *
13411tag_name (code)
13412     enum tag_types code;
13413{
13414  switch (code)
13415    {
13416    case record_type:
13417      return "struct";
13418    case class_type:
13419      return "class";
13420    case union_type:
13421      return "union ";
13422    case enum_type:
13423      return "enum";
13424    default:
13425      abort ();
13426    }
13427}
13428
13429/* Name lookup in an elaborated-type-specifier (after the keyword
13430   indicated by TAG_CODE) has found TYPE.  If the
13431   elaborated-type-specifier is invalid, issue a diagnostic and return
13432   error_mark_node; otherwise, return TYPE itself.  */
13433
13434static tree
13435check_elaborated_type_specifier (enum tag_types tag_code,
13436				 tree type)
13437{
13438  tree t;
13439
13440  t = follow_tag_typedef (type);
13441
13442  /* [dcl.type.elab] If the identifier resolves to a typedef-name or a
13443     template type-parameter, the elaborated-type-specifier is
13444     ill-formed.  */
13445  if (!t)
13446    {
13447      error ("using typedef-name `%D' after `%s'",
13448	     TYPE_NAME (type), tag_name (tag_code));
13449      t = error_mark_node;
13450    }
13451  else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13452    {
13453      error ("using template type parameter `%T' after `%s'",
13454	     type, tag_name (tag_code));
13455      t = error_mark_node;
13456    }
13457
13458  return t;
13459}
13460
13461/* Get the struct, enum or union (CODE says which) with tag NAME.
13462   Define the tag as a forward-reference if it is not defined.
13463
13464   C++: If a class derivation is given, process it here, and report
13465   an error if multiple derivation declarations are not identical.
13466
13467   If this is a definition, come in through xref_tag and only look in
13468   the current frame for the name (since C++ allows new names in any
13469   scope.)  */
13470
13471tree
13472xref_tag (enum tag_types tag_code, tree name, tree attributes,
13473	  bool globalize)
13474{
13475  enum tree_code code;
13476  register tree ref, t;
13477  struct cp_binding_level *b = current_binding_level;
13478  tree context = NULL_TREE;
13479
13480  timevar_push (TV_NAME_LOOKUP);
13481
13482  switch (tag_code)
13483    {
13484    case record_type:
13485    case class_type:
13486      code = RECORD_TYPE;
13487      break;
13488    case union_type:
13489      code = UNION_TYPE;
13490      break;
13491    case enum_type:
13492      code = ENUMERAL_TYPE;
13493      break;
13494    default:
13495      abort ();
13496    }
13497
13498  /* If a cross reference is requested, look up the type
13499     already defined for this tag and return it.  */
13500  if (TYPE_P (name))
13501    {
13502      t = name;
13503      name = TYPE_IDENTIFIER (t);
13504    }
13505  else
13506    t = IDENTIFIER_TYPE_VALUE (name);
13507
13508  /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
13509  if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
13510    {
13511      static int explained;
13512      tree shadowed;
13513
13514      warning ("`%s %T' declares a new type at namespace scope",
13515		  tag_name (tag_code), name);
13516      if (!explained++)
13517	warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
13518		    tag_name (tag_code),
13519		    constructor_name (current_class_type),
13520		    TYPE_IDENTIFIER (t));
13521
13522      /* We need to remove the class scope binding for the
13523         TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
13524      for (shadowed = b->class_shadowed;
13525	   shadowed;
13526	   shadowed = TREE_CHAIN (shadowed))
13527	if (TREE_TYPE (shadowed) == TYPE_NAME (t))
13528	  {
13529	    TREE_PURPOSE (shadowed) = NULL_TREE;
13530	    break;
13531	  }
13532    }
13533
13534  if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
13535      && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
13536    t = NULL_TREE;
13537
13538  if (! globalize)
13539    {
13540      /* If we know we are defining this tag, only look it up in
13541	 this scope and don't try to find it as a type.  */
13542      ref = lookup_tag (code, name, b, 1);
13543    }
13544  else
13545    {
13546      if (t)
13547	{
13548	  ref = check_elaborated_type_specifier (tag_code, t);
13549	  if (ref == error_mark_node)
13550	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
13551	}
13552      else
13553	ref = lookup_tag (code, name, b, 0);
13554
13555      if (! ref)
13556	{
13557	  /* Try finding it as a type declaration.  If that wins,
13558	     use it.  */
13559	  ref = lookup_name (name, 1);
13560
13561	  if (ref != NULL_TREE
13562	      && processing_template_decl
13563	      && DECL_CLASS_TEMPLATE_P (ref)
13564	      && template_class_depth (current_class_type) == 0)
13565	    /* Since GLOBALIZE is true, we're declaring a global
13566	       template, so we want this type.  */
13567	    ref = DECL_TEMPLATE_RESULT (ref);
13568
13569	  if (ref && TREE_CODE (ref) == TYPE_DECL)
13570	    {
13571	      ref = check_elaborated_type_specifier (tag_code,
13572						     TREE_TYPE (ref));
13573	      if (ref == error_mark_node)
13574		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
13575	      if (ref && TREE_CODE (ref) != code)
13576		ref = NULL_TREE;
13577	    }
13578	  else
13579	    ref = NULL_TREE;
13580	}
13581
13582      if (ref && current_class_type
13583	  && template_class_depth (current_class_type)
13584	  && PROCESSING_REAL_TEMPLATE_DECL_P ())
13585	{
13586	  /* Since GLOBALIZE is nonzero, we are not looking at a
13587	     definition of this tag.  Since, in addition, we are currently
13588	     processing a (member) template declaration of a template
13589	     class, we must be very careful; consider:
13590
13591	       template <class X>
13592	       struct S1
13593
13594	       template <class U>
13595	       struct S2
13596	       { template <class V>
13597	       friend struct S1; };
13598
13599	     Here, the S2::S1 declaration should not be confused with the
13600	     outer declaration.  In particular, the inner version should
13601	     have a template parameter of level 2, not level 1.  This
13602	     would be particularly important if the member declaration
13603	     were instead:
13604
13605	       template <class V = U> friend struct S1;
13606
13607	     say, when we should tsubst into `U' when instantiating
13608	     S2.  On the other hand, when presented with:
13609
13610	         template <class T>
13611	         struct S1 {
13612		   template <class U>
13613	           struct S2 {};
13614		   template <class U>
13615		   friend struct S2;
13616		 };
13617
13618              we must find the inner binding eventually.  We
13619	      accomplish this by making sure that the new type we
13620	      create to represent this declaration has the right
13621	      TYPE_CONTEXT.  */
13622	  context = TYPE_CONTEXT (ref);
13623	  ref = NULL_TREE;
13624	}
13625    }
13626
13627  if (! ref)
13628    {
13629      /* If no such tag is yet defined, create a forward-reference node
13630	 and record it as the "definition".
13631	 When a real declaration of this type is found,
13632	 the forward-reference will be altered into a real type.  */
13633      if (code == ENUMERAL_TYPE)
13634	{
13635	  error ("use of enum `%#D' without previous declaration", name);
13636
13637	  ref = make_node (ENUMERAL_TYPE);
13638
13639	  /* Give the type a default layout like unsigned int
13640	     to avoid crashing if it does not get defined.  */
13641	  TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
13642	  TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
13643	  TYPE_USER_ALIGN (ref) = 0;
13644	  TREE_UNSIGNED (ref) = 1;
13645	  TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
13646	  TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
13647	  TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
13648
13649	  /* Enable us to recognize when a type is created in class context.
13650	     To do nested classes correctly, this should probably be cleared
13651	     out when we leave this classes scope.  Currently this in only
13652	     done in `start_enum'.  */
13653
13654	  pushtag (name, ref, globalize);
13655	}
13656      else
13657	{
13658	  struct cp_binding_level *old_b = class_binding_level;
13659
13660	  ref = make_aggr_type (code);
13661	  TYPE_CONTEXT (ref) = context;
13662
13663#ifdef NONNESTED_CLASSES
13664	  /* Class types don't nest the way enums do.  */
13665	  class_binding_level = (struct cp_binding_level *)0;
13666#endif
13667	  pushtag (name, ref, globalize);
13668	  class_binding_level = old_b;
13669	}
13670    }
13671  else
13672    {
13673      if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
13674	redeclare_class_template (ref, current_template_parms);
13675    }
13676
13677  TYPE_ATTRIBUTES (ref) = attributes;
13678
13679  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
13680}
13681
13682tree
13683xref_tag_from_type (old, id, globalize)
13684     tree old, id;
13685     int globalize;
13686{
13687  enum tag_types tag_kind;
13688
13689  if (TREE_CODE (old) == RECORD_TYPE)
13690    tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13691  else
13692    tag_kind  = union_type;
13693
13694  if (id == NULL_TREE)
13695    id = TYPE_IDENTIFIER (old);
13696
13697  return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
13698}
13699
13700/* REF is a type (named NAME), for which we have just seen some
13701   baseclasses.  BINFO is a list of those baseclasses; the
13702   TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
13703   the base-class.  CODE_TYPE_NODE indicates whether REF is a class,
13704   struct, or union.  */
13705
13706void
13707xref_basetypes (ref, binfo)
13708     tree ref;
13709     tree binfo;
13710{
13711  /* In the declaration `A : X, Y, ... Z' we mark all the types
13712     (A, X, Y, ..., Z) so we can check for duplicates.  */
13713  tree binfos;
13714  tree base;
13715
13716  int i, len;
13717  enum tag_types tag_code;
13718
13719  if (TREE_CODE (ref) == UNION_TYPE)
13720    {
13721      error ("derived union `%T' invalid", ref);
13722      return;
13723    }
13724
13725  tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
13726
13727  len = list_length (binfo);
13728
13729  /* First, make sure that any templates in base-classes are
13730     instantiated.  This ensures that if we call ourselves recursively
13731     we do not get confused about which classes are marked and which
13732     are not.  */
13733  for (base = binfo; base; base = TREE_CHAIN (base))
13734    complete_type (TREE_VALUE (base));
13735
13736  SET_CLASSTYPE_MARKED (ref);
13737  BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
13738
13739  for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
13740    {
13741      /* The base of a derived struct is public by default.  */
13742      int via_public
13743	= (TREE_PURPOSE (binfo) == access_public_node
13744	   || TREE_PURPOSE (binfo) == access_public_virtual_node
13745	   || (tag_code != class_type
13746	       && (TREE_PURPOSE (binfo) == access_default_node
13747		   || TREE_PURPOSE (binfo) == access_default_virtual_node)));
13748      int via_protected
13749	= (TREE_PURPOSE (binfo) == access_protected_node
13750	   || TREE_PURPOSE (binfo) == access_protected_virtual_node);
13751      int via_virtual
13752	= (TREE_PURPOSE (binfo) == access_private_virtual_node
13753	   || TREE_PURPOSE (binfo) == access_protected_virtual_node
13754	   || TREE_PURPOSE (binfo) == access_public_virtual_node
13755	   || TREE_PURPOSE (binfo) == access_default_virtual_node);
13756      tree basetype = TREE_VALUE (binfo);
13757      tree base_binfo;
13758
13759      if (basetype && TREE_CODE (basetype) == TYPE_DECL)
13760	basetype = TREE_TYPE (basetype);
13761      if (!basetype
13762	  || (TREE_CODE (basetype) != RECORD_TYPE
13763	      && TREE_CODE (basetype) != TYPENAME_TYPE
13764	      && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
13765	      && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
13766	{
13767	  error ("base type `%T' fails to be a struct or class type",
13768		    TREE_VALUE (binfo));
13769	  continue;
13770	}
13771
13772      /* This code replaces similar code in layout_basetypes.
13773         We put the complete_type first for implicit `typename'.  */
13774      if (!COMPLETE_TYPE_P (basetype)
13775	  && ! (current_template_parms && uses_template_parms (basetype)))
13776	{
13777	  error ("base class `%T' has incomplete type", basetype);
13778	  continue;
13779	}
13780      else
13781	{
13782	  if (CLASSTYPE_MARKED (basetype))
13783	    {
13784	      if (basetype == ref)
13785		error ("recursive type `%T' undefined", basetype);
13786	      else
13787		error ("duplicate base type `%T' invalid", basetype);
13788	      continue;
13789	    }
13790
13791	  if (TYPE_FOR_JAVA (basetype)
13792	      && (current_lang_depth () == 0))
13793	    TYPE_FOR_JAVA (ref) = 1;
13794
13795	  /* Note that the BINFO records which describe individual
13796	     inheritances are *not* shared in the lattice!  They
13797	     cannot be shared because a given baseclass may be
13798	     inherited with different `accessibility' by different
13799	     derived classes.  (Each BINFO record describing an
13800	     individual inheritance contains flags which say what
13801	     the `accessibility' of that particular inheritance is.)  */
13802
13803	  base_binfo
13804	    = make_binfo (size_zero_node, basetype,
13805			  CLASS_TYPE_P (basetype)
13806			  ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
13807			  CLASS_TYPE_P (basetype)
13808			  ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
13809
13810	  TREE_VEC_ELT (binfos, i) = base_binfo;
13811	  TREE_VIA_PUBLIC (base_binfo) = via_public;
13812	  TREE_VIA_PROTECTED (base_binfo) = via_protected;
13813	  TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
13814	  BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
13815
13816	  /* We need to unshare the binfos now so that lookups during class
13817	     definition work.  */
13818	  unshare_base_binfos (base_binfo);
13819
13820	  SET_CLASSTYPE_MARKED (basetype);
13821
13822	  /* We are free to modify these bits because they are meaningless
13823	     at top level, and BASETYPE is a top-level type.  */
13824	  if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
13825	    {
13826	      TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
13827	      /* Converting to a virtual base class requires looking
13828		 up the offset of the virtual base.  */
13829	      TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13830	    }
13831
13832	  if (CLASS_TYPE_P (basetype))
13833	    {
13834	      TYPE_HAS_NEW_OPERATOR (ref)
13835		|= TYPE_HAS_NEW_OPERATOR (basetype);
13836	      TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13837		|= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13838	      TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13839	      /* If the base-class uses multiple inheritance, so do we.  */
13840	      TYPE_USES_MULTIPLE_INHERITANCE (ref)
13841		|= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
13842	      /* Likewise, if converting to a base of the base may require
13843		 code, then we may need to generate code to convert to a
13844		 base as well.  */
13845	      TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
13846		|= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
13847	    }
13848
13849	  i += 1;
13850	}
13851    }
13852  if (i)
13853    TREE_VEC_LENGTH (binfos) = i;
13854  else
13855    BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
13856
13857  if (i > 1)
13858    {
13859      TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
13860      /* If there is more than one non-empty they cannot be at the same
13861	 address.  */
13862      TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13863    }
13864
13865  /* Unmark all the types.  */
13866  while (--i >= 0)
13867    CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
13868  CLEAR_CLASSTYPE_MARKED (ref);
13869
13870  /* Now that we know all the base-classes, set up the list of virtual
13871     bases.  */
13872  get_vbase_types (ref);
13873}
13874
13875
13876/* Begin compiling the definition of an enumeration type.
13877   NAME is its name (or null if anonymous).
13878   Returns the type object, as yet incomplete.
13879   Also records info about it so that build_enumerator
13880   may be used to declare the individual values as they are read.  */
13881
13882tree
13883start_enum (name)
13884     tree name;
13885{
13886  register tree enumtype = NULL_TREE;
13887  struct cp_binding_level *b = current_binding_level;
13888
13889  /* If this is the real definition for a previous forward reference,
13890     fill in the contents in the same object that used to be the
13891     forward reference.  */
13892
13893  if (name != NULL_TREE)
13894    enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13895
13896  if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13897    {
13898      error ("multiple definition of `%#T'", enumtype);
13899      cp_error_at ("previous definition here", enumtype);
13900      /* Clear out TYPE_VALUES, and start again.  */
13901      TYPE_VALUES (enumtype) = NULL_TREE;
13902    }
13903  else
13904    {
13905      enumtype = make_node (ENUMERAL_TYPE);
13906      pushtag (name, enumtype, 0);
13907    }
13908
13909  return enumtype;
13910}
13911
13912/* After processing and defining all the values of an enumeration type,
13913   install their decls in the enumeration type and finish it off.
13914   ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
13915
13916void
13917finish_enum (enumtype)
13918     tree enumtype;
13919{
13920  tree pair;
13921  tree minnode;
13922  tree maxnode;
13923  tree t;
13924  bool unsignedp;
13925  int lowprec;
13926  int highprec;
13927  int precision;
13928
13929  /* We built up the VALUES in reverse order.  */
13930  TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13931
13932  /* For an enum defined in a template, just set the type of the values;
13933     all further processing is postponed until the template is
13934     instantiated.  We need to set the type so that tsubst of a CONST_DECL
13935     works.  */
13936  if (processing_template_decl)
13937    {
13938      for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13939	TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13940      if (at_function_scope_p ())
13941	add_stmt (build_min (TAG_DEFN, enumtype));
13942      return;
13943    }
13944
13945  if (TYPE_VALUES (enumtype))
13946    {
13947      minnode = maxnode = NULL_TREE;
13948
13949      for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13950	{
13951	  tree decl = TREE_VALUE (pair);
13952	  tree value = DECL_INITIAL (decl);
13953
13954	  /* [dcl.enum]: Following the closing brace of an enum-specifier,
13955	     each enumerator has the type of its enumeration.  Prior to the
13956	     closing brace, the type of each enumerator is the type of its
13957	     initializing value.  */
13958	  TREE_TYPE (decl) = enumtype;
13959
13960	  /* Figure out what the minimum and maximum values of the
13961	     enumerators are.  */
13962	  if (!minnode)
13963	    minnode = maxnode = value;
13964	  else if (tree_int_cst_lt (maxnode, value))
13965	    maxnode = value;
13966	  else if (tree_int_cst_lt (value, minnode))
13967	    minnode = value;
13968
13969	  /* Set the TREE_TYPE for the values as well.  That's so that when
13970	     we call decl_constant_value we get an entity of the right type
13971	     (but with the constant value).  But first make a copy so we
13972	     don't clobber shared INTEGER_CSTs.  */
13973	  if (TREE_TYPE (value) != enumtype)
13974	    {
13975	      value = DECL_INITIAL (decl) = copy_node (value);
13976	      TREE_TYPE (value) = enumtype;
13977	    }
13978
13979	  /* In addition, transform the TYPE_VALUES list to contain the
13980	     values, rather than the CONST_DECLs for them.  */
13981	  TREE_VALUE (pair) = value;
13982	}
13983    }
13984  else
13985    minnode = maxnode = integer_zero_node;
13986
13987  /* Compute the number of bits require to represent all values of the
13988     enumeration.  We must do this before the type of MINNODE and
13989     MAXNODE are transformed, since min_precision relies on the
13990     TREE_TYPE of the value it is passed.  */
13991  unsignedp = tree_int_cst_sgn (minnode) >= 0;
13992  lowprec = min_precision (minnode, unsignedp);
13993  highprec = min_precision (maxnode, unsignedp);
13994  precision = MAX (lowprec, highprec);
13995
13996  /* DR 377
13997
13998     IF no integral type can represent all the enumerator values, the
13999     enumeration is ill-formed.  */
14000  if (precision > TYPE_PRECISION (long_long_integer_type_node))
14001    {
14002      error ("no integral type can represent all of the enumerator values "
14003	     "for `%T'", enumtype);
14004      precision = TYPE_PRECISION (long_long_integer_type_node);
14005    }
14006
14007  /* Compute the minium and maximum values for the type, the size of
14008     the type, and so forth.  */
14009  TYPE_PRECISION (enumtype) = precision;
14010  TYPE_SIZE (enumtype) = NULL_TREE;
14011  if (unsignedp)
14012    fixup_unsigned_type (enumtype);
14013  else
14014    fixup_signed_type (enumtype);
14015
14016  /* We use "int" or "unsigned int" as the underlying type, unless all
14017     the values will not fit or the user has requested that we try to
14018     use shorter types where possible.  */
14019  if (precision < TYPE_PRECISION (integer_type_node)
14020      && !flag_short_enums)
14021    {
14022      TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
14023      TYPE_SIZE (enumtype) = NULL_TREE;
14024      layout_type (enumtype);
14025    }
14026
14027  /* Fix up all variant types of this enum type.  */
14028  for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14029    {
14030      TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14031      TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
14032      TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
14033      TYPE_SIZE (t) = TYPE_SIZE (enumtype);
14034      TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
14035      TYPE_MODE (t) = TYPE_MODE (enumtype);
14036      TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
14037      TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
14038      TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
14039      TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
14040    }
14041
14042  /* Finish debugging output for this type.  */
14043  rest_of_type_compilation (enumtype, namespace_bindings_p ());
14044}
14045
14046/* Build and install a CONST_DECL for an enumeration constant of the
14047   enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14048   Assignment of sequential values by default is handled here.  */
14049
14050void
14051build_enumerator (name, value, enumtype)
14052     tree name;
14053     tree value;
14054     tree enumtype;
14055{
14056  tree decl;
14057  tree context;
14058  tree type;
14059
14060  /* Remove no-op casts from the value.  */
14061  if (value)
14062    STRIP_TYPE_NOPS (value);
14063
14064  if (! processing_template_decl)
14065    {
14066      /* Validate and default VALUE.  */
14067      if (value != NULL_TREE)
14068	{
14069	  value = decl_constant_value (value);
14070
14071	  if (TREE_CODE (value) == INTEGER_CST)
14072	    {
14073	      value = default_conversion (value);
14074	      constant_expression_warning (value);
14075	    }
14076	  else
14077	    {
14078	      error ("enumerator value for `%D' not integer constant", name);
14079	      value = NULL_TREE;
14080	    }
14081	}
14082
14083      /* Default based on previous value.  */
14084      if (value == NULL_TREE && ! processing_template_decl)
14085	{
14086	  tree prev_value;
14087
14088	  if (TYPE_VALUES (enumtype))
14089	    {
14090	      /* The next value is the previous value ...  */
14091	      prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14092	      /* ... plus one.  */
14093	      value = cp_build_binary_op (PLUS_EXPR,
14094					  prev_value,
14095					  integer_one_node);
14096
14097	      if (tree_int_cst_lt (value, prev_value))
14098		error ("overflow in enumeration values at `%D'", name);
14099	    }
14100	  else
14101	    value = integer_zero_node;
14102	}
14103
14104      /* Remove no-op casts from the value.  */
14105      STRIP_TYPE_NOPS (value);
14106    }
14107
14108  /* C++ associates enums with global, function, or class declarations.  */
14109  context = current_scope ();
14110
14111  /* Build the actual enumeration constant.  Note that the enumeration
14112    constants have the type of their initializers until the
14113    enumeration is complete:
14114
14115      [ dcl.enum ]
14116
14117      Following the closing brace of an enum-specifier, each enumer-
14118      ator has the type of its enumeration.  Prior to the closing
14119      brace, the type of each enumerator is the type of its
14120      initializing value.
14121
14122    In finish_enum we will reset the type.  Of course, if we're
14123    processing a template, there may be no value.  */
14124  type = value ? TREE_TYPE (value) : NULL_TREE;
14125
14126  if (context && context == current_class_type)
14127    /* This enum declaration is local to the class.  We need the full
14128       lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
14129    decl = build_lang_decl (CONST_DECL, name, type);
14130  else
14131    /* It's a global enum, or it's local to a function.  (Note local to
14132      a function could mean local to a class method.  */
14133    decl = build_decl (CONST_DECL, name, type);
14134
14135  DECL_CONTEXT (decl) = FROB_CONTEXT (context);
14136  DECL_INITIAL (decl) = value;
14137  TREE_READONLY (decl) = 1;
14138
14139  if (context && context == current_class_type)
14140    /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14141      on the TYPE_FIELDS list for `S'.  (That's so that you can say
14142      things like `S::i' later.)  */
14143    finish_member_declaration (decl);
14144  else
14145    pushdecl (decl);
14146
14147  /* Add this enumeration constant to the list for this type.  */
14148  TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14149}
14150
14151
14152/* We're defining DECL.  Make sure that it's type is OK.  */
14153
14154static void
14155check_function_type (decl, current_function_parms)
14156     tree decl;
14157     tree current_function_parms;
14158{
14159  tree fntype = TREE_TYPE (decl);
14160  tree return_type = complete_type (TREE_TYPE (fntype));
14161
14162  /* In a function definition, arg types must be complete.  */
14163  require_complete_types_for_parms (current_function_parms);
14164
14165  if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14166    {
14167      error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
14168
14169      /* Make it return void instead, but don't change the
14170	 type of the DECL_RESULT, in case we have a named return value.  */
14171      if (TREE_CODE (fntype) == METHOD_TYPE)
14172	{
14173	  tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
14174	  TREE_TYPE (decl)
14175	    = build_cplus_method_type (ctype,
14176				       void_type_node,
14177				       FUNCTION_ARG_CHAIN (decl));
14178	}
14179      else
14180	TREE_TYPE (decl)
14181	  = build_function_type (void_type_node,
14182				 TYPE_ARG_TYPES (TREE_TYPE (decl)));
14183      TREE_TYPE (decl)
14184	= build_exception_variant (fntype,
14185				   TYPE_RAISES_EXCEPTIONS (fntype));
14186    }
14187  else
14188    abstract_virtuals_error (decl, TREE_TYPE (fntype));
14189}
14190
14191/* Create the FUNCTION_DECL for a function definition.
14192   DECLSPECS and DECLARATOR are the parts of the declaration;
14193   they describe the function's name and the type it returns,
14194   but twisted together in a fashion that parallels the syntax of C.
14195
14196   FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14197   DECLARATOR is really the DECL for the function we are about to
14198   process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14199   indicating that the function is an inline defined in-class.
14200
14201   This function creates a binding context for the function body
14202   as well as setting up the FUNCTION_DECL in current_function_decl.
14203
14204   Returns 1 on success.  If the DECLARATOR is not suitable for a function
14205   (it defines a datum instead), we return 0, which tells
14206   yyparse to report a parse error.
14207
14208   For C++, we must first check whether that datum makes any sense.
14209   For example, "class A local_a(1,2);" means that variable local_a
14210   is an aggregate of type A, which should have a constructor
14211   applied to it with the argument list [1, 2].  */
14212
14213int
14214start_function (declspecs, declarator, attrs, flags)
14215     tree declspecs, declarator, attrs;
14216     int flags;
14217{
14218  tree decl1;
14219  tree ctype = NULL_TREE;
14220  tree fntype;
14221  tree restype;
14222  int doing_friend = 0;
14223  struct cp_binding_level *bl;
14224  tree current_function_parms;
14225
14226  /* Sanity check.  */
14227  my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
14228  my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
14229
14230  /* This should only be done once on the top most decl.  */
14231  if (have_extern_spec)
14232    {
14233      declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
14234      have_extern_spec = false;
14235    }
14236
14237  if (flags & SF_PRE_PARSED)
14238    {
14239      decl1 = declarator;
14240
14241      fntype = TREE_TYPE (decl1);
14242      if (TREE_CODE (fntype) == METHOD_TYPE)
14243	ctype = TYPE_METHOD_BASETYPE (fntype);
14244
14245      /* ISO C++ 11.4/5.  A friend function defined in a class is in
14246	 the (lexical) scope of the class in which it is defined.  */
14247      if (!ctype && DECL_FRIEND_P (decl1))
14248	{
14249	  ctype = DECL_FRIEND_CONTEXT (decl1);
14250
14251	  /* CTYPE could be null here if we're dealing with a template;
14252	     for example, `inline friend float foo()' inside a template
14253	     will have no CTYPE set.  */
14254	  if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14255	    ctype = NULL_TREE;
14256	  else
14257	    doing_friend = 1;
14258	}
14259
14260      last_function_parms = DECL_ARGUMENTS (decl1);
14261    }
14262  else
14263    {
14264      decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
14265      /* If the declarator is not suitable for a function definition,
14266	 cause a syntax error.  */
14267      if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
14268	return 0;
14269
14270      cplus_decl_attributes (&decl1, attrs, 0);
14271
14272      /* If #pragma weak was used, mark the decl weak now.  */
14273      if (global_scope_p (current_binding_level))
14274	maybe_apply_pragma_weak (decl1);
14275
14276      fntype = TREE_TYPE (decl1);
14277
14278      restype = TREE_TYPE (fntype);
14279      if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
14280	{
14281	  error ("semicolon missing after declaration of `%#T'", restype);
14282	  shadow_tag (build_tree_list (NULL_TREE, restype));
14283	  CLASSTYPE_GOT_SEMICOLON (restype) = 1;
14284	  if (TREE_CODE (fntype) == FUNCTION_TYPE)
14285	    fntype = build_function_type (integer_type_node,
14286					  TYPE_ARG_TYPES (fntype));
14287	  else
14288	    fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
14289					      integer_type_node,
14290					      TYPE_ARG_TYPES (fntype));
14291	  TREE_TYPE (decl1) = fntype;
14292	}
14293
14294      if (TREE_CODE (fntype) == METHOD_TYPE)
14295	ctype = TYPE_METHOD_BASETYPE (fntype);
14296      else if (DECL_MAIN_P (decl1))
14297	{
14298	  /* If this doesn't return integer_type, complain.  */
14299	  if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
14300	    {
14301	      if (pedantic || warn_return_type)
14302		pedwarn ("return type for `main' changed to `int'");
14303	      TREE_TYPE (decl1) = fntype = default_function_type;
14304	    }
14305	}
14306    }
14307
14308  if (DECL_DECLARED_INLINE_P (decl1)
14309      && lookup_attribute ("noinline", attrs))
14310    warning_with_decl (decl1,
14311		       "inline function `%s' given attribute noinline");
14312
14313  if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14314    /* This is a constructor, we must ensure that any default args
14315       introduced by this definition are propagated to the clones
14316       now. The clones are used directly in overload resolution.  */
14317    adjust_clone_args (decl1);
14318
14319  /* Sometimes we don't notice that a function is a static member, and
14320     build a METHOD_TYPE for it.  Fix that up now.  */
14321  if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14322      && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
14323    {
14324      revert_static_member_fn (decl1);
14325      last_function_parms = TREE_CHAIN (last_function_parms);
14326      ctype = NULL_TREE;
14327    }
14328
14329  /* Warn if function was previously implicitly declared
14330     (but not if we warned then).  */
14331  if (! warn_implicit
14332      && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
14333    cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
14334
14335  /* Set up current_class_type, and enter the scope of the class, if
14336     appropriate.  */
14337  if (ctype)
14338    push_nested_class (ctype, 1);
14339  else if (DECL_STATIC_FUNCTION_P (decl1))
14340    push_nested_class (DECL_CONTEXT (decl1), 2);
14341
14342  /* Now that we have entered the scope of the class, we must restore
14343     the bindings for any template parameters surrounding DECL1, if it
14344     is an inline member template.  (Order is important; consider the
14345     case where a template parameter has the same name as a field of
14346     the class.)  It is not until after this point that
14347     PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
14348  if (flags & SF_INCLASS_INLINE)
14349    maybe_begin_member_template_processing (decl1);
14350
14351  /* Effective C++ rule 15.  */
14352  if (warn_ecpp
14353      && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14354      && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
14355    warning ("`operator=' should return a reference to `*this'");
14356
14357  /* Make the init_value nonzero so pushdecl knows this is not tentative.
14358     error_mark_node is replaced below (in poplevel) with the BLOCK.  */
14359  if (!DECL_INITIAL (decl1))
14360    DECL_INITIAL (decl1) = error_mark_node;
14361
14362  /* This function exists in static storage.
14363     (This does not mean `static' in the C sense!)  */
14364  TREE_STATIC (decl1) = 1;
14365
14366  /* We must call push_template_decl after current_class_type is set
14367     up.  (If we are processing inline definitions after exiting a
14368     class scope, current_class_type will be NULL_TREE until set above
14369     by push_nested_class.)  */
14370  if (processing_template_decl)
14371    decl1 = push_template_decl (decl1);
14372
14373  /* We are now in the scope of the function being defined.  */
14374  current_function_decl = decl1;
14375
14376  /* Save the parm names or decls from this function's declarator
14377     where store_parm_decls will find them.  */
14378  current_function_parms = last_function_parms;
14379
14380  /* Make sure the parameter and return types are reasonable.  When
14381     you declare a function, these types can be incomplete, but they
14382     must be complete when you define the function.  */
14383  if (! processing_template_decl)
14384    check_function_type (decl1, current_function_parms);
14385
14386  /* Build the return declaration for the function.  */
14387  restype = TREE_TYPE (fntype);
14388  /* Promote the value to int before returning it.  */
14389  if (c_promoting_integer_type_p (restype))
14390    restype = type_promotes_to (restype);
14391  if (DECL_RESULT (decl1) == NULL_TREE)
14392    {
14393      DECL_RESULT (decl1)
14394	= build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
14395      c_apply_type_quals_to_decl (cp_type_quals (restype),
14396				  DECL_RESULT (decl1));
14397    }
14398
14399  /* Initialize RTL machinery.  We cannot do this until
14400     CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
14401     even when processing a template; this is how we get
14402     CFUN set up, and our per-function variables initialized.
14403     FIXME factor out the non-RTL stuff.  */
14404  bl = current_binding_level;
14405  init_function_start (decl1, input_filename, lineno);
14406  current_binding_level = bl;
14407
14408  /* Even though we're inside a function body, we still don't want to
14409     call expand_expr to calculate the size of a variable-sized array.
14410     We haven't necessarily assigned RTL to all variables yet, so it's
14411     not safe to try to expand expressions involving them.  */
14412  immediate_size_expand = 0;
14413  cfun->x_dont_save_pending_sizes_p = 1;
14414
14415  /* Start the statement-tree, start the tree now.  */
14416  begin_stmt_tree (&DECL_SAVED_TREE (decl1));
14417
14418  /* Let the user know we're compiling this function.  */
14419  announce_function (decl1);
14420
14421  /* Record the decl so that the function name is defined.
14422     If we already have a decl for this name, and it is a FUNCTION_DECL,
14423     use the old decl.  */
14424  if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14425    {
14426      /* A specialization is not used to guide overload resolution.  */
14427      if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
14428	  && ! DECL_FUNCTION_MEMBER_P (decl1))
14429	decl1 = pushdecl (decl1);
14430      else
14431	{
14432	  /* We need to set the DECL_CONTEXT.  */
14433	  if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14434	    DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14435	  /* And make sure we have enough default args.  */
14436	  check_default_args (decl1);
14437	}
14438      fntype = TREE_TYPE (decl1);
14439    }
14440
14441  /* Reset these in case the call to pushdecl changed them.  */
14442  current_function_decl = decl1;
14443  cfun->decl = decl1;
14444
14445  /* If we are (erroneously) defining a function that we have already
14446     defined before, wipe out what we knew before.  */
14447  if (!DECL_PENDING_INLINE_P (decl1))
14448    DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14449
14450  if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14451    {
14452      /* We know that this was set up by `grokclassfn'.  We do not
14453	 wait until `store_parm_decls', since evil parse errors may
14454	 never get us to that point.  Here we keep the consistency
14455	 between `current_class_type' and `current_class_ptr'.  */
14456      tree t = DECL_ARGUMENTS (decl1);
14457
14458      my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
14459			  162);
14460      my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
14461			  19990811);
14462
14463      cp_function_chain->x_current_class_ref
14464	= build_indirect_ref (t, NULL);
14465      cp_function_chain->x_current_class_ptr = t;
14466
14467      /* Constructors and destructors need to know whether they're "in
14468	 charge" of initializing virtual base classes.  */
14469      t = TREE_CHAIN (t);
14470      if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14471	{
14472	  current_in_charge_parm = t;
14473	  t = TREE_CHAIN (t);
14474	}
14475      if (DECL_HAS_VTT_PARM_P (decl1))
14476	{
14477	  if (DECL_NAME (t) != vtt_parm_identifier)
14478	    abort ();
14479	  current_vtt_parm = t;
14480	}
14481    }
14482
14483  if (DECL_INTERFACE_KNOWN (decl1))
14484    {
14485      tree ctx = decl_function_context (decl1);
14486
14487      if (DECL_NOT_REALLY_EXTERN (decl1))
14488	DECL_EXTERNAL (decl1) = 0;
14489
14490      if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
14491	  && TREE_PUBLIC (ctx))
14492	/* This is a function in a local class in an extern inline
14493	   function.  */
14494	comdat_linkage (decl1);
14495    }
14496  /* If this function belongs to an interface, it is public.
14497     If it belongs to someone else's interface, it is also external.
14498     This only affects inlines and template instantiations.  */
14499  else if (interface_unknown == 0
14500	   && (! DECL_TEMPLATE_INSTANTIATION (decl1)
14501	       || flag_alt_external_templates))
14502    {
14503      if (DECL_DECLARED_INLINE_P (decl1)
14504	  || DECL_TEMPLATE_INSTANTIATION (decl1)
14505	  || processing_template_decl)
14506	{
14507	  DECL_EXTERNAL (decl1)
14508	    = (interface_only
14509	       || (DECL_DECLARED_INLINE_P (decl1)
14510		   && ! flag_implement_inlines
14511		   && !DECL_VINDEX (decl1)));
14512
14513	  /* For WIN32 we also want to put these in linkonce sections.  */
14514	  maybe_make_one_only (decl1);
14515	}
14516      else
14517	DECL_EXTERNAL (decl1) = 0;
14518      DECL_NOT_REALLY_EXTERN (decl1) = 0;
14519      DECL_INTERFACE_KNOWN (decl1) = 1;
14520    }
14521  else if (interface_unknown && interface_only
14522	   && (! DECL_TEMPLATE_INSTANTIATION (decl1)
14523	       || flag_alt_external_templates))
14524    {
14525      /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
14526	 interface, we will have interface_only set but not
14527	 interface_known.  In that case, we don't want to use the normal
14528	 heuristics because someone will supply a #pragma implementation
14529	 elsewhere, and deducing it here would produce a conflict.  */
14530      comdat_linkage (decl1);
14531      DECL_EXTERNAL (decl1) = 0;
14532      DECL_INTERFACE_KNOWN (decl1) = 1;
14533      DECL_DEFER_OUTPUT (decl1) = 1;
14534    }
14535  else
14536    {
14537      /* This is a definition, not a reference.
14538	 So clear DECL_EXTERNAL.  */
14539      DECL_EXTERNAL (decl1) = 0;
14540
14541      if ((DECL_DECLARED_INLINE_P (decl1)
14542	   || DECL_TEMPLATE_INSTANTIATION (decl1))
14543	  && ! DECL_INTERFACE_KNOWN (decl1)
14544	  /* Don't try to defer nested functions for now.  */
14545	  && ! decl_function_context (decl1))
14546	DECL_DEFER_OUTPUT (decl1) = 1;
14547      else
14548	DECL_INTERFACE_KNOWN (decl1) = 1;
14549    }
14550
14551  pushlevel (0);
14552  current_binding_level->parm_flag = 1;
14553
14554  ++function_depth;
14555
14556  if (DECL_DESTRUCTOR_P (decl1))
14557    {
14558      dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
14559      DECL_CONTEXT (dtor_label) = current_function_decl;
14560    }
14561
14562  start_fname_decls ();
14563
14564  store_parm_decls (current_function_parms);
14565
14566  return 1;
14567}
14568
14569/* Store the parameter declarations into the current function declaration.
14570   This is called after parsing the parameter declarations, before
14571   digesting the body of the function.
14572
14573   Also install to binding contour return value identifier, if any.  */
14574
14575static void
14576store_parm_decls (current_function_parms)
14577     tree current_function_parms;
14578{
14579  register tree fndecl = current_function_decl;
14580  register tree parm;
14581
14582  /* This is a chain of any other decls that came in among the parm
14583     declarations.  If a parm is declared with  enum {foo, bar} x;
14584     then CONST_DECLs for foo and bar are put here.  */
14585  tree nonparms = NULL_TREE;
14586
14587  if (current_function_parms)
14588    {
14589      /* This case is when the function was defined with an ANSI prototype.
14590	 The parms already have decls, so we need not do anything here
14591	 except record them as in effect
14592	 and complain if any redundant old-style parm decls were written.  */
14593
14594      tree specparms = current_function_parms;
14595      tree next;
14596
14597      /* Must clear this because it might contain TYPE_DECLs declared
14598	     at class level.  */
14599      storedecls (NULL_TREE);
14600
14601      /* If we're doing semantic analysis, then we'll call pushdecl
14602	     for each of these.  We must do them in reverse order so that
14603	     they end in the correct forward order.  */
14604      specparms = nreverse (specparms);
14605
14606      for (parm = specparms; parm; parm = next)
14607	{
14608	  next = TREE_CHAIN (parm);
14609	  if (TREE_CODE (parm) == PARM_DECL)
14610	    {
14611	      if (DECL_NAME (parm) == NULL_TREE
14612		  || TREE_CODE (parm) != VOID_TYPE)
14613		pushdecl (parm);
14614	      else
14615		error ("parameter `%D' declared void", parm);
14616	    }
14617	  else
14618	    {
14619	      /* If we find an enum constant or a type tag,
14620		 put it aside for the moment.  */
14621	      TREE_CHAIN (parm) = NULL_TREE;
14622	      nonparms = chainon (nonparms, parm);
14623	    }
14624	}
14625
14626      /* Get the decls in their original chain order and record in the
14627	 function.  This is all and only the PARM_DECLs that were
14628	 pushed into scope by the loop above.  */
14629      DECL_ARGUMENTS (fndecl) = getdecls ();
14630    }
14631  else
14632    DECL_ARGUMENTS (fndecl) = NULL_TREE;
14633
14634  /* Now store the final chain of decls for the arguments
14635     as the decl-chain of the current lexical scope.
14636     Put the enumerators in as well, at the front so that
14637     DECL_ARGUMENTS is not modified.  */
14638  storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
14639
14640  /* Do the starting of the exception specifications, if we have any.  */
14641  if (flag_exceptions && !processing_template_decl
14642      && flag_enforce_eh_specs
14643      && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14644    current_eh_spec_block = begin_eh_spec_block ();
14645}
14646
14647
14648/* We have finished doing semantic analysis on DECL, but have not yet
14649   generated RTL for its body.  Save away our current state, so that
14650   when we want to generate RTL later we know what to do.  */
14651
14652static void
14653save_function_data (decl)
14654     tree decl;
14655{
14656  struct language_function *f;
14657
14658  /* Save the language-specific per-function data so that we can
14659     get it back when we really expand this function.  */
14660  my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
14661		      19990908);
14662
14663  /* Make a copy.  */
14664  f = ((struct language_function *)
14665       ggc_alloc (sizeof (struct language_function)));
14666  memcpy (f, cp_function_chain, sizeof (struct language_function));
14667  DECL_SAVED_FUNCTION_DATA (decl) = f;
14668
14669  /* Clear out the bits we don't need.  */
14670  f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
14671  f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
14672  f->x_named_label_uses = NULL;
14673  f->bindings = NULL;
14674  f->x_local_names = NULL;
14675
14676  /* When we get back here again, we will be expanding.  */
14677  f->x_expanding_p = 1;
14678
14679  /* If we've already decided that we cannot inline this function, we
14680     must remember that fact when we actually go to expand the
14681     function.  */
14682  if (current_function_cannot_inline)
14683    {
14684      f->cannot_inline = current_function_cannot_inline;
14685      DECL_INLINE (decl) = 0;
14686    }
14687}
14688
14689/* Add a note to mark the beginning of the main body of the constructor.
14690   This is used to set up the data structures for the cleanup regions for
14691   fully-constructed bases and members.  */
14692
14693static void
14694begin_constructor_body ()
14695{
14696}
14697
14698/* Add a note to mark the end of the main body of the constructor.  This is
14699   used to end the cleanup regions for fully-constructed bases and
14700   members.  */
14701
14702static void
14703finish_constructor_body ()
14704{
14705}
14706
14707/* Do all the processing for the beginning of a destructor; set up the
14708   vtable pointers and cleanups for bases and members.  */
14709
14710static void
14711begin_destructor_body ()
14712{
14713  tree if_stmt;
14714  tree compound_stmt;
14715
14716  /* If the dtor is empty, and we know there is not any possible
14717     way we could use any vtable entries, before they are possibly
14718     set by a base class dtor, we don't have to setup the vtables,
14719     as we know that any base class dtor will set up any vtables
14720     it needs.  We avoid MI, because one base class dtor can do a
14721     virtual dispatch to an overridden function that would need to
14722     have a non-related vtable set up, we cannot avoid setting up
14723     vtables in that case.  We could change this to see if there
14724     is just one vtable.
14725
14726     ??? In the destructor for a class, the vtables are set
14727     appropriately for that class.  There will be no non-related
14728     vtables.  jason 2001-12-11.  */
14729  if_stmt = begin_if_stmt ();
14730
14731  /* If it is not safe to avoid setting up the vtables, then
14732     someone will change the condition to be boolean_true_node.
14733     (Actually, for now, we do not have code to set the condition
14734     appropriately, so we just assume that we always need to
14735     initialize the vtables.)  */
14736  finish_if_stmt_cond (boolean_true_node, if_stmt);
14737
14738  compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
14739
14740  /* Make all virtual function table pointers in non-virtual base
14741     classes point to CURRENT_CLASS_TYPE's virtual function
14742     tables.  */
14743  initialize_vtbl_ptrs (current_class_ptr);
14744
14745  finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
14746  finish_then_clause (if_stmt);
14747  finish_if_stmt ();
14748
14749  /* And insert cleanups for our bases and members so that they
14750     will be properly destroyed if we throw.  */
14751  push_base_cleanups ();
14752}
14753
14754/* At the end of every destructor we generate code to delete the object if
14755   necessary.  Do that now.  */
14756
14757static void
14758finish_destructor_body ()
14759{
14760  tree exprstmt;
14761
14762  /* Any return from a destructor will end up here; that way all base
14763     and member cleanups will be run when the function returns.  */
14764  add_stmt (build_stmt (LABEL_STMT, dtor_label));
14765
14766  /* In a virtual destructor, we must call delete.  */
14767  if (DECL_VIRTUAL_P (current_function_decl))
14768    {
14769      tree if_stmt;
14770      tree virtual_size = cxx_sizeof (current_class_type);
14771
14772      /* [class.dtor]
14773
14774      At the point of definition of a virtual destructor (including
14775      an implicit definition), non-placement operator delete shall
14776      be looked up in the scope of the destructor's class and if
14777      found shall be accessible and unambiguous.  */
14778      exprstmt = build_op_delete_call
14779	(DELETE_EXPR, current_class_ptr, virtual_size,
14780	 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
14781
14782      if_stmt = begin_if_stmt ();
14783      finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
14784				  current_in_charge_parm,
14785				  integer_one_node),
14786			   if_stmt);
14787      finish_expr_stmt (exprstmt);
14788      finish_then_clause (if_stmt);
14789      finish_if_stmt ();
14790    }
14791}
14792
14793/* Do the necessary processing for the beginning of a function body, which
14794   in this case includes member-initializers, but not the catch clauses of
14795   a function-try-block.  Currently, this means opening a binding level
14796   for the member-initializers (in a ctor) and member cleanups (in a dtor).
14797   In other functions, this isn't necessary, but it doesn't hurt.  */
14798
14799tree
14800begin_function_body ()
14801{
14802  tree stmt;
14803
14804  if (processing_template_decl)
14805    /* Do nothing now.  */;
14806  else
14807    /* Always keep the BLOCK node associated with the outermost pair of
14808       curly braces of a function.  These are needed for correct
14809       operation of dwarfout.c.  */
14810    keep_next_level (1);
14811
14812  stmt = begin_compound_stmt (0);
14813  COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
14814
14815  if (processing_template_decl)
14816    /* Do nothing now.  */;
14817  else if (DECL_CONSTRUCTOR_P (current_function_decl))
14818    begin_constructor_body ();
14819  else if (DECL_DESTRUCTOR_P (current_function_decl))
14820    begin_destructor_body ();
14821
14822  return stmt;
14823}
14824
14825/* Do the processing for the end of a function body.  Currently, this means
14826   closing out the cleanups for fully-constructed bases and members, and in
14827   the case of the destructor, deleting the object if desired.  Again, this
14828   is only meaningful for [cd]tors, since they are the only functions where
14829   there is a significant distinction between the main body and any
14830   function catch clauses.  Handling, say, main() return semantics here
14831   would be wrong, as flowing off the end of a function catch clause for
14832   main() would also need to return 0.  */
14833
14834void
14835finish_function_body (compstmt)
14836     tree compstmt;
14837{
14838  /* Close the block.  */
14839  finish_compound_stmt (0, compstmt);
14840
14841  if (processing_template_decl)
14842    /* Do nothing now.  */;
14843  else if (DECL_CONSTRUCTOR_P (current_function_decl))
14844    finish_constructor_body ();
14845  else if (DECL_DESTRUCTOR_P (current_function_decl))
14846    finish_destructor_body ();
14847}
14848
14849/* Finish up a function declaration and compile that function
14850   all the way to assembler language output.  The free the storage
14851   for the function definition.
14852
14853   FLAGS is a bitwise or of the following values:
14854     2 - INCLASS_INLINE
14855       We just finished processing the body of an in-class inline
14856       function definition.  (This processing will have taken place
14857       after the class definition is complete.)  */
14858
14859tree
14860finish_function (flags)
14861     int flags;
14862{
14863  register tree fndecl = current_function_decl;
14864  tree fntype, ctype = NULL_TREE;
14865  int inclass_inline = (flags & 2) != 0;
14866  int nested;
14867
14868  /* When we get some parse errors, we can end up without a
14869     current_function_decl, so cope.  */
14870  if (fndecl == NULL_TREE)
14871    return error_mark_node;
14872
14873  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14874      && DECL_VIRTUAL_P (fndecl)
14875      && !processing_template_decl)
14876    {
14877      tree fnclass = DECL_CONTEXT (fndecl);
14878      if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14879	keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14880    }
14881
14882  nested = function_depth > 1;
14883  fntype = TREE_TYPE (fndecl);
14884
14885  /*  TREE_READONLY (fndecl) = 1;
14886      This caused &foo to be of type ptr-to-const-function
14887      which then got a warning when stored in a ptr-to-function variable.  */
14888
14889  my_friendly_assert (building_stmt_tree (), 20000911);
14890
14891  finish_fname_decls ();
14892
14893  /* For a cloned function, we've already got all the code we need;
14894     there's no need to add any extra bits.  */
14895  if (!DECL_CLONED_FUNCTION_P (fndecl))
14896    {
14897      if (DECL_MAIN_P (current_function_decl))
14898	{
14899	  /* Make it so that `main' always returns 0 by default.  */
14900#if VMS_TARGET
14901	  finish_return_stmt (integer_one_node);
14902#else
14903	  finish_return_stmt (integer_zero_node);
14904#endif
14905	}
14906
14907      /* Finish dealing with exception specifiers.  */
14908      if (flag_exceptions && !processing_template_decl
14909	  && flag_enforce_eh_specs
14910	  && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14911	finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14912			      (TREE_TYPE (current_function_decl)),
14913			      current_eh_spec_block);
14914    }
14915
14916  /* If we're saving up tree structure, tie off the function now.  */
14917  finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14918
14919  /* If this function can't throw any exceptions, remember that.  */
14920  if (!processing_template_decl
14921      && !cp_function_chain->can_throw
14922      && !flag_non_call_exceptions)
14923    TREE_NOTHROW (fndecl) = 1;
14924
14925  /* This must come after expand_function_end because cleanups might
14926     have declarations (from inline functions) that need to go into
14927     this function's blocks.  */
14928
14929  /* If the current binding level isn't the outermost binding level
14930     for this function, either there is a bug, or we have experienced
14931     syntax errors and the statement tree is malformed.  */
14932  if (current_binding_level->parm_flag != 1)
14933    {
14934      /* Make sure we have already experienced errors.  */
14935      if (errorcount == 0)
14936	abort ();
14937
14938      /* Throw away the broken statement tree and extra binding
14939         levels.  */
14940      DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14941
14942      while (current_binding_level->parm_flag != 1)
14943	{
14944	  if (current_binding_level->parm_flag == 2)
14945	    pop_nested_class ();
14946	  else
14947	    poplevel (0, 0, 0);
14948	}
14949    }
14950  poplevel (1, 0, 1);
14951
14952  /* Set up the named return value optimization, if we can.  Here, we
14953     eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14954     for the nrv.  genrtl_start_function and declare_return_variable
14955     handle making the nrv and RESULT_DECL share space.  */
14956  if (current_function_return_value)
14957    {
14958      tree r = current_function_return_value;
14959      tree outer;
14960
14961      if (r != error_mark_node
14962	  /* This is only worth doing for fns that return in memory--and
14963	     simpler, since we don't have to worry about promoted modes.  */
14964	  && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
14965	  /* Only allow this for variables declared in the outer scope of
14966	     the function so we know that their lifetime always ends with a
14967	     return; see g++.dg/opt/nrv6.C.  We could be more flexible if
14968	     we were to do this optimization in tree-ssa.  */
14969	  && (outer = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl)),
14970	      chain_member (r, BLOCK_VARS (outer))))
14971	{
14972
14973	  DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14974	  walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14975					nullify_returns_r, r);
14976	}
14977      else
14978	/* Clear it so genrtl_start_function and declare_return_variable
14979	   know we're not optimizing.  */
14980	current_function_return_value = NULL_TREE;
14981    }
14982
14983  /* Remember that we were in class scope.  */
14984  if (current_class_name)
14985    ctype = current_class_type;
14986
14987  /* Must mark the RESULT_DECL as being in this function.  */
14988  DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14989
14990  /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14991     to the FUNCTION_DECL node itself.  */
14992  BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14993
14994  /* Save away current state, if appropriate.  */
14995  if (!processing_template_decl)
14996    save_function_data (fndecl);
14997
14998  /* If this function calls `setjmp' it cannot be inlined.  When
14999     `longjmp' is called it is not guaranteed to restore the value of
15000     local variables that have been modified since the call to
15001     `setjmp'.  So, if were to inline this function into some caller
15002     `c', then when we `longjmp', we might not restore all variables
15003     in `c'.  (It might seem, at first blush, that there's no way for
15004     this function to modify local variables in `c', but their
15005     addresses may have been stored somewhere accessible to this
15006     function.)  */
15007  if (!processing_template_decl && calls_setjmp_p (fndecl))
15008    DECL_UNINLINABLE (fndecl) = 1;
15009
15010  /* Complain if there's just no return statement.  */
15011  if (warn_return_type
15012      && !processing_template_decl
15013      && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
15014      && !current_function_returns_value && !current_function_returns_null
15015      /* Don't complain if we abort or throw.  */
15016      && !current_function_returns_abnormally
15017      && !DECL_NAME (DECL_RESULT (fndecl))
15018      /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
15019	 inline function, as we might never be compiled separately.  */
15020      && DECL_INLINE (fndecl))
15021    warning ("no return statement in function returning non-void");
15022
15023  /* Clear out memory we no longer need.  */
15024  free_after_parsing (cfun);
15025  /* Since we never call rest_of_compilation, we never clear
15026     CFUN.  Do so explicitly.  */
15027  free_after_compilation (cfun);
15028  cfun = NULL;
15029
15030  /* If this is an in-class inline definition, we may have to pop the
15031     bindings for the template parameters that we added in
15032     maybe_begin_member_template_processing when start_function was
15033     called.  */
15034  if (inclass_inline)
15035    maybe_end_member_template_processing ();
15036
15037  /* Leave the scope of the class.  */
15038  if (ctype)
15039    pop_nested_class ();
15040
15041  --function_depth;
15042
15043  /* Clean up.  */
15044  if (! nested)
15045    /* Let the error reporting routines know that we're outside a
15046       function.  For a nested function, this value is used in
15047       cxx_pop_function_context and then reset via pop_function_context.  */
15048    current_function_decl = NULL_TREE;
15049
15050  return fndecl;
15051}
15052
15053/* Create the FUNCTION_DECL for a function definition.
15054   DECLSPECS and DECLARATOR are the parts of the declaration;
15055   they describe the return type and the name of the function,
15056   but twisted together in a fashion that parallels the syntax of C.
15057
15058   This function creates a binding context for the function body
15059   as well as setting up the FUNCTION_DECL in current_function_decl.
15060
15061   Returns a FUNCTION_DECL on success.
15062
15063   If the DECLARATOR is not suitable for a function (it defines a datum
15064   instead), we return 0, which tells yyparse to report a parse error.
15065
15066   May return void_type_node indicating that this method is actually
15067   a friend.  See grokfield for more details.
15068
15069   Came here with a `.pushlevel' .
15070
15071   DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15072   CHANGES TO CODE IN `grokfield'.  */
15073
15074tree
15075start_method (declspecs, declarator, attrlist)
15076     tree declarator, declspecs, attrlist;
15077{
15078  tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15079				&attrlist);
15080
15081  /* Something too ugly to handle.  */
15082  if (fndecl == NULL_TREE)
15083    return NULL_TREE;
15084
15085  if (attrlist)
15086    cplus_decl_attributes (&fndecl, attrlist, 0);
15087
15088  /* Pass friends other than inline friend functions back.  */
15089  if (fndecl == void_type_node)
15090    return fndecl;
15091
15092  if (TREE_CODE (fndecl) != FUNCTION_DECL)
15093    /* Not a function, tell parser to report parse error.  */
15094    return NULL_TREE;
15095
15096  if (DECL_IN_AGGR_P (fndecl))
15097    {
15098      if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
15099	{
15100	  if (DECL_CONTEXT (fndecl)
15101	      && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
15102	    error ("`%D' is already defined in class `%T'", fndecl,
15103	              DECL_CONTEXT (fndecl));
15104	}
15105      return void_type_node;
15106    }
15107
15108  check_template_shadow (fndecl);
15109
15110  DECL_DECLARED_INLINE_P (fndecl) = 1;
15111
15112  DID_INLINE_FUNC (fndecl) = 0;
15113  if (flag_default_inline)
15114    DECL_INLINE (fndecl) = 1;
15115
15116  /* We process method specializations in finish_struct_1.  */
15117  if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15118    fndecl = push_template_decl (fndecl);
15119
15120  if (! DECL_FRIEND_P (fndecl))
15121    {
15122      if (TREE_CHAIN (fndecl))
15123	{
15124	  fndecl = copy_node (fndecl);
15125	  TREE_CHAIN (fndecl) = NULL_TREE;
15126	}
15127      grok_special_member_properties (fndecl);
15128    }
15129
15130  cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
15131
15132  /* Make a place for the parms */
15133  pushlevel (0);
15134  current_binding_level->parm_flag = 1;
15135
15136  DECL_IN_AGGR_P (fndecl) = 1;
15137  return fndecl;
15138}
15139
15140/* Go through the motions of finishing a function definition.
15141   We don't compile this method until after the whole class has
15142   been processed.
15143
15144   FINISH_METHOD must return something that looks as though it
15145   came from GROKFIELD (since we are defining a method, after all).
15146
15147   This is called after parsing the body of the function definition.
15148   STMTS is the chain of statements that makes up the function body.
15149
15150   DECL is the ..._DECL that `start_method' provided.  */
15151
15152tree
15153finish_method (decl)
15154     tree decl;
15155{
15156  register tree fndecl = decl;
15157  tree old_initial;
15158
15159  register tree link;
15160
15161  if (decl == void_type_node)
15162    return decl;
15163
15164  old_initial = DECL_INITIAL (fndecl);
15165
15166  /* Undo the level for the parms (from start_method).
15167     This is like poplevel, but it causes nothing to be
15168     saved.  Saving information here confuses symbol-table
15169     output routines.  Besides, this information will
15170     be correctly output when this method is actually
15171     compiled.  */
15172
15173  /* Clear out the meanings of the local variables of this level;
15174     also record in each decl which block it belongs to.  */
15175
15176  for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
15177    {
15178      if (DECL_NAME (link) != NULL_TREE)
15179	pop_binding (DECL_NAME (link), link);
15180      my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
15181      DECL_CONTEXT (link) = NULL_TREE;
15182    }
15183
15184  poplevel (0, 0, 0);
15185
15186  DECL_INITIAL (fndecl) = old_initial;
15187
15188  /* We used to check if the context of FNDECL was different from
15189     current_class_type as another way to get inside here.  This didn't work
15190     for String.cc in libg++.  */
15191  if (DECL_FRIEND_P (fndecl))
15192    {
15193      CLASSTYPE_INLINE_FRIENDS (current_class_type)
15194	= tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
15195      decl = void_type_node;
15196    }
15197
15198  return decl;
15199}
15200
15201
15202/* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
15203   we can lay it out later, when and if its type becomes complete.  */
15204
15205void
15206maybe_register_incomplete_var (var)
15207     tree var;
15208{
15209  my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
15210
15211  /* Keep track of variables with incomplete types.  */
15212  if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15213      && DECL_EXTERNAL (var))
15214    {
15215      tree inner_type = TREE_TYPE (var);
15216
15217      while (TREE_CODE (inner_type) == ARRAY_TYPE)
15218	inner_type = TREE_TYPE (inner_type);
15219      inner_type = TYPE_MAIN_VARIANT (inner_type);
15220
15221      if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15222	  /* RTTI TD entries are created while defining the type_info.  */
15223	  || (TYPE_LANG_SPECIFIC (inner_type)
15224	      && TYPE_BEING_DEFINED (inner_type)))
15225	incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
15226    }
15227}
15228
15229/* Called when a class type (given by TYPE) is defined.  If there are
15230   any existing VAR_DECLs whose type hsa been completed by this
15231   declaration, update them now.  */
15232
15233void
15234complete_vars (type)
15235     tree type;
15236{
15237  tree *list = &incomplete_vars;
15238
15239  my_friendly_assert (CLASS_TYPE_P (type), 20020406);
15240  while (*list)
15241    {
15242      if (same_type_p (type, TREE_PURPOSE (*list)))
15243	{
15244	  tree var = TREE_VALUE (*list);
15245	  /* Complete the type of the variable.  The VAR_DECL itself
15246	     will be laid out in expand_expr.  */
15247	  complete_type (TREE_TYPE (var));
15248	  /* Remove this entry from the list.  */
15249	  *list = TREE_CHAIN (*list);
15250	}
15251      else
15252	list = &TREE_CHAIN (*list);
15253    }
15254}
15255
15256/* If DECL is of a type which needs a cleanup, build that cleanup
15257   here.  */
15258
15259tree
15260cxx_maybe_build_cleanup (decl)
15261     tree decl;
15262{
15263  tree type = TREE_TYPE (decl);
15264
15265  if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
15266    {
15267      int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
15268      tree rval;
15269
15270      if (TREE_CODE (type) == ARRAY_TYPE)
15271	rval = decl;
15272      else
15273	{
15274	  cxx_mark_addressable (decl);
15275	  rval = build_unary_op (ADDR_EXPR, decl, 0);
15276	}
15277
15278      /* Optimize for space over speed here.  */
15279      if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
15280	  || flag_expensive_optimizations)
15281	flags |= LOOKUP_NONVIRTUAL;
15282
15283      rval = build_delete (TREE_TYPE (rval), rval,
15284			   sfk_complete_destructor, flags, 0);
15285
15286      if (TYPE_USES_VIRTUAL_BASECLASSES (type)
15287	  && ! TYPE_HAS_DESTRUCTOR (type))
15288	rval = build_compound_expr (tree_cons (NULL_TREE, rval,
15289					       build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
15290
15291      return rval;
15292    }
15293  return NULL_TREE;
15294}
15295
15296/* When a stmt has been parsed, this function is called.  */
15297
15298void
15299finish_stmt ()
15300{
15301  /* Always assume this statement was not an expression statement.  If
15302     it actually was an expression statement, its our callers
15303     responsibility to fix this up.  */
15304  last_expr_type = NULL_TREE;
15305}
15306
15307/* DECL was originally constructed as a non-static member function,
15308   but turned out to be static.  Update it accordingly.  */
15309
15310void
15311revert_static_member_fn (decl)
15312     tree decl;
15313{
15314  tree tmp;
15315  tree function = TREE_TYPE (decl);
15316  tree args = TYPE_ARG_TYPES (function);
15317
15318  if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
15319      != TYPE_UNQUALIFIED)
15320    error ("static member function `%#D' declared with type qualifiers",
15321	      decl);
15322
15323  args = TREE_CHAIN (args);
15324  tmp = build_function_type (TREE_TYPE (function), args);
15325  tmp = build_qualified_type (tmp, cp_type_quals (function));
15326  tmp = build_exception_variant (tmp,
15327				 TYPE_RAISES_EXCEPTIONS (function));
15328  TREE_TYPE (decl) = tmp;
15329  if (DECL_ARGUMENTS (decl))
15330    DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
15331  DECL_STATIC_FUNCTION_P (decl) = 1;
15332}
15333
15334/* Initialize the variables used during compilation of a C++
15335   function.  */
15336
15337void
15338cxx_push_function_context (f)
15339     struct function *f;
15340{
15341  struct language_function *p
15342    = ((struct language_function *)
15343       ggc_alloc_cleared (sizeof (struct language_function)));
15344  f->language = p;
15345
15346  /* It takes an explicit call to expand_body to generate RTL for a
15347     function.  */
15348  expanding_p = 0;
15349
15350  /* Whenever we start a new function, we destroy temporaries in the
15351     usual way.  */
15352  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
15353}
15354
15355/* Free the language-specific parts of F, now that we've finished
15356   compiling the function.  */
15357
15358void
15359cxx_pop_function_context (f)
15360     struct function *f;
15361{
15362  f->language = 0;
15363}
15364
15365/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
15366   one of the language-independent trees.  */
15367
15368enum cp_tree_node_structure_enum
15369cp_tree_node_structure (t)
15370     union lang_tree_node *t;
15371{
15372  switch (TREE_CODE (&t->generic))
15373    {
15374    case DEFAULT_ARG:		return TS_CP_IDENTIFIER;
15375    case IDENTIFIER_NODE:	return TS_CP_IDENTIFIER;
15376    case OVERLOAD:		return TS_CP_OVERLOAD;
15377    case TEMPLATE_PARM_INDEX:	return TS_CP_TPI;
15378    case PTRMEM_CST:		return TS_CP_PTRMEM;
15379    case WRAPPER:		return TS_CP_WRAPPER;
15380    case SRCLOC:		return TS_CP_SRCLOC;
15381    default:			return TS_CP_GENERIC;
15382    }
15383}
15384
15385/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
15386   the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
15387
15388tree
15389identifier_global_value	(t)
15390     tree t;
15391{
15392  return IDENTIFIER_GLOBAL_VALUE (t);
15393}
15394
15395/* Build the void_list_node (void_type_node having been created).  */
15396tree
15397build_void_list_node ()
15398{
15399  tree t = build_tree_list (NULL_TREE, void_type_node);
15400  TREE_PARMLIST (t) = 1;
15401  return t;
15402}
15403
15404static int
15405cp_missing_noreturn_ok_p (decl)
15406     tree decl;
15407{
15408  /* A missing noreturn is ok for the `main' function.  */
15409  return DECL_MAIN_P (decl);
15410}
15411
15412#include "gt-cp-decl.h"
15413#include "gtype-cp.h"
15414