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
24/* Process declarations and symbol lookup for C front end.
25   Also constructs types; the standard scalar types at initialization,
26   and structure, union, array and enum types when they are declared.  */
27
28/* ??? not all decl nodes are given the most useful possible
29   line numbers.  For example, the CONST_DECLs for enum values.  */
30
31#include "config.h"
32#include "system.h"
33#include "tree.h"
34#include "rtl.h"
35#include "expr.h"
36#include "flags.h"
37#include "cp-tree.h"
38#include "tree-inline.h"
39#include "decl.h"
40#include "lex.h"
41#include "output.h"
42#include "except.h"
43#include "toplev.h"
44#include "hashtab.h"
45#include "ggc.h"
46#include "tm_p.h"
47#include "target.h"
48#include "c-common.h"
49#include "c-pragma.h"
50#include "diagnostic.h"
51#include "debug.h"
52#include "timevar.h"
53#include "input.h"
54
55static tree grokparms				PARAMS ((tree));
56static const char *redeclaration_error_message	PARAMS ((tree, tree));
57
58static void push_binding_level PARAMS ((struct cp_binding_level *, int,
59				      int));
60static void pop_binding_level PARAMS ((void));
61static void suspend_binding_level PARAMS ((void));
62static void resume_binding_level PARAMS ((struct cp_binding_level *));
63static struct cp_binding_level *make_binding_level PARAMS ((void));
64static void declare_namespace_level PARAMS ((void));
65static int decl_jump_unsafe PARAMS ((tree));
66static void storedecls PARAMS ((tree));
67static void require_complete_types_for_parms PARAMS ((tree));
68static int ambi_op_p PARAMS ((enum tree_code));
69static int unary_op_p PARAMS ((enum tree_code));
70static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
71static tree lookup_tag_reverse PARAMS ((tree, tree));
72static tree lookup_name_real PARAMS ((tree, int, int, int));
73static void push_local_name PARAMS ((tree));
74static void warn_extern_redeclared_static PARAMS ((tree, tree));
75static tree grok_reference_init PARAMS ((tree, tree, tree, tree *));
76static tree grokfndecl PARAMS ((tree, tree, tree, tree, int,
77			      enum overload_flags, tree,
78			      tree, int, int, int, int, int, int, tree));
79static tree grokvardecl PARAMS ((tree, tree, RID_BIT_TYPE *, int, int, tree));
80static tree follow_tag_typedef PARAMS ((tree));
81static tree lookup_tag PARAMS ((enum tree_code, tree,
82			      struct cp_binding_level *, int));
83static void set_identifier_type_value_with_scope
84	PARAMS ((tree, tree, struct cp_binding_level *));
85static void record_unknown_type PARAMS ((tree, const char *));
86static tree builtin_function_1 PARAMS ((const char *, tree, tree, int,
87                                      enum built_in_class, const char *,
88				      tree));
89static tree build_library_fn_1 PARAMS ((tree, enum tree_code, tree));
90static int member_function_or_else PARAMS ((tree, tree, enum overload_flags));
91static void bad_specifiers PARAMS ((tree, const char *, int, int, int, int,
92				  int));
93static tree maybe_process_template_type_declaration PARAMS ((tree, int, struct cp_binding_level*));
94static void check_for_uninitialized_const_var PARAMS ((tree));
95static hashval_t typename_hash PARAMS ((const void *));
96static int typename_compare PARAMS ((const void *, const void *));
97static void push_binding PARAMS ((tree, tree, struct cp_binding_level*));
98static int add_binding PARAMS ((tree, tree));
99static void pop_binding PARAMS ((tree, tree));
100static tree local_variable_p_walkfn PARAMS ((tree *, int *, void *));
101static cxx_binding *find_binding (tree, tree, cxx_binding *);
102static tree select_decl (cxx_binding *, int);
103static int lookup_flags PARAMS ((int, int));
104static tree qualify_lookup PARAMS ((tree, int));
105static tree record_builtin_java_type PARAMS ((const char *, int));
106static const char *tag_name PARAMS ((enum tag_types code));
107static void find_class_binding_level PARAMS ((void));
108static struct cp_binding_level *innermost_nonclass_level PARAMS ((void));
109static void warn_about_implicit_typename_lookup PARAMS ((tree, tree));
110static int walk_namespaces_r PARAMS ((tree, walk_namespaces_fn, void *));
111static int walk_globals_r PARAMS ((tree, void *));
112static int walk_vtables_r PARAMS ((tree, void*));
113static void add_decl_to_level PARAMS ((tree, struct cp_binding_level *));
114static tree make_label_decl PARAMS ((tree, int));
115static void use_label PARAMS ((tree));
116static void check_previous_goto_1 PARAMS ((tree, struct cp_binding_level *, tree,
117					   const char *, int));
118static void check_previous_goto PARAMS ((struct named_label_use_list *));
119static void check_switch_goto PARAMS ((struct cp_binding_level *));
120static void check_previous_gotos PARAMS ((tree));
121static void pop_label PARAMS ((tree, tree));
122static void pop_labels PARAMS ((tree));
123static void maybe_deduce_size_from_array_init PARAMS ((tree, tree));
124static void layout_var_decl PARAMS ((tree));
125static void maybe_commonize_var PARAMS ((tree));
126static tree check_initializer (tree, tree, int, tree *);
127static void make_rtl_for_nonlocal_decl PARAMS ((tree, tree, const char *));
128static void save_function_data PARAMS ((tree));
129static void check_function_type PARAMS ((tree, tree));
130static void begin_constructor_body PARAMS ((void));
131static void finish_constructor_body PARAMS ((void));
132static void begin_destructor_body PARAMS ((void));
133static void finish_destructor_body PARAMS ((void));
134static tree create_array_type_for_decl PARAMS ((tree, tree, tree));
135static tree get_atexit_node PARAMS ((void));
136static tree get_dso_handle_node PARAMS ((void));
137static tree start_cleanup_fn PARAMS ((void));
138static void end_cleanup_fn PARAMS ((void));
139static tree cp_make_fname_decl PARAMS ((tree, int));
140static void initialize_predefined_identifiers PARAMS ((void));
141static tree check_special_function_return_type
142  PARAMS ((special_function_kind, tree, tree));
143static tree push_cp_library_fn PARAMS ((enum tree_code, tree));
144static tree build_cp_library_fn PARAMS ((tree, enum tree_code, tree));
145static void store_parm_decls PARAMS ((tree));
146static int cp_missing_noreturn_ok_p PARAMS ((tree));
147static void initialize_local_var (tree, tree);
148static void expand_static_init (tree, tree);
149static tree next_initializable_field (tree);
150static bool reshape_init_array (tree, tree, tree *, tree);
151static tree reshape_init (tree, tree *);
152
153/* Erroneous argument lists can use this *IFF* they do not modify it.  */
154tree error_mark_list;
155
156/* The following symbols are subsumed in the cp_global_trees array, and
157   listed here individually for documentation purposes.
158
159   C++ extensions
160	tree wchar_decl_node;
161
162	tree vtable_entry_type;
163	tree delta_type_node;
164	tree __t_desc_type_node;
165        tree ti_desc_type_node;
166	tree bltn_desc_type_node, ptr_desc_type_node;
167	tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
168	tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
169	tree ptm_desc_type_node;
170	tree base_desc_type_node;
171
172	tree class_type_node, record_type_node, union_type_node, enum_type_node;
173	tree unknown_type_node;
174
175   Array type `vtable_entry_type[]'
176
177	tree vtbl_type_node;
178	tree vtbl_ptr_type_node;
179
180   Namespaces,
181
182	tree std_node;
183	tree abi_node;
184
185   A FUNCTION_DECL which can call `abort'.  Not necessarily the
186   one that the user will declare, but sufficient to be called
187   by routines that want to abort the program.
188
189	tree abort_fndecl;
190
191   The FUNCTION_DECL for the default `::operator delete'.
192
193	tree global_delete_fndecl;
194
195   Used by RTTI
196	tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
197	tree tinfo_var_id;
198
199*/
200
201tree cp_global_trees[CPTI_MAX];
202
203/* Indicates that there is a type value in some namespace, although
204   that is not necessarily in scope at the moment.  */
205
206static GTY(()) tree global_type_node;
207
208/* Expect only namespace names now.  */
209static int only_namespace_names;
210
211/* Used only for jumps to as-yet undefined labels, since jumps to
212   defined labels can have their validity checked immediately.  */
213
214struct named_label_use_list GTY(())
215{
216  struct cp_binding_level *binding_level;
217  tree names_in_scope;
218  tree label_decl;
219  const char *filename_o_goto;
220  int lineno_o_goto;
221  struct named_label_use_list *next;
222};
223
224#define named_label_uses cp_function_chain->x_named_label_uses
225
226#define local_names cp_function_chain->x_local_names
227
228/* A list of objects which have constructors or destructors
229   which reside in the global scope.  The decl is stored in
230   the TREE_VALUE slot and the initializer is stored
231   in the TREE_PURPOSE slot.  */
232tree static_aggregates;
233
234/* -- end of C++ */
235
236/* A node for the integer constants 2, and 3.  */
237
238tree integer_two_node, integer_three_node;
239
240/* Similar, for last_function_parm_tags.  */
241tree last_function_parms;
242
243/* A list of all LABEL_DECLs in the function that have names.  Here so
244   we can clear out their names' definitions at the end of the
245   function, and so we can check the validity of jumps to these labels.  */
246
247struct named_label_list GTY(())
248{
249  struct cp_binding_level *binding_level;
250  tree names_in_scope;
251  tree old_value;
252  tree label_decl;
253  tree bad_decls;
254  struct named_label_list *next;
255  unsigned int in_try_scope : 1;
256  unsigned int in_catch_scope : 1;
257};
258
259#define named_labels cp_function_chain->x_named_labels
260
261/* The name of the anonymous namespace, throughout this translation
262   unit.  */
263tree anonymous_namespace_name;
264
265/* The number of function bodies which we are currently processing.
266   (Zero if we are at namespace scope, one inside the body of a
267   function, two inside the body of a function in a local class, etc.)  */
268int function_depth;
269
270/* States indicating how grokdeclarator() should handle declspecs marked
271   with __attribute__((deprecated)).  An object declared as
272   __attribute__((deprecated)) suppresses warnings of uses of other
273   deprecated items.  */
274
275enum deprecated_states {
276  DEPRECATED_NORMAL,
277  DEPRECATED_SUPPRESS
278};
279
280static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
281
282/* Set by add_implicitly_declared_members() to keep those members from
283   being flagged as deprecated or reported as using deprecated
284   types.  */
285int adding_implicit_members = 0;
286
287/* True if a declaration with an `extern' linkage specifier is being
288   processed.  */
289bool have_extern_spec;
290
291
292/* Compute the chain index of a binding_entry given the HASH value of its
293   name and the total COUNT of chains.  COUNT is assumed to be a power
294   of 2.  */
295#define ENTRY_INDEX(HASH, COUNT) (((HASH) >> 3) & ((COUNT) - 1))
296
297/* A free list of "binding_entry"s awaiting for re-use.  */
298static GTY((deletable("")))  binding_entry free_binding_entry;
299
300/* Create a binding_entry object for (NAME, TYPE).  */
301static inline binding_entry
302binding_entry_make (tree name, tree type)
303{
304  binding_entry entry;
305
306  if (free_binding_entry)
307    {
308      entry = free_binding_entry;
309      free_binding_entry = entry->chain;
310    }
311  else
312    entry = ggc_alloc (sizeof (struct binding_entry_s));
313
314  entry->name = name;
315  entry->type = type;
316
317  return entry;
318}
319
320/* Put ENTRY back on the free list.  */
321static inline void
322binding_entry_free (binding_entry entry)
323{
324  entry->chain = free_binding_entry;
325  free_binding_entry = entry;
326}
327
328/* The datatype used to implement the mapping from names to types at
329   a given scope.  */
330struct binding_table_s GTY(())
331{
332  /* Array of chains of "binding_entry"s  */
333  binding_entry * GTY((length ("%h.chain_count"))) chain;
334
335  /* The number of chains in this table.  This is the length of the
336     the member "chaiin" considered as an array.  */
337  size_t chain_count;
338
339  /* Number of "binding_entry"s in this table.  */
340  size_t entry_count;
341};
342
343/* These macros indicate the initial chains count for binding_table.  */
344#define SCOPE_DEFAULT_HT_SIZE                        (1 << 3)
345#define CLASS_SCOPE_HT_SIZE                          (1 << 3)
346#define NAMESPACE_ORDINARY_HT_SIZE                   (1 << 5)
347#define NAMESPACE_STD_HT_SIZE                        (1 << 8)
348#define GLOBAL_SCOPE_HT_SIZE                         (1 << 8)
349
350/* Construct TABLE with an initial CHAIN_COUNT.  */
351static inline void
352binding_table_construct (binding_table table, size_t chain_count)
353{
354  table->chain_count = chain_count;
355  table->entry_count = 0;
356  table->chain = ggc_alloc_cleared
357    (table->chain_count * sizeof (binding_entry));
358}
359
360/* Free TABLE by making its entries ready for reuse. */
361static inline void
362binding_table_free (binding_table table)
363{
364  size_t i;
365  if (table == NULL)
366    return;
367
368  for (i = 0; i < table->chain_count; ++i)
369    {
370      while (table->chain[i] != NULL)
371        {
372          binding_entry entry = table->chain[i];
373          table->chain[i] = entry->chain;
374          binding_entry_free (entry);
375        }
376    }
377  table->entry_count = 0;
378}
379
380/* Allocate a table with CHAIN_COUNT, assumed to be a power of two.  */
381static inline binding_table
382binding_table_new (size_t chain_count)
383{
384  binding_table table = ggc_alloc (sizeof (struct binding_table_s));
385  binding_table_construct (table, chain_count);
386  return table;
387}
388
389/* Expand TABLE to twice its current chain_count.  */
390static void
391binding_table_expand (binding_table table)
392{
393  const size_t old_chain_count = table->chain_count;
394  const size_t old_entry_count = table->entry_count;
395  const size_t new_chain_count = 2 * old_chain_count;
396  binding_entry *old_chains = table->chain;
397  size_t i;
398
399  binding_table_construct (table, new_chain_count);
400  for (i = 0; i < old_chain_count; ++i)
401    {
402      binding_entry entry = old_chains[i];
403      for (; entry != NULL; entry = old_chains[i])
404        {
405          const unsigned int hash = IDENTIFIER_HASH_VALUE (entry->name);
406          const size_t j = ENTRY_INDEX (hash, new_chain_count);
407
408          old_chains[i] = entry->chain;
409          entry->chain = table->chain[j];
410          table->chain[j] = entry;
411        }
412    }
413  table->entry_count = old_entry_count;
414}
415
416/* Insert a binding for NAME to TYPe into TABLE.  */
417static inline void
418binding_table_insert (binding_table table, tree name, tree type)
419{
420  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
421  const size_t i = ENTRY_INDEX (hash, table->chain_count);
422  binding_entry entry = binding_entry_make (name, type);
423
424  entry->chain = table->chain[i];
425  table->chain[i] = entry;
426  ++table->entry_count;
427
428  if (3 * table->chain_count < 5 * table->entry_count)
429    binding_table_expand (table);
430}
431
432/* Return the binding_entry, if any, that maps NAME.  */
433binding_entry
434binding_table_find (binding_table table, tree name)
435{
436  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
437  binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
438
439  while (entry != NULL && entry->name != name)
440    entry = entry->chain;
441
442  return entry;
443}
444
445/* Return the binding_entry, if any, that maps name to an anonymous type.  */
446static inline tree
447binding_table_find_anon_type (binding_table table, tree name)
448{
449  const unsigned int hash = IDENTIFIER_HASH_VALUE (name);
450  binding_entry entry = table->chain[ENTRY_INDEX (hash, table->chain_count)];
451
452  while (entry != NULL && TYPE_IDENTIFIER (entry->type) != name)
453    entry = entry->chain;
454
455  return entry ? entry->type : NULL;
456}
457
458/* Return the binding_entry, if any, that has TYPE as target.  If NAME
459   is non-null, then set the domain and rehash that entry.  */
460static inline binding_entry
461binding_table_reverse_maybe_remap (binding_table table, tree type, tree name)
462{
463  const size_t chain_count = table->chain_count;
464  binding_entry entry = NULL;
465  binding_entry *p = NULL;
466  size_t i;
467
468  for (i = 0; i < chain_count && entry == NULL; ++i)
469    {
470      p = &table->chain[i];
471      while (*p != NULL && entry == NULL)
472        if ((*p)->type == type)
473          entry = *p;
474        else
475          p = &(*p)->chain;
476    }
477
478  if (entry != NULL && name != NULL && entry->name != name)
479    {
480      /* Remove the bucket from the previous chain.  */
481      *p = (*p)->chain;
482
483      /* Remap the name type to type.  */
484      i = ENTRY_INDEX (IDENTIFIER_HASH_VALUE (name), chain_count);
485      entry->chain = table->chain[i];
486      entry->name = name;
487      table->chain[i] = entry;
488    }
489
490  return entry;
491}
492
493/* Remove from TABLE all entries that map to anonymous enums or
494   class-types.  */
495static void
496binding_table_remove_anonymous_types (binding_table table)
497{
498  const size_t chain_count = table->chain_count;
499  size_t i;
500
501  for (i = 0; i < chain_count; ++i)
502    {
503      binding_entry *p = &table->chain[i];
504
505      while (*p != NULL)
506        if (ANON_AGGRNAME_P ((*p)->name))
507          {
508            binding_entry e = *p;
509            *p = (*p)->chain;
510            --table->entry_count;
511            binding_entry_free (e);
512          }
513        else
514          p = &(*p)->chain;
515    }
516}
517
518/* Apply PROC -- with DATA -- to all entries in TABLE.  */
519void
520binding_table_foreach (binding_table table, bt_foreach_proc proc, void *data)
521{
522  const size_t chain_count = table->chain_count;
523  size_t i;
524
525  for (i = 0; i < chain_count; ++i)
526    {
527      binding_entry entry = table->chain[i];
528      for (; entry != NULL; entry = entry->chain)
529        proc (entry, data);
530    }
531}
532
533
534/* For each binding contour we allocate a binding_level structure
535   which records the names defined in that contour.
536   Contours include:
537    0) the global one
538    1) one for each function definition,
539       where internal declarations of the parameters appear.
540    2) one for each compound statement,
541       to record its declarations.
542
543   The current meaning of a name can be found by searching the levels
544   from the current one out to the global one.
545
546   Off to the side, may be the class_binding_level.  This exists only
547   to catch class-local declarations.  It is otherwise nonexistent.
548
549   Also there may be binding levels that catch cleanups that must be
550   run when exceptions occur.  Thus, to see whether a name is bound in
551   the current scope, it is not enough to look in the
552   CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
553   instead.  */
554
555/* Note that the information in the `names' component of the global contour
556   is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
557
558struct cp_binding_level GTY(())
559  {
560    /* A chain of _DECL nodes for all variables, constants, functions,
561       and typedef types.  These are in the reverse of the order
562       supplied.  There may be OVERLOADs on this list, too, but they
563       are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
564    tree names;
565
566    /* Count of elements in names chain.  */
567    size_t names_size;
568
569    /* A chain of NAMESPACE_DECL nodes.  */
570    tree namespaces;
571
572    /* An array of static functions and variables (for namespaces only) */
573    varray_type static_decls;
574
575    /* A chain of VTABLE_DECL nodes.  */
576    tree vtables;
577
578    /* A dictionary for looking up enums or class-types names.  */
579    binding_table type_decls;
580
581    /* A list of USING_DECL nodes.  */
582    tree usings;
583
584    /* A list of used namespaces. PURPOSE is the namespace,
585       VALUE the common ancestor with this binding_level's namespace.  */
586    tree using_directives;
587
588    /* If this binding level is the binding level for a class, then
589       class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
590       is the name of an entity bound in the class.  The TREE_TYPE is
591       the DECL bound by this name in the class.  */
592    tree class_shadowed;
593
594    /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
595       is used for all binding levels. In addition the TREE_VALUE is the
596       IDENTIFIER_TYPE_VALUE before we entered the class.  */
597    tree type_shadowed;
598
599    /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
600       label in this scope.  The TREE_PURPOSE is the previous value of
601       the IDENTIFIER_LABEL VALUE.  */
602    tree shadowed_labels;
603
604    /* For each level (except not the global one),
605       a chain of BLOCK nodes for all the levels
606       that were entered and exited one level down.  */
607    tree blocks;
608
609    /* The _TYPE node for this level, if parm_flag == 2.  */
610    tree this_class;
611
612    /* The binding level which this one is contained in (inherits from).  */
613    struct cp_binding_level *level_chain;
614
615    /* List of VAR_DECLS saved from a previous for statement.
616       These would be dead in ISO-conforming code, but might
617       be referenced in ARM-era code.  These are stored in a
618       TREE_LIST; the TREE_VALUE is the actual declaration.  */
619    tree dead_vars_from_for;
620
621    /* 1 for the level that holds the parameters of a function.
622       2 for the level that holds a class declaration.  */
623    unsigned parm_flag : 2;
624
625    /* 1 means make a BLOCK for this level regardless of all else.
626       2 for temporary binding contours created by the compiler.  */
627    unsigned keep : 2;
628
629    /* Nonzero if this level "doesn't exist" for tags.  */
630    unsigned tag_transparent : 1;
631
632    /* Nonzero if this level can safely have additional
633       cleanup-needing variables added to it.  */
634    unsigned more_cleanups_ok : 1;
635    unsigned have_cleanups : 1;
636
637    /* Nonzero if this scope is for storing the decls for template
638       parameters and generic decls; these decls will be discarded and
639       replaced with a TEMPLATE_DECL.  */
640    unsigned template_parms_p : 1;
641
642    /* Nonzero if this scope corresponds to the `<>' in a
643       `template <>' clause.  Whenever this flag is set,
644       TEMPLATE_PARMS_P will be set as well.  */
645    unsigned template_spec_p : 1;
646
647    /* This is set for a namespace binding level.  */
648    unsigned namespace_p : 1;
649
650    /* True if this level is that of a for-statement where we need to
651       worry about ambiguous (ARM or ISO) scope rules.  */
652    unsigned is_for_scope : 1;
653
654    /* True if this level corresponds to a TRY block.  Currently this
655       information is only available while building the tree structure.  */
656    unsigned is_try_scope : 1;
657
658    /* True if this level corresponds to a CATCH block.  Currently this
659       information is only available while building the tree structure.  */
660    unsigned is_catch_scope : 1;
661
662    /* Three bits left for this word.  */
663
664    /* Binding depth at which this level began.  */
665    unsigned binding_depth;
666  };
667
668#define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
669
670/* True if SCOPE designates the global scope binding contour.  */
671#define global_scope_p(SCOPE)  \
672  ((SCOPE) == NAMESPACE_LEVEL (global_namespace))
673
674/* The binding level currently in effect.  */
675
676#define current_binding_level			\
677  (*(cfun && cp_function_chain->bindings	\
678   ? &cp_function_chain->bindings		\
679   : &scope_chain->bindings))
680
681/* The binding level of the current class, if any.  */
682
683#define class_binding_level scope_chain->class_bindings
684
685/* A chain of binding_level structures awaiting reuse.  */
686
687static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
688
689/* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
690
691static int keep_next_level_flag;
692
693/* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
694   UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
695   time the VAR_DECL was declared, the type was incomplete.  */
696
697static GTY(()) tree incomplete_vars;
698
699#ifndef ENABLE_SCOPE_CHECKING
700#  define ENABLE_SCOPE_CHECKING 0
701#else
702#  define ENABLE_SCOPE_CHECKING 1
703#endif
704
705static unsigned binding_depth = 0;
706static int is_class_level = 0;
707
708static void
709indent (unsigned depth)
710{
711  unsigned i;
712
713  for (i = 0; i < depth * 2; i++)
714    putc (' ', stderr);
715}
716
717static tree pushdecl_with_scope	PARAMS ((tree, struct cp_binding_level *));
718
719static void
720push_binding_level (newlevel, tag_transparent, keep)
721     struct cp_binding_level *newlevel;
722     int tag_transparent, keep;
723{
724  /* Add this level to the front of the chain (stack) of levels that
725     are active.  */
726  memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
727  newlevel->level_chain = current_binding_level;
728  current_binding_level = newlevel;
729  newlevel->tag_transparent = tag_transparent;
730  newlevel->more_cleanups_ok = 1;
731
732  newlevel->keep = keep;
733  if (ENABLE_SCOPE_CHECKING)
734    {
735      newlevel->binding_depth = binding_depth;
736      indent (binding_depth);
737      verbatim ("push %s level %p line %d\n",
738                (is_class_level) ? "class" : "block",
739                (void *) newlevel, lineno);
740      is_class_level = 0;
741      binding_depth++;
742    }
743}
744
745/* Find the innermost enclosing class scope, and reset
746   CLASS_BINDING_LEVEL appropriately.  */
747
748static void
749find_class_binding_level ()
750{
751  struct cp_binding_level *level = current_binding_level;
752
753  while (level && level->parm_flag != 2)
754    level = level->level_chain;
755  if (level && level->parm_flag == 2)
756    class_binding_level = level;
757  else
758    class_binding_level = 0;
759}
760
761static void
762pop_binding_level ()
763{
764  if (NAMESPACE_LEVEL (global_namespace))
765    /* Cannot pop a level, if there are none left to pop.  */
766    my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
767  /* Pop the current level, and free the structure for reuse.  */
768  if (ENABLE_SCOPE_CHECKING)
769    {
770      indent (--binding_depth);
771      verbatim ("pop  %s level %p line %d\n",
772                (is_class_level) ? "class" : "block",
773                (void *) current_binding_level, lineno);
774      if (is_class_level != (current_binding_level == class_binding_level))
775        {
776          indent (binding_depth);
777          verbatim ("XXX is_class_level != (current_binding_level "
778                    "== class_binding_level)\n");
779        }
780      is_class_level = 0;
781    }
782  {
783    register struct cp_binding_level *level = current_binding_level;
784    current_binding_level = current_binding_level->level_chain;
785    level->level_chain = free_binding_level;
786    if (level->parm_flag != 2)
787      binding_table_free (level->type_decls);
788    else
789      level->type_decls = NULL;
790    my_friendly_assert (!ENABLE_SCOPE_CHECKING
791                        || level->binding_depth == binding_depth, 20030529);
792    free_binding_level = level;
793    find_class_binding_level ();
794  }
795}
796
797static void
798suspend_binding_level ()
799{
800  if (class_binding_level)
801    current_binding_level = class_binding_level;
802
803  if (NAMESPACE_LEVEL (global_namespace))
804    my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
805  /* Suspend the current level.  */
806  if (ENABLE_SCOPE_CHECKING)
807    {
808      indent (--binding_depth);
809      verbatim("suspend  %s level %p line %d\n",
810               (is_class_level) ? "class" : "block",
811               (void *) current_binding_level, lineno);
812      if (is_class_level != (current_binding_level == class_binding_level))
813        {
814          indent (binding_depth);
815          verbatim ("XXX is_class_level != (current_binding_level "
816                    "== class_binding_level)\n");
817        }
818      is_class_level = 0;
819    }
820  current_binding_level = current_binding_level->level_chain;
821  find_class_binding_level ();
822}
823
824static void
825resume_binding_level (b)
826     struct cp_binding_level *b;
827{
828  /* Resuming binding levels is meant only for namespaces,
829     and those cannot nest into classes.  */
830  my_friendly_assert(!class_binding_level, 386);
831  /* Also, resuming a non-directly nested namespace is a no-no.  */
832  my_friendly_assert(b->level_chain == current_binding_level, 386);
833  current_binding_level = b;
834  if (ENABLE_SCOPE_CHECKING)
835    {
836      b->binding_depth = binding_depth;
837      indent (binding_depth);
838      verbatim ("resume %s level %p line %d\n",
839                (is_class_level) ? "class" : "block", (void *) b, lineno);
840      is_class_level = 0;
841      binding_depth++;
842    }
843}
844
845/* Create a new `struct cp_binding_level'.  */
846
847static
848struct cp_binding_level *
849make_binding_level ()
850{
851  /* NOSTRICT */
852  return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
853}
854
855/* Nonzero if we are currently in the global binding level.  */
856
857int
858global_bindings_p ()
859{
860  return global_scope_p (current_binding_level);
861}
862
863/* Return the innermost binding level that is not for a class scope.  */
864
865static struct cp_binding_level *
866innermost_nonclass_level ()
867{
868  struct cp_binding_level *b;
869
870  b = current_binding_level;
871  while (b->parm_flag == 2)
872    b = b->level_chain;
873
874  return b;
875}
876
877/* Nonzero if we are currently in a toplevel binding level.  This
878   means either the global binding level or a namespace in a toplevel
879   binding level.  Since there are no non-toplevel namespace levels,
880   this really means any namespace or template parameter level.  We
881   also include a class whose context is toplevel.  */
882
883int
884toplevel_bindings_p ()
885{
886  struct cp_binding_level *b = innermost_nonclass_level ();
887
888  return b->namespace_p || b->template_parms_p;
889}
890
891/* Nonzero if this is a namespace scope, or if we are defining a class
892   which is itself at namespace scope, or whose enclosing class is
893   such a class, etc.  */
894
895int
896namespace_bindings_p ()
897{
898  struct cp_binding_level *b = innermost_nonclass_level ();
899
900  return b->namespace_p;
901}
902
903/* If KEEP is nonzero, make a BLOCK node for the next binding level,
904   unconditionally.  Otherwise, use the normal logic to decide whether
905   or not to create a BLOCK.  */
906
907void
908keep_next_level (keep)
909     int keep;
910{
911  keep_next_level_flag = keep;
912}
913
914/* Nonzero if the current level needs to have a BLOCK made.  */
915
916int
917kept_level_p ()
918{
919  return (current_binding_level->blocks != NULL_TREE
920	  || current_binding_level->keep
921	  || current_binding_level->names != NULL_TREE
922	  || (current_binding_level->type_decls != NULL
923	      && !current_binding_level->tag_transparent));
924}
925
926/* Returns the kind of the innermost scope.  */
927
928bool
929innermost_scope_is_class_p ()
930{
931  return current_binding_level->parm_flag == 2;
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 != error_mark_node
1080      && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
1081      && current_binding_level->more_cleanups_ok == 0)
1082    {
1083      keep_next_level (2);
1084      pushlevel (1);
1085      clear_last_expr ();
1086      add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
1087    }
1088}
1089
1090/* Enter a new scope.  The KIND indicates what kind of scope is being
1091   created.  */
1092
1093void
1094begin_scope (sk)
1095     scope_kind sk;
1096{
1097  pushlevel (0);
1098
1099  switch (sk)
1100    {
1101    case sk_template_spec:
1102      current_binding_level->template_spec_p = 1;
1103      /* Fall through.  */
1104
1105    case sk_template_parms:
1106      current_binding_level->template_parms_p = 1;
1107      break;
1108
1109    default:
1110      abort ();
1111    }
1112}
1113
1114/* Exit the current scope.  */
1115
1116void
1117finish_scope ()
1118{
1119  poplevel (0, 0, 0);
1120}
1121
1122void
1123note_level_for_for ()
1124{
1125  current_binding_level->is_for_scope = 1;
1126}
1127
1128/* Record that the current binding level represents a try block.  */
1129
1130void
1131note_level_for_try ()
1132{
1133  current_binding_level->is_try_scope = 1;
1134}
1135
1136/* Record that the current binding level represents a catch block.  */
1137
1138void
1139note_level_for_catch ()
1140{
1141  current_binding_level->is_catch_scope = 1;
1142}
1143
1144/* For a binding between a name and an entity at a block scope,
1145   this is the `struct cp_binding_level' for the block.  */
1146#define BINDING_LEVEL(NODE) ((NODE)->scope.level)
1147
1148/* A free list of "cxx_binding"s, connected by their PREVIOUS.  */
1149
1150static GTY((deletable (""))) cxx_binding *free_bindings;
1151
1152/* Make DECL the innermost binding for ID.  The LEVEL is the binding
1153   level at which this declaration is being bound.  */
1154
1155static void
1156push_binding (id, decl, level)
1157     tree id;
1158     tree decl;
1159     struct cp_binding_level* level;
1160{
1161  cxx_binding *binding;
1162
1163  if (free_bindings)
1164    {
1165      binding = free_bindings;
1166      free_bindings = binding->previous;
1167    }
1168  else
1169    binding = cxx_binding_make ();
1170
1171  /* Now, fill in the binding information.  */
1172  BINDING_VALUE (binding) = decl;
1173  BINDING_TYPE (binding) = NULL_TREE;
1174  BINDING_LEVEL (binding) = level;
1175  INHERITED_VALUE_BINDING_P (binding) = 0;
1176  LOCAL_BINDING_P (binding) = (level != class_binding_level);
1177  BINDING_HAS_LEVEL_P (binding) = 1;
1178
1179  /* And put it on the front of the list of bindings for ID.  */
1180  binding->previous = IDENTIFIER_BINDING (id);
1181  IDENTIFIER_BINDING (id) = binding;
1182}
1183
1184/* ID is already bound in the current scope.  But, DECL is an
1185   additional binding for ID in the same scope.  This is the `struct
1186   stat' hack whereby a non-typedef class-name or enum-name can be
1187   bound at the same level as some other kind of entity.  It's the
1188   responsibility of the caller to check that inserting this name is
1189   valid here.  Returns nonzero if the new binding was successful.  */
1190static int
1191add_binding (id, decl)
1192     tree id;
1193     tree decl;
1194{
1195  cxx_binding *binding = IDENTIFIER_BINDING (id);
1196  int ok = 1;
1197
1198  timevar_push (TV_NAME_LOOKUP);
1199  if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
1200    /* The new name is the type name.  */
1201    BINDING_TYPE (binding) = decl;
1202  else if (!BINDING_VALUE (binding))
1203    /* This situation arises when push_class_level_binding moves an
1204       inherited type-binding out of the way to make room for a new
1205       value binding.  */
1206    BINDING_VALUE (binding) = decl;
1207  else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1208	   && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
1209    {
1210      /* The old binding was a type name.  It was placed in
1211	 BINDING_VALUE because it was thought, at the point it was
1212	 declared, to be the only entity with such a name.  Move the
1213	 type name into the type slot; it is now hidden by the new
1214	 binding.  */
1215      BINDING_TYPE (binding) = BINDING_VALUE (binding);
1216      BINDING_VALUE (binding) = decl;
1217      INHERITED_VALUE_BINDING_P (binding) = 0;
1218    }
1219  else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
1220	   && TREE_CODE (decl) == TYPE_DECL
1221	   && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
1222	   && (same_type_p (TREE_TYPE (decl),
1223			    TREE_TYPE (BINDING_VALUE (binding)))
1224	       /* If either type involves template parameters, we must
1225		  wait until instantiation.  */
1226	       || uses_template_parms (TREE_TYPE (decl))
1227	       || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
1228    /* We have two typedef-names, both naming the same type to have
1229       the same name.  This is OK because of:
1230
1231         [dcl.typedef]
1232
1233	 In a given scope, a typedef specifier can be used to redefine
1234	 the name of any type declared in that scope to refer to the
1235	 type to which it already refers.  */
1236    ok = 0;
1237  /* There can be two block-scope declarations of the same variable,
1238     so long as they are `extern' declarations.  However, there cannot
1239     be two declarations of the same static data member:
1240
1241       [class.mem]
1242
1243       A member shall not be declared twice in the
1244       member-specification.  */
1245  else if (TREE_CODE (decl) == VAR_DECL
1246	   && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
1247	   && DECL_EXTERNAL (decl)
1248	   && DECL_EXTERNAL (BINDING_VALUE (binding))
1249	   && !DECL_CLASS_SCOPE_P (decl))
1250    {
1251      duplicate_decls (decl, BINDING_VALUE (binding));
1252      ok = 0;
1253    }
1254  else
1255    {
1256      error ("declaration of `%#D'", decl);
1257      cp_error_at ("conflicts with previous declaration `%#D'",
1258		   BINDING_VALUE (binding));
1259      ok = 0;
1260    }
1261
1262  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
1263}
1264
1265/* Add DECL to the list of things declared in B.  */
1266
1267static void
1268add_decl_to_level (decl, b)
1269     tree decl;
1270     struct cp_binding_level *b;
1271{
1272  if (TREE_CODE (decl) == NAMESPACE_DECL
1273      && !DECL_NAMESPACE_ALIAS (decl))
1274    {
1275      TREE_CHAIN (decl) = b->namespaces;
1276      b->namespaces = decl;
1277    }
1278  else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1279    {
1280      TREE_CHAIN (decl) = b->vtables;
1281      b->vtables = decl;
1282    }
1283  else
1284    {
1285      /* We build up the list in reverse order, and reverse it later if
1286         necessary.  */
1287      TREE_CHAIN (decl) = b->names;
1288      b->names = decl;
1289      b->names_size++;
1290
1291      /* If appropriate, add decl to separate list of statics.  We
1292	 include extern variables because they might turn out to be
1293	 static later.  It's OK for this list to contain a few false
1294	 positives. */
1295      if (b->namespace_p)
1296	if ((TREE_CODE (decl) == VAR_DECL
1297	     && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
1298	    || (TREE_CODE (decl) == FUNCTION_DECL
1299		&& (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1300	  VARRAY_PUSH_TREE (b->static_decls, decl);
1301    }
1302}
1303
1304/* Bind DECL to ID in the current_binding_level, assumed to be a local
1305   binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1306   doesn't really belong to this binding level, that it got here
1307   through a using-declaration.  */
1308
1309void
1310push_local_binding (id, decl, flags)
1311     tree id;
1312     tree decl;
1313     int flags;
1314{
1315  struct cp_binding_level *b;
1316
1317  /* Skip over any local classes.  This makes sense if we call
1318     push_local_binding with a friend decl of a local class.  */
1319  b = current_binding_level;
1320  while (b->parm_flag == 2)
1321    b = b->level_chain;
1322
1323  if (lookup_name_current_level (id))
1324    {
1325      /* Supplement the existing binding.  */
1326      if (!add_binding (id, decl))
1327	/* It didn't work.  Something else must be bound at this
1328	   level.  Do not add DECL to the list of things to pop
1329	   later.  */
1330	return;
1331    }
1332  else
1333    /* Create a new binding.  */
1334    push_binding (id, decl, b);
1335
1336  if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1337    /* We must put the OVERLOAD into a TREE_LIST since the
1338       TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1339       decls that got here through a using-declaration.  */
1340    decl = build_tree_list (NULL_TREE, decl);
1341
1342  /* And put DECL on the list of things declared by the current
1343     binding level.  */
1344  add_decl_to_level (decl, b);
1345}
1346
1347/* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1348   binding was successful.  */
1349
1350int
1351push_class_binding (id, decl)
1352     tree id;
1353     tree decl;
1354{
1355  int result = 1;
1356  cxx_binding *binding = IDENTIFIER_BINDING (id);
1357  tree context;
1358
1359  timevar_push (TV_NAME_LOOKUP);
1360  /* Note that we declared this value so that we can issue an error if
1361     this is an invalid redeclaration of a name already used for some
1362     other purpose.  */
1363  note_name_declared_in_class (id, decl);
1364
1365  if (binding && BINDING_LEVEL (binding) == class_binding_level)
1366    /* Supplement the existing binding.  */
1367    result = add_binding (id, decl);
1368  else
1369    /* Create a new binding.  */
1370    push_binding (id, decl, class_binding_level);
1371
1372  /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1373     class-level declaration.  Note that we do not use DECL here
1374     because of the possibility of the `struct stat' hack; if DECL is
1375     a class-name or enum-name we might prefer a field-name, or some
1376     such.  */
1377  IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1378
1379  /* If this is a binding from a base class, mark it as such.  */
1380  binding = IDENTIFIER_BINDING (id);
1381  if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1382    {
1383      /* Any implicit typename must be from a base-class.  The
1384	 context for an implicit typename declaration is always
1385	 the derived class in which the lookup was done, so the checks
1386	 based on the context of DECL below will not trigger.  */
1387      if (IMPLICIT_TYPENAME_TYPE_DECL_P (decl))
1388	INHERITED_VALUE_BINDING_P (binding) = 1;
1389      else
1390	{
1391	  if (TREE_CODE (decl) == OVERLOAD)
1392	    context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1393	  else
1394	    {
1395	      my_friendly_assert (DECL_P (decl), 0);
1396	      context = context_for_name_lookup (decl);
1397	    }
1398
1399	  if (is_properly_derived_from (current_class_type, context))
1400	    INHERITED_VALUE_BINDING_P (binding) = 1;
1401	  else
1402	    INHERITED_VALUE_BINDING_P (binding) = 0;
1403	}
1404    }
1405  else if (BINDING_VALUE (binding) == decl)
1406    /* We only encounter a TREE_LIST when push_class_decls detects an
1407       ambiguity.  Such an ambiguity can be overridden by a definition
1408       in this class.  */
1409    INHERITED_VALUE_BINDING_P (binding) = 1;
1410
1411  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1412}
1413
1414/* Remove the binding for DECL which should be the innermost binding
1415   for ID.  */
1416
1417static void
1418pop_binding (id, decl)
1419     tree id;
1420     tree decl;
1421{
1422  cxx_binding *binding;
1423
1424  if (id == NULL_TREE)
1425    /* It's easiest to write the loops that call this function without
1426       checking whether or not the entities involved have names.  We
1427       get here for such an entity.  */
1428    return;
1429
1430  /* Get the innermost binding for ID.  */
1431  binding = IDENTIFIER_BINDING (id);
1432
1433  /* The name should be bound.  */
1434  my_friendly_assert (binding != NULL, 0);
1435
1436  /* The DECL will be either the ordinary binding or the type
1437     binding for this identifier.  Remove that binding.  */
1438  if (BINDING_VALUE (binding) == decl)
1439    BINDING_VALUE (binding) = NULL_TREE;
1440  else if (BINDING_TYPE (binding) == decl)
1441    BINDING_TYPE (binding) = NULL_TREE;
1442  else
1443    abort ();
1444
1445  if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1446    {
1447      /* We're completely done with the innermost binding for this
1448	 identifier.  Unhook it from the list of bindings.  */
1449      IDENTIFIER_BINDING (id) = binding->previous;
1450
1451      /* Add it to the free list.  */
1452      binding->previous = free_bindings;
1453      free_bindings = binding;
1454
1455      /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1456	 it.  */
1457      BINDING_LEVEL (binding) = NULL;
1458    }
1459}
1460
1461/* When a label goes out of scope, check to see if that label was used
1462   in a valid manner, and issue any appropriate warnings or errors.  */
1463
1464static void
1465pop_label (label, old_value)
1466     tree label;
1467     tree old_value;
1468{
1469  if (!processing_template_decl && doing_semantic_analysis_p ())
1470    {
1471      if (DECL_INITIAL (label) == NULL_TREE)
1472	{
1473	  cp_error_at ("label `%D' used but not defined", label);
1474	  /* Avoid crashing later.  */
1475	  define_label (input_filename, 1, DECL_NAME (label));
1476	}
1477      else if (warn_unused_label && !TREE_USED (label))
1478	cp_warning_at ("label `%D' defined but not used", label);
1479    }
1480
1481  SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1482}
1483
1484/* At the end of a function, all labels declared within the function
1485   go out of scope.  BLOCK is the top-level block for the
1486   function.  */
1487
1488static void
1489pop_labels (block)
1490     tree block;
1491{
1492  struct named_label_list *link;
1493
1494  /* Clear out the definitions of all label names, since their scopes
1495     end here.  */
1496  for (link = named_labels; link; link = link->next)
1497    {
1498      pop_label (link->label_decl, link->old_value);
1499      /* Put the labels into the "variables" of the top-level block,
1500	 so debugger can see them.  */
1501      TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1502      BLOCK_VARS (block) = link->label_decl;
1503    }
1504
1505  named_labels = NULL;
1506}
1507
1508/* Exit a binding level.
1509   Pop the level off, and restore the state of the identifier-decl mappings
1510   that were in effect when this level was entered.
1511
1512   If KEEP == 1, this level had explicit declarations, so
1513   and create a "block" (a BLOCK node) for the level
1514   to record its declarations and subblocks for symbol table output.
1515
1516   If FUNCTIONBODY is nonzero, this level is the body of a function,
1517   so create a block as if KEEP were set and also clear out all
1518   label names.
1519
1520   If REVERSE is nonzero, reverse the order of decls before putting
1521   them into the BLOCK.  */
1522
1523tree
1524poplevel (keep, reverse, functionbody)
1525     int keep;
1526     int reverse;
1527     int functionbody;
1528{
1529  register tree link;
1530  /* The chain of decls was accumulated in reverse order.
1531     Put it into forward order, just for cleanliness.  */
1532  tree decls;
1533  int tmp = functionbody;
1534  int real_functionbody;
1535  tree subblocks;
1536  tree block = NULL_TREE;
1537  tree decl;
1538  int leaving_for_scope;
1539
1540  timevar_push (TV_NAME_LOOKUP);
1541
1542  if (cfun && !doing_semantic_analysis_p ())
1543    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1544
1545  my_friendly_assert (current_binding_level->parm_flag != 2,
1546		      19990916);
1547
1548  real_functionbody = (current_binding_level->keep == 2
1549		       ? ((functionbody = 0), tmp) : functionbody);
1550  subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1551
1552  my_friendly_assert (!current_binding_level->class_shadowed,
1553		      19990414);
1554
1555  /* We used to use KEEP == 2 to indicate that the new block should go
1556     at the beginning of the list of blocks at this binding level,
1557     rather than the end.  This hack is no longer used.  */
1558  my_friendly_assert (keep == 0 || keep == 1, 0);
1559
1560  if (current_binding_level->keep == 1)
1561    keep = 1;
1562
1563  /* Any uses of undefined labels, and any defined labels, now operate
1564     under constraints of next binding contour.  */
1565  if (cfun && !functionbody)
1566    {
1567      struct cp_binding_level *level_chain;
1568      level_chain = current_binding_level->level_chain;
1569      if (level_chain)
1570	{
1571	  struct named_label_use_list *uses;
1572	  struct named_label_list *labels;
1573	  for (labels = named_labels; labels; labels = labels->next)
1574	    if (labels->binding_level == current_binding_level)
1575	      {
1576		tree decl;
1577		if (current_binding_level->is_try_scope)
1578		  labels->in_try_scope = 1;
1579		if (current_binding_level->is_catch_scope)
1580		  labels->in_catch_scope = 1;
1581		for (decl = labels->names_in_scope; decl;
1582		     decl = TREE_CHAIN (decl))
1583		  if (decl_jump_unsafe (decl))
1584		    labels->bad_decls = tree_cons (NULL_TREE, decl,
1585						   labels->bad_decls);
1586		labels->binding_level = level_chain;
1587		labels->names_in_scope = level_chain->names;
1588	      }
1589
1590	  for (uses = named_label_uses; uses; uses = uses->next)
1591	    if (uses->binding_level == current_binding_level)
1592	      {
1593		uses->binding_level = level_chain;
1594		uses->names_in_scope = level_chain->names;
1595	      }
1596	}
1597    }
1598
1599  /* Get the decls in the order they were written.
1600     Usually current_binding_level->names is in reverse order.
1601     But parameter decls were previously put in forward order.  */
1602
1603  if (reverse)
1604    current_binding_level->names
1605      = decls = nreverse (current_binding_level->names);
1606  else
1607    decls = current_binding_level->names;
1608
1609  /* Output any nested inline functions within this block
1610     if they weren't already output.  */
1611  for (decl = decls; decl; decl = TREE_CHAIN (decl))
1612    if (TREE_CODE (decl) == FUNCTION_DECL
1613	&& ! TREE_ASM_WRITTEN (decl)
1614	&& DECL_INITIAL (decl) != NULL_TREE
1615	&& TREE_ADDRESSABLE (decl)
1616	&& decl_function_context (decl) == current_function_decl)
1617      {
1618	/* If this decl was copied from a file-scope decl
1619	   on account of a block-scope extern decl,
1620	   propagate TREE_ADDRESSABLE to the file-scope decl.  */
1621	if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1622	  TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1623	else
1624	  {
1625	    push_function_context ();
1626	    output_inline_function (decl);
1627	    pop_function_context ();
1628	  }
1629      }
1630
1631  /* When not in function-at-a-time mode, expand_end_bindings will
1632     warn about unused variables.  But, in function-at-a-time mode
1633     expand_end_bindings is not passed the list of variables in the
1634     current scope, and therefore no warning is emitted.  So, we
1635     explicitly warn here.  */
1636  if (!processing_template_decl)
1637    warn_about_unused_variables (getdecls ());
1638
1639  /* If there were any declarations or structure tags in that level,
1640     or if this level is a function body,
1641     create a BLOCK to record them for the life of this function.  */
1642  block = NULL_TREE;
1643  if (keep == 1 || functionbody)
1644    block = make_node (BLOCK);
1645  if (block != NULL_TREE)
1646    {
1647      BLOCK_VARS (block) = decls;
1648      BLOCK_SUBBLOCKS (block) = subblocks;
1649    }
1650
1651  /* In each subblock, record that this is its superior.  */
1652  if (keep >= 0)
1653    for (link = subblocks; link; link = TREE_CHAIN (link))
1654      BLOCK_SUPERCONTEXT (link) = block;
1655
1656  /* We still support the old for-scope rules, whereby the variables
1657     in a for-init statement were in scope after the for-statement
1658     ended.  We only use the new rules in flag_new_for_scope is
1659     nonzero.  */
1660  leaving_for_scope
1661    = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1662
1663  /* Remove declarations for all the DECLs in this level.  */
1664  for (link = decls; link; link = TREE_CHAIN (link))
1665    {
1666      if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1667          && DECL_NAME (link))
1668	{
1669	  cxx_binding *outer_binding
1670	    = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1671	  tree ns_binding;
1672
1673	  if (!outer_binding)
1674	    ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1675	  else
1676	    ns_binding = NULL_TREE;
1677
1678	  if (outer_binding
1679	      && (BINDING_LEVEL (outer_binding)
1680		  == current_binding_level->level_chain))
1681	    /* We have something like:
1682
1683	         int i;
1684	         for (int i; ;);
1685
1686	       and we are leaving the `for' scope.  There's no reason to
1687	       keep the binding of the inner `i' in this case.  */
1688	    pop_binding (DECL_NAME (link), link);
1689	  else if ((outer_binding
1690		    && (TREE_CODE (BINDING_VALUE (outer_binding))
1691			== TYPE_DECL))
1692		   || (ns_binding
1693		       && TREE_CODE (ns_binding) == TYPE_DECL))
1694	    /* Here, we have something like:
1695
1696		 typedef int I;
1697
1698		 void f () {
1699		   for (int I; ;);
1700		 }
1701
1702	       We must pop the for-scope binding so we know what's a
1703	       type and what isn't.  */
1704	    pop_binding (DECL_NAME (link), link);
1705	  else
1706	    {
1707	      /* Mark this VAR_DECL as dead so that we can tell we left it
1708		 there only for backward compatibility.  */
1709	      DECL_DEAD_FOR_LOCAL (link) = 1;
1710
1711	      /* Keep track of what should of have happenned when we
1712		 popped the binding.  */
1713	      if (outer_binding && BINDING_VALUE (outer_binding))
1714		DECL_SHADOWED_FOR_VAR (link)
1715		  = BINDING_VALUE (outer_binding);
1716
1717	      /* Add it to the list of dead variables in the next
1718		 outermost binding to that we can remove these when we
1719		 leave that binding.  */
1720	      current_binding_level->level_chain->dead_vars_from_for
1721		= tree_cons (NULL_TREE, link,
1722			     current_binding_level->level_chain->
1723			     dead_vars_from_for);
1724
1725	      /* Although we don't pop the cxx_binding, we do clear
1726		 its BINDING_LEVEL since the level is going away now.  */
1727	      BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1728		= 0;
1729	    }
1730	}
1731      else
1732	{
1733	  /* Remove the binding.  */
1734	  decl = link;
1735	  if (TREE_CODE (decl) == TREE_LIST)
1736	    decl = TREE_VALUE (decl);
1737	  if (DECL_P (decl))
1738	    pop_binding (DECL_NAME (decl), decl);
1739	  else if (TREE_CODE (decl) == OVERLOAD)
1740	    pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1741	  else
1742	    abort ();
1743	}
1744    }
1745
1746  /* Remove declarations for any `for' variables from inner scopes
1747     that we kept around.  */
1748  for (link = current_binding_level->dead_vars_from_for;
1749       link; link = TREE_CHAIN (link))
1750    pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1751
1752  /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1753  for (link = current_binding_level->type_shadowed;
1754       link; link = TREE_CHAIN (link))
1755    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1756
1757  /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1758  for (link = current_binding_level->shadowed_labels;
1759       link;
1760       link = TREE_CHAIN (link))
1761    pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1762
1763  /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1764     list if a `using' declaration put them there.  The debugging
1765     back-ends won't understand OVERLOAD, so we remove them here.
1766     Because the BLOCK_VARS are (temporarily) shared with
1767     CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1768     popped all the bindings.  */
1769  if (block)
1770    {
1771      tree* d;
1772
1773      for (d = &BLOCK_VARS (block); *d; )
1774	{
1775	  if (TREE_CODE (*d) == TREE_LIST)
1776	    *d = TREE_CHAIN (*d);
1777	  else
1778	    d = &TREE_CHAIN (*d);
1779	}
1780    }
1781
1782  /* If the level being exited is the top level of a function,
1783     check over all the labels.  */
1784  if (functionbody)
1785    {
1786      /* Since this is the top level block of a function, the vars are
1787	 the function's parameters.  Don't leave them in the BLOCK
1788	 because they are found in the FUNCTION_DECL instead.  */
1789      BLOCK_VARS (block) = 0;
1790      pop_labels (block);
1791    }
1792
1793  tmp = current_binding_level->keep;
1794
1795  pop_binding_level ();
1796  if (functionbody)
1797    DECL_INITIAL (current_function_decl) = block;
1798  else if (block)
1799    current_binding_level->blocks
1800      = chainon (current_binding_level->blocks, block);
1801
1802  /* If we did not make a block for the level just exited,
1803     any blocks made for inner levels
1804     (since they cannot be recorded as subblocks in that level)
1805     must be carried forward so they will later become subblocks
1806     of something else.  */
1807  else if (subblocks)
1808    current_binding_level->blocks
1809      = chainon (current_binding_level->blocks, subblocks);
1810
1811  /* Each and every BLOCK node created here in `poplevel' is important
1812     (e.g. for proper debugging information) so if we created one
1813     earlier, mark it as "used".  */
1814  if (block)
1815    TREE_USED (block) = 1;
1816
1817  /* Take care of compiler's internal binding structures.  */
1818  if (tmp == 2)
1819    {
1820      tree scope_stmts;
1821
1822      scope_stmts
1823	= add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1824      if (block)
1825	{
1826	  SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1827	  SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1828	}
1829
1830      block = poplevel (keep, reverse, functionbody);
1831    }
1832
1833  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1834}
1835
1836/* Delete the node BLOCK from the current binding level.
1837   This is used for the block inside a stmt expr ({...})
1838   so that the block can be reinserted where appropriate.  */
1839
1840void
1841delete_block (block)
1842     tree block;
1843{
1844  tree t;
1845  if (current_binding_level->blocks == block)
1846    current_binding_level->blocks = TREE_CHAIN (block);
1847  for (t = current_binding_level->blocks; t;)
1848    {
1849      if (TREE_CHAIN (t) == block)
1850	TREE_CHAIN (t) = TREE_CHAIN (block);
1851      else
1852	t = TREE_CHAIN (t);
1853    }
1854  TREE_CHAIN (block) = NULL_TREE;
1855  /* Clear TREE_USED which is always set by poplevel.
1856     The flag is set again if insert_block is called.  */
1857  TREE_USED (block) = 0;
1858}
1859
1860/* Insert BLOCK at the end of the list of subblocks of the
1861   current binding level.  This is used when a BIND_EXPR is expanded,
1862   to handle the BLOCK node inside the BIND_EXPR.  */
1863
1864void
1865insert_block (block)
1866     tree block;
1867{
1868  TREE_USED (block) = 1;
1869  current_binding_level->blocks
1870    = chainon (current_binding_level->blocks, block);
1871}
1872
1873/* Set the BLOCK node for the innermost scope
1874   (the one we are currently in).  */
1875
1876void
1877set_block (block)
1878    tree block ATTRIBUTE_UNUSED;
1879{
1880  /* The RTL expansion machinery requires us to provide this callback,
1881     but it is not applicable in function-at-a-time mode.  */
1882  my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1883}
1884
1885/* Do a pushlevel for class declarations.  */
1886
1887void
1888pushlevel_class ()
1889{
1890  register struct cp_binding_level *newlevel;
1891
1892  /* Reuse or create a struct for this binding level.  */
1893  if (!ENABLE_SCOPE_CHECKING && free_binding_level)
1894    {
1895      newlevel = free_binding_level;
1896      free_binding_level = free_binding_level->level_chain;
1897    }
1898  else
1899    newlevel = make_binding_level ();
1900
1901  if (ENABLE_SCOPE_CHECKING)
1902    is_class_level = 1;
1903
1904  push_binding_level (newlevel, 0, 0);
1905
1906  class_binding_level = current_binding_level;
1907  class_binding_level->parm_flag = 2;
1908  class_binding_level->this_class = current_class_type;
1909}
1910
1911/* ...and a poplevel for class declarations.  */
1912
1913void
1914poplevel_class ()
1915{
1916  register struct cp_binding_level *level = class_binding_level;
1917  tree shadowed;
1918
1919  timevar_push (TV_NAME_LOOKUP);
1920
1921  my_friendly_assert (level != 0, 354);
1922
1923  /* If we're leaving a toplevel class, don't bother to do the setting
1924     of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1925     shouldn't even be used when current_class_type isn't set, and second,
1926     if we don't touch it here, we're able to use the cache effect if the
1927     next time we're entering a class scope, it is the same class.  */
1928  if (current_class_depth != 1)
1929    {
1930      struct cp_binding_level* b;
1931
1932      /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1933      for (shadowed = level->class_shadowed;
1934	   shadowed;
1935	   shadowed = TREE_CHAIN (shadowed))
1936	IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1937
1938      /* Find the next enclosing class, and recreate
1939	 IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1940      b = level->level_chain;
1941      while (b && b->parm_flag != 2)
1942	b = b->level_chain;
1943
1944      if (b)
1945	for (shadowed = b->class_shadowed;
1946	     shadowed;
1947	     shadowed = TREE_CHAIN (shadowed))
1948	  {
1949	    cxx_binding *binding;
1950
1951	    binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1952	    while (binding && BINDING_LEVEL (binding) != b)
1953	      binding = binding->previous;
1954
1955	    if (binding)
1956	      IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1957		= BINDING_VALUE (binding);
1958	  }
1959    }
1960  else
1961    /* Remember to save what IDENTIFIER's were bound in this scope so we
1962       can recover from cache misses.  */
1963    {
1964      previous_class_type = current_class_type;
1965      previous_class_values = class_binding_level->class_shadowed;
1966    }
1967  for (shadowed = level->type_shadowed;
1968       shadowed;
1969       shadowed = TREE_CHAIN (shadowed))
1970    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1971
1972  /* Remove the bindings for all of the class-level declarations.  */
1973  for (shadowed = level->class_shadowed;
1974       shadowed;
1975       shadowed = TREE_CHAIN (shadowed))
1976    pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1977
1978  /* Now, pop out of the binding level which we created up in the
1979     `pushlevel_class' routine.  */
1980  if (ENABLE_SCOPE_CHECKING)
1981    is_class_level = 1;
1982
1983  pop_binding_level ();
1984
1985  timevar_pop (TV_NAME_LOOKUP);
1986}
1987
1988/* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1989   for any names in enclosing classes.  */
1990
1991void
1992clear_identifier_class_values ()
1993{
1994  tree t;
1995
1996  if (!class_binding_level)
1997    return;
1998
1999  for (t = class_binding_level->class_shadowed;
2000       t;
2001       t = TREE_CHAIN (t))
2002    IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
2003}
2004
2005/* Returns nonzero if T is a virtual function table.  */
2006
2007int
2008vtable_decl_p (t, data)
2009     tree t;
2010     void *data ATTRIBUTE_UNUSED;
2011{
2012  return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
2013}
2014
2015/* Returns nonzero if T is a TYPE_DECL for a type with virtual
2016   functions.  */
2017
2018int
2019vtype_decl_p (t, data)
2020     tree t;
2021     void *data ATTRIBUTE_UNUSED;
2022{
2023  return (TREE_CODE (t) == TYPE_DECL
2024	  && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
2025	  && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
2026}
2027
2028/* Return the declarations that are members of the namespace NS.  */
2029
2030tree
2031cp_namespace_decls (ns)
2032     tree ns;
2033{
2034  return NAMESPACE_LEVEL (ns)->names;
2035}
2036
2037struct walk_globals_data {
2038  walk_globals_pred p;
2039  walk_globals_fn f;
2040  void *data;
2041};
2042
2043/* Walk the vtable declarations in NAMESPACE.  Whenever one is found
2044   for which P returns nonzero, call F with its address.  If any call
2045   to F returns a nonzero value, return a nonzero value.  */
2046
2047static int
2048walk_vtables_r (namespace, data)
2049     tree namespace;
2050     void *data;
2051{
2052  struct walk_globals_data* wgd = (struct walk_globals_data *) data;
2053  walk_globals_fn f = wgd->f;
2054  void *d = wgd->data;
2055  tree decl = NAMESPACE_LEVEL (namespace)->vtables;
2056  int result = 0;
2057
2058  for (; decl ; decl = TREE_CHAIN (decl))
2059    result |= (*f) (&decl, d);
2060
2061  return result;
2062}
2063
2064/* Walk the vtable declarations.  Whenever one is found for which P
2065   returns nonzero, call F with its address.  If any call to F
2066   returns a nonzero value, return a nonzero value.  */
2067int
2068walk_vtables (p, f, data)
2069     walk_globals_pred p;
2070     walk_globals_fn f;
2071     void *data;
2072{
2073  struct walk_globals_data wgd;
2074  wgd.p = p;
2075  wgd.f = f;
2076  wgd.data = data;
2077
2078  return walk_namespaces (walk_vtables_r, &wgd);
2079}
2080
2081/* Walk all the namespaces contained NAMESPACE, including NAMESPACE
2082   itself, calling F for each.  The DATA is passed to F as well.  */
2083
2084static int
2085walk_namespaces_r (namespace, f, data)
2086     tree namespace;
2087     walk_namespaces_fn f;
2088     void *data;
2089{
2090  int result = 0;
2091  tree current = NAMESPACE_LEVEL (namespace)->namespaces;
2092
2093  result |= (*f) (namespace, data);
2094
2095  for (; current; current = TREE_CHAIN (current))
2096    result |= walk_namespaces_r (current, f, data);
2097
2098  return result;
2099}
2100
2101/* Walk all the namespaces, calling F for each.  The DATA is passed to
2102   F as well.  */
2103
2104int
2105walk_namespaces (f, data)
2106     walk_namespaces_fn f;
2107     void *data;
2108{
2109  return walk_namespaces_r (global_namespace, f, data);
2110}
2111
2112/* Walk the global declarations in NAMESPACE.  Whenever one is found
2113   for which P returns nonzero, call F with its address.  If any call
2114   to F returns a nonzero value, return a nonzero value.  */
2115
2116static int
2117walk_globals_r (namespace, data)
2118     tree namespace;
2119     void *data;
2120{
2121  struct walk_globals_data* wgd = (struct walk_globals_data *) data;
2122  walk_globals_pred p = wgd->p;
2123  walk_globals_fn f = wgd->f;
2124  void *d = wgd->data;
2125  tree *t;
2126  int result = 0;
2127
2128  t = &NAMESPACE_LEVEL (namespace)->names;
2129
2130  while (*t)
2131    {
2132      tree glbl = *t;
2133
2134      if ((*p) (glbl, d))
2135	result |= (*f) (t, d);
2136
2137      /* If F changed *T, then *T still points at the next item to
2138	 examine.  */
2139      if (*t == glbl)
2140	t = &TREE_CHAIN (*t);
2141    }
2142
2143  return result;
2144}
2145
2146/* Walk the global declarations.  Whenever one is found for which P
2147   returns nonzero, call F with its address.  If any call to F
2148   returns a nonzero value, return a nonzero value.  */
2149
2150int
2151walk_globals (p, f, data)
2152     walk_globals_pred p;
2153     walk_globals_fn f;
2154     void *data;
2155{
2156  struct walk_globals_data wgd;
2157  wgd.p = p;
2158  wgd.f = f;
2159  wgd.data = data;
2160
2161  return walk_namespaces (walk_globals_r, &wgd);
2162}
2163
2164/* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
2165   DATA is non-NULL, this is the last time we will call
2166   wrapup_global_declarations for this NAMESPACE.  */
2167
2168int
2169wrapup_globals_for_namespace (namespace, data)
2170     tree namespace;
2171     void *data;
2172{
2173  struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
2174  varray_type statics = level->static_decls;
2175  tree *vec = &VARRAY_TREE (statics, 0);
2176  int len = VARRAY_ACTIVE_SIZE (statics);
2177  int last_time = (data != 0);
2178
2179  if (last_time)
2180    {
2181      check_global_declarations (vec, len);
2182      return 0;
2183    }
2184
2185  /* Write out any globals that need to be output.  */
2186  return wrapup_global_declarations (vec, len);
2187}
2188
2189
2190/* For debugging.  */
2191static int no_print_functions = 0;
2192static int no_print_builtins = 0;
2193
2194/* Called from print_binding_level through binding_table_foreach to
2195   print the content of binding ENTRY.  DATA is a pointer to line offset
2196   marker.  */
2197static void
2198bt_print_entry (binding_entry entry, void *data)
2199{
2200  int *p = (int *) data;
2201  int len;
2202
2203  if (entry->name == NULL)
2204    len = 3;
2205  else if (entry->name == TYPE_IDENTIFIER (entry->type))
2206    len = 2;
2207  else
2208    len = 4;
2209
2210  *p += len;
2211
2212  if (*p > 5)
2213    {
2214      fprintf (stderr, "\n\t");
2215      *p = len;
2216    }
2217  if (entry->name == NULL)
2218    {
2219      print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
2220      fprintf (stderr, ">");
2221    }
2222  else if (entry->name == TYPE_IDENTIFIER (entry->type))
2223    print_node_brief (stderr, "", entry->type, 0);
2224  else
2225    {
2226      print_node_brief (stderr, "<typedef", entry->name, 0);
2227      print_node_brief (stderr, "", entry->type, 0);
2228      fprintf (stderr, ">");
2229    }
2230}
2231
2232void
2233print_binding_level (lvl)
2234     struct cp_binding_level *lvl;
2235{
2236  tree t;
2237  int i = 0, len;
2238  fprintf (stderr, " blocks=");
2239  fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
2240  if (lvl->tag_transparent)
2241    fprintf (stderr, " tag-transparent");
2242  if (lvl->more_cleanups_ok)
2243    fprintf (stderr, " more-cleanups-ok");
2244  if (lvl->have_cleanups)
2245    fprintf (stderr, " have-cleanups");
2246  fprintf (stderr, "\n");
2247  if (lvl->names)
2248    {
2249      fprintf (stderr, " names:\t");
2250      /* We can probably fit 3 names to a line?  */
2251      for (t = lvl->names; t; t = TREE_CHAIN (t))
2252	{
2253	  if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
2254	    continue;
2255	  if (no_print_builtins
2256	      && (TREE_CODE (t) == TYPE_DECL)
2257	      && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
2258	    continue;
2259
2260	  /* Function decls tend to have longer names.  */
2261	  if (TREE_CODE (t) == FUNCTION_DECL)
2262	    len = 3;
2263	  else
2264	    len = 2;
2265	  i += len;
2266	  if (i > 6)
2267	    {
2268	      fprintf (stderr, "\n\t");
2269	      i = len;
2270	    }
2271	  print_node_brief (stderr, "", t, 0);
2272	  if (t == error_mark_node)
2273	    break;
2274	}
2275      if (i)
2276        fprintf (stderr, "\n");
2277    }
2278  if (lvl->type_decls)
2279    {
2280      fprintf (stderr, " tags:\t");
2281      i = 0;
2282      binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
2283      if (i)
2284	fprintf (stderr, "\n");
2285    }
2286  if (lvl->class_shadowed)
2287    {
2288      fprintf (stderr, " class-shadowed:");
2289      for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
2290	{
2291	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2292	}
2293      fprintf (stderr, "\n");
2294    }
2295  if (lvl->type_shadowed)
2296    {
2297      fprintf (stderr, " type-shadowed:");
2298      for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
2299        {
2300	  fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
2301        }
2302      fprintf (stderr, "\n");
2303    }
2304}
2305
2306void
2307print_other_binding_stack (stack)
2308     struct cp_binding_level *stack;
2309{
2310  struct cp_binding_level *level;
2311  for (level = stack; !global_scope_p (level); level = level->level_chain)
2312    {
2313      fprintf (stderr, "binding level ");
2314      fprintf (stderr, HOST_PTR_PRINTF, level);
2315      fprintf (stderr, "\n");
2316      print_binding_level (level);
2317    }
2318}
2319
2320void
2321print_binding_stack ()
2322{
2323  struct cp_binding_level *b;
2324  fprintf (stderr, "current_binding_level=");
2325  fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2326  fprintf (stderr, "\nclass_binding_level=");
2327  fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2328  fprintf (stderr, "\nNAMESPACE_LEVEL (global_namespace)=");
2329  fprintf (stderr, HOST_PTR_PRINTF,
2330           (void *) NAMESPACE_LEVEL (global_namespace));
2331  fprintf (stderr, "\n");
2332  if (class_binding_level)
2333    {
2334      for (b = class_binding_level; b; b = b->level_chain)
2335	if (b == current_binding_level)
2336	  break;
2337      if (b)
2338	b = class_binding_level;
2339      else
2340	b = current_binding_level;
2341    }
2342  else
2343    b = current_binding_level;
2344  print_other_binding_stack (b);
2345  fprintf (stderr, "global:\n");
2346  print_binding_level (NAMESPACE_LEVEL (global_namespace));
2347}
2348
2349/* Namespace binding access routines.   */
2350
2351/* Check whether the a binding for the name to scope is known.
2352   Returns the binding found, or NULL.  */
2353
2354static inline cxx_binding *
2355find_binding (tree name, tree scope, cxx_binding *front)
2356{
2357  cxx_binding *iter;
2358  cxx_binding *prev = NULL;
2359
2360  timevar_push (TV_NAME_LOOKUP);
2361
2362  for (iter = front; iter; iter = iter->previous)
2363    {
2364      if (BINDING_SCOPE (iter) == scope)
2365	{
2366	  /* Move binding found to the front of the list, so
2367             subsequent lookups will find it faster.  */
2368	  if (prev)
2369	    {
2370	      prev->previous = iter->previous;
2371	      iter->previous = front;
2372	      IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2373	    }
2374	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, iter);
2375	}
2376      prev = iter;
2377    }
2378  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL);
2379}
2380
2381/* Return the binding for NAME in SCOPE, if any.  Otherwise, return NULL.  */
2382cxx_binding *
2383cxx_scope_find_binding_for_name (tree scope, tree name)
2384{
2385  cxx_binding *b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2386  if (b)
2387    {
2388      scope = ORIGINAL_NAMESPACE (scope);
2389      /* Fold-in case where NAME is used only once.  */
2390      if (scope == BINDING_SCOPE (b) && b->previous == NULL)
2391        return b;
2392      return find_binding (name, scope, b);
2393    }
2394  return b;
2395}
2396
2397
2398/* Always returns a binding for name in scope.
2399   If no binding is found, make a new one.  */
2400
2401cxx_binding *
2402binding_for_name (tree name, tree scope)
2403{
2404  cxx_binding *result;
2405
2406  scope = ORIGINAL_NAMESPACE (scope);
2407  result = cxx_scope_find_binding_for_name (scope, name);
2408  if (result)
2409    return result;
2410  /* Not found, make a new one.  */
2411  result = cxx_binding_make ();
2412  result->previous = IDENTIFIER_NAMESPACE_BINDINGS (name);
2413  BINDING_TYPE (result) = NULL_TREE;
2414  BINDING_VALUE (result) = NULL_TREE;
2415  BINDING_SCOPE (result) = scope;
2416  result->is_local = false;
2417  result->value_is_inherited = false;
2418  result->has_level = false;
2419  IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2420  return result;
2421}
2422
2423/* Return the binding value for name in scope.  */
2424
2425tree
2426namespace_binding (tree name, tree scope)
2427{
2428  cxx_binding *b =
2429    cxx_scope_find_binding_for_name (scope ? scope : global_namespace, name);
2430
2431  return b ? b->value : NULL_TREE;
2432}
2433
2434/* Set the binding value for name in scope.  */
2435
2436void
2437set_namespace_binding (name, scope, val)
2438     tree name;
2439     tree scope;
2440     tree val;
2441{
2442  cxx_binding *b;
2443
2444  timevar_push (TV_NAME_LOOKUP);
2445  if (scope == NULL_TREE)
2446    scope = global_namespace;
2447
2448  b = binding_for_name (name, scope);
2449  BINDING_VALUE (b) = val;
2450  timevar_pop (TV_NAME_LOOKUP);
2451}
2452
2453/* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2454   select a name that is unique to this compilation unit.  */
2455
2456void
2457push_namespace (name)
2458     tree name;
2459{
2460  tree d = NULL_TREE;
2461  int need_new = 1;
2462  int implicit_use = 0;
2463  int global = 0;
2464
2465  timevar_push (TV_NAME_LOOKUP);
2466
2467  if (!global_namespace)
2468    {
2469      /* This must be ::.  */
2470      my_friendly_assert (name == get_identifier ("::"), 377);
2471      global = 1;
2472    }
2473  else if (!name)
2474    {
2475      /* The name of anonymous namespace is unique for the translation
2476         unit.  */
2477      if (!anonymous_namespace_name)
2478        anonymous_namespace_name = get_file_function_name ('N');
2479      name = anonymous_namespace_name;
2480      d = IDENTIFIER_NAMESPACE_VALUE (name);
2481      if (d)
2482        /* Reopening anonymous namespace.  */
2483        need_new = 0;
2484      implicit_use = 1;
2485    }
2486  else
2487    {
2488      /* Check whether this is an extended namespace definition.  */
2489      d = IDENTIFIER_NAMESPACE_VALUE (name);
2490      if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2491        {
2492          need_new = 0;
2493          if (DECL_NAMESPACE_ALIAS (d))
2494            {
2495              error ("namespace alias `%D' not allowed here, assuming `%D'",
2496                        d, DECL_NAMESPACE_ALIAS (d));
2497              d = DECL_NAMESPACE_ALIAS (d);
2498            }
2499        }
2500    }
2501
2502  if (need_new)
2503    {
2504      /* Make a new namespace, binding the name to it.  */
2505      d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2506      /* The global namespace is not pushed, and the global binding
2507	 level is set elsewhere.  */
2508      if (!global)
2509	{
2510	  DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2511	  d = pushdecl (d);
2512	  pushlevel (0);
2513	  declare_namespace_level ();
2514	  NAMESPACE_LEVEL (d) = current_binding_level;
2515          current_binding_level->type_decls =
2516            binding_table_new (name == std_identifier
2517                               ? NAMESPACE_STD_HT_SIZE
2518                               : NAMESPACE_ORDINARY_HT_SIZE);
2519	  VARRAY_TREE_INIT (current_binding_level->static_decls,
2520			    name != std_identifier ? 10 : 200,
2521			    "Static declarations");
2522	}
2523    }
2524  else
2525    resume_binding_level (NAMESPACE_LEVEL (d));
2526
2527  if (implicit_use)
2528    do_using_directive (d);
2529  /* Enter the name space.  */
2530  current_namespace = d;
2531
2532  timevar_pop (TV_NAME_LOOKUP);
2533}
2534
2535/* Pop from the scope of the current namespace.  */
2536
2537void
2538pop_namespace ()
2539{
2540  my_friendly_assert (current_namespace != global_namespace, 20010801);
2541  current_namespace = CP_DECL_CONTEXT (current_namespace);
2542  /* The binding level is not popped, as it might be re-opened later.  */
2543  suspend_binding_level ();
2544}
2545
2546/* Push into the scope of the namespace NS, even if it is deeply
2547   nested within another namespace.  */
2548
2549void
2550push_nested_namespace (ns)
2551     tree ns;
2552{
2553  if (ns == global_namespace)
2554    push_to_top_level ();
2555  else
2556    {
2557      push_nested_namespace (CP_DECL_CONTEXT (ns));
2558      push_namespace (DECL_NAME (ns));
2559    }
2560}
2561
2562/* Pop back from the scope of the namespace NS, which was previously
2563   entered with push_nested_namespace.  */
2564
2565void
2566pop_nested_namespace (ns)
2567     tree ns;
2568{
2569  timevar_push (TV_NAME_LOOKUP);
2570  while (ns != global_namespace)
2571    {
2572      pop_namespace ();
2573      ns = CP_DECL_CONTEXT (ns);
2574    }
2575
2576  pop_from_top_level ();
2577  timevar_pop (TV_NAME_LOOKUP);
2578}
2579
2580
2581/* Allocate storage for saving a C++ binding.  */
2582#define cxx_saved_binding_make() \
2583  (ggc_alloc (sizeof (cxx_saved_binding)))
2584
2585struct cxx_saved_binding GTY(())
2586{
2587  /* Link that chains saved C++ bindings for a given name into a stack.  */
2588  cxx_saved_binding *previous;
2589  /* The name of the current binding.  */
2590  tree identifier;
2591  /* The binding we're saving.  */
2592  cxx_binding *binding;
2593  tree class_value;
2594  tree real_type_value;
2595};
2596
2597/* Subroutines for reverting temporarily to top-level for instantiation
2598   of templates and such.  We actually need to clear out the class- and
2599   local-value slots of all identifiers, so that only the global values
2600   are at all visible.  Simply setting current_binding_level to the global
2601   scope isn't enough, because more binding levels may be pushed.  */
2602struct saved_scope *scope_chain;
2603
2604static cxx_saved_binding *
2605store_bindings (tree names, cxx_saved_binding *old_bindings)
2606{
2607  tree t;
2608  cxx_saved_binding *search_bindings = old_bindings;
2609
2610  timevar_push (TV_NAME_LOOKUP);
2611  for (t = names; t; t = TREE_CHAIN (t))
2612    {
2613      tree id;
2614      cxx_saved_binding *saved;
2615      cxx_saved_binding *t1;
2616
2617      if (TREE_CODE (t) == TREE_LIST)
2618	id = TREE_PURPOSE (t);
2619      else
2620	id = DECL_NAME (t);
2621
2622      if (!id
2623	  /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2624	     we have no IDENTIFIER_BINDING if we have left the class
2625	     scope, but cached the class-level declarations.  */
2626	  || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2627	continue;
2628
2629      for (t1 = search_bindings; t1; t1 = t1->previous)
2630	if (t1->identifier == id)
2631	  goto skip_it;
2632
2633      my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2634      saved = cxx_saved_binding_make ();
2635      saved->previous = old_bindings;
2636      saved->identifier = id;
2637      saved->binding = IDENTIFIER_BINDING (id);
2638      saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2639      saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2640      IDENTIFIER_BINDING (id) = NULL;
2641      IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2642      old_bindings = saved;
2643    skip_it:
2644      ;
2645    }
2646  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2647}
2648
2649void
2650maybe_push_to_top_level (pseudo)
2651     int pseudo;
2652{
2653  struct saved_scope *s;
2654  struct cp_binding_level *b;
2655  cxx_saved_binding *old_bindings;
2656  int need_pop;
2657
2658  timevar_push (TV_NAME_LOOKUP);
2659
2660  s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2661
2662  b = scope_chain ? current_binding_level : 0;
2663
2664  /* If we're in the middle of some function, save our state.  */
2665  if (cfun)
2666    {
2667      need_pop = 1;
2668      push_function_context_to (NULL_TREE);
2669    }
2670  else
2671    need_pop = 0;
2672
2673  old_bindings = NULL;
2674  if (scope_chain && previous_class_type)
2675    old_bindings = store_bindings (previous_class_values, old_bindings);
2676
2677  /* Have to include the global scope, because class-scope decls
2678     aren't listed anywhere useful.  */
2679  for (; b; b = b->level_chain)
2680    {
2681      tree t;
2682
2683      /* Template IDs are inserted into the global level. If they were
2684	 inserted into namespace level, finish_file wouldn't find them
2685	 when doing pending instantiations. Therefore, don't stop at
2686	 namespace level, but continue until :: .  */
2687      if (global_scope_p (b) || (pseudo && b->template_parms_p))
2688	break;
2689
2690      old_bindings = store_bindings (b->names, old_bindings);
2691      /* We also need to check class_shadowed to save class-level type
2692	 bindings, since pushclass doesn't fill in b->names.  */
2693      if (b->parm_flag == 2)
2694	old_bindings = store_bindings (b->class_shadowed, old_bindings);
2695
2696      /* Unwind type-value slots back to top level.  */
2697      for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2698	SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2699    }
2700  s->prev = scope_chain;
2701  s->old_bindings = old_bindings;
2702  s->bindings = b;
2703  s->need_pop_function_context = need_pop;
2704  s->function_decl = current_function_decl;
2705  s->last_parms = last_function_parms;
2706
2707  scope_chain = s;
2708  current_function_decl = NULL_TREE;
2709  VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2710  current_lang_name = lang_name_cplusplus;
2711  current_namespace = global_namespace;
2712  timevar_pop (TV_NAME_LOOKUP);
2713}
2714
2715void
2716push_to_top_level ()
2717{
2718  maybe_push_to_top_level (0);
2719}
2720
2721void
2722pop_from_top_level ()
2723{
2724  struct saved_scope *s = scope_chain;
2725  cxx_saved_binding *saved;
2726
2727  timevar_push (TV_NAME_LOOKUP);
2728
2729  /* Clear out class-level bindings cache.  */
2730  if (previous_class_type)
2731    invalidate_class_lookup_cache ();
2732
2733  current_lang_base = 0;
2734
2735  scope_chain = s->prev;
2736  for (saved = s->old_bindings; saved; saved = saved->previous)
2737    {
2738      tree id = saved->identifier;
2739
2740      IDENTIFIER_BINDING (id) = saved->binding;
2741      IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2742      SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2743    }
2744
2745  /* If we were in the middle of compiling a function, restore our
2746     state.  */
2747  if (s->need_pop_function_context)
2748    pop_function_context_from (NULL_TREE);
2749  current_function_decl = s->function_decl;
2750  last_function_parms = s->last_parms;
2751
2752  timevar_pop (TV_NAME_LOOKUP);
2753}
2754
2755/* Push a definition of struct, union or enum tag "name".
2756   into binding_level "b".   "type" should be the type node,
2757   We assume that the tag "name" is not already defined.
2758
2759   Note that the definition may really be just a forward reference.
2760   In that case, the TYPE_SIZE will be a NULL_TREE.
2761
2762   C++ gratuitously puts all these tags in the name space.  */
2763
2764/* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2765   record the shadowed value for this binding contour.  TYPE is
2766   the type that ID maps to.  */
2767
2768static void
2769set_identifier_type_value_with_scope (id, type, b)
2770     tree id;
2771     tree type;
2772     struct cp_binding_level *b;
2773{
2774  if (!b->namespace_p)
2775    {
2776      /* Shadow the marker, not the real thing, so that the marker
2777	 gets restored later.  */
2778      tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2779      b->type_shadowed
2780	= tree_cons (id, old_type_value, b->type_shadowed);
2781    }
2782  else
2783    {
2784      cxx_binding *binding = binding_for_name (id, current_namespace);
2785      BINDING_TYPE (binding) = type;
2786      /* Store marker instead of real type.  */
2787      type = global_type_node;
2788    }
2789  SET_IDENTIFIER_TYPE_VALUE (id, type);
2790}
2791
2792/* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2793
2794void
2795set_identifier_type_value (id, type)
2796     tree id;
2797     tree type;
2798{
2799  set_identifier_type_value_with_scope (id, type, current_binding_level);
2800}
2801
2802/* Return the type associated with id.  */
2803
2804tree
2805identifier_type_value (id)
2806     tree id;
2807{
2808  timevar_push (TV_NAME_LOOKUP);
2809  /* There is no type with that name, anywhere.  */
2810  if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2811    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2812  /* This is not the type marker, but the real thing.  */
2813  if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2814    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2815  /* Have to search for it. It must be on the global level, now.
2816     Ask lookup_name not to return non-types.  */
2817  id = lookup_name_real (id, 2, 1, 0);
2818  if (id)
2819    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2820  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2821}
2822
2823/* Pop off extraneous binding levels left over due to syntax errors.
2824
2825   We don't pop past namespaces, as they might be valid.  */
2826
2827void
2828pop_everything ()
2829{
2830  if (ENABLE_SCOPE_CHECKING)
2831    verbatim ("XXX entering pop_everything ()\n");
2832  while (!toplevel_bindings_p ())
2833    {
2834      if (current_binding_level->parm_flag == 2)
2835	pop_nested_class ();
2836      else
2837	poplevel (0, 0, 0);
2838    }
2839  if (ENABLE_SCOPE_CHECKING)
2840    verbatim ("XXX leaving pop_everything ()\n");
2841}
2842
2843/* The type TYPE is being declared.  If it is a class template, or a
2844   specialization of a class template, do any processing required and
2845   perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2846   being declared a friend.  B is the binding level at which this TYPE
2847   should be bound.
2848
2849   Returns the TYPE_DECL for TYPE, which may have been altered by this
2850   processing.  */
2851
2852static tree
2853maybe_process_template_type_declaration (type, globalize, b)
2854     tree type;
2855     int globalize;
2856     struct cp_binding_level* b;
2857{
2858  tree decl = TYPE_NAME (type);
2859
2860  if (processing_template_parmlist)
2861    /* You can't declare a new template type in a template parameter
2862       list.  But, you can declare a non-template type:
2863
2864         template <class A*> struct S;
2865
2866       is a forward-declaration of `A'.  */
2867    ;
2868  else
2869    {
2870      maybe_check_template_type (type);
2871
2872      my_friendly_assert (IS_AGGR_TYPE (type)
2873			  || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2874
2875
2876      if (processing_template_decl)
2877	{
2878	  /* This may change after the call to
2879	     push_template_decl_real, but we want the original value.  */
2880	  tree name = DECL_NAME (decl);
2881
2882	  decl = push_template_decl_real (decl, globalize);
2883	  /* If the current binding level is the binding level for the
2884	     template parameters (see the comment in
2885	     begin_template_parm_list) and the enclosing level is a class
2886	     scope, and we're not looking at a friend, push the
2887	     declaration of the member class into the class scope.  In the
2888	     friend case, push_template_decl will already have put the
2889	     friend into global scope, if appropriate.  */
2890	  if (TREE_CODE (type) != ENUMERAL_TYPE
2891	      && !globalize && b->template_parms_p
2892	      && b->level_chain->parm_flag == 2)
2893	    {
2894	      finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2895	      /* Put this tag on the list of tags for the class, since
2896		 that won't happen below because B is not the class
2897		 binding level, but is instead the pseudo-global level.  */
2898              if (b->level_chain->type_decls == NULL)
2899                b->level_chain->type_decls =
2900                  binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2901              binding_table_insert (b->level_chain->type_decls, name, type);
2902	      if (!COMPLETE_TYPE_P (current_class_type))
2903		{
2904		  maybe_add_class_template_decl_list (current_class_type,
2905						      type, /*friend_p=*/0);
2906		  CLASSTYPE_NESTED_UDTS (current_class_type) =
2907                    b->level_chain->type_decls;
2908		}
2909	    }
2910	}
2911    }
2912
2913  return decl;
2914}
2915
2916/* In C++, you don't have to write `struct S' to refer to `S'; you
2917   can just use `S'.  We accomplish this by creating a TYPE_DECL as
2918   if the user had written `typedef struct S S'.  Create and return
2919   the TYPE_DECL for TYPE.  */
2920
2921tree
2922create_implicit_typedef (name, type)
2923     tree name;
2924     tree type;
2925{
2926  tree decl;
2927
2928  decl = build_decl (TYPE_DECL, name, type);
2929  DECL_ARTIFICIAL (decl) = 1;
2930  /* There are other implicit type declarations, like the one *within*
2931     a class that allows you to write `S::S'.  We must distinguish
2932     amongst these.  */
2933  SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2934  TYPE_NAME (type) = decl;
2935
2936  return decl;
2937}
2938
2939/* Remember a local name for name-mangling purposes.  */
2940
2941static void
2942push_local_name (decl)
2943     tree decl;
2944{
2945  size_t i, nelts;
2946  tree t, name;
2947
2948  timevar_push (TV_NAME_LOOKUP);
2949
2950  if (!local_names)
2951    VARRAY_TREE_INIT (local_names, 8, "local_names");
2952
2953  name = DECL_NAME (decl);
2954
2955  nelts = VARRAY_ACTIVE_SIZE (local_names);
2956  for (i = 0; i < nelts; i++)
2957    {
2958      t = VARRAY_TREE (local_names, i);
2959      if (DECL_NAME (t) == name)
2960	{
2961	  if (!DECL_LANG_SPECIFIC (decl))
2962	    retrofit_lang_decl (decl);
2963	  DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2964	  if (DECL_LANG_SPECIFIC (t))
2965	    DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2966	  else
2967	    DECL_DISCRIMINATOR (decl) = 1;
2968
2969	  VARRAY_TREE (local_names, i) = decl;
2970	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
2971	}
2972    }
2973
2974  VARRAY_PUSH_TREE (local_names, decl);
2975  timevar_pop (TV_NAME_LOOKUP);
2976}
2977
2978/* Push a tag name NAME for struct/class/union/enum type TYPE.
2979   Normally put it into the inner-most non-tag-transparent scope,
2980   but if GLOBALIZE is true, put it in the inner-most non-class scope.
2981   The latter is needed for implicit declarations.  */
2982
2983void
2984pushtag (name, type, globalize)
2985     tree name, type;
2986     int globalize;
2987{
2988  register struct cp_binding_level *b;
2989
2990  timevar_push (TV_NAME_LOOKUP);
2991
2992  b = current_binding_level;
2993  while (b->tag_transparent
2994	 || (b->parm_flag == 2
2995	     && (globalize
2996		 /* We may be defining a new type in the initializer
2997		    of a static member variable. We allow this when
2998		    not pedantic, and it is particularly useful for
2999		    type punning via an anonymous union.  */
3000		 || COMPLETE_TYPE_P (b->this_class))))
3001    b = b->level_chain;
3002
3003  if (b->type_decls == NULL)
3004    b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
3005  binding_table_insert (b->type_decls, name, type);
3006
3007  if (name)
3008    {
3009      /* Do C++ gratuitous typedefing.  */
3010      if (IDENTIFIER_TYPE_VALUE (name) != type)
3011        {
3012          register tree d = NULL_TREE;
3013	  int in_class = 0;
3014	  tree context = TYPE_CONTEXT (type);
3015
3016	  if (! context)
3017	    {
3018	      tree cs = current_scope ();
3019
3020	      if (! globalize)
3021		context = cs;
3022	      else if (cs != NULL_TREE && TYPE_P (cs))
3023		/* When declaring a friend class of a local class, we want
3024		   to inject the newly named class into the scope
3025		   containing the local class, not the namespace scope.  */
3026		context = decl_function_context (get_type_decl (cs));
3027	    }
3028	  if (!context)
3029	    context = current_namespace;
3030
3031	  if ((b->template_parms_p && b->level_chain->parm_flag == 2)
3032	      || b->parm_flag == 2)
3033	    in_class = 1;
3034
3035	  if (current_lang_name == lang_name_java)
3036	    TYPE_FOR_JAVA (type) = 1;
3037
3038	  d = create_implicit_typedef (name, type);
3039	  DECL_CONTEXT (d) = FROB_CONTEXT (context);
3040	  if (! in_class)
3041	    set_identifier_type_value_with_scope (name, type, b);
3042
3043	  d = maybe_process_template_type_declaration (type,
3044						       globalize, b);
3045
3046	  if (b->parm_flag == 2)
3047	    {
3048	      if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
3049		/* Put this TYPE_DECL on the TYPE_FIELDS list for the
3050		   class.  But if it's a member template class, we
3051		   want the TEMPLATE_DECL, not the TYPE_DECL, so this
3052		   is done later.  */
3053		finish_member_declaration (d);
3054	      else
3055		pushdecl_class_level (d);
3056	    }
3057	  else
3058	    d = pushdecl_with_scope (d, b);
3059
3060	  /* FIXME what if it gets a name from typedef?  */
3061	  if (ANON_AGGRNAME_P (name))
3062	    DECL_IGNORED_P (d) = 1;
3063
3064	  TYPE_CONTEXT (type) = DECL_CONTEXT (d);
3065
3066	  /* If this is a local class, keep track of it.  We need this
3067	     information for name-mangling, and so that it is possible to find
3068	     all function definitions in a translation unit in a convenient
3069	     way.  (It's otherwise tricky to find a member function definition
3070	     it's only pointed to from within a local class.)  */
3071	  if (TYPE_CONTEXT (type)
3072	      && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
3073	      && !processing_template_decl)
3074	    VARRAY_PUSH_TREE (local_classes, type);
3075        }
3076      if (b->parm_flag == 2)
3077	{
3078	  if (!COMPLETE_TYPE_P (current_class_type))
3079	    {
3080	      maybe_add_class_template_decl_list (current_class_type,
3081						  type, /*friend_p=*/0);
3082	      CLASSTYPE_NESTED_UDTS (current_class_type) = b->type_decls;
3083	    }
3084	}
3085    }
3086
3087  if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
3088    /* Use the canonical TYPE_DECL for this node.  */
3089    TYPE_STUB_DECL (type) = TYPE_NAME (type);
3090  else
3091    {
3092      /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
3093	 will be the tagged type we just added to the current
3094	 binding level.  This fake NULL-named TYPE_DECL node helps
3095	 dwarfout.c to know when it needs to output a
3096	 representation of a tagged type, and it also gives us a
3097	 convenient place to record the "scope start" address for
3098	 the tagged type.  */
3099
3100      tree d = build_decl (TYPE_DECL, NULL_TREE, type);
3101      TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
3102    }
3103
3104  timevar_pop (TV_NAME_LOOKUP);
3105}
3106
3107/* Counter used to create anonymous type names.  */
3108
3109static int anon_cnt = 0;
3110
3111/* Return an IDENTIFIER which can be used as a name for
3112   anonymous structs and unions.  */
3113
3114tree
3115make_anon_name ()
3116{
3117  char buf[32];
3118
3119  sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
3120  return get_identifier (buf);
3121}
3122
3123/* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
3124   This keeps dbxout from getting confused.  */
3125
3126void
3127clear_anon_tags ()
3128{
3129  register struct cp_binding_level *b;
3130  static int last_cnt = 0;
3131
3132  /* Fast out if no new anon names were declared.  */
3133  if (last_cnt == anon_cnt)
3134    return;
3135
3136  b = current_binding_level;
3137  while (b->tag_transparent)
3138    b = b->level_chain;
3139  if (b->type_decls != NULL)
3140    binding_table_remove_anonymous_types (b->type_decls);
3141  last_cnt = anon_cnt;
3142}
3143
3144/* Subroutine of duplicate_decls: return truthvalue of whether
3145   or not types of these decls match.
3146
3147   For C++, we must compare the parameter list so that `int' can match
3148   `int&' in a parameter position, but `int&' is not confused with
3149   `const int&'.  */
3150
3151int
3152decls_match (newdecl, olddecl)
3153     tree newdecl, olddecl;
3154{
3155  int types_match;
3156
3157  if (newdecl == olddecl)
3158    return 1;
3159
3160  if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
3161    /* If the two DECLs are not even the same kind of thing, we're not
3162       interested in their types.  */
3163    return 0;
3164
3165  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3166    {
3167      tree f1 = TREE_TYPE (newdecl);
3168      tree f2 = TREE_TYPE (olddecl);
3169      tree p1 = TYPE_ARG_TYPES (f1);
3170      tree p2 = TYPE_ARG_TYPES (f2);
3171
3172      if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
3173	  && ! (DECL_EXTERN_C_P (newdecl)
3174		&& DECL_EXTERN_C_P (olddecl)))
3175	return 0;
3176
3177      if (TREE_CODE (f1) != TREE_CODE (f2))
3178        return 0;
3179
3180      if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
3181	{
3182	  if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
3183	      && (DECL_BUILT_IN (olddecl)
3184#ifndef NO_IMPLICIT_EXTERN_C
3185	          || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
3186	          || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
3187#endif
3188	      ))
3189	    {
3190	      types_match = self_promoting_args_p (p1);
3191	      if (p1 == void_list_node)
3192		TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3193	    }
3194#ifndef NO_IMPLICIT_EXTERN_C
3195	  else if (p1 == NULL_TREE
3196		   && (DECL_EXTERN_C_P (olddecl)
3197	               && DECL_IN_SYSTEM_HEADER (olddecl)
3198	               && !DECL_CLASS_SCOPE_P (olddecl))
3199		   && (DECL_EXTERN_C_P (newdecl)
3200	               && DECL_IN_SYSTEM_HEADER (newdecl)
3201	               && !DECL_CLASS_SCOPE_P (newdecl)))
3202	    {
3203	      types_match = self_promoting_args_p (p2);
3204	      TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
3205	    }
3206#endif
3207	  else
3208	    types_match = compparms (p1, p2);
3209	}
3210      else
3211	types_match = 0;
3212    }
3213  else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3214    {
3215      if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3216				DECL_TEMPLATE_PARMS (olddecl)))
3217	return 0;
3218
3219      if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
3220	  != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
3221	return 0;
3222
3223      if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3224	types_match = 1;
3225      else
3226	types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
3227				   DECL_TEMPLATE_RESULT (newdecl));
3228    }
3229  else
3230    {
3231      if (TREE_TYPE (newdecl) == error_mark_node)
3232	types_match = TREE_TYPE (olddecl) == error_mark_node;
3233      else if (TREE_TYPE (olddecl) == NULL_TREE)
3234	types_match = TREE_TYPE (newdecl) == NULL_TREE;
3235      else if (TREE_TYPE (newdecl) == NULL_TREE)
3236	types_match = 0;
3237      else
3238	types_match = comptypes (TREE_TYPE (newdecl),
3239				 TREE_TYPE (olddecl),
3240				 COMPARE_REDECLARATION);
3241    }
3242
3243  return types_match;
3244}
3245
3246/* If NEWDECL is `static' and an `extern' was seen previously,
3247   warn about it.  OLDDECL is the previous declaration.
3248
3249   Note that this does not apply to the C++ case of declaring
3250   a variable `extern const' and then later `const'.
3251
3252   Don't complain about built-in functions, since they are beyond
3253   the user's control.  */
3254
3255static void
3256warn_extern_redeclared_static (newdecl, olddecl)
3257     tree newdecl, olddecl;
3258{
3259  static const char *const explicit_extern_static_warning
3260    = "`%D' was declared `extern' and later `static'";
3261  static const char *const implicit_extern_static_warning
3262    = "`%D' was declared implicitly `extern' and later `static'";
3263
3264  tree name;
3265
3266  if (TREE_CODE (newdecl) == TYPE_DECL
3267      || TREE_CODE (newdecl) == TEMPLATE_DECL
3268      || TREE_CODE (newdecl) == CONST_DECL)
3269    return;
3270
3271  /* Don't get confused by static member functions; that's a different
3272     use of `static'.  */
3273  if (TREE_CODE (newdecl) == FUNCTION_DECL
3274      && DECL_STATIC_FUNCTION_P (newdecl))
3275    return;
3276
3277  /* If the old declaration was `static', or the new one isn't, then
3278     then everything is OK.  */
3279  if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
3280    return;
3281
3282  /* It's OK to declare a builtin function as `static'.  */
3283  if (TREE_CODE (olddecl) == FUNCTION_DECL
3284      && DECL_ARTIFICIAL (olddecl))
3285    return;
3286
3287  name = DECL_ASSEMBLER_NAME (newdecl);
3288  pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
3289	      ? implicit_extern_static_warning
3290	      : explicit_extern_static_warning, newdecl);
3291  cp_pedwarn_at ("previous declaration of `%D'", olddecl);
3292}
3293
3294/* Handle when a new declaration NEWDECL has the same name as an old
3295   one OLDDECL in the same binding contour.  Prints an error message
3296   if appropriate.
3297
3298   If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
3299   Otherwise, return 0.  */
3300
3301int
3302duplicate_decls (newdecl, olddecl)
3303     tree newdecl, olddecl;
3304{
3305  unsigned olddecl_uid = DECL_UID (olddecl);
3306  int olddecl_friend = 0, types_match = 0;
3307  int new_defines_function = 0;
3308
3309  if (newdecl == olddecl)
3310    return 1;
3311
3312  types_match = decls_match (newdecl, olddecl);
3313
3314  /* If either the type of the new decl or the type of the old decl is an
3315     error_mark_node, then that implies that we have already issued an
3316     error (earlier) for some bogus type specification, and in that case,
3317     it is rather pointless to harass the user with yet more error message
3318     about the same declaration, so just pretend the types match here.  */
3319  if (TREE_TYPE (newdecl) == error_mark_node
3320      || TREE_TYPE (olddecl) == error_mark_node)
3321    types_match = 1;
3322
3323  if (DECL_P (olddecl)
3324      && TREE_CODE (newdecl) == FUNCTION_DECL
3325      && TREE_CODE (olddecl) == FUNCTION_DECL
3326      && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
3327    {
3328      if (DECL_DECLARED_INLINE_P (newdecl)
3329	  && DECL_UNINLINABLE (newdecl)
3330	  && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3331	/* Already warned elsewhere.  */;
3332      else if (DECL_DECLARED_INLINE_P (olddecl)
3333	       && DECL_UNINLINABLE (olddecl)
3334	       && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3335	/* Already warned.  */;
3336      else if (DECL_DECLARED_INLINE_P (newdecl)
3337	       && DECL_UNINLINABLE (olddecl)
3338	       && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
3339	{
3340	  warning_with_decl (newdecl,
3341			     "function `%s' redeclared as inline");
3342	  warning_with_decl (olddecl,
3343			     "previous declaration of function `%s' with attribute noinline");
3344	}
3345      else if (DECL_DECLARED_INLINE_P (olddecl)
3346	       && DECL_UNINLINABLE (newdecl)
3347	       && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
3348	{
3349	  warning_with_decl (newdecl,
3350			     "function `%s' redeclared with attribute noinline");
3351	  warning_with_decl (olddecl,
3352			     "previous declaration of function `%s' was inline");
3353	}
3354    }
3355
3356  /* Check for redeclaration and other discrepancies.  */
3357  if (TREE_CODE (olddecl) == FUNCTION_DECL
3358      && DECL_ARTIFICIAL (olddecl))
3359    {
3360      if (TREE_CODE (newdecl) != FUNCTION_DECL)
3361	{
3362          /* Avoid warnings redeclaring anticipated built-ins.  */
3363          if (DECL_ANTICIPATED (olddecl))
3364            return 0;
3365
3366	  /* If you declare a built-in or predefined function name as static,
3367	     the old definition is overridden, but optionally warn this was a
3368	     bad choice of name.  */
3369	  if (! TREE_PUBLIC (newdecl))
3370	    {
3371	      if (warn_shadow)
3372		warning ("shadowing %s function `%#D'",
3373			    DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3374			    olddecl);
3375	      /* Discard the old built-in function.  */
3376	      return 0;
3377	    }
3378	  /* If the built-in is not ansi, then programs can override
3379	     it even globally without an error.  */
3380	  else if (! DECL_BUILT_IN (olddecl))
3381	    warning ("library function `%#D' redeclared as non-function `%#D'",
3382			olddecl, newdecl);
3383	  else
3384	    {
3385	      error ("declaration of `%#D'", newdecl);
3386	      error ("conflicts with built-in declaration `%#D'",
3387			olddecl);
3388	    }
3389	  return 0;
3390	}
3391      else if (!types_match)
3392	{
3393          /* Avoid warnings redeclaring anticipated built-ins.  */
3394          if (DECL_ANTICIPATED (olddecl))
3395            ;  /* Do nothing yet.  */
3396	  else if ((DECL_EXTERN_C_P (newdecl)
3397		    && DECL_EXTERN_C_P (olddecl))
3398		   || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3399				 TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3400	    {
3401	      /* A near match; override the builtin.  */
3402
3403	      if (TREE_PUBLIC (newdecl))
3404		{
3405		  warning ("new declaration `%#D'", newdecl);
3406		  warning ("ambiguates built-in declaration `%#D'",
3407			      olddecl);
3408		}
3409	      else if (warn_shadow)
3410		warning ("shadowing %s function `%#D'",
3411			    DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3412			    olddecl);
3413	    }
3414	  else
3415	    /* Discard the old built-in function.  */
3416	    return 0;
3417
3418	  /* Replace the old RTL to avoid problems with inlining.  */
3419	  SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3420	}
3421      /* Even if the types match, prefer the new declarations type
3422	 for anitipated built-ins, for exception lists, etc...  */
3423      else if (DECL_ANTICIPATED (olddecl))
3424	{
3425	  tree type = TREE_TYPE (newdecl);
3426	  tree attribs = (*targetm.merge_type_attributes)
3427	    (TREE_TYPE (olddecl), type);
3428
3429	  type = build_type_attribute_variant (type, attribs);
3430	  TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = type;
3431	}
3432
3433      /* Whether or not the builtin can throw exceptions has no
3434	 bearing on this declarator.  */
3435      TREE_NOTHROW (olddecl) = 0;
3436
3437      if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3438	{
3439	  /* If a builtin function is redeclared as `static', merge
3440	     the declarations, but make the original one static.  */
3441	  DECL_THIS_STATIC (olddecl) = 1;
3442	  TREE_PUBLIC (olddecl) = 0;
3443
3444	  /* Make the old declaration consistent with the new one so
3445	     that all remnants of the builtin-ness of this function
3446	     will be banished.  */
3447	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3448	  SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3449	}
3450    }
3451  else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3452    {
3453      if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3454	   && TREE_CODE (newdecl) != TYPE_DECL
3455	   && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3456		 && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3457	  || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3458	      && TREE_CODE (olddecl) != TYPE_DECL
3459	      && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3460		    && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3461			== TYPE_DECL))))
3462	{
3463	  /* We do nothing special here, because C++ does such nasty
3464	     things with TYPE_DECLs.  Instead, just let the TYPE_DECL
3465	     get shadowed, and know that if we need to find a TYPE_DECL
3466	     for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3467	     slot of the identifier.  */
3468	  return 0;
3469	}
3470
3471      if ((TREE_CODE (newdecl) == FUNCTION_DECL
3472	   && DECL_FUNCTION_TEMPLATE_P (olddecl))
3473	  || (TREE_CODE (olddecl) == FUNCTION_DECL
3474	      && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3475	return 0;
3476
3477      error ("`%#D' redeclared as different kind of symbol", newdecl);
3478      if (TREE_CODE (olddecl) == TREE_LIST)
3479	olddecl = TREE_VALUE (olddecl);
3480      cp_error_at ("previous declaration of `%#D'", olddecl);
3481
3482      /* New decl is completely inconsistent with the old one =>
3483	 tell caller to replace the old one.  */
3484
3485      return 0;
3486    }
3487  else if (!types_match)
3488    {
3489      if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3490	/* These are certainly not duplicate declarations; they're
3491	   from different scopes.  */
3492	return 0;
3493
3494      if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3495	{
3496	  /* The name of a class template may not be declared to refer to
3497	     any other template, class, function, object, namespace, value,
3498	     or type in the same scope.  */
3499	  if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3500	      || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3501	    {
3502	      error ("declaration of template `%#D'", newdecl);
3503	      cp_error_at ("conflicts with previous declaration `%#D'",
3504			   olddecl);
3505	    }
3506	  else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3507		   && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3508		   && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3509				 TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3510		   && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3511					   DECL_TEMPLATE_PARMS (olddecl))
3512		   /* Template functions can be disambiguated by
3513		      return type.  */
3514		   && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3515				   TREE_TYPE (TREE_TYPE (olddecl))))
3516	    {
3517	      error ("new declaration `%#D'", newdecl);
3518	      cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3519	    }
3520	  return 0;
3521	}
3522      if (TREE_CODE (newdecl) == FUNCTION_DECL)
3523	{
3524	  if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3525	    {
3526	      error ("declaration of C function `%#D' conflicts with",
3527			newdecl);
3528	      cp_error_at ("previous declaration `%#D' here", olddecl);
3529	    }
3530	  else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3531			      TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3532	    {
3533	      error ("new declaration `%#D'", newdecl);
3534	      cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3535	    }
3536	  else
3537	    return 0;
3538	}
3539
3540      /* Already complained about this, so don't do so again.  */
3541      else if (current_class_type == NULL_TREE
3542	       || !DECL_ASSEMBLER_NAME_SET_P (newdecl)
3543	       || (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl))
3544		   != current_class_type))
3545	{
3546	  error ("conflicting types for `%#D'", newdecl);
3547	  cp_error_at ("previous declaration as `%#D'", olddecl);
3548	}
3549    }
3550  else if (TREE_CODE (newdecl) == FUNCTION_DECL
3551	    && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3552		 && (!DECL_TEMPLATE_INFO (newdecl)
3553		     || (DECL_TI_TEMPLATE (newdecl)
3554			 != DECL_TI_TEMPLATE (olddecl))))
3555		|| (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3556		    && (!DECL_TEMPLATE_INFO (olddecl)
3557			|| (DECL_TI_TEMPLATE (olddecl)
3558			    != DECL_TI_TEMPLATE (newdecl))))))
3559    /* It's OK to have a template specialization and a non-template
3560       with the same type, or to have specializations of two
3561       different templates with the same type.  Note that if one is a
3562       specialization, and the other is an instantiation of the same
3563       template, that we do not exit at this point.  That situation
3564       can occur if we instantiate a template class, and then
3565       specialize one of its methods.  This situation is valid, but
3566       the declarations must be merged in the usual way.  */
3567    return 0;
3568  else if (TREE_CODE (newdecl) == FUNCTION_DECL
3569	   && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3570		&& !DECL_USE_TEMPLATE (newdecl))
3571	       || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3572		   && !DECL_USE_TEMPLATE (olddecl))))
3573    /* One of the declarations is a template instantiation, and the
3574       other is not a template at all.  That's OK.  */
3575    return 0;
3576  else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3577           && DECL_NAMESPACE_ALIAS (newdecl)
3578           && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3579    /* Redeclaration of namespace alias, ignore it.  */
3580    return 1;
3581  else
3582    {
3583      const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3584      if (errmsg)
3585	{
3586	  error (errmsg, newdecl);
3587	  if (DECL_NAME (olddecl) != NULL_TREE)
3588	    cp_error_at ((DECL_INITIAL (olddecl)
3589			  && namespace_bindings_p ())
3590			 ? "`%#D' previously defined here"
3591			 : "`%#D' previously declared here", olddecl);
3592	  return 0;
3593	}
3594      else if (TREE_CODE (olddecl) == FUNCTION_DECL
3595	       && DECL_INITIAL (olddecl) != NULL_TREE
3596	       && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3597	       && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3598	{
3599	  /* Prototype decl follows defn w/o prototype.  */
3600	  cp_warning_at ("prototype for `%#D'", newdecl);
3601	  cp_warning_at ("follows non-prototype definition here", olddecl);
3602	}
3603      else if (TREE_CODE (olddecl) == FUNCTION_DECL
3604	       && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3605	{
3606	  /* extern "C" int foo ();
3607	     int foo () { bar (); }
3608	     is OK.  */
3609	  if (current_lang_depth () == 0)
3610	    SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3611	  else
3612	    {
3613	      cp_error_at ("previous declaration of `%#D' with %L linkage",
3614			   olddecl, DECL_LANGUAGE (olddecl));
3615	      error ("conflicts with new declaration with %L linkage",
3616			DECL_LANGUAGE (newdecl));
3617	    }
3618	}
3619
3620      if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3621	;
3622      else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3623	{
3624	  tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3625	  tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3626	  int i = 1;
3627
3628	  if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3629	    t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3630
3631	  for (; t1 && t1 != void_list_node;
3632	       t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3633	    if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3634	      {
3635		if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3636					   TREE_PURPOSE (t2)))
3637		  {
3638		    pedwarn ("default argument given for parameter %d of `%#D'",
3639			     i, newdecl);
3640		    cp_pedwarn_at ("after previous specification in `%#D'",
3641			           olddecl);
3642		  }
3643		else
3644		  {
3645		    error ("default argument given for parameter %d of `%#D'",
3646			      i, newdecl);
3647		    cp_error_at ("after previous specification in `%#D'",
3648				 olddecl);
3649		  }
3650	      }
3651
3652	  if (DECL_DECLARED_INLINE_P (newdecl)
3653	      && ! DECL_DECLARED_INLINE_P (olddecl)
3654	      && TREE_ADDRESSABLE (olddecl) && warn_inline)
3655	    {
3656	      warning ("`%#D' was used before it was declared inline",
3657			  newdecl);
3658	      cp_warning_at ("previous non-inline declaration here",
3659			     olddecl);
3660	    }
3661	}
3662    }
3663
3664  /* Do not merge an implicit typedef with an explicit one.  In:
3665
3666       class A;
3667       ...
3668       typedef class A A __attribute__ ((foo));
3669
3670     the attribute should apply only to the typedef.  */
3671  if (TREE_CODE (olddecl) == TYPE_DECL
3672      && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3673	  || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3674    return 0;
3675
3676  /* If new decl is `static' and an `extern' was seen previously,
3677     warn about it.  */
3678  warn_extern_redeclared_static (newdecl, olddecl);
3679
3680  /* We have committed to returning 1 at this point.  */
3681  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3682    {
3683      /* Now that functions must hold information normally held
3684	 by field decls, there is extra work to do so that
3685	 declaration information does not get destroyed during
3686	 definition.  */
3687      if (DECL_VINDEX (olddecl))
3688	DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3689      if (DECL_CONTEXT (olddecl))
3690	DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3691      DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3692      DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3693      DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3694      DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3695      DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3696      DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3697      if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3698	SET_OVERLOADED_OPERATOR_CODE
3699	  (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3700      new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3701
3702      /* Optionally warn about more than one declaration for the same
3703         name, but don't warn about a function declaration followed by a
3704         definition.  */
3705      if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3706	  && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3707	  /* Don't warn about extern decl followed by definition.  */
3708	  && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3709	  /* Don't warn about friends, let add_friend take care of it.  */
3710	  && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3711	{
3712	  warning ("redundant redeclaration of `%D' in same scope", newdecl);
3713	  cp_warning_at ("previous declaration of `%D'", olddecl);
3714	}
3715    }
3716
3717  /* Deal with C++: must preserve virtual function table size.  */
3718  if (TREE_CODE (olddecl) == TYPE_DECL)
3719    {
3720      register tree newtype = TREE_TYPE (newdecl);
3721      register tree oldtype = TREE_TYPE (olddecl);
3722
3723      if (newtype != error_mark_node && oldtype != error_mark_node
3724	  && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3725	CLASSTYPE_FRIEND_CLASSES (newtype)
3726	  = CLASSTYPE_FRIEND_CLASSES (oldtype);
3727\
3728      DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3729    }
3730
3731  /* Copy all the DECL_... slots specified in the new decl
3732     except for any that we copy here from the old type.  */
3733  DECL_ATTRIBUTES (newdecl)
3734    = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3735
3736  if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3737    {
3738      TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3739      DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3740	= chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3741		   DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3742
3743      /* If the new declaration is a definition, update the file and
3744	 line information on the declaration.  */
3745      if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3746	  && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3747	{
3748	  DECL_SOURCE_LOCATION (olddecl)
3749	    = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3750	    = DECL_SOURCE_LOCATION (newdecl);
3751	}
3752
3753      if (DECL_FUNCTION_TEMPLATE_P (newdecl))
3754	{
3755	  DECL_INLINE (DECL_TEMPLATE_RESULT (olddecl))
3756	    |= DECL_INLINE (DECL_TEMPLATE_RESULT (newdecl));
3757	  DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (olddecl))
3758	    |= DECL_DECLARED_INLINE_P (DECL_TEMPLATE_RESULT (newdecl));
3759	}
3760
3761      return 1;
3762    }
3763
3764  if (types_match)
3765    {
3766      /* Automatically handles default parameters.  */
3767      tree oldtype = TREE_TYPE (olddecl);
3768      tree newtype;
3769
3770      /* Merge the data types specified in the two decls.  */
3771      newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3772
3773      /* If merge_types produces a non-typedef type, just use the old type.  */
3774      if (TREE_CODE (newdecl) == TYPE_DECL
3775	  && newtype == DECL_ORIGINAL_TYPE (newdecl))
3776	newtype = oldtype;
3777
3778      if (TREE_CODE (newdecl) == VAR_DECL)
3779	{
3780	  DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3781	  DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3782	}
3783
3784      /* Do this after calling `merge_types' so that default
3785	 parameters don't confuse us.  */
3786      else if (TREE_CODE (newdecl) == FUNCTION_DECL
3787	  && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3788	      != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3789	{
3790	  TREE_TYPE (newdecl) = build_exception_variant (newtype,
3791							 TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3792	  TREE_TYPE (olddecl) = build_exception_variant (newtype,
3793							 TYPE_RAISES_EXCEPTIONS (oldtype));
3794
3795	  if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3796	      && DECL_SOURCE_LINE (olddecl) != 0
3797	      && flag_exceptions
3798	      && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3799	                             TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3800	    {
3801	      error ("declaration of `%F' throws different exceptions",
3802			newdecl);
3803	      cp_error_at ("than previous declaration `%F'", olddecl);
3804	    }
3805	}
3806      TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3807
3808      /* Lay the type out, unless already done.  */
3809      if (! same_type_p (newtype, oldtype)
3810	  && TREE_TYPE (newdecl) != error_mark_node
3811	  && !(processing_template_decl && uses_template_parms (newdecl)))
3812	layout_type (TREE_TYPE (newdecl));
3813
3814      if ((TREE_CODE (newdecl) == VAR_DECL
3815	   || TREE_CODE (newdecl) == PARM_DECL
3816	   || TREE_CODE (newdecl) == RESULT_DECL
3817	   || TREE_CODE (newdecl) == FIELD_DECL
3818	   || TREE_CODE (newdecl) == TYPE_DECL)
3819	  && !(processing_template_decl && uses_template_parms (newdecl)))
3820	layout_decl (newdecl, 0);
3821
3822      /* Merge the type qualifiers.  */
3823      if (TREE_READONLY (newdecl))
3824	TREE_READONLY (olddecl) = 1;
3825      if (TREE_THIS_VOLATILE (newdecl))
3826	TREE_THIS_VOLATILE (olddecl) = 1;
3827
3828      /* Merge the initialization information.  */
3829      if (DECL_INITIAL (newdecl) == NULL_TREE
3830	  && DECL_INITIAL (olddecl) != NULL_TREE)
3831	{
3832	  DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3833	  DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3834	  if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3835	      && DECL_LANG_SPECIFIC (newdecl)
3836	      && DECL_LANG_SPECIFIC (olddecl))
3837	    DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3838	}
3839
3840      /* Merge the section attribute.
3841         We want to issue an error if the sections conflict but that must be
3842	 done later in decl_attributes since we are called before attributes
3843	 are assigned.  */
3844      if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3845	DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3846
3847      if (TREE_CODE (newdecl) == FUNCTION_DECL)
3848	{
3849	  DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3850	    |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3851	  DECL_NO_LIMIT_STACK (newdecl)
3852	    |= DECL_NO_LIMIT_STACK (olddecl);
3853	  /* Keep the old RTL.  */
3854	  COPY_DECL_RTL (olddecl, newdecl);
3855	}
3856      else if (TREE_CODE (newdecl) == VAR_DECL
3857	       && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3858	{
3859	  /* Keep the old RTL.  We cannot keep the old RTL if the old
3860	     declaration was for an incomplete object and the new
3861	     declaration is not since many attributes of the RTL will
3862	     change.  */
3863	  COPY_DECL_RTL (olddecl, newdecl);
3864	}
3865    }
3866  /* If cannot merge, then use the new type and qualifiers,
3867     and don't preserve the old rtl.  */
3868  else
3869    {
3870      /* Clean out any memory we had of the old declaration.  */
3871      tree oldstatic = value_member (olddecl, static_aggregates);
3872      if (oldstatic)
3873	TREE_VALUE (oldstatic) = error_mark_node;
3874
3875      TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3876      TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3877      TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3878      TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3879    }
3880
3881  /* Merge the storage class information.  */
3882  merge_weak (newdecl, olddecl);
3883
3884  DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3885  DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3886  TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3887  TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3888  if (! DECL_EXTERNAL (olddecl))
3889    DECL_EXTERNAL (newdecl) = 0;
3890
3891  if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3892    {
3893      DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3894      DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3895      DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3896      DECL_TEMPLATE_INSTANTIATED (newdecl)
3897	|= DECL_TEMPLATE_INSTANTIATED (olddecl);
3898      /* Don't really know how much of the language-specific
3899	 values we should copy from old to new.  */
3900      DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3901      DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 =
3902	DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3903      DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3904      DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3905      DECL_INITIALIZED_IN_CLASS_P (newdecl)
3906        |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3907      olddecl_friend = DECL_FRIEND_P (olddecl);
3908
3909      /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3910      if (TREE_CODE (newdecl) == FUNCTION_DECL
3911	  || DECL_FUNCTION_TEMPLATE_P (newdecl))
3912	{
3913	  DECL_BEFRIENDING_CLASSES (newdecl)
3914	    = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3915		       DECL_BEFRIENDING_CLASSES (olddecl));
3916	  /* DECL_THUNKS is only valid for virtual functions,
3917	     otherwise it is a DECL_FRIEND_CONTEXT.  */
3918	  if (DECL_VIRTUAL_P (newdecl))
3919	    DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3920	}
3921    }
3922
3923  if (TREE_CODE (newdecl) == FUNCTION_DECL)
3924    {
3925      if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3926	  && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3927	{
3928	  /* If newdecl is not a specialization, then it is not a
3929	     template-related function at all.  And that means that we
3930	     shoud have exited above, returning 0.  */
3931	  my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3932			      0);
3933
3934	  if (TREE_USED (olddecl))
3935	    /* From [temp.expl.spec]:
3936
3937	       If a template, a member template or the member of a class
3938	       template is explicitly specialized then that
3939	       specialization shall be declared before the first use of
3940	       that specialization that would cause an implicit
3941	       instantiation to take place, in every translation unit in
3942	       which such a use occurs.  */
3943	    error ("explicit specialization of %D after first use",
3944		      olddecl);
3945
3946	  SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3947
3948	  /* [temp.expl.spec/14] We don't inline explicit specialization
3949	     just because the primary template says so.  */
3950	}
3951      else
3952	{
3953	  if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3954	    DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3955
3956	  DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3957
3958	  /* If either decl says `inline', this fn is inline, unless
3959	     its definition was passed already.  */
3960	  if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3961	    DECL_INLINE (olddecl) = 1;
3962	  DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3963
3964	  DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3965	    = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3966	}
3967
3968      /* Preserve abstractness on cloned [cd]tors.  */
3969      DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3970
3971      if (! types_match)
3972	{
3973	  SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3974	  COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3975	  SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3976	}
3977      if (! types_match || new_defines_function)
3978	{
3979	  /* These need to be copied so that the names are available.
3980	     Note that if the types do match, we'll preserve inline
3981	     info and other bits, but if not, we won't.  */
3982	  DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3983	  DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3984	}
3985      if (new_defines_function)
3986	/* If defining a function declared with other language
3987	   linkage, use the previously declared language linkage.  */
3988	SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3989      else if (types_match)
3990	{
3991	  /* If redeclaring a builtin function, and not a definition,
3992	     it stays built in.  */
3993	  if (DECL_BUILT_IN (olddecl))
3994	    {
3995	      DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3996	      DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3997	      /* If we're keeping the built-in definition, keep the rtl,
3998		 regardless of declaration matches.  */
3999	      SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
4000	    }
4001	  else
4002	    DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
4003
4004	  DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
4005	  /* Don't clear out the arguments if we're redefining a function.  */
4006	  if (DECL_ARGUMENTS (olddecl))
4007	    DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
4008	}
4009    }
4010  else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
4011    NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
4012
4013  /* Now preserve various other info from the definition.  */
4014  TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
4015  TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
4016  DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
4017  COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
4018
4019  if (TREE_CODE (newdecl) == FUNCTION_DECL)
4020    {
4021      int function_size;
4022
4023      function_size = sizeof (struct tree_decl);
4024
4025      memcpy ((char *) olddecl + sizeof (struct tree_common),
4026	      (char *) newdecl + sizeof (struct tree_common),
4027	      function_size - sizeof (struct tree_common));
4028
4029      if (DECL_TEMPLATE_INSTANTIATION (newdecl))
4030	{
4031	  /* If newdecl is a template instantiation, it is possible that
4032	     the following sequence of events has occurred:
4033
4034	     o A friend function was declared in a class template.  The
4035	     class template was instantiated.
4036
4037	     o The instantiation of the friend declaration was
4038	     recorded on the instantiation list, and is newdecl.
4039
4040	     o Later, however, instantiate_class_template called pushdecl
4041	     on the newdecl to perform name injection.  But, pushdecl in
4042	     turn called duplicate_decls when it discovered that another
4043	     declaration of a global function with the same name already
4044	     existed.
4045
4046	     o Here, in duplicate_decls, we decided to clobber newdecl.
4047
4048	     If we're going to do that, we'd better make sure that
4049	     olddecl, and not newdecl, is on the list of
4050	     instantiations so that if we try to do the instantiation
4051	     again we won't get the clobbered declaration.  */
4052
4053	  tree tmpl = DECL_TI_TEMPLATE (newdecl);
4054	  tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
4055
4056	  for (; decls; decls = TREE_CHAIN (decls))
4057	    if (TREE_VALUE (decls) == newdecl)
4058	      TREE_VALUE (decls) = olddecl;
4059	}
4060    }
4061  else
4062    {
4063      memcpy ((char *) olddecl + sizeof (struct tree_common),
4064	      (char *) newdecl + sizeof (struct tree_common),
4065	      sizeof (struct tree_decl) - sizeof (struct tree_common)
4066	      + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
4067    }
4068
4069  DECL_UID (olddecl) = olddecl_uid;
4070  if (olddecl_friend)
4071    DECL_FRIEND_P (olddecl) = 1;
4072
4073  /* NEWDECL contains the merged attribute lists.
4074     Update OLDDECL to be the same.  */
4075  DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
4076
4077  /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
4078     so that encode_section_info has a chance to look at the new decl
4079     flags and attributes.  */
4080  if (DECL_RTL_SET_P (olddecl)
4081      && (TREE_CODE (olddecl) == FUNCTION_DECL
4082	  || (TREE_CODE (olddecl) == VAR_DECL
4083	      && TREE_STATIC (olddecl))))
4084    make_decl_rtl (olddecl, NULL);
4085
4086  return 1;
4087}
4088
4089/* Record a decl-node X as belonging to the current lexical scope.
4090   Check for errors (such as an incompatible declaration for the same
4091   name already seen in the same scope).
4092
4093   Returns either X or an old decl for the same name.
4094   If an old decl is returned, it may have been smashed
4095   to agree with what X says.  */
4096
4097tree
4098pushdecl (x)
4099     tree x;
4100{
4101  register tree t;
4102  register tree name;
4103  int need_new_binding;
4104
4105  timevar_push (TV_NAME_LOOKUP);
4106
4107  /* We shouldn't be calling pushdecl when we're generating RTL for a
4108     function that we already did semantic analysis on previously.  */
4109  my_friendly_assert (!cfun || doing_semantic_analysis_p (),
4110		      19990913);
4111
4112  need_new_binding = 1;
4113
4114  if (DECL_TEMPLATE_PARM_P (x))
4115    /* Template parameters have no context; they are not X::T even
4116       when declared within a class or namespace.  */
4117    ;
4118  else
4119    {
4120      if (current_function_decl && x != current_function_decl
4121	  /* A local declaration for a function doesn't constitute
4122             nesting.  */
4123	  && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
4124	  /* A local declaration for an `extern' variable is in the
4125	     scope of the current namespace, not the current
4126	     function.  */
4127	  && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
4128	  && !DECL_CONTEXT (x))
4129	DECL_CONTEXT (x) = current_function_decl;
4130
4131      /* If this is the declaration for a namespace-scope function,
4132	 but the declaration itself is in a local scope, mark the
4133	 declaration.  */
4134      if (TREE_CODE (x) == FUNCTION_DECL
4135	  && DECL_NAMESPACE_SCOPE_P (x)
4136	  && current_function_decl
4137	  && x != current_function_decl)
4138	DECL_LOCAL_FUNCTION_P (x) = 1;
4139    }
4140
4141  name = DECL_NAME (x);
4142  if (name)
4143    {
4144      int different_binding_level = 0;
4145
4146      if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
4147	name = TREE_OPERAND (name, 0);
4148
4149      /* In case this decl was explicitly namespace-qualified, look it
4150	 up in its namespace context.  */
4151      if ((DECL_CONTEXT (x) && TREE_CODE (DECL_CONTEXT (x)) == NAMESPACE_DECL)
4152          && namespace_bindings_p ())
4153        t = namespace_binding (name, DECL_CONTEXT (x));
4154      else
4155	t = lookup_name_current_level (name);
4156
4157      /* [basic.link] If there is a visible declaration of an entity
4158	 with linkage having the same name and type, ignoring entities
4159	 declared outside the innermost enclosing namespace scope, the
4160	 block scope declaration declares that same entity and
4161	 receives the linkage of the previous declaration.  */
4162      if (! t && current_function_decl && x != current_function_decl
4163	  && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
4164	  && DECL_EXTERNAL (x))
4165	{
4166	  /* Look in block scope.  */
4167	  t = IDENTIFIER_VALUE (name);
4168	  /* Or in the innermost namespace.  */
4169	  if (! t)
4170	    t = namespace_binding (name, DECL_CONTEXT (x));
4171	  /* Does it have linkage?  Note that if this isn't a DECL, it's an
4172	     OVERLOAD, which is OK.  */
4173	  if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
4174	    t = NULL_TREE;
4175	  if (t)
4176	    different_binding_level = 1;
4177	}
4178
4179      /* If we are declaring a function, and the result of name-lookup
4180	 was an OVERLOAD, look for an overloaded instance that is
4181	 actually the same as the function we are declaring.  (If
4182	 there is one, we have to merge our declaration with the
4183	 previous declaration.)  */
4184      if (t && TREE_CODE (t) == OVERLOAD)
4185	{
4186	  tree match;
4187
4188	  if (TREE_CODE (x) == FUNCTION_DECL)
4189	    for (match = t; match; match = OVL_NEXT (match))
4190	      {
4191		if (decls_match (OVL_CURRENT (match), x))
4192		  break;
4193	      }
4194	  else
4195	    /* Just choose one.  */
4196	    match = t;
4197
4198	  if (match)
4199	    t = OVL_CURRENT (match);
4200	  else
4201	    t = NULL_TREE;
4202	}
4203
4204      if (t == error_mark_node)
4205	{
4206	  /* error_mark_node is 0 for a while during initialization!  */
4207	  t = NULL_TREE;
4208	  cp_error_at ("`%#D' used prior to declaration", x);
4209	}
4210      else if (t != NULL_TREE)
4211	{
4212	  if (different_binding_level)
4213	    {
4214	      if (decls_match (x, t))
4215		/* The standard only says that the local extern
4216		   inherits linkage from the previous decl; in
4217		   particular, default args are not shared.  It would
4218		   be nice to propagate inlining info, though.  FIXME.  */
4219		TREE_PUBLIC (x) = TREE_PUBLIC (t);
4220	    }
4221	  else if (TREE_CODE (t) == PARM_DECL)
4222	    {
4223	      if (DECL_CONTEXT (t) == NULL_TREE)
4224		/* This is probaby caused by too many errors, but calling
4225		   abort will say that if errors have occurred.  */
4226		abort ();
4227
4228	      /* Check for duplicate params.  */
4229	      if (duplicate_decls (x, t))
4230		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4231	    }
4232	  else if ((DECL_EXTERN_C_FUNCTION_P (x)
4233		    || DECL_FUNCTION_TEMPLATE_P (x))
4234		   && is_overloaded_fn (t))
4235	    /* Don't do anything just yet.  */;
4236	  else if (t == wchar_decl_node)
4237	    {
4238	      if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
4239		pedwarn ("redeclaration of `wchar_t' as `%T'",
4240			    TREE_TYPE (x));
4241
4242	      /* Throw away the redeclaration.  */
4243	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4244	    }
4245	  else if (TREE_CODE (t) != TREE_CODE (x))
4246	    {
4247	      if (duplicate_decls (x, t))
4248		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4249	    }
4250	  else if (duplicate_decls (x, t))
4251	    {
4252	      if (TREE_CODE (t) == TYPE_DECL)
4253		SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
4254	      else if (TREE_CODE (t) == FUNCTION_DECL)
4255		check_default_args (t);
4256
4257	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4258	    }
4259	  else if (DECL_MAIN_P (x))
4260	    {
4261	      /* A redeclaration of main, but not a duplicate of the
4262		 previous one.
4263
4264		 [basic.start.main]
4265
4266	         This function shall not be overloaded.  */
4267	      cp_error_at ("invalid redeclaration of `%D'", t);
4268	      error ("as `%D'", x);
4269	      /* We don't try to push this declaration since that
4270		 causes a crash.  */
4271	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4272	    }
4273	}
4274
4275      check_template_shadow (x);
4276
4277      /* If this is a function conjured up by the backend, massage it
4278	 so it looks friendly.  */
4279      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
4280	{
4281	  retrofit_lang_decl (x);
4282	  SET_DECL_LANGUAGE (x, lang_c);
4283	}
4284
4285      if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
4286	{
4287	  t = push_overloaded_decl (x, PUSH_LOCAL);
4288	  if (t != x)
4289	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4290	  if (!namespace_bindings_p ())
4291	    /* We do not need to create a binding for this name;
4292	       push_overloaded_decl will have already done so if
4293	       necessary.  */
4294	    need_new_binding = 0;
4295	}
4296      else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
4297	{
4298	  t = push_overloaded_decl (x, PUSH_GLOBAL);
4299	  if (t == x)
4300	    add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
4301	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4302	}
4303
4304      /* If declaring a type as a typedef, copy the type (unless we're
4305	 at line 0), and install this TYPE_DECL as the new type's typedef
4306	 name.  See the extensive comment in ../c-decl.c (pushdecl).  */
4307      if (TREE_CODE (x) == TYPE_DECL)
4308	{
4309	  tree type = TREE_TYPE (x);
4310	  if (DECL_SOURCE_LINE (x) == 0)
4311            {
4312	      if (TYPE_NAME (type) == 0)
4313	        TYPE_NAME (type) = x;
4314            }
4315          else if (type != error_mark_node && TYPE_NAME (type) != x
4316		   /* We don't want to copy the type when all we're
4317		      doing is making a TYPE_DECL for the purposes of
4318		      inlining.  */
4319		   && (!TYPE_NAME (type)
4320		       || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
4321            {
4322	      DECL_ORIGINAL_TYPE (x) = type;
4323              type = build_type_copy (type);
4324	      TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
4325              TYPE_NAME (type) = x;
4326              TREE_TYPE (x) = type;
4327            }
4328
4329	  if (type != error_mark_node
4330	      && TYPE_NAME (type)
4331	      && TYPE_IDENTIFIER (type))
4332            set_identifier_type_value_with_scope (DECL_NAME (x), type,
4333						  current_binding_level);
4334
4335	}
4336
4337      /* Multiple external decls of the same identifier ought to match.
4338
4339	 We get warnings about inline functions where they are defined.
4340	 We get warnings about other functions from push_overloaded_decl.
4341
4342	 Avoid duplicate warnings where they are used.  */
4343      if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
4344	{
4345	  tree decl;
4346
4347	  decl = IDENTIFIER_NAMESPACE_VALUE (name);
4348	  if (decl && TREE_CODE (decl) == OVERLOAD)
4349	    decl = OVL_FUNCTION (decl);
4350
4351	  if (decl && decl != error_mark_node
4352	      && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
4353	      /* If different sort of thing, we already gave an error.  */
4354	      && TREE_CODE (decl) == TREE_CODE (x)
4355	      && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
4356	    {
4357	      pedwarn ("type mismatch with previous external decl", x);
4358	      cp_pedwarn_at ("previous external decl of `%#D'", decl);
4359	    }
4360	}
4361
4362      /* This name is new in its binding level.
4363	 Install the new declaration and return it.  */
4364      if (namespace_bindings_p ())
4365	{
4366	  /* Install a global value.  */
4367
4368	  /* If the first global decl has external linkage,
4369	     warn if we later see static one.  */
4370	  if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
4371	    TREE_PUBLIC (name) = 1;
4372
4373 	  /* Bind the name for the entity.  */
4374 	  if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
4375  		&& t != NULL_TREE)
4376 	      && (TREE_CODE (x) == TYPE_DECL
4377 		  || TREE_CODE (x) == VAR_DECL
4378 		  || TREE_CODE (x) == NAMESPACE_DECL
4379 		  || TREE_CODE (x) == CONST_DECL
4380 		  || TREE_CODE (x) == TEMPLATE_DECL))
4381 	    SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
4382
4383	  /* Don't forget if the function was used via an implicit decl.  */
4384	  if (IDENTIFIER_IMPLICIT_DECL (name)
4385	      && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
4386	    TREE_USED (x) = 1;
4387
4388	  /* Don't forget if its address was taken in that way.  */
4389	  if (IDENTIFIER_IMPLICIT_DECL (name)
4390	      && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
4391	    TREE_ADDRESSABLE (x) = 1;
4392
4393	  /* Warn about mismatches against previous implicit decl.  */
4394	  if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4395	      /* If this real decl matches the implicit, don't complain.  */
4396	      && ! (TREE_CODE (x) == FUNCTION_DECL
4397		    && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
4398	    warning
4399	      ("`%D' was previously implicitly declared to return `int'", x);
4400
4401	  /* If new decl is `static' and an `extern' was seen previously,
4402	     warn about it.  */
4403	  if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4404	    warn_extern_redeclared_static (x, t);
4405	}
4406      else
4407	{
4408	  /* Here to install a non-global value.  */
4409	  tree oldlocal = IDENTIFIER_VALUE (name);
4410	  tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
4411
4412	  if (need_new_binding)
4413	    {
4414	      push_local_binding (name, x, 0);
4415	      /* Because push_local_binding will hook X on to the
4416		 current_binding_level's name list, we don't want to
4417		 do that again below.  */
4418	      need_new_binding = 0;
4419	    }
4420
4421	  /* If this is a TYPE_DECL, push it into the type value slot.  */
4422	  if (TREE_CODE (x) == TYPE_DECL)
4423	    set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4424						  current_binding_level);
4425
4426	  /* Clear out any TYPE_DECL shadowed by a namespace so that
4427	     we won't think this is a type.  The C struct hack doesn't
4428	     go through namespaces.  */
4429	  if (TREE_CODE (x) == NAMESPACE_DECL)
4430	    set_identifier_type_value_with_scope (name, NULL_TREE,
4431						  current_binding_level);
4432
4433	  if (oldlocal)
4434	    {
4435	      tree d = oldlocal;
4436
4437	      while (oldlocal
4438		     && TREE_CODE (oldlocal) == VAR_DECL
4439		     && DECL_DEAD_FOR_LOCAL (oldlocal))
4440		oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4441
4442	      if (oldlocal == NULL_TREE)
4443		oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4444	    }
4445
4446	  /* If this is an extern function declaration, see if we
4447	     have a global definition or declaration for the function.  */
4448	  if (oldlocal == NULL_TREE
4449	      && DECL_EXTERNAL (x)
4450	      && oldglobal != NULL_TREE
4451	      && TREE_CODE (x) == FUNCTION_DECL
4452	      && TREE_CODE (oldglobal) == FUNCTION_DECL)
4453	    {
4454	      /* We have one.  Their types must agree.  */
4455	      if (decls_match (x, oldglobal))
4456		/* OK */;
4457	      else
4458		{
4459		  warning ("extern declaration of `%#D' doesn't match", x);
4460		  cp_warning_at ("global declaration `%#D'", oldglobal);
4461		}
4462	    }
4463	  /* If we have a local external declaration,
4464	     and no file-scope declaration has yet been seen,
4465	     then if we later have a file-scope decl it must not be static.  */
4466	  if (oldlocal == NULL_TREE
4467	      && oldglobal == NULL_TREE
4468	      && DECL_EXTERNAL (x)
4469	      && TREE_PUBLIC (x))
4470	    TREE_PUBLIC (name) = 1;
4471
4472	  /* Warn if shadowing an argument at the top level of the body.  */
4473	  if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4474	      /* Inline decls shadow nothing.  */
4475	      && !DECL_FROM_INLINE (x)
4476	      && TREE_CODE (oldlocal) == PARM_DECL
4477	      /* Don't check the `this' parameter.  */
4478	      && !DECL_ARTIFICIAL (oldlocal))
4479	    {
4480	      bool err = false;
4481
4482	      /* Don't complain if it's from an enclosing function.  */
4483	      if (DECL_CONTEXT (oldlocal) == current_function_decl
4484		  && TREE_CODE (x) != PARM_DECL)
4485		{
4486		  /* Go to where the parms should be and see if we find
4487		     them there.  */
4488		  struct cp_binding_level *b = current_binding_level->level_chain;
4489
4490		  /* ARM $8.3 */
4491		  if (b->parm_flag == 1)
4492		    {
4493		      error ("declaration of `%#D' shadows a parameter",
4494				name);
4495		      err = true;
4496		    }
4497		}
4498
4499	      if (warn_shadow && !err)
4500		shadow_warning ("a parameter", name, oldlocal);
4501	    }
4502
4503	  /* Maybe warn if shadowing something else.  */
4504	  else if (warn_shadow && !DECL_EXTERNAL (x)
4505	      /* No shadow warnings for internally generated vars.  */
4506	      && ! DECL_ARTIFICIAL (x)
4507	      /* No shadow warnings for vars made for inlining.  */
4508	      && ! DECL_FROM_INLINE (x))
4509	    {
4510	      if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4511		       && current_class_ptr
4512		       && !TREE_STATIC (name))
4513		warning ("declaration of `%s' shadows a member of `this'",
4514			    IDENTIFIER_POINTER (name));
4515	      else if (oldlocal != NULL_TREE
4516		       && TREE_CODE (oldlocal) == VAR_DECL)
4517		shadow_warning ("a previous local", name, oldlocal);
4518	      else if (oldglobal != NULL_TREE
4519		       && TREE_CODE (oldglobal) == VAR_DECL)
4520		/* XXX shadow warnings in outer-more namespaces */
4521		shadow_warning ("a global declaration", name, oldglobal);
4522	    }
4523	}
4524
4525      if (TREE_CODE (x) == FUNCTION_DECL)
4526	check_default_args (x);
4527
4528      if (TREE_CODE (x) == VAR_DECL)
4529	maybe_register_incomplete_var (x);
4530    }
4531
4532  if (need_new_binding)
4533    add_decl_to_level (x,
4534		       DECL_NAMESPACE_SCOPE_P (x)
4535		       ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4536		       : current_binding_level);
4537
4538  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4539}
4540
4541/* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4542   caller to set DECL_CONTEXT properly.  */
4543
4544static tree
4545pushdecl_with_scope (x, level)
4546     tree x;
4547     struct cp_binding_level *level;
4548{
4549  register struct cp_binding_level *b;
4550  tree function_decl = current_function_decl;
4551
4552  timevar_push (TV_NAME_LOOKUP);
4553
4554  current_function_decl = NULL_TREE;
4555  if (level->parm_flag == 2)
4556    {
4557      b = class_binding_level;
4558      class_binding_level = level;
4559      pushdecl_class_level (x);
4560      class_binding_level = b;
4561    }
4562  else
4563    {
4564      b = current_binding_level;
4565      current_binding_level = level;
4566      x = pushdecl (x);
4567      current_binding_level = b;
4568    }
4569  current_function_decl = function_decl;
4570  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4571}
4572
4573/* Like pushdecl, only it places X in the current namespace,
4574   if appropriate.  */
4575
4576tree
4577pushdecl_namespace_level (x)
4578     tree x;
4579{
4580  register struct cp_binding_level *b = current_binding_level;
4581  register tree t;
4582
4583  timevar_push (TV_NAME_LOOKUP);
4584  t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4585
4586  /* Now, the type_shadowed stack may screw us.  Munge it so it does
4587     what we want.  */
4588  if (TREE_CODE (x) == TYPE_DECL)
4589    {
4590      tree name = DECL_NAME (x);
4591      tree newval;
4592      tree *ptr = (tree *)0;
4593      for (; !global_scope_p (b); b = b->level_chain)
4594        {
4595          tree shadowed = b->type_shadowed;
4596          for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4597            if (TREE_PURPOSE (shadowed) == name)
4598              {
4599		ptr = &TREE_VALUE (shadowed);
4600		/* Can't break out of the loop here because sometimes
4601		   a binding level will have duplicate bindings for
4602		   PT names.  It's gross, but I haven't time to fix it.  */
4603              }
4604        }
4605      newval = TREE_TYPE (x);
4606      if (ptr == (tree *)0)
4607        {
4608          /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4609             up here if this is changed to an assertion.  --KR  */
4610	  SET_IDENTIFIER_TYPE_VALUE (name, newval);
4611	}
4612      else
4613        {
4614	  *ptr = newval;
4615        }
4616    }
4617  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4618}
4619
4620/* Like pushdecl, only it places X in the global scope if appropriate.
4621   Calls cp_finish_decl to register the variable, initializing it with
4622   *INIT, if INIT is non-NULL.  */
4623
4624static tree
4625pushdecl_top_level_1 (tree x, tree *init)
4626{
4627  timevar_push (TV_NAME_LOOKUP);
4628  push_to_top_level ();
4629  x = pushdecl_namespace_level (x);
4630  if (init)
4631    cp_finish_decl (x, *init, NULL_TREE, 0);
4632  pop_from_top_level ();
4633  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4634}
4635
4636/* Like pushdecl, only it places X in the global scope if appropriate.  */
4637
4638tree
4639pushdecl_top_level (tree x)
4640{
4641  return pushdecl_top_level_1 (x, NULL);
4642}
4643
4644/* Like pushdecl, only it places X in the global scope if
4645   appropriate.  Calls cp_finish_decl to register the variable,
4646   initializing it with INIT.  */
4647
4648tree
4649pushdecl_top_level_and_finish (tree x, tree init)
4650{
4651  return pushdecl_top_level_1 (x, &init);
4652}
4653
4654/* Make the declaration of X appear in CLASS scope.  */
4655
4656bool
4657pushdecl_class_level (x)
4658     tree x;
4659{
4660  tree name;
4661  bool is_valid = true;
4662
4663  timevar_push (TV_NAME_LOOKUP);
4664  /* Get the name of X.  */
4665  if (TREE_CODE (x) == OVERLOAD)
4666    name = DECL_NAME (get_first_fn (x));
4667  else
4668    name = DECL_NAME (x);
4669
4670  if (name)
4671    {
4672      is_valid = push_class_level_binding (name, x);
4673      if (TREE_CODE (x) == TYPE_DECL)
4674	set_identifier_type_value (name, TREE_TYPE (x));
4675    }
4676  else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4677    {
4678      /* If X is an anonymous aggregate, all of its members are
4679	 treated as if they were members of the class containing the
4680	 aggregate, for naming purposes.  */
4681      tree f;
4682
4683      for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4684	{
4685	  push_srcloc (DECL_SOURCE_FILE (f), DECL_SOURCE_LINE (f));
4686	  if (!pushdecl_class_level (f))
4687	    is_valid = false;
4688	  pop_srcloc ();
4689	}
4690    }
4691  timevar_pop (TV_NAME_LOOKUP);
4692
4693  return is_valid;
4694}
4695
4696/* Enter DECL into the symbol table, if that's appropriate.  Returns
4697   DECL, or a modified version thereof.  */
4698
4699tree
4700maybe_push_decl (decl)
4701     tree decl;
4702{
4703  tree type = TREE_TYPE (decl);
4704
4705  /* Add this decl to the current binding level, but not if it comes
4706     from another scope, e.g. a static member variable.  TEM may equal
4707     DECL or it may be a previous decl of the same name.  */
4708  if (decl == error_mark_node
4709      || (TREE_CODE (decl) != PARM_DECL
4710	  && DECL_CONTEXT (decl) != NULL_TREE
4711	  /* Definitions of namespace members outside their namespace are
4712	     possible.  */
4713	  && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4714      || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4715      || TREE_CODE (type) == UNKNOWN_TYPE
4716      /* The declaration of a template specialization does not affect
4717	 the functions available for overload resolution, so we do not
4718	 call pushdecl.  */
4719      || (TREE_CODE (decl) == FUNCTION_DECL
4720	  && DECL_TEMPLATE_SPECIALIZATION (decl)))
4721    return decl;
4722  else
4723    return pushdecl (decl);
4724}
4725
4726/* Make the declaration(s) of X appear in CLASS scope under the name
4727   NAME.  Returns true if the binding is valid.  */
4728
4729bool
4730push_class_level_binding (tree name, tree x)
4731{
4732  cxx_binding *binding;
4733
4734  timevar_push (TV_NAME_LOOKUP);
4735  /* The class_binding_level will be NULL if x is a template
4736     parameter name in a member template.  */
4737  if (!class_binding_level)
4738    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4739
4740  /* Make sure that this new member does not have the same name
4741     as a template parameter.  */
4742  if (TYPE_BEING_DEFINED (current_class_type))
4743    check_template_shadow (x);
4744
4745  /* If this declaration shadows a declaration from an enclosing
4746     class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4747     we leave this class.  Record the shadowed declaration here.  */
4748  binding = IDENTIFIER_BINDING (name);
4749  if (binding
4750      && ((TREE_CODE (x) == OVERLOAD
4751	   && BINDING_VALUE (binding)
4752	   && is_overloaded_fn (BINDING_VALUE (binding)))
4753	  || INHERITED_VALUE_BINDING_P (binding)))
4754    {
4755      tree shadow;
4756      tree old_decl;
4757
4758      /* If the old binding was from a base class, and was for a tag
4759	 name, slide it over to make room for the new binding.  The
4760	 old binding is still visible if explicitly qualified with a
4761	 class-key.  */
4762      if (INHERITED_VALUE_BINDING_P (binding)
4763	  && BINDING_VALUE (binding)
4764	  && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4765	  && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4766	  && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4767	{
4768	  old_decl = BINDING_TYPE (binding);
4769	  BINDING_TYPE (binding) = BINDING_VALUE (binding);
4770	  BINDING_VALUE (binding) = NULL_TREE;
4771	  INHERITED_VALUE_BINDING_P (binding) = 0;
4772	}
4773      else
4774	old_decl = BINDING_VALUE (binding);
4775
4776      /* Find the previous binding of name on the class-shadowed
4777         list, and update it.  */
4778      for (shadow = class_binding_level->class_shadowed;
4779	   shadow;
4780	   shadow = TREE_CHAIN (shadow))
4781	if (TREE_PURPOSE (shadow) == name
4782	    && TREE_TYPE (shadow) == old_decl)
4783	  {
4784	    BINDING_VALUE (binding) = x;
4785	    INHERITED_VALUE_BINDING_P (binding) = 0;
4786	    TREE_TYPE (shadow) = x;
4787	    IDENTIFIER_CLASS_VALUE (name) = x;
4788	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4789	  }
4790    }
4791
4792  /* If we didn't replace an existing binding, put the binding on the
4793     stack of bindings for the identifier, and update the shadowed list.  */
4794  if (push_class_binding (name, x))
4795    {
4796      class_binding_level->class_shadowed
4797	= tree_cons (name, NULL,
4798		     class_binding_level->class_shadowed);
4799      /* Record the value we are binding NAME to so that we can know
4800	 what to pop later.  */
4801      TREE_TYPE (class_binding_level->class_shadowed) = x;
4802      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, true);
4803    }
4804
4805  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, false);
4806}
4807
4808/* Insert another USING_DECL into the current binding level, returning
4809   this declaration. If this is a redeclaration, do nothing, and
4810   return NULL_TREE if this not in namespace scope (in namespace
4811   scope, a using decl might extend any previous bindings).  */
4812
4813tree
4814push_using_decl (scope, name)
4815     tree scope;
4816     tree name;
4817{
4818  tree decl;
4819
4820  timevar_push (TV_NAME_LOOKUP);
4821
4822  my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4823  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4824  for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4825    if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4826      break;
4827  if (decl)
4828    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4829			    namespace_bindings_p () ? decl : NULL_TREE);
4830  decl = build_lang_decl (USING_DECL, name, void_type_node);
4831  DECL_INITIAL (decl) = scope;
4832  TREE_CHAIN (decl) = current_binding_level->usings;
4833  current_binding_level->usings = decl;
4834  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4835}
4836
4837/* Add namespace to using_directives. Return NULL_TREE if nothing was
4838   changed (i.e. there was already a directive), or the fresh
4839   TREE_LIST otherwise.  */
4840
4841tree
4842push_using_directive (used)
4843     tree used;
4844{
4845  tree ud = current_binding_level->using_directives;
4846  tree iter, ancestor;
4847
4848  timevar_push (TV_NAME_LOOKUP);
4849
4850  /* Check if we already have this.  */
4851  if (purpose_member (used, ud) != NULL_TREE)
4852    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4853
4854  ancestor = namespace_ancestor (current_decl_namespace (), used);
4855  ud = current_binding_level->using_directives;
4856  ud = tree_cons (used, ancestor, ud);
4857  current_binding_level->using_directives = ud;
4858
4859  /* Recursively add all namespaces used.  */
4860  for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4861    push_using_directive (TREE_PURPOSE (iter));
4862
4863  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4864}
4865
4866/* DECL is a FUNCTION_DECL for a non-member function, which may have
4867   other definitions already in place.  We get around this by making
4868   the value of the identifier point to a list of all the things that
4869   want to be referenced by that name.  It is then up to the users of
4870   that name to decide what to do with that list.
4871
4872   DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4873   DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4874
4875   FLAGS is a bitwise-or of the following values:
4876     PUSH_LOCAL: Bind DECL in the current scope, rather than at
4877                 namespace scope.
4878     PUSH_USING: DECL is being pushed as the result of a using
4879                 declaration.
4880
4881   The value returned may be a previous declaration if we guessed wrong
4882   about what language DECL should belong to (C or C++).  Otherwise,
4883   it's always DECL (and never something that's not a _DECL).  */
4884
4885tree
4886push_overloaded_decl (decl, flags)
4887     tree decl;
4888     int flags;
4889{
4890  tree name = DECL_NAME (decl);
4891  tree old;
4892  tree new_binding;
4893  int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4894
4895  timevar_push (TV_NAME_LOOKUP);
4896
4897  if (doing_global)
4898    old = namespace_binding (name, DECL_CONTEXT (decl));
4899  else
4900    old = lookup_name_current_level (name);
4901
4902  if (old)
4903    {
4904      if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4905	{
4906	  tree t = TREE_TYPE (old);
4907	  if (IS_AGGR_TYPE (t) && warn_shadow
4908	      && (! DECL_IN_SYSTEM_HEADER (decl)
4909		  || ! DECL_IN_SYSTEM_HEADER (old)))
4910	    warning ("`%#D' hides constructor for `%#T'", decl, t);
4911	  old = NULL_TREE;
4912	}
4913      else if (is_overloaded_fn (old))
4914        {
4915          tree tmp;
4916
4917	  for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4918	    {
4919	      tree fn = OVL_CURRENT (tmp);
4920
4921	      if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4922		  && !(flags & PUSH_USING)
4923		  && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4924				TYPE_ARG_TYPES (TREE_TYPE (decl))))
4925		error ("`%#D' conflicts with previous using declaration `%#D'",
4926			  decl, fn);
4927
4928	      if (duplicate_decls (decl, fn))
4929		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4930	    }
4931	}
4932      else if (old == error_mark_node)
4933	/* Ignore the undefined symbol marker.  */
4934	old = NULL_TREE;
4935      else
4936	{
4937	  cp_error_at ("previous non-function declaration `%#D'", old);
4938	  error ("conflicts with function declaration `%#D'", decl);
4939	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4940	}
4941    }
4942
4943  if (old || TREE_CODE (decl) == TEMPLATE_DECL
4944      /* If it's a using declaration, we always need to build an OVERLOAD,
4945	 because it's the only way to remember that the declaration comes
4946	 from 'using', and have the lookup behave correctly.  */
4947      || (flags & PUSH_USING))
4948    {
4949      if (old && TREE_CODE (old) != OVERLOAD)
4950	new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4951      else
4952	new_binding = ovl_cons (decl, old);
4953      if (flags & PUSH_USING)
4954	OVL_USED (new_binding) = 1;
4955    }
4956  else
4957    /* NAME is not ambiguous.  */
4958    new_binding = decl;
4959
4960  if (doing_global)
4961    set_namespace_binding (name, current_namespace, new_binding);
4962  else
4963    {
4964      /* We only create an OVERLOAD if there was a previous binding at
4965	 this level, or if decl is a template. In the former case, we
4966	 need to remove the old binding and replace it with the new
4967	 binding.  We must also run through the NAMES on the binding
4968	 level where the name was bound to update the chain.  */
4969
4970      if (TREE_CODE (new_binding) == OVERLOAD && old)
4971	{
4972	  tree *d;
4973
4974	  for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4975	       *d;
4976	       d = &TREE_CHAIN (*d))
4977	    if (*d == old
4978		|| (TREE_CODE (*d) == TREE_LIST
4979		    && TREE_VALUE (*d) == old))
4980	      {
4981		if (TREE_CODE (*d) == TREE_LIST)
4982		  /* Just replace the old binding with the new.  */
4983		  TREE_VALUE (*d) = new_binding;
4984		else
4985		  /* Build a TREE_LIST to wrap the OVERLOAD.  */
4986		  *d = tree_cons (NULL_TREE, new_binding,
4987				  TREE_CHAIN (*d));
4988
4989		/* And update the cxx_binding node.  */
4990		BINDING_VALUE (IDENTIFIER_BINDING (name))
4991		  = new_binding;
4992		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4993	      }
4994
4995	  /* We should always find a previous binding in this case.  */
4996	  abort ();
4997	}
4998
4999      /* Install the new binding.  */
5000      push_local_binding (name, new_binding, flags);
5001    }
5002
5003  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5004}
5005
5006/* Generate an implicit declaration for identifier FUNCTIONID
5007   as a function of type int ().  Print a warning if appropriate.  */
5008
5009tree
5010implicitly_declare (functionid)
5011     tree functionid;
5012{
5013  register tree decl;
5014
5015  /* We used to reuse an old implicit decl here,
5016     but this loses with inline functions because it can clobber
5017     the saved decl chains.  */
5018  decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
5019
5020  DECL_EXTERNAL (decl) = 1;
5021  TREE_PUBLIC (decl) = 1;
5022
5023  /* ISO standard says implicit declarations are in the innermost block.
5024     So we record the decl in the standard fashion.  */
5025  pushdecl (decl);
5026  rest_of_decl_compilation (decl, NULL, 0, 0);
5027
5028  if (warn_implicit
5029      /* Only one warning per identifier.  */
5030      && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
5031    {
5032      pedwarn ("implicit declaration of function `%#D'", decl);
5033    }
5034
5035  SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
5036
5037  return decl;
5038}
5039
5040/* Return zero if the declaration NEWDECL is valid
5041   when the declaration OLDDECL (assumed to be for the same name)
5042   has already been seen.
5043   Otherwise return an error message format string with a %s
5044   where the identifier should go.  */
5045
5046static const char *
5047redeclaration_error_message (newdecl, olddecl)
5048     tree newdecl, olddecl;
5049{
5050  if (TREE_CODE (newdecl) == TYPE_DECL)
5051    {
5052      /* Because C++ can put things into name space for free,
5053	 constructs like "typedef struct foo { ... } foo"
5054	 would look like an erroneous redeclaration.  */
5055      if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
5056	return 0;
5057      else
5058	return "redefinition of `%#D'";
5059    }
5060  else if (TREE_CODE (newdecl) == FUNCTION_DECL)
5061    {
5062      /* If this is a pure function, its olddecl will actually be
5063	 the original initialization to `0' (which we force to call
5064	 abort()).  Don't complain about redefinition in this case.  */
5065      if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
5066	return 0;
5067
5068      /* If both functions come from different namespaces, this is not
5069	 a redeclaration - this is a conflict with a used function.  */
5070      if (DECL_NAMESPACE_SCOPE_P (olddecl)
5071	  && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
5072	return "`%D' conflicts with used function";
5073
5074      /* We'll complain about linkage mismatches in
5075         warn_extern_redeclared_static.  */
5076
5077      /* Defining the same name twice is no good.  */
5078      if (DECL_INITIAL (olddecl) != NULL_TREE
5079	  && DECL_INITIAL (newdecl) != NULL_TREE)
5080	{
5081	  if (DECL_NAME (olddecl) == NULL_TREE)
5082	    return "`%#D' not declared in class";
5083	  else
5084	    return "redefinition of `%#D'";
5085	}
5086      return 0;
5087    }
5088  else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
5089    {
5090      if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
5091	   && (DECL_TEMPLATE_RESULT (newdecl)
5092	       != DECL_TEMPLATE_RESULT (olddecl))
5093	   && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
5094	   && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
5095	  || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
5096	      && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
5097	      && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
5098	return "redefinition of `%#D'";
5099      return 0;
5100    }
5101  else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
5102    {
5103      /* Objects declared at top level:  */
5104      /* If at least one is a reference, it's ok.  */
5105      if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
5106	return 0;
5107      /* Reject two definitions.  */
5108      return "redefinition of `%#D'";
5109    }
5110  else
5111    {
5112      /* Objects declared with block scope:  */
5113      /* Reject two definitions, and reject a definition
5114	 together with an external reference.  */
5115      if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
5116	return "redeclaration of `%#D'";
5117      return 0;
5118    }
5119}
5120
5121/* Create a new label, named ID.  */
5122
5123static tree
5124make_label_decl (id, local_p)
5125     tree id;
5126     int local_p;
5127{
5128  tree decl;
5129
5130  decl = build_decl (LABEL_DECL, id, void_type_node);
5131  if (expanding_p)
5132    /* Make sure every label has an rtx.  */
5133    label_rtx (decl);
5134
5135  DECL_CONTEXT (decl) = current_function_decl;
5136  DECL_MODE (decl) = VOIDmode;
5137  C_DECLARED_LABEL_FLAG (decl) = local_p;
5138
5139  /* Say where one reference is to the label, for the sake of the
5140     error if it is not defined.  */
5141  DECL_SOURCE_LINE (decl) = lineno;
5142  DECL_SOURCE_FILE (decl) = input_filename;
5143
5144  /* Record the fact that this identifier is bound to this label.  */
5145  SET_IDENTIFIER_LABEL_VALUE (id, decl);
5146
5147  return decl;
5148}
5149
5150/* Record this label on the list of used labels so that we can check
5151   at the end of the function to see whether or not the label was
5152   actually defined, and so we can check when the label is defined whether
5153   this use is valid.  */
5154
5155static void
5156use_label (decl)
5157     tree decl;
5158{
5159  if (named_label_uses == NULL
5160      || named_label_uses->names_in_scope != current_binding_level->names
5161      || named_label_uses->label_decl != decl)
5162    {
5163      struct named_label_use_list *new_ent;
5164      new_ent = ((struct named_label_use_list *)
5165		 ggc_alloc (sizeof (struct named_label_use_list)));
5166      new_ent->label_decl = decl;
5167      new_ent->names_in_scope = current_binding_level->names;
5168      new_ent->binding_level = current_binding_level;
5169      new_ent->lineno_o_goto = lineno;
5170      new_ent->filename_o_goto = input_filename;
5171      new_ent->next = named_label_uses;
5172      named_label_uses = new_ent;
5173    }
5174}
5175
5176/* Look for a label named ID in the current function.  If one cannot
5177   be found, create one.  (We keep track of used, but undefined,
5178   labels, and complain about them at the end of a function.)  */
5179
5180tree
5181lookup_label (id)
5182     tree id;
5183{
5184  tree decl;
5185  struct named_label_list *ent;
5186
5187  timevar_push (TV_NAME_LOOKUP);
5188
5189  /* You can't use labels at global scope.  */
5190  if (current_function_decl == NULL_TREE)
5191    {
5192      error ("label `%s' referenced outside of any function",
5193	     IDENTIFIER_POINTER (id));
5194      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5195    }
5196
5197  /* See if we've already got this label.  */
5198  decl = IDENTIFIER_LABEL_VALUE (id);
5199  if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
5200    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5201
5202  /* Record this label on the list of labels used in this function.
5203     We do this before calling make_label_decl so that we get the
5204     IDENTIFIER_LABEL_VALUE before the new label is declared.  */
5205  ent = ((struct named_label_list *)
5206	 ggc_alloc_cleared (sizeof (struct named_label_list)));
5207  ent->old_value = IDENTIFIER_LABEL_VALUE (id);
5208  ent->next = named_labels;
5209  named_labels = ent;
5210
5211  /* We need a new label.  */
5212  decl = make_label_decl (id, /*local_p=*/0);
5213
5214  /* Now fill in the information we didn't have before.  */
5215  ent->label_decl = decl;
5216
5217  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5218}
5219
5220/* Declare a local label named ID.  */
5221
5222tree
5223declare_local_label (id)
5224     tree id;
5225{
5226  tree decl;
5227
5228  /* Add a new entry to the SHADOWED_LABELS list so that when we leave
5229     this scope we can restore the old value of
5230     IDENTIFIER_TYPE_VALUE.  */
5231  current_binding_level->shadowed_labels
5232    = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
5233		 current_binding_level->shadowed_labels);
5234  /* Look for the label.  */
5235  decl = make_label_decl (id, /*local_p=*/1);
5236  /* Now fill in the information we didn't have before.  */
5237  TREE_VALUE (current_binding_level->shadowed_labels) = decl;
5238
5239  return decl;
5240}
5241
5242/* Returns nonzero if it is ill-formed to jump past the declaration of
5243   DECL.  Returns 2 if it's also a real problem.  */
5244
5245static int
5246decl_jump_unsafe (decl)
5247     tree decl;
5248{
5249  if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
5250    return 0;
5251
5252  if (DECL_INITIAL (decl) == NULL_TREE
5253      && pod_type_p (TREE_TYPE (decl)))
5254    return 0;
5255
5256  /* This is really only important if we're crossing an initialization.
5257     The POD stuff is just pedantry; why should it matter if the class
5258     contains a field of pointer to member type?  */
5259  if (DECL_INITIAL (decl)
5260      || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
5261    return 2;
5262  return 1;
5263}
5264
5265/* Check that a single previously seen jump to a newly defined label
5266   is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
5267   the jump context; NAMES are the names in scope in LEVEL at the jump
5268   context; FILE and LINE are the source position of the jump or 0.  */
5269
5270static void
5271check_previous_goto_1 (decl, level, names, file, line)
5272     tree decl;
5273     struct cp_binding_level *level;
5274     tree names;
5275     const char *file;
5276     int line;
5277{
5278  int identified = 0;
5279  int saw_eh = 0;
5280  struct cp_binding_level *b = current_binding_level;
5281  for (; b; b = b->level_chain)
5282    {
5283      tree new_decls = b->names;
5284      tree old_decls = (b == level ? names : NULL_TREE);
5285      for (; new_decls != old_decls;
5286	   new_decls = TREE_CHAIN (new_decls))
5287	{
5288	  int problem = decl_jump_unsafe (new_decls);
5289	  if (! problem)
5290	    continue;
5291
5292	  if (! identified)
5293	    {
5294	      if (decl)
5295		pedwarn ("jump to label `%D'", decl);
5296	      else
5297		pedwarn ("jump to case label");
5298
5299	      if (file)
5300		pedwarn_with_file_and_line (file, line, "  from here");
5301	      identified = 1;
5302	    }
5303
5304	  if (problem > 1)
5305	    cp_error_at ("  crosses initialization of `%#D'",
5306			 new_decls);
5307	  else
5308	    cp_pedwarn_at ("  enters scope of non-POD `%#D'",
5309			   new_decls);
5310	}
5311
5312      if (b == level)
5313	break;
5314      if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
5315	{
5316	  if (! identified)
5317	    {
5318	      if (decl)
5319		pedwarn ("jump to label `%D'", decl);
5320	      else
5321		pedwarn ("jump to case label");
5322
5323	      if (file)
5324		pedwarn_with_file_and_line (file, line, "  from here");
5325	      identified = 1;
5326	    }
5327	  if (b->is_try_scope)
5328	    error ("  enters try block");
5329	  else
5330	    error ("  enters catch block");
5331	  saw_eh = 1;
5332	}
5333    }
5334}
5335
5336static void
5337check_previous_goto (use)
5338     struct named_label_use_list *use;
5339{
5340  check_previous_goto_1 (use->label_decl, use->binding_level,
5341			 use->names_in_scope, use->filename_o_goto,
5342			 use->lineno_o_goto);
5343}
5344
5345static void
5346check_switch_goto (level)
5347     struct cp_binding_level *level;
5348{
5349  check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
5350}
5351
5352/* Check that any previously seen jumps to a newly defined label DECL
5353   are OK.  Called by define_label.  */
5354
5355static void
5356check_previous_gotos (decl)
5357     tree decl;
5358{
5359  struct named_label_use_list **usep;
5360
5361  if (! TREE_USED (decl))
5362    return;
5363
5364  for (usep = &named_label_uses; *usep; )
5365    {
5366      struct named_label_use_list *use = *usep;
5367      if (use->label_decl == decl)
5368	{
5369	  check_previous_goto (use);
5370	  *usep = use->next;
5371	}
5372      else
5373	usep = &(use->next);
5374    }
5375}
5376
5377/* Check that a new jump to a label DECL is OK.  Called by
5378   finish_goto_stmt.  */
5379
5380void
5381check_goto (decl)
5382     tree decl;
5383{
5384  int identified = 0;
5385  tree bad;
5386  struct named_label_list *lab;
5387
5388  /* We can't know where a computed goto is jumping.  So we assume
5389     that it's OK.  */
5390  if (! DECL_P (decl))
5391    return;
5392
5393  /* If the label hasn't been defined yet, defer checking.  */
5394  if (! DECL_INITIAL (decl))
5395    {
5396      use_label (decl);
5397      return;
5398    }
5399
5400  for (lab = named_labels; lab; lab = lab->next)
5401    if (decl == lab->label_decl)
5402      break;
5403
5404  /* If the label is not on named_labels it's a gcc local label, so
5405     it must be in an outer scope, so jumping to it is always OK.  */
5406  if (lab == 0)
5407    return;
5408
5409  if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
5410      && !identified)
5411    {
5412      cp_pedwarn_at ("jump to label `%D'", decl);
5413      pedwarn ("  from here");
5414      identified = 1;
5415    }
5416
5417  for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
5418    {
5419      tree b = TREE_VALUE (bad);
5420      int u = decl_jump_unsafe (b);
5421
5422      if (u > 1 && DECL_ARTIFICIAL (b))
5423	/* Can't skip init of __exception_info.  */
5424	cp_error_at ("  enters catch block", b);
5425      else if (u > 1)
5426	cp_error_at ("  skips initialization of `%#D'", b);
5427      else
5428	cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
5429    }
5430
5431  if (lab->in_try_scope)
5432    error ("  enters try block");
5433  else if (lab->in_catch_scope)
5434    error ("  enters catch block");
5435}
5436
5437/* Define a label, specifying the location in the source file.
5438   Return the LABEL_DECL node for the label.  */
5439
5440tree
5441define_label (filename, line, name)
5442     const char *filename;
5443     int line;
5444     tree name;
5445{
5446  tree decl = lookup_label (name);
5447  struct named_label_list *ent;
5448  register struct cp_binding_level *p;
5449
5450  timevar_push (TV_NAME_LOOKUP);
5451
5452  for (ent = named_labels; ent; ent = ent->next)
5453    if (ent->label_decl == decl)
5454      break;
5455
5456  /* After labels, make any new cleanups in the function go into their
5457     own new (temporary) binding contour.  */
5458  for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5459    p->more_cleanups_ok = 0;
5460
5461  if (name == get_identifier ("wchar_t"))
5462    pedwarn ("label named wchar_t");
5463
5464  if (DECL_INITIAL (decl) != NULL_TREE)
5465    error ("duplicate label `%D'", decl);
5466  else
5467    {
5468      /* Mark label as having been defined.  */
5469      DECL_INITIAL (decl) = error_mark_node;
5470      /* Say where in the source.  */
5471      DECL_SOURCE_FILE (decl) = filename;
5472      DECL_SOURCE_LINE (decl) = line;
5473      if (ent)
5474	{
5475	  ent->names_in_scope = current_binding_level->names;
5476	  ent->binding_level = current_binding_level;
5477	}
5478      check_previous_gotos (decl);
5479    }
5480
5481  timevar_pop (TV_NAME_LOOKUP);
5482  return decl;
5483}
5484
5485struct cp_switch
5486{
5487  struct cp_binding_level *level;
5488  struct cp_switch *next;
5489  /* The SWITCH_STMT being built.  */
5490  tree switch_stmt;
5491  /* A splay-tree mapping the low element of a case range to the high
5492     element, or NULL_TREE if there is no high element.  Used to
5493     determine whether or not a new case label duplicates an old case
5494     label.  We need a tree, rather than simply a hash table, because
5495     of the GNU case range extension.  */
5496  splay_tree cases;
5497};
5498
5499/* A stack of the currently active switch statements.  The innermost
5500   switch statement is on the top of the stack.  There is no need to
5501   mark the stack for garbage collection because it is only active
5502   during the processing of the body of a function, and we never
5503   collect at that point.  */
5504
5505static struct cp_switch *switch_stack;
5506
5507/* Called right after a switch-statement condition is parsed.
5508   SWITCH_STMT is the switch statement being parsed.  */
5509
5510void
5511push_switch (switch_stmt)
5512     tree switch_stmt;
5513{
5514  struct cp_switch *p
5515    = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5516  p->level = current_binding_level;
5517  p->next = switch_stack;
5518  p->switch_stmt = switch_stmt;
5519  p->cases = splay_tree_new (case_compare, NULL, NULL);
5520  switch_stack = p;
5521}
5522
5523void
5524pop_switch ()
5525{
5526  struct cp_switch *cs;
5527
5528  cs = switch_stack;
5529  splay_tree_delete (cs->cases);
5530  switch_stack = switch_stack->next;
5531  free (cs);
5532}
5533
5534/* Note that we've seen a definition of a case label, and complain if this
5535   is a bad place for one.  */
5536
5537tree
5538finish_case_label (low_value, high_value)
5539     tree low_value;
5540     tree high_value;
5541{
5542  tree cond, r;
5543  register struct cp_binding_level *p;
5544
5545  if (! switch_stack)
5546    {
5547      if (high_value)
5548	error ("case label not within a switch statement");
5549      else if (low_value)
5550	error ("case label `%E' not within a switch statement",
5551		  low_value);
5552      else
5553	error ("`default' label not within a switch statement");
5554      return NULL_TREE;
5555    }
5556
5557  if (processing_template_decl)
5558    {
5559      tree label;
5560
5561      /* For templates, just add the case label; we'll do semantic
5562	 analysis at instantiation-time.  */
5563      label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5564      return add_stmt (build_case_label (low_value, high_value, label));
5565    }
5566
5567  /* Find the condition on which this switch statement depends.  */
5568  cond = SWITCH_COND (switch_stack->switch_stmt);
5569  if (cond && TREE_CODE (cond) == TREE_LIST)
5570    cond = TREE_VALUE (cond);
5571
5572  r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5573  if (r == error_mark_node)
5574    r = NULL_TREE;
5575
5576  check_switch_goto (switch_stack->level);
5577
5578  /* After labels, make any new cleanups in the function go into their
5579     own new (temporary) binding contour.  */
5580  for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5581    p->more_cleanups_ok = 0;
5582
5583  return r;
5584}
5585
5586/* Return the list of declarations of the current level.
5587   Note that this list is in reverse order unless/until
5588   you nreverse it; and when you do nreverse it, you must
5589   store the result back using `storedecls' or you will lose.  */
5590
5591tree
5592getdecls ()
5593{
5594  return current_binding_level->names;
5595}
5596
5597/* Store the list of declarations of the current level.
5598   This is done for the parameter declarations of a function being defined,
5599   after they are modified in the light of any missing parameters.  */
5600
5601static void
5602storedecls (decls)
5603     tree decls;
5604{
5605  current_binding_level->names = decls;
5606}
5607
5608/* Set the current binding TABLE for type declarations..  This is a
5609   temporary workaround of the fact that the data structure classtypes
5610   does not currently carry its allocated cxx_scope structure.  */
5611void
5612cxx_remember_type_decls (binding_table table)
5613{
5614  current_binding_level->type_decls = table;
5615}
5616
5617
5618/* Return the type that should be used when TYPE's name is preceded
5619   by a tag such as 'struct' or 'union', or null if the name cannot
5620   be used in this way.
5621
5622   For example, when processing the third line of:
5623
5624	struct A;
5625	typedef struct A A;
5626	struct A;
5627
5628   lookup of A will find the typedef.  Given A's typedef, this function
5629   will return the type associated with "struct A".  For the tag to be
5630   anything other than TYPE, TYPE must be a typedef whose original type
5631   has the same name and context as TYPE itself.
5632
5633   It is not valid for a typedef of an anonymous type to be used with
5634   an explicit tag:
5635
5636       typedef struct { ... } B;
5637       struct B;
5638
5639   Return null for this case.  */
5640
5641static tree
5642follow_tag_typedef (type)
5643     tree type;
5644{
5645  tree original;
5646
5647  original = original_type (type);
5648  if (! TYPE_NAME (original))
5649    return NULL_TREE;
5650  if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5651      && (CP_DECL_CONTEXT (TYPE_NAME (original))
5652	  == CP_DECL_CONTEXT (TYPE_NAME (type)))
5653      && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5654    return original;
5655  else
5656    return NULL_TREE;
5657}
5658
5659/* Given NAME, an IDENTIFIER_NODE,
5660   return the structure (or union or enum) definition for that name.
5661   Searches binding levels from BINDING_LEVEL up to the global level.
5662   If THISLEVEL_ONLY is nonzero, searches only the specified context
5663   (but skips any tag-transparent contexts to find one that is
5664   meaningful for tags).
5665   FORM says which kind of type the caller wants;
5666   it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5667   If the wrong kind of type is found, and it's not a template, an error is
5668   reported.  */
5669
5670static tree
5671lookup_tag (form, name, binding_level, thislevel_only)
5672     enum tree_code form;
5673     tree name;
5674     struct cp_binding_level *binding_level;
5675     int thislevel_only;
5676{
5677  register struct cp_binding_level *level;
5678  /* Nonzero if, we should look past a template parameter level, even
5679     if THISLEVEL_ONLY.  */
5680  int allow_template_parms_p = 1;
5681  bool type_is_anonymous = ANON_AGGRNAME_P (name);
5682
5683  timevar_push (TV_NAME_LOOKUP);
5684
5685  for (level = binding_level; level; level = level->level_chain)
5686    {
5687      register tree tail;
5688      if (type_is_anonymous && level->type_decls != NULL)
5689        {
5690          tree type = binding_table_find_anon_type (level->type_decls, name);
5691          /* There's no need for error checking here, because
5692             anon names are unique throughout the compilation.  */
5693          if (type != NULL)
5694            POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5695        }
5696      else if (level->namespace_p)
5697	/* Do namespace lookup.  */
5698	for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5699	  {
5700	    cxx_binding *binding =
5701              cxx_scope_find_binding_for_name (tail, name);
5702            tree old;
5703
5704	    /* If we just skipped past a template parameter level,
5705	       even though THISLEVEL_ONLY, and we find a template
5706	       class declaration, then we use the _TYPE node for the
5707	       template.  See the example below.  */
5708	    if (thislevel_only && !allow_template_parms_p
5709		&& binding && BINDING_VALUE (binding)
5710		&& DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5711	      old = TREE_TYPE (BINDING_VALUE (binding));
5712	    else if (binding)
5713	      old = BINDING_TYPE (binding);
5714            else
5715              old = NULL;
5716
5717	    if (old)
5718	      {
5719		/* We've found something at this binding level.  If it is
5720		   a typedef, extract the tag it refers to.  Lookup fails
5721		   if the typedef doesn't refer to a taggable type.  */
5722		old = follow_tag_typedef (old);
5723		if (!old)
5724		  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5725		if (TREE_CODE (old) != form
5726		    && (form == ENUMERAL_TYPE
5727			|| TREE_CODE (old) == ENUMERAL_TYPE))
5728		  {
5729		    error ("`%#D' redeclared as %C", old, form);
5730		    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5731		  }
5732		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5733	      }
5734	    if (thislevel_only || tail == global_namespace)
5735	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5736	  }
5737      else if (level->type_decls != NULL)
5738        {
5739          binding_entry entry = binding_table_find (level->type_decls, name);
5740          if (entry != NULL)
5741            {
5742              enum tree_code code = TREE_CODE (entry->type);
5743
5744              if (code != form
5745                  && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5746                {
5747                  /* Definition isn't the kind we were looking for.  */
5748                  error ("`%#D' redeclared as %C", entry->type, form);
5749                  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5750                }
5751              POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
5752            }
5753        }
5754      if (thislevel_only && ! level->tag_transparent)
5755	{
5756	  if (level->template_parms_p && allow_template_parms_p)
5757	    {
5758	      /* We must deal with cases like this:
5759
5760	           template <class T> struct S;
5761		   template <class T> struct S {};
5762
5763		 When looking up `S', for the second declaration, we
5764		 would like to find the first declaration.  But, we
5765		 are in the pseudo-global level created for the
5766		 template parameters, rather than the (surrounding)
5767		 namespace level.  Thus, we keep going one more level,
5768		 even though THISLEVEL_ONLY is nonzero.  */
5769	      allow_template_parms_p = 0;
5770	      continue;
5771	    }
5772	  else
5773	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5774	}
5775    }
5776  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5777}
5778
5779#if 0
5780void
5781set_current_level_tags_transparency (tags_transparent)
5782     int tags_transparent;
5783{
5784  current_binding_level->tag_transparent = tags_transparent;
5785}
5786#endif
5787
5788/* Given a type, find the tag that was defined for it and return the tag name.
5789   Otherwise return 0.  However, the value can never be 0
5790   in the cases in which this is used.
5791
5792   C++: If NAME is nonzero, this is the new name to install.  This is
5793   done when replacing anonymous tags with real tag names.  */
5794
5795static tree
5796lookup_tag_reverse (type, name)
5797     tree type;
5798     tree name;
5799{
5800  register struct cp_binding_level *level;
5801
5802  timevar_push (TV_NAME_LOOKUP);
5803
5804  for (level = current_binding_level; level; level = level->level_chain)
5805    {
5806      binding_entry entry = level->type_decls == NULL
5807        ? NULL
5808        : binding_table_reverse_maybe_remap (level->type_decls, type, name);
5809      if (entry)
5810        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
5811    }
5812  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5813}
5814
5815/* Look up NAME in the NAMESPACE.  */
5816
5817tree
5818lookup_namespace_name (namespace, name)
5819     tree namespace, name;
5820{
5821  tree val;
5822  tree template_id = NULL_TREE;
5823  cxx_binding binding;
5824
5825  timevar_push (TV_NAME_LOOKUP);
5826
5827  my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5828
5829  if (TREE_CODE (name) == NAMESPACE_DECL)
5830    /* This happens for A::B<int> when B is a namespace.  */
5831    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5832  else if (TREE_CODE (name) == TEMPLATE_DECL)
5833    {
5834      /* This happens for A::B where B is a template, and there are no
5835	 template arguments.  */
5836      error ("invalid use of `%D'", name);
5837      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5838    }
5839
5840  namespace = ORIGINAL_NAMESPACE (namespace);
5841
5842  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5843    {
5844      template_id = name;
5845      name = TREE_OPERAND (name, 0);
5846      if (TREE_CODE (name) == OVERLOAD)
5847	name = DECL_NAME (OVL_CURRENT (name));
5848      else if (DECL_P (name))
5849	name = DECL_NAME (name);
5850    }
5851
5852  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5853
5854  cxx_binding_clear (&binding);
5855  if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5856    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5857
5858  if (binding.value)
5859    {
5860      val = binding.value;
5861
5862      if (template_id)
5863	{
5864	  if (DECL_CLASS_TEMPLATE_P (val))
5865	    val = lookup_template_class (val,
5866					 TREE_OPERAND (template_id, 1),
5867					 /*in_decl=*/NULL_TREE,
5868					 /*context=*/NULL_TREE,
5869					 /*entering_scope=*/0,
5870	                                 tf_error | tf_warning);
5871	  else if (DECL_FUNCTION_TEMPLATE_P (val)
5872		   || TREE_CODE (val) == OVERLOAD)
5873	    val = lookup_template_function (val,
5874					    TREE_OPERAND (template_id, 1));
5875	  else
5876	    {
5877	      error ("`%D::%D' is not a template",
5878			namespace, name);
5879	      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5880	    }
5881	}
5882
5883      /* If we have a single function from a using decl, pull it out.  */
5884      if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5885	val = OVL_FUNCTION (val);
5886
5887      /* Ignore built-in functions that haven't been prototyped yet.  */
5888      if (!val || !DECL_P(val)
5889          || !DECL_LANG_SPECIFIC(val)
5890          || !DECL_ANTICIPATED (val))
5891        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5892    }
5893
5894  error ("`%D' undeclared in namespace `%D'", name, namespace);
5895  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5896}
5897
5898/* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5899
5900static hashval_t
5901typename_hash (k)
5902     const void * k;
5903{
5904  hashval_t hash;
5905  tree t = (tree) k;
5906
5907  hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5908	  ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5909
5910  return hash;
5911}
5912
5913/* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5914
5915static int
5916typename_compare (k1, k2)
5917     const void * k1;
5918     const void * k2;
5919{
5920  tree t1;
5921  tree t2;
5922  tree d1;
5923  tree d2;
5924
5925  t1 = (tree) k1;
5926  t2 = (tree) k2;
5927  d1 = TYPE_NAME (t1);
5928  d2 = TYPE_NAME (t2);
5929
5930  return (DECL_NAME (d1) == DECL_NAME (d2)
5931	  && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5932	  && ((TREE_TYPE (t1) != NULL_TREE)
5933	      == (TREE_TYPE (t2) != NULL_TREE))
5934	  && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5935	  && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5936}
5937
5938/* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5939   the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5940   is non-NULL, this type is being created by the implicit typename
5941   extension, and BASE_TYPE is a type named `t' in some base class of
5942   `T' which depends on template parameters.
5943
5944   Returns the new TYPENAME_TYPE.  */
5945
5946static GTY ((param_is (union tree_node))) htab_t typename_htab;
5947
5948tree
5949build_typename_type (context, name, fullname, base_type)
5950     tree context;
5951     tree name;
5952     tree fullname;
5953     tree base_type;
5954{
5955  tree t;
5956  tree d;
5957  PTR *e;
5958
5959  if (typename_htab == NULL)
5960    {
5961      typename_htab = htab_create_ggc (61, &typename_hash,
5962				       &typename_compare, NULL);
5963    }
5964
5965  /* Build the TYPENAME_TYPE.  */
5966  t = make_aggr_type (TYPENAME_TYPE);
5967  TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5968  TYPENAME_TYPE_FULLNAME (t) = fullname;
5969  TREE_TYPE (t) = base_type;
5970
5971  /* Build the corresponding TYPE_DECL.  */
5972  d = build_decl (TYPE_DECL, name, t);
5973  TYPE_NAME (TREE_TYPE (d)) = d;
5974  TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5975  DECL_CONTEXT (d) = FROB_CONTEXT (context);
5976  DECL_ARTIFICIAL (d) = 1;
5977
5978  /* See if we already have this type.  */
5979  e = htab_find_slot (typename_htab, t, INSERT);
5980  if (*e)
5981    t = (tree) *e;
5982  else
5983    *e = t;
5984
5985  return t;
5986}
5987
5988/* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5989   unless an error occurs, in which case error_mark_node is returned.
5990   If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5991   set, we return that, rather than the _TYPE it corresponds to, in
5992   other cases we look through the type decl.  If TF_ERROR is set,
5993   complain about errors, otherwise be quiet.  */
5994
5995tree
5996make_typename_type (context, name, complain)
5997     tree context, name;
5998     tsubst_flags_t complain;
5999{
6000  tree fullname;
6001
6002  if (TYPE_P (name))
6003    {
6004      if (!(TYPE_LANG_SPECIFIC (name)
6005	    && (CLASSTYPE_IS_TEMPLATE (name)
6006		|| CLASSTYPE_USE_TEMPLATE (name))))
6007	name = TYPE_IDENTIFIER (name);
6008      else
6009	/* Create a TEMPLATE_ID_EXPR for the type.  */
6010	name = build_nt (TEMPLATE_ID_EXPR,
6011			 CLASSTYPE_TI_TEMPLATE (name),
6012			 CLASSTYPE_TI_ARGS (name));
6013    }
6014  else if (TREE_CODE (name) == TYPE_DECL)
6015    name = DECL_NAME (name);
6016
6017  fullname = name;
6018
6019  if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
6020    {
6021      name = TREE_OPERAND (name, 0);
6022      if (TREE_CODE (name) == TEMPLATE_DECL)
6023	name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
6024    }
6025  if (TREE_CODE (name) == TEMPLATE_DECL)
6026    {
6027      error ("`%D' used without template parameters", name);
6028      return error_mark_node;
6029    }
6030  if (TREE_CODE (name) != IDENTIFIER_NODE)
6031    abort ();
6032
6033  if (TREE_CODE (context) == NAMESPACE_DECL)
6034    {
6035      /* We can get here from typename_sub0 in the explicit_template_type
6036	 expansion.  Just fail.  */
6037      if (complain & tf_error)
6038	error ("no class template named `%#T' in `%#T'",
6039		  name, context);
6040      return error_mark_node;
6041    }
6042
6043  if (! uses_template_parms (context)
6044      || currently_open_class (context))
6045    {
6046      if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
6047	{
6048	  tree tmpl = NULL_TREE;
6049	  if (IS_AGGR_TYPE (context))
6050	    tmpl = lookup_field (context, name, 0, 0);
6051	  if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6052	    {
6053	      if (complain & tf_error)
6054		error ("no class template named `%#T' in `%#T'",
6055			  name, context);
6056	      return error_mark_node;
6057	    }
6058
6059	  if (complain & tf_error)
6060	    {
6061	      if (complain & tf_parsing)
6062		type_access_control (context, tmpl);
6063	      else
6064		enforce_access (context, tmpl);
6065	    }
6066
6067	  return lookup_template_class (tmpl,
6068					TREE_OPERAND (fullname, 1),
6069					NULL_TREE, context,
6070					/*entering_scope=*/0,
6071	                                tf_error | tf_warning);
6072	}
6073      else
6074	{
6075          tree t;
6076
6077	  if (!IS_AGGR_TYPE (context))
6078	    {
6079	      if (complain & tf_error)
6080		error ("no type named `%#T' in `%#T'", name, context);
6081	      return error_mark_node;
6082	    }
6083
6084	  t = lookup_field (context, name, 0, 1);
6085	  if (t)
6086	    {
6087	      if (TREE_CODE (t) != TYPE_DECL)
6088		{
6089		  if (complain & tf_error)
6090		    error ("no type named `%#T' in `%#T'", name, context);
6091		  return error_mark_node;
6092		}
6093
6094	      if (complain & tf_error)
6095		{
6096	      	  if (complain & tf_parsing)
6097		    type_access_control (context, t);
6098		  else
6099		    enforce_access (context, t);
6100		}
6101
6102	      if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
6103		t = TREE_TYPE (t);
6104	      if (IMPLICIT_TYPENAME_P (t))
6105		{
6106		  /* Lookup found an implicit typename that we had
6107		     injected into the current scope. Doing things
6108		     properly would have located the exact same type,
6109		     so there is no error here.  We must remove the
6110		     implicitness so that we do not warn about it.  */
6111		  t = copy_node (t);
6112		  TREE_TYPE (t) = NULL_TREE;
6113		}
6114
6115	      return t;
6116	    }
6117	}
6118    }
6119
6120  /* If the CONTEXT is not a template type, then either the field is
6121     there now or its never going to be.  */
6122  if (!uses_template_parms (context))
6123    {
6124      if (complain & tf_error)
6125	error ("no type named `%#T' in `%#T'", name, context);
6126      return error_mark_node;
6127    }
6128
6129  return build_typename_type (context, name, fullname,  NULL_TREE);
6130}
6131
6132/* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
6133   unless an error occurs, in which case error_mark_node is returned.
6134   If we locate a TYPE_DECL, we return that, rather than the _TYPE it
6135   corresponds to.  If COMPLAIN zero, don't complain about any errors
6136   that occur.  */
6137
6138tree
6139make_unbound_class_template (context, name, complain)
6140     tree context, name;
6141     tsubst_flags_t complain;
6142{
6143  tree t;
6144  tree d;
6145
6146  if (TYPE_P (name))
6147    name = TYPE_IDENTIFIER (name);
6148  else if (DECL_P (name))
6149    name = DECL_NAME (name);
6150  if (TREE_CODE (name) != IDENTIFIER_NODE)
6151    abort ();
6152
6153  if (!uses_template_parms (context)
6154      || currently_open_class (context))
6155    {
6156      tree tmpl = NULL_TREE;
6157
6158      if (IS_AGGR_TYPE (context))
6159	tmpl = lookup_field (context, name, 0, 0);
6160
6161      if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
6162	{
6163	  if (complain & tf_error)
6164	    error ("no class template named `%#T' in `%#T'", name, context);
6165	  return error_mark_node;
6166	}
6167
6168      if (complain & tf_error)
6169	{
6170	  if (complain & tf_parsing)
6171	    type_access_control (context, tmpl);
6172	  else
6173	    enforce_access (context, tmpl);
6174	}
6175
6176      return tmpl;
6177    }
6178
6179  /* Build the UNBOUND_CLASS_TEMPLATE.  */
6180  t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
6181  TYPE_CONTEXT (t) = FROB_CONTEXT (context);
6182  TREE_TYPE (t) = NULL_TREE;
6183
6184  /* Build the corresponding TEMPLATE_DECL.  */
6185  d = build_decl (TEMPLATE_DECL, name, t);
6186  TYPE_NAME (TREE_TYPE (d)) = d;
6187  TYPE_STUB_DECL (TREE_TYPE (d)) = d;
6188  DECL_CONTEXT (d) = FROB_CONTEXT (context);
6189  DECL_ARTIFICIAL (d) = 1;
6190
6191  return t;
6192}
6193
6194/* Select the right _DECL from multiple choices.  */
6195
6196static tree
6197select_decl (cxx_binding *binding, int flags)
6198{
6199  tree val;
6200
6201  timevar_push (TV_NAME_LOOKUP);
6202
6203  val = BINDING_VALUE (binding);
6204
6205  if (LOOKUP_NAMESPACES_ONLY (flags))
6206    {
6207      /* We are not interested in types.  */
6208      if (val && TREE_CODE (val) == NAMESPACE_DECL)
6209        POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6210      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
6211    }
6212
6213  /* If we could have a type and
6214     we have nothing or we need a type and have none.  */
6215  if (BINDING_TYPE (binding)
6216      && (!val || ((flags & LOOKUP_PREFER_TYPES)
6217                   && TREE_CODE (val) != TYPE_DECL)))
6218    val = TYPE_STUB_DECL (BINDING_TYPE (binding));
6219  /* Don't return non-types if we really prefer types.  */
6220  else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
6221	   && (TREE_CODE (val) != TEMPLATE_DECL
6222	       || !DECL_CLASS_TEMPLATE_P (val)))
6223    val = NULL_TREE;
6224
6225  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6226}
6227
6228/* Unscoped lookup of a global: iterate over current namespaces,
6229   considering using-directives.  If SPACESP is non-NULL, store a list
6230   of the namespaces we've considered in it.  */
6231
6232tree
6233unqualified_namespace_lookup (name, flags, spacesp)
6234     tree name;
6235     int flags;
6236     tree *spacesp;
6237{
6238  tree initial = current_decl_namespace ();
6239  tree scope = initial;
6240  tree siter;
6241  struct cp_binding_level *level;
6242  tree val = NULL_TREE;
6243  cxx_binding binding;
6244
6245  timevar_push (TV_NAME_LOOKUP);
6246  cxx_binding_clear (&binding);
6247  if (spacesp)
6248    *spacesp = NULL_TREE;
6249
6250  for (; !val; scope = CP_DECL_CONTEXT (scope))
6251    {
6252      cxx_binding *b;
6253      if (spacesp)
6254	*spacesp = tree_cons (scope, NULL_TREE, *spacesp);
6255      b = cxx_scope_find_binding_for_name (scope, name);
6256
6257      /* Ignore anticipated built-in functions.  */
6258      if (b && BINDING_VALUE (b) && DECL_P (BINDING_VALUE (b))
6259          && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
6260          && DECL_ANTICIPATED (BINDING_VALUE (b)))
6261        /* Keep binding cleared.  */;
6262      else if (b)
6263        {
6264          /* Initialize binding for this context.  */
6265          binding.value = BINDING_VALUE (b);
6266          binding.type = BINDING_TYPE (b);
6267        }
6268
6269      /* Add all _DECLs seen through local using-directives.  */
6270      for (level = current_binding_level;
6271	   !level->namespace_p;
6272	   level = level->level_chain)
6273	if (!lookup_using_namespace (name, &binding, level->using_directives,
6274                                     scope, flags, spacesp))
6275	  /* Give up because of error.  */
6276	  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6277
6278      /* Add all _DECLs seen through global using-directives.  */
6279      /* XXX local and global using lists should work equally.  */
6280      siter = initial;
6281      while (1)
6282	{
6283	  if (!lookup_using_namespace (name, &binding,
6284                                       DECL_NAMESPACE_USING (siter),
6285				       scope, flags, spacesp))
6286	    /* Give up because of error.  */
6287	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6288	  if (siter == scope) break;
6289	  siter = CP_DECL_CONTEXT (siter);
6290	}
6291
6292      val = select_decl (&binding, flags);
6293      if (scope == global_namespace)
6294	break;
6295    }
6296  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6297}
6298
6299/* Combine prefer_type and namespaces_only into flags.  */
6300
6301static int
6302lookup_flags (prefer_type, namespaces_only)
6303  int prefer_type, namespaces_only;
6304{
6305  if (namespaces_only)
6306    return LOOKUP_PREFER_NAMESPACES;
6307  if (prefer_type > 1)
6308    return LOOKUP_PREFER_TYPES;
6309  if (prefer_type > 0)
6310    return LOOKUP_PREFER_BOTH;
6311  return 0;
6312}
6313
6314/* Given a lookup that returned VAL, use FLAGS to decide if we want to
6315   ignore it or not.  Subroutine of lookup_name_real.  */
6316
6317static tree
6318qualify_lookup (val, flags)
6319     tree val;
6320     int flags;
6321{
6322  if (val == NULL_TREE)
6323    return val;
6324  if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
6325    return val;
6326  if ((flags & LOOKUP_PREFER_TYPES)
6327      && (TREE_CODE (val) == TYPE_DECL
6328	  || ((flags & LOOKUP_TEMPLATES_EXPECTED)
6329	      && DECL_CLASS_TEMPLATE_P (val))))
6330    return val;
6331  if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
6332    return NULL_TREE;
6333  return val;
6334}
6335
6336/* Any other BINDING overrides an implicit TYPENAME.  Warn about
6337   that.  */
6338
6339static void
6340warn_about_implicit_typename_lookup (typename, binding)
6341     tree typename;
6342     tree binding;
6343{
6344  tree subtype = TREE_TYPE (TREE_TYPE (typename));
6345  tree name = DECL_NAME (typename);
6346
6347  if (! (TREE_CODE (binding) == TEMPLATE_DECL
6348	 && CLASS_TYPE_P (subtype)
6349	 && CLASSTYPE_TEMPLATE_INFO (subtype)
6350	 && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
6351      && ! (TREE_CODE (binding) == TYPE_DECL
6352	    && same_type_p (TREE_TYPE (binding), subtype)))
6353    {
6354      warning ("lookup of `%D' finds `%#D'",
6355		  name, binding);
6356      warning ("  instead of `%D' from dependent base class",
6357		  typename);
6358      warning ("  (use `typename %T::%D' if that's what you meant)",
6359		  constructor_name (current_class_type), name);
6360    }
6361}
6362
6363/* Check to see whether or not DECL is a variable that would have been
6364   in scope under the ARM, but is not in scope under the ANSI/ISO
6365   standard.  If so, issue an error message.  If name lookup would
6366   work in both cases, but return a different result, this function
6367   returns the result of ANSI/ISO lookup.  Otherwise, it returns
6368   DECL.  */
6369
6370tree
6371check_for_out_of_scope_variable (tree decl)
6372{
6373  tree shadowed;
6374
6375  /* We only care about out of scope variables.  */
6376  if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
6377    return decl;
6378
6379  shadowed = DECL_SHADOWED_FOR_VAR (decl);
6380  while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
6381	 && DECL_DEAD_FOR_LOCAL (shadowed))
6382    shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
6383  if (!shadowed)
6384    shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
6385  if (shadowed)
6386    {
6387      if (!DECL_ERROR_REPORTED (decl))
6388	{
6389	  warning ("name lookup of `%D' changed",
6390		      DECL_NAME (decl));
6391	  cp_warning_at ("  matches this `%D' under ISO standard rules",
6392			 shadowed);
6393	  cp_warning_at ("  matches this `%D' under old rules", decl);
6394	  DECL_ERROR_REPORTED (decl) = 1;
6395	}
6396      return shadowed;
6397    }
6398
6399  /* If we have already complained about this declaration, there's no
6400     need to do it again.  */
6401  if (DECL_ERROR_REPORTED (decl))
6402    return decl;
6403
6404  DECL_ERROR_REPORTED (decl) = 1;
6405  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
6406    {
6407      error ("name lookup of `%D' changed for new ISO `for' scoping",
6408	     DECL_NAME (decl));
6409      cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
6410      return error_mark_node;
6411    }
6412  else
6413    {
6414      pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
6415	       DECL_NAME (decl));
6416      cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
6417    }
6418
6419  return decl;
6420}
6421
6422/* Look up NAME in the current binding level and its superiors in the
6423   namespace of variables, functions and typedefs.  Return a ..._DECL
6424   node of some kind representing its definition if there is only one
6425   such declaration, or return a TREE_LIST with all the overloaded
6426   definitions if there are many, or return 0 if it is undefined.
6427
6428   If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
6429   If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
6430   If PREFER_TYPE is -2, we're being called from yylex(). (UGLY)
6431   Otherwise we prefer non-TYPE_DECLs.
6432
6433   If NONCLASS is nonzero, we don't look for the NAME in class scope,
6434   using IDENTIFIER_CLASS_VALUE.  */
6435
6436static tree
6437lookup_name_real (name, prefer_type, nonclass, namespaces_only)
6438     tree name;
6439     int prefer_type, nonclass, namespaces_only;
6440{
6441  tree t;
6442  tree val = NULL_TREE;
6443  int yylex = 0;
6444  tree from_obj = NULL_TREE;
6445  int flags;
6446  int val_is_implicit_typename = 0;
6447  cxx_binding *iter;
6448
6449  timevar_push (TV_NAME_LOOKUP);
6450
6451  /* Hack: copy flag set by parser, if set.  */
6452  if (only_namespace_names)
6453    namespaces_only = 1;
6454
6455  if (prefer_type == -2)
6456    {
6457      extern int looking_for_typename;
6458      tree type = NULL_TREE;
6459
6460      yylex = 1;
6461      prefer_type = looking_for_typename;
6462
6463      flags = lookup_flags (prefer_type, namespaces_only);
6464      /* If the next thing is '<', class templates are types.  */
6465      if (looking_for_template)
6466        flags |= LOOKUP_TEMPLATES_EXPECTED;
6467
6468      if (got_scope)
6469	type = got_scope;
6470      else if (got_object != error_mark_node)
6471	type = got_object;
6472
6473      if (type)
6474	{
6475	  if (type == error_mark_node)
6476	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
6477	  if (IMPLICIT_TYPENAME_P (type))
6478	    type = TREE_TYPE (type);
6479
6480	  if (TYPE_P (type))
6481	    type = complete_type (type);
6482
6483	  if (TREE_CODE (type) == VOID_TYPE)
6484	    type = global_namespace;
6485	  if (TREE_CODE (type) == NAMESPACE_DECL)
6486	    {
6487              cxx_binding b;
6488              cxx_binding_clear (&b);
6489	      flags |= LOOKUP_COMPLAIN;
6490	      if (!qualified_lookup_using_namespace (name, type, &b, flags))
6491		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
6492	      val = select_decl (&b, flags);
6493	    }
6494	  else if (! IS_AGGR_TYPE (type)
6495		   || TREE_CODE (type) == TEMPLATE_TYPE_PARM
6496		   || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
6497		   || TREE_CODE (type) == TYPENAME_TYPE)
6498	    /* Someone else will give an error about this if needed.  */
6499	    val = NULL_TREE;
6500	  else if (type == current_class_type)
6501	    val = IDENTIFIER_CLASS_VALUE (name);
6502	  else
6503	    {
6504	      val = lookup_member (type, name, 0, prefer_type);
6505	      if (!uses_template_parms (type))
6506		type_access_control (type, val);
6507
6508	      /* Restore the containing TYPENAME_TYPE if we looked
6509		 through it before.  */
6510	      if (got_scope && got_scope != type
6511		  && val && TREE_CODE (val) == TYPE_DECL
6512		  && TREE_CODE (TREE_TYPE (val)) == TYPENAME_TYPE)
6513		{
6514		  val = TREE_TYPE (val);
6515		  val = build_typename_type (got_scope, name,
6516					     TYPENAME_TYPE_FULLNAME (val),
6517					     TREE_TYPE (val));
6518		  val = TYPE_STUB_DECL (val);
6519		}
6520	    }
6521	}
6522      else
6523	val = NULL_TREE;
6524
6525      if (got_scope)
6526	goto done;
6527      else if (got_object && val)
6528	{
6529	  from_obj = val;
6530	  val = NULL_TREE;
6531	}
6532    }
6533  else
6534    {
6535      flags = lookup_flags (prefer_type, namespaces_only);
6536      /* If we're not parsing, we need to complain.  */
6537      flags |= LOOKUP_COMPLAIN;
6538    }
6539
6540  /* Conversion operators are handled specially because ordinary
6541     unqualified name lookup will not find template conversion
6542     operators.  */
6543  if (IDENTIFIER_TYPENAME_P (name))
6544    {
6545      struct cp_binding_level *level;
6546
6547      for (level = current_binding_level;
6548	   level && !level->namespace_p;
6549	   level = level->level_chain)
6550	{
6551	  tree class_type;
6552	  tree operators;
6553
6554	  /* A conversion operator can only be declared in a class
6555	     scope.  */
6556	  if (level->parm_flag != 2)
6557	    continue;
6558
6559	  /* Lookup the conversion operator in the class.  */
6560	  class_type = level->this_class;
6561	  operators = lookup_fnfields (class_type, name, /*protect=*/0);
6562	  if (operators)
6563	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
6564	}
6565
6566      POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
6567    }
6568
6569  /* First, look in non-namespace scopes.  */
6570
6571  if (current_class_type == NULL_TREE)
6572    nonclass = 1;
6573
6574  for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
6575    {
6576      tree binding;
6577
6578      if (!LOCAL_BINDING_P (iter) && nonclass)
6579	/* We're not looking for class-scoped bindings, so keep going.  */
6580	continue;
6581
6582      /* If this is the kind of thing we're looking for, we're done.  */
6583      if (qualify_lookup (BINDING_VALUE (iter), flags))
6584	binding = BINDING_VALUE (iter);
6585      else if ((flags & LOOKUP_PREFER_TYPES)
6586	       && qualify_lookup (BINDING_TYPE (iter), flags))
6587	binding = BINDING_TYPE (iter);
6588      else
6589	binding = NULL_TREE;
6590
6591      /* Handle access control on types from enclosing or base classes.  */
6592      if (binding && ! yylex
6593	  && BINDING_LEVEL (iter) && BINDING_LEVEL (iter)->parm_flag == 2)
6594	type_access_control (BINDING_LEVEL (iter)->this_class, binding);
6595
6596      if (binding
6597	  && (!val || !IMPLICIT_TYPENAME_TYPE_DECL_P (binding)))
6598	{
6599	  if (val_is_implicit_typename && !yylex)
6600	    warn_about_implicit_typename_lookup (val, binding);
6601	  val = binding;
6602	  val_is_implicit_typename
6603	    = IMPLICIT_TYPENAME_TYPE_DECL_P (val);
6604	  if (!val_is_implicit_typename)
6605	    break;
6606	}
6607    }
6608
6609  /* The name might be from an enclosing class of the current scope.  */
6610  if (!val && !nonclass && current_class_type)
6611    val = qualify_lookup (lookup_nested_field (name, !yylex), flags);
6612
6613  /* Now lookup in namespace scopes.  */
6614  if (!val || val_is_implicit_typename)
6615    {
6616      t = unqualified_namespace_lookup (name, flags, 0);
6617      if (t)
6618	{
6619	  if (val_is_implicit_typename && !yylex)
6620	    warn_about_implicit_typename_lookup (val, t);
6621	  val = t;
6622	}
6623    }
6624
6625 done:
6626  if (val)
6627    {
6628      /* This should only warn about types used in qualified-ids.  */
6629      if (from_obj && from_obj != val)
6630	{
6631	  if (looking_for_typename && TREE_CODE (from_obj) == TYPE_DECL
6632	      && TREE_CODE (val) == TYPE_DECL
6633	      && ! same_type_p (TREE_TYPE (from_obj), TREE_TYPE (val)))
6634	    pedwarn ("\
6635lookup of `%D' in the scope of `%#T' (`%#D') \
6636does not match lookup in the current scope (`%#D')",
6637			name, got_object, from_obj, val);
6638
6639	  /* We don't change val to from_obj if got_object depends on
6640	     template parms because that breaks implicit typename for
6641	     destructor calls.  */
6642	  if (! uses_template_parms (got_object))
6643	    val = from_obj;
6644	}
6645
6646      /* If we have a single function from a using decl, pull it out.  */
6647      if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
6648	val = OVL_FUNCTION (val);
6649    }
6650  else if (from_obj)
6651    val = from_obj;
6652
6653  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6654}
6655
6656tree
6657lookup_name_nonclass (name)
6658     tree name;
6659{
6660  return lookup_name_real (name, 0, 1, 0);
6661}
6662
6663tree
6664lookup_function_nonclass (name, args)
6665     tree name;
6666     tree args;
6667{
6668  return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6669}
6670
6671tree
6672lookup_name_namespace_only (name)
6673     tree name;
6674{
6675  /* type-or-namespace, nonclass, namespace_only */
6676  return lookup_name_real (name, 1, 1, 1);
6677}
6678
6679tree
6680lookup_name (name, prefer_type)
6681     tree name;
6682     int prefer_type;
6683{
6684  return lookup_name_real (name, prefer_type, 0, 0);
6685}
6686
6687/* Similar to `lookup_name' but look only in the innermost non-class
6688   binding level.  */
6689
6690tree
6691lookup_name_current_level (name)
6692     tree name;
6693{
6694  struct cp_binding_level *b;
6695  tree t = NULL_TREE;
6696
6697  timevar_push (TV_NAME_LOOKUP);
6698
6699  b = current_binding_level;
6700  while (b->parm_flag == 2)
6701    b = b->level_chain;
6702
6703  if (b->namespace_p)
6704    {
6705      t = IDENTIFIER_NAMESPACE_VALUE (name);
6706
6707      /* extern "C" function() */
6708      if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6709	t = TREE_VALUE (t);
6710    }
6711  else if (IDENTIFIER_BINDING (name)
6712	   && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6713    {
6714      while (1)
6715	{
6716	  if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6717	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
6718
6719	  if (b->keep == 2)
6720	    b = b->level_chain;
6721	  else
6722	    break;
6723	}
6724    }
6725
6726  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6727}
6728
6729/* Like lookup_name_current_level, but for types.  */
6730
6731tree
6732lookup_type_current_level (name)
6733     tree name;
6734{
6735  register tree t = NULL_TREE;
6736
6737  timevar_push (TV_NAME_LOOKUP);
6738
6739  my_friendly_assert (! current_binding_level->namespace_p, 980716);
6740
6741  if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6742      && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6743    {
6744      struct cp_binding_level *b = current_binding_level;
6745      while (1)
6746	{
6747	  if (purpose_member (name, b->type_shadowed))
6748	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
6749				    REAL_IDENTIFIER_TYPE_VALUE (name));
6750	  if (b->keep == 2)
6751	    b = b->level_chain;
6752	  else
6753	    break;
6754	}
6755    }
6756
6757  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6758}
6759
6760void
6761begin_only_namespace_names ()
6762{
6763  only_namespace_names = 1;
6764}
6765
6766void
6767end_only_namespace_names ()
6768{
6769  only_namespace_names = 0;
6770}
6771
6772/* Push the declarations of builtin types into the namespace.
6773   RID_INDEX is the index of the builtin type
6774   in the array RID_POINTERS.  NAME is the name used when looking
6775   up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6776
6777void
6778record_builtin_type (rid_index, name, type)
6779     enum rid rid_index;
6780     const char *name;
6781     tree type;
6782{
6783  tree rname = NULL_TREE, tname = NULL_TREE;
6784  tree tdecl = NULL_TREE;
6785
6786  if ((int) rid_index < (int) RID_MAX)
6787    rname = ridpointers[(int) rid_index];
6788  if (name)
6789    tname = get_identifier (name);
6790
6791  TYPE_BUILT_IN (type) = 1;
6792
6793  if (tname)
6794    {
6795      tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6796      set_identifier_type_value (tname, NULL_TREE);
6797      if ((int) rid_index < (int) RID_MAX)
6798	/* Built-in types live in the global namespace.  */
6799	SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6800    }
6801  if (rname != NULL_TREE)
6802    {
6803      if (tname != NULL_TREE)
6804	{
6805	  set_identifier_type_value (rname, NULL_TREE);
6806	  SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6807	}
6808      else
6809	{
6810	  tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6811	  set_identifier_type_value (rname, NULL_TREE);
6812	}
6813    }
6814}
6815
6816/* Record one of the standard Java types.
6817 * Declare it as having the given NAME.
6818 * If SIZE > 0, it is the size of one of the integral types;
6819 * otherwise it is the negative of the size of one of the other types.  */
6820
6821static tree
6822record_builtin_java_type (name, size)
6823     const char *name;
6824     int size;
6825{
6826  tree type, decl;
6827  if (size > 0)
6828    type = make_signed_type (size);
6829  else if (size > -32)
6830    { /* "__java_char" or ""__java_boolean".  */
6831      type = make_unsigned_type (-size);
6832      /*if (size == -1)	TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6833    }
6834  else
6835    { /* "__java_float" or ""__java_double".  */
6836      type = make_node (REAL_TYPE);
6837      TYPE_PRECISION (type) = - size;
6838      layout_type (type);
6839    }
6840  record_builtin_type (RID_MAX, name, type);
6841  decl = TYPE_NAME (type);
6842
6843  /* Suppress generate debug symbol entries for these types,
6844     since for normal C++ they are just clutter.
6845     However, push_lang_context undoes this if extern "Java" is seen.  */
6846  DECL_IGNORED_P (decl) = 1;
6847
6848  TYPE_FOR_JAVA (type) = 1;
6849  return type;
6850}
6851
6852/* Push a type into the namespace so that the back-ends ignore it.  */
6853
6854static void
6855record_unknown_type (type, name)
6856     tree type;
6857     const char *name;
6858{
6859  tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6860  /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6861  DECL_IGNORED_P (decl) = 1;
6862  TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6863  TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6864  TYPE_ALIGN (type) = 1;
6865  TYPE_USER_ALIGN (type) = 0;
6866  TYPE_MODE (type) = TYPE_MODE (void_type_node);
6867}
6868
6869/* An string for which we should create an IDENTIFIER_NODE at
6870   startup.  */
6871
6872typedef struct predefined_identifier
6873{
6874  /* The name of the identifier.  */
6875  const char *const name;
6876  /* The place where the IDENTIFIER_NODE should be stored.  */
6877  tree *const node;
6878  /* Nonzero if this is the name of a constructor or destructor.  */
6879  const int ctor_or_dtor_p;
6880} predefined_identifier;
6881
6882/* Create all the predefined identifiers.  */
6883
6884static void
6885initialize_predefined_identifiers ()
6886{
6887  const predefined_identifier *pid;
6888
6889  /* A table of identifiers to create at startup.  */
6890  static const predefined_identifier predefined_identifiers[] = {
6891    { "C++", &lang_name_cplusplus, 0 },
6892    { "C", &lang_name_c, 0 },
6893    { "Java", &lang_name_java, 0 },
6894    { CTOR_NAME, &ctor_identifier, 1 },
6895    { "__base_ctor", &base_ctor_identifier, 1 },
6896    { "__comp_ctor", &complete_ctor_identifier, 1 },
6897    { DTOR_NAME, &dtor_identifier, 1 },
6898    { "__comp_dtor", &complete_dtor_identifier, 1 },
6899    { "__base_dtor", &base_dtor_identifier, 1 },
6900    { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6901    { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6902    { "nelts", &nelts_identifier, 0 },
6903    { THIS_NAME, &this_identifier, 0 },
6904    { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6905    { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6906    { "_vptr", &vptr_identifier, 0 },
6907    { "__vtt_parm", &vtt_parm_identifier, 0 },
6908    { "std", &std_identifier, 0 },
6909    { NULL, NULL, 0 }
6910  };
6911
6912  for (pid = predefined_identifiers; pid->name; ++pid)
6913    {
6914      *pid->node = get_identifier (pid->name);
6915      if (pid->ctor_or_dtor_p)
6916	IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6917    }
6918}
6919
6920/* Create the predefined scalar types of C,
6921   and some nodes representing standard constants (0, 1, (void *)0).
6922   Initialize the global binding level.
6923   Make definitions for built-in primitive functions.  */
6924
6925void
6926cxx_init_decl_processing ()
6927{
6928  tree void_ftype;
6929  tree void_ftype_ptr;
6930
6931  /* Create all the identifiers we need.  */
6932  initialize_predefined_identifiers ();
6933
6934  /* Fill in back-end hooks.  */
6935  lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6936
6937  /* Create the global variables.  */
6938  push_to_top_level ();
6939
6940  /* Enter the global namespace.  */
6941  my_friendly_assert (global_namespace == NULL_TREE, 375);
6942  push_namespace (get_identifier ("::"));
6943  global_namespace = current_namespace;
6944  current_lang_name = NULL_TREE;
6945
6946  /* Adjust various flags based on command-line settings.  */
6947  if (!flag_permissive)
6948    flag_pedantic_errors = 1;
6949  if (!flag_no_inline)
6950    {
6951      flag_inline_trees = 1;
6952      flag_no_inline = 1;
6953    }
6954  if (flag_inline_functions)
6955    {
6956      flag_inline_trees = 2;
6957      flag_inline_functions = 0;
6958    }
6959
6960  /* Force minimum function alignment if using the least significant
6961     bit of function pointers to store the virtual bit.  */
6962  if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6963      && force_align_functions_log < 1)
6964    force_align_functions_log = 1;
6965
6966  /* Initially, C.  */
6967  current_lang_name = lang_name_c;
6968
6969  current_function_decl = NULL_TREE;
6970  current_binding_level = NULL_BINDING_LEVEL;
6971  free_binding_level = NULL_BINDING_LEVEL;
6972
6973  build_common_tree_nodes (flag_signed_char);
6974
6975  error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6976  TREE_TYPE (error_mark_list) = error_mark_node;
6977
6978  /* Make the binding_level structure for global names.  */
6979  pushlevel (0);
6980  current_binding_level->type_decls = binding_table_new (GLOBAL_SCOPE_HT_SIZE);
6981  /* The global level is the namespace level of ::.  */
6982  NAMESPACE_LEVEL (global_namespace) = current_binding_level;
6983  declare_namespace_level ();
6984
6985  VARRAY_TREE_INIT (current_binding_level->static_decls,
6986		    200,
6987		    "Static declarations");
6988
6989  /* Create the `std' namespace.  */
6990  push_namespace (std_identifier);
6991  std_node = current_namespace;
6992  pop_namespace ();
6993
6994  c_common_nodes_and_builtins ();
6995
6996  java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6997  java_short_type_node = record_builtin_java_type ("__java_short", 16);
6998  java_int_type_node = record_builtin_java_type ("__java_int", 32);
6999  java_long_type_node = record_builtin_java_type ("__java_long", 64);
7000  java_float_type_node = record_builtin_java_type ("__java_float", -32);
7001  java_double_type_node = record_builtin_java_type ("__java_double", -64);
7002  java_char_type_node = record_builtin_java_type ("__java_char", -16);
7003  java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
7004
7005  integer_two_node = build_int_2 (2, 0);
7006  TREE_TYPE (integer_two_node) = integer_type_node;
7007  integer_three_node = build_int_2 (3, 0);
7008  TREE_TYPE (integer_three_node) = integer_type_node;
7009
7010  boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
7011  TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
7012  TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
7013  TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
7014  TYPE_PRECISION (boolean_type_node) = 1;
7015  record_builtin_type (RID_BOOL, "bool", boolean_type_node);
7016  boolean_false_node = build_int_2 (0, 0);
7017  TREE_TYPE (boolean_false_node) = boolean_type_node;
7018  boolean_true_node = build_int_2 (1, 0);
7019  TREE_TYPE (boolean_true_node) = boolean_type_node;
7020
7021  empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
7022
7023#if 0
7024  record_builtin_type (RID_MAX, NULL, string_type_node);
7025#endif
7026
7027  delta_type_node = ptrdiff_type_node;
7028  vtable_index_type = ptrdiff_type_node;
7029
7030  vtt_parm_type = build_pointer_type (const_ptr_type_node);
7031  void_ftype = build_function_type (void_type_node, void_list_node);
7032  void_ftype_ptr = build_function_type (void_type_node,
7033					tree_cons (NULL_TREE,
7034						   ptr_type_node,
7035						   void_list_node));
7036  void_ftype_ptr
7037    = build_exception_variant (void_ftype_ptr, empty_except_spec);
7038
7039  /* C++ extensions */
7040
7041  unknown_type_node = make_node (UNKNOWN_TYPE);
7042  record_unknown_type (unknown_type_node, "unknown type");
7043
7044  /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
7045  TREE_TYPE (unknown_type_node) = unknown_type_node;
7046
7047  /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
7048     result.  */
7049  TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
7050  TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
7051
7052  {
7053    /* Make sure we get a unique function type, so we can give
7054       its pointer type a name.  (This wins for gdb.) */
7055    tree vfunc_type = make_node (FUNCTION_TYPE);
7056    TREE_TYPE (vfunc_type) = integer_type_node;
7057    TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
7058    layout_type (vfunc_type);
7059
7060    vtable_entry_type = build_pointer_type (vfunc_type);
7061  }
7062  record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
7063
7064  vtbl_type_node
7065    = build_cplus_array_type (vtable_entry_type, NULL_TREE);
7066  layout_type (vtbl_type_node);
7067  vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
7068  record_builtin_type (RID_MAX, NULL, vtbl_type_node);
7069  vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
7070  layout_type (vtbl_ptr_type_node);
7071  record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
7072
7073  push_namespace (get_identifier ("__cxxabiv1"));
7074  abi_node = current_namespace;
7075  pop_namespace ();
7076
7077  global_type_node = make_node (LANG_TYPE);
7078  record_unknown_type (global_type_node, "global type");
7079
7080  /* Now, C++.  */
7081  current_lang_name = lang_name_cplusplus;
7082
7083  {
7084    tree bad_alloc_type_node, newtype, deltype;
7085    tree ptr_ftype_sizetype;
7086
7087    push_namespace (std_identifier);
7088    bad_alloc_type_node
7089      = xref_tag (class_type, get_identifier ("bad_alloc"),
7090		  /*attributes=*/NULL_TREE, 1);
7091    pop_namespace ();
7092    ptr_ftype_sizetype
7093      = build_function_type (ptr_type_node,
7094			     tree_cons (NULL_TREE,
7095					size_type_node,
7096					void_list_node));
7097    newtype = build_exception_variant
7098      (ptr_ftype_sizetype, add_exception_specifier
7099       (NULL_TREE, bad_alloc_type_node, -1));
7100    deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
7101    push_cp_library_fn (NEW_EXPR, newtype);
7102    push_cp_library_fn (VEC_NEW_EXPR, newtype);
7103    global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
7104    push_cp_library_fn (VEC_DELETE_EXPR, deltype);
7105  }
7106
7107  abort_fndecl
7108    = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
7109
7110  /* Perform other language dependent initializations.  */
7111  init_class_processing ();
7112  init_search_processing ();
7113  init_rtti_processing ();
7114
7115  if (flag_exceptions)
7116    init_exception_processing ();
7117
7118  if (! supports_one_only ())
7119    flag_weak = 0;
7120
7121  make_fname_decl = cp_make_fname_decl;
7122  start_fname_decls ();
7123
7124  /* Show we use EH for cleanups.  */
7125  using_eh_for_cleanups ();
7126
7127  /* Maintain consistency.  Perhaps we should just complain if they
7128     say -fwritable-strings?  */
7129  if (flag_writable_strings)
7130    flag_const_strings = 0;
7131}
7132
7133/* Generate an initializer for a function naming variable from
7134   NAME. NAME may be NULL, in which case we generate a special
7135   ERROR_MARK node which should be replaced later.  */
7136
7137tree
7138cp_fname_init (name)
7139     const char *name;
7140{
7141  tree domain = NULL_TREE;
7142  tree type;
7143  tree init = NULL_TREE;
7144  size_t length = 0;
7145
7146  if (name)
7147    {
7148      length = strlen (name);
7149      domain = build_index_type (size_int (length));
7150      init = build_string (length + 1, name);
7151    }
7152
7153  type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
7154  type = build_cplus_array_type (type, domain);
7155
7156  if (init)
7157    TREE_TYPE (init) = type;
7158  else
7159    /* We don't know the value until instantiation time. Make
7160       something which will be digested now, but replaced later.  */
7161    init = build (ERROR_MARK, type);
7162
7163  return init;
7164}
7165
7166/* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
7167   decl, NAME is the initialization string and TYPE_DEP indicates whether
7168   NAME depended on the type of the function. We make use of that to detect
7169   __PRETTY_FUNCTION__ inside a template fn. This is being done
7170   lazily at the point of first use, so we musn't push the decl now.  */
7171
7172static tree
7173cp_make_fname_decl (id, type_dep)
7174     tree id;
7175     int type_dep;
7176{
7177  const char *const name = (type_dep && processing_template_decl
7178		      ? NULL : fname_as_string (type_dep));
7179  tree init = cp_fname_init (name);
7180  tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
7181
7182  /* As we don't push the decl here, we must set the context.  */
7183  DECL_CONTEXT (decl) = current_function_decl;
7184  DECL_PRETTY_FUNCTION_P (decl) = type_dep;
7185
7186  TREE_STATIC (decl) = 1;
7187  TREE_READONLY (decl) = 1;
7188  DECL_ARTIFICIAL (decl) = 1;
7189  DECL_INITIAL (decl) = init;
7190
7191  TREE_USED (decl) = 1;
7192
7193  cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
7194
7195  return decl;
7196}
7197
7198/* Make a definition for a builtin function named NAME in the current
7199   namespace, whose data type is TYPE and whose context is CONTEXT.
7200   TYPE should be a function type with argument types.
7201
7202   CLASS and CODE tell later passes how to compile calls to this function.
7203   See tree.h for possible values.
7204
7205   If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
7206   the name to be called if we can't opencode the function.
7207   If ATTRS is nonzero, use that for the function's attribute
7208   list.  */
7209
7210static tree
7211builtin_function_1 (name, type, context, code, class, libname, attrs)
7212     const char *name;
7213     tree type;
7214     tree context;
7215     int code;
7216     enum built_in_class class;
7217     const char *libname;
7218     tree attrs;
7219{
7220  tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
7221  DECL_BUILT_IN_CLASS (decl) = class;
7222  DECL_FUNCTION_CODE (decl) = code;
7223  DECL_CONTEXT (decl) = context;
7224
7225  pushdecl (decl);
7226
7227  /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
7228     we cannot change DECL_ASSEMBLER_NAME until we have installed this
7229     function in the namespace.  */
7230  if (libname)
7231    SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
7232  make_decl_rtl (decl, NULL);
7233
7234  /* Warn if a function in the namespace for users
7235     is used without an occasion to consider it declared.  */
7236  if (name[0] != '_' || name[1] != '_')
7237    DECL_ANTICIPATED (decl) = 1;
7238
7239  /* Possibly apply some default attributes to this built-in function.  */
7240  if (attrs)
7241    decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
7242  else
7243    decl_attributes (&decl, NULL_TREE, 0);
7244
7245  return decl;
7246}
7247
7248/* Entry point for the benefit of c_common_nodes_and_builtins.
7249
7250   Make a defintion for a builtin function named NAME and whose data type
7251   is TYPE.  TYPE should be a function type with argument types.  This
7252   function places the anticipated declaration in the global namespace
7253   and additionally in the std namespace if appropriate.
7254
7255   CLASS and CODE tell later passes how to compile calls to this function.
7256   See tree.h for possible values.
7257
7258   If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
7259   the name to be called if we can't opencode the function.
7260
7261   If ATTRS is nonzero, use that for the function's attribute
7262   list.  */
7263
7264tree
7265builtin_function (name, type, code, class, libname, attrs)
7266     const char *name;
7267     tree type;
7268     int code;
7269     enum built_in_class class;
7270     const char *libname;
7271     tree attrs;
7272{
7273  /* All builtins that don't begin with an '_' should additionally
7274     go in the 'std' namespace.  */
7275  if (name[0] != '_')
7276    {
7277      push_namespace (std_identifier);
7278      builtin_function_1 (name, type, std_node, code, class, libname, attrs);
7279      pop_namespace ();
7280    }
7281
7282  return builtin_function_1 (name, type, NULL_TREE, code,
7283			     class, libname, attrs);
7284}
7285
7286/* Generate a FUNCTION_DECL with the typical flags for a runtime library
7287   function.  Not called directly.  */
7288
7289static tree
7290build_library_fn_1 (name, operator_code, type)
7291     tree name;
7292     enum tree_code operator_code;
7293     tree type;
7294{
7295  tree fn = build_lang_decl (FUNCTION_DECL, name, type);
7296  DECL_EXTERNAL (fn) = 1;
7297  TREE_PUBLIC (fn) = 1;
7298  DECL_ARTIFICIAL (fn) = 1;
7299  TREE_NOTHROW (fn) = 1;
7300  SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
7301  SET_DECL_LANGUAGE (fn, lang_c);
7302  return fn;
7303}
7304
7305/* Returns the _DECL for a library function with C linkage.
7306   We assume that such functions never throw; if this is incorrect,
7307   callers should unset TREE_NOTHROW.  */
7308
7309tree
7310build_library_fn (name, type)
7311     tree name;
7312     tree type;
7313{
7314  return build_library_fn_1 (name, ERROR_MARK, type);
7315}
7316
7317/* Returns the _DECL for a library function with C++ linkage.  */
7318
7319static tree
7320build_cp_library_fn (name, operator_code, type)
7321     tree name;
7322     enum tree_code operator_code;
7323     tree type;
7324{
7325  tree fn = build_library_fn_1 (name, operator_code, type);
7326  TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
7327  DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
7328  SET_DECL_LANGUAGE (fn, lang_cplusplus);
7329  set_mangled_name_for_decl (fn);
7330  return fn;
7331}
7332
7333/* Like build_library_fn, but takes a C string instead of an
7334   IDENTIFIER_NODE.  */
7335
7336tree
7337build_library_fn_ptr (name, type)
7338     const char *name;
7339     tree type;
7340{
7341  return build_library_fn (get_identifier (name), type);
7342}
7343
7344/* Like build_cp_library_fn, but takes a C string instead of an
7345   IDENTIFIER_NODE.  */
7346
7347tree
7348build_cp_library_fn_ptr (name, type)
7349     const char *name;
7350     tree type;
7351{
7352  return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
7353}
7354
7355/* Like build_library_fn, but also pushes the function so that we will
7356   be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
7357
7358tree
7359push_library_fn (name, type)
7360     tree name, type;
7361{
7362  tree fn = build_library_fn (name, type);
7363  pushdecl_top_level (fn);
7364  return fn;
7365}
7366
7367/* Like build_cp_library_fn, but also pushes the function so that it
7368   will be found by normal lookup.  */
7369
7370static tree
7371push_cp_library_fn (operator_code, type)
7372     enum tree_code operator_code;
7373     tree type;
7374{
7375  tree fn = build_cp_library_fn (ansi_opname (operator_code),
7376				 operator_code,
7377				 type);
7378  pushdecl (fn);
7379  return fn;
7380}
7381
7382/* Like push_library_fn, but takes a TREE_LIST of parm types rather than
7383   a FUNCTION_TYPE.  */
7384
7385tree
7386push_void_library_fn (name, parmtypes)
7387     tree name, parmtypes;
7388{
7389  tree type = build_function_type (void_type_node, parmtypes);
7390  return push_library_fn (name, type);
7391}
7392
7393/* Like push_library_fn, but also note that this function throws
7394   and does not return.  Used for __throw_foo and the like.  */
7395
7396tree
7397push_throw_library_fn (name, type)
7398     tree name, type;
7399{
7400  tree fn = push_library_fn (name, type);
7401  TREE_THIS_VOLATILE (fn) = 1;
7402  TREE_NOTHROW (fn) = 0;
7403  return fn;
7404}
7405
7406/* Apply default attributes to a function, if a system function with default
7407   attributes.  */
7408
7409void
7410cxx_insert_default_attributes (decl)
7411     tree decl;
7412{
7413  if (!DECL_EXTERN_C_FUNCTION_P (decl))
7414    return;
7415  if (!TREE_PUBLIC (decl))
7416    return;
7417  c_common_insert_default_attributes (decl);
7418}
7419
7420/* When we call finish_struct for an anonymous union, we create
7421   default copy constructors and such.  But, an anonymous union
7422   shouldn't have such things; this function undoes the damage to the
7423   anonymous union type T.
7424
7425   (The reason that we create the synthesized methods is that we don't
7426   distinguish `union { int i; }' from `typedef union { int i; } U'.
7427   The first is an anonymous union; the second is just an ordinary
7428   union type.)  */
7429
7430void
7431fixup_anonymous_aggr (t)
7432     tree t;
7433{
7434  tree *q;
7435
7436  /* Wipe out memory of synthesized methods */
7437  TYPE_HAS_CONSTRUCTOR (t) = 0;
7438  TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
7439  TYPE_HAS_INIT_REF (t) = 0;
7440  TYPE_HAS_CONST_INIT_REF (t) = 0;
7441  TYPE_HAS_ASSIGN_REF (t) = 0;
7442  TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
7443
7444  /* Splice the implicitly generated functions out of the TYPE_METHODS
7445     list.  */
7446  q = &TYPE_METHODS (t);
7447  while (*q)
7448    {
7449      if (DECL_ARTIFICIAL (*q))
7450	*q = TREE_CHAIN (*q);
7451      else
7452	q = &TREE_CHAIN (*q);
7453    }
7454
7455  /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
7456  if (TYPE_METHODS (t))
7457    cp_error_at ("an anonymous union cannot have function members", t);
7458
7459  /* Anonymous aggregates cannot have fields with ctors, dtors or complex
7460     assignment operators (because they cannot have these methods themselves).
7461     For anonymous unions this is already checked because they are not allowed
7462     in any union, otherwise we have to check it.  */
7463  if (TREE_CODE (t) != UNION_TYPE)
7464    {
7465      tree field, type;
7466
7467      for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
7468	if (TREE_CODE (field) == FIELD_DECL)
7469	  {
7470	    type = TREE_TYPE (field);
7471	    if (CLASS_TYPE_P (type))
7472	      {
7473	        if (TYPE_NEEDS_CONSTRUCTING (type))
7474		  cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
7475			       field);
7476		if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7477		  cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
7478			       field);
7479		if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
7480		  cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
7481			       field);
7482	      }
7483	  }
7484    }
7485}
7486
7487/* Make sure that a declaration with no declarator is well-formed, i.e.
7488   just defines a tagged type or anonymous union.
7489
7490   Returns the type defined, if any.  */
7491
7492tree
7493check_tag_decl (declspecs)
7494     tree declspecs;
7495{
7496  int found_type = 0;
7497  int saw_friend = 0;
7498  int saw_typedef = 0;
7499  tree ob_modifier = NULL_TREE;
7500  register tree link;
7501  register tree t = NULL_TREE;
7502
7503  for (link = declspecs; link; link = TREE_CHAIN (link))
7504    {
7505      register tree value = TREE_VALUE (link);
7506
7507      if (TYPE_P (value)
7508	  || TREE_CODE (value) == TYPE_DECL
7509	  || (TREE_CODE (value) == IDENTIFIER_NODE
7510	      && IDENTIFIER_GLOBAL_VALUE (value)
7511	      && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
7512	{
7513	  ++found_type;
7514
7515	  if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
7516	    {
7517	      if (! in_system_header)
7518		pedwarn ("redeclaration of C++ built-in type `%T'", value);
7519	      return NULL_TREE;
7520	    }
7521
7522	  if (TYPE_P (value)
7523	      && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
7524		  || TREE_CODE (value) == ENUMERAL_TYPE))
7525	    {
7526	      my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
7527	      t = value;
7528	    }
7529	}
7530      else if (value == ridpointers[(int) RID_TYPEDEF])
7531        saw_typedef = 1;
7532      else if (value == ridpointers[(int) RID_FRIEND])
7533	{
7534	  if (current_class_type == NULL_TREE
7535	      || current_scope () != current_class_type)
7536	    ob_modifier = value;
7537	  else
7538	    saw_friend = 1;
7539	}
7540      else if (value == ridpointers[(int) RID_STATIC]
7541	       || value == ridpointers[(int) RID_EXTERN]
7542	       || value == ridpointers[(int) RID_AUTO]
7543	       || value == ridpointers[(int) RID_REGISTER]
7544	       || value == ridpointers[(int) RID_INLINE]
7545	       || value == ridpointers[(int) RID_VIRTUAL]
7546	       || value == ridpointers[(int) RID_CONST]
7547	       || value == ridpointers[(int) RID_VOLATILE]
7548	       || value == ridpointers[(int) RID_EXPLICIT]
7549	       || value == ridpointers[(int) RID_THREAD])
7550	ob_modifier = value;
7551    }
7552
7553  if (found_type > 1)
7554    error ("multiple types in one declaration");
7555
7556  if (t == NULL_TREE && ! saw_friend)
7557    pedwarn ("declaration does not declare anything");
7558
7559  /* Check for an anonymous union.  */
7560  else if (t && IS_AGGR_TYPE_CODE (TREE_CODE (t))
7561	   && TYPE_ANONYMOUS_P (t))
7562    {
7563      /* 7/3 In a simple-declaration, the optional init-declarator-list
7564         can be omitted only when declaring a class (clause 9) or
7565         enumeration (7.2), that is, when the decl-specifier-seq contains
7566         either a class-specifier, an elaborated-type-specifier with
7567         a class-key (9.1), or an enum-specifier.  In these cases and
7568         whenever a class-specifier or enum-specifier is present in the
7569         decl-specifier-seq, the identifiers in these specifiers are among
7570         the names being declared by the declaration (as class-name,
7571         enum-names, or enumerators, depending on the syntax).  In such
7572         cases, and except for the declaration of an unnamed bit-field (9.6),
7573         the decl-specifier-seq shall introduce one or more names into the
7574         program, or shall redeclare a name introduced by a previous
7575         declaration.  [Example:
7576             enum { };            // ill-formed
7577             typedef class { };   // ill-formed
7578         --end example]  */
7579      if (saw_typedef)
7580        {
7581          error ("missing type-name in typedef-declaration");
7582          return NULL_TREE;
7583        }
7584      /* Anonymous unions are objects, so they can have specifiers.  */;
7585      SET_ANON_AGGR_TYPE_P (t);
7586
7587      if (TREE_CODE (t) != UNION_TYPE && pedantic && ! in_system_header)
7588	pedwarn ("ISO C++ prohibits anonymous structs");
7589    }
7590
7591  else if (ob_modifier)
7592    {
7593      if (ob_modifier == ridpointers[(int) RID_INLINE]
7594	  || ob_modifier == ridpointers[(int) RID_VIRTUAL])
7595	error ("`%D' can only be specified for functions", ob_modifier);
7596      else if (ob_modifier == ridpointers[(int) RID_FRIEND])
7597	error ("`%D' can only be specified inside a class", ob_modifier);
7598      else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
7599	error ("`%D' can only be specified for constructors",
7600		  ob_modifier);
7601      else
7602	error ("`%D' can only be specified for objects and functions",
7603		  ob_modifier);
7604    }
7605
7606  return t;
7607}
7608
7609/* Called when a declaration is seen that contains no names to declare.
7610   If its type is a reference to a structure, union or enum inherited
7611   from a containing scope, shadow that tag name for the current scope
7612   with a forward reference.
7613   If its type defines a new named structure or union
7614   or defines an enum, it is valid but we need not do anything here.
7615   Otherwise, it is an error.
7616
7617   C++: may have to grok the declspecs to learn about static,
7618   complain for anonymous unions.  */
7619
7620void
7621shadow_tag (declspecs)
7622     tree declspecs;
7623{
7624  tree t = check_tag_decl (declspecs);
7625
7626  if (t)
7627    maybe_process_partial_specialization (t);
7628
7629  /* This is where the variables in an anonymous union are
7630     declared.  An anonymous union declaration looks like:
7631     union { ... } ;
7632     because there is no declarator after the union, the parser
7633     sends that declaration here.  */
7634  if (t && ANON_AGGR_TYPE_P (t))
7635    {
7636      fixup_anonymous_aggr (t);
7637
7638      if (TYPE_FIELDS (t))
7639	{
7640	  tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
7641				      NULL);
7642	  finish_anon_union (decl);
7643	}
7644    }
7645}
7646
7647/* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
7648
7649tree
7650groktypename (typename)
7651     tree typename;
7652{
7653  tree specs, attrs;
7654  tree type;
7655  if (TREE_CODE (typename) != TREE_LIST)
7656    return typename;
7657  split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
7658  type = grokdeclarator (TREE_VALUE (typename), specs,
7659			 TYPENAME, 0, &attrs);
7660  if (attrs)
7661    cplus_decl_attributes (&type, attrs, 0);
7662  return type;
7663}
7664
7665/* Decode a declarator in an ordinary declaration or data definition.
7666   This is called as soon as the type information and variable name
7667   have been parsed, before parsing the initializer if any.
7668   Here we create the ..._DECL node, fill in its type,
7669   and put it on the list of decls for the current context.
7670   The ..._DECL node is returned as the value.
7671
7672   Exception: for arrays where the length is not specified,
7673   the type is left null, to be filled in by `cp_finish_decl'.
7674
7675   Function definitions do not come here; they go to start_function
7676   instead.  However, external and forward declarations of functions
7677   do go through here.  Structure field declarations are done by
7678   grokfield and not through here.  */
7679
7680tree
7681start_decl (declarator, declspecs, initialized, attributes, prefix_attributes)
7682     tree declarator, declspecs;
7683     int initialized;
7684     tree attributes, prefix_attributes;
7685{
7686  tree decl;
7687  register tree type, tem;
7688  tree context;
7689
7690#if 0
7691  /* See code below that used this.  */
7692  int init_written = initialized;
7693#endif
7694
7695  /* This should only be done once on the top most decl.  */
7696  if (have_extern_spec)
7697    {
7698      declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
7699			     declspecs);
7700      have_extern_spec = false;
7701    }
7702
7703  /* An object declared as __attribute__((deprecated)) suppresses
7704     warnings of uses of other deprecated items.  */
7705  if (lookup_attribute ("deprecated", attributes))
7706    deprecated_state = DEPRECATED_SUPPRESS;
7707
7708  attributes = chainon (attributes, prefix_attributes);
7709
7710  decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7711			 &attributes);
7712
7713  deprecated_state = DEPRECATED_NORMAL;
7714
7715  if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7716    return NULL_TREE;
7717
7718  type = TREE_TYPE (decl);
7719
7720  if (type == error_mark_node)
7721    return NULL_TREE;
7722
7723  context = DECL_CONTEXT (decl);
7724
7725  if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7726      && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7727    {
7728      /* When parsing the initializer, lookup should use the object's
7729	 namespace.  */
7730      push_decl_namespace (context);
7731    }
7732
7733  /* We are only interested in class contexts, later.  */
7734  if (context && TREE_CODE (context) == NAMESPACE_DECL)
7735    context = NULL_TREE;
7736
7737  if (initialized)
7738    /* Is it valid for this decl to have an initializer at all?
7739       If not, set INITIALIZED to zero, which will indirectly
7740       tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7741    switch (TREE_CODE (decl))
7742      {
7743      case TYPE_DECL:
7744	error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7745	initialized = 0;
7746	break;
7747
7748      case FUNCTION_DECL:
7749	error ("function `%#D' is initialized like a variable", decl);
7750	initialized = 0;
7751	break;
7752
7753      default:
7754	break;
7755      }
7756
7757  if (initialized)
7758    {
7759      if (! toplevel_bindings_p ()
7760	  && DECL_EXTERNAL (decl))
7761	warning ("declaration of `%#D' has `extern' and is initialized",
7762		    decl);
7763      DECL_EXTERNAL (decl) = 0;
7764      if (toplevel_bindings_p ())
7765	TREE_STATIC (decl) = 1;
7766
7767      /* Tell `pushdecl' this is an initialized decl
7768	 even though we don't yet have the initializer expression.
7769	 Also tell `cp_finish_decl' it may store the real initializer.  */
7770      DECL_INITIAL (decl) = error_mark_node;
7771    }
7772
7773  /* Set attributes here so if duplicate decl, will have proper attributes.  */
7774  cplus_decl_attributes (&decl, attributes, 0);
7775
7776  /* If #pragma weak was used, mark the decl weak now.  */
7777  if (global_scope_p (current_binding_level))
7778    maybe_apply_pragma_weak (decl);
7779
7780  if (TREE_CODE (decl) == FUNCTION_DECL
7781      && DECL_DECLARED_INLINE_P (decl)
7782      && DECL_UNINLINABLE (decl)
7783      && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7784    warning_with_decl (decl,
7785		       "inline function `%s' given attribute noinline");
7786
7787  if (context && COMPLETE_TYPE_P (complete_type (context)))
7788    {
7789      push_nested_class (context, 2);
7790
7791      if (TREE_CODE (decl) == VAR_DECL)
7792	{
7793	  tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7794	  if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7795	    error ("`%#D' is not a static member of `%#T'", decl, context);
7796	  else
7797	    {
7798	      if (DECL_CONTEXT (field) != context)
7799		{
7800		  pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7801			      DECL_CONTEXT (field), DECL_NAME (decl),
7802			      context, DECL_NAME (decl));
7803		  DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7804		}
7805	      /* Static data member are tricky; an in-class initialization
7806		 still doesn't provide a definition, so the in-class
7807		 declaration will have DECL_EXTERNAL set, but will have an
7808		 initialization.  Thus, duplicate_decls won't warn
7809		 about this situation, and so we check here.  */
7810	      if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7811		error ("duplicate initialization of %D", decl);
7812	      if (duplicate_decls (decl, field))
7813		decl = field;
7814	    }
7815	}
7816      else
7817	{
7818	  tree field = check_classfn (context, decl);
7819	  if (field && duplicate_decls (decl, field))
7820	    decl = field;
7821	}
7822
7823      /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7824      DECL_IN_AGGR_P (decl) = 0;
7825      if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7826	  || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7827	{
7828	  SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7829	  /* [temp.expl.spec] An explicit specialization of a static data
7830	     member of a template is a definition if the declaration
7831	     includes an initializer; otherwise, it is a declaration.
7832
7833	     We check for processing_specialization so this only applies
7834	     to the new specialization syntax.  */
7835	  if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7836	    DECL_EXTERNAL (decl) = 1;
7837	}
7838
7839      if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7840	pedwarn ("declaration of `%#D' outside of class is not definition",
7841		    decl);
7842    }
7843
7844  /* Enter this declaration into the symbol table.  */
7845  tem = maybe_push_decl (decl);
7846
7847  if (processing_template_decl)
7848    tem = push_template_decl (tem);
7849
7850#if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7851  /* Tell the back-end to use or not use .common as appropriate.  If we say
7852     -fconserve-space, we want this to save .data space, at the expense of
7853     wrong semantics.  If we say -fno-conserve-space, we want this to
7854     produce errors about redefs; to do this we force variables into the
7855     data segment.  */
7856  DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7857			|| !DECL_THREAD_LOCAL (tem))
7858		       && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7859#endif
7860
7861  if (! processing_template_decl)
7862    start_decl_1 (tem);
7863
7864  return tem;
7865}
7866
7867void
7868start_decl_1 (decl)
7869     tree decl;
7870{
7871  tree type = TREE_TYPE (decl);
7872  int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7873
7874  if (type == error_mark_node)
7875    return;
7876
7877  if (initialized)
7878    /* Is it valid for this decl to have an initializer at all?
7879       If not, set INITIALIZED to zero, which will indirectly
7880       tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7881    {
7882      /* Don't allow initializations for incomplete types except for
7883	 arrays which might be completed by the initialization.  */
7884      if (COMPLETE_TYPE_P (complete_type (type)))
7885	;			/* A complete type is ok.  */
7886      else if (TREE_CODE (type) != ARRAY_TYPE)
7887	{
7888	  error ("variable `%#D' has initializer but incomplete type",
7889		    decl);
7890	  initialized = 0;
7891	  type = TREE_TYPE (decl) = error_mark_node;
7892	}
7893      else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7894	{
7895	  if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7896	    error ("elements of array `%#D' have incomplete type", decl);
7897	  /* else we already gave an error in start_decl.  */
7898	  initialized = 0;
7899	}
7900    }
7901
7902  if (!initialized
7903      && TREE_CODE (decl) != TYPE_DECL
7904      && TREE_CODE (decl) != TEMPLATE_DECL
7905      && type != error_mark_node
7906      && IS_AGGR_TYPE (type)
7907      && ! DECL_EXTERNAL (decl))
7908    {
7909      if ((! processing_template_decl || ! uses_template_parms (type))
7910	  && !COMPLETE_TYPE_P (complete_type (type)))
7911	{
7912	  error ("aggregate `%#D' has incomplete type and cannot be defined",
7913		 decl);
7914	  /* Change the type so that assemble_variable will give
7915	     DECL an rtl we can live with: (mem (const_int 0)).  */
7916	  type = TREE_TYPE (decl) = error_mark_node;
7917	}
7918      else
7919	{
7920	  /* If any base type in the hierarchy of TYPE needs a constructor,
7921	     then we set initialized to 1.  This way any nodes which are
7922	     created for the purposes of initializing this aggregate
7923	     will live as long as it does.  This is necessary for global
7924	     aggregates which do not have their initializers processed until
7925	     the end of the file.  */
7926	  initialized = TYPE_NEEDS_CONSTRUCTING (type);
7927	}
7928    }
7929
7930  if (! initialized)
7931    DECL_INITIAL (decl) = NULL_TREE;
7932
7933  /* Create a new scope to hold this declaration if necessary.
7934     Whether or not a new scope is necessary cannot be determined
7935     until after the type has been completed; if the type is a
7936     specialization of a class template it is not until after
7937     instantiation has occurred that TYPE_HAS_NONTRIVIAL_DESTRUCTOR
7938     will be set correctly.  */
7939  maybe_push_cleanup_level (type);
7940}
7941
7942/* Handle initialization of references.  DECL, TYPE, and INIT have the
7943   same meaning as in cp_finish_decl.  *CLEANUP must be NULL on entry,
7944   but will be set to a new CLEANUP_STMT if a temporary is created
7945   that must be destroeyd subsequently.
7946
7947   Returns an initializer expression to use to initialize DECL, or
7948   NULL if the initialization can be performed statically.
7949
7950   Quotes on semantics can be found in ARM 8.4.3.  */
7951
7952static tree
7953grok_reference_init (tree decl, tree type, tree init, tree *cleanup)
7954{
7955  tree tmp;
7956
7957  if (init == NULL_TREE)
7958    {
7959      if ((DECL_LANG_SPECIFIC (decl) == 0
7960	   || DECL_IN_AGGR_P (decl) == 0)
7961	  && ! DECL_THIS_EXTERN (decl))
7962	error ("`%D' declared as reference but not initialized", decl);
7963      return NULL_TREE;
7964    }
7965
7966  if (init == error_mark_node)
7967    return NULL_TREE;
7968
7969  if (TREE_CODE (init) == CONSTRUCTOR)
7970    {
7971      error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7972      return NULL_TREE;
7973    }
7974
7975  if (TREE_CODE (init) == TREE_LIST)
7976    init = build_compound_expr (init);
7977
7978  if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7979    init = convert_from_reference (init);
7980
7981  if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7982      && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7983    {
7984      /* Note: default conversion is only called in very special cases.  */
7985      init = default_conversion (init);
7986    }
7987
7988  /* Convert INIT to the reference type TYPE.  This may involve the
7989     creation of a temporary, whose lifetime must be the same as that
7990     of the reference.  If so, a DECL_STMT for the temporary will be
7991     added just after the DECL_STMT for DECL.  That's why we don't set
7992     DECL_INITIAL for local references (instead assigning to them
7993     explicitly); we need to allow the temporary to be initialized
7994     first.  */
7995  tmp = initialize_reference (type, init, decl, cleanup);
7996
7997  if (tmp == error_mark_node)
7998    return NULL_TREE;
7999  else if (tmp == NULL_TREE)
8000    {
8001      error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
8002      return NULL_TREE;
8003    }
8004
8005  if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
8006    return tmp;
8007
8008  DECL_INITIAL (decl) = tmp;
8009
8010  return NULL_TREE;
8011}
8012
8013/* When parsing `int a[] = {1, 2};' we don't know the size of the
8014   array until we finish parsing the initializer.  If that's the
8015   situation we're in, update DECL accordingly.  */
8016
8017static void
8018maybe_deduce_size_from_array_init (decl, init)
8019     tree decl;
8020     tree init;
8021{
8022  tree type = TREE_TYPE (decl);
8023
8024  if (TREE_CODE (type) == ARRAY_TYPE
8025      && TYPE_DOMAIN (type) == NULL_TREE
8026      && TREE_CODE (decl) != TYPE_DECL)
8027    {
8028      /* do_default is really a C-ism to deal with tentative definitions.
8029	 But let's leave it here to ease the eventual merge.  */
8030      int do_default = !DECL_EXTERNAL (decl);
8031      tree initializer = init ? init : DECL_INITIAL (decl);
8032      int failure = complete_array_type (type, initializer, do_default);
8033
8034      if (failure == 1)
8035	error ("initializer fails to determine size of `%D'", decl);
8036
8037      if (failure == 2)
8038	{
8039	  if (do_default)
8040	    error ("array size missing in `%D'", decl);
8041	  /* If a `static' var's size isn't known, make it extern as
8042	     well as static, so it does not get allocated.  If it's not
8043	     `static', then don't mark it extern; finish_incomplete_decl
8044	     will give it a default size and it will get allocated.  */
8045	  else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
8046	    DECL_EXTERNAL (decl) = 1;
8047	}
8048
8049      if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
8050	  && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
8051			      integer_zero_node))
8052	error ("zero-size array `%D'", decl);
8053
8054      layout_decl (decl, 0);
8055    }
8056}
8057
8058/* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
8059   any appropriate error messages regarding the layout.  */
8060
8061static void
8062layout_var_decl (decl)
8063     tree decl;
8064{
8065  tree type = TREE_TYPE (decl);
8066#if 0
8067  tree ttype = target_type (type);
8068#endif
8069
8070  /* If we haven't already layed out this declaration, do so now.
8071     Note that we must not call complete type for an external object
8072     because it's type might involve templates that we are not
8073     supposed to isntantiate yet.  (And it's perfectly valid to say
8074     `extern X x' for some incomplete type `X'.)  */
8075  if (!DECL_EXTERNAL (decl))
8076    complete_type (type);
8077  if (!DECL_SIZE (decl)
8078      && TREE_TYPE (decl) != error_mark_node
8079      && (COMPLETE_TYPE_P (type)
8080	  || (TREE_CODE (type) == ARRAY_TYPE
8081	      && !TYPE_DOMAIN (type)
8082	      && COMPLETE_TYPE_P (TREE_TYPE (type)))))
8083    layout_decl (decl, 0);
8084
8085  if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
8086    {
8087      /* An automatic variable with an incomplete type: that is an error.
8088	 Don't talk about array types here, since we took care of that
8089	 message in grokdeclarator.  */
8090      error ("storage size of `%D' isn't known", decl);
8091      TREE_TYPE (decl) = error_mark_node;
8092    }
8093#if 0
8094  /* Keep this code around in case we later want to control debug info
8095     based on whether a type is "used".  (jason 1999-11-11) */
8096
8097  else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
8098    /* Let debugger know it should output info for this type.  */
8099    note_debug_info_needed (ttype);
8100
8101  if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
8102    note_debug_info_needed (DECL_CONTEXT (decl));
8103#endif
8104
8105  if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
8106      && DECL_SIZE (decl) != NULL_TREE
8107      && ! TREE_CONSTANT (DECL_SIZE (decl)))
8108    {
8109      if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
8110	constant_expression_warning (DECL_SIZE (decl));
8111      else
8112	error ("storage size of `%D' isn't constant", decl);
8113    }
8114
8115  if (TREE_STATIC (decl)
8116      && !DECL_ARTIFICIAL (decl)
8117      && current_function_decl
8118      && DECL_CONTEXT (decl) == current_function_decl)
8119    push_local_name (decl);
8120}
8121
8122/* If a local static variable is declared in an inline function, or if
8123   we have a weak definition, we must endeavor to create only one
8124   instance of the variable at link-time.  */
8125
8126static void
8127maybe_commonize_var (decl)
8128     tree decl;
8129{
8130  /* Static data in a function with comdat linkage also has comdat
8131     linkage.  */
8132  if (TREE_STATIC (decl)
8133      /* Don't mess with __FUNCTION__.  */
8134      && ! DECL_ARTIFICIAL (decl)
8135      && DECL_FUNCTION_SCOPE_P (decl)
8136      /* Unfortunately, import_export_decl has not always been called
8137	 before the function is processed, so we cannot simply check
8138	 DECL_COMDAT.  */
8139      && (DECL_COMDAT (DECL_CONTEXT (decl))
8140	  || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
8141	       || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
8142	      && TREE_PUBLIC (DECL_CONTEXT (decl)))))
8143    {
8144      if (flag_weak)
8145	{
8146	  /* With weak symbols, we simply make the variable COMDAT;
8147	     that will cause copies in multiple translations units to
8148	     be merged.  */
8149	  comdat_linkage (decl);
8150	}
8151      else
8152	{
8153	  if (DECL_INITIAL (decl) == NULL_TREE
8154	      || DECL_INITIAL (decl) == error_mark_node)
8155	    {
8156	      /* Without weak symbols, we can use COMMON to merge
8157		 uninitialized variables.  */
8158	      TREE_PUBLIC (decl) = 1;
8159	      DECL_COMMON (decl) = 1;
8160	    }
8161	  else
8162	    {
8163	      /* While for initialized variables, we must use internal
8164		 linkage -- which means that multiple copies will not
8165		 be merged.  */
8166	      TREE_PUBLIC (decl) = 0;
8167	      DECL_COMMON (decl) = 0;
8168	      cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
8169	      cp_warning_at ("  you can work around this by removing the initializer", decl);
8170	    }
8171	}
8172    }
8173  else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
8174    /* Set it up again; we might have set DECL_INITIAL since the last
8175       time.  */
8176    comdat_linkage (decl);
8177}
8178
8179/* Issue an error message if DECL is an uninitialized const variable.  */
8180
8181static void
8182check_for_uninitialized_const_var (decl)
8183     tree decl;
8184{
8185  tree type = TREE_TYPE (decl);
8186
8187  /* ``Unless explicitly declared extern, a const object does not have
8188     external linkage and must be initialized. ($8.4; $12.1)'' ARM
8189     7.1.6 */
8190  if (TREE_CODE (decl) == VAR_DECL
8191      && TREE_CODE (type) != REFERENCE_TYPE
8192      && CP_TYPE_CONST_P (type)
8193      && !TYPE_NEEDS_CONSTRUCTING (type)
8194      && !DECL_INITIAL (decl))
8195    error ("uninitialized const `%D'", decl);
8196}
8197
8198/* FIELD is a FIELD_DECL or NULL.  In the former case, the value
8199   returned is the next FIELD_DECL (possibly FIELD itself) that can be
8200   initialized.  If there are no more such fields, the return value
8201   will be NULL.  */
8202
8203static tree
8204next_initializable_field (tree field)
8205{
8206  while (field
8207	 && (TREE_CODE (field) != FIELD_DECL
8208	     || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
8209	     || DECL_ARTIFICIAL (field)))
8210    field = TREE_CHAIN (field);
8211
8212  return field;
8213}
8214
8215/* Subroutine of reshape_init. Reshape the constructor for an array. INITP
8216   is the pointer to the old constructor list (to the CONSTRUCTOR_ELTS of
8217   the CONSTRUCTOR we are processing), while NEW_INIT is the CONSTRUCTOR we
8218   are building.
8219   ELT_TYPE is the element type of the array. MAX_INDEX is an INTEGER_CST
8220   representing the size of the array minus one (the maximum index), or
8221   NULL_TREE if the array was declared without specifying the size.  */
8222
8223static bool
8224reshape_init_array (tree elt_type, tree max_index,
8225		    tree *initp, tree new_init)
8226{
8227  bool sized_array_p = (max_index != NULL_TREE);
8228  HOST_WIDE_INT max_index_cst = 0;
8229  HOST_WIDE_INT index;
8230
8231  if (sized_array_p)
8232    /* HWI is either 32bit or 64bit, so it must be enough to represent the
8233	array size.  */
8234    max_index_cst = tree_low_cst (max_index, 1);
8235
8236  /* Loop until there are no more initializers.  */
8237  for (index = 0;
8238       *initp && (!sized_array_p || index <= max_index_cst);
8239       ++index)
8240    {
8241      tree element_init;
8242      tree designated_index;
8243
8244      element_init = reshape_init (elt_type, initp);
8245      if (element_init == error_mark_node)
8246	return false;
8247      TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
8248      CONSTRUCTOR_ELTS (new_init) = element_init;
8249      designated_index = TREE_PURPOSE (element_init);
8250      if (designated_index)
8251	{
8252	  if (TREE_CODE (designated_index) != INTEGER_CST)
8253	    abort ();
8254	  if (sized_array_p
8255	      && tree_int_cst_lt (max_index, designated_index))
8256	    {
8257	      error ("Designated initializer `%E' larger than array "
8258		      "size", designated_index);
8259	      TREE_PURPOSE (element_init) = NULL_TREE;
8260	    }
8261	  else
8262	    index = tree_low_cst (designated_index, 1);
8263	}
8264    }
8265
8266  return true;
8267}
8268
8269/* Undo the brace-elision allowed by [dcl.init.aggr] in a
8270   brace-enclosed aggregate initializer.
8271
8272   *INITP is one of a list of initializers describing a brace-enclosed
8273   initializer for an entity of the indicated aggregate TYPE.  It may
8274   not presently match the shape of the TYPE; for example:
8275
8276     struct S { int a; int b; };
8277     struct S a[] = { 1, 2, 3, 4 };
8278
8279   Here *INITP will point to TREE_LIST of four elements, rather than a
8280   list of two elements, each itself a list of two elements.  This
8281   routine transforms INIT from the former form into the latter.  The
8282   revised initializer is returned.  */
8283
8284static tree
8285reshape_init (tree type, tree *initp)
8286{
8287  tree inits;
8288  tree old_init;
8289  tree old_init_value;
8290  tree new_init;
8291  bool brace_enclosed_p;
8292
8293  old_init = *initp;
8294  old_init_value = (TREE_CODE (*initp) == TREE_LIST
8295		    ? TREE_VALUE (*initp) : old_init);
8296
8297  /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
8298  if (!old_init_value)
8299    {
8300      my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
8301      TREE_VALUE (old_init) = error_mark_node;
8302      *initp = TREE_CHAIN (old_init);
8303      return old_init;
8304    }
8305
8306  /* If the initializer is brace-enclosed, pull initializers from the
8307     enclosed elements.  Advance past the brace-enclosed initializer
8308     now.  */
8309  if (TREE_CODE (old_init_value) == CONSTRUCTOR
8310      && TREE_TYPE (old_init_value) == NULL_TREE
8311      && TREE_HAS_CONSTRUCTOR (old_init_value))
8312    {
8313      *initp = TREE_CHAIN (old_init);
8314      TREE_CHAIN (old_init) = NULL_TREE;
8315      inits = CONSTRUCTOR_ELTS (old_init_value);
8316      initp = &inits;
8317      brace_enclosed_p = true;
8318    }
8319  else
8320    {
8321      inits = NULL_TREE;
8322      brace_enclosed_p = false;
8323    }
8324
8325  /* A non-aggregate type is always initialized with a single
8326     initializer.  */
8327  if (!CP_AGGREGATE_TYPE_P (type))
8328      {
8329	*initp = TREE_CHAIN (old_init);
8330	TREE_CHAIN (old_init) = NULL_TREE;
8331	/* It is invalid to initialize a non-aggregate type with a
8332	   brace-enclosed initializer.  */
8333	if (brace_enclosed_p)
8334	  {
8335	    error ("brace-enclosed initializer used to initialize `%T'",
8336		   type);
8337	    if (TREE_CODE (old_init) == TREE_LIST)
8338	      TREE_VALUE (old_init) = error_mark_node;
8339	    else
8340	      old_init = error_mark_node;
8341	  }
8342
8343	return old_init;
8344      }
8345
8346  /* [dcl.init.aggr]
8347
8348     All implicit type conversions (clause _conv_) are considered when
8349     initializing the aggregate member with an initializer from an
8350     initializer-list.  If the initializer can initialize a member,
8351     the member is initialized.  Otherwise, if the member is itself a
8352     non-empty subaggregate, brace elision is assumed and the
8353     initializer is considered for the initialization of the first
8354     member of the subaggregate.  */
8355  if (!brace_enclosed_p
8356      && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
8357    {
8358      *initp = TREE_CHAIN (old_init);
8359      TREE_CHAIN (old_init) = NULL_TREE;
8360      return old_init;
8361    }
8362
8363  if (TREE_CODE (old_init_value) == STRING_CST
8364      && TREE_CODE (type) == ARRAY_TYPE
8365      && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
8366    {
8367      /* [dcl.init.string]
8368
8369	 A char array (whether plain char, signed char, or unsigned char)
8370	 can be initialized by a string-literal (optionally enclosed in
8371	 braces); a wchar_t array can be initialized by a wide
8372	 string-literal (optionally enclosed in braces).  */
8373      new_init = old_init;
8374      /* Move past the initializer.  */
8375      *initp = TREE_CHAIN (old_init);
8376      TREE_CHAIN (old_init) = NULL_TREE;
8377    }
8378  else
8379    {
8380      /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */
8381      new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
8382      TREE_HAS_CONSTRUCTOR (new_init) = 1;
8383
8384      if (CLASS_TYPE_P (type))
8385	{
8386	  tree field;
8387
8388	  field = next_initializable_field (TYPE_FIELDS (type));
8389
8390	  if (!field)
8391	    {
8392	      /* [dcl.init.aggr]
8393
8394		 An initializer for an aggregate member that is an
8395		 empty class shall have the form of an empty
8396		 initializer-list {}.  */
8397	      if (!brace_enclosed_p)
8398                {
8399                  error ("initializer for `%T' must be brace-enclosed",
8400                         type);
8401                  return error_mark_node;
8402                }
8403	    }
8404	  else
8405	    {
8406	      /* Loop through the initializable fields, gathering
8407		 initializers.  */
8408	      while (*initp)
8409		{
8410		  tree field_init;
8411
8412		  /* Handle designated initializers, as an extension.  */
8413		  if (TREE_PURPOSE (*initp))
8414		    {
8415		      if (pedantic)
8416			pedwarn ("ISO C++ does not allow designated initializers");
8417		      field = lookup_field_1 (type, TREE_PURPOSE (*initp),
8418					      /*want_type=*/false);
8419		      if (!field || TREE_CODE (field) != FIELD_DECL)
8420			error ("`%T' has no non-static data member named `%D'",
8421			       type, TREE_PURPOSE (*initp));
8422		    }
8423		  if (!field)
8424		    break;
8425
8426		  field_init = reshape_init (TREE_TYPE (field), initp);
8427                  if (field_init == error_mark_node)
8428                    return error_mark_node;
8429		  TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
8430		  CONSTRUCTOR_ELTS (new_init) = field_init;
8431		  /* [dcl.init.aggr]
8432
8433		     When a union  is  initialized with a brace-enclosed
8434		     initializer, the braces shall only contain an
8435		     initializer for the first member of the union.  */
8436		  if (TREE_CODE (type) == UNION_TYPE)
8437		    break;
8438		  field = next_initializable_field (TREE_CHAIN (field));
8439		}
8440	    }
8441	}
8442      else if ((TREE_CODE (type) == ARRAY_TYPE)|| (TREE_CODE (type) == VECTOR_TYPE))
8443	{
8444	  tree max_index;
8445
8446	  /* If the bound of the array is known, take no more initializers
8447	     than are allowed.  */
8448	  max_index = ((TYPE_DOMAIN (type) && (TREE_CODE (type) == ARRAY_TYPE))
8449		       ? array_type_nelts (type) : NULL_TREE);
8450	  if (!reshape_init_array (TREE_TYPE (type), max_index,
8451				   initp, new_init))
8452	    return error_mark_node;
8453	}
8454      else
8455	abort ();
8456
8457      /* The initializers were placed in reverse order in the
8458	 CONSTRUCTOR.  */
8459      CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
8460
8461      if (TREE_CODE (old_init) == TREE_LIST)
8462	new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
8463    }
8464
8465  /* If this was a brace-enclosed initializer and all of the
8466     initializers were not used up, there is a problem.  */
8467  if (brace_enclosed_p && *initp)
8468    error ("too many initializers for `%T'", type);
8469
8470  return new_init;
8471}
8472
8473/* Verify INIT (the initializer for DECL), and record the
8474   initialization in DECL_INITIAL, if appropriate.  CLEANUP is as for
8475   grok_reference_init.
8476
8477   If the return value is non-NULL, it is an expression that must be
8478   evaluated dynamically to initialize DECL.  */
8479
8480static tree
8481check_initializer (tree decl, tree init, int flags, tree *cleanup)
8482{
8483  tree type = TREE_TYPE (decl);
8484  tree init_code = NULL;
8485
8486  /* If `start_decl' didn't like having an initialization, ignore it now.  */
8487  if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
8488    init = NULL_TREE;
8489
8490  /* If an initializer is present, DECL_INITIAL has been
8491     error_mark_node, to indicate that an as-of-yet unevaluated
8492     initialization will occur.  From now on, DECL_INITIAL reflects
8493     the static initialization -- if any -- of DECL.  */
8494  DECL_INITIAL (decl) = NULL_TREE;
8495
8496  /* Things that are going to be initialized need to have complete
8497     type.  */
8498  TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
8499
8500  if (type == error_mark_node)
8501    /* We will have already complained.  */
8502    init = NULL_TREE;
8503  else if (init && COMPLETE_TYPE_P (type)
8504	   && !TREE_CONSTANT (TYPE_SIZE (type)))
8505    {
8506      error ("variable-sized object `%D' may not be initialized", decl);
8507      init = NULL_TREE;
8508    }
8509  else if (TREE_CODE (type) == ARRAY_TYPE
8510	   && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
8511    {
8512      error ("elements of array `%#D' have incomplete type", decl);
8513      init = NULL_TREE;
8514    }
8515  else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
8516    {
8517      error ("`%D' has incomplete type", decl);
8518      TREE_TYPE (decl) = error_mark_node;
8519      init = NULL_TREE;
8520    }
8521
8522  if (TREE_CODE (decl) == CONST_DECL)
8523    {
8524      my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
8525
8526      DECL_INITIAL (decl) = init;
8527
8528      my_friendly_assert (init != NULL_TREE, 149);
8529      init = NULL_TREE;
8530    }
8531  else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
8532    init = grok_reference_init (decl, type, init, cleanup);
8533  else if (init)
8534    {
8535      if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
8536	{
8537	  /* [dcl.init] paragraph 13,
8538	     If T is a scalar type, then a declaration of the form
8539	     T x = { a };
8540	     is equivalent to
8541	     T x = a;
8542
8543	     reshape_init will complain about the extra braces,
8544	     and doesn't do anything useful in the case where TYPE is
8545	     scalar, so just don't call it.  */
8546	  if (CP_AGGREGATE_TYPE_P (type))
8547	    init = reshape_init (type, &init);
8548	}
8549
8550      /* If DECL has an array type without a specific bound, deduce the
8551	 array size from the initializer.  */
8552      maybe_deduce_size_from_array_init (decl, init);
8553      type = TREE_TYPE (decl);
8554      if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
8555	TREE_TYPE (init) = type;
8556
8557      if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
8558	{
8559	  if (TREE_CODE (type) == ARRAY_TYPE)
8560	    goto initialize_aggr;
8561	  else if (TREE_CODE (init) == CONSTRUCTOR
8562		   && TREE_HAS_CONSTRUCTOR (init))
8563	    {
8564	      if (TYPE_NON_AGGREGATE_CLASS (type))
8565		{
8566		  error ("`%D' must be initialized by constructor, not by `{...}'",
8567			 decl);
8568		  init = error_mark_node;
8569		}
8570	      else
8571		goto dont_use_constructor;
8572	    }
8573	  else
8574	    {
8575	      int saved_stmts_are_full_exprs_p;
8576
8577	    initialize_aggr:
8578	      saved_stmts_are_full_exprs_p = 0;
8579	      if (building_stmt_tree ())
8580		{
8581		  saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8582		  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8583		}
8584	      init = build_aggr_init (decl, init, flags);
8585	      if (building_stmt_tree ())
8586		current_stmt_tree ()->stmts_are_full_exprs_p =
8587		  saved_stmts_are_full_exprs_p;
8588	      return init;
8589	    }
8590	}
8591      else
8592	{
8593	dont_use_constructor:
8594	  if (TREE_CODE (init) != TREE_VEC)
8595	    {
8596	      init_code = store_init_value (decl, init);
8597	      init = NULL;
8598	    }
8599	}
8600    }
8601  else if (DECL_EXTERNAL (decl))
8602    ;
8603  else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
8604    goto initialize_aggr;
8605  else if (IS_AGGR_TYPE (type))
8606    {
8607      tree core_type = strip_array_types (type);
8608
8609      if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
8610	error ("structure `%D' with uninitialized const members", decl);
8611      if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
8612	error ("structure `%D' with uninitialized reference members",
8613	       decl);
8614
8615      check_for_uninitialized_const_var (decl);
8616    }
8617  else
8618    check_for_uninitialized_const_var (decl);
8619
8620  if (init && init != error_mark_node)
8621    init_code = build (INIT_EXPR, type, decl, init);
8622
8623  return init_code;
8624}
8625
8626/* If DECL is not a local variable, give it RTL.  */
8627
8628static void
8629make_rtl_for_nonlocal_decl (decl, init, asmspec)
8630     tree decl;
8631     tree init;
8632     const char *asmspec;
8633{
8634  int toplev = toplevel_bindings_p ();
8635  int defer_p;
8636
8637  /* Handle non-variables up front.  */
8638  if (TREE_CODE (decl) != VAR_DECL)
8639    {
8640      rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8641      return;
8642    }
8643
8644  /* If we see a class member here, it should be a static data
8645     member.  */
8646  if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
8647    {
8648      my_friendly_assert (TREE_STATIC (decl), 19990828);
8649      /* An in-class declaration of a static data member should be
8650	 external; it is only a declaration, and not a definition.  */
8651      if (init == NULL_TREE)
8652	my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
8653    }
8654
8655  /* Set the DECL_ASSEMBLER_NAME for the variable.  */
8656  if (asmspec)
8657    {
8658      SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8659      /* The `register' keyword, when used together with an
8660	 asm-specification, indicates that the variable should be
8661	 placed in a particular register.  */
8662      if (DECL_REGISTER (decl))
8663	DECL_C_HARD_REGISTER (decl) = 1;
8664    }
8665
8666  /* We don't create any RTL for local variables.  */
8667  if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
8668    return;
8669
8670  /* We defer emission of local statics until the corresponding
8671     DECL_STMT is expanded.  */
8672  defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
8673
8674  /* We try to defer namespace-scope static constants so that they are
8675     not emitted into the object file unnecessarily.  */
8676  if (!DECL_VIRTUAL_P (decl)
8677      && TREE_READONLY (decl)
8678      && DECL_INITIAL (decl) != NULL_TREE
8679      && DECL_INITIAL (decl) != error_mark_node
8680      && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
8681      && toplev
8682      && !TREE_PUBLIC (decl))
8683    {
8684      /* Fool with the linkage of static consts according to #pragma
8685	 interface.  */
8686      if (!interface_unknown && !TREE_PUBLIC (decl))
8687	{
8688	  TREE_PUBLIC (decl) = 1;
8689	  DECL_EXTERNAL (decl) = interface_only;
8690	}
8691
8692      defer_p = 1;
8693    }
8694  /* Likewise for template instantiations.  */
8695  else if (DECL_COMDAT (decl))
8696    defer_p = 1;
8697
8698  /* If we're deferring the variable, we only need to make RTL if
8699     there's an ASMSPEC.  Otherwise, we'll lazily create it later when
8700     we need it.  (There's no way to lazily create RTL for things that
8701     have assembly specs because the information about the specifier
8702     isn't stored in the tree, yet)  */
8703  if (defer_p && asmspec)
8704    make_decl_rtl (decl, asmspec);
8705  /* If we're not deferring, go ahead and assemble the variable.  */
8706  else if (!defer_p)
8707    rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
8708}
8709
8710/* The old ARM scoping rules injected variables declared in the
8711   initialization statement of a for-statement into the surrounding
8712   scope.  We support this usage, in order to be backward-compatible.
8713   DECL is a just-declared VAR_DECL; if necessary inject its
8714   declaration into the surrounding scope.  */
8715
8716void
8717maybe_inject_for_scope_var (decl)
8718     tree decl;
8719{
8720  timevar_push (TV_NAME_LOOKUP);
8721
8722  if (!DECL_NAME (decl))
8723    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
8724
8725  /* Declarations of __FUNCTION__ and its ilk appear magically when
8726     the variable is first used.  If that happens to be inside a
8727     for-loop, we don't want to do anything special.  */
8728  if (DECL_PRETTY_FUNCTION_P (decl))
8729    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
8730
8731  if (current_binding_level->is_for_scope)
8732    {
8733      struct cp_binding_level *outer
8734	= current_binding_level->level_chain;
8735
8736      /* Check to see if the same name is already bound at the outer
8737	 level, either because it was directly declared, or because a
8738	 dead for-decl got preserved.  In either case, the code would
8739	 not have been valid under the ARM scope rules, so clear
8740	 is_for_scope for the current_binding_level.
8741
8742	 Otherwise, we need to preserve the temp slot for decl to last
8743	 into the outer binding level.  */
8744
8745      cxx_binding *outer_binding
8746	= IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
8747
8748      if (outer_binding && BINDING_LEVEL (outer_binding) == outer
8749	  && (TREE_CODE (BINDING_VALUE (outer_binding))
8750	      == VAR_DECL)
8751	  && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
8752	{
8753	  BINDING_VALUE (outer_binding)
8754	    = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
8755	  current_binding_level->is_for_scope = 0;
8756	}
8757      else if (DECL_IN_MEMORY_P (decl))
8758	preserve_temp_slots (DECL_RTL (decl));
8759    }
8760
8761  timevar_pop (TV_NAME_LOOKUP);
8762}
8763
8764/* Generate code to initialize DECL (a local variable).  */
8765
8766static void
8767initialize_local_var (decl, init)
8768     tree decl;
8769     tree init;
8770{
8771  tree type = TREE_TYPE (decl);
8772  tree cleanup;
8773
8774  my_friendly_assert (TREE_CODE (decl) == VAR_DECL
8775		      || TREE_CODE (decl) == RESULT_DECL,
8776		      20021010);
8777  my_friendly_assert (!TREE_STATIC (decl), 20021010);
8778
8779  if (DECL_SIZE (decl) == NULL_TREE)
8780    {
8781      /* If we used it already as memory, it must stay in memory.  */
8782      DECL_INITIAL (decl) = NULL_TREE;
8783      TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8784    }
8785
8786  if (DECL_SIZE (decl) && type != error_mark_node)
8787    {
8788      int already_used;
8789
8790      /* Compute and store the initial value.  */
8791      already_used = TREE_USED (decl) || TREE_USED (type);
8792
8793      /* Perform the initialization.  */
8794      if (init)
8795	{
8796	  int saved_stmts_are_full_exprs_p;
8797
8798	  my_friendly_assert (building_stmt_tree (), 20000906);
8799	  saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8800	  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8801	  finish_expr_stmt (init);
8802	  current_stmt_tree ()->stmts_are_full_exprs_p =
8803	    saved_stmts_are_full_exprs_p;
8804	}
8805
8806      /* Set this to 0 so we can tell whether an aggregate which was
8807	 initialized was ever used.  Don't do this if it has a
8808	 destructor, so we don't complain about the 'resource
8809	 allocation is initialization' idiom.  Now set
8810	 attribute((unused)) on types so decls of that type will be
8811	 marked used. (see TREE_USED, above.)  */
8812      if (TYPE_NEEDS_CONSTRUCTING (type)
8813	  && ! already_used
8814	  && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8815	  && DECL_NAME (decl))
8816	TREE_USED (decl) = 0;
8817      else if (already_used)
8818	TREE_USED (decl) = 1;
8819    }
8820
8821  /* Generate a cleanup, if necessary.  */
8822  cleanup = cxx_maybe_build_cleanup (decl);
8823  if (DECL_SIZE (decl) && cleanup)
8824    finish_decl_cleanup (decl, cleanup);
8825}
8826
8827/* Finish processing of a declaration;
8828   install its line number and initial value.
8829   If the length of an array type is not known before,
8830   it must be determined now, from the initial value, or it is an error.
8831
8832   INIT holds the value of an initializer that should be allowed to escape
8833   the normal rules.
8834
8835   FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8836   if the (init) syntax was used.  */
8837
8838void
8839cp_finish_decl (decl, init, asmspec_tree, flags)
8840     tree decl, init;
8841     tree asmspec_tree;
8842     int flags;
8843{
8844  tree type;
8845  tree ttype = NULL_TREE;
8846  tree cleanup;
8847  const char *asmspec = NULL;
8848  int was_readonly = 0;
8849
8850  if (! decl)
8851    {
8852      if (init)
8853	error ("assignment (not initialization) in declaration");
8854      return;
8855    }
8856
8857  /* Assume no cleanup is required.  */
8858  cleanup = NULL_TREE;
8859
8860  /* If a name was specified, get the string.  */
8861  if (global_scope_p (current_binding_level))
8862    asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8863  if (asmspec_tree)
8864    asmspec = TREE_STRING_POINTER (asmspec_tree);
8865
8866  if (init && TREE_CODE (init) == NAMESPACE_DECL)
8867    {
8868      error ("cannot initialize `%D' to namespace `%D'",
8869		decl, init);
8870      init = NULL_TREE;
8871    }
8872
8873  if (current_class_type
8874      && CP_DECL_CONTEXT (decl) == current_class_type
8875      && TYPE_BEING_DEFINED (current_class_type)
8876      && (DECL_INITIAL (decl) || init))
8877    DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8878
8879  if (TREE_CODE (decl) == VAR_DECL
8880      && DECL_CONTEXT (decl)
8881      && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8882      && DECL_CONTEXT (decl) != current_namespace
8883      && init)
8884    {
8885      /* Leave the namespace of the object.  */
8886      pop_decl_namespace ();
8887    }
8888
8889  type = TREE_TYPE (decl);
8890
8891  if (type == error_mark_node)
8892    return;
8893
8894  if (TYPE_HAS_MUTABLE_P (type))
8895    TREE_READONLY (decl) = 0;
8896
8897  if (processing_template_decl)
8898    {
8899      /* Add this declaration to the statement-tree.  */
8900      if (at_function_scope_p ()
8901	  && TREE_CODE (decl) != RESULT_DECL)
8902	add_decl_stmt (decl);
8903
8904      if (init && DECL_INITIAL (decl))
8905	DECL_INITIAL (decl) = init;
8906      goto finish_end0;
8907    }
8908
8909  /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8910  my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8911
8912  /* Take care of TYPE_DECLs up front.  */
8913  if (TREE_CODE (decl) == TYPE_DECL)
8914    {
8915      if (type != error_mark_node
8916	  && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8917	{
8918	  if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8919	    warning ("shadowing previous type declaration of `%#D'", decl);
8920	  set_identifier_type_value (DECL_NAME (decl), type);
8921	  CLASSTYPE_GOT_SEMICOLON (type) = 1;
8922	}
8923
8924      /* If we have installed this as the canonical typedef for this
8925	 type, and that type has not been defined yet, delay emitting
8926	 the debug information for it, as we will emit it later.  */
8927      if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8928	  && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8929	TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8930
8931      rest_of_decl_compilation (decl, NULL,
8932				DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8933      goto finish_end;
8934    }
8935
8936  if (TREE_CODE (decl) != FUNCTION_DECL)
8937    ttype = target_type (type);
8938
8939
8940  /* Currently, GNU C++ puts constants in text space, making them
8941     impossible to initialize.  In the future, one would hope for
8942     an operating system which understood the difference between
8943     initialization and the running of a program.  */
8944  if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl))
8945    {
8946      was_readonly = 1;
8947      if (TYPE_NEEDS_CONSTRUCTING (type)
8948	  || TREE_CODE (type) == REFERENCE_TYPE)
8949	TREE_READONLY (decl) = 0;
8950    }
8951
8952  if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8953    {
8954      /* This must override the asm specifier which was placed by
8955	 grokclassfn.  Lay this out fresh.  */
8956      SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8957      SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8958      make_decl_rtl (decl, asmspec);
8959    }
8960  else if (TREE_CODE (decl) == RESULT_DECL)
8961    init = check_initializer (decl, init, flags, &cleanup);
8962  else if (TREE_CODE (decl) == VAR_DECL)
8963    {
8964      /* Only PODs can have thread-local storage.  Other types may require
8965	 various kinds of non-trivial initialization.  */
8966      if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8967	error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8968	       decl, TREE_TYPE (decl));
8969      /* Convert the initializer to the type of DECL, if we have not
8970	 already initialized DECL.  */
8971      if (!DECL_INITIALIZED_P (decl)
8972	  /* If !DECL_EXTERNAL then DECL is being defined.  In the
8973	     case of a static data member initialized inside the
8974	     class-specifier, there can be an initializer even if DECL
8975	     is *not* defined.  */
8976	  && (!DECL_EXTERNAL (decl) || init))
8977	{
8978	  init = check_initializer (decl, init, flags, &cleanup);
8979	  /* Thread-local storage cannot be dynamically initialized.  */
8980	  if (DECL_THREAD_LOCAL (decl) && init)
8981	    {
8982	      error ("`%D' is thread-local and so cannot be dynamically "
8983		     "initialized", decl);
8984	      init = NULL_TREE;
8985	    }
8986	  /* Handle:
8987
8988	     [dcl.init]
8989
8990	     The memory occupied by any object of static storage
8991	     duration is zero-initialized at program startup before
8992	     any other initialization takes place.
8993
8994	     We cannot create an appropriate initializer until after
8995	     the type of DECL is finalized.  If DECL_INITIAL is set,
8996	     then the DECL is statically initialized, and any
8997	     necessary zero-initialization has already been performed.  */
8998	  if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8999	    DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
9000						   /*nelts=*/NULL_TREE,
9001						   /*static_storage_p=*/true);
9002	  /* Remember that the initialization for this variable has
9003	     taken place.  */
9004	  DECL_INITIALIZED_P (decl) = 1;
9005	}
9006      /* If the variable has an array type, lay out the type, even if
9007	 there is no initializer.  It is valid to index through the
9008	 array, and we must get TYPE_ALIGN set correctly on the array
9009	 type.  */
9010      else if (TREE_CODE (type) == ARRAY_TYPE)
9011	layout_type (type);
9012    }
9013
9014  /* Add this declaration to the statement-tree.  This needs to happen
9015     after the call to check_initializer so that the DECL_STMT for a
9016     reference temp is added before the DECL_STMT for the reference itself.  */
9017  if (building_stmt_tree ()
9018      && at_function_scope_p ()
9019      && TREE_CODE (decl) != RESULT_DECL)
9020    add_decl_stmt (decl);
9021
9022  if (TREE_CODE (decl) == VAR_DECL)
9023    layout_var_decl (decl);
9024
9025  /* Output the assembler code and/or RTL code for variables and functions,
9026     unless the type is an undefined structure or union.
9027     If not, it will get done when the type is completed.  */
9028  if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
9029      || TREE_CODE (decl) == RESULT_DECL)
9030    {
9031      if (TREE_CODE (decl) == VAR_DECL)
9032	maybe_commonize_var (decl);
9033
9034      make_rtl_for_nonlocal_decl (decl, init, asmspec);
9035
9036      if (TREE_CODE (type) == FUNCTION_TYPE
9037	  || TREE_CODE (type) == METHOD_TYPE)
9038	abstract_virtuals_error (decl,
9039				 strip_array_types (TREE_TYPE (type)));
9040      else
9041	abstract_virtuals_error (decl, strip_array_types (type));
9042
9043      if (TREE_CODE (decl) == FUNCTION_DECL
9044	  || TREE_TYPE (decl) == error_mark_node)
9045	/* No initialization required.  */
9046	;
9047      else if (DECL_EXTERNAL (decl)
9048	       && ! (DECL_LANG_SPECIFIC (decl)
9049		     && DECL_NOT_REALLY_EXTERN (decl)))
9050	{
9051	  if (init)
9052	    DECL_INITIAL (decl) = init;
9053	}
9054      else
9055	{
9056	  /* A variable definition.  */
9057	  if (DECL_FUNCTION_SCOPE_P (decl))
9058	    {
9059	      /* This is a local declaration.  */
9060	      if (doing_semantic_analysis_p ())
9061		maybe_inject_for_scope_var (decl);
9062	      /* Initialize the local variable.  */
9063	      if (processing_template_decl)
9064		{
9065		  if (init || DECL_INITIAL (decl) == error_mark_node)
9066		    DECL_INITIAL (decl) = init;
9067		}
9068	      else if (!TREE_STATIC (decl))
9069		initialize_local_var (decl, init);
9070	    }
9071
9072	  if (TREE_STATIC (decl))
9073	    expand_static_init (decl, init);
9074	}
9075    finish_end0:
9076
9077      /* Undo call to `pushclass' that was done in `start_decl'
9078	 due to initialization of qualified member variable.
9079	 I.e., Foo::x = 10;  */
9080      {
9081	tree context = CP_DECL_CONTEXT (decl);
9082	if (context
9083	    && TYPE_P (context)
9084	    && (TREE_CODE (decl) == VAR_DECL
9085		/* We also have a pushclass done that we need to undo here
9086		   if we're at top level and declare a method.  */
9087		|| TREE_CODE (decl) == FUNCTION_DECL)
9088	    /* If size hasn't been set, we're still defining it,
9089	       and therefore inside the class body; don't pop
9090	       the binding level..  */
9091	    && COMPLETE_TYPE_P (context)
9092	    && context == current_class_type)
9093	  pop_nested_class ();
9094      }
9095    }
9096
9097  /* If a CLEANUP_STMT was created to destroy a temporary bound to a
9098     reference, insert it in the statement-tree now.  */
9099  if (cleanup)
9100    add_stmt (cleanup);
9101
9102 finish_end:
9103
9104  if (was_readonly)
9105    TREE_READONLY (decl) = 1;
9106}
9107
9108/* This is here for a midend callback from c-common.c */
9109
9110void
9111finish_decl (decl, init, asmspec_tree)
9112     tree decl, init;
9113     tree asmspec_tree;
9114{
9115  cp_finish_decl (decl, init, asmspec_tree, 0);
9116}
9117
9118/* Returns a declaration for a VAR_DECL as if:
9119
9120     extern "C" TYPE NAME;
9121
9122   had been seen.  Used to create compiler-generated global
9123   variables.  */
9124
9125tree
9126declare_global_var (name, type)
9127     tree name;
9128     tree type;
9129{
9130  tree decl;
9131
9132  push_to_top_level ();
9133  decl = build_decl (VAR_DECL, name, type);
9134  TREE_PUBLIC (decl) = 1;
9135  DECL_EXTERNAL (decl) = 1;
9136  DECL_ARTIFICIAL (decl) = 1;
9137  pushdecl (decl);
9138  cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
9139  pop_from_top_level ();
9140
9141  return decl;
9142}
9143
9144/* Returns a pointer to the `atexit' function.  Note that if
9145   FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
9146   `__cxa_atexit' function specified in the IA64 C++ ABI.  */
9147
9148static tree
9149get_atexit_node ()
9150{
9151  tree atexit_fndecl;
9152  tree arg_types;
9153  tree fn_type;
9154  tree fn_ptr_type;
9155  const char *name;
9156
9157  if (atexit_node)
9158    return atexit_node;
9159
9160  if (flag_use_cxa_atexit)
9161    {
9162      /* The declaration for `__cxa_atexit' is:
9163
9164	   int __cxa_atexit (void (*)(void *), void *, void *)
9165
9166	 We build up the argument types and then then function type
9167	 itself.  */
9168
9169      /* First, build the pointer-to-function type for the first
9170	 argument.  */
9171      arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
9172      fn_type = build_function_type (void_type_node, arg_types);
9173      fn_ptr_type = build_pointer_type (fn_type);
9174      /* Then, build the rest of the argument types.  */
9175      arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
9176      arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
9177      arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
9178      /* And the final __cxa_atexit type.  */
9179      fn_type = build_function_type (integer_type_node, arg_types);
9180      fn_ptr_type = build_pointer_type (fn_type);
9181      name = "__cxa_atexit";
9182    }
9183  else
9184    {
9185      /* The declaration for `atexit' is:
9186
9187           int atexit (void (*)());
9188
9189	 We build up the argument types and then then function type
9190	 itself.  */
9191      fn_type = build_function_type (void_type_node, void_list_node);
9192      fn_ptr_type = build_pointer_type (fn_type);
9193      arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
9194      /* Build the final atexit type.  */
9195      fn_type = build_function_type (integer_type_node, arg_types);
9196      name = "atexit";
9197    }
9198
9199  /* Now, build the function declaration.  */
9200  push_lang_context (lang_name_c);
9201  atexit_fndecl = build_library_fn_ptr (name, fn_type);
9202  mark_used (atexit_fndecl);
9203  pop_lang_context ();
9204  atexit_node = default_conversion (atexit_fndecl);
9205
9206  return atexit_node;
9207}
9208
9209/* Returns the __dso_handle VAR_DECL.  */
9210
9211static tree
9212get_dso_handle_node ()
9213{
9214  if (dso_handle_node)
9215    return dso_handle_node;
9216
9217  /* Declare the variable.  */
9218  dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
9219					ptr_type_node);
9220
9221  return dso_handle_node;
9222}
9223
9224/* Begin a new function with internal linkage whose job will be simply
9225   to destroy some particular variable.  */
9226
9227static tree
9228start_cleanup_fn ()
9229{
9230  static int counter = 0;
9231  int old_interface_only = interface_only;
9232  int old_interface_unknown = interface_unknown;
9233  char name[32];
9234  tree parmtypes;
9235  tree fntype;
9236  tree fndecl;
9237
9238  push_to_top_level ();
9239
9240  /* No need to mangle this.  */
9241  push_lang_context (lang_name_c);
9242
9243  interface_only = 0;
9244  interface_unknown = 1;
9245
9246  /* Build the parameter-types.  */
9247  parmtypes = void_list_node;
9248  /* Functions passed to __cxa_atexit take an additional parameter.
9249     We'll just ignore it.  After we implement the new calling
9250     convention for destructors, we can eliminate the use of
9251     additional cleanup functions entirely in the -fnew-abi case.  */
9252  if (flag_use_cxa_atexit)
9253    parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
9254  /* Build the function type itself.  */
9255  fntype = build_function_type (void_type_node, parmtypes);
9256  /* Build the name of the function.  */
9257  sprintf (name, "__tcf_%d", counter++);
9258  /* Build the function declaration.  */
9259  fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
9260  /* It's a function with internal linkage, generated by the
9261     compiler.  */
9262  TREE_PUBLIC (fndecl) = 0;
9263  DECL_ARTIFICIAL (fndecl) = 1;
9264  /* Make the function `inline' so that it is only emitted if it is
9265     actually needed.  It is unlikely that it will be inlined, since
9266     it is only called via a function pointer, but we avoid unnecessary
9267     emissions this way.  */
9268  DECL_INLINE (fndecl) = 1;
9269  /* Build the parameter.  */
9270  if (flag_use_cxa_atexit)
9271    {
9272      tree parmdecl;
9273
9274      parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
9275      DECL_CONTEXT (parmdecl) = fndecl;
9276      TREE_USED (parmdecl) = 1;
9277      DECL_ARGUMENTS (fndecl) = parmdecl;
9278    }
9279
9280  pushdecl (fndecl);
9281  start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
9282
9283  interface_unknown = old_interface_unknown;
9284  interface_only = old_interface_only;
9285
9286  pop_lang_context ();
9287
9288  return current_function_decl;
9289}
9290
9291/* Finish the cleanup function begun by start_cleanup_fn.  */
9292
9293static void
9294end_cleanup_fn ()
9295{
9296  expand_body (finish_function (0));
9297
9298  pop_from_top_level ();
9299}
9300
9301/* Generate code to handle the destruction of DECL, an object with
9302   static storage duration.  */
9303
9304void
9305register_dtor_fn (decl)
9306     tree decl;
9307{
9308  tree cleanup;
9309  tree compound_stmt;
9310  tree args;
9311  tree fcall;
9312
9313  int saved_flag_access_control;
9314
9315  if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
9316    return;
9317
9318  /* Call build_cleanup before we enter the anonymous function so that
9319     any access checks will be done relative to the current scope,
9320     rather than the scope of the anonymous function.  */
9321  build_cleanup (decl);
9322
9323  /* Now start the function.  */
9324  cleanup = start_cleanup_fn ();
9325
9326  /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
9327     to the original function, rather than the anonymous one.  That
9328     will make the back-end think that nested functions are in use,
9329     which causes confusion.  */
9330  saved_flag_access_control = flag_access_control;
9331  flag_access_control = 0;
9332  fcall = build_cleanup (decl);
9333  flag_access_control = saved_flag_access_control;
9334
9335  /* Create the body of the anonymous function.  */
9336  compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
9337  finish_expr_stmt (fcall);
9338  finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
9339  end_cleanup_fn ();
9340
9341  /* Call atexit with the cleanup function.  */
9342  cxx_mark_addressable (cleanup);
9343  cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
9344  if (flag_use_cxa_atexit)
9345    {
9346      args = tree_cons (NULL_TREE,
9347			build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
9348			NULL_TREE);
9349      args = tree_cons (NULL_TREE, null_pointer_node, args);
9350      args = tree_cons (NULL_TREE, cleanup, args);
9351    }
9352  else
9353    args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
9354  finish_expr_stmt (build_function_call (get_atexit_node (), args));
9355}
9356
9357/* DECL is a VAR_DECL with static storage duration.  INIT, if present,
9358   is its initializer.  Generate code to handle the construction
9359   and destruction of DECL.  */
9360
9361static void
9362expand_static_init (decl, init)
9363     tree decl;
9364     tree init;
9365{
9366  tree oldstatic;
9367
9368  my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
9369  my_friendly_assert (TREE_STATIC (decl), 20021010);
9370
9371  /* Some variables require no initialization.  */
9372  if (!init
9373      && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
9374      && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
9375    return;
9376
9377  oldstatic = value_member (decl, static_aggregates);
9378
9379  if (oldstatic)
9380    {
9381      if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
9382	error ("multiple initializations given for `%D'", decl);
9383    }
9384  else if (! toplevel_bindings_p ())
9385    {
9386      /* Emit code to perform this initialization but once.  */
9387      tree if_stmt;
9388      tree then_clause;
9389      tree assignment;
9390      tree guard;
9391      tree guard_init;
9392
9393      /* Emit code to perform this initialization but once.  This code
9394	 looks like:
9395
9396           static int guard = 0;
9397           if (!guard) {
9398             // Do initialization.
9399	     guard = 1;
9400	     // Register variable for destruction at end of program.
9401	   }
9402
9403	 Note that the `temp' variable is only set to 1 *after* the
9404	 initialization is complete.  This ensures that an exception,
9405	 thrown during the construction, will cause the variable to
9406	 reinitialized when we pass through this code again, as per:
9407
9408	   [stmt.dcl]
9409
9410	   If the initialization exits by throwing an exception, the
9411	   initialization is not complete, so it will be tried again
9412	   the next time control enters the declaration.
9413
9414         In theory, this process should be thread-safe, too; multiple
9415	 threads should not be able to initialize the variable more
9416	 than once.  We don't yet attempt to ensure thread-safety.  */
9417
9418      /* Create the guard variable.  */
9419      guard = get_guard (decl);
9420
9421      /* Begin the conditional initialization.  */
9422      if_stmt = begin_if_stmt ();
9423      finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
9424      then_clause = begin_compound_stmt (/*has_no_scope=*/0);
9425
9426      /* Do the initialization itself.  */
9427      assignment = init ? init : NULL_TREE;
9428
9429      /* Once the assignment is complete, set TEMP to 1.  Since the
9430	 construction of the static object is complete at this point,
9431	 we want to make sure TEMP is set to 1 even if a temporary
9432	 constructed during the initialization throws an exception
9433	 when it is destroyed.  So, we combine the initialization and
9434	 the assignment to TEMP into a single expression, ensuring
9435	 that when we call finish_expr_stmt the cleanups will not be
9436	 run until after TEMP is set to 1.  */
9437      guard_init = set_guard (guard);
9438      if (assignment)
9439	{
9440	  assignment = tree_cons (NULL_TREE, assignment,
9441				  build_tree_list (NULL_TREE,
9442						   guard_init));
9443	  assignment = build_compound_expr (assignment);
9444	}
9445      else
9446	assignment = guard_init;
9447      finish_expr_stmt (assignment);
9448
9449      /* Use atexit to register a function for destroying this static
9450	 variable.  */
9451      register_dtor_fn (decl);
9452
9453      finish_compound_stmt (/*has_no_scope=*/0, then_clause);
9454      finish_then_clause (if_stmt);
9455      finish_if_stmt ();
9456    }
9457  else
9458    static_aggregates = tree_cons (init, decl, static_aggregates);
9459}
9460
9461/* Finish the declaration of a catch-parameter.  */
9462
9463tree
9464start_handler_parms (declspecs, declarator)
9465     tree declspecs;
9466     tree declarator;
9467{
9468  tree decl;
9469  if (declspecs)
9470    {
9471      decl = grokdeclarator (declarator, declspecs, CATCHPARM,
9472			     1, NULL);
9473      if (decl == NULL_TREE)
9474	error ("invalid catch parameter");
9475    }
9476  else
9477    decl = NULL_TREE;
9478
9479  return decl;
9480}
9481
9482
9483/* Make TYPE a complete type based on INITIAL_VALUE.
9484   Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
9485   2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
9486
9487int
9488complete_array_type (type, initial_value, do_default)
9489     tree type, initial_value;
9490     int do_default;
9491{
9492  register tree maxindex = NULL_TREE;
9493  int value = 0;
9494
9495  if (initial_value)
9496    {
9497      /* An array of character type can be initialized from a
9498	 brace-enclosed string constant.  */
9499      if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
9500	  && TREE_CODE (initial_value) == CONSTRUCTOR
9501	  && CONSTRUCTOR_ELTS (initial_value)
9502	  && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
9503	      == STRING_CST)
9504	  && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
9505	initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
9506
9507      /* Note MAXINDEX is really the maximum index, one less than the
9508	 size.  */
9509      if (TREE_CODE (initial_value) == STRING_CST)
9510	{
9511	  int eltsize
9512	    = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
9513	  maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
9514				   / eltsize) - 1, 0);
9515	}
9516      else if (TREE_CODE (initial_value) == CONSTRUCTOR)
9517	{
9518	  tree elts = CONSTRUCTOR_ELTS (initial_value);
9519
9520	  maxindex = ssize_int (-1);
9521	  for (; elts; elts = TREE_CHAIN (elts))
9522	    {
9523	      if (TREE_PURPOSE (elts))
9524		maxindex = TREE_PURPOSE (elts);
9525	      else
9526		maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
9527	    }
9528	  maxindex = copy_node (maxindex);
9529	}
9530      else
9531	{
9532	  /* Make an error message unless that happened already.  */
9533	  if (initial_value != error_mark_node)
9534	    value = 1;
9535	  else
9536	    initial_value = NULL_TREE;
9537
9538	  /* Prevent further error messages.  */
9539	  maxindex = build_int_2 (0, 0);
9540	}
9541    }
9542
9543  if (!maxindex)
9544    {
9545      if (do_default)
9546	maxindex = build_int_2 (0, 0);
9547      value = 2;
9548    }
9549
9550  if (maxindex)
9551    {
9552      tree itype;
9553      tree domain;
9554
9555      domain = build_index_type (maxindex);
9556      TYPE_DOMAIN (type) = domain;
9557
9558      if (! TREE_TYPE (maxindex))
9559	TREE_TYPE (maxindex) = domain;
9560      if (initial_value)
9561        itype = TREE_TYPE (initial_value);
9562      else
9563	itype = NULL;
9564      if (itype && !TYPE_DOMAIN (itype))
9565	TYPE_DOMAIN (itype) = domain;
9566      /* The type of the main variant should never be used for arrays
9567	 of different sizes.  It should only ever be completed with the
9568	 size of the array.  */
9569      if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
9570	TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
9571    }
9572
9573  /* Lay out the type now that we can get the real answer.  */
9574
9575  layout_type (type);
9576
9577  return value;
9578}
9579
9580/* Return zero if something is declared to be a member of type
9581   CTYPE when in the context of CUR_TYPE.  STRING is the error
9582   message to print in that case.  Otherwise, quietly return 1.  */
9583
9584static int
9585member_function_or_else (ctype, cur_type, flags)
9586     tree ctype, cur_type;
9587     enum overload_flags flags;
9588{
9589  if (ctype && ctype != cur_type)
9590    {
9591      if (flags == DTOR_FLAG)
9592	error ("destructor for alien class `%T' cannot be a member",
9593	          ctype);
9594      else
9595	error ("constructor for alien class `%T' cannot be a member",
9596	          ctype);
9597      return 0;
9598    }
9599  return 1;
9600}
9601
9602/* Subroutine of `grokdeclarator'.  */
9603
9604/* Generate errors possibly applicable for a given set of specifiers.
9605   This is for ARM $7.1.2.  */
9606
9607static void
9608bad_specifiers (object, type, virtualp, quals, inlinep, friendp, raises)
9609     tree object;
9610     const char *type;
9611     int virtualp, quals, friendp, raises, inlinep;
9612{
9613  if (virtualp)
9614    error ("`%D' declared as a `virtual' %s", object, type);
9615  if (inlinep)
9616    error ("`%D' declared as an `inline' %s", object, type);
9617  if (quals)
9618    error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
9619	      object, type);
9620  if (friendp)
9621    cp_error_at ("`%D' declared as a friend", object);
9622  if (raises
9623      && (TREE_CODE (object) == TYPE_DECL
9624	  || (!TYPE_PTRFN_P (TREE_TYPE (object))
9625	      && !TYPE_REFFN_P (TREE_TYPE (object))
9626	      && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
9627    cp_error_at ("`%D' declared with an exception specification", object);
9628}
9629
9630/* CTYPE is class type, or null if non-class.
9631   TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
9632   or METHOD_TYPE.
9633   DECLARATOR is the function's name.
9634   VIRTUALP is truthvalue of whether the function is virtual or not.
9635   FLAGS are to be passed through to `grokclassfn'.
9636   QUALS are qualifiers indicating whether the function is `const'
9637   or `volatile'.
9638   RAISES is a list of exceptions that this function can raise.
9639   CHECK is 1 if we must find this method in CTYPE, 0 if we should
9640   not look, and -1 if we should not call `grokclassfn' at all.
9641
9642   Returns `NULL_TREE' if something goes wrong, after issuing
9643   applicable error messages.  */
9644
9645static tree
9646grokfndecl (ctype, type, declarator, orig_declarator, virtualp, flags, quals,
9647	    raises, check, friendp, publicp, inlinep, funcdef_flag,
9648	    template_count, in_namespace)
9649     tree ctype, type;
9650     tree declarator;
9651     tree orig_declarator;
9652     int virtualp;
9653     enum overload_flags flags;
9654     tree quals, raises;
9655     int check, friendp, publicp, inlinep, funcdef_flag, template_count;
9656     tree in_namespace;
9657{
9658  tree decl;
9659  int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
9660  int has_default_arg = 0;
9661  tree t;
9662
9663  if (raises)
9664    type = build_exception_variant (type, raises);
9665
9666  decl = build_lang_decl (FUNCTION_DECL, declarator, type);
9667  /* Propagate volatile out from type to decl.  */
9668  if (TYPE_VOLATILE (type))
9669    TREE_THIS_VOLATILE (decl) = 1;
9670
9671  /* If this decl has namespace scope, set that up.  */
9672  if (in_namespace)
9673    set_decl_namespace (decl, in_namespace, friendp);
9674  else if (!ctype)
9675    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
9676
9677  /* `main' and builtins have implicit 'C' linkage.  */
9678  if ((MAIN_NAME_P (declarator)
9679       || (IDENTIFIER_LENGTH (declarator) > 10
9680	   && IDENTIFIER_POINTER (declarator)[0] == '_'
9681	   && IDENTIFIER_POINTER (declarator)[1] == '_'
9682	   && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
9683      && current_lang_name == lang_name_cplusplus
9684      && ctype == NULL_TREE
9685      /* NULL_TREE means global namespace.  */
9686      && DECL_CONTEXT (decl) == NULL_TREE)
9687    SET_DECL_LANGUAGE (decl, lang_c);
9688
9689  /* Should probably propagate const out from type to decl I bet (mrs).  */
9690  if (staticp)
9691    {
9692      DECL_STATIC_FUNCTION_P (decl) = 1;
9693      DECL_CONTEXT (decl) = ctype;
9694    }
9695
9696  if (ctype)
9697    DECL_CONTEXT (decl) = ctype;
9698
9699  if (ctype == NULL_TREE && DECL_MAIN_P (decl))
9700    {
9701      if (processing_template_decl)
9702	error ("cannot declare `::main' to be a template");
9703      if (inlinep)
9704	error ("cannot declare `::main' to be inline");
9705      if (!publicp)
9706	error ("cannot declare `::main' to be static");
9707      if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
9708			integer_type_node))
9709	error ("`main' must return `int'");
9710      inlinep = 0;
9711      publicp = 1;
9712    }
9713
9714  /* Members of anonymous types and local classes have no linkage; make
9715     them internal.  */
9716  /* FIXME what if it gets a name from typedef?  */
9717  if (ctype && (TYPE_ANONYMOUS_P (ctype)
9718		|| decl_function_context (TYPE_MAIN_DECL (ctype))))
9719    publicp = 0;
9720
9721  if (publicp)
9722    {
9723      /* [basic.link]: A name with no linkage (notably, the name of a class
9724	 or enumeration declared in a local scope) shall not be used to
9725	 declare an entity with linkage.
9726
9727	 Only check this for public decls for now.  */
9728      t = no_linkage_check (TREE_TYPE (decl));
9729      if (t)
9730	{
9731	  if (TYPE_ANONYMOUS_P (t))
9732	    {
9733	      if (DECL_EXTERN_C_P (decl))
9734		/* Allow this; it's pretty common in C.  */;
9735	      else
9736		{
9737		  pedwarn ("non-local function `%#D' uses anonymous type",
9738			      decl);
9739		  if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
9740		    cp_pedwarn_at ("\
9741`%#D' does not refer to the unqualified type, so it is not used for linkage",
9742				TYPE_NAME (t));
9743		}
9744	    }
9745	  else
9746	    pedwarn ("non-local function `%#D' uses local type `%T'",
9747			decl, t);
9748	}
9749    }
9750
9751  TREE_PUBLIC (decl) = publicp;
9752  if (! publicp)
9753    {
9754      DECL_INTERFACE_KNOWN (decl) = 1;
9755      DECL_NOT_REALLY_EXTERN (decl) = 1;
9756    }
9757
9758  DID_INLINE_FUNC (decl) = 0;
9759  /* If the declaration was declared inline, mark it as such.  */
9760  if (inlinep)
9761    DECL_DECLARED_INLINE_P (decl) = 1;
9762  /* We inline functions that are explicitly declared inline, or, when
9763     the user explicitly asks us to, all functions.  */
9764  if (DECL_DECLARED_INLINE_P (decl))
9765    DECL_INLINE (decl) = 1;
9766  if (flag_inline_trees == 2 && !DECL_INLINE (decl) && funcdef_flag)
9767    {
9768      DID_INLINE_FUNC (decl) = 1;
9769      DECL_INLINE (decl) = 1;
9770    }
9771
9772  DECL_EXTERNAL (decl) = 1;
9773  if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
9774    {
9775      error ("%smember function `%D' cannot have `%T' method qualifier",
9776		(ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
9777      quals = NULL_TREE;
9778    }
9779
9780  if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
9781    grok_op_properties (decl, friendp);
9782
9783  if (ctype && decl_function_context (decl))
9784    DECL_NO_STATIC_CHAIN (decl) = 1;
9785
9786  for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
9787    if (TREE_PURPOSE (t)
9788	&& TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
9789      {
9790	has_default_arg = 1;
9791	break;
9792      }
9793
9794  if (friendp
9795      && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9796    {
9797      if (funcdef_flag)
9798	error
9799	  ("defining explicit specialization `%D' in friend declaration",
9800	   orig_declarator);
9801      else
9802	{
9803	  tree fns = TREE_OPERAND (orig_declarator, 0);
9804	  tree args = TREE_OPERAND (orig_declarator, 1);
9805
9806	  if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9807	    {
9808	      /* Something like `template <class T> friend void f<T>()'.  */
9809	      error ("invalid use of template-id `%D' in declaration of primary template",
9810			orig_declarator);
9811	      return NULL_TREE;
9812	    }
9813
9814
9815	  /* A friend declaration of the form friend void f<>().  Record
9816	     the information in the TEMPLATE_ID_EXPR.  */
9817	  SET_DECL_IMPLICIT_INSTANTIATION (decl);
9818
9819          if (TREE_CODE (fns) == COMPONENT_REF)
9820            {
9821              /* Due to bison parser ickiness, we will have already looked
9822                 up an operator_name or PFUNCNAME within the current class
9823                 (see template_id in parse.y). If the current class contains
9824                 such a name, we'll get a COMPONENT_REF here. Undo that.  */
9825
9826              my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
9827                                  == current_class_type, 20001120);
9828              fns = TREE_OPERAND (fns, 1);
9829            }
9830	  my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
9831	                      || TREE_CODE (fns) == LOOKUP_EXPR
9832	                      || TREE_CODE (fns) == OVERLOAD, 20001120);
9833	  DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
9834
9835	  if (has_default_arg)
9836	    {
9837	      error ("default arguments are not allowed in declaration of friend template specialization `%D'",
9838			decl);
9839	      return NULL_TREE;
9840	    }
9841
9842	  if (inlinep)
9843	    {
9844	      error ("`inline' is not allowed in declaration of friend template specialization `%D'",
9845			decl);
9846	      return NULL_TREE;
9847	    }
9848	}
9849    }
9850
9851  if (has_default_arg)
9852    add_defarg_fn (decl);
9853
9854  if (funcdef_flag)
9855    /* Make the init_value nonzero so pushdecl knows this is not
9856       tentative.  error_mark_node is replaced later with the BLOCK.  */
9857    DECL_INITIAL (decl) = error_mark_node;
9858
9859  if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9860    TREE_NOTHROW (decl) = 1;
9861
9862  /* Caller will do the rest of this.  */
9863  if (check < 0)
9864    return decl;
9865
9866  if (flags == NO_SPECIAL && ctype && constructor_name (ctype) == declarator)
9867    DECL_CONSTRUCTOR_P (decl) = 1;
9868
9869  /* Function gets the ugly name, field gets the nice one.  This call
9870     may change the type of the function (because of default
9871     parameters)!  */
9872  if (ctype != NULL_TREE)
9873    grokclassfn (ctype, decl, flags, quals);
9874
9875  decl = check_explicit_specialization (orig_declarator, decl,
9876					template_count,
9877					2 * (funcdef_flag != 0) +
9878					4 * (friendp != 0));
9879  if (decl == error_mark_node)
9880    return NULL_TREE;
9881
9882  if (ctype != NULL_TREE
9883      && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9884      && check)
9885    {
9886      tree old_decl;
9887
9888      old_decl = check_classfn (ctype, decl);
9889
9890      if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9891	/* Because grokfndecl is always supposed to return a
9892	   FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9893	   here.  We depend on our callers to figure out that its
9894	   really a template that's being returned.  */
9895	old_decl = DECL_TEMPLATE_RESULT (old_decl);
9896
9897      if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9898	  && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9899	{
9900	  /* Remove the `this' parm added by grokclassfn.
9901	     XXX Isn't this done in start_function, too?  */
9902	  revert_static_member_fn (decl);
9903	  last_function_parms = TREE_CHAIN (last_function_parms);
9904	}
9905      if (old_decl && DECL_ARTIFICIAL (old_decl))
9906	error ("definition of implicitly-declared `%D'", old_decl);
9907
9908      if (old_decl)
9909	{
9910	  /* Since we've smashed OLD_DECL to its
9911	     DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9912	  if (TREE_CODE (decl) == TEMPLATE_DECL)
9913	    decl = DECL_TEMPLATE_RESULT (decl);
9914
9915	  /* Attempt to merge the declarations.  This can fail, in
9916	     the case of some invalid specialization declarations.  */
9917	  if (!duplicate_decls (decl, old_decl))
9918	    error ("no `%#D' member function declared in class `%T'",
9919		      decl, ctype);
9920	  return old_decl;
9921	}
9922    }
9923
9924  if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9925    return NULL_TREE;
9926
9927  if (ctype == NULL_TREE || check)
9928    return decl;
9929
9930  if (virtualp)
9931    DECL_VIRTUAL_P (decl) = 1;
9932
9933  return decl;
9934}
9935
9936/* Create a VAR_DECL named NAME with the indicated TYPE.
9937
9938   If SCOPE is non-NULL, it is the class type or namespace containing
9939   the variable.  If SCOPE is NULL, the variable should is created in
9940   the innermost enclosings scope.  */
9941
9942static tree
9943grokvardecl (type, name, specbits_in, initialized, constp, scope)
9944     tree type;
9945     tree name;
9946     RID_BIT_TYPE *specbits_in;
9947     int initialized;
9948     int constp;
9949     tree scope;
9950{
9951  tree decl;
9952  RID_BIT_TYPE specbits;
9953
9954  my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE,
9955		      20020808);
9956
9957  specbits = *specbits_in;
9958
9959  /* Compute the scope in which to place the variable.  */
9960  if (!scope)
9961    {
9962      /* An explicit "extern" specifier indicates a namespace-scope
9963	 variable.  */
9964      if (RIDBIT_SETP (RID_EXTERN, specbits))
9965	scope = current_namespace;
9966      else if (!at_function_scope_p ())
9967	{
9968	  scope = current_scope ();
9969	  if (!scope)
9970	    scope = current_namespace;
9971	}
9972    }
9973
9974  if (scope
9975      && (/* If the variable is a namespace-scope variable declared in a
9976	     template, we need DECL_LANG_SPECIFIC.  */
9977	  (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9978	  /* Similarly for namespace-scope variables with language linkage
9979	     other than C++.  */
9980	  || (TREE_CODE (scope) == NAMESPACE_DECL
9981	      && current_lang_name != lang_name_cplusplus)
9982	  /* Similarly for static data members.  */
9983	  || TYPE_P (scope)))
9984    decl = build_lang_decl (VAR_DECL, name, type);
9985  else
9986    decl = build_decl (VAR_DECL, name, type);
9987
9988  if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9989    set_decl_namespace (decl, scope, 0);
9990  else
9991    DECL_CONTEXT (decl) = scope;
9992
9993  if (name && scope && current_lang_name != lang_name_c)
9994    /* We can't mangle lazily here because we don't have any
9995       way to recover whether or not a variable was `extern
9996       "C"' later.  */
9997    mangle_decl (decl);
9998
9999  if (RIDBIT_SETP (RID_EXTERN, specbits))
10000    {
10001      DECL_THIS_EXTERN (decl) = 1;
10002      DECL_EXTERNAL (decl) = !initialized;
10003    }
10004
10005  /* In class context, static means one per class,
10006     public access, and static storage.  */
10007  if (DECL_CLASS_SCOPE_P (decl))
10008    {
10009      TREE_PUBLIC (decl) = 1;
10010      TREE_STATIC (decl) = 1;
10011      DECL_EXTERNAL (decl) = 0;
10012    }
10013  /* At top level, either `static' or no s.c. makes a definition
10014     (perhaps tentative), and absence of `static' makes it public.  */
10015  else if (toplevel_bindings_p ())
10016    {
10017      TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
10018			    && (DECL_THIS_EXTERN (decl) || ! constp));
10019      TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
10020    }
10021  /* Not at top level, only `static' makes a static definition.  */
10022  else
10023    {
10024      TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
10025      TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
10026    }
10027
10028  if (RIDBIT_SETP (RID_THREAD, specbits))
10029    {
10030      if (targetm.have_tls)
10031	DECL_THREAD_LOCAL (decl) = 1;
10032      else
10033	/* A mere warning is sure to result in improper semantics
10034	   at runtime.  Don't bother to allow this to compile.  */
10035	error ("thread-local storage not supported for this target");
10036    }
10037
10038  if (TREE_PUBLIC (decl))
10039    {
10040      /* [basic.link]: A name with no linkage (notably, the name of a class
10041	 or enumeration declared in a local scope) shall not be used to
10042	 declare an entity with linkage.
10043
10044	 Only check this for public decls for now.  */
10045      tree t = no_linkage_check (TREE_TYPE (decl));
10046      if (t)
10047	{
10048	  if (TYPE_ANONYMOUS_P (t))
10049	    /* Ignore for now; `enum { foo } e' is pretty common.  */;
10050	  else
10051	    pedwarn ("non-local variable `%#D' uses local type `%T'",
10052			decl, t);
10053	}
10054    }
10055
10056  return decl;
10057}
10058
10059/* Create and return a canonical pointer to member function type, for
10060   TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
10061
10062tree
10063build_ptrmemfunc_type (tree type)
10064{
10065  tree fields[4];
10066  tree t;
10067  tree unqualified_variant = NULL_TREE;
10068
10069  if (type == error_mark_node)
10070    return type;
10071
10072  /* If a canonical type already exists for this type, use it.  We use
10073     this method instead of type_hash_canon, because it only does a
10074     simple equality check on the list of field members.  */
10075
10076  if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
10077    return t;
10078
10079  /* Make sure that we always have the unqualified pointer-to-member
10080     type first.  */
10081  if (cp_type_quals (type) != TYPE_UNQUALIFIED)
10082    unqualified_variant
10083      = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
10084
10085  t = make_aggr_type (RECORD_TYPE);
10086  /* Let the front-end know this is a pointer to member function...  */
10087  TYPE_PTRMEMFUNC_FLAG (t) = 1;
10088  /* ... and not really an aggregate.  */
10089  SET_IS_AGGR_TYPE (t, 0);
10090
10091  fields[0] = build_decl (FIELD_DECL, pfn_identifier, type);
10092  fields[1] = build_decl (FIELD_DECL, delta_identifier,
10093			  delta_type_node);
10094  finish_builtin_type (t, "__ptrmemfunc_type", fields, 1, ptr_type_node);
10095
10096  /* Zap out the name so that the back-end will give us the debugging
10097     information for this anonymous RECORD_TYPE.  */
10098  TYPE_NAME (t) = NULL_TREE;
10099
10100  /* If this is not the unqualified form of this pointer-to-member
10101     type, set the TYPE_MAIN_VARIANT for this type to be the
10102     unqualified type.  Since they are actually RECORD_TYPEs that are
10103     not variants of each other, we must do this manually.  */
10104  if (cp_type_quals (type) != TYPE_UNQUALIFIED)
10105    {
10106      t = build_qualified_type (t, cp_type_quals (type));
10107      TYPE_MAIN_VARIANT (t) = unqualified_variant;
10108      TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
10109      TYPE_NEXT_VARIANT (unqualified_variant) = t;
10110    }
10111
10112  /* Cache this pointer-to-member type so that we can find it again
10113     later.  */
10114  TYPE_SET_PTRMEMFUNC_TYPE (type, t);
10115
10116  /* Seems to be wanted.  */
10117  CLASSTYPE_GOT_SEMICOLON (t) = 1;
10118
10119  return t;
10120}
10121
10122/* Create and return a pointer to data member type.  */
10123
10124tree
10125build_ptrmem_type (tree class_type, tree member_type)
10126{
10127  return build_pointer_type (build_offset_type (class_type, member_type));
10128}
10129
10130/* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
10131   Check to see that the definition is valid.  Issue appropriate error
10132   messages.  Return 1 if the definition is particularly bad, or 0
10133   otherwise.  */
10134
10135int
10136check_static_variable_definition (decl, type)
10137     tree decl;
10138     tree type;
10139{
10140  /* Motion 10 at San Diego: If a static const integral data member is
10141     initialized with an integral constant expression, the initializer
10142     may appear either in the declaration (within the class), or in
10143     the definition, but not both.  If it appears in the class, the
10144     member is a member constant.  The file-scope definition is always
10145     required.  */
10146  if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
10147    {
10148      error ("invalid in-class initialization of static data member of non-integral type `%T'",
10149	     type);
10150      /* If we just return the declaration, crashes will sometimes
10151	 occur.  We therefore return void_type_node, as if this was a
10152	 friend declaration, to cause callers to completely ignore
10153	 this declaration.  */
10154      return 1;
10155    }
10156  else if (!CP_TYPE_CONST_P (type))
10157    error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
10158	      decl);
10159  else if (pedantic && !INTEGRAL_TYPE_P (type))
10160    pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
10161
10162  return 0;
10163}
10164
10165/* Given the SIZE (i.e., number of elements) in an array, compute an
10166   appropriate index type for the array.  If non-NULL, NAME is the
10167   name of the thing being declared.  */
10168
10169tree
10170compute_array_index_type (name, size)
10171     tree name;
10172     tree size;
10173{
10174  tree itype;
10175
10176  /* If this involves a template parameter, it will be a constant at
10177     instantiation time, but we don't know what the value is yet.
10178     Even if no template parameters are involved, we may an expression
10179     that is not a constant; we don't even simplify `1 + 2' when
10180     processing a template.  */
10181  if (processing_template_decl)
10182    {
10183      /* Resolve a qualified reference to an enumerator or static
10184	 const data member of ours.  */
10185      if (TREE_CODE (size) == SCOPE_REF
10186	  && TREE_OPERAND (size, 0) == current_class_type)
10187	{
10188	  tree t = lookup_field (current_class_type,
10189				 TREE_OPERAND (size, 1), 0, 0);
10190	  if (t)
10191	    size = t;
10192	}
10193
10194      return build_index_type (build_min (MINUS_EXPR, sizetype,
10195					  size, integer_one_node));
10196    }
10197
10198  /* The size might be the result of a cast.  */
10199  STRIP_TYPE_NOPS (size);
10200
10201  /* It might be a const variable or enumeration constant.  */
10202  size = decl_constant_value (size);
10203
10204  /* The array bound must be an integer type.  */
10205  if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
10206      && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
10207      && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
10208    {
10209      if (name)
10210	error ("size of array `%D' has non-integer type", name);
10211      else
10212	error ("size of array has non-integer type");
10213      size = integer_one_node;
10214    }
10215
10216  /* Normally, the array-bound will be a constant.  */
10217  if (TREE_CODE (size) == INTEGER_CST)
10218    {
10219      /* Check to see if the array bound overflowed.  Make that an
10220	 error, no matter how generous we're being.  */
10221      int old_flag_pedantic_errors = flag_pedantic_errors;
10222      int old_pedantic = pedantic;
10223      pedantic = flag_pedantic_errors = 1;
10224      constant_expression_warning (size);
10225      pedantic = old_pedantic;
10226      flag_pedantic_errors = old_flag_pedantic_errors;
10227
10228      /* An array must have a positive number of elements.  */
10229      if (INT_CST_LT (size, integer_zero_node))
10230	{
10231	  if (name)
10232	    error ("size of array `%D' is negative", name);
10233	  else
10234	    error ("size of array is negative");
10235	  size = integer_one_node;
10236	}
10237      /* As an extension we allow zero-sized arrays.  We always allow
10238         them in system headers because glibc uses them.  */
10239      else if (integer_zerop (size) && pedantic && !in_system_header)
10240	{
10241	  if (name)
10242	    pedwarn ("ISO C++ forbids zero-size array `%D'", name);
10243	  else
10244	    pedwarn ("ISO C++ forbids zero-size array");
10245	}
10246    }
10247  else if (TREE_CONSTANT (size))
10248    {
10249      /* `(int) &fn' is not a valid array bound.  */
10250      if (name)
10251	error ("size of array `%D' is not an integral constant-expression",
10252		  name);
10253      else
10254	error ("size of array is not an integral constant-expression");
10255    }
10256
10257  /* Compute the index of the largest element in the array.  It is
10258     one less than the number of elements in the array.  */
10259  itype
10260    = fold (cp_build_binary_op (MINUS_EXPR,
10261				cp_convert (ssizetype, size),
10262				cp_convert (ssizetype,
10263					    integer_one_node)));
10264
10265  /* Check for variable-sized arrays.  We allow such things as an
10266     extension, even though they are not allowed in ANSI/ISO C++.  */
10267  if (!TREE_CONSTANT (itype))
10268    {
10269      if (pedantic)
10270	{
10271	  if (name)
10272	    pedwarn ("ISO C++ forbids variable-size array `%D'",
10273			name);
10274	  else
10275	    pedwarn ("ISO C++ forbids variable-size array");
10276	}
10277
10278      /* Create a variable-sized array index type.  */
10279      itype = variable_size (itype);
10280    }
10281  /* Make sure that there was no overflow when creating to a signed
10282     index type.  (For example, on a 32-bit machine, an array with
10283     size 2^32 - 1 is too big.)  */
10284  else if (TREE_OVERFLOW (itype))
10285    {
10286      error ("overflow in array dimension");
10287      TREE_OVERFLOW (itype) = 0;
10288    }
10289
10290  /* Create and return the appropriate index type.  */
10291  return build_index_type (itype);
10292}
10293
10294/* Returns an ARRAY_TYPE for an array with SIZE elements of the
10295   indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
10296   with this type.  */
10297
10298static tree
10299create_array_type_for_decl (name, type, size)
10300     tree name;
10301     tree type;
10302     tree size;
10303{
10304  tree itype = NULL_TREE;
10305  const char* error_msg;
10306
10307  /* If things have already gone awry, bail now.  */
10308  if (type == error_mark_node || size == error_mark_node)
10309    return error_mark_node;
10310
10311  /* Assume that everything will go OK.  */
10312  error_msg = NULL;
10313
10314  /* There are some types which cannot be array elements.  */
10315  switch (TREE_CODE (type))
10316    {
10317    case VOID_TYPE:
10318      error_msg = "array of void";
10319      break;
10320
10321    case FUNCTION_TYPE:
10322      error_msg = "array of functions";
10323      break;
10324
10325    case REFERENCE_TYPE:
10326      error_msg = "array of references";
10327      break;
10328
10329    case OFFSET_TYPE:
10330      error_msg = "array of data members";
10331      break;
10332
10333    case METHOD_TYPE:
10334      error_msg = "array of function members";
10335      break;
10336
10337    default:
10338      break;
10339    }
10340
10341  /* If something went wrong, issue an error-message and return.  */
10342  if (error_msg)
10343    {
10344      if (name)
10345	error ("declaration of `%D' as %s", name, error_msg);
10346      else
10347	error ("creating %s", error_msg);
10348
10349      return error_mark_node;
10350    }
10351
10352  /* [dcl.array]
10353
10354     The constant expressions that specify the bounds of the arrays
10355     can be omitted only for the first member of the sequence.  */
10356  if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
10357    {
10358      if (name)
10359	error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
10360		  name);
10361      else
10362	error ("multidimensional array must have bounds for all dimensions except the first");
10363
10364      return error_mark_node;
10365    }
10366
10367  /* Figure out the index type for the array.  */
10368  if (size)
10369    itype = compute_array_index_type (name, size);
10370
10371  return build_cplus_array_type (type, itype);
10372}
10373
10374/* Check that it's OK to declare a function with the indicated TYPE.
10375   SFK indicates the kind of special function (if any) that this
10376   function is.  OPTYPE is the type given in a conversion operator
10377   declaration.  Returns the actual return type of the function; that
10378   may be different than TYPE if an error occurs, or for certain
10379   special functions.  */
10380
10381static tree
10382check_special_function_return_type (sfk, type, optype)
10383     special_function_kind sfk;
10384     tree type;
10385     tree optype;
10386{
10387  switch (sfk)
10388    {
10389    case sfk_constructor:
10390      if (type)
10391	error ("return type specification for constructor invalid");
10392
10393      type = void_type_node;
10394      break;
10395
10396    case sfk_destructor:
10397      if (type)
10398	error ("return type specification for destructor invalid");
10399      type = void_type_node;
10400      break;
10401
10402    case sfk_conversion:
10403      if (type && !same_type_p (type, optype))
10404	error ("operator `%T' declared to return `%T'", optype, type);
10405      else if (type)
10406	pedwarn ("return type specified for `operator %T'",  optype);
10407      type = optype;
10408      break;
10409
10410    default:
10411      abort ();
10412      break;
10413    }
10414
10415  return type;
10416}
10417
10418/* Given declspecs and a declarator,
10419   determine the name and type of the object declared
10420   and construct a ..._DECL node for it.
10421   (In one case we can return a ..._TYPE node instead.
10422    For invalid input we sometimes return 0.)
10423
10424   DECLSPECS is a chain of tree_list nodes whose value fields
10425    are the storage classes and type specifiers.
10426
10427   DECL_CONTEXT says which syntactic context this declaration is in:
10428     NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
10429     FUNCDEF for a function definition.  Like NORMAL but a few different
10430      error messages in each case.  Return value may be zero meaning
10431      this definition is too screwy to try to parse.
10432     MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
10433      handle member functions (which have FIELD context).
10434      Return value may be zero meaning this definition is too screwy to
10435      try to parse.
10436     PARM for a parameter declaration (either within a function prototype
10437      or before a function body).  Make a PARM_DECL, or return void_type_node.
10438     CATCHPARM for a parameter declaration before a catch clause.
10439     TYPENAME if for a typename (in a cast or sizeof).
10440      Don't make a DECL node; just return the ..._TYPE node.
10441     FIELD for a struct or union field; make a FIELD_DECL.
10442     BITFIELD for a field with specified width.
10443   INITIALIZED is 1 if the decl has an initializer.
10444
10445   ATTRLIST is a pointer to the list of attributes, which may be NULL
10446   if there are none; *ATTRLIST may be modified if attributes from inside
10447   the declarator should be applied to the declaration.
10448
10449   In the TYPENAME case, DECLARATOR is really an abstract declarator.
10450   It may also be so in the PARM case, for a prototype where the
10451   argument type is specified but not the name.
10452
10453   This function is where the complicated C meanings of `static'
10454   and `extern' are interpreted.
10455
10456   For C++, if there is any monkey business to do, the function which
10457   calls this one must do it, i.e., prepending instance variables,
10458   renaming overloaded function names, etc.
10459
10460   Note that for this C++, it is an error to define a method within a class
10461   which does not belong to that class.
10462
10463   Except in the case where SCOPE_REFs are implicitly known (such as
10464   methods within a class being redundantly qualified),
10465   declarations which involve SCOPE_REFs are returned as SCOPE_REFs
10466   (class_name::decl_name).  The caller must also deal with this.
10467
10468   If a constructor or destructor is seen, and the context is FIELD,
10469   then the type gains the attribute TREE_HAS_x.  If such a declaration
10470   is erroneous, NULL_TREE is returned.
10471
10472   QUALS is used only for FUNCDEF and MEMFUNCDEF cases.  For a member
10473   function, these are the qualifiers to give to the `this' pointer. We
10474   apply TYPE_QUAL_RESTRICT to the this ptr, not the object.
10475
10476   May return void_type_node if the declarator turned out to be a friend.
10477   See grokfield for details.  */
10478
10479tree
10480grokdeclarator (declarator, declspecs, decl_context, initialized, attrlist)
10481     tree declspecs;
10482     tree declarator;
10483     enum decl_context decl_context;
10484     int initialized;
10485     tree *attrlist;
10486{
10487  RID_BIT_TYPE specbits;
10488  int nclasses = 0;
10489  tree spec;
10490  tree type = NULL_TREE;
10491  int longlong = 0;
10492  int type_quals;
10493  int virtualp, explicitp, friendp, inlinep, staticp;
10494  int explicit_int = 0;
10495  int explicit_char = 0;
10496  int defaulted_int = 0;
10497  int extern_langp = 0;
10498  tree dependant_name = NULL_TREE;
10499
10500  tree typedef_decl = NULL_TREE;
10501  const char *name;
10502  tree typedef_type = NULL_TREE;
10503  int funcdef_flag = 0;
10504  enum tree_code innermost_code = ERROR_MARK;
10505  int bitfield = 0;
10506#if 0
10507  /* See the code below that used this.  */
10508  tree decl_attr = NULL_TREE;
10509#endif
10510  /* Set this to error_mark_node for FIELD_DECLs we could not handle properly.
10511     All FIELD_DECLs we build here have `init' put into their DECL_INITIAL.  */
10512  tree init = NULL_TREE;
10513
10514  /* Keep track of what sort of function is being processed
10515     so that we can warn about default return values, or explicit
10516     return values which do not match prescribed defaults.  */
10517  special_function_kind sfk = sfk_none;
10518
10519  tree dname = NULL_TREE;
10520  tree ctype = current_class_type;
10521  tree ctor_return_type = NULL_TREE;
10522  enum overload_flags flags = NO_SPECIAL;
10523  tree quals = NULL_TREE;
10524  tree raises = NULL_TREE;
10525  int template_count = 0;
10526  tree in_namespace = NULL_TREE;
10527  tree returned_attrs = NULL_TREE;
10528
10529  RIDBIT_RESET_ALL (specbits);
10530  if (decl_context == FUNCDEF)
10531    funcdef_flag = 1, decl_context = NORMAL;
10532  else if (decl_context == MEMFUNCDEF)
10533    funcdef_flag = -1, decl_context = FIELD;
10534  else if (decl_context == BITFIELD)
10535    bitfield = 1, decl_context = FIELD;
10536
10537  /* Look inside a declarator for the name being declared
10538     and get it as a string, for an error message.  */
10539  {
10540    tree *next = &declarator;
10541    register tree decl;
10542    name = NULL;
10543
10544    while (next && *next)
10545      {
10546	decl = *next;
10547	switch (TREE_CODE (decl))
10548	  {
10549	  case TREE_LIST:
10550	    /* For attributes.  */
10551	    next = &TREE_VALUE (decl);
10552	    break;
10553
10554	  case COND_EXPR:
10555	    ctype = NULL_TREE;
10556	    next = &TREE_OPERAND (decl, 0);
10557	    break;
10558
10559	  case BIT_NOT_EXPR:	/* For C++ destructors!  */
10560	    {
10561	      tree name = TREE_OPERAND (decl, 0);
10562	      tree rename = NULL_TREE;
10563
10564	      my_friendly_assert (flags == NO_SPECIAL, 152);
10565	      flags = DTOR_FLAG;
10566	      sfk = sfk_destructor;
10567	      if (TREE_CODE (name) == TYPE_DECL)
10568		TREE_OPERAND (decl, 0) = name = constructor_name (name);
10569	      my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
10570	      if (ctype == NULL_TREE)
10571		{
10572		  if (current_class_type == NULL_TREE)
10573		    {
10574		      error ("destructors must be member functions");
10575		      flags = NO_SPECIAL;
10576		    }
10577		  else
10578		    {
10579		      tree t = constructor_name (current_class_name);
10580		      if (t != name)
10581			rename = t;
10582		    }
10583		}
10584	      else
10585		{
10586		  tree t = constructor_name (ctype);
10587		  if (t != name)
10588		    rename = t;
10589		}
10590
10591	      if (rename)
10592		{
10593		  error ("destructor `%T' must match class name `%T'",
10594			    name, rename);
10595		  TREE_OPERAND (decl, 0) = rename;
10596		}
10597	      next = &name;
10598	    }
10599	    break;
10600
10601	  case ADDR_EXPR:	/* C++ reference declaration */
10602	    /* Fall through.  */
10603	  case ARRAY_REF:
10604	  case INDIRECT_REF:
10605	    ctype = NULL_TREE;
10606	    innermost_code = TREE_CODE (decl);
10607	    next = &TREE_OPERAND (decl, 0);
10608	    break;
10609
10610	  case CALL_EXPR:
10611	    if (parmlist_is_exprlist (CALL_DECLARATOR_PARMS (decl)))
10612	      {
10613		/* This is actually a variable declaration using
10614		   constructor syntax.  We need to call start_decl and
10615		   cp_finish_decl so we can get the variable
10616		   initialized...  */
10617
10618		tree attributes;
10619
10620		if (decl_context != NORMAL)
10621		  {
10622		    error ("variable declaration is not allowed here");
10623		    return error_mark_node;
10624		  }
10625
10626		*next = TREE_OPERAND (decl, 0);
10627		init = CALL_DECLARATOR_PARMS (decl);
10628
10629		if (attrlist)
10630		  {
10631		    attributes = *attrlist;
10632		  }
10633		else
10634		  {
10635		    attributes = NULL_TREE;
10636		  }
10637
10638		decl = start_decl (declarator, declspecs, 1,
10639				   attributes, NULL_TREE);
10640		decl_type_access_control (decl);
10641		if (decl)
10642		  {
10643		    /* Look for __unused__ attribute */
10644		    if (TREE_USED (TREE_TYPE (decl)))
10645		      TREE_USED (decl) = 1;
10646		    finish_decl (decl, init, NULL_TREE);
10647		  }
10648		else
10649		  error ("invalid declarator");
10650		return NULL_TREE;
10651	      }
10652	    innermost_code = TREE_CODE (decl);
10653	    if (decl_context == FIELD && ctype == NULL_TREE)
10654	      ctype = current_class_type;
10655	    if (ctype
10656		&& TREE_OPERAND (decl, 0)
10657		&& (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
10658		    && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
10659					   ctype)))
10660	      TREE_OPERAND (decl, 0) = constructor_name (ctype);
10661	    next = &TREE_OPERAND (decl, 0);
10662	    decl = *next;
10663	    if (ctype != NULL_TREE
10664		&& decl != NULL_TREE && flags != DTOR_FLAG
10665		&& decl == constructor_name (ctype))
10666	      {
10667		sfk = sfk_constructor;
10668		ctor_return_type = ctype;
10669	      }
10670	    ctype = NULL_TREE;
10671	    break;
10672
10673	  case TEMPLATE_ID_EXPR:
10674	      {
10675		tree fns = TREE_OPERAND (decl, 0);
10676
10677		if (TREE_CODE (fns) == LOOKUP_EXPR)
10678		  fns = TREE_OPERAND (fns, 0);
10679
10680		dname = fns;
10681		if (TREE_CODE (dname) == COMPONENT_REF)
10682		  dname = TREE_OPERAND (dname, 1);
10683		if (TREE_CODE (dname) != IDENTIFIER_NODE)
10684		  {
10685		    my_friendly_assert (is_overloaded_fn (dname),
10686					19990331);
10687		    dname = DECL_NAME (get_first_fn (dname));
10688		  }
10689	      }
10690	  /* Fall through.  */
10691
10692	  case IDENTIFIER_NODE:
10693	    if (TREE_CODE (decl) == IDENTIFIER_NODE)
10694	      dname = decl;
10695
10696	    next = 0;
10697
10698	    if (C_IS_RESERVED_WORD (dname))
10699	      {
10700		error ("declarator-id missing; using reserved word `%D'",
10701			  dname);
10702		name = IDENTIFIER_POINTER (dname);
10703	      }
10704	    else if (!IDENTIFIER_TYPENAME_P (dname))
10705	      name = IDENTIFIER_POINTER (dname);
10706	    else
10707	      {
10708		my_friendly_assert (flags == NO_SPECIAL, 154);
10709		flags = TYPENAME_FLAG;
10710		ctor_return_type = TREE_TYPE (dname);
10711		sfk = sfk_conversion;
10712		if (IDENTIFIER_GLOBAL_VALUE (dname)
10713		    && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
10714			== TYPE_DECL))
10715		  name = IDENTIFIER_POINTER (dname);
10716		else
10717		  name = "<invalid operator>";
10718	      }
10719	    break;
10720
10721	    /* C++ extension */
10722	  case SCOPE_REF:
10723	    {
10724	      /* Perform error checking, and decide on a ctype.  */
10725	      tree cname = TREE_OPERAND (decl, 0);
10726	      if (cname == NULL_TREE)
10727		ctype = NULL_TREE;
10728	      else if (TREE_CODE (cname) == NAMESPACE_DECL)
10729		{
10730		  ctype = NULL_TREE;
10731		  in_namespace = TREE_OPERAND (decl, 0);
10732		  TREE_OPERAND (decl, 0) = NULL_TREE;
10733		}
10734	      else if (! is_aggr_type (cname, 1))
10735		TREE_OPERAND (decl, 0) = NULL_TREE;
10736	      /* Must test TREE_OPERAND (decl, 1), in case user gives
10737		 us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
10738	      else if (TREE_OPERAND (decl, 1)
10739		       && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
10740		ctype = cname;
10741	      else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
10742		       || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
10743		{
10744	  	  /* This might be declaring a member of a template
10745		     parm to be a friend.  */
10746		  ctype = cname;
10747		  dependant_name = TREE_OPERAND (decl, 1);
10748		}
10749	      else if (ctype == NULL_TREE)
10750		ctype = cname;
10751	      else if (TREE_COMPLEXITY (decl) == current_class_depth)
10752		TREE_OPERAND (decl, 0) = ctype;
10753	      else
10754		{
10755		  if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
10756		    {
10757		      error ("type `%T' is not derived from type `%T'",
10758				cname, ctype);
10759		      TREE_OPERAND (decl, 0) = NULL_TREE;
10760		    }
10761		  else
10762		    ctype = cname;
10763		}
10764
10765	      /* If the parser sees something like "void a::b" where
10766		 "a::b" is a namespace, it will build a SCOPE_REF with
10767		 a NAMESPACE_DECL, rather than an IDENTIFIER_NODE, as
10768		 the second operand.  Since the SCOPE_REF is being
10769		 used as a declarator, we recover from that here.  */
10770	      if (TREE_CODE (TREE_OPERAND (decl, 1)) == NAMESPACE_DECL)
10771		TREE_OPERAND (decl, 1) = DECL_NAME (TREE_OPERAND (decl, 1));
10772
10773	      if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
10774		  && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
10775					 ctype))
10776		TREE_OPERAND (decl, 1) = constructor_name (ctype);
10777	      next = &TREE_OPERAND (decl, 1);
10778	      decl = *next;
10779	      if (ctype)
10780		{
10781		  if (TREE_CODE (decl) == IDENTIFIER_NODE
10782		      && constructor_name (ctype) == decl)
10783		    {
10784		      sfk = sfk_constructor;
10785		      ctor_return_type = ctype;
10786		    }
10787		  else if (TREE_CODE (decl) == BIT_NOT_EXPR
10788			   && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
10789			   && constructor_name_p (TREE_OPERAND (decl, 0),
10790						  ctype))
10791		    {
10792		      sfk = sfk_destructor;
10793		      ctor_return_type = ctype;
10794		      flags = DTOR_FLAG;
10795		      TREE_OPERAND (decl, 0) = constructor_name (ctype);
10796		      next = &TREE_OPERAND (decl, 0);
10797		    }
10798		}
10799	    }
10800	    break;
10801
10802	  case ERROR_MARK:
10803	    next = 0;
10804	    break;
10805
10806	  case TYPE_DECL:
10807	    /* Parse error puts this typespec where
10808	       a declarator should go.  */
10809	    error ("`%T' specified as declarator-id", DECL_NAME (decl));
10810	    if (TREE_TYPE (decl) == current_class_type)
10811	      error ("  perhaps you want `%T' for a constructor",
10812			current_class_name);
10813	    dname = DECL_NAME (decl);
10814	    name = IDENTIFIER_POINTER (dname);
10815
10816	    /* Avoid giving two errors for this.  */
10817	    IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
10818
10819	    declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
10820	    *next = dname;
10821	    next = 0;
10822	    break;
10823
10824	  case BASELINK:
10825	    next = &BASELINK_FUNCTIONS (decl);
10826	    break;
10827
10828	  case TEMPLATE_DECL:
10829	    /* Sometimes, we see a template-name used as part of a
10830	       decl-specifier like in
10831	          std::allocator alloc;
10832	       Handle that gracefully.  */
10833	    error ("invalid use of template-name '%E' in a declarator", decl);
10834	    return error_mark_node;
10835	    break;
10836
10837	  default:
10838	    my_friendly_assert (0, 20020917);
10839	  }
10840      }
10841  }
10842
10843  /* A function definition's declarator must have the form of
10844     a function declarator.  */
10845
10846  if (funcdef_flag && innermost_code != CALL_EXPR)
10847    return 0;
10848
10849  if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
10850      && innermost_code != CALL_EXPR
10851      && ! (ctype && declspecs == NULL_TREE))
10852    {
10853      error ("declaration of `%D' as non-function", dname);
10854      return void_type_node;
10855    }
10856
10857  /* Anything declared one level down from the top level
10858     must be one of the parameters of a function
10859     (because the body is at least two levels down).  */
10860
10861  /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10862     by not allowing C++ class definitions to specify their parameters
10863     with xdecls (must be spec.d in the parmlist).
10864
10865     Since we now wait to push a class scope until we are sure that
10866     we are in a legitimate method context, we must set oldcname
10867     explicitly (since current_class_name is not yet alive).
10868
10869     We also want to avoid calling this a PARM if it is in a namespace.  */
10870
10871  if (decl_context == NORMAL && !toplevel_bindings_p ())
10872    {
10873      struct cp_binding_level *b = current_binding_level;
10874      current_binding_level = b->level_chain;
10875      if (current_binding_level != 0 && toplevel_bindings_p ())
10876	decl_context = PARM;
10877      current_binding_level = b;
10878    }
10879
10880  if (name == NULL)
10881    name = decl_context == PARM ? "parameter" : "type name";
10882
10883  /* Look through the decl specs and record which ones appear.
10884     Some typespecs are defined as built-in typenames.
10885     Others, the ones that are modifiers of other types,
10886     are represented by bits in SPECBITS: set the bits for
10887     the modifiers that appear.  Storage class keywords are also in SPECBITS.
10888
10889     If there is a typedef name or a type, store the type in TYPE.
10890     This includes builtin typedefs such as `int'.
10891
10892     Set EXPLICIT_INT if the type is `int' or `char' and did not
10893     come from a user typedef.
10894
10895     Set LONGLONG if `long' is mentioned twice.
10896
10897     For C++, constructors and destructors have their own fast treatment.  */
10898
10899  for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10900    {
10901      register int i;
10902      register tree id;
10903
10904      /* Certain parse errors slip through.  For example,
10905	 `int class;' is not caught by the parser. Try
10906	 weakly to recover here.  */
10907      if (TREE_CODE (spec) != TREE_LIST)
10908	return 0;
10909
10910      id = TREE_VALUE (spec);
10911
10912      /* If the entire declaration is itself tagged as deprecated then
10913         suppress reports of deprecated items.  */
10914      if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10915        {
10916	  if (deprecated_state != DEPRECATED_SUPPRESS)
10917	    warn_deprecated_use (id);
10918        }
10919
10920      if (TREE_CODE (id) == IDENTIFIER_NODE)
10921	{
10922	  if (id == ridpointers[(int) RID_INT]
10923	      || id == ridpointers[(int) RID_CHAR]
10924	      || id == ridpointers[(int) RID_BOOL]
10925	      || id == ridpointers[(int) RID_WCHAR])
10926	    {
10927	      if (type)
10928		{
10929		  if (id == ridpointers[(int) RID_BOOL])
10930		    error ("`bool' is now a keyword");
10931		  else
10932		    error ("extraneous `%T' ignored", id);
10933		}
10934	      else
10935		{
10936		  if (id == ridpointers[(int) RID_INT])
10937		    explicit_int = 1;
10938		  else if (id == ridpointers[(int) RID_CHAR])
10939		    explicit_char = 1;
10940		  type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10941		}
10942	      goto found;
10943	    }
10944	  /* C++ aggregate types.  */
10945	  if (IDENTIFIER_HAS_TYPE_VALUE (id))
10946	    {
10947	      if (type)
10948		error ("multiple declarations `%T' and `%T'", type, id);
10949	      else
10950		type = IDENTIFIER_TYPE_VALUE (id);
10951	      goto found;
10952	    }
10953
10954	  for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10955	    {
10956	      if (ridpointers[i] == id)
10957		{
10958		  if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10959		    {
10960		      if (pedantic && ! in_system_header && warn_long_long)
10961			pedwarn ("ISO C++ does not support `long long'");
10962		      if (longlong)
10963			error ("`long long long' is too long for GCC");
10964		      else
10965			longlong = 1;
10966		    }
10967		  else if (RIDBIT_SETP (i, specbits))
10968		    pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10969
10970		  /* Diagnose "__thread extern".  Recall that this list
10971		     is in the reverse order seen in the text.  */
10972		  if (i == (int)RID_THREAD)
10973		    {
10974		      if (RIDBIT_SETP (RID_EXTERN, specbits))
10975			error ("`__thread' before `extern'");
10976		      if (RIDBIT_SETP (RID_STATIC, specbits))
10977			error ("`__thread' before `static'");
10978		    }
10979
10980		  if (i == (int)RID_EXTERN
10981		      && TREE_PURPOSE (spec) == error_mark_node)
10982		    /* This extern was part of a language linkage.  */
10983		    extern_langp = 1;
10984
10985		  RIDBIT_SET (i, specbits);
10986		  goto found;
10987		}
10988	    }
10989	}
10990      else if (TREE_CODE (id) == TYPE_DECL)
10991	{
10992	  if (type)
10993	    error ("multiple declarations `%T' and `%T'", type,
10994		      TREE_TYPE (id));
10995	  else
10996	    {
10997	      type = TREE_TYPE (id);
10998	      TREE_VALUE (spec) = type;
10999	      typedef_decl = id;
11000	    }
11001	  goto found;
11002	}
11003      if (type)
11004	error ("two or more data types in declaration of `%s'", name);
11005      else if (TREE_CODE (id) == IDENTIFIER_NODE)
11006	{
11007	  register tree t = lookup_name (id, 1);
11008	  if (!t || TREE_CODE (t) != TYPE_DECL)
11009	    error ("`%s' fails to be a typedef or built in type",
11010		   IDENTIFIER_POINTER (id));
11011	  else
11012	    {
11013	      type = TREE_TYPE (t);
11014	      typedef_decl = t;
11015	    }
11016	}
11017      else if (id != error_mark_node)
11018	/* Can't change CLASS nodes into RECORD nodes here!  */
11019	type = id;
11020
11021    found: ;
11022    }
11023
11024#if 0
11025  /* See the code below that used this.  */
11026  if (typedef_decl)
11027    decl_attr = DECL_ATTRIBUTES (typedef_decl);
11028#endif
11029  typedef_type = type;
11030
11031  /* No type at all: default to `int', and set DEFAULTED_INT
11032     because it was not a user-defined typedef.  */
11033
11034  if (type == NULL_TREE
11035      && (RIDBIT_SETP (RID_SIGNED, specbits)
11036	  || RIDBIT_SETP (RID_UNSIGNED, specbits)
11037	  || RIDBIT_SETP (RID_LONG, specbits)
11038	  || RIDBIT_SETP (RID_SHORT, specbits)))
11039    {
11040      /* These imply 'int'.  */
11041      type = integer_type_node;
11042      defaulted_int = 1;
11043    }
11044
11045  if (sfk != sfk_none)
11046    type = check_special_function_return_type (sfk, type,
11047					       ctor_return_type);
11048  else if (type == NULL_TREE)
11049    {
11050      int is_main;
11051
11052      explicit_int = -1;
11053
11054      /* We handle `main' specially here, because 'main () { }' is so
11055	 common.  With no options, it is allowed.  With -Wreturn-type,
11056	 it is a warning.  It is only an error with -pedantic-errors.  */
11057      is_main = (funcdef_flag
11058		 && dname && MAIN_NAME_P (dname)
11059		 && ctype == NULL_TREE
11060		 && in_namespace == NULL_TREE
11061		 && current_namespace == global_namespace);
11062
11063      if (in_system_header || flag_ms_extensions)
11064	/* Allow it, sigh.  */;
11065      else if (pedantic || ! is_main)
11066	pedwarn ("ISO C++ forbids declaration of `%s' with no type",
11067		    name);
11068      else if (warn_return_type)
11069	warning ("ISO C++ forbids declaration of `%s' with no type",
11070		    name);
11071
11072      type = integer_type_node;
11073    }
11074
11075  if (type && IMPLICIT_TYPENAME_P (type))
11076    {
11077      /* The implicit typename extension is deprecated and will be
11078	 removed.  Warn about its use now.  */
11079      warning ("`%T' is implicitly a typename", type);
11080      cp_deprecated ("implicit typename");
11081
11082      /* Now remove its implicitness, so that we don't warn again.
11083         For instance this might be a typedef, and we do not want to
11084         warn on uses of the typedef itself.  Simply clearing the
11085         TREE_TYPE is insufficient.  */
11086      type = copy_node (type);
11087      TREE_TYPE (type) = NULL_TREE;
11088    }
11089
11090  ctype = NULL_TREE;
11091
11092  /* Now process the modifiers that were specified
11093     and check for invalid combinations.  */
11094
11095  /* Long double is a special combination.  */
11096
11097  if (RIDBIT_SETP (RID_LONG, specbits)
11098      && TYPE_MAIN_VARIANT (type) == double_type_node)
11099    {
11100      RIDBIT_RESET (RID_LONG, specbits);
11101      type = build_qualified_type (long_double_type_node,
11102				   cp_type_quals (type));
11103    }
11104
11105  /* Check all other uses of type modifiers.  */
11106
11107  if (RIDBIT_SETP (RID_UNSIGNED, specbits)
11108      || RIDBIT_SETP (RID_SIGNED, specbits)
11109      || RIDBIT_SETP (RID_LONG, specbits)
11110      || RIDBIT_SETP (RID_SHORT, specbits))
11111    {
11112      int ok = 0;
11113
11114      if (TREE_CODE (type) == REAL_TYPE)
11115	error ("short, signed or unsigned invalid for `%s'", name);
11116      else if (TREE_CODE (type) != INTEGER_TYPE)
11117	error ("long, short, signed or unsigned invalid for `%s'", name);
11118      else if (RIDBIT_SETP (RID_LONG, specbits)
11119	       && RIDBIT_SETP (RID_SHORT, specbits))
11120	error ("long and short specified together for `%s'", name);
11121      else if ((RIDBIT_SETP (RID_LONG, specbits)
11122		|| RIDBIT_SETP (RID_SHORT, specbits))
11123	       && explicit_char)
11124	error ("long or short specified with char for `%s'", name);
11125      else if ((RIDBIT_SETP (RID_LONG, specbits)
11126		|| RIDBIT_SETP (RID_SHORT, specbits))
11127	       && TREE_CODE (type) == REAL_TYPE)
11128	error ("long or short specified with floating type for `%s'", name);
11129      else if (RIDBIT_SETP (RID_SIGNED, specbits)
11130	       && RIDBIT_SETP (RID_UNSIGNED, specbits))
11131	error ("signed and unsigned given together for `%s'", name);
11132      else
11133	{
11134	  ok = 1;
11135	  if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
11136	    {
11137	      pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
11138		       name);
11139	      if (flag_pedantic_errors)
11140		ok = 0;
11141	    }
11142	}
11143
11144      /* Discard the type modifiers if they are invalid.  */
11145      if (! ok)
11146	{
11147	  RIDBIT_RESET (RID_UNSIGNED, specbits);
11148	  RIDBIT_RESET (RID_SIGNED, specbits);
11149	  RIDBIT_RESET (RID_LONG, specbits);
11150	  RIDBIT_RESET (RID_SHORT, specbits);
11151	  longlong = 0;
11152	}
11153    }
11154
11155  if (RIDBIT_SETP (RID_COMPLEX, specbits)
11156      && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
11157    {
11158      error ("complex invalid for `%s'", name);
11159      RIDBIT_RESET (RID_COMPLEX, specbits);
11160    }
11161
11162  /* Decide whether an integer type is signed or not.
11163     Optionally treat bitfields as signed by default.  */
11164  if (RIDBIT_SETP (RID_UNSIGNED, specbits)
11165      /* [class.bit]
11166
11167	 It is implementation-defined whether a plain (neither
11168	 explicitly signed or unsigned) char, short, int, or long
11169	 bit-field is signed or unsigned.
11170
11171	 Naturally, we extend this to long long as well.  Note that
11172	 this does not include wchar_t.  */
11173      || (bitfield && !flag_signed_bitfields
11174	  && RIDBIT_NOTSETP (RID_SIGNED, specbits)
11175	  /* A typedef for plain `int' without `signed' can be
11176	     controlled just like plain `int', but a typedef for
11177	     `signed int' cannot be so controlled.  */
11178	  && !(typedef_decl
11179	       && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
11180	  && (TREE_CODE (type) == INTEGER_TYPE
11181	      || TREE_CODE (type) == CHAR_TYPE)
11182	  && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
11183    {
11184      if (longlong)
11185	type = long_long_unsigned_type_node;
11186      else if (RIDBIT_SETP (RID_LONG, specbits))
11187	type = long_unsigned_type_node;
11188      else if (RIDBIT_SETP (RID_SHORT, specbits))
11189	type = short_unsigned_type_node;
11190      else if (type == char_type_node)
11191	type = unsigned_char_type_node;
11192      else if (typedef_decl)
11193	type = c_common_unsigned_type (type);
11194      else
11195	type = unsigned_type_node;
11196    }
11197  else if (RIDBIT_SETP (RID_SIGNED, specbits)
11198	   && type == char_type_node)
11199    type = signed_char_type_node;
11200  else if (longlong)
11201    type = long_long_integer_type_node;
11202  else if (RIDBIT_SETP (RID_LONG, specbits))
11203    type = long_integer_type_node;
11204  else if (RIDBIT_SETP (RID_SHORT, specbits))
11205    type = short_integer_type_node;
11206
11207  if (RIDBIT_SETP (RID_COMPLEX, specbits))
11208    {
11209      /* If we just have "complex", it is equivalent to
11210	 "complex double", but if any modifiers at all are specified it is
11211	 the complex form of TYPE.  E.g, "complex short" is
11212	 "complex short int".  */
11213
11214      if (defaulted_int && ! longlong
11215	  && ! (RIDBIT_SETP (RID_LONG, specbits)
11216		|| RIDBIT_SETP (RID_SHORT, specbits)
11217		|| RIDBIT_SETP (RID_SIGNED, specbits)
11218		|| RIDBIT_SETP (RID_UNSIGNED, specbits)))
11219	type = complex_double_type_node;
11220      else if (type == integer_type_node)
11221	type = complex_integer_type_node;
11222      else if (type == float_type_node)
11223	type = complex_float_type_node;
11224      else if (type == double_type_node)
11225	type = complex_double_type_node;
11226      else if (type == long_double_type_node)
11227	type = complex_long_double_type_node;
11228      else
11229	type = build_complex_type (type);
11230    }
11231
11232  type_quals = TYPE_UNQUALIFIED;
11233  if (RIDBIT_SETP (RID_CONST, specbits))
11234    type_quals |= TYPE_QUAL_CONST;
11235  if (RIDBIT_SETP (RID_VOLATILE, specbits))
11236    type_quals |= TYPE_QUAL_VOLATILE;
11237  if (RIDBIT_SETP (RID_RESTRICT, specbits))
11238    type_quals |= TYPE_QUAL_RESTRICT;
11239  if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
11240    error ("qualifiers are not allowed on declaration of `operator %T'",
11241	      ctor_return_type);
11242
11243  type_quals |= cp_type_quals (type);
11244  type = cp_build_qualified_type_real
11245    (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
11246 			 ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
11247  /* We might have ignored or rejected some of the qualifiers.  */
11248  type_quals = cp_type_quals (type);
11249
11250  staticp = 0;
11251  inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
11252  virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
11253  RIDBIT_RESET (RID_VIRTUAL, specbits);
11254  explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
11255  RIDBIT_RESET (RID_EXPLICIT, specbits);
11256
11257  if (RIDBIT_SETP (RID_STATIC, specbits))
11258    staticp = 1 + (decl_context == FIELD);
11259
11260  if (virtualp && staticp == 2)
11261    {
11262      error ("member `%D' cannot be declared both virtual and static",
11263		dname);
11264      staticp = 0;
11265    }
11266  friendp = RIDBIT_SETP (RID_FRIEND, specbits);
11267  RIDBIT_RESET (RID_FRIEND, specbits);
11268
11269  if (dependant_name && !friendp)
11270    {
11271      error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
11272      return void_type_node;
11273    }
11274
11275  /* Warn if two storage classes are given. Default to `auto'.  */
11276
11277  if (RIDBIT_ANY_SET (specbits))
11278    {
11279      if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
11280      if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
11281      if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
11282      if (decl_context == PARM && nclasses > 0)
11283	error ("storage class specifiers invalid in parameter declarations");
11284      if (RIDBIT_SETP (RID_TYPEDEF, specbits))
11285	{
11286	  if (decl_context == PARM)
11287	    error ("typedef declaration invalid in parameter declaration");
11288	  nclasses++;
11289	}
11290      if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
11291      if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
11292      if (!nclasses && !friendp && extern_langp)
11293	nclasses++;
11294    }
11295
11296  /* Give error if `virtual' is used outside of class declaration.  */
11297  if (virtualp
11298      && (current_class_name == NULL_TREE || decl_context != FIELD))
11299    {
11300      error ("virtual outside class declaration");
11301      virtualp = 0;
11302    }
11303
11304  /* Static anonymous unions are dealt with here.  */
11305  if (staticp && decl_context == TYPENAME
11306      && TREE_CODE (declspecs) == TREE_LIST
11307      && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
11308    decl_context = FIELD;
11309
11310  /* Warn about storage classes that are invalid for certain
11311     kinds of declarations (parameters, typenames, etc.).  */
11312
11313  /* "static __thread" and "extern __thread" are allowed.  */
11314  if (nclasses == 2
11315      && RIDBIT_SETP (RID_THREAD, specbits)
11316      && (RIDBIT_SETP (RID_EXTERN, specbits)
11317	  || RIDBIT_SETP (RID_STATIC, specbits)))
11318    nclasses = 1;
11319
11320  if (nclasses > 1)
11321    error ("multiple storage classes in declaration of `%s'", name);
11322  else if (decl_context != NORMAL && nclasses > 0)
11323    {
11324      if ((decl_context == PARM || decl_context == CATCHPARM)
11325	  && (RIDBIT_SETP (RID_REGISTER, specbits)
11326	      || RIDBIT_SETP (RID_AUTO, specbits)))
11327	;
11328      else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
11329	;
11330      else if (decl_context == FIELD
11331	       /* C++ allows static class elements  */
11332	       && RIDBIT_SETP (RID_STATIC, specbits))
11333	/* C++ also allows inlines and signed and unsigned elements,
11334	   but in those cases we don't come in here.  */
11335	;
11336      else
11337	{
11338	  if (decl_context == FIELD)
11339	    {
11340	      tree tmp = NULL_TREE;
11341	      register int op = 0;
11342
11343	      if (declarator)
11344		{
11345		  /* Avoid trying to get an operand off an identifier node.  */
11346		  if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11347		    tmp = declarator;
11348		  else
11349		    tmp = TREE_OPERAND (declarator, 0);
11350		  op = IDENTIFIER_OPNAME_P (tmp);
11351		  if (IDENTIFIER_TYPENAME_P (tmp))
11352		    {
11353		      if (IDENTIFIER_GLOBAL_VALUE (tmp)
11354			  && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
11355			      == TYPE_DECL))
11356			name = IDENTIFIER_POINTER (tmp);
11357		      else
11358			name = "<invalid operator>";
11359		    }
11360		}
11361	      error ("storage class specified for %s `%s'",
11362		     op ? "member operator" : "field",
11363		     name);
11364	    }
11365	  else
11366	    {
11367	      if (decl_context == PARM || decl_context == CATCHPARM)
11368		error ("storage class specified for parameter `%s'", name);
11369	      else
11370		error ("storage class specified for typename");
11371	    }
11372	  RIDBIT_RESET (RID_REGISTER, specbits);
11373	  RIDBIT_RESET (RID_AUTO, specbits);
11374	  RIDBIT_RESET (RID_EXTERN, specbits);
11375	  RIDBIT_RESET (RID_THREAD, specbits);
11376	}
11377    }
11378  else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
11379    {
11380      if (toplevel_bindings_p ())
11381	{
11382	  /* It's common practice (and completely valid) to have a const
11383	     be initialized and declared extern.  */
11384	  if (!(type_quals & TYPE_QUAL_CONST))
11385	    warning ("`%s' initialized and declared `extern'", name);
11386	}
11387      else
11388	error ("`%s' has both `extern' and initializer", name);
11389    }
11390  else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
11391	   && ! toplevel_bindings_p ())
11392    error ("nested function `%s' declared `extern'", name);
11393  else if (toplevel_bindings_p ())
11394    {
11395      if (RIDBIT_SETP (RID_AUTO, specbits))
11396	error ("top-level declaration of `%s' specifies `auto'", name);
11397    }
11398  else if (RIDBIT_SETP (RID_THREAD, specbits)
11399	   && !RIDBIT_SETP (RID_EXTERN, specbits)
11400	   && !RIDBIT_SETP (RID_STATIC, specbits))
11401    {
11402      error ("function-scope `%s' implicitly auto and declared `__thread'",
11403	     name);
11404      RIDBIT_RESET (RID_THREAD, specbits);
11405    }
11406
11407  if (nclasses > 0 && friendp)
11408    error ("storage class specifiers invalid in friend function declarations");
11409
11410  /* Now figure out the structure of the declarator proper.
11411     Descend through it, creating more complex types, until we reach
11412     the declared identifier (or NULL_TREE, in an abstract declarator).  */
11413
11414  while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
11415	 && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
11416    {
11417      /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
11418	 an INDIRECT_REF (for *...),
11419	 a CALL_EXPR (for ...(...)),
11420	 an identifier (for the name being declared)
11421	 or a null pointer (for the place in an absolute declarator
11422	 where the name was omitted).
11423	 For the last two cases, we have just exited the loop.
11424
11425	 For C++ it could also be
11426	 a SCOPE_REF (for class :: ...).  In this case, we have converted
11427	 sensible names to types, and those are the values we use to
11428	 qualify the member name.
11429	 an ADDR_EXPR (for &...),
11430	 a BIT_NOT_EXPR (for destructors)
11431
11432	 At this point, TYPE is the type of elements of an array,
11433	 or for a function to return, or for a pointer to point to.
11434	 After this sequence of ifs, TYPE is the type of the
11435	 array or function or pointer, and DECLARATOR has had its
11436	 outermost layer removed.  */
11437
11438      if (type == error_mark_node)
11439	{
11440	  if (TREE_CODE (declarator) == SCOPE_REF)
11441	    declarator = TREE_OPERAND (declarator, 1);
11442	  else
11443	    declarator = TREE_OPERAND (declarator, 0);
11444	  continue;
11445	}
11446      if (quals != NULL_TREE
11447	  && (declarator == NULL_TREE
11448	      || TREE_CODE (declarator) != SCOPE_REF))
11449	{
11450	  if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
11451	    ctype = TYPE_METHOD_BASETYPE (type);
11452	  if (ctype != NULL_TREE)
11453	    {
11454	      tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
11455	      grok_method_quals (ctype, dummy, quals);
11456	      type = TREE_TYPE (dummy);
11457	      ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (type)));
11458	      quals = NULL_TREE;
11459	    }
11460	}
11461
11462      switch (TREE_CODE (declarator))
11463	{
11464	case TREE_LIST:
11465	  {
11466	    /* We encode a declarator with embedded attributes using
11467	       a TREE_LIST.  */
11468	    tree attrs = TREE_PURPOSE (declarator);
11469	    tree inner_decl;
11470	    int attr_flags;
11471
11472	    declarator = TREE_VALUE (declarator);
11473	    inner_decl = declarator;
11474	    while (inner_decl != NULL_TREE
11475		   && TREE_CODE (inner_decl) == TREE_LIST)
11476	      inner_decl = TREE_VALUE (inner_decl);
11477	    attr_flags = 0;
11478	    if (inner_decl == NULL_TREE
11479		|| TREE_CODE (inner_decl) == IDENTIFIER_NODE)
11480	      attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
11481	    if (TREE_CODE (inner_decl) == CALL_EXPR)
11482	      attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
11483	    if (TREE_CODE (inner_decl) == ARRAY_REF)
11484	      attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
11485	    returned_attrs = decl_attributes (&type,
11486					      chainon (returned_attrs, attrs),
11487					      attr_flags);
11488	  }
11489	  break;
11490
11491	case ARRAY_REF:
11492	  {
11493	    register tree size;
11494
11495	    size = TREE_OPERAND (declarator, 1);
11496
11497	    /* VC++ spells a zero-sized array with [].  */
11498	    if (size == NULL_TREE && decl_context == FIELD && !	staticp
11499		&& ! RIDBIT_SETP (RID_TYPEDEF, specbits))
11500	      size = integer_zero_node;
11501
11502	    declarator = TREE_OPERAND (declarator, 0);
11503
11504	    type = create_array_type_for_decl (dname, type, size);
11505
11506	    ctype = NULL_TREE;
11507	  }
11508	  break;
11509
11510	case CALL_EXPR:
11511	  {
11512	    tree arg_types;
11513	    int funcdecl_p;
11514	    tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
11515	    tree inner_decl = TREE_OPERAND (declarator, 0);
11516
11517	    /* Declaring a function type.
11518	       Make sure we have a valid type for the function to return.  */
11519
11520	    /* We now know that the TYPE_QUALS don't apply to the
11521               decl, but to its return type.  */
11522	    type_quals = TYPE_UNQUALIFIED;
11523
11524	    /* Warn about some types functions can't return.  */
11525
11526	    if (TREE_CODE (type) == FUNCTION_TYPE)
11527	      {
11528		error ("`%s' declared as function returning a function", name);
11529		type = integer_type_node;
11530	      }
11531	    if (TREE_CODE (type) == ARRAY_TYPE)
11532	      {
11533		error ("`%s' declared as function returning an array", name);
11534		type = integer_type_node;
11535	      }
11536
11537	    if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
11538	      inner_decl = TREE_OPERAND (inner_decl, 1);
11539
11540	    if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
11541	      inner_decl = dname;
11542
11543	    /* Pick up type qualifiers which should be applied to `this'.  */
11544	    quals = CALL_DECLARATOR_QUALS (declarator);
11545
11546	    /* Pick up the exception specifications.  */
11547	    raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
11548
11549	    /* Say it's a definition only for the CALL_EXPR
11550	       closest to the identifier.  */
11551	    funcdecl_p
11552	      = inner_decl
11553	      && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
11554		  || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
11555		  || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
11556
11557	    if (ctype == NULL_TREE
11558		&& decl_context == FIELD
11559		&& funcdecl_p
11560		&& (friendp == 0 || dname == current_class_name))
11561	      ctype = current_class_type;
11562
11563	    if (ctype && sfk == sfk_conversion)
11564	      TYPE_HAS_CONVERSION (ctype) = 1;
11565	    if (ctype && constructor_name (ctype) == dname)
11566	      {
11567		/* We are within a class's scope. If our declarator name
11568		   is the same as the class name, and we are defining
11569		   a function, then it is a constructor/destructor, and
11570		   therefore returns a void type.  */
11571
11572		if (flags == DTOR_FLAG)
11573		  {
11574		    /* ISO C++ 12.4/2.  A destructor may not be
11575		       declared const or volatile.  A destructor may
11576		       not be static.  */
11577		    if (staticp == 2)
11578		      error ("destructor cannot be static member function");
11579		    if (quals)
11580		      {
11581			error ("destructors may not be `%s'",
11582				  IDENTIFIER_POINTER (TREE_VALUE (quals)));
11583			quals = NULL_TREE;
11584		      }
11585		    if (decl_context == FIELD)
11586		      {
11587			if (! member_function_or_else (ctype,
11588						       current_class_type,
11589						       flags))
11590			  return void_type_node;
11591		      }
11592		  }
11593		else            /* It's a constructor.  */
11594		  {
11595		    if (explicitp == 1)
11596		      explicitp = 2;
11597		    /* ISO C++ 12.1.  A constructor may not be
11598		       declared const or volatile.  A constructor may
11599		       not be virtual.  A constructor may not be
11600		       static.  */
11601		    if (staticp == 2)
11602		      error ("constructor cannot be static member function");
11603		    if (virtualp)
11604		      {
11605			pedwarn ("constructors cannot be declared virtual");
11606			virtualp = 0;
11607		      }
11608		    if (quals)
11609		      {
11610			error ("constructors may not be `%s'",
11611				  IDENTIFIER_POINTER (TREE_VALUE (quals)));
11612			quals = NULL_TREE;
11613		      }
11614		    {
11615		      RID_BIT_TYPE tmp_bits;
11616		      memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
11617		      RIDBIT_RESET (RID_INLINE, tmp_bits);
11618		      RIDBIT_RESET (RID_STATIC, tmp_bits);
11619		      if (RIDBIT_ANY_SET (tmp_bits))
11620			error ("return value type specifier for constructor ignored");
11621		    }
11622		    if (decl_context == FIELD)
11623		      {
11624			if (! member_function_or_else (ctype,
11625						       current_class_type,
11626						       flags))
11627			  return void_type_node;
11628			TYPE_HAS_CONSTRUCTOR (ctype) = 1;
11629			if (sfk != sfk_constructor)
11630			  return NULL_TREE;
11631		      }
11632		  }
11633		if (decl_context == FIELD)
11634		  staticp = 0;
11635	      }
11636	    else if (friendp)
11637	      {
11638		if (initialized)
11639		  error ("can't initialize friend function `%s'", name);
11640		if (virtualp)
11641		  {
11642		    /* Cannot be both friend and virtual.  */
11643		    error ("virtual functions cannot be friends");
11644		    RIDBIT_RESET (RID_FRIEND, specbits);
11645		    friendp = 0;
11646		  }
11647		if (decl_context == NORMAL)
11648		  error ("friend declaration not in class definition");
11649		if (current_function_decl && funcdef_flag)
11650		  error ("can't define friend function `%s' in a local class definition",
11651			    name);
11652	      }
11653
11654	    /* Construct the function type and go to the next
11655	       inner layer of declarator.  */
11656
11657	    declarator = TREE_OPERAND (declarator, 0);
11658
11659	    /* FIXME: This is where default args should be fully
11660	       processed.  */
11661
11662	    arg_types = grokparms (inner_parms);
11663
11664	    if (declarator && flags == DTOR_FLAG)
11665	      {
11666		/* A destructor declared in the body of a class will
11667		   be represented as a BIT_NOT_EXPR.  But, we just
11668		   want the underlying IDENTIFIER.  */
11669		if (TREE_CODE (declarator) == BIT_NOT_EXPR)
11670		  declarator = TREE_OPERAND (declarator, 0);
11671
11672                if (arg_types != void_list_node)
11673		  {
11674		    error ("destructors may not have parameters");
11675		    arg_types = void_list_node;
11676		    last_function_parms = NULL_TREE;
11677		  }
11678	      }
11679
11680	    /* ANSI says that `const int foo ();'
11681	       does not make the function foo const.  */
11682	    type = build_function_type (type, arg_types);
11683
11684	    {
11685	      tree t;
11686	      for (t = arg_types; t; t = TREE_CHAIN (t))
11687		if (TREE_PURPOSE (t)
11688		    && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
11689		  {
11690		    add_defarg_fn (type);
11691		    break;
11692		  }
11693	    }
11694	  }
11695	  break;
11696
11697	case ADDR_EXPR:
11698	case INDIRECT_REF:
11699	  /* Filter out pointers-to-references and references-to-references.
11700	     We can get these if a TYPE_DECL is used.  */
11701
11702	  if (TREE_CODE (type) == REFERENCE_TYPE)
11703	    {
11704	      error (TREE_CODE (declarator) == ADDR_EXPR
11705		     ? "cannot declare reference to `%#T'"
11706		     : "cannot declare pointer to `%#T'", type);
11707	      type = TREE_TYPE (type);
11708	    }
11709	  else if (VOID_TYPE_P (type)
11710		   && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
11711	    error (ctype ? "cannot declare pointer to `%#T' member"
11712		     : "cannot declare reference to `%#T'", type);
11713
11714	  /* Merge any constancy or volatility into the target type
11715	     for the pointer.  */
11716
11717	  /* We now know that the TYPE_QUALS don't apply to the decl,
11718	     but to the target of the pointer.  */
11719	  type_quals = TYPE_UNQUALIFIED;
11720
11721	  if (TREE_CODE (declarator) == ADDR_EXPR)
11722	    {
11723	      if (!VOID_TYPE_P (type))
11724		type = build_reference_type (type);
11725	    }
11726	  else if (TREE_CODE (type) == METHOD_TYPE)
11727	    type = build_ptrmemfunc_type (build_pointer_type (type));
11728	  else if (ctype)
11729	    type = build_ptrmem_type (ctype, type);
11730	  else
11731	    type = build_pointer_type (type);
11732
11733	  /* Process a list of type modifier keywords (such as
11734	     const or volatile) that were given inside the `*' or `&'.  */
11735
11736	  if (TREE_TYPE (declarator))
11737	    {
11738	      register tree typemodlist;
11739	      int erred = 0;
11740	      int constp = 0;
11741	      int volatilep = 0;
11742	      int restrictp = 0;
11743
11744	      for (typemodlist = TREE_TYPE (declarator); typemodlist;
11745		   typemodlist = TREE_CHAIN (typemodlist))
11746		{
11747		  tree qualifier = TREE_VALUE (typemodlist);
11748
11749		  if (qualifier == ridpointers[(int) RID_CONST])
11750		    {
11751		      constp++;
11752		      type_quals |= TYPE_QUAL_CONST;
11753		    }
11754		  else if (qualifier == ridpointers[(int) RID_VOLATILE])
11755		    {
11756		      volatilep++;
11757		      type_quals |= TYPE_QUAL_VOLATILE;
11758		    }
11759		  else if (qualifier == ridpointers[(int) RID_RESTRICT])
11760		    {
11761		      restrictp++;
11762		      type_quals |= TYPE_QUAL_RESTRICT;
11763		    }
11764		  else if (!erred)
11765		    {
11766		      erred = 1;
11767		      error ("invalid type modifier within pointer declarator");
11768		    }
11769		}
11770	      if (constp > 1)
11771		pedwarn ("duplicate `const'");
11772	      if (volatilep > 1)
11773		pedwarn ("duplicate `volatile'");
11774	      if (restrictp > 1)
11775		pedwarn ("duplicate `restrict'");
11776	      type = cp_build_qualified_type (type, type_quals);
11777	      type_quals = cp_type_quals (type);
11778	    }
11779	  declarator = TREE_OPERAND (declarator, 0);
11780	  ctype = NULL_TREE;
11781	  break;
11782
11783	case SCOPE_REF:
11784	  {
11785	    /* We have converted type names to NULL_TREE if the
11786	       name was bogus, or to a _TYPE node, if not.
11787
11788	       The variable CTYPE holds the type we will ultimately
11789	       resolve to.  The code here just needs to build
11790	       up appropriate member types.  */
11791	    tree sname = TREE_OPERAND (declarator, 1);
11792	    tree t;
11793
11794	    /* Destructors can have their visibilities changed as well.  */
11795	    if (TREE_CODE (sname) == BIT_NOT_EXPR)
11796	      sname = TREE_OPERAND (sname, 0);
11797
11798	    if (TREE_COMPLEXITY (declarator) == 0)
11799	      /* This needs to be here, in case we are called
11800		 multiple times.  */ ;
11801	    else if (TREE_COMPLEXITY (declarator) == -1)
11802	      /* Namespace member.  */
11803	      pop_decl_namespace ();
11804	    else if (friendp && (TREE_COMPLEXITY (declarator) < 2))
11805	      /* Don't fall out into global scope. Hides real bug? --eichin */ ;
11806	    else if (!TREE_OPERAND (declarator, 0)
11807		     || !IS_AGGR_TYPE_CODE
11808		          (TREE_CODE (TREE_OPERAND (declarator, 0))))
11809	      ;
11810	    else if (TREE_COMPLEXITY (declarator) == current_class_depth)
11811	      {
11812		/* Resolve any TYPENAME_TYPEs from the decl-specifier-seq
11813		   that refer to ctype.  They couldn't be resolved earlier
11814		   because we hadn't pushed into the class yet.
11815		   Example: resolve 'B<T>::type' in
11816		   'B<typename B<T>::type> B<T>::f () { }'.  */
11817		if (current_template_parms
11818		    && uses_template_parms (type)
11819		    && uses_template_parms (current_class_type))
11820		  {
11821		    tree args = current_template_args ();
11822		    type = tsubst (type, args, tf_error | tf_warning,
11823				   NULL_TREE);
11824		  }
11825
11826		/* This pop_nested_class corresponds to the
11827                   push_nested_class used to push into class scope for
11828                   parsing the argument list of a function decl, in
11829                   qualified_id.  */
11830		pop_nested_class ();
11831		TREE_COMPLEXITY (declarator) = current_class_depth;
11832	      }
11833	    else
11834	      abort ();
11835
11836	    if (TREE_OPERAND (declarator, 0) == NULL_TREE)
11837	      {
11838		/* We had a reference to a global decl, or
11839		   perhaps we were given a non-aggregate typedef,
11840		   in which case we cleared this out, and should just
11841		   keep going as though it wasn't there.  */
11842		declarator = sname;
11843		continue;
11844	      }
11845	    ctype = TREE_OPERAND (declarator, 0);
11846
11847	    t = ctype;
11848	    while (t != NULL_TREE && CLASS_TYPE_P (t))
11849	      {
11850		/* You're supposed to have one `template <...>'
11851		   for every template class, but you don't need one
11852		   for a full specialization.  For example:
11853
11854		     template <class T> struct S{};
11855		     template <> struct S<int> { void f(); };
11856		     void S<int>::f () {}
11857
11858		   is correct; there shouldn't be a `template <>' for
11859		   the definition of `S<int>::f'.  */
11860		if (CLASSTYPE_TEMPLATE_INFO (t)
11861		    && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
11862			|| uses_template_parms (CLASSTYPE_TI_ARGS (t)))
11863	            && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
11864		  template_count += 1;
11865
11866		t = TYPE_MAIN_DECL (t);
11867		t = DECL_CONTEXT (t);
11868	      }
11869
11870	    if (sname == NULL_TREE)
11871	      goto done_scoping;
11872
11873	    if (TREE_CODE (sname) == IDENTIFIER_NODE)
11874	      {
11875		/* This is the `standard' use of the scoping operator:
11876		   basetype :: member .  */
11877
11878		if (ctype == current_class_type)
11879		  {
11880		    /* class A {
11881		         void A::f ();
11882		       };
11883
11884		       Is this ill-formed?  */
11885
11886		    if (pedantic)
11887		      pedwarn ("extra qualification `%T::' on member `%s' ignored",
11888				  ctype, name);
11889		  }
11890		else if (TREE_CODE (type) == FUNCTION_TYPE)
11891		  {
11892		    if (current_class_type == NULL_TREE || friendp)
11893		      type = build_cplus_method_type (ctype, TREE_TYPE (type),
11894						      TYPE_ARG_TYPES (type));
11895		    else
11896		      {
11897			error ("cannot declare member function `%T::%s' within `%T'",
11898				  ctype, name, current_class_type);
11899			return void_type_node;
11900		      }
11901		  }
11902		else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
11903			 || COMPLETE_TYPE_P (complete_type (ctype)))
11904		  {
11905		    /* Have to move this code elsewhere in this function.
11906		       this code is used for i.e., typedef int A::M; M *pm;
11907
11908		       It is?  How? jason 10/2/94 */
11909
11910		    if (current_class_type)
11911		      {
11912			error ("cannot declare member `%T::%s' within `%T'",
11913				  ctype, name, current_class_type);
11914			return void_type_node;
11915		      }
11916		  }
11917		else
11918	          {
11919	            cxx_incomplete_type_error (NULL_TREE, ctype);
11920	            return error_mark_node;
11921		  }
11922
11923		declarator = sname;
11924	      }
11925	    else if (TREE_CODE (sname) == SCOPE_REF)
11926	      abort ();
11927	    else
11928	      {
11929	      done_scoping:
11930		declarator = TREE_OPERAND (declarator, 1);
11931		if (declarator && TREE_CODE (declarator) == CALL_EXPR)
11932		  /* In this case, we will deal with it later.  */
11933		  ;
11934		else if (TREE_CODE (type) == FUNCTION_TYPE)
11935		  type = build_cplus_method_type (ctype, TREE_TYPE (type),
11936						  TYPE_ARG_TYPES (type));
11937	      }
11938	  }
11939	  break;
11940
11941	case BIT_NOT_EXPR:
11942	  declarator = TREE_OPERAND (declarator, 0);
11943	  break;
11944
11945	case BASELINK:
11946	  declarator = BASELINK_FUNCTIONS (declarator);
11947	  break;
11948
11949	case RECORD_TYPE:
11950	case UNION_TYPE:
11951	case ENUMERAL_TYPE:
11952	  declarator = NULL_TREE;
11953	  break;
11954
11955	case ERROR_MARK:
11956	  declarator = NULL_TREE;
11957	  break;
11958
11959	default:
11960	  abort ();
11961	}
11962    }
11963
11964  if (returned_attrs)
11965    {
11966      if (attrlist)
11967	*attrlist = chainon (returned_attrs, *attrlist);
11968      else
11969	attrlist = &returned_attrs;
11970    }
11971
11972  /* Now TYPE has the actual type.  */
11973
11974  /* Did array size calculations overflow?  */
11975
11976  if (TREE_CODE (type) == ARRAY_TYPE
11977      && COMPLETE_TYPE_P (type)
11978      && TREE_OVERFLOW (TYPE_SIZE (type)))
11979    {
11980      error ("size of array `%s' is too large", name);
11981      /* If we proceed with the array type as it is, we'll eventually
11982	 crash in tree_low_cst().  */
11983      type = error_mark_node;
11984    }
11985
11986  if ((decl_context == FIELD || decl_context == PARM)
11987      && !processing_template_decl
11988      && variably_modified_type_p (type))
11989    {
11990      if (decl_context == FIELD)
11991	error ("data member may not have variably modified type `%T'", type);
11992      else
11993	error ("parameter may not have variably modified type `%T'", type);
11994      type = error_mark_node;
11995    }
11996
11997  if (explicitp == 1 || (explicitp && friendp))
11998    {
11999      /* [dcl.fct.spec] The explicit specifier shall only be used in
12000         declarations of constructors within a class definition.  */
12001      error ("only declarations of constructors can be `explicit'");
12002      explicitp = 0;
12003    }
12004
12005  if (RIDBIT_SETP (RID_MUTABLE, specbits))
12006    {
12007      if (decl_context != FIELD || friendp)
12008        {
12009	  error ("non-member `%s' cannot be declared `mutable'", name);
12010          RIDBIT_RESET (RID_MUTABLE, specbits);
12011        }
12012      else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
12013	{
12014	  error ("non-object member `%s' cannot be declared `mutable'", name);
12015	  RIDBIT_RESET (RID_MUTABLE, specbits);
12016	}
12017      else if (TREE_CODE (type) == FUNCTION_TYPE
12018               || TREE_CODE (type) == METHOD_TYPE)
12019        {
12020	  error ("function `%s' cannot be declared `mutable'", name);
12021	  RIDBIT_RESET (RID_MUTABLE, specbits);
12022        }
12023      else if (staticp)
12024	{
12025	  error ("static `%s' cannot be declared `mutable'", name);
12026	  RIDBIT_RESET (RID_MUTABLE, specbits);
12027	}
12028      else if (type_quals & TYPE_QUAL_CONST)
12029	{
12030	  error ("const `%s' cannot be declared `mutable'", name);
12031 	  RIDBIT_RESET (RID_MUTABLE, specbits);
12032	}
12033    }
12034
12035  if (declarator == NULL_TREE
12036      || TREE_CODE (declarator) == IDENTIFIER_NODE
12037      || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
12038	  && (TREE_CODE (type) == FUNCTION_TYPE
12039	      || TREE_CODE (type) == METHOD_TYPE)))
12040    /* OK */;
12041  else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
12042    {
12043      error ("template-id `%D' used as a declarator", declarator);
12044      declarator = dname;
12045    }
12046  else
12047    /* Unexpected declarator format.  */
12048    abort ();
12049
12050  /* If this is declaring a typedef name, return a TYPE_DECL.  */
12051
12052  if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
12053    {
12054      tree decl;
12055
12056      /* Note that the grammar rejects storage classes
12057	 in typenames, fields or parameters.  */
12058      if (current_lang_name == lang_name_java)
12059	TYPE_FOR_JAVA (type) = 1;
12060
12061      if (decl_context == FIELD)
12062	{
12063	  if (declarator == constructor_name (current_class_type))
12064	    pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
12065			declarator);
12066	  decl = build_lang_decl (TYPE_DECL, declarator, type);
12067	}
12068      else
12069	{
12070	  decl = build_decl (TYPE_DECL, declarator, type);
12071	  if (!current_function_decl)
12072	    DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
12073	}
12074
12075      /* If the user declares "typedef struct {...} foo" then the
12076	 struct will have an anonymous name.  Fill that name in now.
12077	 Nothing can refer to it, so nothing needs know about the name
12078	 change.  */
12079      if (type != error_mark_node
12080	  && declarator
12081	  && TYPE_NAME (type)
12082	  && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
12083	  && TYPE_ANONYMOUS_P (type)
12084	  /* Don't do this if there are attributes.  */
12085	  && (!attrlist || !*attrlist)
12086	  && cp_type_quals (type) == TYPE_UNQUALIFIED)
12087	{
12088	  tree oldname = TYPE_NAME (type);
12089	  tree t;
12090
12091	  /* Replace the anonymous name with the real name everywhere.  */
12092	  lookup_tag_reverse (type, declarator);
12093	  for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
12094	    if (TYPE_NAME (t) == oldname)
12095	      TYPE_NAME (t) = decl;
12096
12097	  if (TYPE_LANG_SPECIFIC (type))
12098	    TYPE_WAS_ANONYMOUS (type) = 1;
12099
12100	  /* If this is a typedef within a template class, the nested
12101	     type is a (non-primary) template.  The name for the
12102	     template needs updating as well.  */
12103	  if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
12104	    DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
12105	      = TYPE_IDENTIFIER (type);
12106
12107	  /* FIXME remangle member functions; member functions of a
12108	     type with external linkage have external linkage.  */
12109	}
12110
12111      if (TREE_CODE (type) == OFFSET_TYPE || TREE_CODE (type) == METHOD_TYPE)
12112	{
12113	  cp_error_at ("typedef name may not be class-qualified", decl);
12114	  return NULL_TREE;
12115	}
12116      else if (quals)
12117	{
12118	  if (ctype == NULL_TREE)
12119	    {
12120	      if (TREE_CODE (type) != METHOD_TYPE)
12121		cp_error_at ("invalid type qualifier for non-member function type", decl);
12122	      else
12123		ctype = TYPE_METHOD_BASETYPE (type);
12124	    }
12125	  if (ctype != NULL_TREE)
12126	    grok_method_quals (ctype, decl, quals);
12127	}
12128
12129      if (RIDBIT_SETP (RID_SIGNED, specbits)
12130	  || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
12131	C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
12132
12133      bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
12134		      inlinep, friendp, raises != NULL_TREE);
12135
12136      return decl;
12137    }
12138
12139  /* Detect the case of an array type of unspecified size
12140     which came, as such, direct from a typedef name.
12141     We must copy the type, so that the array's domain can be
12142     individually set by the object's initializer.  */
12143
12144  if (type && typedef_type
12145      && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
12146      && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
12147    type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
12148
12149  /* Detect where we're using a typedef of function type to declare a
12150     function. last_function_parms will not be set, so we must create
12151     it now.  */
12152
12153  if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
12154    {
12155      tree decls = NULL_TREE;
12156      tree args;
12157
12158      for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
12159	{
12160	  tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
12161
12162	  TREE_CHAIN (decl) = decls;
12163	  decls = decl;
12164	}
12165
12166      last_function_parms = nreverse (decls);
12167    }
12168
12169  /* If this is a type name (such as, in a cast or sizeof),
12170     compute the type and return it now.  */
12171
12172  if (decl_context == TYPENAME)
12173    {
12174      /* Note that the grammar rejects storage classes
12175	 in typenames, fields or parameters.  */
12176      if (type_quals != TYPE_UNQUALIFIED)
12177	type_quals = TYPE_UNQUALIFIED;
12178
12179      /* Special case: "friend class foo" looks like a TYPENAME context.  */
12180      if (friendp)
12181	{
12182	  if (type_quals != TYPE_UNQUALIFIED)
12183	    {
12184	      error ("type qualifiers specified for friend class declaration");
12185	      type_quals = TYPE_UNQUALIFIED;
12186	    }
12187	  if (inlinep)
12188	    {
12189	      error ("`inline' specified for friend class declaration");
12190	      inlinep = 0;
12191	    }
12192
12193	  if (!current_aggr)
12194	    {
12195	      /* Don't allow friend declaration without a class-key.  */
12196	      if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12197		pedwarn ("template parameters cannot be friends");
12198	      else if (TREE_CODE (type) == TYPENAME_TYPE)
12199	        pedwarn ("friend declaration requires class-key, "
12200			 "i.e. `friend class %T::%D'",
12201			 TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
12202	      else
12203	        pedwarn ("friend declaration requires class-key, "
12204			 "i.e. `friend %#T'",
12205			 type);
12206	    }
12207
12208	  /* Only try to do this stuff if we didn't already give up.  */
12209	  if (type != integer_type_node)
12210	    {
12211	      decl_type_access_control (TYPE_NAME (type));
12212
12213	      /* A friendly class?  */
12214	      if (current_class_type)
12215		make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
12216	      else
12217		error ("trying to make class `%T' a friend of global scope",
12218		          type);
12219
12220	      type = void_type_node;
12221	    }
12222	}
12223      else if (quals)
12224	{
12225	  if (ctype == NULL_TREE)
12226	    {
12227	      if (TREE_CODE (type) != METHOD_TYPE)
12228	        error ("invalid qualifiers on non-member function type");
12229	      else
12230	        ctype = TYPE_METHOD_BASETYPE (type);
12231	    }
12232	  if (ctype)
12233	    {
12234	      tree dummy = build_decl (TYPE_DECL, declarator, type);
12235	      grok_method_quals (ctype, dummy, quals);
12236	      type = TREE_TYPE (dummy);
12237	    }
12238	}
12239
12240      return type;
12241    }
12242  else if (declarator == NULL_TREE && decl_context != PARM
12243	   && decl_context != CATCHPARM
12244	   && TREE_CODE (type) != UNION_TYPE
12245	   && ! bitfield)
12246    {
12247      error ("abstract declarator `%T' used as declaration", type);
12248      declarator = make_anon_name ();
12249    }
12250
12251  /* `void' at top level (not within pointer)
12252     is allowed only in typedefs or type names.
12253     We don't complain about parms either, but that is because
12254     a better error message can be made later.  */
12255
12256  if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
12257    {
12258      if (! declarator)
12259	error ("unnamed variable or field declared void");
12260      else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
12261	{
12262	  if (IDENTIFIER_OPNAME_P (declarator))
12263	    abort ();
12264	  else
12265	    error ("variable or field `%s' declared void", name);
12266	}
12267      else
12268	error ("variable or field declared void");
12269      type = integer_type_node;
12270    }
12271
12272  /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
12273     or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
12274
12275  if (decl_context == PARM || decl_context == CATCHPARM)
12276    {
12277      if (ctype || in_namespace)
12278	error ("cannot use `::' in parameter declaration");
12279
12280      /* A parameter declared as an array of T is really a pointer to T.
12281	 One declared as a function is really a pointer to a function.
12282	 One declared as a member is really a pointer to member.  */
12283
12284      if (TREE_CODE (type) == ARRAY_TYPE)
12285	{
12286	  /* Transfer const-ness of array into that of type pointed to.  */
12287	  type = build_pointer_type (TREE_TYPE (type));
12288	  type_quals = TYPE_UNQUALIFIED;
12289	}
12290      else if (TREE_CODE (type) == FUNCTION_TYPE)
12291	type = build_pointer_type (type);
12292      else if (TREE_CODE (type) == OFFSET_TYPE)
12293	type = build_pointer_type (type);
12294    }
12295
12296  {
12297    register tree decl;
12298
12299    if (decl_context == PARM)
12300      {
12301	decl = cp_build_parm_decl (declarator, type);
12302
12303	bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
12304			inlinep, friendp, raises != NULL_TREE);
12305      }
12306    else if (decl_context == FIELD)
12307      {
12308	if (type == error_mark_node)
12309	  {
12310	    /* Happens when declaring arrays of sizes which
12311	       are error_mark_node, for example.  */
12312	    decl = NULL_TREE;
12313	  }
12314	else if (in_namespace && !friendp)
12315	  {
12316	    /* Something like struct S { int N::j; };  */
12317	    error ("invalid use of `::'");
12318	    decl = NULL_TREE;
12319	  }
12320	else if (TREE_CODE (type) == FUNCTION_TYPE)
12321	  {
12322	    int publicp = 0;
12323	    tree function_context;
12324
12325	    /* We catch the others as conflicts with the builtin
12326	       typedefs.  */
12327	    if (friendp && declarator == ridpointers[(int) RID_SIGNED])
12328	      {
12329		error ("function `%D' cannot be declared friend",
12330			  declarator);
12331		friendp = 0;
12332	      }
12333
12334	    if (friendp == 0)
12335	      {
12336		if (ctype == NULL_TREE)
12337		  ctype = current_class_type;
12338
12339		if (ctype == NULL_TREE)
12340		  {
12341		    error ("can't make `%D' into a method -- not in a class",
12342			      declarator);
12343		    return void_type_node;
12344		  }
12345
12346		/* ``A union may [ ... ] not [ have ] virtual functions.''
12347		   ARM 9.5 */
12348		if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
12349		  {
12350		    error ("function `%D' declared virtual inside a union",
12351			      declarator);
12352		    return void_type_node;
12353		  }
12354
12355		if (declarator == ansi_opname (NEW_EXPR)
12356		    || declarator == ansi_opname (VEC_NEW_EXPR)
12357		    || declarator == ansi_opname (DELETE_EXPR)
12358		    || declarator == ansi_opname (VEC_DELETE_EXPR))
12359		  {
12360		    if (virtualp)
12361		      {
12362			error ("`%D' cannot be declared virtual, since it is always static",
12363				  declarator);
12364			virtualp = 0;
12365		      }
12366		  }
12367		else if (staticp < 2)
12368		  type = build_cplus_method_type (ctype, TREE_TYPE (type),
12369						  TYPE_ARG_TYPES (type));
12370	      }
12371
12372	    /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
12373	    function_context = (ctype != NULL_TREE) ?
12374	      decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
12375	    publicp = (! friendp || ! staticp)
12376	      && function_context == NULL_TREE;
12377	    decl = grokfndecl (ctype, type,
12378			       TREE_CODE (declarator) != TEMPLATE_ID_EXPR
12379			       ? declarator : dname,
12380			       declarator,
12381			       virtualp, flags, quals, raises,
12382			       friendp ? -1 : 0, friendp, publicp, inlinep,
12383			       funcdef_flag, template_count, in_namespace);
12384	    if (decl == NULL_TREE)
12385	      return decl;
12386#if 0
12387	    /* This clobbers the attrs stored in `decl' from `attrlist'.  */
12388	    /* The decl and setting of decl_attr is also turned off.  */
12389	    decl = build_decl_attribute_variant (decl, decl_attr);
12390#endif
12391
12392	    /* [class.conv.ctor]
12393
12394	       A constructor declared without the function-specifier
12395	       explicit that can be called with a single parameter
12396	       specifies a conversion from the type of its first
12397	       parameter to the type of its class.  Such a constructor
12398	       is called a converting constructor.  */
12399	    if (explicitp == 2)
12400	      DECL_NONCONVERTING_P (decl) = 1;
12401	    else if (DECL_CONSTRUCTOR_P (decl))
12402	      {
12403		/* The constructor can be called with exactly one
12404		   parameter if there is at least one parameter, and
12405		   any subsequent parameters have default arguments.
12406		   Ignore any compiler-added parms.  */
12407		tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
12408
12409		if (arg_types == void_list_node
12410		    || (arg_types
12411			&& TREE_CHAIN (arg_types)
12412			&& TREE_CHAIN (arg_types) != void_list_node
12413			&& !TREE_PURPOSE (TREE_CHAIN (arg_types))))
12414		  DECL_NONCONVERTING_P (decl) = 1;
12415	      }
12416	  }
12417	else if (TREE_CODE (type) == METHOD_TYPE)
12418	  {
12419	    /* We only get here for friend declarations of
12420	       members of other classes.  */
12421	    /* All method decls are public, so tell grokfndecl to set
12422	       TREE_PUBLIC, also.  */
12423	    decl = grokfndecl (ctype, type,
12424			       TREE_CODE (declarator) != TEMPLATE_ID_EXPR
12425			       ? declarator : dname,
12426			       declarator,
12427			       virtualp, flags, quals, raises,
12428			       friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
12429			       template_count, in_namespace);
12430	    if (decl == NULL_TREE)
12431	      return NULL_TREE;
12432	  }
12433	else if (!staticp && ! processing_template_decl
12434		 && !COMPLETE_TYPE_P (complete_type (type))
12435		 && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
12436	  {
12437	    if (declarator)
12438	      error ("field `%D' has incomplete type", declarator);
12439	    else
12440	      error ("name `%T' has incomplete type", type);
12441
12442	    /* If we're instantiating a template, tell them which
12443	       instantiation made the field's type be incomplete.  */
12444	    if (current_class_type
12445		&& TYPE_NAME (current_class_type)
12446		&& IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
12447		&& declspecs && TREE_VALUE (declspecs)
12448		&& TREE_TYPE (TREE_VALUE (declspecs)) == type)
12449	      error ("  in instantiation of template `%T'",
12450			current_class_type);
12451
12452	    type = error_mark_node;
12453	    decl = NULL_TREE;
12454	  }
12455	else
12456	  {
12457	    if (friendp)
12458	      {
12459		error ("`%s' is neither function nor member function; cannot be declared friend",
12460		       IDENTIFIER_POINTER (declarator));
12461		friendp = 0;
12462	      }
12463	    decl = NULL_TREE;
12464	  }
12465
12466	if (friendp)
12467	  {
12468	    /* Friends are treated specially.  */
12469	    if (ctype == current_class_type)
12470	      warning ("member functions are implicitly friends of their class");
12471 	    else
12472 	      {
12473 		tree t = NULL_TREE;
12474 		if (decl && DECL_NAME (decl))
12475 		  {
12476 		    if (template_class_depth (current_class_type) == 0)
12477 		      {
12478 			decl
12479 			  = check_explicit_specialization
12480 			  (declarator, decl,
12481 			   template_count, 2 * (funcdef_flag != 0) + 4);
12482 			if (decl == error_mark_node)
12483 			  return error_mark_node;
12484 		      }
12485
12486 		    t = do_friend (ctype, declarator, decl,
12487 				   last_function_parms, *attrlist,
12488				   flags, quals, funcdef_flag);
12489 		  }
12490 		if (t && funcdef_flag)
12491 		  return t;
12492
12493 		return void_type_node;
12494 	      }
12495	  }
12496
12497	/* Structure field.  It may not be a function, except for C++ */
12498
12499	if (decl == NULL_TREE)
12500	  {
12501	    if (initialized)
12502	      {
12503		if (!staticp)
12504		  {
12505		    /* An attempt is being made to initialize a non-static
12506		       member.  But, from [class.mem]:
12507
12508		       4 A member-declarator can contain a
12509		       constant-initializer only if it declares a static
12510		       member (_class.static_) of integral or enumeration
12511		       type, see _class.static.data_.
12512
12513		       This used to be relatively common practice, but
12514		       the rest of the compiler does not correctly
12515		       handle the initialization unless the member is
12516		       static so we make it static below.  */
12517		    pedwarn ("ISO C++ forbids initialization of member `%D'",
12518				declarator);
12519		    pedwarn ("making `%D' static", declarator);
12520		    staticp = 1;
12521		  }
12522
12523		if (uses_template_parms (type))
12524		  /* We'll check at instantiation time.  */
12525		  ;
12526		else if (check_static_variable_definition (declarator,
12527							   type))
12528		  /* If we just return the declaration, crashes
12529		     will sometimes occur.  We therefore return
12530		     void_type_node, as if this was a friend
12531		     declaration, to cause callers to completely
12532		     ignore this declaration.  */
12533		  return void_type_node;
12534	      }
12535
12536	    /* 9.2p13 [class.mem] */
12537	    if (declarator == constructor_name (current_class_type)
12538		/* The standard does not allow non-static data members
12539		   here either, but we agreed at the 10/99 meeting
12540		   to change that in TC 1 so that they are allowed in
12541		   classes with no user-defined constructors.  */
12542		&& staticp)
12543	      pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
12544			  declarator);
12545
12546	    if (staticp)
12547	      {
12548		/* C++ allows static class members.  All other work
12549		   for this is done by grokfield.  */
12550		decl = build_lang_decl (VAR_DECL, declarator, type);
12551		TREE_STATIC (decl) = 1;
12552		/* In class context, 'static' means public access.  */
12553		TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
12554	      }
12555	    else
12556	      {
12557		decl = build_decl (FIELD_DECL, declarator, type);
12558		DECL_NONADDRESSABLE_P (decl) = bitfield;
12559		if (RIDBIT_SETP (RID_MUTABLE, specbits))
12560		  {
12561		    DECL_MUTABLE_P (decl) = 1;
12562		    RIDBIT_RESET (RID_MUTABLE, specbits);
12563		  }
12564	      }
12565
12566	    bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
12567			    inlinep, friendp, raises != NULL_TREE);
12568	  }
12569      }
12570    else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
12571      {
12572	tree original_name;
12573	int publicp = 0;
12574
12575	if (! declarator)
12576	  return NULL_TREE;
12577
12578	if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
12579	  original_name = dname;
12580	else
12581	  original_name = declarator;
12582
12583	if (RIDBIT_SETP (RID_AUTO, specbits))
12584	  error ("storage class `auto' invalid for function `%s'", name);
12585	else if (RIDBIT_SETP (RID_REGISTER, specbits))
12586	  error ("storage class `register' invalid for function `%s'", name);
12587	else if (RIDBIT_SETP (RID_THREAD, specbits))
12588	  error ("storage class `__thread' invalid for function `%s'", name);
12589
12590	/* Function declaration not at top level.
12591	   Storage classes other than `extern' are not allowed
12592	   and `extern' makes no difference.  */
12593	if (! toplevel_bindings_p ()
12594	    && (RIDBIT_SETP (RID_STATIC, specbits)
12595		|| RIDBIT_SETP (RID_INLINE, specbits))
12596	    && pedantic)
12597	  {
12598	    if (RIDBIT_SETP (RID_STATIC, specbits))
12599	      pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
12600	    else
12601	      pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
12602	  }
12603
12604	if (ctype == NULL_TREE)
12605	  {
12606	    if (virtualp)
12607	      {
12608		error ("virtual non-class function `%s'", name);
12609		virtualp = 0;
12610	      }
12611	  }
12612	else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
12613	  type = build_cplus_method_type (ctype, TREE_TYPE (type),
12614					  TYPE_ARG_TYPES (type));
12615
12616	/* Record presence of `static'.  */
12617	publicp = (ctype != NULL_TREE
12618		   || RIDBIT_SETP (RID_EXTERN, specbits)
12619		   || !RIDBIT_SETP (RID_STATIC, specbits));
12620
12621	decl = grokfndecl (ctype, type, original_name, declarator,
12622			   virtualp, flags, quals, raises,
12623			   1, friendp,
12624			   publicp, inlinep, funcdef_flag,
12625			   template_count, in_namespace);
12626	if (decl == NULL_TREE)
12627	  return NULL_TREE;
12628
12629	if (staticp == 1)
12630	  {
12631	    int invalid_static = 0;
12632
12633	    /* Don't allow a static member function in a class, and forbid
12634	       declaring main to be static.  */
12635	    if (TREE_CODE (type) == METHOD_TYPE)
12636	      {
12637		pedwarn ("cannot declare member function `%D' to have static linkage", decl);
12638		invalid_static = 1;
12639	      }
12640	    else if (current_function_decl)
12641	      {
12642		/* FIXME need arm citation */
12643		error ("cannot declare static function inside another function");
12644		invalid_static = 1;
12645	      }
12646
12647	    if (invalid_static)
12648	      {
12649		staticp = 0;
12650		RIDBIT_RESET (RID_STATIC, specbits);
12651	      }
12652	  }
12653      }
12654    else
12655      {
12656	/* It's a variable.  */
12657
12658	/* An uninitialized decl with `extern' is a reference.  */
12659	decl = grokvardecl (type, declarator, &specbits,
12660			    initialized,
12661			    (type_quals & TYPE_QUAL_CONST) != 0,
12662			    ctype ? ctype : in_namespace);
12663	bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
12664			inlinep, friendp, raises != NULL_TREE);
12665
12666	if (ctype)
12667	  {
12668	    DECL_CONTEXT (decl) = ctype;
12669	    if (staticp == 1)
12670	      {
12671                pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
12672	        staticp = 0;
12673		RIDBIT_RESET (RID_STATIC, specbits);
12674	      }
12675	    if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
12676	      {
12677		error ("static member `%D' declared `register'", decl);
12678		RIDBIT_RESET (RID_REGISTER, specbits);
12679	      }
12680	    if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
12681	      {
12682	        pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
12683			    decl);
12684		RIDBIT_RESET (RID_EXTERN, specbits);
12685	      }
12686	  }
12687      }
12688
12689    my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
12690
12691    /* Record `register' declaration for warnings on &
12692       and in case doing stupid register allocation.  */
12693
12694    if (RIDBIT_SETP (RID_REGISTER, specbits))
12695      DECL_REGISTER (decl) = 1;
12696
12697    if (RIDBIT_SETP (RID_EXTERN, specbits))
12698      DECL_THIS_EXTERN (decl) = 1;
12699
12700    if (RIDBIT_SETP (RID_STATIC, specbits))
12701      DECL_THIS_STATIC (decl) = 1;
12702
12703    /* Record constancy and volatility.  There's no need to do this
12704       when processing a template; we'll do this for the instantiated
12705       declaration based on the type of DECL.  */
12706    if (!processing_template_decl)
12707      c_apply_type_quals_to_decl (type_quals, decl);
12708
12709    return decl;
12710  }
12711}
12712
12713/* Tell if a parmlist/exprlist looks like an exprlist or a parmlist.
12714   An empty exprlist is a parmlist.  An exprlist which
12715   contains only identifiers at the global level
12716   is a parmlist.  Otherwise, it is an exprlist.  */
12717
12718int
12719parmlist_is_exprlist (exprs)
12720     tree exprs;
12721{
12722  if (exprs == NULL_TREE || TREE_PARMLIST (exprs))
12723    return 0;
12724
12725  if (toplevel_bindings_p ())
12726    {
12727      /* At the global level, if these are all identifiers,
12728	 then it is a parmlist.  */
12729      while (exprs)
12730	{
12731	  if (TREE_CODE (TREE_VALUE (exprs)) != IDENTIFIER_NODE)
12732	    return 1;
12733	  exprs = TREE_CHAIN (exprs);
12734	}
12735      return 0;
12736    }
12737  return 1;
12738}
12739
12740/* Subroutine of start_function.  Ensure that each of the parameter
12741   types (as listed in PARMS) is complete, as is required for a
12742   function definition.  */
12743
12744static void
12745require_complete_types_for_parms (parms)
12746     tree parms;
12747{
12748  for (; parms; parms = TREE_CHAIN (parms))
12749    {
12750      if (VOID_TYPE_P (TREE_TYPE (parms)))
12751        /* grokparms will have already issued an error */
12752        TREE_TYPE (parms) = error_mark_node;
12753      else if (complete_type_or_else (TREE_TYPE (parms), parms))
12754	{
12755	  layout_decl (parms, 0);
12756	  DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
12757	}
12758    }
12759}
12760
12761/* Returns nonzero if T is a local variable.  */
12762
12763int
12764local_variable_p (t)
12765     tree t;
12766{
12767  if ((TREE_CODE (t) == VAR_DECL
12768       /* A VAR_DECL with a context that is a _TYPE is a static data
12769	  member.  */
12770       && !TYPE_P (CP_DECL_CONTEXT (t))
12771       /* Any other non-local variable must be at namespace scope.  */
12772       && !DECL_NAMESPACE_SCOPE_P (t))
12773      || (TREE_CODE (t) == PARM_DECL))
12774    return 1;
12775
12776  return 0;
12777}
12778
12779/* Returns nonzero if T is an automatic local variable or a label.
12780   (These are the declarations that need to be remapped when the code
12781   containing them is duplicated.)  */
12782
12783int
12784nonstatic_local_decl_p (t)
12785     tree t;
12786{
12787  return ((local_variable_p (t) && !TREE_STATIC (t))
12788	  || TREE_CODE (t) == LABEL_DECL
12789	  || TREE_CODE (t) == RESULT_DECL);
12790}
12791
12792/* Like local_variable_p, but suitable for use as a tree-walking
12793   function.  */
12794
12795static tree
12796local_variable_p_walkfn (tp, walk_subtrees, data)
12797     tree *tp;
12798     int *walk_subtrees ATTRIBUTE_UNUSED;
12799     void *data ATTRIBUTE_UNUSED;
12800{
12801  return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
12802	  ? *tp : NULL_TREE);
12803}
12804
12805/* Check that ARG, which is a default-argument expression for a
12806   parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
12807   something goes wrong.  DECL may also be a _TYPE node, rather than a
12808   DECL, if there is no DECL available.  */
12809
12810tree
12811check_default_argument (decl, arg)
12812     tree decl;
12813     tree arg;
12814{
12815  tree var;
12816  tree decl_type;
12817
12818  if (TREE_CODE (arg) == DEFAULT_ARG)
12819    /* We get a DEFAULT_ARG when looking at an in-class declaration
12820       with a default argument.  Ignore the argument for now; we'll
12821       deal with it after the class is complete.  */
12822    return arg;
12823
12824  if (processing_template_decl || uses_template_parms (arg))
12825    /* We don't do anything checking until instantiation-time.  Note
12826       that there may be uninstantiated arguments even for an
12827       instantiated function, since default arguments are not
12828       instantiated until they are needed.  */
12829    return arg;
12830
12831  if (TYPE_P (decl))
12832    {
12833      decl_type = decl;
12834      decl = NULL_TREE;
12835    }
12836  else
12837    decl_type = TREE_TYPE (decl);
12838
12839  if (arg == error_mark_node
12840      || decl == error_mark_node
12841      || TREE_TYPE (arg) == error_mark_node
12842      || decl_type == error_mark_node)
12843    /* Something already went wrong.  There's no need to check
12844       further.  */
12845    return error_mark_node;
12846
12847  /* [dcl.fct.default]
12848
12849     A default argument expression is implicitly converted to the
12850     parameter type.  */
12851  if (!TREE_TYPE (arg)
12852      || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
12853    {
12854      if (decl)
12855	error ("default argument for `%#D' has type `%T'",
12856		  decl, TREE_TYPE (arg));
12857      else
12858	error ("default argument for parameter of type `%T' has type `%T'",
12859		  decl_type, TREE_TYPE (arg));
12860
12861      return error_mark_node;
12862    }
12863
12864  /* [dcl.fct.default]
12865
12866     Local variables shall not be used in default argument
12867     expressions.
12868
12869     The keyword `this' shall not be used in a default argument of a
12870     member function.  */
12871  var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
12872				      NULL);
12873  if (var)
12874    {
12875      error ("default argument `%E' uses local variable `%D'",
12876		arg, var);
12877      return error_mark_node;
12878    }
12879
12880  /* All is well.  */
12881  return arg;
12882}
12883
12884/* Decode the list of parameter types for a function type.
12885   Given the list of things declared inside the parens,
12886   return a list of types.
12887
12888   We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
12889   flag. If unset, we append void_list_node. A parmlist declared
12890   as `(void)' is accepted as the empty parmlist.
12891
12892   Also set last_function_parms to the chain of PARM_DECLs.  */
12893
12894static tree
12895grokparms (first_parm)
12896     tree first_parm;
12897{
12898  tree result = NULL_TREE;
12899  tree decls = NULL_TREE;
12900  int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
12901  tree parm, chain;
12902  int any_error = 0;
12903
12904  my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
12905
12906  for (parm = first_parm; parm != NULL_TREE; parm = chain)
12907    {
12908      tree type = NULL_TREE;
12909      tree decl = TREE_VALUE (parm);
12910      tree init = TREE_PURPOSE (parm);
12911      tree specs, attrs;
12912
12913      chain = TREE_CHAIN (parm);
12914      /* @@ weak defense against parse errors.  */
12915      if (TREE_CODE (decl) != VOID_TYPE
12916	  && TREE_CODE (decl) != TREE_LIST)
12917	{
12918	  /* Give various messages as the need arises.  */
12919	  if (TREE_CODE (decl) == STRING_CST)
12920	    error ("invalid string constant `%E'", decl);
12921	  else if (TREE_CODE (decl) == INTEGER_CST)
12922	    error ("invalid integer constant in parameter list, did you forget to give parameter name?");
12923	  continue;
12924	}
12925
12926      if (parm == void_list_node)
12927        break;
12928
12929      split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
12930      decl = grokdeclarator (TREE_VALUE (decl), specs,
12931			     PARM, init != NULL_TREE, &attrs);
12932      if (! decl || TREE_TYPE (decl) == error_mark_node)
12933        continue;
12934
12935      if (attrs)
12936	cplus_decl_attributes (&decl, attrs, 0);
12937
12938      type = TREE_TYPE (decl);
12939      if (VOID_TYPE_P (type))
12940        {
12941          if (same_type_p (type, void_type_node)
12942              && !DECL_NAME (decl) && !result && !chain && !ellipsis)
12943            /* this is a parmlist of `(void)', which is ok.  */
12944            break;
12945          cxx_incomplete_type_error (decl, type);
12946	  /* It's not a good idea to actually create parameters of
12947	     type `void'; other parts of the compiler assume that a
12948	     void type terminates the parameter list.  */
12949	  type = error_mark_node;
12950	  TREE_TYPE (decl) = error_mark_node;
12951        }
12952
12953      if (type != error_mark_node)
12954	{
12955	  /* Top-level qualifiers on the parameters are
12956	     ignored for function types.  */
12957	  type = TYPE_MAIN_VARIANT (type);
12958	  if (TREE_CODE (type) == METHOD_TYPE)
12959	    {
12960	      error ("parameter `%D' invalidly declared method type", decl);
12961	      type = build_pointer_type (type);
12962	      TREE_TYPE (decl) = type;
12963	    }
12964	  else if (TREE_CODE (type) == OFFSET_TYPE)
12965	    {
12966	      error ("parameter `%D' invalidly declared offset type", decl);
12967	      type = build_pointer_type (type);
12968	      TREE_TYPE (decl) = type;
12969	    }
12970	  else if (abstract_virtuals_error (decl, type))
12971	    any_error = 1;  /* Seems like a good idea.  */
12972	  else if (POINTER_TYPE_P (type))
12973	    {
12974	      /* [dcl.fct]/6, parameter types cannot contain pointers
12975		 (references) to arrays of unknown bound.  */
12976	      tree t = TREE_TYPE (type);
12977	      int ptr = TYPE_PTR_P (type);
12978
12979              while (1)
12980                {
12981                  if (TYPE_PTR_P (t))
12982                    ptr = 1;
12983                  else if (TREE_CODE (t) != ARRAY_TYPE)
12984                    break;
12985                  else if (!TYPE_DOMAIN (t))
12986	            break;
12987	          t = TREE_TYPE (t);
12988	        }
12989	      if (TREE_CODE (t) == ARRAY_TYPE)
12990		error ("parameter `%D' includes %s to array of unknown bound `%T'",
12991			  decl, ptr ? "pointer" : "reference", t);
12992	    }
12993
12994	  if (!any_error && init)
12995	    init = check_default_argument (decl, init);
12996	  else
12997	    init = NULL_TREE;
12998	}
12999
13000      TREE_CHAIN (decl) = decls;
13001      decls = decl;
13002      result = tree_cons (init, type, result);
13003    }
13004  decls = nreverse (decls);
13005  result = nreverse (result);
13006  if (!ellipsis)
13007    result = chainon (result, void_list_node);
13008  last_function_parms = decls;
13009
13010  return result;
13011}
13012
13013
13014/* D is a constructor or overloaded `operator='.
13015
13016   Let T be the class in which D is declared. Then, this function
13017   returns:
13018
13019   -1 if D's is an ill-formed constructor or copy assignment operator
13020      whose first parameter is of type `T'.
13021   0  if D is not a copy constructor or copy assignment
13022      operator.
13023   1  if D is a copy constructor or copy assignment operator whose
13024      first parameter is a reference to const qualified T.
13025   2  if D is a copy constructor or copy assignment operator whose
13026      first parameter is a reference to non-const qualified T.
13027
13028   This function can be used as a predicate. Positive values indicate
13029   a copy constructor and nonzero values indicate a copy assignment
13030   operator.  */
13031
13032int
13033copy_fn_p (d)
13034     tree d;
13035{
13036  tree args;
13037  tree arg_type;
13038  int result = 1;
13039
13040  my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
13041
13042  if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
13043    /* Instantiations of template member functions are never copy
13044       functions.  Note that member functions of templated classes are
13045       represented as template functions internally, and we must
13046       accept those as copy functions.  */
13047    return 0;
13048
13049  args = FUNCTION_FIRST_USER_PARMTYPE (d);
13050  if (!args)
13051    return 0;
13052
13053  arg_type = TREE_VALUE (args);
13054
13055  if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
13056    {
13057      /* Pass by value copy assignment operator.  */
13058      result = -1;
13059    }
13060  else if (TREE_CODE (arg_type) == REFERENCE_TYPE
13061	   && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
13062    {
13063      if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
13064	result = 2;
13065    }
13066  else
13067    return 0;
13068
13069  args = TREE_CHAIN (args);
13070
13071  if (args && args != void_list_node && !TREE_PURPOSE (args))
13072    /* There are more non-optional args.  */
13073    return 0;
13074
13075  return result;
13076}
13077
13078/* Remember any special properties of member function DECL.  */
13079
13080void grok_special_member_properties (decl)
13081     tree decl;
13082{
13083  if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
13084    ; /* Not special.  */
13085  else if (DECL_CONSTRUCTOR_P (decl))
13086    {
13087      int ctor = copy_fn_p (decl);
13088
13089      if (ctor > 0)
13090	{
13091	  /* [class.copy]
13092
13093     	     A non-template constructor for class X is a copy
13094     	     constructor if its first parameter is of type X&, const
13095     	     X&, volatile X& or const volatile X&, and either there
13096     	     are no other parameters or else all other parameters have
13097     	     default arguments.  */
13098	  TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
13099	  if (ctor > 1)
13100	    TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
13101	}
13102      else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
13103	TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
13104    }
13105  else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
13106    {
13107      /* [class.copy]
13108
13109     	 A non-template assignment operator for class X is a copy
13110     	 assignment operator if its parameter is of type X, X&, const
13111     	 X&, volatile X& or const volatile X&.  */
13112
13113      int assop = copy_fn_p (decl);
13114
13115      if (assop)
13116	{
13117	  TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
13118	  if (assop != 1)
13119	    TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
13120	  if (DECL_PURE_VIRTUAL_P (decl))
13121	    TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
13122	}
13123    }
13124}
13125
13126/* Check a constructor DECL has the correct form.  Complains
13127   if the class has a constructor of the form X(X).  */
13128
13129int
13130grok_ctor_properties (ctype, decl)
13131     tree ctype, decl;
13132{
13133  int ctor_parm = copy_fn_p (decl);
13134
13135  if (ctor_parm < 0)
13136    {
13137      /* [class.copy]
13138
13139     	 A declaration of a constructor for a class X is ill-formed if
13140     	 its first parameter is of type (optionally cv-qualified) X
13141     	 and either there are no other parameters or else all other
13142     	 parameters have default arguments.
13143
13144     	 We *don't* complain about member template instantiations that
13145     	 have this form, though; they can occur as we try to decide
13146     	 what constructor to use during overload resolution.  Since
13147     	 overload resolution will never prefer such a constructor to
13148     	 the non-template copy constructor (which is either explicitly
13149     	 or implicitly defined), there's no need to worry about their
13150     	 existence.  Theoretically, they should never even be
13151     	 instantiated, but that's hard to forestall.  */
13152      error ("invalid constructor; you probably meant `%T (const %T&)'",
13153		ctype, ctype);
13154      SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
13155      return 0;
13156    }
13157
13158  return 1;
13159}
13160
13161/* An operator with this code is unary, but can also be binary.  */
13162
13163static int
13164ambi_op_p (code)
13165     enum tree_code code;
13166{
13167  return (code == INDIRECT_REF
13168	  || code == ADDR_EXPR
13169	  || code == CONVERT_EXPR
13170	  || code == NEGATE_EXPR
13171	  || code == PREINCREMENT_EXPR
13172	  || code == PREDECREMENT_EXPR);
13173}
13174
13175/* An operator with this name can only be unary.  */
13176
13177static int
13178unary_op_p (code)
13179     enum tree_code code;
13180{
13181  return (code == TRUTH_NOT_EXPR
13182	  || code == BIT_NOT_EXPR
13183	  || code == COMPONENT_REF
13184	  || code == TYPE_EXPR);
13185}
13186
13187/* Do a little sanity-checking on how they declared their operator.  */
13188
13189void
13190grok_op_properties (decl, friendp)
13191     tree decl;
13192     int friendp;
13193{
13194  tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
13195  tree argtype;
13196  int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
13197  tree name = DECL_NAME (decl);
13198  enum tree_code operator_code;
13199  int arity;
13200
13201  /* Count the number of arguments.  */
13202  for (argtype = argtypes, arity = 0;
13203       argtype && argtype != void_list_node;
13204       argtype = TREE_CHAIN (argtype))
13205    ++arity;
13206
13207  if (current_class_type == NULL_TREE)
13208    friendp = 1;
13209
13210  if (DECL_CONV_FN_P (decl))
13211    operator_code = TYPE_EXPR;
13212  else
13213    do
13214      {
13215#define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)	\
13216	if (ansi_opname (CODE) == name)				\
13217	  {							\
13218	    operator_code = (CODE);				\
13219	    break;						\
13220	  }							\
13221	else if (ansi_assopname (CODE) == name)			\
13222	  {							\
13223	    operator_code = (CODE);				\
13224	    DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;		\
13225	    break;						\
13226	  }
13227
13228#include "operators.def"
13229#undef DEF_OPERATOR
13230
13231	abort ();
13232      }
13233    while (0);
13234  my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
13235  SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13236
13237  if (! friendp)
13238    {
13239      switch (operator_code)
13240	{
13241	case CALL_EXPR:
13242	  TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
13243	  break;
13244
13245	case ARRAY_REF:
13246	  TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
13247	  break;
13248
13249	case COMPONENT_REF:
13250	case MEMBER_REF:
13251	  TYPE_OVERLOADS_ARROW (current_class_type) = 1;
13252	  break;
13253
13254	case NEW_EXPR:
13255	  TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
13256	  break;
13257
13258	case DELETE_EXPR:
13259	  TYPE_GETS_DELETE (current_class_type) |= 1;
13260	  break;
13261
13262	case VEC_NEW_EXPR:
13263	  TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
13264	  break;
13265
13266	case VEC_DELETE_EXPR:
13267	  TYPE_GETS_DELETE (current_class_type) |= 2;
13268	  break;
13269
13270	default:
13271	  break;
13272	}
13273    }
13274
13275  if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
13276    {
13277      /* When the compiler encounters the definition of A::operator new, it
13278	 doesn't look at the class declaration to find out if it's static.  */
13279      if (methodp)
13280	revert_static_member_fn (decl);
13281
13282      TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
13283    }
13284  else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
13285    {
13286      if (methodp)
13287	revert_static_member_fn (decl);
13288
13289      TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
13290    }
13291  else
13292    {
13293      /* An operator function must either be a non-static member function
13294	 or have at least one parameter of a class, a reference to a class,
13295	 an enumeration, or a reference to an enumeration.  13.4.0.6 */
13296      if (! methodp || DECL_STATIC_FUNCTION_P (decl))
13297	{
13298	  if (operator_code == TYPE_EXPR
13299	      || operator_code == CALL_EXPR
13300	      || operator_code == COMPONENT_REF
13301	      || operator_code == ARRAY_REF
13302	      || operator_code == NOP_EXPR)
13303	    error ("`%D' must be a nonstatic member function", decl);
13304	  else
13305	    {
13306	      tree p = argtypes;
13307
13308	      if (DECL_STATIC_FUNCTION_P (decl))
13309		error ("`%D' must be either a non-static member function or a non-member function", decl);
13310
13311	      if (p)
13312		for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
13313		  {
13314		    tree arg = TREE_VALUE (p);
13315		    if (TREE_CODE (arg) == REFERENCE_TYPE)
13316		      arg = TREE_TYPE (arg);
13317
13318		    /* This lets bad template code slip through.  */
13319		    if (IS_AGGR_TYPE (arg)
13320			|| TREE_CODE (arg) == ENUMERAL_TYPE
13321			|| TREE_CODE (arg) == TEMPLATE_TYPE_PARM
13322			|| TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
13323		      goto foundaggr;
13324		  }
13325	      error
13326		("`%D' must have an argument of class or enumerated type",
13327		 decl);
13328	    foundaggr:
13329	      ;
13330	    }
13331	}
13332
13333      if (operator_code == CALL_EXPR)
13334	return;			/* No restrictions on args.  */
13335
13336      if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
13337	{
13338	  tree t = TREE_TYPE (name);
13339	  if (! friendp)
13340	    {
13341	      int ref = (TREE_CODE (t) == REFERENCE_TYPE);
13342	      const char *what = 0;
13343
13344	      if (ref)
13345		t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
13346
13347	      if (TREE_CODE (t) == VOID_TYPE)
13348	        what = "void";
13349	      else if (t == current_class_type)
13350		what = "the same type";
13351	      /* Don't force t to be complete here.  */
13352	      else if (IS_AGGR_TYPE (t)
13353		       && COMPLETE_TYPE_P (t)
13354		       && DERIVED_FROM_P (t, current_class_type))
13355		what = "a base class";
13356
13357	      if (what && warn_conversion)
13358		warning ("conversion to %s%s will never use a type conversion operator",
13359			 ref ? "a reference to " : "", what);
13360	    }
13361	}
13362      if (operator_code == COND_EXPR)
13363	{
13364	  /* 13.4.0.3 */
13365	  error ("ISO C++ prohibits overloading operator ?:");
13366	}
13367      else if (ambi_op_p (operator_code))
13368	{
13369	  if (arity == 1)
13370	    /* We pick the one-argument operator codes by default, so
13371	       we don't have to change anything.  */
13372	    ;
13373	  else if (arity == 2)
13374	    {
13375	      /* If we thought this was a unary operator, we now know
13376		 it to be a binary operator.  */
13377	      switch (operator_code)
13378		{
13379		case INDIRECT_REF:
13380		  operator_code = MULT_EXPR;
13381		  break;
13382
13383		case ADDR_EXPR:
13384		  operator_code = BIT_AND_EXPR;
13385		  break;
13386
13387		case CONVERT_EXPR:
13388		  operator_code = PLUS_EXPR;
13389		  break;
13390
13391		case NEGATE_EXPR:
13392		  operator_code = MINUS_EXPR;
13393		  break;
13394
13395		case PREINCREMENT_EXPR:
13396		  operator_code = POSTINCREMENT_EXPR;
13397		  break;
13398
13399		case PREDECREMENT_EXPR:
13400		  operator_code = POSTDECREMENT_EXPR;
13401		  break;
13402
13403		default:
13404		  abort ();
13405		}
13406
13407	      SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
13408
13409	      if ((operator_code == POSTINCREMENT_EXPR
13410		   || operator_code == POSTDECREMENT_EXPR)
13411		  && ! processing_template_decl
13412		  && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
13413		{
13414		  if (methodp)
13415		    error ("postfix `%D' must take `int' as its argument",
13416			      decl);
13417		  else
13418		    error
13419		      ("postfix `%D' must take `int' as its second argument",
13420		       decl);
13421		}
13422	    }
13423	  else
13424	    {
13425	      if (methodp)
13426		error ("`%D' must take either zero or one argument", decl);
13427	      else
13428		error ("`%D' must take either one or two arguments", decl);
13429	    }
13430
13431	  /* More Effective C++ rule 6.  */
13432	  if (warn_ecpp
13433	      && (operator_code == POSTINCREMENT_EXPR
13434		  || operator_code == POSTDECREMENT_EXPR
13435		  || operator_code == PREINCREMENT_EXPR
13436		  || operator_code == PREDECREMENT_EXPR))
13437	    {
13438	      tree arg = TREE_VALUE (argtypes);
13439	      tree ret = TREE_TYPE (TREE_TYPE (decl));
13440	      if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
13441		arg = TREE_TYPE (arg);
13442	      arg = TYPE_MAIN_VARIANT (arg);
13443	      if (operator_code == PREINCREMENT_EXPR
13444		  || operator_code == PREDECREMENT_EXPR)
13445		{
13446		  if (TREE_CODE (ret) != REFERENCE_TYPE
13447		      || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
13448				       arg))
13449		    warning ("prefix `%D' should return `%T'", decl,
13450				build_reference_type (arg));
13451		}
13452	      else
13453		{
13454		  if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
13455		    warning ("postfix `%D' should return `%T'", decl, arg);
13456		}
13457	    }
13458	}
13459      else if (unary_op_p (operator_code))
13460	{
13461	  if (arity != 1)
13462	    {
13463	      if (methodp)
13464		error ("`%D' must take `void'", decl);
13465	      else
13466		error ("`%D' must take exactly one argument", decl);
13467	    }
13468	}
13469      else /* if (binary_op_p (operator_code)) */
13470	{
13471	  if (arity != 2)
13472	    {
13473	      if (methodp)
13474		error ("`%D' must take exactly one argument", decl);
13475	      else
13476		error ("`%D' must take exactly two arguments", decl);
13477	    }
13478
13479	  /* More Effective C++ rule 7.  */
13480	  if (warn_ecpp
13481	      && (operator_code == TRUTH_ANDIF_EXPR
13482		  || operator_code == TRUTH_ORIF_EXPR
13483		  || operator_code == COMPOUND_EXPR))
13484	    warning ("user-defined `%D' always evaluates both arguments",
13485			decl);
13486	}
13487
13488      /* Effective C++ rule 23.  */
13489      if (warn_ecpp
13490	  && arity == 2
13491	  && !DECL_ASSIGNMENT_OPERATOR_P (decl)
13492	  && (operator_code == PLUS_EXPR
13493	      || operator_code == MINUS_EXPR
13494	      || operator_code == TRUNC_DIV_EXPR
13495	      || operator_code == MULT_EXPR
13496	      || operator_code == TRUNC_MOD_EXPR)
13497	  && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
13498	warning ("`%D' should return by value", decl);
13499
13500      /* [over.oper]/8 */
13501      for (; argtypes && argtypes != void_list_node;
13502          argtypes = TREE_CHAIN (argtypes))
13503        if (TREE_PURPOSE (argtypes))
13504          {
13505            TREE_PURPOSE (argtypes) = NULL_TREE;
13506            if (operator_code == POSTINCREMENT_EXPR
13507		|| operator_code == POSTDECREMENT_EXPR)
13508              {
13509                if (pedantic)
13510                  pedwarn ("`%D' cannot have default arguments", decl);
13511              }
13512            else
13513              error ("`%D' cannot have default arguments", decl);
13514          }
13515
13516    }
13517}
13518
13519static const char *
13520tag_name (code)
13521     enum tag_types code;
13522{
13523  switch (code)
13524    {
13525    case record_type:
13526      return "struct";
13527    case class_type:
13528      return "class";
13529    case union_type:
13530      return "union ";
13531    case enum_type:
13532      return "enum";
13533    default:
13534      abort ();
13535    }
13536}
13537
13538/* Name lookup in an elaborated-type-specifier (after the keyword
13539   indicated by TAG_CODE) has found TYPE.  If the
13540   elaborated-type-specifier is invalid, issue a diagnostic and return
13541   error_mark_node; otherwise, return TYPE itself.  */
13542
13543static tree
13544check_elaborated_type_specifier (enum tag_types tag_code,
13545				 tree type)
13546{
13547  tree t;
13548
13549  t = follow_tag_typedef (type);
13550
13551  /* [dcl.type.elab] If the identifier resolves to a typedef-name or a
13552     template type-parameter, the elaborated-type-specifier is
13553     ill-formed.  */
13554  if (!t)
13555    {
13556      error ("using typedef-name `%D' after `%s'",
13557	     TYPE_NAME (type), tag_name (tag_code));
13558      t = error_mark_node;
13559    }
13560  else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
13561    {
13562      error ("using template type parameter `%T' after `%s'",
13563	     type, tag_name (tag_code));
13564      t = error_mark_node;
13565    }
13566
13567  return t;
13568}
13569
13570/* Get the struct, enum or union (CODE says which) with tag NAME.
13571   Define the tag as a forward-reference if it is not defined.
13572
13573   C++: If a class derivation is given, process it here, and report
13574   an error if multiple derivation declarations are not identical.
13575
13576   If this is a definition, come in through xref_tag and only look in
13577   the current frame for the name (since C++ allows new names in any
13578   scope.)  */
13579
13580tree
13581xref_tag (enum tag_types tag_code, tree name, tree attributes,
13582	  bool globalize)
13583{
13584  enum tree_code code;
13585  register tree ref, t;
13586  struct cp_binding_level *b = current_binding_level;
13587  tree context = NULL_TREE;
13588
13589  timevar_push (TV_NAME_LOOKUP);
13590
13591  switch (tag_code)
13592    {
13593    case record_type:
13594    case class_type:
13595      code = RECORD_TYPE;
13596      break;
13597    case union_type:
13598      code = UNION_TYPE;
13599      break;
13600    case enum_type:
13601      code = ENUMERAL_TYPE;
13602      break;
13603    default:
13604      abort ();
13605    }
13606
13607  /* If a cross reference is requested, look up the type
13608     already defined for this tag and return it.  */
13609  if (TYPE_P (name))
13610    {
13611      t = name;
13612      name = TYPE_IDENTIFIER (t);
13613    }
13614  else
13615    t = IDENTIFIER_TYPE_VALUE (name);
13616
13617  /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
13618  if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
13619    {
13620      static int explained;
13621      tree shadowed;
13622
13623      warning ("`%s %T' declares a new type at namespace scope",
13624		  tag_name (tag_code), name);
13625      if (!explained++)
13626	warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
13627		    tag_name (tag_code),
13628		    constructor_name (current_class_type),
13629		    TYPE_IDENTIFIER (t));
13630
13631      /* We need to remove the class scope binding for the
13632         TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
13633      for (shadowed = b->class_shadowed;
13634	   shadowed;
13635	   shadowed = TREE_CHAIN (shadowed))
13636	if (TREE_TYPE (shadowed) == TYPE_NAME (t))
13637	  {
13638	    TREE_PURPOSE (shadowed) = NULL_TREE;
13639	    break;
13640	  }
13641    }
13642
13643  if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
13644      && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
13645    t = NULL_TREE;
13646
13647  if (! globalize)
13648    {
13649      /* If we know we are defining this tag, only look it up in
13650	 this scope and don't try to find it as a type.  */
13651      ref = lookup_tag (code, name, b, 1);
13652    }
13653  else
13654    {
13655      if (t)
13656	{
13657	  ref = check_elaborated_type_specifier (tag_code, t);
13658	  if (ref == error_mark_node)
13659	    POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
13660	}
13661      else
13662	ref = lookup_tag (code, name, b, 0);
13663
13664      if (! ref)
13665	{
13666	  /* Try finding it as a type declaration.  If that wins,
13667	     use it.  */
13668	  ref = lookup_name (name, 1);
13669
13670	  if (ref != NULL_TREE
13671	      && processing_template_decl
13672	      && DECL_CLASS_TEMPLATE_P (ref)
13673	      && template_class_depth (current_class_type) == 0)
13674	    /* Since GLOBALIZE is true, we're declaring a global
13675	       template, so we want this type.  */
13676	    ref = DECL_TEMPLATE_RESULT (ref);
13677
13678	  if (ref && TREE_CODE (ref) == TYPE_DECL)
13679	    {
13680	      ref = check_elaborated_type_specifier (tag_code,
13681						     TREE_TYPE (ref));
13682	      if (ref == error_mark_node)
13683		POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
13684	      if (ref && TREE_CODE (ref) != code)
13685		ref = NULL_TREE;
13686	    }
13687	  else
13688	    ref = NULL_TREE;
13689	}
13690
13691      if (ref && current_class_type
13692	  && template_class_depth (current_class_type)
13693	  && PROCESSING_REAL_TEMPLATE_DECL_P ())
13694	{
13695	  /* Since GLOBALIZE is nonzero, we are not looking at a
13696	     definition of this tag.  Since, in addition, we are currently
13697	     processing a (member) template declaration of a template
13698	     class, we must be very careful; consider:
13699
13700	       template <class X>
13701	       struct S1
13702
13703	       template <class U>
13704	       struct S2
13705	       { template <class V>
13706	       friend struct S1; };
13707
13708	     Here, the S2::S1 declaration should not be confused with the
13709	     outer declaration.  In particular, the inner version should
13710	     have a template parameter of level 2, not level 1.  This
13711	     would be particularly important if the member declaration
13712	     were instead:
13713
13714	       template <class V = U> friend struct S1;
13715
13716	     say, when we should tsubst into `U' when instantiating
13717	     S2.  On the other hand, when presented with:
13718
13719	         template <class T>
13720	         struct S1 {
13721		   template <class U>
13722	           struct S2 {};
13723		   template <class U>
13724		   friend struct S2;
13725		 };
13726
13727              we must find the inner binding eventually.  We
13728	      accomplish this by making sure that the new type we
13729	      create to represent this declaration has the right
13730	      TYPE_CONTEXT.  */
13731	  context = TYPE_CONTEXT (ref);
13732	  ref = NULL_TREE;
13733	}
13734    }
13735
13736  if (! ref)
13737    {
13738      /* If no such tag is yet defined, create a forward-reference node
13739	 and record it as the "definition".
13740	 When a real declaration of this type is found,
13741	 the forward-reference will be altered into a real type.  */
13742      if (code == ENUMERAL_TYPE)
13743	{
13744	  error ("use of enum `%#D' without previous declaration", name);
13745          POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
13746	}
13747      else
13748	{
13749	  struct cp_binding_level *old_b = class_binding_level;
13750
13751	  ref = make_aggr_type (code);
13752	  TYPE_CONTEXT (ref) = context;
13753
13754#ifdef NONNESTED_CLASSES
13755	  /* Class types don't nest the way enums do.  */
13756	  class_binding_level = (struct cp_binding_level *)0;
13757#endif
13758	  pushtag (name, ref, globalize);
13759	  class_binding_level = old_b;
13760	}
13761    }
13762  else
13763    {
13764      if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
13765	redeclare_class_template (ref, current_template_parms);
13766    }
13767
13768  TYPE_ATTRIBUTES (ref) = attributes;
13769
13770  POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
13771}
13772
13773tree
13774xref_tag_from_type (old, id, globalize)
13775     tree old, id;
13776     int globalize;
13777{
13778  enum tag_types tag_kind;
13779
13780  if (TREE_CODE (old) == RECORD_TYPE)
13781    tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
13782  else
13783    tag_kind  = union_type;
13784
13785  if (id == NULL_TREE)
13786    id = TYPE_IDENTIFIER (old);
13787
13788  return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
13789}
13790
13791/* REF is a type (named NAME), for which we have just seen some
13792   baseclasses.  BINFO is a list of those baseclasses; the
13793   TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
13794   the base-class.  CODE_TYPE_NODE indicates whether REF is a class,
13795   struct, or union.  */
13796
13797void
13798xref_basetypes (ref, binfo)
13799     tree ref;
13800     tree binfo;
13801{
13802  /* In the declaration `A : X, Y, ... Z' we mark all the types
13803     (A, X, Y, ..., Z) so we can check for duplicates.  */
13804  tree binfos;
13805  tree base;
13806
13807  int i, len;
13808  enum tag_types tag_code;
13809
13810  if (TREE_CODE (ref) == UNION_TYPE)
13811    {
13812      error ("derived union `%T' invalid", ref);
13813      return;
13814    }
13815
13816  tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
13817
13818  len = list_length (binfo);
13819
13820  /* First, make sure that any templates in base-classes are
13821     instantiated.  This ensures that if we call ourselves recursively
13822     we do not get confused about which classes are marked and which
13823     are not.  */
13824  for (base = binfo; base; base = TREE_CHAIN (base))
13825    complete_type (TREE_VALUE (base));
13826
13827  SET_CLASSTYPE_MARKED (ref);
13828  BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos = make_tree_vec (len);
13829
13830  for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
13831    {
13832      /* The base of a derived struct is public by default.  */
13833      int via_public
13834	= (TREE_PURPOSE (binfo) == access_public_node
13835	   || TREE_PURPOSE (binfo) == access_public_virtual_node
13836	   || (tag_code != class_type
13837	       && (TREE_PURPOSE (binfo) == access_default_node
13838		   || TREE_PURPOSE (binfo) == access_default_virtual_node)));
13839      int via_protected
13840	= (TREE_PURPOSE (binfo) == access_protected_node
13841	   || TREE_PURPOSE (binfo) == access_protected_virtual_node);
13842      int via_virtual
13843	= (TREE_PURPOSE (binfo) == access_private_virtual_node
13844	   || TREE_PURPOSE (binfo) == access_protected_virtual_node
13845	   || TREE_PURPOSE (binfo) == access_public_virtual_node
13846	   || TREE_PURPOSE (binfo) == access_default_virtual_node);
13847      tree basetype = TREE_VALUE (binfo);
13848      tree base_binfo;
13849
13850      if (basetype && TREE_CODE (basetype) == TYPE_DECL)
13851	basetype = TREE_TYPE (basetype);
13852      if (!basetype
13853	  || (TREE_CODE (basetype) != RECORD_TYPE
13854	      && TREE_CODE (basetype) != TYPENAME_TYPE
13855	      && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
13856	      && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
13857	{
13858	  error ("base type `%T' fails to be a struct or class type",
13859		    TREE_VALUE (binfo));
13860	  continue;
13861	}
13862
13863      /* This code replaces similar code in layout_basetypes.
13864         We put the complete_type first for implicit `typename'.  */
13865      if (!COMPLETE_TYPE_P (basetype)
13866	  && ! (current_template_parms && uses_template_parms (basetype)))
13867	{
13868	  error ("base class `%T' has incomplete type", basetype);
13869	  continue;
13870	}
13871      else
13872	{
13873	  if (CLASSTYPE_MARKED (basetype))
13874	    {
13875	      if (basetype == ref)
13876		error ("recursive type `%T' undefined", basetype);
13877	      else
13878		error ("duplicate base type `%T' invalid", basetype);
13879	      continue;
13880	    }
13881
13882	  if (TYPE_FOR_JAVA (basetype)
13883	      && (current_lang_depth () == 0))
13884	    TYPE_FOR_JAVA (ref) = 1;
13885
13886	  /* Note that the BINFO records which describe individual
13887	     inheritances are *not* shared in the lattice!  They
13888	     cannot be shared because a given baseclass may be
13889	     inherited with different `accessibility' by different
13890	     derived classes.  (Each BINFO record describing an
13891	     individual inheritance contains flags which say what
13892	     the `accessibility' of that particular inheritance is.)  */
13893
13894	  base_binfo
13895	    = make_binfo (size_zero_node, basetype,
13896			  CLASS_TYPE_P (basetype)
13897			  ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
13898			  CLASS_TYPE_P (basetype)
13899			  ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
13900
13901	  TREE_VEC_ELT (binfos, i) = base_binfo;
13902	  TREE_VIA_PUBLIC (base_binfo) = via_public;
13903	  TREE_VIA_PROTECTED (base_binfo) = via_protected;
13904	  TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
13905	  BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
13906
13907	  /* We need to unshare the binfos now so that lookups during class
13908	     definition work.  */
13909	  unshare_base_binfos (base_binfo);
13910
13911	  SET_CLASSTYPE_MARKED (basetype);
13912
13913	  /* We are free to modify these bits because they are meaningless
13914	     at top level, and BASETYPE is a top-level type.  */
13915	  if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
13916	    {
13917	      TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
13918	      /* Converting to a virtual base class requires looking
13919		 up the offset of the virtual base.  */
13920	      TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13921	    }
13922
13923	  if (CLASS_TYPE_P (basetype))
13924	    {
13925	      TYPE_HAS_NEW_OPERATOR (ref)
13926		|= TYPE_HAS_NEW_OPERATOR (basetype);
13927	      TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
13928		|= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
13929	      TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
13930	      /* If the base-class uses multiple inheritance, so do we.  */
13931	      TYPE_USES_MULTIPLE_INHERITANCE (ref)
13932		|= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
13933	      /* Likewise, if converting to a base of the base may require
13934		 code, then we may need to generate code to convert to a
13935		 base as well.  */
13936	      TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
13937		|= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
13938	    }
13939
13940	  i += 1;
13941	}
13942    }
13943  if (i)
13944    TREE_VEC_LENGTH (binfos) = i;
13945  else
13946    BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
13947
13948  if (i > 1)
13949    {
13950      TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
13951      /* If there is more than one non-empty they cannot be at the same
13952	 address.  */
13953      TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13954    }
13955
13956  /* Unmark all the types.  */
13957  while (--i >= 0)
13958    CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
13959  CLEAR_CLASSTYPE_MARKED (ref);
13960
13961  /* Now that we know all the base-classes, set up the list of virtual
13962     bases.  */
13963  get_vbase_types (ref);
13964}
13965
13966
13967/* Begin compiling the definition of an enumeration type.
13968   NAME is its name (or null if anonymous).
13969   Returns the type object, as yet incomplete.
13970   Also records info about it so that build_enumerator
13971   may be used to declare the individual values as they are read.  */
13972
13973tree
13974start_enum (name)
13975     tree name;
13976{
13977  register tree enumtype = NULL_TREE;
13978  struct cp_binding_level *b = current_binding_level;
13979
13980  /* If this is the real definition for a previous forward reference,
13981     fill in the contents in the same object that used to be the
13982     forward reference.  */
13983
13984  if (name != NULL_TREE)
13985    enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13986
13987  if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13988    {
13989      error ("multiple definition of `%#T'", enumtype);
13990      cp_error_at ("previous definition here", enumtype);
13991      /* Clear out TYPE_VALUES, and start again.  */
13992      TYPE_VALUES (enumtype) = NULL_TREE;
13993    }
13994  else
13995    {
13996      enumtype = make_node (ENUMERAL_TYPE);
13997      pushtag (name, enumtype, 0);
13998    }
13999
14000  return enumtype;
14001}
14002
14003/* After processing and defining all the values of an enumeration type,
14004   install their decls in the enumeration type and finish it off.
14005   ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
14006
14007void
14008finish_enum (enumtype)
14009     tree enumtype;
14010{
14011  tree pair;
14012  tree minnode;
14013  tree maxnode;
14014  tree t;
14015  bool unsignedp;
14016  int lowprec;
14017  int highprec;
14018  int precision;
14019
14020  /* We built up the VALUES in reverse order.  */
14021  TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
14022
14023  /* For an enum defined in a template, just set the type of the values;
14024     all further processing is postponed until the template is
14025     instantiated.  We need to set the type so that tsubst of a CONST_DECL
14026     works.  */
14027  if (processing_template_decl)
14028    {
14029      for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
14030	TREE_TYPE (TREE_VALUE (pair)) = enumtype;
14031      if (at_function_scope_p ())
14032	add_stmt (build_min (TAG_DEFN, enumtype));
14033      return;
14034    }
14035
14036  if (TYPE_VALUES (enumtype))
14037    {
14038      minnode = maxnode = NULL_TREE;
14039
14040      for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
14041	{
14042	  tree decl = TREE_VALUE (pair);
14043	  tree value = DECL_INITIAL (decl);
14044
14045	  /* [dcl.enum]: Following the closing brace of an enum-specifier,
14046	     each enumerator has the type of its enumeration.  Prior to the
14047	     closing brace, the type of each enumerator is the type of its
14048	     initializing value.  */
14049	  TREE_TYPE (decl) = enumtype;
14050
14051	  /* Figure out what the minimum and maximum values of the
14052	     enumerators are.  */
14053	  if (!minnode)
14054	    minnode = maxnode = value;
14055	  else if (tree_int_cst_lt (maxnode, value))
14056	    maxnode = value;
14057	  else if (tree_int_cst_lt (value, minnode))
14058	    minnode = value;
14059
14060	  /* Set the TREE_TYPE for the values as well.  That's so that when
14061	     we call decl_constant_value we get an entity of the right type
14062	     (but with the constant value).  But first make a copy so we
14063	     don't clobber shared INTEGER_CSTs.  */
14064	  if (TREE_TYPE (value) != enumtype)
14065	    {
14066	      value = DECL_INITIAL (decl) = copy_node (value);
14067	      TREE_TYPE (value) = enumtype;
14068	    }
14069
14070	  /* In addition, transform the TYPE_VALUES list to contain the
14071	     values, rather than the CONST_DECLs for them.  */
14072	  TREE_VALUE (pair) = value;
14073	}
14074    }
14075  else
14076    minnode = maxnode = integer_zero_node;
14077
14078  /* Compute the number of bits require to represent all values of the
14079     enumeration.  We must do this before the type of MINNODE and
14080     MAXNODE are transformed, since min_precision relies on the
14081     TREE_TYPE of the value it is passed.  */
14082  unsignedp = tree_int_cst_sgn (minnode) >= 0;
14083  lowprec = min_precision (minnode, unsignedp);
14084  highprec = min_precision (maxnode, unsignedp);
14085  precision = MAX (lowprec, highprec);
14086
14087  /* DR 377
14088
14089     IF no integral type can represent all the enumerator values, the
14090     enumeration is ill-formed.  */
14091  if (precision > TYPE_PRECISION (long_long_integer_type_node))
14092    {
14093      error ("no integral type can represent all of the enumerator values "
14094	     "for `%T'", enumtype);
14095      precision = TYPE_PRECISION (long_long_integer_type_node);
14096    }
14097
14098  /* Compute the minium and maximum values for the type, the size of
14099     the type, and so forth.  */
14100  TYPE_PRECISION (enumtype) = precision;
14101  TYPE_SIZE (enumtype) = NULL_TREE;
14102  if (unsignedp)
14103    fixup_unsigned_type (enumtype);
14104  else
14105    fixup_signed_type (enumtype);
14106
14107  if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
14108    /* Use the width of the narrowest normal C type which is wide
14109       enough.  */
14110    TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
14111						(precision, 1));
14112  else
14113    TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
14114
14115  TYPE_SIZE (enumtype) = NULL_TREE;
14116  layout_type (enumtype);
14117
14118  /* Fix up all variant types of this enum type.  */
14119  for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
14120    {
14121      TYPE_VALUES (t) = TYPE_VALUES (enumtype);
14122      TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
14123      TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
14124      TYPE_SIZE (t) = TYPE_SIZE (enumtype);
14125      TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
14126      TYPE_MODE (t) = TYPE_MODE (enumtype);
14127      TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
14128      TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
14129      TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
14130      TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
14131    }
14132
14133  /* Finish debugging output for this type.  */
14134  rest_of_type_compilation (enumtype, namespace_bindings_p ());
14135}
14136
14137/* Build and install a CONST_DECL for an enumeration constant of the
14138   enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
14139   Assignment of sequential values by default is handled here.  */
14140
14141void
14142build_enumerator (name, value, enumtype)
14143     tree name;
14144     tree value;
14145     tree enumtype;
14146{
14147  tree decl;
14148  tree context;
14149  tree type;
14150
14151  /* Remove no-op casts from the value.  */
14152  if (value)
14153    STRIP_TYPE_NOPS (value);
14154
14155  if (! processing_template_decl)
14156    {
14157      /* Validate and default VALUE.  */
14158      if (value != NULL_TREE)
14159	{
14160	  value = decl_constant_value (value);
14161
14162	  if (TREE_CODE (value) == INTEGER_CST)
14163	    {
14164	      value = default_conversion (value);
14165	      constant_expression_warning (value);
14166	    }
14167	  else
14168	    {
14169	      error ("enumerator value for `%D' not integer constant", name);
14170	      value = NULL_TREE;
14171	    }
14172	}
14173
14174      /* Default based on previous value.  */
14175      if (value == NULL_TREE && ! processing_template_decl)
14176	{
14177	  tree prev_value;
14178
14179	  if (TYPE_VALUES (enumtype))
14180	    {
14181	      /* The next value is the previous value ...  */
14182	      prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
14183	      /* ... plus one.  */
14184	      value = cp_build_binary_op (PLUS_EXPR,
14185					  prev_value,
14186					  integer_one_node);
14187
14188	      if (tree_int_cst_lt (value, prev_value))
14189		error ("overflow in enumeration values at `%D'", name);
14190	    }
14191	  else
14192	    value = integer_zero_node;
14193	}
14194
14195      /* Remove no-op casts from the value.  */
14196      STRIP_TYPE_NOPS (value);
14197    }
14198
14199  /* C++ associates enums with global, function, or class declarations.  */
14200  context = current_scope ();
14201
14202  /* Build the actual enumeration constant.  Note that the enumeration
14203    constants have the type of their initializers until the
14204    enumeration is complete:
14205
14206      [ dcl.enum ]
14207
14208      Following the closing brace of an enum-specifier, each enumer-
14209      ator has the type of its enumeration.  Prior to the closing
14210      brace, the type of each enumerator is the type of its
14211      initializing value.
14212
14213    In finish_enum we will reset the type.  Of course, if we're
14214    processing a template, there may be no value.  */
14215  type = value ? TREE_TYPE (value) : NULL_TREE;
14216
14217  if (context && context == current_class_type)
14218    /* This enum declaration is local to the class.  We need the full
14219       lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
14220    decl = build_lang_decl (CONST_DECL, name, type);
14221  else
14222    /* It's a global enum, or it's local to a function.  (Note local to
14223      a function could mean local to a class method.  */
14224    decl = build_decl (CONST_DECL, name, type);
14225
14226  DECL_INITIAL (decl) = value;
14227  TREE_READONLY (decl) = 1;
14228
14229  if (context && context == current_class_type)
14230    /* In something like `struct S { enum E { i = 7 }; };' we put `i'
14231      on the TYPE_FIELDS list for `S'.  (That's so that you can say
14232      things like `S::i' later.)  */
14233    finish_member_declaration (decl);
14234  else
14235    {
14236      pushdecl (decl);
14237      /* Contrary to finish_member_declaration, pushdecl does not properly
14238         set the DECL_CONTEXT.  Do that now here.  Doing that before calling
14239         pushdecl will confuse the logic used in that function.  Hopefully,
14240         future versions will implement a more straight logic.  */
14241      DECL_CONTEXT (decl) = FROB_CONTEXT (context);
14242    }
14243
14244  /* Add this enumeration constant to the list for this type.  */
14245  TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
14246}
14247
14248
14249/* We're defining DECL.  Make sure that it's type is OK.  */
14250
14251static void
14252check_function_type (decl, current_function_parms)
14253     tree decl;
14254     tree current_function_parms;
14255{
14256  tree fntype = TREE_TYPE (decl);
14257  tree return_type = complete_type (TREE_TYPE (fntype));
14258
14259  /* In a function definition, arg types must be complete.  */
14260  require_complete_types_for_parms (current_function_parms);
14261
14262  if (!COMPLETE_OR_VOID_TYPE_P (return_type))
14263    {
14264      error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
14265
14266      /* Make it return void instead, but don't change the
14267	 type of the DECL_RESULT, in case we have a named return value.  */
14268      if (TREE_CODE (fntype) == METHOD_TYPE)
14269	{
14270	  tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
14271	  TREE_TYPE (decl)
14272	    = build_cplus_method_type (ctype,
14273				       void_type_node,
14274				       FUNCTION_ARG_CHAIN (decl));
14275	}
14276      else
14277	TREE_TYPE (decl)
14278	  = build_function_type (void_type_node,
14279				 TYPE_ARG_TYPES (TREE_TYPE (decl)));
14280      TREE_TYPE (decl)
14281	= build_exception_variant (fntype,
14282				   TYPE_RAISES_EXCEPTIONS (fntype));
14283    }
14284  else
14285    abstract_virtuals_error (decl, TREE_TYPE (fntype));
14286}
14287
14288/* Create the FUNCTION_DECL for a function definition.
14289   DECLSPECS and DECLARATOR are the parts of the declaration;
14290   they describe the function's name and the type it returns,
14291   but twisted together in a fashion that parallels the syntax of C.
14292
14293   FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
14294   DECLARATOR is really the DECL for the function we are about to
14295   process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
14296   indicating that the function is an inline defined in-class.
14297
14298   This function creates a binding context for the function body
14299   as well as setting up the FUNCTION_DECL in current_function_decl.
14300
14301   Returns 1 on success.  If the DECLARATOR is not suitable for a function
14302   (it defines a datum instead), we return 0, which tells
14303   yyparse to report a parse error.
14304
14305   For C++, we must first check whether that datum makes any sense.
14306   For example, "class A local_a(1,2);" means that variable local_a
14307   is an aggregate of type A, which should have a constructor
14308   applied to it with the argument list [1, 2].  */
14309
14310int
14311start_function (declspecs, declarator, attrs, flags)
14312     tree declspecs, declarator, attrs;
14313     int flags;
14314{
14315  tree decl1;
14316  tree ctype = NULL_TREE;
14317  tree fntype;
14318  tree restype;
14319  int doing_friend = 0;
14320  struct cp_binding_level *bl;
14321  tree current_function_parms;
14322
14323  /* Sanity check.  */
14324  my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
14325  my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
14326
14327  /* This should only be done once on the top most decl.  */
14328  if (have_extern_spec)
14329    {
14330      declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
14331      have_extern_spec = false;
14332    }
14333
14334  if (flags & SF_PRE_PARSED)
14335    {
14336      decl1 = declarator;
14337
14338      fntype = TREE_TYPE (decl1);
14339      if (TREE_CODE (fntype) == METHOD_TYPE)
14340	ctype = TYPE_METHOD_BASETYPE (fntype);
14341
14342      /* ISO C++ 11.4/5.  A friend function defined in a class is in
14343	 the (lexical) scope of the class in which it is defined.  */
14344      if (!ctype && DECL_FRIEND_P (decl1))
14345	{
14346	  ctype = DECL_FRIEND_CONTEXT (decl1);
14347
14348	  /* CTYPE could be null here if we're dealing with a template;
14349	     for example, `inline friend float foo()' inside a template
14350	     will have no CTYPE set.  */
14351	  if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
14352	    ctype = NULL_TREE;
14353	  else
14354	    doing_friend = 1;
14355	}
14356
14357      last_function_parms = DECL_ARGUMENTS (decl1);
14358    }
14359  else
14360    {
14361      decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, &attrs);
14362      /* If the declarator is not suitable for a function definition,
14363	 cause a syntax error.  */
14364      if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
14365	return 0;
14366
14367      cplus_decl_attributes (&decl1, attrs, 0);
14368
14369      /* If #pragma weak was used, mark the decl weak now.  */
14370      if (global_scope_p (current_binding_level))
14371	maybe_apply_pragma_weak (decl1);
14372
14373      fntype = TREE_TYPE (decl1);
14374
14375      restype = TREE_TYPE (fntype);
14376      if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
14377	{
14378	  error ("semicolon missing after declaration of `%#T'", restype);
14379	  shadow_tag (build_tree_list (NULL_TREE, restype));
14380	  CLASSTYPE_GOT_SEMICOLON (restype) = 1;
14381	  if (TREE_CODE (fntype) == FUNCTION_TYPE)
14382	    fntype = build_function_type (integer_type_node,
14383					  TYPE_ARG_TYPES (fntype));
14384	  else
14385	    fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
14386					      integer_type_node,
14387					      TYPE_ARG_TYPES (fntype));
14388	  TREE_TYPE (decl1) = fntype;
14389	}
14390
14391      if (TREE_CODE (fntype) == METHOD_TYPE)
14392	ctype = TYPE_METHOD_BASETYPE (fntype);
14393      else if (DECL_MAIN_P (decl1))
14394	{
14395	  /* If this doesn't return integer_type, complain.  */
14396	  if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
14397	    {
14398	      if (pedantic || warn_return_type)
14399		pedwarn ("return type for `main' changed to `int'");
14400	      TREE_TYPE (decl1) = fntype = default_function_type;
14401	    }
14402	}
14403    }
14404
14405  if (DECL_DECLARED_INLINE_P (decl1)
14406      && lookup_attribute ("noinline", attrs))
14407    warning_with_decl (decl1,
14408		       "inline function `%s' given attribute noinline");
14409
14410  if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
14411    /* This is a constructor, we must ensure that any default args
14412       introduced by this definition are propagated to the clones
14413       now. The clones are used directly in overload resolution.  */
14414    adjust_clone_args (decl1);
14415
14416  /* Sometimes we don't notice that a function is a static member, and
14417     build a METHOD_TYPE for it.  Fix that up now.  */
14418  if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
14419      && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
14420    {
14421      revert_static_member_fn (decl1);
14422      last_function_parms = TREE_CHAIN (last_function_parms);
14423      ctype = NULL_TREE;
14424    }
14425
14426  /* Warn if function was previously implicitly declared
14427     (but not if we warned then).  */
14428  if (! warn_implicit
14429      && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
14430    cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
14431
14432  /* Set up current_class_type, and enter the scope of the class, if
14433     appropriate.  */
14434  if (ctype)
14435    push_nested_class (ctype, 1);
14436  else if (DECL_STATIC_FUNCTION_P (decl1))
14437    push_nested_class (DECL_CONTEXT (decl1), 2);
14438
14439  /* Now that we have entered the scope of the class, we must restore
14440     the bindings for any template parameters surrounding DECL1, if it
14441     is an inline member template.  (Order is important; consider the
14442     case where a template parameter has the same name as a field of
14443     the class.)  It is not until after this point that
14444     PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
14445  if (flags & SF_INCLASS_INLINE)
14446    maybe_begin_member_template_processing (decl1);
14447
14448  /* Effective C++ rule 15.  */
14449  if (warn_ecpp
14450      && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
14451      && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
14452    warning ("`operator=' should return a reference to `*this'");
14453
14454  /* Make the init_value nonzero so pushdecl knows this is not tentative.
14455     error_mark_node is replaced below (in poplevel) with the BLOCK.  */
14456  if (!DECL_INITIAL (decl1))
14457    DECL_INITIAL (decl1) = error_mark_node;
14458
14459  /* This function exists in static storage.
14460     (This does not mean `static' in the C sense!)  */
14461  TREE_STATIC (decl1) = 1;
14462
14463  /* We must call push_template_decl after current_class_type is set
14464     up.  (If we are processing inline definitions after exiting a
14465     class scope, current_class_type will be NULL_TREE until set above
14466     by push_nested_class.)  */
14467  if (processing_template_decl)
14468    decl1 = push_template_decl (decl1);
14469
14470  /* We are now in the scope of the function being defined.  */
14471  current_function_decl = decl1;
14472
14473  /* Save the parm names or decls from this function's declarator
14474     where store_parm_decls will find them.  */
14475  current_function_parms = last_function_parms;
14476
14477  /* Make sure the parameter and return types are reasonable.  When
14478     you declare a function, these types can be incomplete, but they
14479     must be complete when you define the function.  */
14480  if (! processing_template_decl)
14481    check_function_type (decl1, current_function_parms);
14482
14483  /* Build the return declaration for the function.  */
14484  restype = TREE_TYPE (fntype);
14485  /* Promote the value to int before returning it.  */
14486  if (c_promoting_integer_type_p (restype))
14487    restype = type_promotes_to (restype);
14488  if (DECL_RESULT (decl1) == NULL_TREE)
14489    {
14490      DECL_RESULT (decl1)
14491	= build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
14492      c_apply_type_quals_to_decl (cp_type_quals (restype),
14493				  DECL_RESULT (decl1));
14494    }
14495
14496  /* Initialize RTL machinery.  We cannot do this until
14497     CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
14498     even when processing a template; this is how we get
14499     CFUN set up, and our per-function variables initialized.
14500     FIXME factor out the non-RTL stuff.  */
14501  bl = current_binding_level;
14502  init_function_start (decl1, input_filename, lineno);
14503  current_binding_level = bl;
14504
14505  /* Even though we're inside a function body, we still don't want to
14506     call expand_expr to calculate the size of a variable-sized array.
14507     We haven't necessarily assigned RTL to all variables yet, so it's
14508     not safe to try to expand expressions involving them.  */
14509  immediate_size_expand = 0;
14510  cfun->x_dont_save_pending_sizes_p = 1;
14511
14512  /* Start the statement-tree, start the tree now.  */
14513  begin_stmt_tree (&DECL_SAVED_TREE (decl1));
14514
14515  /* Let the user know we're compiling this function.  */
14516  announce_function (decl1);
14517
14518  /* Record the decl so that the function name is defined.
14519     If we already have a decl for this name, and it is a FUNCTION_DECL,
14520     use the old decl.  */
14521  if (!processing_template_decl && !(flags & SF_PRE_PARSED))
14522    {
14523      /* A specialization is not used to guide overload resolution.  */
14524      if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
14525	  && ! DECL_FUNCTION_MEMBER_P (decl1))
14526	decl1 = pushdecl (decl1);
14527      else
14528	{
14529	  /* We need to set the DECL_CONTEXT.  */
14530	  if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
14531	    DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
14532	  /* And make sure we have enough default args.  */
14533	  check_default_args (decl1);
14534	}
14535      fntype = TREE_TYPE (decl1);
14536    }
14537
14538  /* Reset these in case the call to pushdecl changed them.  */
14539  current_function_decl = decl1;
14540  cfun->decl = decl1;
14541
14542  /* If we are (erroneously) defining a function that we have already
14543     defined before, wipe out what we knew before.  */
14544  if (!DECL_PENDING_INLINE_P (decl1))
14545    DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
14546
14547  if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
14548    {
14549      /* We know that this was set up by `grokclassfn'.  We do not
14550	 wait until `store_parm_decls', since evil parse errors may
14551	 never get us to that point.  Here we keep the consistency
14552	 between `current_class_type' and `current_class_ptr'.  */
14553      tree t = DECL_ARGUMENTS (decl1);
14554
14555      my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
14556			  162);
14557      my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
14558			  19990811);
14559
14560      cp_function_chain->x_current_class_ref
14561	= build_indirect_ref (t, NULL);
14562      cp_function_chain->x_current_class_ptr = t;
14563
14564      /* Constructors and destructors need to know whether they're "in
14565	 charge" of initializing virtual base classes.  */
14566      t = TREE_CHAIN (t);
14567      if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
14568	{
14569	  current_in_charge_parm = t;
14570	  t = TREE_CHAIN (t);
14571	}
14572      if (DECL_HAS_VTT_PARM_P (decl1))
14573	{
14574	  if (DECL_NAME (t) != vtt_parm_identifier)
14575	    abort ();
14576	  current_vtt_parm = t;
14577	}
14578    }
14579
14580  if (DECL_INTERFACE_KNOWN (decl1))
14581    {
14582      tree ctx = decl_function_context (decl1);
14583
14584      if (DECL_NOT_REALLY_EXTERN (decl1))
14585	DECL_EXTERNAL (decl1) = 0;
14586
14587      if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
14588	  && TREE_PUBLIC (ctx))
14589	/* This is a function in a local class in an extern inline
14590	   function.  */
14591	comdat_linkage (decl1);
14592    }
14593  /* If this function belongs to an interface, it is public.
14594     If it belongs to someone else's interface, it is also external.
14595     This only affects inlines and template instantiations.  */
14596  else if (interface_unknown == 0
14597	   && (! DECL_TEMPLATE_INSTANTIATION (decl1)
14598	       || flag_alt_external_templates))
14599    {
14600      if (DECL_DECLARED_INLINE_P (decl1)
14601	  || DECL_TEMPLATE_INSTANTIATION (decl1)
14602	  || processing_template_decl)
14603	{
14604	  DECL_EXTERNAL (decl1)
14605	    = (interface_only
14606	       || (DECL_DECLARED_INLINE_P (decl1)
14607		   && ! flag_implement_inlines
14608		   && !DECL_VINDEX (decl1)));
14609
14610	  /* For WIN32 we also want to put these in linkonce sections.  */
14611	  maybe_make_one_only (decl1);
14612	}
14613      else
14614	DECL_EXTERNAL (decl1) = 0;
14615      DECL_NOT_REALLY_EXTERN (decl1) = 0;
14616      DECL_INTERFACE_KNOWN (decl1) = 1;
14617    }
14618  else if (interface_unknown && interface_only
14619	   && (! DECL_TEMPLATE_INSTANTIATION (decl1)
14620	       || flag_alt_external_templates))
14621    {
14622      /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
14623	 interface, we will have interface_only set but not
14624	 interface_known.  In that case, we don't want to use the normal
14625	 heuristics because someone will supply a #pragma implementation
14626	 elsewhere, and deducing it here would produce a conflict.  */
14627      comdat_linkage (decl1);
14628      DECL_EXTERNAL (decl1) = 0;
14629      DECL_INTERFACE_KNOWN (decl1) = 1;
14630      DECL_DEFER_OUTPUT (decl1) = 1;
14631    }
14632  else
14633    {
14634      /* This is a definition, not a reference.
14635	 So clear DECL_EXTERNAL.  */
14636      DECL_EXTERNAL (decl1) = 0;
14637
14638      if ((DECL_DECLARED_INLINE_P (decl1)
14639	   || DECL_TEMPLATE_INSTANTIATION (decl1))
14640	  && ! DECL_INTERFACE_KNOWN (decl1)
14641	  /* Don't try to defer nested functions for now.  */
14642	  && ! decl_function_context (decl1))
14643	DECL_DEFER_OUTPUT (decl1) = 1;
14644      else
14645	DECL_INTERFACE_KNOWN (decl1) = 1;
14646    }
14647
14648  pushlevel (0);
14649  current_binding_level->parm_flag = 1;
14650
14651  ++function_depth;
14652
14653  if (DECL_DESTRUCTOR_P (decl1))
14654    {
14655      dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
14656      DECL_CONTEXT (dtor_label) = current_function_decl;
14657    }
14658
14659  start_fname_decls ();
14660
14661  store_parm_decls (current_function_parms);
14662
14663  return 1;
14664}
14665
14666/* Store the parameter declarations into the current function declaration.
14667   This is called after parsing the parameter declarations, before
14668   digesting the body of the function.
14669
14670   Also install to binding contour return value identifier, if any.  */
14671
14672static void
14673store_parm_decls (current_function_parms)
14674     tree current_function_parms;
14675{
14676  register tree fndecl = current_function_decl;
14677  register tree parm;
14678
14679  /* This is a chain of any other decls that came in among the parm
14680     declarations.  If a parm is declared with  enum {foo, bar} x;
14681     then CONST_DECLs for foo and bar are put here.  */
14682  tree nonparms = NULL_TREE;
14683
14684  if (current_function_parms)
14685    {
14686      /* This case is when the function was defined with an ANSI prototype.
14687	 The parms already have decls, so we need not do anything here
14688	 except record them as in effect
14689	 and complain if any redundant old-style parm decls were written.  */
14690
14691      tree specparms = current_function_parms;
14692      tree next;
14693
14694      /* Must clear this because it might contain TYPE_DECLs declared
14695	     at class level.  */
14696      storedecls (NULL_TREE);
14697
14698      /* If we're doing semantic analysis, then we'll call pushdecl
14699	     for each of these.  We must do them in reverse order so that
14700	     they end in the correct forward order.  */
14701      specparms = nreverse (specparms);
14702
14703      for (parm = specparms; parm; parm = next)
14704	{
14705	  next = TREE_CHAIN (parm);
14706	  if (TREE_CODE (parm) == PARM_DECL)
14707	    {
14708	      if (DECL_NAME (parm) == NULL_TREE
14709		  || TREE_CODE (parm) != VOID_TYPE)
14710		pushdecl (parm);
14711	      else
14712		error ("parameter `%D' declared void", parm);
14713	    }
14714	  else
14715	    {
14716	      /* If we find an enum constant or a type tag,
14717		 put it aside for the moment.  */
14718	      TREE_CHAIN (parm) = NULL_TREE;
14719	      nonparms = chainon (nonparms, parm);
14720	    }
14721	}
14722
14723      /* Get the decls in their original chain order and record in the
14724	 function.  This is all and only the PARM_DECLs that were
14725	 pushed into scope by the loop above.  */
14726      DECL_ARGUMENTS (fndecl) = getdecls ();
14727    }
14728  else
14729    DECL_ARGUMENTS (fndecl) = NULL_TREE;
14730
14731  /* Now store the final chain of decls for the arguments
14732     as the decl-chain of the current lexical scope.
14733     Put the enumerators in as well, at the front so that
14734     DECL_ARGUMENTS is not modified.  */
14735  storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
14736
14737  /* Do the starting of the exception specifications, if we have any.  */
14738  if (flag_exceptions && !processing_template_decl
14739      && flag_enforce_eh_specs
14740      && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14741    current_eh_spec_block = begin_eh_spec_block ();
14742}
14743
14744
14745/* We have finished doing semantic analysis on DECL, but have not yet
14746   generated RTL for its body.  Save away our current state, so that
14747   when we want to generate RTL later we know what to do.  */
14748
14749static void
14750save_function_data (decl)
14751     tree decl;
14752{
14753  struct language_function *f;
14754
14755  /* Save the language-specific per-function data so that we can
14756     get it back when we really expand this function.  */
14757  my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
14758		      19990908);
14759
14760  /* Make a copy.  */
14761  f = ((struct language_function *)
14762       ggc_alloc (sizeof (struct language_function)));
14763  memcpy (f, cp_function_chain, sizeof (struct language_function));
14764  DECL_SAVED_FUNCTION_DATA (decl) = f;
14765
14766  /* Clear out the bits we don't need.  */
14767  f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
14768  f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
14769  f->x_named_label_uses = NULL;
14770  f->bindings = NULL;
14771  f->x_local_names = NULL;
14772
14773  /* When we get back here again, we will be expanding.  */
14774  f->x_expanding_p = 1;
14775
14776  /* If we've already decided that we cannot inline this function, we
14777     must remember that fact when we actually go to expand the
14778     function.  */
14779  if (current_function_cannot_inline)
14780    {
14781      f->cannot_inline = current_function_cannot_inline;
14782      DECL_INLINE (decl) = 0;
14783    }
14784}
14785
14786/* Add a note to mark the beginning of the main body of the constructor.
14787   This is used to set up the data structures for the cleanup regions for
14788   fully-constructed bases and members.  */
14789
14790static void
14791begin_constructor_body ()
14792{
14793}
14794
14795/* Add a note to mark the end of the main body of the constructor.  This is
14796   used to end the cleanup regions for fully-constructed bases and
14797   members.  */
14798
14799static void
14800finish_constructor_body ()
14801{
14802}
14803
14804/* Do all the processing for the beginning of a destructor; set up the
14805   vtable pointers and cleanups for bases and members.  */
14806
14807static void
14808begin_destructor_body ()
14809{
14810  tree if_stmt;
14811  tree compound_stmt;
14812
14813  /* If the dtor is empty, and we know there is not any possible
14814     way we could use any vtable entries, before they are possibly
14815     set by a base class dtor, we don't have to setup the vtables,
14816     as we know that any base class dtor will set up any vtables
14817     it needs.  We avoid MI, because one base class dtor can do a
14818     virtual dispatch to an overridden function that would need to
14819     have a non-related vtable set up, we cannot avoid setting up
14820     vtables in that case.  We could change this to see if there
14821     is just one vtable.
14822
14823     ??? In the destructor for a class, the vtables are set
14824     appropriately for that class.  There will be no non-related
14825     vtables.  jason 2001-12-11.  */
14826  if_stmt = begin_if_stmt ();
14827
14828  /* If it is not safe to avoid setting up the vtables, then
14829     someone will change the condition to be boolean_true_node.
14830     (Actually, for now, we do not have code to set the condition
14831     appropriately, so we just assume that we always need to
14832     initialize the vtables.)  */
14833  finish_if_stmt_cond (boolean_true_node, if_stmt);
14834
14835  compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
14836
14837  /* Make all virtual function table pointers in non-virtual base
14838     classes point to CURRENT_CLASS_TYPE's virtual function
14839     tables.  */
14840  initialize_vtbl_ptrs (current_class_ptr);
14841
14842  finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
14843  finish_then_clause (if_stmt);
14844  finish_if_stmt ();
14845
14846  /* And insert cleanups for our bases and members so that they
14847     will be properly destroyed if we throw.  */
14848  push_base_cleanups ();
14849}
14850
14851/* At the end of every destructor we generate code to delete the object if
14852   necessary.  Do that now.  */
14853
14854static void
14855finish_destructor_body ()
14856{
14857  tree exprstmt;
14858
14859  /* Any return from a destructor will end up here; that way all base
14860     and member cleanups will be run when the function returns.  */
14861  add_stmt (build_stmt (LABEL_STMT, dtor_label));
14862
14863  /* In a virtual destructor, we must call delete.  */
14864  if (DECL_VIRTUAL_P (current_function_decl))
14865    {
14866      tree if_stmt;
14867      tree virtual_size = cxx_sizeof (current_class_type);
14868
14869      /* [class.dtor]
14870
14871      At the point of definition of a virtual destructor (including
14872      an implicit definition), non-placement operator delete shall
14873      be looked up in the scope of the destructor's class and if
14874      found shall be accessible and unambiguous.  */
14875      exprstmt = build_op_delete_call
14876	(DELETE_EXPR, current_class_ptr, virtual_size,
14877	 LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
14878
14879      if_stmt = begin_if_stmt ();
14880      finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
14881				  current_in_charge_parm,
14882				  integer_one_node),
14883			   if_stmt);
14884      finish_expr_stmt (exprstmt);
14885      finish_then_clause (if_stmt);
14886      finish_if_stmt ();
14887    }
14888}
14889
14890/* Do the necessary processing for the beginning of a function body, which
14891   in this case includes member-initializers, but not the catch clauses of
14892   a function-try-block.  Currently, this means opening a binding level
14893   for the member-initializers (in a ctor) and member cleanups (in a dtor).
14894   In other functions, this isn't necessary, but it doesn't hurt.  */
14895
14896tree
14897begin_function_body ()
14898{
14899  tree stmt;
14900
14901  if (processing_template_decl)
14902    /* Do nothing now.  */;
14903  else
14904    /* Always keep the BLOCK node associated with the outermost pair of
14905       curly braces of a function.  These are needed for correct
14906       operation of dwarfout.c.  */
14907    keep_next_level (1);
14908
14909  stmt = begin_compound_stmt (0);
14910  COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
14911
14912  if (processing_template_decl)
14913    /* Do nothing now.  */;
14914  else if (DECL_CONSTRUCTOR_P (current_function_decl))
14915    begin_constructor_body ();
14916  else if (DECL_DESTRUCTOR_P (current_function_decl))
14917    begin_destructor_body ();
14918
14919  return stmt;
14920}
14921
14922/* Do the processing for the end of a function body.  Currently, this means
14923   closing out the cleanups for fully-constructed bases and members, and in
14924   the case of the destructor, deleting the object if desired.  Again, this
14925   is only meaningful for [cd]tors, since they are the only functions where
14926   there is a significant distinction between the main body and any
14927   function catch clauses.  Handling, say, main() return semantics here
14928   would be wrong, as flowing off the end of a function catch clause for
14929   main() would also need to return 0.  */
14930
14931void
14932finish_function_body (compstmt)
14933     tree compstmt;
14934{
14935  /* Close the block.  */
14936  finish_compound_stmt (0, compstmt);
14937
14938  if (processing_template_decl)
14939    /* Do nothing now.  */;
14940  else if (DECL_CONSTRUCTOR_P (current_function_decl))
14941    finish_constructor_body ();
14942  else if (DECL_DESTRUCTOR_P (current_function_decl))
14943    finish_destructor_body ();
14944}
14945
14946/* Finish up a function declaration and compile that function
14947   all the way to assembler language output.  The free the storage
14948   for the function definition.
14949
14950   FLAGS is a bitwise or of the following values:
14951     2 - INCLASS_INLINE
14952       We just finished processing the body of an in-class inline
14953       function definition.  (This processing will have taken place
14954       after the class definition is complete.)  */
14955
14956tree
14957finish_function (flags)
14958     int flags;
14959{
14960  register tree fndecl = current_function_decl;
14961  tree fntype, ctype = NULL_TREE;
14962  int inclass_inline = (flags & 2) != 0;
14963  int nested;
14964
14965  /* When we get some parse errors, we can end up without a
14966     current_function_decl, so cope.  */
14967  if (fndecl == NULL_TREE)
14968    return error_mark_node;
14969
14970  if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14971      && DECL_VIRTUAL_P (fndecl)
14972      && !processing_template_decl)
14973    {
14974      tree fnclass = DECL_CONTEXT (fndecl);
14975      if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14976	keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14977    }
14978
14979  nested = function_depth > 1;
14980  fntype = TREE_TYPE (fndecl);
14981
14982  /*  TREE_READONLY (fndecl) = 1;
14983      This caused &foo to be of type ptr-to-const-function
14984      which then got a warning when stored in a ptr-to-function variable.  */
14985
14986  my_friendly_assert (building_stmt_tree (), 20000911);
14987
14988  finish_fname_decls ();
14989
14990  /* For a cloned function, we've already got all the code we need;
14991     there's no need to add any extra bits.  */
14992  if (!DECL_CLONED_FUNCTION_P (fndecl))
14993    {
14994      if (DECL_MAIN_P (current_function_decl))
14995	{
14996	  /* Make it so that `main' always returns 0 by default.  */
14997#if VMS_TARGET
14998	  finish_return_stmt (integer_one_node);
14999#else
15000	  finish_return_stmt (integer_zero_node);
15001#endif
15002	}
15003
15004      /* Finish dealing with exception specifiers.  */
15005      if (flag_exceptions && !processing_template_decl
15006	  && flag_enforce_eh_specs
15007	  && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
15008	finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
15009			      (TREE_TYPE (current_function_decl)),
15010			      current_eh_spec_block);
15011    }
15012
15013  /* If we're saving up tree structure, tie off the function now.  */
15014  finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
15015
15016  /* If this function can't throw any exceptions, remember that.  */
15017  if (!processing_template_decl
15018      && !cp_function_chain->can_throw
15019      && !flag_non_call_exceptions)
15020    TREE_NOTHROW (fndecl) = 1;
15021
15022  /* This must come after expand_function_end because cleanups might
15023     have declarations (from inline functions) that need to go into
15024     this function's blocks.  */
15025
15026  /* If the current binding level isn't the outermost binding level
15027     for this function, either there is a bug, or we have experienced
15028     syntax errors and the statement tree is malformed.  */
15029  if (current_binding_level->parm_flag != 1)
15030    {
15031      /* Make sure we have already experienced errors.  */
15032      if (errorcount == 0)
15033	abort ();
15034
15035      /* Throw away the broken statement tree and extra binding
15036         levels.  */
15037      DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
15038
15039      while (current_binding_level->parm_flag != 1)
15040	{
15041	  if (current_binding_level->parm_flag == 2)
15042	    pop_nested_class ();
15043	  else
15044	    poplevel (0, 0, 0);
15045	}
15046    }
15047  poplevel (1, 0, 1);
15048
15049  /* Set up the named return value optimization, if we can.  Here, we
15050     eliminate the copy from the nrv into the RESULT_DECL and any cleanup
15051     for the nrv.  genrtl_start_function and declare_return_variable
15052     handle making the nrv and RESULT_DECL share space.  */
15053  if (current_function_return_value)
15054    {
15055      tree r = current_function_return_value;
15056      tree outer;
15057
15058      if (r != error_mark_node
15059	  /* This is only worth doing for fns that return in memory--and
15060	     simpler, since we don't have to worry about promoted modes.  */
15061	  && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
15062	  /* Only allow this for variables declared in the outer scope of
15063	     the function so we know that their lifetime always ends with a
15064	     return; see g++.dg/opt/nrv6.C.  We could be more flexible if
15065	     we were to do this optimization in tree-ssa.  */
15066	  && (outer = BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl)),
15067	      chain_member (r, BLOCK_VARS (outer))))
15068	{
15069
15070	  DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
15071	  walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
15072					nullify_returns_r, r);
15073	}
15074      else
15075	/* Clear it so genrtl_start_function and declare_return_variable
15076	   know we're not optimizing.  */
15077	current_function_return_value = NULL_TREE;
15078    }
15079
15080  /* Remember that we were in class scope.  */
15081  if (current_class_name)
15082    ctype = current_class_type;
15083
15084  /* Must mark the RESULT_DECL as being in this function.  */
15085  DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
15086
15087  /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
15088     to the FUNCTION_DECL node itself.  */
15089  BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
15090
15091  /* Save away current state, if appropriate.  */
15092  if (!processing_template_decl)
15093    save_function_data (fndecl);
15094
15095  /* If this function calls `setjmp' it cannot be inlined.  When
15096     `longjmp' is called it is not guaranteed to restore the value of
15097     local variables that have been modified since the call to
15098     `setjmp'.  So, if were to inline this function into some caller
15099     `c', then when we `longjmp', we might not restore all variables
15100     in `c'.  (It might seem, at first blush, that there's no way for
15101     this function to modify local variables in `c', but their
15102     addresses may have been stored somewhere accessible to this
15103     function.)  */
15104  if (!processing_template_decl && calls_setjmp_p (fndecl))
15105    DECL_UNINLINABLE (fndecl) = 1;
15106
15107  /* Complain if there's just no return statement.  */
15108  if (warn_return_type
15109      && !processing_template_decl
15110      && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
15111      && !current_function_returns_value && !current_function_returns_null
15112      /* Don't complain if we abort or throw.  */
15113      && !current_function_returns_abnormally
15114      && !DECL_NAME (DECL_RESULT (fndecl))
15115      /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
15116	 inline function, as we might never be compiled separately.  */
15117      && DECL_INLINE (fndecl))
15118    warning ("no return statement in function returning non-void");
15119
15120  /* Clear out memory we no longer need.  */
15121  free_after_parsing (cfun);
15122  /* Since we never call rest_of_compilation, we never clear
15123     CFUN.  Do so explicitly.  */
15124  free_after_compilation (cfun);
15125  cfun = NULL;
15126
15127  /* If this is an in-class inline definition, we may have to pop the
15128     bindings for the template parameters that we added in
15129     maybe_begin_member_template_processing when start_function was
15130     called.  */
15131  if (inclass_inline)
15132    maybe_end_member_template_processing ();
15133
15134  /* Leave the scope of the class.  */
15135  if (ctype)
15136    pop_nested_class ();
15137
15138  --function_depth;
15139
15140  /* Clean up.  */
15141  if (! nested)
15142    /* Let the error reporting routines know that we're outside a
15143       function.  For a nested function, this value is used in
15144       cxx_pop_function_context and then reset via pop_function_context.  */
15145    current_function_decl = NULL_TREE;
15146
15147  return fndecl;
15148}
15149
15150/* Create the FUNCTION_DECL for a function definition.
15151   DECLSPECS and DECLARATOR are the parts of the declaration;
15152   they describe the return type and the name of the function,
15153   but twisted together in a fashion that parallels the syntax of C.
15154
15155   This function creates a binding context for the function body
15156   as well as setting up the FUNCTION_DECL in current_function_decl.
15157
15158   Returns a FUNCTION_DECL on success.
15159
15160   If the DECLARATOR is not suitable for a function (it defines a datum
15161   instead), we return 0, which tells yyparse to report a parse error.
15162
15163   May return void_type_node indicating that this method is actually
15164   a friend.  See grokfield for more details.
15165
15166   Came here with a `.pushlevel' .
15167
15168   DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
15169   CHANGES TO CODE IN `grokfield'.  */
15170
15171tree
15172start_method (declspecs, declarator, attrlist)
15173     tree declarator, declspecs, attrlist;
15174{
15175  tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
15176				&attrlist);
15177
15178  /* Something too ugly to handle.  */
15179  if (fndecl == NULL_TREE)
15180    return NULL_TREE;
15181
15182  if (attrlist)
15183    cplus_decl_attributes (&fndecl, attrlist, 0);
15184
15185  /* Pass friends other than inline friend functions back.  */
15186  if (fndecl == void_type_node)
15187    return fndecl;
15188
15189  if (TREE_CODE (fndecl) != FUNCTION_DECL)
15190    /* Not a function, tell parser to report parse error.  */
15191    return NULL_TREE;
15192
15193  if (DECL_IN_AGGR_P (fndecl))
15194    {
15195      if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
15196	{
15197	  if (DECL_CONTEXT (fndecl)
15198	      && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
15199	    error ("`%D' is already defined in class `%T'", fndecl,
15200	              DECL_CONTEXT (fndecl));
15201	}
15202      return void_type_node;
15203    }
15204
15205  check_template_shadow (fndecl);
15206
15207  DECL_DECLARED_INLINE_P (fndecl) = 1;
15208
15209  DID_INLINE_FUNC (fndecl) = 0;
15210  if (flag_default_inline)
15211    DECL_INLINE (fndecl) = 1;
15212
15213  /* We process method specializations in finish_struct_1.  */
15214  if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
15215    fndecl = push_template_decl (fndecl);
15216
15217  if (! DECL_FRIEND_P (fndecl))
15218    {
15219      if (TREE_CHAIN (fndecl))
15220	{
15221	  fndecl = copy_node (fndecl);
15222	  TREE_CHAIN (fndecl) = NULL_TREE;
15223	}
15224      grok_special_member_properties (fndecl);
15225    }
15226
15227  cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
15228
15229  /* Make a place for the parms */
15230  pushlevel (0);
15231  current_binding_level->parm_flag = 1;
15232
15233  DECL_IN_AGGR_P (fndecl) = 1;
15234  return fndecl;
15235}
15236
15237/* Go through the motions of finishing a function definition.
15238   We don't compile this method until after the whole class has
15239   been processed.
15240
15241   FINISH_METHOD must return something that looks as though it
15242   came from GROKFIELD (since we are defining a method, after all).
15243
15244   This is called after parsing the body of the function definition.
15245   STMTS is the chain of statements that makes up the function body.
15246
15247   DECL is the ..._DECL that `start_method' provided.  */
15248
15249tree
15250finish_method (decl)
15251     tree decl;
15252{
15253  register tree fndecl = decl;
15254  tree old_initial;
15255
15256  register tree link;
15257
15258  if (decl == void_type_node)
15259    return decl;
15260
15261  old_initial = DECL_INITIAL (fndecl);
15262
15263  /* Undo the level for the parms (from start_method).
15264     This is like poplevel, but it causes nothing to be
15265     saved.  Saving information here confuses symbol-table
15266     output routines.  Besides, this information will
15267     be correctly output when this method is actually
15268     compiled.  */
15269
15270  /* Clear out the meanings of the local variables of this level;
15271     also record in each decl which block it belongs to.  */
15272
15273  for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
15274    {
15275      if (DECL_NAME (link) != NULL_TREE)
15276	pop_binding (DECL_NAME (link), link);
15277      my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
15278      DECL_CONTEXT (link) = NULL_TREE;
15279    }
15280
15281  poplevel (0, 0, 0);
15282
15283  DECL_INITIAL (fndecl) = old_initial;
15284
15285  /* We used to check if the context of FNDECL was different from
15286     current_class_type as another way to get inside here.  This didn't work
15287     for String.cc in libg++.  */
15288  if (DECL_FRIEND_P (fndecl))
15289    {
15290      CLASSTYPE_INLINE_FRIENDS (current_class_type)
15291	= tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
15292      decl = void_type_node;
15293    }
15294
15295  return decl;
15296}
15297
15298
15299/* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
15300   we can lay it out later, when and if its type becomes complete.  */
15301
15302void
15303maybe_register_incomplete_var (var)
15304     tree var;
15305{
15306  my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
15307
15308  /* Keep track of variables with incomplete types.  */
15309  if (!processing_template_decl && TREE_TYPE (var) != error_mark_node
15310      && DECL_EXTERNAL (var))
15311    {
15312      tree inner_type = TREE_TYPE (var);
15313
15314      while (TREE_CODE (inner_type) == ARRAY_TYPE)
15315	inner_type = TREE_TYPE (inner_type);
15316      inner_type = TYPE_MAIN_VARIANT (inner_type);
15317
15318      if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
15319	  /* RTTI TD entries are created while defining the type_info.  */
15320	  || (TYPE_LANG_SPECIFIC (inner_type)
15321	      && TYPE_BEING_DEFINED (inner_type)))
15322	incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
15323    }
15324}
15325
15326/* Called when a class type (given by TYPE) is defined.  If there are
15327   any existing VAR_DECLs whose type hsa been completed by this
15328   declaration, update them now.  */
15329
15330void
15331complete_vars (type)
15332     tree type;
15333{
15334  tree *list = &incomplete_vars;
15335
15336  my_friendly_assert (CLASS_TYPE_P (type), 20020406);
15337  while (*list)
15338    {
15339      if (same_type_p (type, TREE_PURPOSE (*list)))
15340	{
15341	  tree var = TREE_VALUE (*list);
15342	  /* Complete the type of the variable.  The VAR_DECL itself
15343	     will be laid out in expand_expr.  */
15344	  complete_type (TREE_TYPE (var));
15345	  /* Remove this entry from the list.  */
15346	  *list = TREE_CHAIN (*list);
15347	}
15348      else
15349	list = &TREE_CHAIN (*list);
15350    }
15351}
15352
15353/* If DECL is of a type which needs a cleanup, build that cleanup
15354   here.  */
15355
15356tree
15357cxx_maybe_build_cleanup (decl)
15358     tree decl;
15359{
15360  tree type = TREE_TYPE (decl);
15361
15362  if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
15363    {
15364      int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
15365      tree rval;
15366
15367      if (TREE_CODE (type) == ARRAY_TYPE)
15368	rval = decl;
15369      else
15370	{
15371	  cxx_mark_addressable (decl);
15372	  rval = build_unary_op (ADDR_EXPR, decl, 0);
15373	}
15374
15375      /* Optimize for space over speed here.  */
15376      if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
15377	  || flag_expensive_optimizations)
15378	flags |= LOOKUP_NONVIRTUAL;
15379
15380      rval = build_delete (TREE_TYPE (rval), rval,
15381			   sfk_complete_destructor, flags, 0);
15382
15383      if (TYPE_USES_VIRTUAL_BASECLASSES (type)
15384	  && ! TYPE_HAS_DESTRUCTOR (type))
15385	rval = build_compound_expr (tree_cons (NULL_TREE, rval,
15386					       build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
15387
15388      return rval;
15389    }
15390  return NULL_TREE;
15391}
15392
15393/* When a stmt has been parsed, this function is called.  */
15394
15395void
15396finish_stmt ()
15397{
15398  /* Always assume this statement was not an expression statement.  If
15399     it actually was an expression statement, its our callers
15400     responsibility to fix this up.  */
15401  last_expr_type = NULL_TREE;
15402}
15403
15404/* DECL was originally constructed as a non-static member function,
15405   but turned out to be static.  Update it accordingly.  */
15406
15407void
15408revert_static_member_fn (decl)
15409     tree decl;
15410{
15411  tree tmp;
15412  tree function = TREE_TYPE (decl);
15413  tree args = TYPE_ARG_TYPES (function);
15414
15415  if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
15416      != TYPE_UNQUALIFIED)
15417    error ("static member function `%#D' declared with type qualifiers",
15418	      decl);
15419
15420  args = TREE_CHAIN (args);
15421  tmp = build_function_type (TREE_TYPE (function), args);
15422  tmp = build_qualified_type (tmp, cp_type_quals (function));
15423  tmp = build_exception_variant (tmp,
15424				 TYPE_RAISES_EXCEPTIONS (function));
15425  TREE_TYPE (decl) = tmp;
15426  if (DECL_ARGUMENTS (decl))
15427    DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
15428  DECL_STATIC_FUNCTION_P (decl) = 1;
15429}
15430
15431/* Initialize the variables used during compilation of a C++
15432   function.  */
15433
15434void
15435cxx_push_function_context (f)
15436     struct function *f;
15437{
15438  struct language_function *p
15439    = ((struct language_function *)
15440       ggc_alloc_cleared (sizeof (struct language_function)));
15441  f->language = p;
15442
15443  /* It takes an explicit call to expand_body to generate RTL for a
15444     function.  */
15445  expanding_p = 0;
15446
15447  /* Whenever we start a new function, we destroy temporaries in the
15448     usual way.  */
15449  current_stmt_tree ()->stmts_are_full_exprs_p = 1;
15450}
15451
15452/* Free the language-specific parts of F, now that we've finished
15453   compiling the function.  */
15454
15455void
15456cxx_pop_function_context (f)
15457     struct function *f;
15458{
15459  f->language = 0;
15460}
15461
15462/* Return which tree structure is used by T, or TS_CP_GENERIC if T is
15463   one of the language-independent trees.  */
15464
15465enum cp_tree_node_structure_enum
15466cp_tree_node_structure (t)
15467     union lang_tree_node *t;
15468{
15469  switch (TREE_CODE (&t->generic))
15470    {
15471    case DEFAULT_ARG:		return TS_CP_IDENTIFIER;
15472    case IDENTIFIER_NODE:	return TS_CP_IDENTIFIER;
15473    case OVERLOAD:		return TS_CP_OVERLOAD;
15474    case TEMPLATE_PARM_INDEX:	return TS_CP_TPI;
15475    case PTRMEM_CST:		return TS_CP_PTRMEM;
15476    case WRAPPER:		return TS_CP_WRAPPER;
15477    case SRCLOC:		return TS_CP_SRCLOC;
15478    default:			return TS_CP_GENERIC;
15479    }
15480}
15481
15482/* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
15483   the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
15484
15485tree
15486identifier_global_value	(t)
15487     tree t;
15488{
15489  return IDENTIFIER_GLOBAL_VALUE (t);
15490}
15491
15492/* Build the void_list_node (void_type_node having been created).  */
15493tree
15494build_void_list_node ()
15495{
15496  tree t = build_tree_list (NULL_TREE, void_type_node);
15497  TREE_PARMLIST (t) = 1;
15498  return t;
15499}
15500
15501static int
15502cp_missing_noreturn_ok_p (decl)
15503     tree decl;
15504{
15505  /* A missing noreturn is ok for the `main' function.  */
15506  return DECL_MAIN_P (decl);
15507}
15508
15509#include "gt-cp-decl.h"
15510#include "gtype-cp.h"
15511