OSDN Git Service

PR c++/9364, c++/10553, c++/10586
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl.c
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
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, 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 "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "lex.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "diagnostic.h"
52 #include "debug.h"
53 #include "timevar.h"
54
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
57
58 static void push_binding_level (struct cp_binding_level *, int,
59                                 int);
60 static void pop_binding_level (void);
61 static void suspend_binding_level (void);
62 static void resume_binding_level (struct cp_binding_level *);
63 static struct cp_binding_level *make_binding_level (void);
64 static void declare_namespace_level (void);
65 static int decl_jump_unsafe (tree);
66 static void storedecls (tree);
67 static void require_complete_types_for_parms (tree);
68 static int ambi_op_p (enum tree_code);
69 static int unary_op_p (enum tree_code);
70 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
71 static tree lookup_tag_reverse (tree, tree);
72 static void push_local_name (tree);
73 static void warn_extern_redeclared_static (tree, tree);
74 static tree grok_reference_init (tree, tree, tree);
75 static tree grokfndecl (tree, tree, tree, tree, int,
76                         enum overload_flags, tree,
77                         tree, int, int, int, int, int, int, tree);
78 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
79 static tree follow_tag_typedef (tree);
80 static tree lookup_tag (enum tree_code, tree,
81                         struct cp_binding_level *, int);
82 static void set_identifier_type_value_with_scope
83         (tree, tree, struct cp_binding_level *);
84 static void record_unknown_type (tree, const char *);
85 static tree builtin_function_1 (const char *, tree, tree, int,
86                                 enum built_in_class, const char *,
87                                 tree);
88 static tree build_library_fn_1 (tree, enum tree_code, tree);
89 static int member_function_or_else (tree, tree, enum overload_flags);
90 static void bad_specifiers (tree, const char *, int, int, int, int,
91                             int);
92 static tree maybe_process_template_type_declaration 
93         (tree, int, struct cp_binding_level*);
94 static void check_for_uninitialized_const_var (tree);
95 static hashval_t typename_hash (const void *);
96 static int typename_compare (const void *, const void *);
97 static void push_binding (tree, tree, struct cp_binding_level*);
98 static int add_binding (tree, tree);
99 static void pop_binding (tree, tree);
100 static tree local_variable_p_walkfn (tree *, int *, void *);
101 static tree select_decl (cxx_binding *, int);
102 static int lookup_flags (int, int);
103 static tree qualify_lookup (tree, int);
104 static tree record_builtin_java_type (const char *, int);
105 static const char *tag_name (enum tag_types code);
106 static void find_class_binding_level (void);
107 static struct cp_binding_level *innermost_nonclass_level (void);
108 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
109 static int walk_globals_r (tree, void*);
110 static int walk_vtables_r (tree, void*);
111 static void add_decl_to_level (tree, struct cp_binding_level *);
112 static tree make_label_decl (tree, int);
113 static void use_label (tree);
114 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
115                                    const location_t *);
116 static void check_previous_goto (struct named_label_use_list *);
117 static void check_switch_goto (struct cp_binding_level *);
118 static void check_previous_gotos (tree);
119 static void pop_label (tree, tree);
120 static void pop_labels (tree);
121 static void maybe_deduce_size_from_array_init (tree, tree);
122 static void layout_var_decl (tree);
123 static void maybe_commonize_var (tree);
124 static tree check_initializer (tree, tree, int);
125 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
126 static void save_function_data (tree);
127 static void check_function_type (tree, tree);
128 static void begin_constructor_body (void);
129 static void finish_constructor_body (void);
130 static void begin_destructor_body (void);
131 static void finish_destructor_body (void);
132 static tree create_array_type_for_decl (tree, tree, tree);
133 static tree get_atexit_node (void);
134 static tree get_dso_handle_node (void);
135 static tree start_cleanup_fn (void);
136 static void end_cleanup_fn (void);
137 static tree cp_make_fname_decl (tree, int);
138 static void initialize_predefined_identifiers (void);
139 static tree check_special_function_return_type 
140         (special_function_kind, tree, tree);
141 static tree push_cp_library_fn (enum tree_code, tree);
142 static tree build_cp_library_fn (tree, enum tree_code, tree);
143 static void store_parm_decls (tree);
144 static int cp_missing_noreturn_ok_p (tree);
145 static void initialize_local_var (tree, tree);
146 static void expand_static_init (tree, tree);
147 static tree next_initializable_field (tree);
148 static tree reshape_init (tree, tree *);
149 static tree build_typename_type (tree, tree, tree);
150
151 #if defined (DEBUG_BINDING_LEVELS)
152 static void indent (void);
153 #endif
154
155 /* Erroneous argument lists can use this *IFF* they do not modify it.  */
156 tree error_mark_list;
157
158 /* The following symbols are subsumed in the cp_global_trees array, and
159    listed here individually for documentation purposes.
160
161    C++ extensions
162         tree wchar_decl_node;
163
164         tree vtable_entry_type;
165         tree delta_type_node;
166         tree __t_desc_type_node;
167         tree ti_desc_type_node;
168         tree bltn_desc_type_node, ptr_desc_type_node;
169         tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
170         tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
171         tree ptm_desc_type_node;
172         tree base_desc_type_node;
173
174         tree class_type_node, record_type_node, union_type_node, enum_type_node;
175         tree unknown_type_node;
176
177    Array type `vtable_entry_type[]'
178
179         tree vtbl_type_node;
180         tree vtbl_ptr_type_node;
181
182    Namespaces,
183
184         tree std_node;
185         tree abi_node;
186
187    A FUNCTION_DECL which can call `abort'.  Not necessarily the
188    one that the user will declare, but sufficient to be called
189    by routines that want to abort the program.
190
191         tree abort_fndecl;
192
193    The FUNCTION_DECL for the default `::operator delete'.
194
195         tree global_delete_fndecl;
196
197    Used by RTTI
198         tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
199         tree tinfo_var_id;
200
201 */
202
203 tree cp_global_trees[CPTI_MAX];
204
205 /* Indicates that there is a type value in some namespace, although
206    that is not necessarily in scope at the moment.  */
207
208 static GTY(()) tree global_type_node;
209
210 /* Used only for jumps to as-yet undefined labels, since jumps to
211    defined labels can have their validity checked immediately.  */
212
213 struct named_label_use_list GTY(())
214 {
215   struct cp_binding_level *binding_level;
216   tree names_in_scope;
217   tree label_decl;
218   location_t o_goto_locus;
219   struct named_label_use_list *next;
220 };
221
222 #define named_label_uses cp_function_chain->x_named_label_uses
223
224 #define local_names cp_function_chain->x_local_names
225
226 /* A list of objects which have constructors or destructors
227    which reside in the global scope.  The decl is stored in
228    the TREE_VALUE slot and the initializer is stored
229    in the TREE_PURPOSE slot.  */
230 tree static_aggregates;
231
232 /* -- end of C++ */
233
234 /* A node for the integer constants 2, and 3.  */
235
236 tree integer_two_node, integer_three_node;
237
238 /* Similar, for last_function_parm_tags.  */
239 tree last_function_parms;
240
241 /* A list of all LABEL_DECLs in the function that have names.  Here so
242    we can clear out their names' definitions at the end of the
243    function, and so we can check the validity of jumps to these labels.  */
244
245 struct named_label_list GTY(())
246 {
247   struct cp_binding_level *binding_level;
248   tree names_in_scope;
249   tree old_value;
250   tree label_decl;
251   tree bad_decls;
252   struct named_label_list *next;
253   unsigned int in_try_scope : 1;
254   unsigned int in_catch_scope : 1;
255 };
256
257 #define named_labels cp_function_chain->x_named_labels
258 \f
259 /* The name of the anonymous namespace, throughout this translation
260    unit.  */
261 tree anonymous_namespace_name;
262
263 /* The number of function bodies which we are currently processing.
264    (Zero if we are at namespace scope, one inside the body of a
265    function, two inside the body of a function in a local class, etc.)  */
266 int function_depth;
267
268 /* States indicating how grokdeclarator() should handle declspecs marked
269    with __attribute__((deprecated)).  An object declared as
270    __attribute__((deprecated)) suppresses warnings of uses of other
271    deprecated items.  */
272    
273 enum deprecated_states {
274   DEPRECATED_NORMAL,
275   DEPRECATED_SUPPRESS
276 };
277
278 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
279
280 /* Set by add_implicitly_declared_members() to keep those members from
281    being flagged as deprecated or reported as using deprecated
282    types.  */
283 int adding_implicit_members = 0;
284
285 /* True if a declaration with an `extern' linkage specifier is being
286    processed.  */
287 bool have_extern_spec;
288
289 \f
290 /* For each binding contour we allocate a binding_level structure
291    which records the names defined in that contour.
292    Contours include:
293     0) the global one
294     1) one for each function definition,
295        where internal declarations of the parameters appear.
296     2) one for each compound statement,
297        to record its declarations.
298
299    The current meaning of a name can be found by searching the levels
300    from the current one out to the global one.
301
302    Off to the side, may be the class_binding_level.  This exists only
303    to catch class-local declarations.  It is otherwise nonexistent.
304
305    Also there may be binding levels that catch cleanups that must be
306    run when exceptions occur.  Thus, to see whether a name is bound in
307    the current scope, it is not enough to look in the
308    CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
309    instead.  */
310
311 /* Note that the information in the `names' component of the global contour
312    is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
313
314 struct cp_binding_level GTY(())
315   {
316     /* A chain of _DECL nodes for all variables, constants, functions,
317        and typedef types.  These are in the reverse of the order
318        supplied.  There may be OVERLOADs on this list, too, but they
319        are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
320     tree names;
321
322     /* Count of elements in names chain.  */
323     size_t names_size;
324
325     /* A chain of NAMESPACE_DECL nodes.  */
326     tree namespaces;
327
328     /* An array of static functions and variables (for namespaces only) */
329     varray_type static_decls;
330
331     /* A chain of VTABLE_DECL nodes.  */
332     tree vtables; 
333
334     /* A list of structure, union and enum definitions, for looking up
335        tag names.
336        It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
337        or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
338        or ENUMERAL_TYPE node.
339
340        C++: the TREE_VALUE nodes can be simple types for
341        component_bindings.  */
342     tree tags;
343
344     /* A list of USING_DECL nodes.  */
345     tree usings;
346
347     /* A list of used namespaces. PURPOSE is the namespace,
348        VALUE the common ancestor with this binding_level's namespace.  */
349     tree using_directives;
350
351     /* If this binding level is the binding level for a class, then
352        class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
353        is the name of an entity bound in the class.  The TREE_TYPE is
354        the DECL bound by this name in the class.  */
355     tree class_shadowed;
356
357     /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
358        is used for all binding levels. In addition the TREE_VALUE is the
359        IDENTIFIER_TYPE_VALUE before we entered the class.  */
360     tree type_shadowed;
361
362     /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
363        label in this scope.  The TREE_PURPOSE is the previous value of
364        the IDENTIFIER_LABEL VALUE.  */
365     tree shadowed_labels;
366
367     /* For each level (except not the global one),
368        a chain of BLOCK nodes for all the levels
369        that were entered and exited one level down.  */
370     tree blocks;
371
372     /* The _TYPE node for this level, if parm_flag == 2.  */
373     tree this_class;
374
375     /* The binding level which this one is contained in (inherits from).  */
376     struct cp_binding_level *level_chain;
377
378     /* List of VAR_DECLS saved from a previous for statement.
379        These would be dead in ISO-conforming code, but might
380        be referenced in ARM-era code.  These are stored in a
381        TREE_LIST; the TREE_VALUE is the actual declaration.  */
382     tree dead_vars_from_for;
383
384     /* 1 for the level that holds the parameters of a function.
385        2 for the level that holds a class declaration.  */
386     unsigned parm_flag : 2;
387
388     /* 1 means make a BLOCK for this level regardless of all else.
389        2 for temporary binding contours created by the compiler.  */
390     unsigned keep : 2;
391
392     /* Nonzero if this level "doesn't exist" for tags.  */
393     unsigned tag_transparent : 1;
394
395     /* Nonzero if this level can safely have additional
396        cleanup-needing variables added to it.  */
397     unsigned more_cleanups_ok : 1;
398     unsigned have_cleanups : 1;
399
400     /* Nonzero if this scope is for storing the decls for template
401        parameters and generic decls; these decls will be discarded and
402        replaced with a TEMPLATE_DECL.  */
403     unsigned template_parms_p : 1;
404
405     /* Nonzero if this scope corresponds to the `<>' in a
406        `template <>' clause.  Whenever this flag is set,
407        TEMPLATE_PARMS_P will be set as well.  */
408     unsigned template_spec_p : 1;
409
410     /* This is set for a namespace binding level.  */
411     unsigned namespace_p : 1;
412
413     /* True if this level is that of a for-statement where we need to
414        worry about ambiguous (ARM or ISO) scope rules.  */
415     unsigned is_for_scope : 1;
416
417     /* True if this level corresponds to a TRY block.  Currently this
418        information is only available while building the tree structure.  */
419     unsigned is_try_scope : 1;
420
421     /* True if this level corresponds to a CATCH block.  Currently this
422        information is only available while building the tree structure.  */
423     unsigned is_catch_scope : 1;
424
425     /* Three bits left for this word.  */
426
427     /* Binding depth at which this level began.  */
428     unsigned binding_depth;
429   };
430
431 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
432
433 /* The binding level currently in effect.  */
434
435 #define current_binding_level                   \
436   (*(cfun && cp_function_chain->bindings        \
437    ? &cp_function_chain->bindings               \
438    : &scope_chain->bindings))
439
440 /* The binding level of the current class, if any.  */
441
442 #define class_binding_level scope_chain->class_bindings
443
444 /* A chain of binding_level structures awaiting reuse.  */
445
446 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
447
448 /* The outermost binding level, for names of file scope.
449    This is created when the compiler is started and exists
450    through the entire run.  */
451
452 static GTY(()) struct cp_binding_level *global_binding_level;
453
454 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
455
456 static int keep_next_level_flag;
457
458 /* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
459    UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
460    time the VAR_DECL was declared, the type was incomplete.  */
461
462 static GTY(()) tree incomplete_vars;
463
464 #if defined(DEBUG_BINDING_LEVELS)
465 static int binding_depth = 0;
466 static int is_class_level = 0;
467
468 static void
469 indent (void)
470 {
471   register unsigned i;
472
473   for (i = 0; i < binding_depth*2; i++)
474     putc (' ', stderr);
475 }
476 #endif /* defined(DEBUG_BINDING_LEVELS) */
477
478 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
479
480 static void
481 push_binding_level (struct cp_binding_level *newlevel, 
482                     int tag_transparent, 
483                     int keep)
484 {
485   /* Add this level to the front of the chain (stack) of levels that
486      are active.  */
487   memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
488   newlevel->level_chain = current_binding_level;
489   current_binding_level = newlevel;
490   newlevel->tag_transparent = tag_transparent;
491   newlevel->more_cleanups_ok = 1;
492
493   newlevel->keep = keep;
494 #if defined(DEBUG_BINDING_LEVELS)
495   newlevel->binding_depth = binding_depth;
496   indent ();
497   fprintf (stderr, "push %s level 0x%08x line %d\n",
498            (is_class_level) ? "class" : "block", newlevel, input_line);
499   is_class_level = 0;
500   binding_depth++;
501 #endif /* defined(DEBUG_BINDING_LEVELS) */
502 }
503
504 /* Find the innermost enclosing class scope, and reset
505    CLASS_BINDING_LEVEL appropriately.  */
506
507 static void
508 find_class_binding_level (void)
509 {
510   struct cp_binding_level *level = current_binding_level;
511
512   while (level && level->parm_flag != 2)
513     level = level->level_chain;
514   if (level && level->parm_flag == 2)
515     class_binding_level = level;
516   else
517     class_binding_level = 0;
518 }
519
520 static void
521 pop_binding_level (void)
522 {
523   if (global_binding_level)
524     {
525       /* Cannot pop a level, if there are none left to pop.  */
526       if (current_binding_level == global_binding_level)
527         abort ();
528     }
529   /* Pop the current level, and free the structure for reuse.  */
530 #if defined(DEBUG_BINDING_LEVELS)
531   binding_depth--;
532   indent ();
533   fprintf (stderr, "pop  %s level 0x%08x line %d\n",
534           (is_class_level) ? "class" : "block",
535           current_binding_level, input_line);
536   if (is_class_level != (current_binding_level == class_binding_level))
537     {
538       indent ();
539       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
540     }
541   is_class_level = 0;
542 #endif /* defined(DEBUG_BINDING_LEVELS) */
543   {
544     register struct cp_binding_level *level = current_binding_level;
545     current_binding_level = current_binding_level->level_chain;
546     level->level_chain = free_binding_level;
547 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
548     if (level->binding_depth != binding_depth)
549       abort ();
550 #endif /* defined(DEBUG_BINDING_LEVELS) */
551     free_binding_level = level;
552     find_class_binding_level ();
553   }
554 }
555
556 static void
557 suspend_binding_level (void)
558 {
559   if (class_binding_level)
560     current_binding_level = class_binding_level;
561
562   if (global_binding_level)
563     {
564       /* Cannot suspend a level, if there are none left to suspend.  */
565       if (current_binding_level == global_binding_level)
566         abort ();
567     }
568   /* Suspend the current level.  */
569 #if defined(DEBUG_BINDING_LEVELS)
570   binding_depth--;
571   indent ();
572   fprintf (stderr, "suspend  %s level 0x%08x line %d\n",
573           (is_class_level) ? "class" : "block",
574           current_binding_level, input_line);
575   if (is_class_level != (current_binding_level == class_binding_level))
576     {
577       indent ();
578       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
579     }
580   is_class_level = 0;
581 #endif /* defined(DEBUG_BINDING_LEVELS) */
582   current_binding_level = current_binding_level->level_chain;
583   find_class_binding_level ();
584 }
585
586 static void
587 resume_binding_level (struct cp_binding_level* b)
588 {
589   /* Resuming binding levels is meant only for namespaces,
590      and those cannot nest into classes.  */
591   my_friendly_assert(!class_binding_level, 386);
592   /* Also, resuming a non-directly nested namespace is a no-no.  */
593   my_friendly_assert(b->level_chain == current_binding_level, 386);
594   current_binding_level = b;
595 #if defined(DEBUG_BINDING_LEVELS)
596   b->binding_depth = binding_depth;
597   indent ();
598   fprintf (stderr, "resume %s level 0x%08x line %d\n",
599            (is_class_level) ? "class" : "block", b, input_line);
600   is_class_level = 0;
601   binding_depth++;
602 #endif /* defined(DEBUG_BINDING_LEVELS) */
603 }
604 \f
605 /* Create a new `struct cp_binding_level'.  */
606
607 static
608 struct cp_binding_level *
609 make_binding_level (void)
610 {
611   /* NOSTRICT */
612   return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
613 }
614
615 /* Nonzero if we are currently in the global binding level.  */
616
617 int
618 global_bindings_p (void)
619 {
620   return current_binding_level == global_binding_level;
621 }
622
623 /* Return the innermost binding level that is not for a class scope.  */
624
625 static struct cp_binding_level *
626 innermost_nonclass_level (void)
627 {
628   struct cp_binding_level *b;
629
630   b = current_binding_level;
631   while (b->parm_flag == 2)
632     b = b->level_chain;
633
634   return b;
635 }
636
637 /* Nonzero if we are currently in a toplevel binding level.  This
638    means either the global binding level or a namespace in a toplevel
639    binding level.  Since there are no non-toplevel namespace levels,
640    this really means any namespace or template parameter level.  We
641    also include a class whose context is toplevel.  */
642
643 int
644 toplevel_bindings_p (void)
645 {
646   struct cp_binding_level *b = innermost_nonclass_level ();
647
648   return b->namespace_p || b->template_parms_p;
649 }
650
651 /* Nonzero if this is a namespace scope, or if we are defining a class
652    which is itself at namespace scope, or whose enclosing class is
653    such a class, etc.  */
654
655 int
656 namespace_bindings_p (void)
657 {
658   struct cp_binding_level *b = innermost_nonclass_level ();
659
660   return b->namespace_p;
661 }
662
663 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
664    unconditionally.  Otherwise, use the normal logic to decide whether
665    or not to create a BLOCK.  */
666
667 void
668 keep_next_level (int keep)
669 {
670   keep_next_level_flag = keep;
671 }
672
673 /* Nonzero if the current level needs to have a BLOCK made.  */
674
675 int
676 kept_level_p (void)
677 {
678   return (current_binding_level->blocks != NULL_TREE
679           || current_binding_level->keep
680           || current_binding_level->names != NULL_TREE
681           || (current_binding_level->tags != NULL_TREE
682               && !current_binding_level->tag_transparent));
683 }
684
685 static void
686 declare_namespace_level (void)
687 {
688   current_binding_level->namespace_p = 1;
689 }
690
691 /* Returns nonzero if this scope was created to store template
692    parameters.  */
693
694 int
695 template_parm_scope_p (void)
696 {
697   return current_binding_level->template_parms_p;
698 }
699
700 /* Returns the kind of template specialization we are currently
701    processing, given that it's declaration contained N_CLASS_SCOPES
702    explicit scope qualifications.  */
703
704 tmpl_spec_kind
705 current_tmpl_spec_kind (int n_class_scopes)
706 {
707   int n_template_parm_scopes = 0;
708   int seen_specialization_p = 0;
709   int innermost_specialization_p = 0;
710   struct cp_binding_level *b;
711
712   /* Scan through the template parameter scopes.  */
713   for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
714     {
715       /* If we see a specialization scope inside a parameter scope,
716          then something is wrong.  That corresponds to a declaration
717          like:
718
719             template <class T> template <> ...
720
721          which is always invalid since [temp.expl.spec] forbids the
722          specialization of a class member template if the enclosing
723          class templates are not explicitly specialized as well.  */
724       if (b->template_spec_p)
725         {
726           if (n_template_parm_scopes == 0)
727             innermost_specialization_p = 1;
728           else
729             seen_specialization_p = 1;
730         }
731       else if (seen_specialization_p == 1)
732         return tsk_invalid_member_spec;
733
734       ++n_template_parm_scopes;
735     }
736
737   /* Handle explicit instantiations.  */
738   if (processing_explicit_instantiation)
739     {
740       if (n_template_parm_scopes != 0)
741         /* We've seen a template parameter list during an explicit
742            instantiation.  For example:
743
744              template <class T> template void f(int);
745
746            This is erroneous.  */
747         return tsk_invalid_expl_inst;
748       else
749         return tsk_expl_inst;
750     }
751
752   if (n_template_parm_scopes < n_class_scopes)
753     /* We've not seen enough template headers to match all the
754        specialized classes present.  For example:
755
756          template <class T> void R<T>::S<T>::f(int);
757
758        This is invalid; there needs to be one set of template
759        parameters for each class.  */
760     return tsk_insufficient_parms;
761   else if (n_template_parm_scopes == n_class_scopes)
762     /* We're processing a non-template declaration (even though it may
763        be a member of a template class.)  For example:
764
765          template <class T> void S<T>::f(int);
766
767        The `class T' maches the `S<T>', leaving no template headers
768        corresponding to the `f'.  */
769     return tsk_none;
770   else if (n_template_parm_scopes > n_class_scopes + 1)
771     /* We've got too many template headers.  For example:
772
773          template <> template <class T> void f (T);
774
775        There need to be more enclosing classes.  */
776     return tsk_excessive_parms;
777   else
778     /* This must be a template.  It's of the form:
779
780          template <class T> template <class U> void S<T>::f(U);
781
782        This is a specialization if the innermost level was a
783        specialization; otherwise it's just a definition of the
784        template.  */
785     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
786 }
787
788 void
789 set_class_shadows (tree shadows)
790 {
791   class_binding_level->class_shadowed = shadows;
792 }
793
794 /* Enter a new binding level.
795    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
796    not for that of tags.  */
797
798 void
799 pushlevel (int tag_transparent)
800 {
801   struct cp_binding_level *newlevel;
802
803   if (cfun && !doing_semantic_analysis_p ())
804     return;
805
806   /* Reuse or create a struct for this binding level.  */
807 #if defined(DEBUG_BINDING_LEVELS)
808   if (0)
809 #else /* !defined(DEBUG_BINDING_LEVELS) */
810   if (free_binding_level)
811 #endif /* !defined(DEBUG_BINDING_LEVELS) */
812     {
813       newlevel = free_binding_level;
814       free_binding_level = free_binding_level->level_chain;
815     }
816   else
817     newlevel = make_binding_level ();
818
819   push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
820   keep_next_level_flag = 0;
821 }
822
823 /* We're defining an object of type TYPE.  If it needs a cleanup, but
824    we're not allowed to add any more objects with cleanups to the current
825    scope, create a new binding level.  */
826
827 void
828 maybe_push_cleanup_level (tree type)
829 {
830   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
831       && current_binding_level->more_cleanups_ok == 0)
832     {
833       keep_next_level (2);
834       pushlevel (1);
835       clear_last_expr ();
836       add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
837     }
838 }
839   
840 /* Enter a new scope.  The KIND indicates what kind of scope is being
841    created.  */
842
843 void
844 begin_scope (scope_kind sk)
845 {
846   pushlevel (0);
847
848   switch (sk)
849     {
850     case sk_block:
851       break;
852
853     case sk_try:
854       current_binding_level->is_try_scope = 1;
855       break;
856
857     case sk_catch:
858       current_binding_level->is_catch_scope = 1;
859       break;
860
861     case sk_for:
862       current_binding_level->is_for_scope = 1;
863       break;
864
865     case sk_template_spec:
866       current_binding_level->template_spec_p = 1;
867       /* Fall through.  */
868
869     case sk_template_parms:
870       current_binding_level->template_parms_p = 1;
871       break;
872
873     default:
874       abort ();
875     }
876 }
877
878 /* Exit the current scope.  */
879
880 void
881 finish_scope (void)
882 {
883   poplevel (0, 0, 0);
884 }
885
886 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
887    level at which this declaration is being bound.  */
888
889 static void
890 push_binding (tree id, tree decl, cxx_scope* level)
891 {
892    cxx_binding *binding = cxx_binding_make (decl, NULL);
893
894   /* Now, fill in the binding information.  */
895   binding->previous = IDENTIFIER_BINDING (id);
896   BINDING_SCOPE (binding) = level;
897   INHERITED_VALUE_BINDING_P (binding) = 0;
898   LOCAL_BINDING_P (binding) = (level != class_binding_level);
899
900   /* And put it on the front of the list of bindings for ID.  */
901   IDENTIFIER_BINDING (id) = binding;
902 }
903
904 /* ID is already bound in the current scope.  But, DECL is an
905    additional binding for ID in the same scope.  This is the `struct
906    stat' hack whereby a non-typedef class-name or enum-name can be
907    bound at the same level as some other kind of entity.  It's the
908    responsibility of the caller to check that inserting this name is
909    valid here.  Returns nonzero if the new binding was successful.  */
910 static int
911 add_binding (tree id, tree decl)
912 {
913   cxx_binding *binding = IDENTIFIER_BINDING (id);
914   int ok = 1;
915
916   timevar_push (TV_NAME_LOOKUP);
917   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
918     /* The new name is the type name.  */
919     BINDING_TYPE (binding) = decl;
920   else if (!BINDING_VALUE (binding))
921     /* This situation arises when push_class_level_binding moves an
922        inherited type-binding out of the way to make room for a new
923        value binding.  */
924     BINDING_VALUE (binding) = decl;
925   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
926            && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
927     {
928       /* The old binding was a type name.  It was placed in
929          BINDING_VALUE because it was thought, at the point it was
930          declared, to be the only entity with such a name.  Move the
931          type name into the type slot; it is now hidden by the new
932          binding.  */
933       BINDING_TYPE (binding) = BINDING_VALUE (binding);
934       BINDING_VALUE (binding) = decl;
935       INHERITED_VALUE_BINDING_P (binding) = 0;
936     }
937   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
938            && TREE_CODE (decl) == TYPE_DECL
939            && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
940            && (same_type_p (TREE_TYPE (decl),
941                             TREE_TYPE (BINDING_VALUE (binding)))
942                /* If either type involves template parameters, we must
943                   wait until instantiation.  */
944                || uses_template_parms (TREE_TYPE (decl))
945                || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
946     /* We have two typedef-names, both naming the same type to have
947        the same name.  This is OK because of:
948
949          [dcl.typedef]
950
951          In a given scope, a typedef specifier can be used to redefine
952          the name of any type declared in that scope to refer to the
953          type to which it already refers.  */
954     ok = 0;
955   /* There can be two block-scope declarations of the same variable,
956      so long as they are `extern' declarations.  */
957   else if (TREE_CODE (decl) == VAR_DECL
958            && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
959            && DECL_EXTERNAL (decl)
960            && DECL_EXTERNAL (BINDING_VALUE (binding)))
961     {
962       duplicate_decls (decl, BINDING_VALUE (binding));
963       ok = 0;
964     }
965   else
966     {
967       error ("declaration of `%#D'", decl);
968       cp_error_at ("conflicts with previous declaration `%#D'",
969                    BINDING_VALUE (binding));
970       ok = 0;
971     }
972
973   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
974 }
975
976 /* Add DECL to the list of things declared in B.  */
977
978 static void
979 add_decl_to_level (tree decl, 
980                    struct cp_binding_level* b)
981 {
982   if (TREE_CODE (decl) == NAMESPACE_DECL 
983       && !DECL_NAMESPACE_ALIAS (decl))
984     {
985       TREE_CHAIN (decl) = b->namespaces;
986       b->namespaces = decl;
987     }
988   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
989     {
990       TREE_CHAIN (decl) = b->vtables;
991       b->vtables = decl;
992     }
993   else       
994     {
995       /* We build up the list in reverse order, and reverse it later if
996          necessary.  */
997       TREE_CHAIN (decl) = b->names;
998       b->names = decl;
999       b->names_size++;
1000
1001       /* If appropriate, add decl to separate list of statics */
1002       if (b->namespace_p)
1003         if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1004             || (TREE_CODE (decl) == FUNCTION_DECL
1005                 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1006           VARRAY_PUSH_TREE (b->static_decls, decl);
1007     }
1008 }
1009
1010 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1011    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1012    doesn't really belong to this binding level, that it got here
1013    through a using-declaration.  */
1014
1015 void
1016 push_local_binding (tree id, tree decl, int flags)
1017 {
1018   struct cp_binding_level *b;
1019
1020   /* Skip over any local classes.  This makes sense if we call
1021      push_local_binding with a friend decl of a local class.  */
1022   b = current_binding_level;
1023   while (b->parm_flag == 2)
1024     b = b->level_chain;
1025
1026   if (lookup_name_current_level (id))
1027     {
1028       /* Supplement the existing binding.  */
1029       if (!add_binding (id, decl))
1030         /* It didn't work.  Something else must be bound at this
1031            level.  Do not add DECL to the list of things to pop
1032            later.  */
1033         return;
1034     }
1035   else
1036     /* Create a new binding.  */
1037     push_binding (id, decl, b);
1038
1039   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1040     /* We must put the OVERLOAD into a TREE_LIST since the
1041        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1042        decls that got here through a using-declaration.  */
1043     decl = build_tree_list (NULL_TREE, decl);
1044
1045   /* And put DECL on the list of things declared by the current
1046      binding level.  */
1047   add_decl_to_level (decl, b);
1048 }
1049
1050 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1051    binding was successful.  */
1052
1053 int
1054 push_class_binding (tree id, tree decl)
1055 {
1056   int result = 1;
1057   cxx_binding *binding = IDENTIFIER_BINDING (id);
1058   tree context;
1059
1060   timevar_push (TV_NAME_LOOKUP);
1061   /* Note that we declared this value so that we can issue an error if
1062      this is an invalid redeclaration of a name already used for some
1063      other purpose.  */
1064   note_name_declared_in_class (id, decl);
1065
1066   if (binding && BINDING_SCOPE (binding) == class_binding_level)
1067     /* Supplement the existing binding.  */
1068     result = add_binding (id, decl);
1069   else
1070     /* Create a new binding.  */
1071     push_binding (id, decl, class_binding_level);
1072
1073   /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1074      class-level declaration.  Note that we do not use DECL here
1075      because of the possibility of the `struct stat' hack; if DECL is
1076      a class-name or enum-name we might prefer a field-name, or some
1077      such.  */
1078   IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1079
1080   /* If this is a binding from a base class, mark it as such.  */
1081   binding = IDENTIFIER_BINDING (id);
1082   if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1083     {
1084       if (TREE_CODE (decl) == OVERLOAD)
1085         context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1086       else
1087         {
1088           my_friendly_assert (DECL_P (decl), 0);
1089           context = context_for_name_lookup (decl);
1090         }
1091
1092       if (is_properly_derived_from (current_class_type, context))
1093         INHERITED_VALUE_BINDING_P (binding) = 1;
1094       else
1095         INHERITED_VALUE_BINDING_P (binding) = 0;
1096     }
1097   else if (BINDING_VALUE (binding) == decl)
1098     /* We only encounter a TREE_LIST when push_class_decls detects an
1099        ambiguity.  Such an ambiguity can be overridden by a definition
1100        in this class.  */
1101     INHERITED_VALUE_BINDING_P (binding) = 1;
1102
1103   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1104 }
1105
1106 /* Remove the binding for DECL which should be the innermost binding
1107    for ID.  */
1108
1109 static void
1110 pop_binding (tree id, tree decl)
1111 {
1112   cxx_binding *binding;
1113
1114   if (id == NULL_TREE)
1115     /* It's easiest to write the loops that call this function without
1116        checking whether or not the entities involved have names.  We
1117        get here for such an entity.  */
1118     return;
1119
1120   /* Get the innermost binding for ID.  */
1121   binding = IDENTIFIER_BINDING (id);
1122
1123   /* The name should be bound.  */
1124   my_friendly_assert (binding != NULL, 0);
1125
1126   /* The DECL will be either the ordinary binding or the type
1127      binding for this identifier.  Remove that binding.  */
1128   if (BINDING_VALUE (binding) == decl)
1129     BINDING_VALUE (binding) = NULL_TREE;
1130   else if (BINDING_TYPE (binding) == decl)
1131     BINDING_TYPE (binding) = NULL_TREE;
1132   else
1133     abort ();
1134
1135   if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1136     {
1137       /* We're completely done with the innermost binding for this
1138          identifier.  Unhook it from the list of bindings.  */
1139       IDENTIFIER_BINDING (id) = binding->previous;
1140
1141       /* Add it to the free list.  */
1142       cxx_binding_free (binding);
1143
1144       /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1145          it.  */
1146       BINDING_SCOPE (binding) = NULL;
1147     }
1148 }
1149
1150 /* When a label goes out of scope, check to see if that label was used
1151    in a valid manner, and issue any appropriate warnings or errors.  */
1152
1153 static void
1154 pop_label (tree label, tree old_value)
1155 {
1156   if (!processing_template_decl && doing_semantic_analysis_p ())
1157     {
1158       if (DECL_INITIAL (label) == NULL_TREE)
1159         {
1160           cp_error_at ("label `%D' used but not defined", label);
1161           /* Avoid crashing later.  */
1162           define_label (input_filename, 1, DECL_NAME (label));
1163         }
1164       else if (warn_unused_label && !TREE_USED (label))
1165         cp_warning_at ("label `%D' defined but not used", label);
1166     }
1167
1168   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1169 }
1170
1171 /* At the end of a function, all labels declared within the function
1172    go out of scope.  BLOCK is the top-level block for the
1173    function.  */
1174
1175 static void
1176 pop_labels (tree block)
1177 {
1178   struct named_label_list *link;
1179
1180   /* Clear out the definitions of all label names, since their scopes
1181      end here.  */
1182   for (link = named_labels; link; link = link->next)
1183     {
1184       pop_label (link->label_decl, link->old_value);
1185       /* Put the labels into the "variables" of the top-level block,
1186          so debugger can see them.  */
1187       TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1188       BLOCK_VARS (block) = link->label_decl;
1189     }
1190
1191   named_labels = NULL;
1192 }
1193
1194 /* Exit a binding level.
1195    Pop the level off, and restore the state of the identifier-decl mappings
1196    that were in effect when this level was entered.
1197
1198    If KEEP == 1, this level had explicit declarations, so
1199    and create a "block" (a BLOCK node) for the level
1200    to record its declarations and subblocks for symbol table output.
1201
1202    If FUNCTIONBODY is nonzero, this level is the body of a function,
1203    so create a block as if KEEP were set and also clear out all
1204    label names.
1205
1206    If REVERSE is nonzero, reverse the order of decls before putting
1207    them into the BLOCK.  */
1208
1209 tree
1210 poplevel (int keep, int reverse, int functionbody)
1211 {
1212   register tree link;
1213   /* The chain of decls was accumulated in reverse order.
1214      Put it into forward order, just for cleanliness.  */
1215   tree decls;
1216   int tmp = functionbody;
1217   int real_functionbody;
1218   tree tags;
1219   tree subblocks;
1220   tree block = NULL_TREE;
1221   tree decl;
1222   int leaving_for_scope;
1223
1224   timevar_push (TV_NAME_LOOKUP);
1225   if (cfun && !doing_semantic_analysis_p ())
1226     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1227
1228   my_friendly_assert (current_binding_level->parm_flag != 2,
1229                       19990916);
1230
1231   real_functionbody = (current_binding_level->keep == 2
1232                        ? ((functionbody = 0), tmp) : functionbody);
1233   tags = functionbody >= 0 ? current_binding_level->tags : 0;
1234   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1235
1236   my_friendly_assert (!current_binding_level->class_shadowed,
1237                       19990414);
1238
1239   /* We used to use KEEP == 2 to indicate that the new block should go
1240      at the beginning of the list of blocks at this binding level,
1241      rather than the end.  This hack is no longer used.  */
1242   my_friendly_assert (keep == 0 || keep == 1, 0);
1243
1244   if (current_binding_level->keep == 1)
1245     keep = 1;
1246
1247   /* Any uses of undefined labels, and any defined labels, now operate
1248      under constraints of next binding contour.  */
1249   if (cfun && !functionbody)
1250     {
1251       struct cp_binding_level *level_chain;
1252       level_chain = current_binding_level->level_chain;
1253       if (level_chain)
1254         {
1255           struct named_label_use_list *uses;
1256           struct named_label_list *labels;
1257           for (labels = named_labels; labels; labels = labels->next)
1258             if (labels->binding_level == current_binding_level)
1259               {
1260                 tree decl;
1261                 if (current_binding_level->is_try_scope)
1262                   labels->in_try_scope = 1;
1263                 if (current_binding_level->is_catch_scope)
1264                   labels->in_catch_scope = 1;
1265                 for (decl = labels->names_in_scope; decl;
1266                      decl = TREE_CHAIN (decl))
1267                   if (decl_jump_unsafe (decl))
1268                     labels->bad_decls = tree_cons (NULL_TREE, decl,
1269                                                    labels->bad_decls);
1270                 labels->binding_level = level_chain;
1271                 labels->names_in_scope = level_chain->names;
1272               }
1273
1274           for (uses = named_label_uses; uses; uses = uses->next)
1275             if (uses->binding_level == current_binding_level)
1276               {
1277                 uses->binding_level = level_chain;
1278                 uses->names_in_scope = level_chain->names;
1279               }
1280         }
1281     }
1282
1283   /* Get the decls in the order they were written.
1284      Usually current_binding_level->names is in reverse order.
1285      But parameter decls were previously put in forward order.  */
1286
1287   if (reverse)
1288     current_binding_level->names
1289       = decls = nreverse (current_binding_level->names);
1290   else
1291     decls = current_binding_level->names;
1292
1293   /* Output any nested inline functions within this block
1294      if they weren't already output.  */
1295   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1296     if (TREE_CODE (decl) == FUNCTION_DECL
1297         && ! TREE_ASM_WRITTEN (decl)
1298         && DECL_INITIAL (decl) != NULL_TREE
1299         && TREE_ADDRESSABLE (decl)
1300         && decl_function_context (decl) == current_function_decl)
1301       {
1302         /* If this decl was copied from a file-scope decl
1303            on account of a block-scope extern decl,
1304            propagate TREE_ADDRESSABLE to the file-scope decl.  */
1305         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1306           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1307         else
1308           {
1309             push_function_context ();
1310             output_inline_function (decl);
1311             pop_function_context ();
1312           }
1313       }
1314
1315   /* When not in function-at-a-time mode, expand_end_bindings will
1316      warn about unused variables.  But, in function-at-a-time mode
1317      expand_end_bindings is not passed the list of variables in the
1318      current scope, and therefore no warning is emitted.  So, we
1319      explicitly warn here.  */
1320   if (!processing_template_decl)
1321     warn_about_unused_variables (getdecls ());
1322
1323   /* If there were any declarations or structure tags in that level,
1324      or if this level is a function body,
1325      create a BLOCK to record them for the life of this function.  */
1326   block = NULL_TREE;
1327   if (keep == 1 || functionbody)
1328     block = make_node (BLOCK);
1329   if (block != NULL_TREE)
1330     {
1331       BLOCK_VARS (block) = decls;
1332       BLOCK_SUBBLOCKS (block) = subblocks;
1333     }
1334
1335   /* In each subblock, record that this is its superior.  */
1336   if (keep >= 0)
1337     for (link = subblocks; link; link = TREE_CHAIN (link))
1338       BLOCK_SUPERCONTEXT (link) = block;
1339
1340   /* We still support the old for-scope rules, whereby the variables
1341      in a for-init statement were in scope after the for-statement
1342      ended.  We only use the new rules if flag_new_for_scope is
1343      nonzero.  */
1344   leaving_for_scope
1345     = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1346
1347   /* Remove declarations for all the DECLs in this level.  */
1348   for (link = decls; link; link = TREE_CHAIN (link))
1349     {
1350       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1351           && DECL_NAME (link))
1352         {
1353           cxx_binding *outer_binding
1354             = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1355           tree ns_binding;
1356
1357           if (!outer_binding)
1358             ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1359           else
1360             ns_binding = NULL_TREE;
1361
1362           if (outer_binding
1363               && (BINDING_SCOPE (outer_binding)
1364                   == current_binding_level->level_chain))
1365             /* We have something like:
1366
1367                  int i;
1368                  for (int i; ;);
1369
1370                and we are leaving the `for' scope.  There's no reason to
1371                keep the binding of the inner `i' in this case.  */
1372             pop_binding (DECL_NAME (link), link);
1373           else if ((outer_binding
1374                     && (TREE_CODE (BINDING_VALUE (outer_binding))
1375                         == TYPE_DECL))
1376                    || (ns_binding
1377                        && TREE_CODE (ns_binding) == TYPE_DECL))
1378             /* Here, we have something like:
1379
1380                  typedef int I;
1381
1382                  void f () {
1383                    for (int I; ;);
1384                  }
1385
1386                We must pop the for-scope binding so we know what's a
1387                type and what isn't.  */
1388             pop_binding (DECL_NAME (link), link);
1389           else
1390             {
1391               /* Mark this VAR_DECL as dead so that we can tell we left it
1392                  there only for backward compatibility.  */
1393               DECL_DEAD_FOR_LOCAL (link) = 1;
1394
1395               /* Keep track of what should of have happenned when we
1396                  popped the binding.  */
1397               if (outer_binding && BINDING_VALUE (outer_binding))
1398                 DECL_SHADOWED_FOR_VAR (link)
1399                   = BINDING_VALUE (outer_binding);
1400
1401               /* Add it to the list of dead variables in the next
1402                  outermost binding to that we can remove these when we
1403                  leave that binding.  */
1404               current_binding_level->level_chain->dead_vars_from_for
1405                 = tree_cons (NULL_TREE, link,
1406                              current_binding_level->level_chain->
1407                              dead_vars_from_for);
1408
1409               /* Although we don't pop the cxx_binding, we do clear
1410                  its BINDING_SCOPE since the level is going away now.  */
1411               BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1412             }
1413         }
1414       else
1415         {
1416           /* Remove the binding.  */
1417           decl = link;
1418           if (TREE_CODE (decl) == TREE_LIST)
1419             decl = TREE_VALUE (decl);
1420           if (DECL_P (decl))
1421             pop_binding (DECL_NAME (decl), decl);
1422           else if (TREE_CODE (decl) == OVERLOAD)
1423             pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1424           else
1425             abort ();
1426         }
1427     }
1428
1429   /* Remove declarations for any `for' variables from inner scopes
1430      that we kept around.  */
1431   for (link = current_binding_level->dead_vars_from_for;
1432        link; link = TREE_CHAIN (link))
1433     pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1434
1435   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1436   for (link = current_binding_level->type_shadowed;
1437        link; link = TREE_CHAIN (link))
1438     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1439
1440   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1441   for (link = current_binding_level->shadowed_labels;
1442        link;
1443        link = TREE_CHAIN (link))
1444     pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1445
1446   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1447      list if a `using' declaration put them there.  The debugging
1448      back-ends won't understand OVERLOAD, so we remove them here.
1449      Because the BLOCK_VARS are (temporarily) shared with
1450      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1451      popped all the bindings.  */
1452   if (block)
1453     {
1454       tree* d;
1455
1456       for (d = &BLOCK_VARS (block); *d; )
1457         {
1458           if (TREE_CODE (*d) == TREE_LIST)
1459             *d = TREE_CHAIN (*d);
1460           else
1461             d = &TREE_CHAIN (*d);
1462         }
1463     }
1464
1465   /* If the level being exited is the top level of a function,
1466      check over all the labels.  */
1467   if (functionbody)
1468     {
1469       /* Since this is the top level block of a function, the vars are
1470          the function's parameters.  Don't leave them in the BLOCK
1471          because they are found in the FUNCTION_DECL instead.  */
1472       BLOCK_VARS (block) = 0;
1473       pop_labels (block);
1474     }
1475
1476   tmp = current_binding_level->keep;
1477
1478   pop_binding_level ();
1479   if (functionbody)
1480     DECL_INITIAL (current_function_decl) = block;
1481   else if (block)
1482     current_binding_level->blocks
1483       = chainon (current_binding_level->blocks, block);
1484
1485   /* If we did not make a block for the level just exited,
1486      any blocks made for inner levels
1487      (since they cannot be recorded as subblocks in that level)
1488      must be carried forward so they will later become subblocks
1489      of something else.  */
1490   else if (subblocks)
1491     current_binding_level->blocks
1492       = chainon (current_binding_level->blocks, subblocks);
1493
1494   /* Each and every BLOCK node created here in `poplevel' is important
1495      (e.g. for proper debugging information) so if we created one
1496      earlier, mark it as "used".  */
1497   if (block)
1498     TREE_USED (block) = 1;
1499
1500   /* Take care of compiler's internal binding structures.  */
1501   if (tmp == 2)
1502     {
1503       tree scope_stmts;
1504
1505       scope_stmts
1506         = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1507       if (block)
1508         {
1509           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1510           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1511         }
1512
1513       block = poplevel (keep, reverse, functionbody);
1514     }
1515
1516   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1517 }
1518
1519 /* Delete the node BLOCK from the current binding level.
1520    This is used for the block inside a stmt expr ({...})
1521    so that the block can be reinserted where appropriate.  */
1522
1523 void
1524 delete_block (tree block)
1525 {
1526   tree t;
1527   if (current_binding_level->blocks == block)
1528     current_binding_level->blocks = TREE_CHAIN (block);
1529   for (t = current_binding_level->blocks; t;)
1530     {
1531       if (TREE_CHAIN (t) == block)
1532         TREE_CHAIN (t) = TREE_CHAIN (block);
1533       else
1534         t = TREE_CHAIN (t);
1535     }
1536   TREE_CHAIN (block) = NULL_TREE;
1537   /* Clear TREE_USED which is always set by poplevel.
1538      The flag is set again if insert_block is called.  */
1539   TREE_USED (block) = 0;
1540 }
1541
1542 /* Insert BLOCK at the end of the list of subblocks of the
1543    current binding level.  This is used when a BIND_EXPR is expanded,
1544    to handle the BLOCK node inside the BIND_EXPR.  */
1545
1546 void
1547 insert_block (tree block)
1548 {
1549   TREE_USED (block) = 1;
1550   current_binding_level->blocks
1551     = chainon (current_binding_level->blocks, block);
1552 }
1553
1554 /* Set the BLOCK node for the innermost scope
1555    (the one we are currently in).  */
1556
1557 void
1558 set_block (tree block ATTRIBUTE_UNUSED )
1559 {
1560   /* The RTL expansion machinery requires us to provide this callback,
1561      but it is not applicable in function-at-a-time mode.  */
1562   my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1563 }
1564
1565 /* Do a pushlevel for class declarations.  */
1566
1567 void
1568 pushlevel_class (void)
1569 {
1570   register struct cp_binding_level *newlevel;
1571
1572   /* Reuse or create a struct for this binding level.  */
1573 #if defined(DEBUG_BINDING_LEVELS)
1574   if (0)
1575 #else /* !defined(DEBUG_BINDING_LEVELS) */
1576   if (free_binding_level)
1577 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1578     {
1579       newlevel = free_binding_level;
1580       free_binding_level = free_binding_level->level_chain;
1581     }
1582   else
1583     newlevel = make_binding_level ();
1584
1585 #if defined(DEBUG_BINDING_LEVELS)
1586   is_class_level = 1;
1587 #endif /* defined(DEBUG_BINDING_LEVELS) */
1588
1589   push_binding_level (newlevel, 0, 0);
1590
1591   class_binding_level = current_binding_level;
1592   class_binding_level->parm_flag = 2;
1593   class_binding_level->this_class = current_class_type;
1594 }
1595
1596 /* ...and a poplevel for class declarations.  */
1597
1598 void
1599 poplevel_class (void)
1600 {
1601   register struct cp_binding_level *level = class_binding_level;
1602   tree shadowed;
1603
1604   timevar_push (TV_NAME_LOOKUP);
1605   my_friendly_assert (level != 0, 354);
1606
1607   /* If we're leaving a toplevel class, don't bother to do the setting
1608      of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1609      shouldn't even be used when current_class_type isn't set, and second,
1610      if we don't touch it here, we're able to use the cache effect if the
1611      next time we're entering a class scope, it is the same class.  */
1612   if (current_class_depth != 1)
1613     {
1614       struct cp_binding_level* b;
1615
1616       /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1617       for (shadowed = level->class_shadowed;
1618            shadowed;
1619            shadowed = TREE_CHAIN (shadowed))
1620         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1621
1622       /* Find the next enclosing class, and recreate
1623          IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1624       b = level->level_chain;
1625       while (b && b->parm_flag != 2)
1626         b = b->level_chain;
1627
1628       if (b)
1629         for (shadowed = b->class_shadowed;
1630              shadowed;
1631              shadowed = TREE_CHAIN (shadowed))
1632           {
1633             cxx_binding *binding;
1634             
1635             binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1636             while (binding && BINDING_SCOPE (binding) != b)
1637               binding = binding->previous;
1638
1639             if (binding)
1640               IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1641                 = BINDING_VALUE (binding);
1642           }
1643     }
1644   else
1645     /* Remember to save what IDENTIFIER's were bound in this scope so we
1646        can recover from cache misses.  */
1647     {
1648       previous_class_type = current_class_type;
1649       previous_class_values = class_binding_level->class_shadowed;
1650     }
1651   for (shadowed = level->type_shadowed;
1652        shadowed;
1653        shadowed = TREE_CHAIN (shadowed))
1654     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1655
1656   /* Remove the bindings for all of the class-level declarations.  */
1657   for (shadowed = level->class_shadowed;
1658        shadowed;
1659        shadowed = TREE_CHAIN (shadowed))
1660     pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1661
1662   /* Now, pop out of the binding level which we created up in the
1663      `pushlevel_class' routine.  */
1664 #if defined(DEBUG_BINDING_LEVELS)
1665   is_class_level = 1;
1666 #endif /* defined(DEBUG_BINDING_LEVELS) */
1667
1668   pop_binding_level ();
1669   timevar_pop (TV_NAME_LOOKUP);
1670 }
1671
1672 /* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1673    for any names in enclosing classes.  */
1674
1675 void
1676 clear_identifier_class_values (void)
1677 {
1678   tree t;
1679
1680   if (!class_binding_level)
1681     return;
1682
1683   for (t = class_binding_level->class_shadowed;
1684        t;
1685        t = TREE_CHAIN (t))
1686     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1687 }
1688
1689 /* Returns nonzero if T is a virtual function table.  */
1690
1691 int
1692 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1693 {
1694   return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1695 }
1696
1697 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1698    functions.  */
1699
1700 int
1701 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1702 {
1703   return (TREE_CODE (t) == TYPE_DECL
1704           && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1705           && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1706 }
1707
1708 /* Return the declarations that are members of the namespace NS.  */
1709
1710 tree
1711 cp_namespace_decls (tree ns)
1712 {
1713   return NAMESPACE_LEVEL (ns)->names;
1714 }
1715
1716 struct walk_globals_data {
1717   walk_globals_pred p;
1718   walk_globals_fn f;
1719   void *data;
1720 };
1721
1722 /* Walk the vtable declarations in NAMESPACE.  Whenever one is found
1723    for which P returns nonzero, call F with its address.  If any call
1724    to F returns a nonzero value, return a nonzero value.  */
1725
1726 static int
1727 walk_vtables_r (tree namespace, void* data)
1728 {
1729   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1730   walk_globals_fn f = wgd->f;
1731   void *d = wgd->data;
1732   tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1733   int result = 0;
1734
1735   for (; decl ; decl = TREE_CHAIN (decl))
1736     result |= (*f) (&decl, d);
1737
1738   return result;
1739 }
1740
1741 /* Walk the vtable declarations.  Whenever one is found for which P
1742    returns nonzero, call F with its address.  If any call to F
1743    returns a nonzero value, return a nonzero value.  */
1744 bool
1745 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1746 {    
1747   struct walk_globals_data wgd;
1748   wgd.p = p;    
1749   wgd.f = f;
1750   wgd.data = data;
1751
1752   return walk_namespaces (walk_vtables_r, &wgd);
1753 }
1754
1755 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1756    itself, calling F for each.  The DATA is passed to F as well.  */
1757
1758 static int
1759 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1760 {
1761   int result = 0;
1762   tree current = NAMESPACE_LEVEL (namespace)->namespaces;     
1763
1764   result |= (*f) (namespace, data);
1765
1766   for (; current; current = TREE_CHAIN (current))
1767     result |= walk_namespaces_r (current, f, data);
1768
1769   return result;
1770 }
1771
1772 /* Walk all the namespaces, calling F for each.  The DATA is passed to
1773    F as well.  */
1774
1775 int
1776 walk_namespaces (walk_namespaces_fn f, void* data)
1777 {
1778   return walk_namespaces_r (global_namespace, f, data);
1779 }
1780
1781 /* Walk the global declarations in NAMESPACE.  Whenever one is found
1782    for which P returns nonzero, call F with its address.  If any call
1783    to F returns a nonzero value, return a nonzero value.  */
1784
1785 static int
1786 walk_globals_r (tree namespace, void* data)
1787 {
1788   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1789   walk_globals_pred p = wgd->p;
1790   walk_globals_fn f = wgd->f;
1791   void *d = wgd->data;
1792   tree *t;
1793   int result = 0;
1794
1795   t = &NAMESPACE_LEVEL (namespace)->names;
1796
1797   while (*t)
1798     {
1799       tree glbl = *t;
1800
1801       if ((*p) (glbl, d))
1802         result |= (*f) (t, d);
1803
1804       /* If F changed *T, then *T still points at the next item to
1805          examine.  */
1806       if (*t == glbl)
1807         t = &TREE_CHAIN (*t);
1808     }
1809
1810   return result;
1811 }
1812
1813 /* Walk the global declarations.  Whenever one is found for which P
1814    returns true, call F with its address.  If any call to F
1815    returns true, return true.  */
1816
1817 bool
1818 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1819 {
1820   struct walk_globals_data wgd;
1821   wgd.p = p;
1822   wgd.f = f;
1823   wgd.data = data;
1824
1825   return walk_namespaces (walk_globals_r, &wgd);
1826 }
1827
1828 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1829    DATA is non-NULL, this is the last time we will call
1830    wrapup_global_declarations for this NAMESPACE.  */
1831
1832 int
1833 wrapup_globals_for_namespace (tree namespace, void* data)
1834 {
1835   struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1836   varray_type statics = level->static_decls;
1837   tree *vec = &VARRAY_TREE (statics, 0);
1838   int len = VARRAY_ACTIVE_SIZE (statics);
1839   int last_time = (data != 0);
1840
1841   if (last_time)
1842     {
1843       check_global_declarations (vec, len);
1844       return 0;
1845     }
1846
1847   /* Write out any globals that need to be output.  */
1848   return wrapup_global_declarations (vec, len);
1849 }
1850
1851 \f
1852 /* For debugging.  */
1853 static int no_print_functions = 0;
1854 static int no_print_builtins = 0;
1855
1856 void
1857 print_binding_level (struct cp_binding_level* lvl)
1858 {
1859   tree t;
1860   int i = 0, len;
1861   fprintf (stderr, " blocks=");
1862   fprintf (stderr, HOST_PTR_PRINTF, (void *) lvl->blocks);
1863   if (lvl->tag_transparent)
1864     fprintf (stderr, " tag-transparent");
1865   if (lvl->more_cleanups_ok)
1866     fprintf (stderr, " more-cleanups-ok");
1867   if (lvl->have_cleanups)
1868     fprintf (stderr, " have-cleanups");
1869   fprintf (stderr, "\n");
1870   if (lvl->names)
1871     {
1872       fprintf (stderr, " names:\t");
1873       /* We can probably fit 3 names to a line?  */
1874       for (t = lvl->names; t; t = TREE_CHAIN (t))
1875         {
1876           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1877             continue;
1878           if (no_print_builtins
1879               && (TREE_CODE (t) == TYPE_DECL)
1880               && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1881             continue;
1882
1883           /* Function decls tend to have longer names.  */
1884           if (TREE_CODE (t) == FUNCTION_DECL)
1885             len = 3;
1886           else
1887             len = 2;
1888           i += len;
1889           if (i > 6)
1890             {
1891               fprintf (stderr, "\n\t");
1892               i = len;
1893             }
1894           print_node_brief (stderr, "", t, 0);
1895           if (t == error_mark_node)
1896             break;
1897         }
1898       if (i)
1899         fprintf (stderr, "\n");
1900     }
1901   if (lvl->tags)
1902     {
1903       fprintf (stderr, " tags:\t");
1904       i = 0;
1905       for (t = lvl->tags; t; t = TREE_CHAIN (t))
1906         {
1907           if (TREE_PURPOSE (t) == NULL_TREE)
1908             len = 3;
1909           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1910             len = 2;
1911           else
1912             len = 4;
1913           i += len;
1914           if (i > 5)
1915             {
1916               fprintf (stderr, "\n\t");
1917               i = len;
1918             }
1919           if (TREE_PURPOSE (t) == NULL_TREE)
1920             {
1921               print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1922               fprintf (stderr, ">");
1923             }
1924           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1925             print_node_brief (stderr, "", TREE_VALUE (t), 0);
1926           else
1927             {
1928               print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1929               print_node_brief (stderr, "", TREE_VALUE (t), 0);
1930               fprintf (stderr, ">");
1931             }
1932         }
1933       if (i)
1934         fprintf (stderr, "\n");
1935     }
1936   if (lvl->class_shadowed)
1937     {
1938       fprintf (stderr, " class-shadowed:");
1939       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1940         {
1941           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1942         }
1943       fprintf (stderr, "\n");
1944     }
1945   if (lvl->type_shadowed)
1946     {
1947       fprintf (stderr, " type-shadowed:");
1948       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1949         {
1950           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1951         }
1952       fprintf (stderr, "\n");
1953     }
1954 }
1955
1956 void
1957 print_other_binding_stack (struct cp_binding_level *stack)
1958 {
1959   struct cp_binding_level *level;
1960   for (level = stack; level != global_binding_level; level = level->level_chain)
1961     {
1962       fprintf (stderr, "binding level ");
1963       fprintf (stderr, HOST_PTR_PRINTF, (void *) level);
1964       fprintf (stderr, "\n");
1965       print_binding_level (level);
1966     }
1967 }
1968
1969 void
1970 print_binding_stack (void)
1971 {
1972   struct cp_binding_level *b;
1973   fprintf (stderr, "current_binding_level=");
1974   fprintf (stderr, HOST_PTR_PRINTF, (void *) current_binding_level);
1975   fprintf (stderr, "\nclass_binding_level=");
1976   fprintf (stderr, HOST_PTR_PRINTF, (void *) class_binding_level);
1977   fprintf (stderr, "\nglobal_binding_level=");
1978   fprintf (stderr, HOST_PTR_PRINTF, (void *) global_binding_level);
1979   fprintf (stderr, "\n");
1980   if (class_binding_level)
1981     {
1982       for (b = class_binding_level; b; b = b->level_chain)
1983         if (b == current_binding_level)
1984           break;
1985       if (b)
1986         b = class_binding_level;
1987       else
1988         b = current_binding_level;
1989     }
1990   else
1991     b = current_binding_level;
1992   print_other_binding_stack (b);
1993   fprintf (stderr, "global:\n");
1994   print_binding_level (global_binding_level);
1995 }
1996
1997 /* Namespace binding access routines: The namespace_bindings field of
1998    the identifier is polymorphic, with three possible values:
1999    NULL_TREE, a list of "cxx_binding"s.  */
2000
2001 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2002    select a name that is unique to this compilation unit.  */
2003
2004 void
2005 push_namespace (tree name)
2006 {
2007   tree d = NULL_TREE;
2008   int need_new = 1;
2009   int implicit_use = 0;
2010   int global = 0;
2011
2012   timevar_push (TV_NAME_LOOKUP);
2013   
2014   if (!global_namespace)
2015     {
2016       /* This must be ::.  */
2017       my_friendly_assert (name == get_identifier ("::"), 377);
2018       global = 1;
2019     }
2020   else if (!name)
2021     {
2022       /* The name of anonymous namespace is unique for the translation
2023          unit.  */
2024       if (!anonymous_namespace_name)
2025         anonymous_namespace_name = get_file_function_name ('N');
2026       name = anonymous_namespace_name;
2027       d = IDENTIFIER_NAMESPACE_VALUE (name);
2028       if (d)
2029         /* Reopening anonymous namespace.  */
2030         need_new = 0;
2031       implicit_use = 1;
2032     }
2033   else
2034     {
2035       /* Check whether this is an extended namespace definition.  */
2036       d = IDENTIFIER_NAMESPACE_VALUE (name);
2037       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2038         {
2039           need_new = 0;
2040           if (DECL_NAMESPACE_ALIAS (d))
2041             {
2042               error ("namespace alias `%D' not allowed here, assuming `%D'",
2043                         d, DECL_NAMESPACE_ALIAS (d));
2044               d = DECL_NAMESPACE_ALIAS (d);
2045             }
2046         }
2047     }
2048
2049   if (need_new)
2050     {
2051       /* Make a new namespace, binding the name to it.  */
2052       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2053       /* The global namespace is not pushed, and the global binding
2054          level is set elsewhere.  */
2055       if (!global)
2056         {
2057           DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2058           d = pushdecl (d);
2059           pushlevel (0);
2060           declare_namespace_level ();
2061           NAMESPACE_LEVEL (d) = current_binding_level;
2062           VARRAY_TREE_INIT (current_binding_level->static_decls,
2063                             name != std_identifier ? 10 : 200,
2064                             "Static declarations");
2065         }
2066     }
2067   else
2068     resume_binding_level (NAMESPACE_LEVEL (d));
2069
2070   if (implicit_use)
2071     do_using_directive (d);
2072   /* Enter the name space.  */
2073   current_namespace = d;
2074
2075   timevar_pop (TV_NAME_LOOKUP);
2076 }
2077
2078 /* Pop from the scope of the current namespace.  */
2079
2080 void
2081 pop_namespace (void)
2082 {
2083   my_friendly_assert (current_namespace != global_namespace, 20010801);
2084   current_namespace = CP_DECL_CONTEXT (current_namespace);
2085   /* The binding level is not popped, as it might be re-opened later.  */
2086   suspend_binding_level ();
2087 }
2088
2089 /* Push into the scope of the namespace NS, even if it is deeply
2090    nested within another namespace.  */
2091
2092 void
2093 push_nested_namespace (tree ns)
2094 {
2095   if (ns == global_namespace)
2096     push_to_top_level ();
2097   else
2098     {
2099       push_nested_namespace (CP_DECL_CONTEXT (ns));
2100       push_namespace (DECL_NAME (ns));
2101     }
2102 }
2103
2104 /* Pop back from the scope of the namespace NS, which was previously
2105    entered with push_nested_namespace.  */
2106
2107 void
2108 pop_nested_namespace (tree ns)
2109 {
2110   timevar_push (TV_NAME_LOOKUP);
2111   while (ns != global_namespace)
2112     {
2113       pop_namespace ();
2114       ns = CP_DECL_CONTEXT (ns);
2115     }
2116
2117   pop_from_top_level ();
2118   timevar_pop (TV_NAME_LOOKUP);
2119 }
2120
2121 \f
2122 /* Allocate storage for saving a C++ binding.  */
2123 #define cxx_saved_binding_make() \
2124   (ggc_alloc (sizeof (cxx_saved_binding)))
2125
2126 struct cxx_saved_binding GTY(())
2127 {
2128   /* Link that chains saved C++ bindings for a given name into a stack.  */
2129   cxx_saved_binding *previous;
2130   /* The name of the current binding.  */
2131   tree identifier;
2132   /* The binding we're saving.  */
2133   cxx_binding *binding;
2134   tree class_value;
2135   tree real_type_value;
2136 };
2137
2138 /* Subroutines for reverting temporarily to top-level for instantiation
2139    of templates and such.  We actually need to clear out the class- and
2140    local-value slots of all identifiers, so that only the global values
2141    are at all visible.  Simply setting current_binding_level to the global
2142    scope isn't enough, because more binding levels may be pushed.  */
2143 struct saved_scope *scope_chain;
2144
2145 static cxx_saved_binding *
2146 store_bindings (tree names, cxx_saved_binding *old_bindings)
2147 {
2148   tree t;
2149   cxx_saved_binding *search_bindings = old_bindings;
2150
2151   timevar_push (TV_NAME_LOOKUP);
2152   for (t = names; t; t = TREE_CHAIN (t))
2153     {
2154       tree id;
2155       cxx_saved_binding *saved;
2156       cxx_saved_binding *t1;
2157
2158       if (TREE_CODE (t) == TREE_LIST)
2159         id = TREE_PURPOSE (t);
2160       else
2161         id = DECL_NAME (t);
2162
2163       if (!id
2164           /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2165              we have no IDENTIFIER_BINDING if we have left the class
2166              scope, but cached the class-level declarations.  */
2167           || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2168         continue;
2169
2170       for (t1 = search_bindings; t1; t1 = t1->previous)
2171         if (t1->identifier == id)
2172           goto skip_it;
2173
2174       my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2175       saved = cxx_saved_binding_make ();
2176       saved->previous = old_bindings;
2177       saved->identifier = id;
2178       saved->binding = IDENTIFIER_BINDING (id);
2179       saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2180       saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2181       IDENTIFIER_BINDING (id) = NULL;
2182       IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2183       old_bindings = saved;
2184     skip_it:
2185       ;
2186     }
2187   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2188 }
2189
2190 void
2191 maybe_push_to_top_level (int pseudo)
2192 {
2193   struct saved_scope *s;
2194   struct cp_binding_level *b;
2195   cxx_saved_binding *old_bindings;
2196   int need_pop;
2197
2198   timevar_push (TV_NAME_LOOKUP);
2199   s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2200
2201   b = scope_chain ? current_binding_level : 0;
2202
2203   /* If we're in the middle of some function, save our state.  */
2204   if (cfun)
2205     {
2206       need_pop = 1;
2207       push_function_context_to (NULL_TREE);
2208     }
2209   else
2210     need_pop = 0;
2211
2212   old_bindings = NULL;
2213   if (scope_chain && previous_class_type)
2214     old_bindings = store_bindings (previous_class_values, old_bindings);
2215
2216   /* Have to include global_binding_level, because class-level decls
2217      aren't listed anywhere useful.  */
2218   for (; b; b = b->level_chain)
2219     {
2220       tree t;
2221
2222       /* Template IDs are inserted into the global level. If they were
2223          inserted into namespace level, finish_file wouldn't find them
2224          when doing pending instantiations. Therefore, don't stop at
2225          namespace level, but continue until :: .  */
2226       if (b == global_binding_level || (pseudo && b->template_parms_p))
2227         break;
2228
2229       old_bindings = store_bindings (b->names, old_bindings);
2230       /* We also need to check class_shadowed to save class-level type
2231          bindings, since pushclass doesn't fill in b->names.  */
2232       if (b->parm_flag == 2)
2233         old_bindings = store_bindings (b->class_shadowed, old_bindings);
2234
2235       /* Unwind type-value slots back to top level.  */
2236       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2237         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2238     }
2239   s->prev = scope_chain;
2240   s->old_bindings = old_bindings;
2241   s->bindings = b;
2242   s->need_pop_function_context = need_pop;
2243   s->function_decl = current_function_decl;
2244   s->last_parms = last_function_parms;
2245   s->check_access = flag_access_control;
2246
2247   scope_chain = s;
2248   current_function_decl = NULL_TREE;
2249   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2250   current_lang_name = lang_name_cplusplus;
2251   current_namespace = global_namespace;
2252   timevar_pop (TV_NAME_LOOKUP);
2253 }
2254
2255 void
2256 push_to_top_level (void)
2257 {
2258   maybe_push_to_top_level (0);
2259 }
2260
2261 void
2262 pop_from_top_level (void)
2263 {
2264   struct saved_scope *s = scope_chain;
2265   cxx_saved_binding *saved;
2266
2267   timevar_push (TV_NAME_LOOKUP); 
2268   /* Clear out class-level bindings cache.  */
2269   if (previous_class_type)
2270     invalidate_class_lookup_cache ();
2271
2272   current_lang_base = 0;
2273
2274   scope_chain = s->prev;
2275   for (saved = s->old_bindings; saved; saved = saved->previous)
2276     {
2277       tree id = saved->identifier;
2278
2279       IDENTIFIER_BINDING (id) = saved->binding;
2280       IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2281       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2282     }
2283
2284   /* If we were in the middle of compiling a function, restore our
2285      state.  */
2286   if (s->need_pop_function_context)
2287     pop_function_context_from (NULL_TREE);
2288   current_function_decl = s->function_decl;
2289   last_function_parms = s->last_parms;
2290   timevar_pop (TV_NAME_LOOKUP);
2291 }
2292 \f
2293 /* Push a definition of struct, union or enum tag "name".
2294    into binding_level "b".   "type" should be the type node,
2295    We assume that the tag "name" is not already defined.
2296
2297    Note that the definition may really be just a forward reference.
2298    In that case, the TYPE_SIZE will be a NULL_TREE.
2299
2300    C++ gratuitously puts all these tags in the name space.  */
2301
2302 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2303    record the shadowed value for this binding contour.  TYPE is
2304    the type that ID maps to.  */
2305
2306 static void
2307 set_identifier_type_value_with_scope (tree id, 
2308                                       tree type, 
2309                                       struct cp_binding_level* b)
2310 {
2311   if (!b->namespace_p)
2312     {
2313       /* Shadow the marker, not the real thing, so that the marker
2314          gets restored later.  */
2315       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2316       b->type_shadowed
2317         = tree_cons (id, old_type_value, b->type_shadowed);
2318     }
2319   else
2320     {
2321       cxx_binding *binding =
2322          binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2323       BINDING_TYPE (binding) = type;
2324       /* Store marker instead of real type.  */
2325       type = global_type_node;
2326     }
2327   SET_IDENTIFIER_TYPE_VALUE (id, type);
2328 }
2329
2330 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2331
2332 void
2333 set_identifier_type_value (tree id, tree type)
2334 {
2335   set_identifier_type_value_with_scope (id, type, current_binding_level);
2336 }
2337
2338 /* Return the type associated with id.  */
2339
2340 tree
2341 identifier_type_value (tree id)
2342 {
2343   timevar_push (TV_NAME_LOOKUP);
2344   /* There is no type with that name, anywhere.  */
2345   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2346     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2347   /* This is not the type marker, but the real thing.  */
2348   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2349     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2350   /* Have to search for it. It must be on the global level, now.
2351      Ask lookup_name not to return non-types. */
2352   id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2353   if (id)
2354     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2355   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2356 }
2357
2358 /* Pop off extraneous binding levels left over due to syntax errors.
2359
2360    We don't pop past namespaces, as they might be valid.  */
2361
2362 void
2363 pop_everything (void)
2364 {
2365 #ifdef DEBUG_BINDING_LEVELS
2366   fprintf (stderr, "XXX entering pop_everything ()\n");
2367 #endif
2368   while (!toplevel_bindings_p ())
2369     {
2370       if (current_binding_level->parm_flag == 2)
2371         pop_nested_class ();
2372       else
2373         poplevel (0, 0, 0);
2374     }
2375 #ifdef DEBUG_BINDING_LEVELS
2376   fprintf (stderr, "XXX leaving pop_everything ()\n");
2377 #endif
2378 }
2379
2380 /* The type TYPE is being declared.  If it is a class template, or a
2381    specialization of a class template, do any processing required and
2382    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2383    being declared a friend.  B is the binding level at which this TYPE
2384    should be bound.
2385
2386    Returns the TYPE_DECL for TYPE, which may have been altered by this
2387    processing.  */
2388
2389 static tree
2390 maybe_process_template_type_declaration (tree type, 
2391                                          int globalize, 
2392                                          struct cp_binding_level* b)
2393 {
2394   tree decl = TYPE_NAME (type);
2395
2396   if (processing_template_parmlist)
2397     /* You can't declare a new template type in a template parameter
2398        list.  But, you can declare a non-template type:
2399
2400          template <class A*> struct S;
2401
2402        is a forward-declaration of `A'.  */
2403     ;
2404   else
2405     {
2406       maybe_check_template_type (type);
2407
2408       my_friendly_assert (IS_AGGR_TYPE (type)
2409                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2410
2411
2412       if (processing_template_decl)
2413         {
2414           /* This may change after the call to
2415              push_template_decl_real, but we want the original value.  */
2416           tree name = DECL_NAME (decl);
2417
2418           decl = push_template_decl_real (decl, globalize);
2419           /* If the current binding level is the binding level for the
2420              template parameters (see the comment in
2421              begin_template_parm_list) and the enclosing level is a class
2422              scope, and we're not looking at a friend, push the
2423              declaration of the member class into the class scope.  In the
2424              friend case, push_template_decl will already have put the
2425              friend into global scope, if appropriate.  */
2426           if (TREE_CODE (type) != ENUMERAL_TYPE
2427               && !globalize && b->template_parms_p
2428               && b->level_chain->parm_flag == 2)
2429             {
2430               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2431               /* Put this tag on the list of tags for the class, since
2432                  that won't happen below because B is not the class
2433                  binding level, but is instead the pseudo-global level.  */
2434               b->level_chain->tags =
2435                 tree_cons (name, type, b->level_chain->tags);
2436               if (!COMPLETE_TYPE_P (current_class_type))
2437                 {
2438                   maybe_add_class_template_decl_list (current_class_type,
2439                                                       type, /*friend_p=*/0);
2440                   CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2441                 }
2442             }
2443         }
2444     }
2445
2446   return decl;
2447 }
2448
2449 /* In C++, you don't have to write `struct S' to refer to `S'; you
2450    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2451    if the user had written `typedef struct S S'.  Create and return
2452    the TYPE_DECL for TYPE.  */
2453
2454 tree
2455 create_implicit_typedef (tree name, tree type)
2456 {
2457   tree decl;
2458
2459   decl = build_decl (TYPE_DECL, name, type);
2460   DECL_ARTIFICIAL (decl) = 1;
2461   /* There are other implicit type declarations, like the one *within*
2462      a class that allows you to write `S::S'.  We must distinguish
2463      amongst these.  */
2464   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2465   TYPE_NAME (type) = decl;
2466
2467   return decl;
2468 }
2469
2470 /* Remember a local name for name-mangling purposes.  */
2471
2472 static void
2473 push_local_name (tree decl)
2474 {
2475   size_t i, nelts;
2476   tree t, name;
2477
2478   timevar_push (TV_NAME_LOOKUP);
2479   if (!local_names)
2480     VARRAY_TREE_INIT (local_names, 8, "local_names");
2481
2482   name = DECL_NAME (decl);
2483
2484   nelts = VARRAY_ACTIVE_SIZE (local_names);
2485   for (i = 0; i < nelts; i++)
2486     {
2487       t = VARRAY_TREE (local_names, i);
2488       if (DECL_NAME (t) == name)
2489         {
2490           if (!DECL_LANG_SPECIFIC (decl))
2491             retrofit_lang_decl (decl);
2492           DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2493           if (DECL_LANG_SPECIFIC (t))
2494             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2495           else
2496             DECL_DISCRIMINATOR (decl) = 1;
2497
2498           VARRAY_TREE (local_names, i) = decl;
2499           timevar_pop (TV_NAME_LOOKUP);
2500           return;
2501         }
2502     }
2503
2504   VARRAY_PUSH_TREE (local_names, decl);
2505   timevar_pop (TV_NAME_LOOKUP);
2506 }
2507
2508 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2509    Normally put it into the inner-most non-tag-transparent scope,
2510    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2511    The latter is needed for implicit declarations.  */
2512
2513 void
2514 pushtag (tree name, tree type, int globalize)
2515 {
2516   register struct cp_binding_level *b;
2517
2518   timevar_push (TV_NAME_LOOKUP);
2519   b = current_binding_level;
2520   while (b->tag_transparent
2521          || (b->parm_flag == 2
2522              && (globalize
2523                  /* We may be defining a new type in the initializer
2524                     of a static member variable. We allow this when
2525                     not pedantic, and it is particularly useful for
2526                     type punning via an anonymous union.  */
2527                  || COMPLETE_TYPE_P (b->this_class))))
2528     b = b->level_chain;
2529
2530   b->tags = tree_cons (name, type, b->tags);
2531
2532   if (name)
2533     {
2534       /* Do C++ gratuitous typedefing.  */
2535       if (IDENTIFIER_TYPE_VALUE (name) != type)
2536         {
2537           register tree d = NULL_TREE;
2538           int in_class = 0;
2539           tree context = TYPE_CONTEXT (type);
2540
2541           if (! context)
2542             {
2543               tree cs = current_scope ();
2544
2545               if (! globalize)
2546                 context = cs;
2547               else if (cs != NULL_TREE && TYPE_P (cs))
2548                 /* When declaring a friend class of a local class, we want
2549                    to inject the newly named class into the scope
2550                    containing the local class, not the namespace scope.  */
2551                 context = decl_function_context (get_type_decl (cs));
2552             }
2553           if (!context)
2554             context = current_namespace;
2555
2556           if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2557               || b->parm_flag == 2)
2558             in_class = 1;
2559
2560           if (current_lang_name == lang_name_java)
2561             TYPE_FOR_JAVA (type) = 1;
2562
2563           d = create_implicit_typedef (name, type);
2564           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2565           if (! in_class)
2566             set_identifier_type_value_with_scope (name, type, b);
2567
2568           d = maybe_process_template_type_declaration (type,
2569                                                        globalize, b);
2570
2571           if (b->parm_flag == 2)
2572             {
2573               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2574                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2575                    class.  But if it's a member template class, we
2576                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2577                    is done later.  */
2578                 finish_member_declaration (d);
2579               else
2580                 pushdecl_class_level (d);
2581             }
2582           else
2583             d = pushdecl_with_scope (d, b);
2584
2585           /* FIXME what if it gets a name from typedef?  */
2586           if (ANON_AGGRNAME_P (name))
2587             DECL_IGNORED_P (d) = 1;
2588
2589           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2590
2591           /* If this is a local class, keep track of it.  We need this
2592              information for name-mangling, and so that it is possible to find
2593              all function definitions in a translation unit in a convenient
2594              way.  (It's otherwise tricky to find a member function definition
2595              it's only pointed to from within a local class.)  */
2596           if (TYPE_CONTEXT (type)
2597               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2598               && !processing_template_decl)
2599             VARRAY_PUSH_TREE (local_classes, type);
2600         }
2601       if (b->parm_flag == 2)
2602         {
2603           if (!COMPLETE_TYPE_P (current_class_type))
2604             {
2605               maybe_add_class_template_decl_list (current_class_type,
2606                                                   type, /*friend_p=*/0);
2607               CLASSTYPE_TAGS (current_class_type) = b->tags;
2608             }
2609         }
2610     }
2611
2612   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2613     /* Use the canonical TYPE_DECL for this node.  */
2614     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2615   else
2616     {
2617       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2618          will be the tagged type we just added to the current
2619          binding level.  This fake NULL-named TYPE_DECL node helps
2620          dwarfout.c to know when it needs to output a
2621          representation of a tagged type, and it also gives us a
2622          convenient place to record the "scope start" address for
2623          the tagged type.  */
2624
2625       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2626       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2627     }
2628   timevar_pop (TV_NAME_LOOKUP);
2629 }
2630
2631 /* Counter used to create anonymous type names.  */
2632
2633 static GTY(()) int anon_cnt;
2634
2635 /* Return an IDENTIFIER which can be used as a name for
2636    anonymous structs and unions.  */
2637
2638 tree
2639 make_anon_name (void)
2640 {
2641   char buf[32];
2642
2643   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2644   return get_identifier (buf);
2645 }
2646
2647 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2648    This keeps dbxout from getting confused.  */
2649
2650 void
2651 clear_anon_tags (void)
2652 {
2653   register struct cp_binding_level *b;
2654   register tree tags;
2655   static int last_cnt = 0;
2656
2657   /* Fast out if no new anon names were declared.  */
2658   if (last_cnt == anon_cnt)
2659     return;
2660
2661   b = current_binding_level;
2662   while (b->tag_transparent)
2663     b = b->level_chain;
2664   tags = b->tags;
2665   while (tags)
2666     {
2667       /* A NULL purpose means we have already processed all tags
2668          from here to the end of the list.  */
2669       if (TREE_PURPOSE (tags) == NULL_TREE)
2670         break;
2671       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2672         TREE_PURPOSE (tags) = NULL_TREE;
2673       tags = TREE_CHAIN (tags);
2674     }
2675   last_cnt = anon_cnt;
2676 }
2677 \f
2678 /* Subroutine of duplicate_decls: return truthvalue of whether
2679    or not types of these decls match.
2680
2681    For C++, we must compare the parameter list so that `int' can match
2682    `int&' in a parameter position, but `int&' is not confused with
2683    `const int&'.  */
2684
2685 int
2686 decls_match (tree newdecl, tree olddecl)
2687 {
2688   int types_match;
2689
2690   if (newdecl == olddecl)
2691     return 1;
2692
2693   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2694     /* If the two DECLs are not even the same kind of thing, we're not
2695        interested in their types.  */
2696     return 0;
2697
2698   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2699     {
2700       tree f1 = TREE_TYPE (newdecl);
2701       tree f2 = TREE_TYPE (olddecl);
2702       tree p1 = TYPE_ARG_TYPES (f1);
2703       tree p2 = TYPE_ARG_TYPES (f2);
2704
2705       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2706           && ! (DECL_EXTERN_C_P (newdecl)
2707                 && DECL_EXTERN_C_P (olddecl)))
2708         return 0;
2709
2710       if (TREE_CODE (f1) != TREE_CODE (f2))
2711         return 0;
2712
2713       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2714         {
2715           if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2716               && (DECL_BUILT_IN (olddecl)
2717 #ifndef NO_IMPLICIT_EXTERN_C
2718                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2719                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2720 #endif
2721               ))
2722             {
2723               types_match = self_promoting_args_p (p1);
2724               if (p1 == void_list_node)
2725                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2726             }
2727 #ifndef NO_IMPLICIT_EXTERN_C
2728           else if (p1 == NULL_TREE
2729                    && (DECL_EXTERN_C_P (olddecl)
2730                        && DECL_IN_SYSTEM_HEADER (olddecl)
2731                        && !DECL_CLASS_SCOPE_P (olddecl))
2732                    && (DECL_EXTERN_C_P (newdecl)
2733                        && DECL_IN_SYSTEM_HEADER (newdecl)
2734                        && !DECL_CLASS_SCOPE_P (newdecl)))
2735             {
2736               types_match = self_promoting_args_p (p2);
2737               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2738             }
2739 #endif
2740           else
2741             types_match = compparms (p1, p2);
2742         }
2743       else
2744         types_match = 0;
2745     }
2746   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2747     {
2748       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2749                                 DECL_TEMPLATE_PARMS (olddecl)))
2750         return 0;
2751
2752       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2753           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2754         return 0;
2755
2756       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2757         types_match = 1;
2758       else
2759         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2760                                    DECL_TEMPLATE_RESULT (newdecl));
2761     }
2762   else
2763     {
2764       if (TREE_TYPE (newdecl) == error_mark_node)
2765         types_match = TREE_TYPE (olddecl) == error_mark_node;
2766       else if (TREE_TYPE (olddecl) == NULL_TREE)
2767         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2768       else if (TREE_TYPE (newdecl) == NULL_TREE)
2769         types_match = 0;
2770       else
2771         types_match = comptypes (TREE_TYPE (newdecl),
2772                                  TREE_TYPE (olddecl),
2773                                  COMPARE_REDECLARATION);
2774     }
2775
2776   return types_match;
2777 }
2778
2779 /* If NEWDECL is `static' and an `extern' was seen previously,
2780    warn about it.  OLDDECL is the previous declaration.
2781
2782    Note that this does not apply to the C++ case of declaring
2783    a variable `extern const' and then later `const'.
2784
2785    Don't complain about built-in functions, since they are beyond
2786    the user's control.  */
2787
2788 static void
2789 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2790 {
2791   static const char *const explicit_extern_static_warning
2792     = "`%D' was declared `extern' and later `static'";
2793   static const char *const implicit_extern_static_warning
2794     = "`%D' was declared implicitly `extern' and later `static'";
2795
2796   tree name;
2797
2798   if (TREE_CODE (newdecl) == TYPE_DECL
2799       || TREE_CODE (newdecl) == TEMPLATE_DECL
2800       || TREE_CODE (newdecl) == CONST_DECL)
2801     return;
2802
2803   /* Don't get confused by static member functions; that's a different
2804      use of `static'.  */
2805   if (TREE_CODE (newdecl) == FUNCTION_DECL
2806       && DECL_STATIC_FUNCTION_P (newdecl))
2807     return;
2808
2809   /* If the old declaration was `static', or the new one isn't, then
2810      then everything is OK.  */
2811   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2812     return;
2813
2814   /* It's OK to declare a builtin function as `static'.  */
2815   if (TREE_CODE (olddecl) == FUNCTION_DECL
2816       && DECL_ARTIFICIAL (olddecl))
2817     return;
2818
2819   name = DECL_ASSEMBLER_NAME (newdecl);
2820   pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2821               ? implicit_extern_static_warning
2822               : explicit_extern_static_warning, newdecl);
2823   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2824 }
2825
2826 /* Handle when a new declaration NEWDECL has the same name as an old
2827    one OLDDECL in the same binding contour.  Prints an error message
2828    if appropriate.
2829
2830    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2831    Otherwise, return 0.  */
2832
2833 int
2834 duplicate_decls (tree newdecl, tree olddecl)
2835 {
2836   unsigned olddecl_uid = DECL_UID (olddecl);
2837   int olddecl_friend = 0, types_match = 0;
2838   int new_defines_function = 0;
2839
2840   if (newdecl == olddecl)
2841     return 1;
2842
2843   types_match = decls_match (newdecl, olddecl);
2844
2845   /* If either the type of the new decl or the type of the old decl is an
2846      error_mark_node, then that implies that we have already issued an
2847      error (earlier) for some bogus type specification, and in that case,
2848      it is rather pointless to harass the user with yet more error message
2849      about the same declaration, so just pretend the types match here.  */
2850   if (TREE_TYPE (newdecl) == error_mark_node
2851       || TREE_TYPE (olddecl) == error_mark_node)
2852     types_match = 1;
2853
2854   if (DECL_P (olddecl)
2855       && TREE_CODE (newdecl) == FUNCTION_DECL
2856       && TREE_CODE (olddecl) == FUNCTION_DECL
2857       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2858     {
2859       if (DECL_DECLARED_INLINE_P (newdecl)
2860           && DECL_UNINLINABLE (newdecl)
2861           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2862         /* Already warned elsewhere.  */;
2863       else if (DECL_DECLARED_INLINE_P (olddecl)
2864                && DECL_UNINLINABLE (olddecl)
2865                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2866         /* Already warned.  */;
2867       else if (DECL_DECLARED_INLINE_P (newdecl)
2868                && DECL_UNINLINABLE (olddecl)
2869                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2870         {
2871           warning ("%Hfunction '%D' redeclared as inline",
2872                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2873           warning ("%Hprevious declaration of '%D' with attribute noinline",
2874                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2875         }
2876       else if (DECL_DECLARED_INLINE_P (olddecl)
2877                && DECL_UNINLINABLE (newdecl)
2878                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2879         {
2880           warning ("%Hfunction '%D' redeclared with attribute noinline",
2881                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2882           warning ("%Hprevious declaration of '%D' was inline",
2883                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2884         }
2885     }
2886
2887   /* Check for redeclaration and other discrepancies.  */
2888   if (TREE_CODE (olddecl) == FUNCTION_DECL
2889       && DECL_ARTIFICIAL (olddecl))
2890     {
2891       if (TREE_CODE (newdecl) != FUNCTION_DECL)
2892         {
2893           /* Avoid warnings redeclaring anticipated built-ins.  */
2894           if (DECL_ANTICIPATED (olddecl))
2895             return 0;
2896
2897           /* If you declare a built-in or predefined function name as static,
2898              the old definition is overridden, but optionally warn this was a
2899              bad choice of name.  */
2900           if (! TREE_PUBLIC (newdecl))
2901             {
2902               if (warn_shadow)
2903                 warning ("shadowing %s function `%#D'",
2904                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2905                             olddecl);
2906               /* Discard the old built-in function.  */
2907               return 0;
2908             }
2909           /* If the built-in is not ansi, then programs can override
2910              it even globally without an error.  */
2911           else if (! DECL_BUILT_IN (olddecl))
2912             warning ("library function `%#D' redeclared as non-function `%#D'",
2913                         olddecl, newdecl);
2914           else
2915             {
2916               error ("declaration of `%#D'", newdecl);
2917               error ("conflicts with built-in declaration `%#D'",
2918                         olddecl);
2919             }
2920           return 0;
2921         }
2922       else if (!types_match)
2923         {
2924           /* Avoid warnings redeclaring anticipated built-ins.  */
2925           if (DECL_ANTICIPATED (olddecl))
2926             ;  /* Do nothing yet.  */
2927           else if ((DECL_EXTERN_C_P (newdecl)
2928                && DECL_EXTERN_C_P (olddecl))
2929               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2930                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2931             {
2932               /* A near match; override the builtin.  */
2933
2934               if (TREE_PUBLIC (newdecl))
2935                 {
2936                   warning ("new declaration `%#D'", newdecl);
2937                   warning ("ambiguates built-in declaration `%#D'",
2938                               olddecl);
2939                 }
2940               else if (warn_shadow)
2941                 warning ("shadowing %s function `%#D'",
2942                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2943                             olddecl);
2944             }
2945           else
2946             /* Discard the old built-in function.  */
2947             return 0;
2948
2949           /* Replace the old RTL to avoid problems with inlining.  */
2950           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2951         }
2952       /* Even if the types match, prefer the new declarations type
2953          for anitipated built-ins, for exception lists, etc...  */
2954       else if (DECL_ANTICIPATED (olddecl))
2955         TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2956
2957       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2958         {
2959           /* If a builtin function is redeclared as `static', merge
2960              the declarations, but make the original one static.  */
2961           DECL_THIS_STATIC (olddecl) = 1;
2962           TREE_PUBLIC (olddecl) = 0;
2963
2964           /* Make the old declaration consistent with the new one so
2965              that all remnants of the builtin-ness of this function
2966              will be banished.  */
2967           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2968           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2969         }
2970     }
2971   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2972     {
2973       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2974            && TREE_CODE (newdecl) != TYPE_DECL
2975            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2976                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2977           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2978               && TREE_CODE (olddecl) != TYPE_DECL
2979               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2980                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2981                         == TYPE_DECL))))
2982         {
2983           /* We do nothing special here, because C++ does such nasty
2984              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
2985              get shadowed, and know that if we need to find a TYPE_DECL
2986              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2987              slot of the identifier.  */
2988           return 0;
2989         }
2990
2991       if ((TREE_CODE (newdecl) == FUNCTION_DECL
2992            && DECL_FUNCTION_TEMPLATE_P (olddecl))
2993           || (TREE_CODE (olddecl) == FUNCTION_DECL
2994               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2995         return 0;
2996
2997       error ("`%#D' redeclared as different kind of symbol", newdecl);
2998       if (TREE_CODE (olddecl) == TREE_LIST)
2999         olddecl = TREE_VALUE (olddecl);
3000       cp_error_at ("previous declaration of `%#D'", olddecl);
3001
3002       /* New decl is completely inconsistent with the old one =>
3003          tell caller to replace the old one.  */
3004
3005       return 0;
3006     }
3007   else if (!types_match)
3008     {
3009       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3010         /* These are certainly not duplicate declarations; they're
3011            from different scopes.  */
3012         return 0;
3013
3014       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3015         {
3016           /* The name of a class template may not be declared to refer to
3017              any other template, class, function, object, namespace, value,
3018              or type in the same scope.  */
3019           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3020               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3021             {
3022               error ("declaration of template `%#D'", newdecl);
3023               cp_error_at ("conflicts with previous declaration `%#D'",
3024                            olddecl);
3025             }
3026           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3027                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3028                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3029                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3030                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3031                                            DECL_TEMPLATE_PARMS (olddecl))
3032                    /* Template functions can be disambiguated by
3033                       return type.  */
3034                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3035                                    TREE_TYPE (TREE_TYPE (olddecl))))
3036             {
3037               error ("new declaration `%#D'", newdecl);
3038               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3039             }
3040           return 0;
3041         }
3042       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3043         {
3044           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3045             {
3046               error ("declaration of C function `%#D' conflicts with",
3047                         newdecl);
3048               cp_error_at ("previous declaration `%#D' here", olddecl);
3049             }
3050           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3051                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3052             {
3053               error ("new declaration `%#D'", newdecl);
3054               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3055             }
3056           else
3057             return 0;
3058         }
3059
3060       /* Already complained about this, so don't do so again.  */
3061       else if (current_class_type == NULL_TREE
3062           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3063         {
3064           error ("conflicting types for `%#D'", newdecl);
3065           cp_error_at ("previous declaration as `%#D'", olddecl);
3066         }
3067     }
3068   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3069             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3070                  && (!DECL_TEMPLATE_INFO (newdecl)
3071                      || (DECL_TI_TEMPLATE (newdecl)
3072                          != DECL_TI_TEMPLATE (olddecl))))
3073                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3074                     && (!DECL_TEMPLATE_INFO (olddecl)
3075                         || (DECL_TI_TEMPLATE (olddecl)
3076                             != DECL_TI_TEMPLATE (newdecl))))))
3077     /* It's OK to have a template specialization and a non-template
3078        with the same type, or to have specializations of two
3079        different templates with the same type.  Note that if one is a
3080        specialization, and the other is an instantiation of the same
3081        template, that we do not exit at this point.  That situation
3082        can occur if we instantiate a template class, and then
3083        specialize one of its methods.  This situation is valid, but
3084        the declarations must be merged in the usual way.  */
3085     return 0;
3086   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3087            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3088                 && !DECL_USE_TEMPLATE (newdecl))
3089                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3090                    && !DECL_USE_TEMPLATE (olddecl))))
3091     /* One of the declarations is a template instantiation, and the
3092        other is not a template at all.  That's OK.  */
3093     return 0;
3094   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3095            && DECL_NAMESPACE_ALIAS (newdecl)
3096            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3097     /* Redeclaration of namespace alias, ignore it.  */
3098     return 1;
3099   else
3100     {
3101       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3102       if (errmsg)
3103         {
3104           error (errmsg, newdecl);
3105           if (DECL_NAME (olddecl) != NULL_TREE)
3106             cp_error_at ((DECL_INITIAL (olddecl)
3107                           && namespace_bindings_p ())
3108                          ? "`%#D' previously defined here"
3109                          : "`%#D' previously declared here", olddecl);
3110           return 0;
3111         }
3112       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3113                && DECL_INITIAL (olddecl) != NULL_TREE
3114                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3115                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3116         {
3117           /* Prototype decl follows defn w/o prototype.  */
3118           cp_warning_at ("prototype for `%#D'", newdecl);
3119           cp_warning_at ("follows non-prototype definition here", olddecl);
3120         }
3121       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3122                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3123         {
3124           /* extern "C" int foo ();
3125              int foo () { bar (); }
3126              is OK.  */
3127           if (current_lang_depth () == 0)
3128             SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3129           else
3130             {
3131               cp_error_at ("previous declaration of `%#D' with %L linkage",
3132                            olddecl, DECL_LANGUAGE (olddecl));
3133               error ("conflicts with new declaration with %L linkage",
3134                         DECL_LANGUAGE (newdecl));
3135             }
3136         }
3137
3138       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3139         ;
3140       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3141         {
3142           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3143           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3144           int i = 1;
3145
3146           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3147             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3148
3149           for (; t1 && t1 != void_list_node;
3150                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3151             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3152               {
3153                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3154                                            TREE_PURPOSE (t2)))
3155                   {
3156                     pedwarn ("default argument given for parameter %d of `%#D'",
3157                              i, newdecl);
3158                     cp_pedwarn_at ("after previous specification in `%#D'",
3159                                    olddecl);
3160                   }
3161                 else
3162                   {
3163                     error ("default argument given for parameter %d of `%#D'",
3164                               i, newdecl);
3165                     cp_error_at ("after previous specification in `%#D'",
3166                                  olddecl);
3167                   }
3168               }
3169
3170           if (DECL_DECLARED_INLINE_P (newdecl) 
3171               && ! DECL_DECLARED_INLINE_P (olddecl)
3172               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3173             {
3174               warning ("`%#D' was used before it was declared inline",
3175                           newdecl);
3176               cp_warning_at ("previous non-inline declaration here",
3177                              olddecl);
3178             }
3179         }
3180     }
3181
3182   /* Do not merge an implicit typedef with an explicit one.  In:
3183
3184        class A;
3185        ...
3186        typedef class A A __attribute__ ((foo));
3187
3188      the attribute should apply only to the typedef.  */
3189   if (TREE_CODE (olddecl) == TYPE_DECL
3190       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3191           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3192     return 0;
3193
3194   /* If new decl is `static' and an `extern' was seen previously,
3195      warn about it.  */
3196   warn_extern_redeclared_static (newdecl, olddecl);
3197
3198   /* We have committed to returning 1 at this point.  */
3199   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3200     {
3201       /* Now that functions must hold information normally held
3202          by field decls, there is extra work to do so that
3203          declaration information does not get destroyed during
3204          definition.  */
3205       if (DECL_VINDEX (olddecl))
3206         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3207       if (DECL_CONTEXT (olddecl))
3208         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3209       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3210       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3211       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3212       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3213       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3214       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3215       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3216         SET_OVERLOADED_OPERATOR_CODE
3217           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3218       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3219
3220       /* Optionally warn about more than one declaration for the same
3221          name, but don't warn about a function declaration followed by a
3222          definition.  */
3223       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3224           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3225           /* Don't warn about extern decl followed by definition.  */
3226           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3227           /* Don't warn about friends, let add_friend take care of it.  */
3228           && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3229         {
3230           warning ("redundant redeclaration of `%D' in same scope", newdecl);
3231           cp_warning_at ("previous declaration of `%D'", olddecl);
3232         }
3233     }
3234
3235   /* Deal with C++: must preserve virtual function table size.  */
3236   if (TREE_CODE (olddecl) == TYPE_DECL)
3237     {
3238       register tree newtype = TREE_TYPE (newdecl);
3239       register tree oldtype = TREE_TYPE (olddecl);
3240
3241       if (newtype != error_mark_node && oldtype != error_mark_node
3242           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3243         CLASSTYPE_FRIEND_CLASSES (newtype)
3244           = CLASSTYPE_FRIEND_CLASSES (oldtype);
3245 \
3246       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3247     }
3248
3249   /* Copy all the DECL_... slots specified in the new decl
3250      except for any that we copy here from the old type.  */
3251   DECL_ATTRIBUTES (newdecl)
3252     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3253
3254   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3255     {
3256       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3257       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3258         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3259                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3260
3261       /* If the new declaration is a definition, update the file and
3262          line information on the declaration.  */
3263       if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3264           && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3265         {
3266           DECL_SOURCE_LOCATION (olddecl) 
3267             = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3268             = DECL_SOURCE_LOCATION (newdecl);
3269         }
3270
3271       return 1;
3272     }
3273
3274   if (types_match)
3275     {
3276       /* Automatically handles default parameters.  */
3277       tree oldtype = TREE_TYPE (olddecl);
3278       tree newtype;
3279
3280       /* Merge the data types specified in the two decls.  */
3281       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3282
3283       /* If merge_types produces a non-typedef type, just use the old type.  */
3284       if (TREE_CODE (newdecl) == TYPE_DECL
3285           && newtype == DECL_ORIGINAL_TYPE (newdecl))
3286         newtype = oldtype;
3287
3288       if (TREE_CODE (newdecl) == VAR_DECL)
3289         {
3290           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3291           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3292         }
3293
3294       /* Do this after calling `merge_types' so that default
3295          parameters don't confuse us.  */
3296       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3297           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3298               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3299         {
3300           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3301                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3302           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3303                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3304
3305           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3306               && DECL_SOURCE_LINE (olddecl) != 0
3307               && flag_exceptions
3308               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3309                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3310             {
3311               error ("declaration of `%F' throws different exceptions",
3312                         newdecl);
3313               cp_error_at ("than previous declaration `%F'", olddecl);
3314             }
3315         }
3316       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3317
3318       /* Lay the type out, unless already done.  */
3319       if (! same_type_p (newtype, oldtype)
3320           && TREE_TYPE (newdecl) != error_mark_node
3321           && !(processing_template_decl && uses_template_parms (newdecl)))
3322         layout_type (TREE_TYPE (newdecl));
3323
3324       if ((TREE_CODE (newdecl) == VAR_DECL
3325            || TREE_CODE (newdecl) == PARM_DECL
3326            || TREE_CODE (newdecl) == RESULT_DECL
3327            || TREE_CODE (newdecl) == FIELD_DECL
3328            || TREE_CODE (newdecl) == TYPE_DECL)
3329           && !(processing_template_decl && uses_template_parms (newdecl)))
3330         layout_decl (newdecl, 0);
3331
3332       /* Merge the type qualifiers.  */
3333       if (TREE_READONLY (newdecl))
3334         TREE_READONLY (olddecl) = 1;
3335       if (TREE_THIS_VOLATILE (newdecl))
3336         TREE_THIS_VOLATILE (olddecl) = 1;
3337
3338       /* Merge the initialization information.  */
3339       if (DECL_INITIAL (newdecl) == NULL_TREE
3340           && DECL_INITIAL (olddecl) != NULL_TREE)
3341         {
3342           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3343           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3344           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3345               && DECL_LANG_SPECIFIC (newdecl)
3346               && DECL_LANG_SPECIFIC (olddecl))
3347             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3348         }
3349
3350       /* Merge the section attribute.
3351          We want to issue an error if the sections conflict but that must be
3352          done later in decl_attributes since we are called before attributes
3353          are assigned.  */
3354       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3355         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3356
3357       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3358         {
3359           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3360             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3361           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3362           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3363           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3364           TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3365           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3366           DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3367           /* Keep the old RTL.  */
3368           COPY_DECL_RTL (olddecl, newdecl);
3369         }
3370       else if (TREE_CODE (newdecl) == VAR_DECL 
3371                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3372         {
3373           /* Keep the old RTL.  We cannot keep the old RTL if the old
3374              declaration was for an incomplete object and the new
3375              declaration is not since many attributes of the RTL will
3376              change.  */
3377           COPY_DECL_RTL (olddecl, newdecl);
3378         }
3379     }
3380   /* If cannot merge, then use the new type and qualifiers,
3381      and don't preserve the old rtl.  */
3382   else
3383     {
3384       /* Clean out any memory we had of the old declaration.  */
3385       tree oldstatic = value_member (olddecl, static_aggregates);
3386       if (oldstatic)
3387         TREE_VALUE (oldstatic) = error_mark_node;
3388
3389       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3390       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3391       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3392       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3393     }
3394
3395   /* Merge the storage class information.  */
3396   merge_weak (newdecl, olddecl);
3397
3398   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3399   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3400   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3401   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3402   if (! DECL_EXTERNAL (olddecl))
3403     DECL_EXTERNAL (newdecl) = 0;
3404
3405   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3406     {
3407       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3408       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3409       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3410       DECL_TEMPLATE_INSTANTIATED (newdecl)
3411         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3412       /* Don't really know how much of the language-specific
3413          values we should copy from old to new.  */
3414       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3415       DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 
3416         DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3417       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3418       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3419       DECL_INITIALIZED_IN_CLASS_P (newdecl)
3420         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3421       olddecl_friend = DECL_FRIEND_P (olddecl);
3422
3423       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3424       if (TREE_CODE (newdecl) == FUNCTION_DECL
3425           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3426         {
3427           DECL_BEFRIENDING_CLASSES (newdecl)
3428             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3429                        DECL_BEFRIENDING_CLASSES (olddecl));
3430           /* DECL_THUNKS is only valid for virtual functions,
3431              otherwise it is a DECL_FRIEND_CONTEXT.  */
3432           if (DECL_VIRTUAL_P (newdecl))
3433             DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3434         }
3435     }
3436
3437   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3438     {
3439       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3440           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3441         {
3442           /* If newdecl is not a specialization, then it is not a
3443              template-related function at all.  And that means that we
3444              should have exited above, returning 0.  */
3445           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3446                               0);
3447
3448           if (TREE_USED (olddecl))
3449             /* From [temp.expl.spec]:
3450
3451                If a template, a member template or the member of a class
3452                template is explicitly specialized then that
3453                specialization shall be declared before the first use of
3454                that specialization that would cause an implicit
3455                instantiation to take place, in every translation unit in
3456                which such a use occurs.  */
3457             error ("explicit specialization of %D after first use",
3458                       olddecl);
3459
3460           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3461
3462           /* [temp.expl.spec/14] We don't inline explicit specialization
3463              just because the primary template says so.  */
3464         }
3465       else
3466         {
3467           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3468             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3469
3470           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3471
3472           /* If either decl says `inline', this fn is inline, unless 
3473              its definition was passed already.  */
3474           if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3475             DECL_INLINE (olddecl) = 1;
3476           DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3477
3478           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3479             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3480         }
3481
3482       /* Preserve abstractness on cloned [cd]tors.  */
3483       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3484
3485       if (! types_match)
3486         {
3487           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3488           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3489           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3490         }
3491       if (! types_match || new_defines_function)
3492         {
3493           /* These need to be copied so that the names are available.
3494              Note that if the types do match, we'll preserve inline
3495              info and other bits, but if not, we won't.  */
3496           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3497           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3498         }
3499       if (new_defines_function)
3500         /* If defining a function declared with other language
3501            linkage, use the previously declared language linkage.  */
3502         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3503       else if (types_match)
3504         {
3505           /* If redeclaring a builtin function, and not a definition,
3506              it stays built in.  */
3507           if (DECL_BUILT_IN (olddecl))
3508             {
3509               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3510               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3511               /* If we're keeping the built-in definition, keep the rtl,
3512                  regardless of declaration matches.  */
3513               SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3514             }
3515           else
3516             DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3517
3518           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3519           /* Don't clear out the arguments if we're redefining a function.  */
3520           if (DECL_ARGUMENTS (olddecl))
3521             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3522         }
3523     }
3524   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3525     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3526
3527   /* Now preserve various other info from the definition.  */
3528   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3529   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3530   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3531   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3532
3533   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3534     {
3535       int function_size;
3536
3537       function_size = sizeof (struct tree_decl);
3538
3539       memcpy ((char *) olddecl + sizeof (struct tree_common),
3540               (char *) newdecl + sizeof (struct tree_common),
3541               function_size - sizeof (struct tree_common));
3542
3543       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3544         {
3545           /* If newdecl is a template instantiation, it is possible that
3546              the following sequence of events has occurred:
3547
3548              o A friend function was declared in a class template.  The
3549              class template was instantiated.
3550
3551              o The instantiation of the friend declaration was
3552              recorded on the instantiation list, and is newdecl.
3553
3554              o Later, however, instantiate_class_template called pushdecl
3555              on the newdecl to perform name injection.  But, pushdecl in
3556              turn called duplicate_decls when it discovered that another
3557              declaration of a global function with the same name already
3558              existed.
3559
3560              o Here, in duplicate_decls, we decided to clobber newdecl.
3561
3562              If we're going to do that, we'd better make sure that
3563              olddecl, and not newdecl, is on the list of
3564              instantiations so that if we try to do the instantiation
3565              again we won't get the clobbered declaration.  */
3566
3567           tree tmpl = DECL_TI_TEMPLATE (newdecl);
3568           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3569
3570           for (; decls; decls = TREE_CHAIN (decls))
3571             if (TREE_VALUE (decls) == newdecl)
3572               TREE_VALUE (decls) = olddecl;
3573         }
3574     }
3575   else
3576     {
3577       memcpy ((char *) olddecl + sizeof (struct tree_common),
3578               (char *) newdecl + sizeof (struct tree_common),
3579               sizeof (struct tree_decl) - sizeof (struct tree_common)
3580               + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3581     }
3582
3583   DECL_UID (olddecl) = olddecl_uid;
3584   if (olddecl_friend)
3585     DECL_FRIEND_P (olddecl) = 1;
3586
3587   /* NEWDECL contains the merged attribute lists.
3588      Update OLDDECL to be the same.  */
3589   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3590
3591   return 1;
3592 }
3593
3594 /* Record a decl-node X as belonging to the current lexical scope.
3595    Check for errors (such as an incompatible declaration for the same
3596    name already seen in the same scope).
3597
3598    Returns either X or an old decl for the same name.
3599    If an old decl is returned, it may have been smashed
3600    to agree with what X says.  */
3601
3602 tree
3603 pushdecl (tree x)
3604 {
3605   register tree t;
3606   register tree name;
3607   int need_new_binding;
3608
3609   timevar_push (TV_NAME_LOOKUP);
3610   /* We shouldn't be calling pushdecl when we're generating RTL for a
3611      function that we already did semantic analysis on previously.  */
3612   my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3613                       19990913);
3614
3615   need_new_binding = 1;
3616
3617   if (DECL_TEMPLATE_PARM_P (x))
3618     /* Template parameters have no context; they are not X::T even
3619        when declared within a class or namespace.  */
3620     ;
3621   else
3622     {
3623       if (current_function_decl && x != current_function_decl
3624           /* A local declaration for a function doesn't constitute
3625              nesting.  */
3626           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3627           /* A local declaration for an `extern' variable is in the
3628              scope of the current namespace, not the current
3629              function.  */
3630           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3631           && !DECL_CONTEXT (x))
3632         DECL_CONTEXT (x) = current_function_decl;
3633
3634       /* If this is the declaration for a namespace-scope function,
3635          but the declaration itself is in a local scope, mark the
3636          declaration.  */
3637       if (TREE_CODE (x) == FUNCTION_DECL
3638           && DECL_NAMESPACE_SCOPE_P (x)
3639           && current_function_decl
3640           && x != current_function_decl)
3641         DECL_LOCAL_FUNCTION_P (x) = 1;
3642     }
3643
3644   name = DECL_NAME (x);
3645   if (name)
3646     {
3647       int different_binding_level = 0;
3648
3649       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3650         name = TREE_OPERAND (name, 0);
3651
3652       /* In case this decl was explicitly namespace-qualified, look it
3653          up in its namespace context.  */
3654       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3655           && namespace_bindings_p ())
3656         t = namespace_binding (name, DECL_CONTEXT (x));
3657       else
3658         t = lookup_name_current_level (name);
3659
3660       /* [basic.link] If there is a visible declaration of an entity
3661          with linkage having the same name and type, ignoring entities
3662          declared outside the innermost enclosing namespace scope, the
3663          block scope declaration declares that same entity and
3664          receives the linkage of the previous declaration.  */
3665       if (! t && current_function_decl && x != current_function_decl
3666           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3667           && DECL_EXTERNAL (x))
3668         {
3669           /* Look in block scope.  */
3670           t = IDENTIFIER_VALUE (name);
3671           /* Or in the innermost namespace.  */
3672           if (! t)
3673             t = namespace_binding (name, DECL_CONTEXT (x));
3674           /* Does it have linkage?  Note that if this isn't a DECL, it's an
3675              OVERLOAD, which is OK.  */
3676           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3677             t = NULL_TREE;
3678           if (t)
3679             different_binding_level = 1;
3680         }
3681
3682       /* If we are declaring a function, and the result of name-lookup
3683          was an OVERLOAD, look for an overloaded instance that is
3684          actually the same as the function we are declaring.  (If
3685          there is one, we have to merge our declaration with the
3686          previous declaration.)  */
3687       if (t && TREE_CODE (t) == OVERLOAD)
3688         {
3689           tree match;
3690
3691           if (TREE_CODE (x) == FUNCTION_DECL)
3692             for (match = t; match; match = OVL_NEXT (match))
3693               {
3694                 if (decls_match (OVL_CURRENT (match), x))
3695                   break;
3696               }
3697           else
3698             /* Just choose one.  */
3699             match = t;
3700
3701           if (match)
3702             t = OVL_CURRENT (match);
3703           else
3704             t = NULL_TREE;
3705         }
3706
3707       if (t == error_mark_node)
3708         {
3709           /* error_mark_node is 0 for a while during initialization!  */
3710           t = NULL_TREE;
3711           cp_error_at ("`%#D' used prior to declaration", x);
3712         }
3713       else if (t != NULL_TREE)
3714         {
3715           if (different_binding_level)
3716             {
3717               if (decls_match (x, t))
3718                 /* The standard only says that the local extern
3719                    inherits linkage from the previous decl; in
3720                    particular, default args are not shared.  It would
3721                    be nice to propagate inlining info, though.  FIXME.  */
3722                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3723             }
3724           else if (TREE_CODE (t) == PARM_DECL)
3725             {
3726               if (DECL_CONTEXT (t) == NULL_TREE)
3727                 /* This is probaby caused by too many errors, but calling
3728                    abort will say that if errors have occurred.  */
3729                 abort ();
3730
3731               /* Check for duplicate params.  */
3732               if (duplicate_decls (x, t))
3733                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3734             }
3735           else if ((DECL_EXTERN_C_FUNCTION_P (x)
3736                     || DECL_FUNCTION_TEMPLATE_P (x))
3737                    && is_overloaded_fn (t))
3738             /* Don't do anything just yet.  */;
3739           else if (t == wchar_decl_node)
3740             {
3741               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3742                 pedwarn ("redeclaration of `wchar_t' as `%T'",
3743                             TREE_TYPE (x));
3744
3745               /* Throw away the redeclaration.  */
3746               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3747             }
3748           else if (TREE_CODE (t) != TREE_CODE (x))
3749             {
3750               if (duplicate_decls (x, t))
3751                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3752             }
3753           else if (duplicate_decls (x, t))
3754             {
3755               if (TREE_CODE (t) == TYPE_DECL)
3756                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3757               else if (TREE_CODE (t) == FUNCTION_DECL)
3758                 check_default_args (t);
3759
3760               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3761             }
3762           else if (DECL_MAIN_P (x))
3763             {
3764               /* A redeclaration of main, but not a duplicate of the
3765                  previous one.
3766
3767                  [basic.start.main]
3768
3769                  This function shall not be overloaded.  */
3770               cp_error_at ("invalid redeclaration of `%D'", t);
3771               error ("as `%D'", x);
3772               /* We don't try to push this declaration since that
3773                  causes a crash.  */
3774               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3775             }
3776         }
3777
3778       check_template_shadow (x);
3779
3780       /* If this is a function conjured up by the backend, massage it
3781          so it looks friendly.  */
3782       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3783         {
3784           retrofit_lang_decl (x);
3785           SET_DECL_LANGUAGE (x, lang_c);
3786         }
3787
3788       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3789         {
3790           t = push_overloaded_decl (x, PUSH_LOCAL);
3791           if (t != x)
3792             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3793           if (!namespace_bindings_p ())
3794             /* We do not need to create a binding for this name;
3795                push_overloaded_decl will have already done so if
3796                necessary.  */
3797             need_new_binding = 0;
3798         }
3799       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3800         {
3801           t = push_overloaded_decl (x, PUSH_GLOBAL);
3802           if (t == x)
3803             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3804           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3805         }
3806
3807       /* If declaring a type as a typedef, copy the type (unless we're
3808          at line 0), and install this TYPE_DECL as the new type's typedef
3809          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
3810       if (TREE_CODE (x) == TYPE_DECL)
3811         {
3812           tree type = TREE_TYPE (x);
3813           if (DECL_SOURCE_LINE (x) == 0)
3814             {
3815               if (TYPE_NAME (type) == 0)
3816                 TYPE_NAME (type) = x;
3817             }
3818           else if (type != error_mark_node && TYPE_NAME (type) != x
3819                    /* We don't want to copy the type when all we're
3820                       doing is making a TYPE_DECL for the purposes of
3821                       inlining.  */
3822                    && (!TYPE_NAME (type)
3823                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3824             {
3825               DECL_ORIGINAL_TYPE (x) = type;
3826               type = build_type_copy (type);
3827               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3828               TYPE_NAME (type) = x;
3829               TREE_TYPE (x) = type;
3830             }
3831
3832           if (type != error_mark_node
3833               && TYPE_NAME (type)
3834               && TYPE_IDENTIFIER (type))
3835             set_identifier_type_value_with_scope (DECL_NAME (x), type,
3836                                                   current_binding_level);
3837
3838         }
3839
3840       /* Multiple external decls of the same identifier ought to match.
3841
3842          We get warnings about inline functions where they are defined.
3843          We get warnings about other functions from push_overloaded_decl.
3844
3845          Avoid duplicate warnings where they are used.  */
3846       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3847         {
3848           tree decl;
3849
3850           decl = IDENTIFIER_NAMESPACE_VALUE (name);
3851           if (decl && TREE_CODE (decl) == OVERLOAD)
3852             decl = OVL_FUNCTION (decl);
3853
3854           if (decl && decl != error_mark_node
3855               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3856               /* If different sort of thing, we already gave an error.  */
3857               && TREE_CODE (decl) == TREE_CODE (x)
3858               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3859             {
3860               pedwarn ("type mismatch with previous external decl", x);
3861               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3862             }
3863         }
3864
3865       /* This name is new in its binding level.
3866          Install the new declaration and return it.  */
3867       if (namespace_bindings_p ())
3868         {
3869           /* Install a global value.  */
3870
3871           /* If the first global decl has external linkage,
3872              warn if we later see static one.  */
3873           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3874             TREE_PUBLIC (name) = 1;
3875
3876           /* Bind the name for the entity.  */
3877           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3878                 && t != NULL_TREE)
3879               && (TREE_CODE (x) == TYPE_DECL
3880                   || TREE_CODE (x) == VAR_DECL
3881                   || TREE_CODE (x) == ALIAS_DECL
3882                   || TREE_CODE (x) == NAMESPACE_DECL
3883                   || TREE_CODE (x) == CONST_DECL
3884                   || TREE_CODE (x) == TEMPLATE_DECL))
3885             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3886
3887           /* Don't forget if the function was used via an implicit decl.  */
3888           if (IDENTIFIER_IMPLICIT_DECL (name)
3889               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3890             TREE_USED (x) = 1;
3891
3892           /* Don't forget if its address was taken in that way.  */
3893           if (IDENTIFIER_IMPLICIT_DECL (name)
3894               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3895             TREE_ADDRESSABLE (x) = 1;
3896
3897           /* Warn about mismatches against previous implicit decl.  */
3898           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3899               /* If this real decl matches the implicit, don't complain.  */
3900               && ! (TREE_CODE (x) == FUNCTION_DECL
3901                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3902             warning
3903               ("`%D' was previously implicitly declared to return `int'", x);
3904
3905           /* If new decl is `static' and an `extern' was seen previously,
3906              warn about it.  */
3907           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3908             warn_extern_redeclared_static (x, t);
3909         }
3910       else
3911         {
3912           /* Here to install a non-global value.  */
3913           tree oldlocal = IDENTIFIER_VALUE (name);
3914           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3915
3916           if (need_new_binding)
3917             {
3918               push_local_binding (name, x, 0);
3919               /* Because push_local_binding will hook X on to the
3920                  current_binding_level's name list, we don't want to
3921                  do that again below.  */
3922               need_new_binding = 0;
3923             }
3924
3925           /* If this is a TYPE_DECL, push it into the type value slot.  */
3926           if (TREE_CODE (x) == TYPE_DECL)
3927             set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3928                                                   current_binding_level);
3929
3930           /* Clear out any TYPE_DECL shadowed by a namespace so that
3931              we won't think this is a type.  The C struct hack doesn't
3932              go through namespaces.  */
3933           if (TREE_CODE (x) == NAMESPACE_DECL)
3934             set_identifier_type_value_with_scope (name, NULL_TREE,
3935                                                   current_binding_level);
3936
3937           if (oldlocal)
3938             {
3939               tree d = oldlocal;
3940
3941               while (oldlocal
3942                      && TREE_CODE (oldlocal) == VAR_DECL
3943                      && DECL_DEAD_FOR_LOCAL (oldlocal))
3944                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3945
3946               if (oldlocal == NULL_TREE)
3947                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3948             }
3949
3950           /* If this is an extern function declaration, see if we
3951              have a global definition or declaration for the function.  */
3952           if (oldlocal == NULL_TREE
3953               && DECL_EXTERNAL (x)
3954               && oldglobal != NULL_TREE
3955               && TREE_CODE (x) == FUNCTION_DECL
3956               && TREE_CODE (oldglobal) == FUNCTION_DECL)
3957             {
3958               /* We have one.  Their types must agree.  */
3959               if (decls_match (x, oldglobal))
3960                 /* OK */;
3961               else
3962                 {
3963                   warning ("extern declaration of `%#D' doesn't match", x);
3964                   cp_warning_at ("global declaration `%#D'", oldglobal);
3965                 }
3966             }
3967           /* If we have a local external declaration,
3968              and no file-scope declaration has yet been seen,
3969              then if we later have a file-scope decl it must not be static.  */
3970           if (oldlocal == NULL_TREE
3971               && oldglobal == NULL_TREE
3972               && DECL_EXTERNAL (x)
3973               && TREE_PUBLIC (x))
3974             TREE_PUBLIC (name) = 1;
3975
3976           /* Warn if shadowing an argument at the top level of the body.  */
3977           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3978               /* Inline decls shadow nothing.  */
3979               && !DECL_FROM_INLINE (x)
3980               && TREE_CODE (oldlocal) == PARM_DECL
3981               /* Don't check the `this' parameter.  */
3982               && !DECL_ARTIFICIAL (oldlocal))
3983             {
3984               bool err = false;
3985
3986               /* Don't complain if it's from an enclosing function.  */
3987               if (DECL_CONTEXT (oldlocal) == current_function_decl
3988                   && TREE_CODE (x) != PARM_DECL)
3989                 {
3990                   /* Go to where the parms should be and see if we find
3991                      them there.  */
3992                   struct cp_binding_level *b = current_binding_level->level_chain;
3993
3994                   /* Skip the ctor/dtor cleanup level.  */
3995                   b = b->level_chain;
3996
3997                   /* ARM $8.3 */
3998                   if (b->parm_flag == 1)
3999                     {
4000                       error ("declaration of `%#D' shadows a parameter",
4001                                 name);
4002                       err = true;
4003                     }
4004                 }
4005
4006               if (warn_shadow && !err)
4007                 shadow_warning (SW_PARAM,
4008                                 IDENTIFIER_POINTER (name), oldlocal);
4009             }
4010
4011           /* Maybe warn if shadowing something else.  */
4012           else if (warn_shadow && !DECL_EXTERNAL (x)
4013               /* No shadow warnings for internally generated vars.  */
4014               && ! DECL_ARTIFICIAL (x)
4015               /* No shadow warnings for vars made for inlining.  */
4016               && ! DECL_FROM_INLINE (x))
4017             {
4018               if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4019                        && current_class_ptr
4020                        && !TREE_STATIC (name))
4021                 warning ("declaration of `%s' shadows a member of `this'",
4022                             IDENTIFIER_POINTER (name));
4023               else if (oldlocal != NULL_TREE
4024                        && TREE_CODE (oldlocal) == VAR_DECL)
4025                 shadow_warning (SW_LOCAL,
4026                                 IDENTIFIER_POINTER (name), oldlocal);
4027               else if (oldglobal != NULL_TREE
4028                        && TREE_CODE (oldglobal) == VAR_DECL)
4029                 /* XXX shadow warnings in outer-more namespaces */
4030                 shadow_warning (SW_GLOBAL,
4031                                 IDENTIFIER_POINTER (name), oldglobal);
4032             }
4033         }
4034
4035       if (TREE_CODE (x) == FUNCTION_DECL)
4036         check_default_args (x);
4037
4038       if (TREE_CODE (x) == VAR_DECL)
4039         maybe_register_incomplete_var (x);
4040     }
4041
4042   if (need_new_binding)
4043     add_decl_to_level (x,
4044                        DECL_NAMESPACE_SCOPE_P (x)
4045                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4046                        : current_binding_level);
4047
4048   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4049 }
4050
4051 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4052    caller to set DECL_CONTEXT properly.  */
4053
4054 static tree
4055 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4056 {
4057   register struct cp_binding_level *b;
4058   tree function_decl = current_function_decl;
4059
4060   timevar_push (TV_NAME_LOOKUP);
4061   current_function_decl = NULL_TREE;
4062   if (level->parm_flag == 2)
4063     {
4064       b = class_binding_level;
4065       class_binding_level = level;
4066       pushdecl_class_level (x);
4067       class_binding_level = b;
4068     }
4069   else
4070     {
4071       b = current_binding_level;
4072       current_binding_level = level;
4073       x = pushdecl (x);
4074       current_binding_level = b;
4075     }
4076   current_function_decl = function_decl;
4077   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4078 }
4079
4080 /* Like pushdecl, only it places X in the current namespace,
4081    if appropriate.  */
4082
4083 tree
4084 pushdecl_namespace_level (tree x)
4085 {
4086   register struct cp_binding_level *b = current_binding_level;
4087   register tree t;
4088
4089   timevar_push (TV_NAME_LOOKUP);
4090   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4091
4092   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4093      what we want.  */
4094   if (TREE_CODE (x) == TYPE_DECL)
4095     {
4096       tree name = DECL_NAME (x);
4097       tree newval;
4098       tree *ptr = (tree *)0;
4099       for (; b != global_binding_level; b = b->level_chain)
4100         {
4101           tree shadowed = b->type_shadowed;
4102           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4103             if (TREE_PURPOSE (shadowed) == name)
4104               {
4105                 ptr = &TREE_VALUE (shadowed);
4106                 /* Can't break out of the loop here because sometimes
4107                    a binding level will have duplicate bindings for
4108                    PT names.  It's gross, but I haven't time to fix it.  */
4109               }
4110         }
4111       newval = TREE_TYPE (x);
4112       if (ptr == (tree *)0)
4113         {
4114           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4115              up here if this is changed to an assertion.  --KR  */
4116           SET_IDENTIFIER_TYPE_VALUE (name, newval);
4117         }
4118       else
4119         {
4120           *ptr = newval;
4121         }
4122     }
4123   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4124 }
4125
4126 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4127    if appropriate.  */
4128
4129 tree
4130 pushdecl_top_level (tree x)
4131 {
4132   timevar_push (TV_NAME_LOOKUP);
4133   push_to_top_level ();
4134   x = pushdecl_namespace_level (x);
4135   pop_from_top_level ();
4136   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4137 }
4138
4139 /* Make the declaration of X appear in CLASS scope.  */
4140
4141 void
4142 pushdecl_class_level (tree x)
4143 {
4144   tree name;
4145
4146   timevar_push (TV_NAME_LOOKUP);
4147   /* Get the name of X.  */
4148   if (TREE_CODE (x) == OVERLOAD)
4149     name = DECL_NAME (get_first_fn (x));
4150   else
4151     name = DECL_NAME (x);
4152
4153   if (name)
4154     {
4155       push_class_level_binding (name, x);
4156       if (TREE_CODE (x) == TYPE_DECL)
4157         set_identifier_type_value (name, TREE_TYPE (x));
4158     }
4159   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4160     {
4161       /* If X is an anonymous aggregate, all of its members are
4162          treated as if they were members of the class containing the
4163          aggregate, for naming purposes.  */
4164       tree f;
4165
4166       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4167         pushdecl_class_level (f);
4168     }
4169   timevar_pop (TV_NAME_LOOKUP);
4170 }
4171
4172 /* Enter DECL into the symbol table, if that's appropriate.  Returns
4173    DECL, or a modified version thereof.  */
4174
4175 tree
4176 maybe_push_decl (tree decl)
4177 {
4178   tree type = TREE_TYPE (decl);
4179
4180   /* Add this decl to the current binding level, but not if it comes
4181      from another scope, e.g. a static member variable.  TEM may equal
4182      DECL or it may be a previous decl of the same name.  */
4183   if (decl == error_mark_node
4184       || (TREE_CODE (decl) != PARM_DECL
4185           && DECL_CONTEXT (decl) != NULL_TREE
4186           /* Definitions of namespace members outside their namespace are
4187              possible.  */
4188           && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4189       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4190       || TREE_CODE (type) == UNKNOWN_TYPE
4191       /* The declaration of a template specialization does not affect
4192          the functions available for overload resolution, so we do not
4193          call pushdecl.  */
4194       || (TREE_CODE (decl) == FUNCTION_DECL
4195           && DECL_TEMPLATE_SPECIALIZATION (decl)))
4196     return decl;
4197   else
4198     return pushdecl (decl);
4199 }
4200
4201 /* Make the declaration(s) of X appear in CLASS scope
4202    under the name NAME.  */
4203
4204 void
4205 push_class_level_binding (tree name, tree x)
4206 {
4207   cxx_binding *binding;
4208   timevar_push (TV_NAME_LOOKUP);
4209   /* The class_binding_level will be NULL if x is a template
4210      parameter name in a member template.  */
4211   if (!class_binding_level)
4212     {
4213       timevar_pop (TV_NAME_LOOKUP);
4214       return;
4215     }
4216
4217   /* Make sure that this new member does not have the same name
4218      as a template parameter.  */
4219   if (TYPE_BEING_DEFINED (current_class_type))
4220     check_template_shadow (x);
4221
4222   /* If this declaration shadows a declaration from an enclosing
4223      class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4224      we leave this class.  Record the shadowed declaration here.  */
4225   binding = IDENTIFIER_BINDING (name);
4226   if (binding
4227       && ((TREE_CODE (x) == OVERLOAD
4228            && BINDING_VALUE (binding)
4229            && is_overloaded_fn (BINDING_VALUE (binding)))
4230           || INHERITED_VALUE_BINDING_P (binding)))
4231     {
4232       tree shadow;
4233       tree old_decl;
4234
4235       /* If the old binding was from a base class, and was for a tag
4236          name, slide it over to make room for the new binding.  The
4237          old binding is still visible if explicitly qualified with a
4238          class-key.  */
4239       if (INHERITED_VALUE_BINDING_P (binding)
4240           && BINDING_VALUE (binding)
4241           && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4242           && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4243           && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4244         {
4245           old_decl = BINDING_TYPE (binding);
4246           BINDING_TYPE (binding) = BINDING_VALUE (binding);
4247           BINDING_VALUE (binding) = NULL_TREE;
4248           INHERITED_VALUE_BINDING_P (binding) = 0;
4249         }
4250       else
4251         old_decl = BINDING_VALUE (binding);
4252
4253       /* Find the previous binding of name on the class-shadowed
4254          list, and update it.  */
4255       for (shadow = class_binding_level->class_shadowed;
4256            shadow;
4257            shadow = TREE_CHAIN (shadow))
4258         if (TREE_PURPOSE (shadow) == name
4259             && TREE_TYPE (shadow) == old_decl)
4260           {
4261             BINDING_VALUE (binding) = x;
4262             INHERITED_VALUE_BINDING_P (binding) = 0;
4263             TREE_TYPE (shadow) = x;
4264             IDENTIFIER_CLASS_VALUE (name) = x;
4265             timevar_pop (TV_NAME_LOOKUP);
4266             return;
4267           }
4268     }
4269
4270   /* If we didn't replace an existing binding, put the binding on the
4271      stack of bindings for the identifier, and update the shadowed list.  */
4272   if (push_class_binding (name, x))
4273     {
4274       class_binding_level->class_shadowed
4275         = tree_cons (name, NULL,
4276                      class_binding_level->class_shadowed);
4277       /* Record the value we are binding NAME to so that we can know
4278          what to pop later.  */
4279       TREE_TYPE (class_binding_level->class_shadowed) = x;
4280     }
4281   timevar_pop (TV_NAME_LOOKUP);
4282 }
4283
4284 /* Insert another USING_DECL into the current binding level, returning
4285    this declaration. If this is a redeclaration, do nothing, and
4286    return NULL_TREE if this not in namespace scope (in namespace
4287    scope, a using decl might extend any previous bindings).  */
4288
4289 tree
4290 push_using_decl (tree scope, tree name)
4291 {
4292   tree decl;
4293
4294   timevar_push (TV_NAME_LOOKUP);
4295   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4296   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4297   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4298     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4299       break;
4300   if (decl)
4301     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4302                             namespace_bindings_p () ? decl : NULL_TREE);
4303   decl = build_lang_decl (USING_DECL, name, void_type_node);
4304   DECL_INITIAL (decl) = scope;
4305   TREE_CHAIN (decl) = current_binding_level->usings;
4306   current_binding_level->usings = decl;
4307   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4308 }
4309
4310 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4311    changed (i.e. there was already a directive), or the fresh
4312    TREE_LIST otherwise.  */
4313
4314 tree
4315 push_using_directive (tree used)
4316 {
4317   tree ud = current_binding_level->using_directives;
4318   tree iter, ancestor;
4319
4320   timevar_push (TV_NAME_LOOKUP);
4321   /* Check if we already have this.  */
4322   if (purpose_member (used, ud) != NULL_TREE)
4323     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4324
4325   ancestor = namespace_ancestor (current_decl_namespace (), used);
4326   ud = current_binding_level->using_directives;
4327   ud = tree_cons (used, ancestor, ud);
4328   current_binding_level->using_directives = ud;
4329
4330   /* Recursively add all namespaces used.  */
4331   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4332     push_using_directive (TREE_PURPOSE (iter));
4333
4334   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4335 }
4336
4337 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4338    other definitions already in place.  We get around this by making
4339    the value of the identifier point to a list of all the things that
4340    want to be referenced by that name.  It is then up to the users of
4341    that name to decide what to do with that list.
4342
4343    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4344    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4345
4346    FLAGS is a bitwise-or of the following values:
4347      PUSH_LOCAL: Bind DECL in the current scope, rather than at
4348                  namespace scope.
4349      PUSH_USING: DECL is being pushed as the result of a using
4350                  declaration.
4351
4352    The value returned may be a previous declaration if we guessed wrong
4353    about what language DECL should belong to (C or C++).  Otherwise,
4354    it's always DECL (and never something that's not a _DECL).  */
4355
4356 tree
4357 push_overloaded_decl (tree decl, int flags)
4358 {
4359   tree name = DECL_NAME (decl);
4360   tree old;
4361   tree new_binding;
4362   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4363
4364   timevar_push (TV_NAME_LOOKUP);
4365   if (doing_global)
4366     old = namespace_binding (name, DECL_CONTEXT (decl));
4367   else
4368     old = lookup_name_current_level (name);
4369
4370   if (old)
4371     {
4372       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4373         {
4374           tree t = TREE_TYPE (old);
4375           if (IS_AGGR_TYPE (t) && warn_shadow
4376               && (! DECL_IN_SYSTEM_HEADER (decl)
4377                   || ! DECL_IN_SYSTEM_HEADER (old)))
4378             warning ("`%#D' hides constructor for `%#T'", decl, t);
4379           old = NULL_TREE;
4380         }
4381       else if (is_overloaded_fn (old))
4382         {
4383           tree tmp;
4384
4385           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4386             {
4387               tree fn = OVL_CURRENT (tmp);
4388
4389               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4390                   && !(flags & PUSH_USING)
4391                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4392                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4393                 error ("`%#D' conflicts with previous using declaration `%#D'",
4394                           decl, fn);
4395
4396               if (duplicate_decls (decl, fn))
4397                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4398             }
4399         }
4400       else if (old == error_mark_node)
4401         /* Ignore the undefined symbol marker.  */
4402         old = NULL_TREE;
4403       else
4404         {
4405           cp_error_at ("previous non-function declaration `%#D'", old);
4406           error ("conflicts with function declaration `%#D'", decl);
4407           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4408         }
4409     }
4410
4411   if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4412     {
4413       if (old && TREE_CODE (old) != OVERLOAD)
4414         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4415       else
4416         new_binding = ovl_cons (decl, old);
4417       if (flags & PUSH_USING)
4418         OVL_USED (new_binding) = 1;
4419     }
4420   else
4421     /* NAME is not ambiguous.  */
4422     new_binding = decl;
4423
4424   if (doing_global)
4425     set_namespace_binding (name, current_namespace, new_binding);
4426   else
4427     {
4428       /* We only create an OVERLOAD if there was a previous binding at
4429          this level, or if decl is a template. In the former case, we
4430          need to remove the old binding and replace it with the new
4431          binding.  We must also run through the NAMES on the binding
4432          level where the name was bound to update the chain.  */
4433
4434       if (TREE_CODE (new_binding) == OVERLOAD && old)
4435         {
4436           tree *d;
4437
4438           for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names;
4439                *d;
4440                d = &TREE_CHAIN (*d))
4441             if (*d == old
4442                 || (TREE_CODE (*d) == TREE_LIST
4443                     && TREE_VALUE (*d) == old))
4444               {
4445                 if (TREE_CODE (*d) == TREE_LIST)
4446                   /* Just replace the old binding with the new.  */
4447                   TREE_VALUE (*d) = new_binding;
4448                 else
4449                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
4450                   *d = tree_cons (NULL_TREE, new_binding,
4451                                   TREE_CHAIN (*d));
4452
4453                 /* And update the cxx_binding node.  */
4454                 BINDING_VALUE (IDENTIFIER_BINDING (name))
4455                   = new_binding;
4456                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4457               }
4458
4459           /* We should always find a previous binding in this case.  */
4460           abort ();
4461         }
4462
4463       /* Install the new binding.  */
4464       push_local_binding (name, new_binding, flags);
4465     }
4466
4467   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4468 }
4469 \f
4470 /* Generate an implicit declaration for identifier FUNCTIONID
4471    as a function of type int ().  Print a warning if appropriate.  */
4472
4473 tree
4474 implicitly_declare (tree functionid)
4475 {
4476   register tree decl;
4477
4478   /* We used to reuse an old implicit decl here,
4479      but this loses with inline functions because it can clobber
4480      the saved decl chains.  */
4481   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4482
4483   DECL_EXTERNAL (decl) = 1;
4484   TREE_PUBLIC (decl) = 1;
4485
4486   /* ISO standard says implicit declarations are in the innermost block.
4487      So we record the decl in the standard fashion.  */
4488   pushdecl (decl);
4489   rest_of_decl_compilation (decl, NULL, 0, 0);
4490
4491   if (warn_implicit
4492       /* Only one warning per identifier.  */
4493       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4494     {
4495       pedwarn ("implicit declaration of function `%#D'", decl);
4496     }
4497
4498   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4499
4500   return decl;
4501 }
4502
4503 /* Return zero if the declaration NEWDECL is valid
4504    when the declaration OLDDECL (assumed to be for the same name)
4505    has already been seen.
4506    Otherwise return an error message format string with a %s
4507    where the identifier should go.  */
4508
4509 static const char *
4510 redeclaration_error_message (tree newdecl, tree olddecl)
4511 {
4512   if (TREE_CODE (newdecl) == TYPE_DECL)
4513     {
4514       /* Because C++ can put things into name space for free,
4515          constructs like "typedef struct foo { ... } foo"
4516          would look like an erroneous redeclaration.  */
4517       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4518         return 0;
4519       else
4520         return "redefinition of `%#D'";
4521     }
4522   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4523     {
4524       /* If this is a pure function, its olddecl will actually be
4525          the original initialization to `0' (which we force to call
4526          abort()).  Don't complain about redefinition in this case.  */
4527       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4528         return 0;
4529
4530       /* If both functions come from different namespaces, this is not
4531          a redeclaration - this is a conflict with a used function.  */
4532       if (DECL_NAMESPACE_SCOPE_P (olddecl)
4533           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4534         return "`%D' conflicts with used function";
4535
4536       /* We'll complain about linkage mismatches in
4537          warn_extern_redeclared_static.  */
4538
4539       /* Defining the same name twice is no good.  */
4540       if (DECL_INITIAL (olddecl) != NULL_TREE
4541           && DECL_INITIAL (newdecl) != NULL_TREE)
4542         {
4543           if (DECL_NAME (olddecl) == NULL_TREE)
4544             return "`%#D' not declared in class";
4545           else
4546             return "redefinition of `%#D'";
4547         }
4548       return 0;
4549     }
4550   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4551     {
4552       if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4553            && (DECL_TEMPLATE_RESULT (newdecl)
4554                != DECL_TEMPLATE_RESULT (olddecl))
4555            && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4556            && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4557           || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4558               && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4559               && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4560         return "redefinition of `%#D'";
4561       return 0;
4562     }
4563   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4564     {
4565       /* Objects declared at top level:  */
4566       /* If at least one is a reference, it's ok.  */
4567       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4568         return 0;
4569       /* Reject two definitions.  */
4570       return "redefinition of `%#D'";
4571     }
4572   else
4573     {
4574       /* Objects declared with block scope:  */
4575       /* Reject two definitions, and reject a definition
4576          together with an external reference.  */
4577       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4578         return "redeclaration of `%#D'";
4579       return 0;
4580     }
4581 }
4582 \f
4583 /* Create a new label, named ID.  */
4584
4585 static tree
4586 make_label_decl (tree id, int local_p)
4587 {
4588   tree decl;
4589
4590   decl = build_decl (LABEL_DECL, id, void_type_node);
4591   if (expanding_p)
4592     /* Make sure every label has an rtx.  */
4593     label_rtx (decl);
4594
4595   DECL_CONTEXT (decl) = current_function_decl;
4596   DECL_MODE (decl) = VOIDmode;
4597   C_DECLARED_LABEL_FLAG (decl) = local_p;
4598
4599   /* Say where one reference is to the label, for the sake of the
4600      error if it is not defined.  */
4601   DECL_SOURCE_LOCATION (decl) = input_location;
4602
4603   /* Record the fact that this identifier is bound to this label.  */
4604   SET_IDENTIFIER_LABEL_VALUE (id, decl);
4605
4606   return decl;
4607 }
4608
4609 /* Record this label on the list of used labels so that we can check
4610    at the end of the function to see whether or not the label was
4611    actually defined, and so we can check when the label is defined whether
4612    this use is valid.  */
4613
4614 static void
4615 use_label (tree decl)
4616 {
4617   if (named_label_uses == NULL
4618       || named_label_uses->names_in_scope != current_binding_level->names
4619       || named_label_uses->label_decl != decl)
4620     {
4621       struct named_label_use_list *new_ent;
4622       new_ent = ((struct named_label_use_list *)
4623                  ggc_alloc (sizeof (struct named_label_use_list)));
4624       new_ent->label_decl = decl;
4625       new_ent->names_in_scope = current_binding_level->names;
4626       new_ent->binding_level = current_binding_level;
4627       new_ent->o_goto_locus = input_location;
4628       new_ent->next = named_label_uses;
4629       named_label_uses = new_ent;
4630     }
4631 }
4632
4633 /* Look for a label named ID in the current function.  If one cannot
4634    be found, create one.  (We keep track of used, but undefined,
4635    labels, and complain about them at the end of a function.)  */
4636
4637 tree
4638 lookup_label (tree id)
4639 {
4640   tree decl;
4641   struct named_label_list *ent;
4642
4643   timevar_push (TV_NAME_LOOKUP);
4644   /* You can't use labels at global scope.  */
4645   if (current_function_decl == NULL_TREE)
4646     {
4647       error ("label `%s' referenced outside of any function",
4648              IDENTIFIER_POINTER (id));
4649       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4650     }
4651
4652   /* See if we've already got this label.  */
4653   decl = IDENTIFIER_LABEL_VALUE (id);
4654   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4655     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4656
4657   /* Record this label on the list of labels used in this function.
4658      We do this before calling make_label_decl so that we get the
4659      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
4660   ent = ((struct named_label_list *)
4661          ggc_alloc_cleared (sizeof (struct named_label_list)));
4662   ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4663   ent->next = named_labels;
4664   named_labels = ent;
4665
4666   /* We need a new label.  */
4667   decl = make_label_decl (id, /*local_p=*/0);
4668
4669   /* Now fill in the information we didn't have before.  */
4670   ent->label_decl = decl;
4671
4672   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4673 }
4674
4675 /* Declare a local label named ID.  */
4676
4677 tree
4678 declare_local_label (tree id)
4679 {
4680   tree decl;
4681
4682   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4683      this scope we can restore the old value of
4684      IDENTIFIER_TYPE_VALUE.  */
4685   current_binding_level->shadowed_labels
4686     = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4687                  current_binding_level->shadowed_labels);
4688   /* Look for the label.  */
4689   decl = make_label_decl (id, /*local_p=*/1);
4690   /* Now fill in the information we didn't have before.  */
4691   TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4692
4693   return decl;
4694 }
4695
4696 /* Returns nonzero if it is ill-formed to jump past the declaration of
4697    DECL.  Returns 2 if it's also a real problem.  */
4698
4699 static int
4700 decl_jump_unsafe (tree decl)
4701 {
4702   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4703     return 0;
4704
4705   if (DECL_INITIAL (decl) == NULL_TREE
4706       && pod_type_p (TREE_TYPE (decl)))
4707     return 0;
4708
4709   /* This is really only important if we're crossing an initialization.
4710      The POD stuff is just pedantry; why should it matter if the class
4711      contains a field of pointer to member type?  */
4712   if (DECL_INITIAL (decl)
4713       || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4714     return 2;
4715   return 1;
4716 }
4717
4718 /* Check that a single previously seen jump to a newly defined label
4719    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4720    the jump context; NAMES are the names in scope in LEVEL at the jump
4721    context; FILE and LINE are the source position of the jump or 0.  */
4722
4723 static void
4724 check_previous_goto_1 (tree decl,
4725                        struct cp_binding_level* level,
4726                        tree names, const location_t *locus)
4727 {
4728   int identified = 0;
4729   int saw_eh = 0;
4730   struct cp_binding_level *b = current_binding_level;
4731   for (; b; b = b->level_chain)
4732     {
4733       tree new_decls = b->names;
4734       tree old_decls = (b == level ? names : NULL_TREE);
4735       for (; new_decls != old_decls;
4736            new_decls = TREE_CHAIN (new_decls))
4737         {
4738           int problem = decl_jump_unsafe (new_decls);
4739           if (! problem)
4740             continue;
4741
4742           if (! identified)
4743             {
4744               if (decl)
4745                 pedwarn ("jump to label `%D'", decl);
4746               else
4747                 pedwarn ("jump to case label");
4748
4749               if (locus)
4750                 pedwarn ("%H  from here", locus);
4751               identified = 1;
4752             }
4753
4754           if (problem > 1)
4755             cp_error_at ("  crosses initialization of `%#D'",
4756                          new_decls);
4757           else
4758             cp_pedwarn_at ("  enters scope of non-POD `%#D'",
4759                            new_decls);
4760         }
4761
4762       if (b == level)
4763         break;
4764       if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4765         {
4766           if (! identified)
4767             {
4768               if (decl)
4769                 pedwarn ("jump to label `%D'", decl);
4770               else
4771                 pedwarn ("jump to case label");
4772
4773               if (locus)
4774                 pedwarn ("%H  from here", locus);
4775               identified = 1;
4776             }
4777           if (b->is_try_scope)
4778             error ("  enters try block");
4779           else
4780             error ("  enters catch block");
4781           saw_eh = 1;
4782         }
4783     }
4784 }
4785
4786 static void
4787 check_previous_goto (struct named_label_use_list* use)
4788 {
4789   check_previous_goto_1 (use->label_decl, use->binding_level,
4790                          use->names_in_scope, &use->o_goto_locus);
4791 }
4792
4793 static void
4794 check_switch_goto (struct cp_binding_level* level)
4795 {
4796   check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
4797 }
4798
4799 /* Check that any previously seen jumps to a newly defined label DECL
4800    are OK.  Called by define_label.  */
4801
4802 static void
4803 check_previous_gotos (tree decl)
4804 {
4805   struct named_label_use_list **usep;
4806
4807   if (! TREE_USED (decl))
4808     return;
4809
4810   for (usep = &named_label_uses; *usep; )
4811     {
4812       struct named_label_use_list *use = *usep;
4813       if (use->label_decl == decl)
4814         {
4815           check_previous_goto (use);
4816           *usep = use->next;
4817         }
4818       else
4819         usep = &(use->next);
4820     }
4821 }
4822
4823 /* Check that a new jump to a label DECL is OK.  Called by
4824    finish_goto_stmt.  */
4825
4826 void
4827 check_goto (tree decl)
4828 {
4829   int identified = 0;
4830   tree bad;
4831   struct named_label_list *lab;
4832
4833   /* We can't know where a computed goto is jumping.  So we assume
4834      that it's OK.  */
4835   if (! DECL_P (decl))
4836     return;
4837
4838   /* If the label hasn't been defined yet, defer checking.  */
4839   if (! DECL_INITIAL (decl))
4840     {
4841       use_label (decl);
4842       return;
4843     }
4844
4845   for (lab = named_labels; lab; lab = lab->next)
4846     if (decl == lab->label_decl)
4847       break;
4848
4849   /* If the label is not on named_labels it's a gcc local label, so
4850      it must be in an outer scope, so jumping to it is always OK.  */
4851   if (lab == 0)
4852     return;
4853
4854   if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4855       && !identified)
4856     {
4857       cp_pedwarn_at ("jump to label `%D'", decl);
4858       pedwarn ("  from here");
4859       identified = 1;
4860     }
4861
4862   for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4863     {
4864       tree b = TREE_VALUE (bad);
4865       int u = decl_jump_unsafe (b);
4866
4867       if (u > 1 && DECL_ARTIFICIAL (b))
4868         /* Can't skip init of __exception_info.  */
4869         cp_error_at ("  enters catch block", b);
4870       else if (u > 1)
4871         cp_error_at ("  skips initialization of `%#D'", b);
4872       else
4873         cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
4874     }
4875
4876   if (lab->in_try_scope)
4877     error ("  enters try block");
4878   else if (lab->in_catch_scope)
4879     error ("  enters catch block");
4880 }
4881
4882 /* Define a label, specifying the location in the source file.
4883    Return the LABEL_DECL node for the label, if the definition is valid.
4884    Otherwise return 0.  */
4885
4886 tree
4887 define_label (const char* filename, int line, tree name)
4888 {
4889   tree decl = lookup_label (name);
4890   struct named_label_list *ent;
4891   register struct cp_binding_level *p;
4892
4893   timevar_push (TV_NAME_LOOKUP);
4894   for (ent = named_labels; ent; ent = ent->next)
4895     if (ent->label_decl == decl)
4896       break;
4897
4898   /* After labels, make any new cleanups in the function go into their
4899      own new (temporary) binding contour.  */
4900   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4901     p->more_cleanups_ok = 0;
4902
4903   if (name == get_identifier ("wchar_t"))
4904     pedwarn ("label named wchar_t");
4905
4906   if (DECL_INITIAL (decl) != NULL_TREE)
4907     {
4908       error ("duplicate label `%D'", decl);
4909       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4910     }
4911   else
4912     {
4913       /* Mark label as having been defined.  */
4914       DECL_INITIAL (decl) = error_mark_node;
4915       /* Say where in the source.  */
4916       DECL_SOURCE_FILE (decl) = filename;
4917       DECL_SOURCE_LINE (decl) = line;
4918       if (ent)
4919         {
4920           ent->names_in_scope = current_binding_level->names;
4921           ent->binding_level = current_binding_level;
4922         }
4923       check_previous_gotos (decl);
4924       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4925     }
4926   timevar_pop (TV_NAME_LOOKUP);
4927 }
4928
4929 struct cp_switch
4930 {
4931   struct cp_binding_level *level;
4932   struct cp_switch *next;
4933   /* The SWITCH_STMT being built.  */
4934   tree switch_stmt;
4935   /* A splay-tree mapping the low element of a case range to the high
4936      element, or NULL_TREE if there is no high element.  Used to
4937      determine whether or not a new case label duplicates an old case
4938      label.  We need a tree, rather than simply a hash table, because
4939      of the GNU case range extension.  */
4940   splay_tree cases;
4941 };
4942
4943 /* A stack of the currently active switch statements.  The innermost
4944    switch statement is on the top of the stack.  There is no need to
4945    mark the stack for garbage collection because it is only active
4946    during the processing of the body of a function, and we never
4947    collect at that point.  */
4948
4949 static struct cp_switch *switch_stack;
4950
4951 /* Called right after a switch-statement condition is parsed.
4952    SWITCH_STMT is the switch statement being parsed.  */
4953
4954 void
4955 push_switch (tree switch_stmt)
4956 {
4957   struct cp_switch *p
4958     = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
4959   p->level = current_binding_level;
4960   p->next = switch_stack;
4961   p->switch_stmt = switch_stmt;
4962   p->cases = splay_tree_new (case_compare, NULL, NULL);
4963   switch_stack = p;
4964 }
4965
4966 void
4967 pop_switch (void)
4968 {
4969   struct cp_switch *cs;
4970
4971   cs = switch_stack;
4972   splay_tree_delete (cs->cases);
4973   switch_stack = switch_stack->next;
4974   free (cs);
4975 }
4976
4977 /* Note that we've seen a definition of a case label, and complain if this
4978    is a bad place for one.  */
4979
4980 tree
4981 finish_case_label (tree low_value, tree high_value)
4982 {
4983   tree cond, r;
4984   register struct cp_binding_level *p;
4985
4986   if (! switch_stack)
4987     {
4988       if (high_value)
4989         error ("case label not within a switch statement");
4990       else if (low_value)
4991         error ("case label `%E' not within a switch statement",
4992                   low_value);
4993       else
4994         error ("`default' label not within a switch statement");
4995       return NULL_TREE;
4996     }
4997
4998   if (processing_template_decl)
4999     {
5000       tree label;
5001
5002       /* For templates, just add the case label; we'll do semantic
5003          analysis at instantiation-time.  */
5004       label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5005       return add_stmt (build_case_label (low_value, high_value, label));
5006     }
5007
5008   /* Find the condition on which this switch statement depends.  */
5009   cond = SWITCH_COND (switch_stack->switch_stmt);
5010   if (cond && TREE_CODE (cond) == TREE_LIST)
5011     cond = TREE_VALUE (cond);
5012
5013   r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5014
5015   check_switch_goto (switch_stack->level);
5016
5017   /* After labels, make any new cleanups in the function go into their
5018      own new (temporary) binding contour.  */
5019   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5020     p->more_cleanups_ok = 0;
5021
5022   return r;
5023 }
5024 \f
5025 /* Return the list of declarations of the current level.
5026    Note that this list is in reverse order unless/until
5027    you nreverse it; and when you do nreverse it, you must
5028    store the result back using `storedecls' or you will lose.  */
5029
5030 tree
5031 getdecls (void)
5032 {
5033   return current_binding_level->names;
5034 }
5035
5036 /* Return the list of type-tags (for structs, etc) of the current level.  */
5037
5038 tree
5039 gettags (void)
5040 {
5041   return current_binding_level->tags;
5042 }
5043
5044 /* Store the list of declarations of the current level.
5045    This is done for the parameter declarations of a function being defined,
5046    after they are modified in the light of any missing parameters.  */
5047
5048 static void
5049 storedecls (tree decls)
5050 {
5051   current_binding_level->names = decls;
5052 }
5053
5054 /* Similarly, store the list of tags of the current level.  */
5055
5056 void
5057 storetags (tree tags)
5058 {
5059   current_binding_level->tags = tags;
5060 }
5061 \f
5062 /* Return the type that should be used when TYPE's name is preceded
5063    by a tag such as 'struct' or 'union', or null if the name cannot
5064    be used in this way.
5065
5066    For example, when processing the third line of:
5067
5068         struct A;
5069         typedef struct A A;
5070         struct A;
5071
5072    lookup of A will find the typedef.  Given A's typedef, this function
5073    will return the type associated with "struct A".  For the tag to be
5074    anything other than TYPE, TYPE must be a typedef whose original type
5075    has the same name and context as TYPE itself.
5076
5077    It is not valid for a typedef of an anonymous type to be used with
5078    an explicit tag:
5079
5080        typedef struct { ... } B;
5081        struct B;
5082
5083    Return null for this case.  */
5084
5085 static tree
5086 follow_tag_typedef (tree type)
5087 {
5088   tree original;
5089
5090   original = original_type (type);
5091   if (! TYPE_NAME (original))
5092     return NULL_TREE;
5093   if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5094       && (CP_DECL_CONTEXT (TYPE_NAME (original))
5095           == CP_DECL_CONTEXT (TYPE_NAME (type)))
5096       && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5097     return original;
5098   else
5099     return NULL_TREE;
5100 }
5101
5102 /* Given NAME, an IDENTIFIER_NODE,
5103    return the structure (or union or enum) definition for that name.
5104    Searches binding levels from BINDING_SCOPE up to the global level.
5105    If THISLEVEL_ONLY is nonzero, searches only the specified context
5106    (but skips any tag-transparent contexts to find one that is
5107    meaningful for tags).
5108    FORM says which kind of type the caller wants;
5109    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5110    If the wrong kind of type is found, and it's not a template, an error is
5111    reported.  */
5112
5113 static tree
5114 lookup_tag (enum tree_code form, tree name,
5115             struct cp_binding_level* binding_level, int thislevel_only)
5116 {
5117   register struct cp_binding_level *level;
5118   /* Nonzero if, we should look past a template parameter level, even
5119      if THISLEVEL_ONLY.  */
5120   int allow_template_parms_p = 1;
5121
5122   timevar_push (TV_NAME_LOOKUP);
5123   for (level = binding_level; level; level = level->level_chain)
5124     {
5125       register tree tail;
5126       if (ANON_AGGRNAME_P (name))
5127         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5128           {
5129             /* There's no need for error checking here, because
5130                anon names are unique throughout the compilation.  */
5131             if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5132               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5133           }
5134       else if (level->namespace_p)
5135         /* Do namespace lookup.  */
5136         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5137           {
5138             cxx_binding *binding =
5139               cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
5140             tree old;
5141
5142             /* If we just skipped past a template parameter level,
5143                even though THISLEVEL_ONLY, and we find a template
5144                class declaration, then we use the _TYPE node for the
5145                template.  See the example below.  */
5146             if (thislevel_only && !allow_template_parms_p
5147                 && binding && BINDING_VALUE (binding)
5148                 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5149               old = TREE_TYPE (BINDING_VALUE (binding));
5150             else if (binding)
5151               old = BINDING_TYPE (binding);
5152             else
5153               old = NULL_TREE;
5154
5155             if (old)
5156               {
5157                 /* We've found something at this binding level.  If it is
5158                    a typedef, extract the tag it refers to.  Lookup fails
5159                    if the typedef doesn't refer to a taggable type.  */
5160                 old = follow_tag_typedef (old);
5161                 if (!old)
5162                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5163                 if (TREE_CODE (old) != form
5164                     && (form == ENUMERAL_TYPE
5165                         || TREE_CODE (old) == ENUMERAL_TYPE))
5166                   {
5167                     error ("`%#D' redeclared as %C", old, form);
5168                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5169                   }
5170                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5171               }
5172             if (thislevel_only || tail == global_namespace)
5173               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5174           }
5175       else
5176         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5177           {
5178             if (TREE_PURPOSE (tail) == name)
5179               {
5180                 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5181                 
5182                 if (code != form
5183                     && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5184                   {
5185                     /* Definition isn't the kind we were looking for.  */
5186                     error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5187                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5188                   }
5189                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5190               }
5191           }
5192       if (thislevel_only && ! level->tag_transparent)
5193         {
5194           if (level->template_parms_p && allow_template_parms_p)
5195             {
5196               /* We must deal with cases like this:
5197
5198                    template <class T> struct S;
5199                    template <class T> struct S {};
5200
5201                  When looking up `S', for the second declaration, we
5202                  would like to find the first declaration.  But, we
5203                  are in the pseudo-global level created for the
5204                  template parameters, rather than the (surrounding)
5205                  namespace level.  Thus, we keep going one more level,
5206                  even though THISLEVEL_ONLY is nonzero.  */
5207               allow_template_parms_p = 0;
5208               continue;
5209             }
5210           else
5211             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5212         }
5213     }
5214   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5215 }
5216
5217 /* Given a type, find the tag that was defined for it and return the tag name.
5218    Otherwise return 0.  However, the value can never be 0
5219    in the cases in which this is used.
5220
5221    C++: If NAME is nonzero, this is the new name to install.  This is
5222    done when replacing anonymous tags with real tag names.  */
5223
5224 static tree
5225 lookup_tag_reverse (tree type, tree name)
5226 {
5227   register struct cp_binding_level *level;
5228
5229   timevar_push (TV_NAME_LOOKUP);
5230   for (level = current_binding_level; level; level = level->level_chain)
5231     {
5232       register tree tail;
5233       for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5234         {
5235           if (TREE_VALUE (tail) == type)
5236             {
5237               if (name)
5238                 TREE_PURPOSE (tail) = name;
5239               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_PURPOSE (tail));
5240             }
5241         }
5242     }
5243   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5244 }
5245 \f
5246 /* Look up NAME in the NAMESPACE.  */
5247
5248 tree
5249 lookup_namespace_name (tree namespace, tree name)
5250 {
5251   tree val;
5252   tree template_id = NULL_TREE;
5253   cxx_binding binding;
5254
5255   timevar_push (TV_NAME_LOOKUP);
5256   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5257
5258   if (TREE_CODE (name) == NAMESPACE_DECL)
5259     /* This happens for A::B<int> when B is a namespace.  */
5260     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5261   else if (TREE_CODE (name) == TEMPLATE_DECL)
5262     {
5263       /* This happens for A::B where B is a template, and there are no
5264          template arguments.  */
5265       error ("invalid use of `%D'", name);
5266       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5267     }
5268
5269   namespace = ORIGINAL_NAMESPACE (namespace);
5270
5271   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5272     {
5273       template_id = name;
5274       name = TREE_OPERAND (name, 0);
5275       if (TREE_CODE (name) == OVERLOAD)
5276         name = DECL_NAME (OVL_CURRENT (name));
5277       else if (DECL_P (name))
5278         name = DECL_NAME (name);
5279     }
5280
5281   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5282
5283   cxx_binding_clear (&binding);
5284   if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5285     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5286
5287   if (binding.value)
5288     {
5289       val = binding.value;
5290
5291       if (template_id)
5292         {
5293           if (DECL_CLASS_TEMPLATE_P (val))
5294             val = lookup_template_class (val,
5295                                          TREE_OPERAND (template_id, 1),
5296                                          /*in_decl=*/NULL_TREE,
5297                                          /*context=*/NULL_TREE,
5298                                          /*entering_scope=*/0,
5299                                          tf_error | tf_warning);
5300           else if (DECL_FUNCTION_TEMPLATE_P (val)
5301                    || TREE_CODE (val) == OVERLOAD)
5302             val = lookup_template_function (val,
5303                                             TREE_OPERAND (template_id, 1));
5304           else
5305             {
5306               error ("`%D::%D' is not a template",
5307                         namespace, name);
5308               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5309             }
5310         }
5311
5312       /* If we have a single function from a using decl, pull it out.  */
5313       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5314         val = OVL_FUNCTION (val);
5315
5316       /* Ignore built-in functions that haven't been prototyped yet.  */
5317       if (!val || !DECL_P(val)
5318           || !DECL_LANG_SPECIFIC(val)
5319           || !DECL_ANTICIPATED (val))
5320         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5321     }
5322
5323   error ("`%D' undeclared in namespace `%D'", name, namespace);
5324   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5325 }
5326
5327 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5328
5329 static hashval_t
5330 typename_hash (const void* k)
5331 {
5332   hashval_t hash;
5333   tree t = (tree) k;
5334
5335   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5336           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5337
5338   return hash;
5339 }
5340
5341 /* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5342
5343 static int
5344 typename_compare (const void * k1, const void * k2)
5345 {
5346   tree t1;
5347   tree t2;
5348   tree d1;
5349   tree d2;
5350
5351   t1 = (tree) k1;
5352   t2 = (tree) k2;
5353   d1 = TYPE_NAME (t1);
5354   d2 = TYPE_NAME (t2);
5355
5356   return (DECL_NAME (d1) == DECL_NAME (d2)
5357           && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5358           && ((TREE_TYPE (t1) != NULL_TREE)
5359               == (TREE_TYPE (t2) != NULL_TREE))
5360           && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5361           && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5362 }
5363
5364 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5365    the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5366    is non-NULL, this type is being created by the implicit typename
5367    extension, and BASE_TYPE is a type named `t' in some base class of
5368    `T' which depends on template parameters.
5369
5370    Returns the new TYPENAME_TYPE.  */
5371
5372 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5373
5374 tree
5375 build_typename_type (tree context, tree name, tree fullname)
5376 {
5377   tree t;
5378   tree d;
5379   PTR *e;
5380
5381   if (typename_htab == NULL)
5382     {
5383       typename_htab = htab_create_ggc (61, &typename_hash, 
5384                                        &typename_compare, NULL);
5385     }
5386
5387   /* Build the TYPENAME_TYPE.  */
5388   t = make_aggr_type (TYPENAME_TYPE);
5389   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5390   TYPENAME_TYPE_FULLNAME (t) = fullname;
5391
5392   /* Build the corresponding TYPE_DECL.  */
5393   d = build_decl (TYPE_DECL, name, t);
5394   TYPE_NAME (TREE_TYPE (d)) = d;
5395   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5396   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5397   DECL_ARTIFICIAL (d) = 1;
5398
5399   /* See if we already have this type.  */
5400   e = htab_find_slot (typename_htab, t, INSERT);
5401   if (*e)
5402     t = (tree) *e;
5403   else
5404     *e = t;
5405
5406   return t;
5407 }
5408
5409 /* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5410    unless an error occurs, in which case error_mark_node is returned.
5411    If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5412    set, we return that, rather than the _TYPE it corresponds to, in
5413    other cases we look through the type decl.  If TF_ERROR is set,
5414    complain about errors, otherwise be quiet.  */
5415
5416 tree
5417 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5418 {
5419   tree fullname;
5420
5421   if (name == error_mark_node
5422       || context == NULL_TREE
5423       || context == error_mark_node)
5424     return error_mark_node;
5425
5426   if (TYPE_P (name))
5427     {
5428       if (!(TYPE_LANG_SPECIFIC (name)
5429             && (CLASSTYPE_IS_TEMPLATE (name)
5430                 || CLASSTYPE_USE_TEMPLATE (name))))
5431         name = TYPE_IDENTIFIER (name);
5432       else
5433         /* Create a TEMPLATE_ID_EXPR for the type.  */
5434         name = build_nt (TEMPLATE_ID_EXPR,
5435                          CLASSTYPE_TI_TEMPLATE (name),
5436                          CLASSTYPE_TI_ARGS (name));
5437     }
5438   else if (TREE_CODE (name) == TYPE_DECL)
5439     name = DECL_NAME (name);
5440
5441   fullname = name;
5442
5443   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5444     {
5445       name = TREE_OPERAND (name, 0);
5446       if (TREE_CODE (name) == TEMPLATE_DECL)
5447         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5448     }
5449   if (TREE_CODE (name) == TEMPLATE_DECL)
5450     {
5451       error ("`%D' used without template parameters", name);
5452       return error_mark_node;
5453     }
5454   if (TREE_CODE (name) != IDENTIFIER_NODE)
5455     abort ();
5456
5457   if (TREE_CODE (context) == NAMESPACE_DECL)
5458     {
5459       /* We can get here from typename_sub0 in the explicit_template_type
5460          expansion.  Just fail.  */
5461       if (complain & tf_error)
5462         error ("no class template named `%#T' in `%#T'",
5463                   name, context);
5464       return error_mark_node;
5465     }
5466
5467   if (! uses_template_parms (context)
5468       || currently_open_class (context))
5469     {
5470       if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5471         {
5472           tree tmpl = NULL_TREE;
5473           if (IS_AGGR_TYPE (context))
5474             tmpl = lookup_field (context, name, 0, false);
5475           if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5476             {
5477               if (complain & tf_error)
5478                 error ("no class template named `%#T' in `%#T'",
5479                           name, context);
5480               return error_mark_node;
5481             }
5482
5483           if (complain & tf_error)
5484             {
5485               if (complain & tf_parsing)
5486                 perform_or_defer_access_check (context, tmpl);
5487               else
5488                 enforce_access (context, tmpl);
5489             }
5490
5491           return lookup_template_class (tmpl,
5492                                         TREE_OPERAND (fullname, 1),
5493                                         NULL_TREE, context,
5494                                         /*entering_scope=*/0,
5495                                         tf_error | tf_warning);
5496         }
5497       else
5498         {
5499           tree t;
5500
5501           if (!IS_AGGR_TYPE (context))
5502             {
5503               if (complain & tf_error)
5504                 error ("no type named `%#T' in `%#T'", name, context);
5505               return error_mark_node;
5506             }
5507
5508           t = lookup_field (context, name, 0, true);
5509           if (t)
5510             {
5511               if (TREE_CODE (t) != TYPE_DECL)
5512                 {
5513                   if (complain & tf_error)
5514                     error ("no type named `%#T' in `%#T'", name, context);
5515                   return error_mark_node;
5516                 }
5517
5518               if (complain & tf_error)
5519                 {
5520                   if (complain & tf_parsing)
5521                     perform_or_defer_access_check (context, t);
5522                   else
5523                     enforce_access (context, t);
5524                 }
5525
5526               if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5527                 t = TREE_TYPE (t);
5528               
5529               return t;
5530             }
5531         }
5532     }
5533
5534   /* If the CONTEXT is not a template type, then either the field is
5535      there now or its never going to be.  */
5536   if (!uses_template_parms (context))
5537     {
5538       if (complain & tf_error)
5539         error ("no type named `%#T' in `%#T'", name, context);
5540       return error_mark_node;
5541     }
5542
5543   return build_typename_type (context, name, fullname);
5544 }
5545
5546 /* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
5547    unless an error occurs, in which case error_mark_node is returned.
5548    If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5549    corresponds to.  If COMPLAIN zero, don't complain about any errors
5550    that occur.  */
5551
5552 tree
5553 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5554 {
5555   tree t;
5556   tree d;
5557
5558   if (TYPE_P (name))
5559     name = TYPE_IDENTIFIER (name);
5560   else if (DECL_P (name))
5561     name = DECL_NAME (name);
5562   if (TREE_CODE (name) != IDENTIFIER_NODE)
5563     abort ();
5564
5565   if (!uses_template_parms (context)
5566       || currently_open_class (context))
5567     {
5568       tree tmpl = NULL_TREE;
5569
5570       if (IS_AGGR_TYPE (context))
5571         tmpl = lookup_field (context, name, 0, false);
5572
5573       if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5574         {
5575           if (complain & tf_error)
5576             error ("no class template named `%#T' in `%#T'", name, context);
5577           return error_mark_node;
5578         }
5579       
5580       if (complain & tf_error)
5581         {
5582           if (complain & tf_parsing)
5583             perform_or_defer_access_check (context, tmpl);
5584           else
5585             enforce_access (context, tmpl);
5586         }
5587
5588       return tmpl;
5589     }
5590
5591   /* Build the UNBOUND_CLASS_TEMPLATE.  */
5592   t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5593   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5594   TREE_TYPE (t) = NULL_TREE;
5595
5596   /* Build the corresponding TEMPLATE_DECL.  */
5597   d = build_decl (TEMPLATE_DECL, name, t);
5598   TYPE_NAME (TREE_TYPE (d)) = d;
5599   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5600   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5601   DECL_ARTIFICIAL (d) = 1;
5602
5603   return t;
5604 }
5605
5606 /* Select the right _DECL from multiple choices.  */
5607
5608 static tree
5609 select_decl (cxx_binding *binding, int flags)
5610 {
5611   tree val;
5612   val = BINDING_VALUE (binding);
5613
5614   timevar_push (TV_NAME_LOOKUP);
5615   if (LOOKUP_NAMESPACES_ONLY (flags))
5616     {
5617       /* We are not interested in types.  */
5618       if (val && TREE_CODE (val) == NAMESPACE_DECL)
5619         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5620       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5621     }
5622
5623   /* If we could have a type and
5624      we have nothing or we need a type and have none.  */
5625   if (BINDING_TYPE (binding)
5626       && (!val || ((flags & LOOKUP_PREFER_TYPES)
5627                    && TREE_CODE (val) != TYPE_DECL)))
5628     val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5629   /* Don't return non-types if we really prefer types.  */
5630   else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5631            && (TREE_CODE (val) != TEMPLATE_DECL
5632                || !DECL_CLASS_TEMPLATE_P (val)))
5633     val = NULL_TREE;
5634
5635   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5636 }
5637
5638 /* Unscoped lookup of a global: iterate over current namespaces,
5639    considering using-directives.  If SPACESP is non-NULL, store a list
5640    of the namespaces we've considered in it.  */
5641
5642 tree
5643 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5644 {
5645   tree initial = current_decl_namespace ();
5646   tree scope = initial;
5647   tree siter;
5648   struct cp_binding_level *level;
5649   tree val = NULL_TREE;
5650   cxx_binding binding;
5651
5652   timevar_push (TV_NAME_LOOKUP);
5653   cxx_binding_clear (&binding);
5654   if (spacesp)
5655     *spacesp = NULL_TREE;
5656
5657   for (; !val; scope = CP_DECL_CONTEXT (scope))
5658     {
5659       cxx_binding *b =
5660          cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5661       if (spacesp)
5662         *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5663
5664       /* Ignore anticipated built-in functions.  */
5665       if (b && BINDING_VALUE (b)
5666           && DECL_P (BINDING_VALUE (b))
5667           && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5668           && DECL_ANTICIPATED (BINDING_VALUE (b)))
5669         /* Keep binding cleared.  */;
5670       else if (b)
5671         {
5672           /* Initialize binding for this context.  */
5673           binding.value = BINDING_VALUE (b);
5674           binding.type = BINDING_TYPE (b);
5675         }
5676
5677       /* Add all _DECLs seen through local using-directives.  */
5678       for (level = current_binding_level;
5679            !level->namespace_p;
5680            level = level->level_chain)
5681         if (!lookup_using_namespace (name, &binding, level->using_directives,
5682                                      scope, flags, spacesp))
5683           /* Give up because of error.  */
5684           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5685
5686       /* Add all _DECLs seen through global using-directives.  */
5687       /* XXX local and global using lists should work equally.  */
5688       siter = initial;
5689       while (1)
5690         {
5691           if (!lookup_using_namespace (name, &binding,
5692                                        DECL_NAMESPACE_USING (siter),
5693                                        scope, flags, spacesp))
5694             /* Give up because of error.  */
5695             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5696           if (siter == scope) break;
5697           siter = CP_DECL_CONTEXT (siter);
5698         }
5699
5700       val = select_decl (&binding, flags);
5701       if (scope == global_namespace)
5702         break;
5703     }
5704   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5705 }
5706
5707 /* Combine prefer_type and namespaces_only into flags.  */
5708
5709 static int
5710 lookup_flags (int prefer_type, int namespaces_only)
5711 {
5712   if (namespaces_only)
5713     return LOOKUP_PREFER_NAMESPACES;
5714   if (prefer_type > 1)
5715     return LOOKUP_PREFER_TYPES;
5716   if (prefer_type > 0)
5717     return LOOKUP_PREFER_BOTH;
5718   return 0;
5719 }
5720
5721 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5722    ignore it or not.  Subroutine of lookup_name_real.  */
5723
5724 static tree
5725 qualify_lookup (tree val, int flags)
5726 {
5727   if (val == NULL_TREE)
5728     return val;
5729   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5730     return val;
5731   if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5732     return val;
5733   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5734     return NULL_TREE;
5735   return val;
5736 }
5737
5738 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5739    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
5740    bindings.  
5741
5742    Returns a DECL (or OVERLOAD, or BASELINK) representing the
5743    declaration found.  */
5744
5745 tree
5746 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5747 {
5748   if (TREE_CODE (scope) == NAMESPACE_DECL)
5749     {
5750       cxx_binding binding;
5751
5752       cxx_binding_clear (&binding);
5753       flags |= LOOKUP_COMPLAIN;
5754       if (is_type_p)
5755         flags |= LOOKUP_PREFER_TYPES;
5756       if (!qualified_lookup_using_namespace (name, scope, &binding, flags))
5757         return NULL_TREE;
5758       return select_decl (&binding, flags);
5759     }
5760   else
5761     return lookup_member (scope, name, 0, is_type_p);
5762 }
5763
5764 /* Check to see whether or not DECL is a variable that would have been
5765    in scope under the ARM, but is not in scope under the ANSI/ISO
5766    standard.  If so, issue an error message.  If name lookup would
5767    work in both cases, but return a different result, this function
5768    returns the result of ANSI/ISO lookup.  Otherwise, it returns
5769    DECL.  */
5770
5771 tree
5772 check_for_out_of_scope_variable (tree decl)
5773 {
5774   tree shadowed;
5775
5776   /* We only care about out of scope variables.  */
5777   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5778     return decl;
5779
5780   shadowed = DECL_SHADOWED_FOR_VAR (decl);
5781   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5782          && DECL_DEAD_FOR_LOCAL (shadowed))
5783     shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5784   if (!shadowed)
5785     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5786   if (shadowed)
5787     {
5788       if (!DECL_ERROR_REPORTED (decl))
5789         {
5790           warning ("name lookup of `%D' changed",
5791                       DECL_NAME (decl));
5792           cp_warning_at ("  matches this `%D' under ISO standard rules",
5793                          shadowed);
5794           cp_warning_at ("  matches this `%D' under old rules", decl);
5795           DECL_ERROR_REPORTED (decl) = 1;
5796         }
5797       return shadowed;
5798     }
5799
5800   /* If we have already complained about this declaration, there's no
5801      need to do it again.  */
5802   if (DECL_ERROR_REPORTED (decl))
5803     return decl;
5804
5805   DECL_ERROR_REPORTED (decl) = 1;
5806   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5807     {
5808       error ("name lookup of `%D' changed for new ISO `for' scoping",
5809              DECL_NAME (decl));
5810       cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
5811       return error_mark_node;
5812     }
5813   else
5814     {
5815       pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5816                DECL_NAME (decl));
5817       cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
5818     }
5819
5820   return decl;
5821 }
5822
5823 /* Look up NAME in the current binding level and its superiors in the
5824    namespace of variables, functions and typedefs.  Return a ..._DECL
5825    node of some kind representing its definition if there is only one
5826    such declaration, or return a TREE_LIST with all the overloaded
5827    definitions if there are many, or return 0 if it is undefined.
5828
5829    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5830    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5831    Otherwise we prefer non-TYPE_DECLs.
5832
5833    If NONCLASS is nonzero, we don't look for the NAME in class scope,
5834    using IDENTIFIER_CLASS_VALUE.  */
5835
5836 tree
5837 lookup_name_real (tree name, int prefer_type, int nonclass, 
5838                   int namespaces_only, int flags)
5839 {
5840   cxx_binding *iter;
5841   tree val = NULL_TREE;
5842
5843   timevar_push (TV_NAME_LOOKUP);
5844   /* Conversion operators are handled specially because ordinary
5845      unqualified name lookup will not find template conversion
5846      operators.  */
5847   if (IDENTIFIER_TYPENAME_P (name)) 
5848     {
5849       struct cp_binding_level *level;
5850
5851       for (level = current_binding_level; 
5852            level && !level->namespace_p; 
5853            level = level->level_chain)
5854         {
5855           tree class_type;
5856           tree operators;
5857           
5858           /* A conversion operator can only be declared in a class 
5859              scope.  */
5860           if (level->parm_flag != 2)
5861             continue;
5862           
5863           /* Lookup the conversion operator in the class.  */
5864           class_type = level->this_class;
5865           operators = lookup_fnfields (class_type, name, /*protect=*/0);
5866           if (operators)
5867             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5868         }
5869
5870       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5871     }
5872
5873   flags |= lookup_flags (prefer_type, namespaces_only);
5874
5875   /* First, look in non-namespace scopes.  */
5876
5877   if (current_class_type == NULL_TREE)
5878     nonclass = 1;
5879
5880   for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5881     {
5882       tree binding;
5883
5884       if (!LOCAL_BINDING_P (iter) && nonclass)
5885         /* We're not looking for class-scoped bindings, so keep going.  */
5886         continue;
5887
5888       /* If this is the kind of thing we're looking for, we're done.  */
5889       if (qualify_lookup (BINDING_VALUE (iter), flags))
5890         binding = BINDING_VALUE (iter);
5891       else if ((flags & LOOKUP_PREFER_TYPES)
5892                && qualify_lookup (BINDING_TYPE (iter), flags))
5893         binding = BINDING_TYPE (iter);
5894       else
5895         binding = NULL_TREE;
5896
5897       if (binding)
5898         {
5899           val = binding;
5900           break;
5901         }
5902     }
5903
5904   /* Now lookup in namespace scopes.  */
5905   if (!val)
5906     {
5907       tree t = unqualified_namespace_lookup (name, flags, 0);
5908       if (t)
5909         val = t;
5910     }
5911
5912   if (val)
5913     {
5914       /* If we have a single function from a using decl, pull it out.  */
5915       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5916         val = OVL_FUNCTION (val);
5917     }
5918
5919   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5920 }
5921
5922 tree
5923 lookup_name_nonclass (tree name)
5924 {
5925   return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5926 }
5927
5928 tree
5929 lookup_function_nonclass (tree name, tree args)
5930 {
5931   return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5932 }
5933
5934 tree
5935 lookup_name (tree name, int prefer_type)
5936 {
5937   return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5938 }
5939
5940 /* Similar to `lookup_name' but look only in the innermost non-class
5941    binding level.  */
5942
5943 tree
5944 lookup_name_current_level (tree name)
5945 {
5946   struct cp_binding_level *b;
5947   tree t = NULL_TREE;
5948
5949   timevar_push (TV_NAME_LOOKUP);
5950   b = current_binding_level;
5951   while (b->parm_flag == 2)
5952     b = b->level_chain;
5953
5954   if (b->namespace_p)
5955     {
5956       t = IDENTIFIER_NAMESPACE_VALUE (name);
5957
5958       /* extern "C" function() */
5959       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5960         t = TREE_VALUE (t);
5961     }
5962   else if (IDENTIFIER_BINDING (name)
5963            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5964     {
5965       while (1)
5966         {
5967           if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b)
5968             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
5969
5970           if (b->keep == 2)
5971             b = b->level_chain;
5972           else
5973             break;
5974         }
5975     }
5976
5977   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5978 }
5979
5980 /* Like lookup_name_current_level, but for types.  */
5981
5982 tree
5983 lookup_type_current_level (tree name)
5984 {
5985   register tree t = NULL_TREE;
5986
5987   timevar_push (TV_NAME_LOOKUP);
5988   my_friendly_assert (! current_binding_level->namespace_p, 980716);
5989
5990   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5991       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5992     {
5993       struct cp_binding_level *b = current_binding_level;
5994       while (1)
5995         {
5996           if (purpose_member (name, b->type_shadowed))
5997             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5998                                     REAL_IDENTIFIER_TYPE_VALUE (name));
5999           if (b->keep == 2)
6000             b = b->level_chain;
6001           else
6002             break;
6003         }
6004     }
6005
6006   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6007 }
6008
6009 \f
6010 /* Push the declarations of builtin types into the namespace.
6011    RID_INDEX is the index of the builtin type
6012    in the array RID_POINTERS.  NAME is the name used when looking
6013    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6014
6015 void
6016 record_builtin_type (enum rid rid_index, 
6017                      const char* name, 
6018                      tree type)
6019 {
6020   tree rname = NULL_TREE, tname = NULL_TREE;
6021   tree tdecl = NULL_TREE;
6022
6023   if ((int) rid_index < (int) RID_MAX)
6024     rname = ridpointers[(int) rid_index];
6025   if (name)
6026     tname = get_identifier (name);
6027
6028   if (tname)
6029     {
6030       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6031       set_identifier_type_value (tname, NULL_TREE);
6032       if ((int) rid_index < (int) RID_MAX)
6033         /* Built-in types live in the global namespace.  */
6034         SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6035     }
6036   if (rname != NULL_TREE)
6037     {
6038       if (tname != NULL_TREE)
6039         {
6040           set_identifier_type_value (rname, NULL_TREE);
6041           SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6042         }
6043       else
6044         {
6045           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6046           set_identifier_type_value (rname, NULL_TREE);
6047         }
6048     }
6049 }
6050
6051 /* Record one of the standard Java types.
6052  * Declare it as having the given NAME.
6053  * If SIZE > 0, it is the size of one of the integral types;
6054  * otherwise it is the negative of the size of one of the other types.  */
6055
6056 static tree
6057 record_builtin_java_type (const char* name, int size)
6058 {
6059   tree type, decl;
6060   if (size > 0)
6061     type = make_signed_type (size);
6062   else if (size > -32)
6063     { /* "__java_char" or ""__java_boolean".  */
6064       type = make_unsigned_type (-size);
6065       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6066     }
6067   else
6068     { /* "__java_float" or ""__java_double".  */
6069       type = make_node (REAL_TYPE);
6070       TYPE_PRECISION (type) = - size;
6071       layout_type (type);
6072     }
6073   record_builtin_type (RID_MAX, name, type);
6074   decl = TYPE_NAME (type);
6075
6076   /* Suppress generate debug symbol entries for these types,
6077      since for normal C++ they are just clutter.
6078      However, push_lang_context undoes this if extern "Java" is seen.  */
6079   DECL_IGNORED_P (decl) = 1;
6080
6081   TYPE_FOR_JAVA (type) = 1;
6082   return type;
6083 }
6084
6085 /* Push a type into the namespace so that the back-ends ignore it.  */
6086
6087 static void
6088 record_unknown_type (tree type, const char* name)
6089 {
6090   tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6091   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6092   DECL_IGNORED_P (decl) = 1;
6093   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6094   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6095   TYPE_ALIGN (type) = 1;
6096   TYPE_USER_ALIGN (type) = 0;
6097   TYPE_MODE (type) = TYPE_MODE (void_type_node);
6098 }
6099
6100 /* An string for which we should create an IDENTIFIER_NODE at
6101    startup.  */
6102
6103 typedef struct predefined_identifier
6104 {
6105   /* The name of the identifier.  */
6106   const char *const name;
6107   /* The place where the IDENTIFIER_NODE should be stored.  */
6108   tree *const node;
6109   /* Nonzero if this is the name of a constructor or destructor.  */
6110   const int ctor_or_dtor_p;
6111 } predefined_identifier;
6112
6113 /* Create all the predefined identifiers.  */
6114
6115 static void
6116 initialize_predefined_identifiers (void)
6117 {
6118   const predefined_identifier *pid;
6119
6120   /* A table of identifiers to create at startup.  */
6121   static const predefined_identifier predefined_identifiers[] = {
6122     { "C++", &lang_name_cplusplus, 0 },
6123     { "C", &lang_name_c, 0 },
6124     { "Java", &lang_name_java, 0 },
6125     { CTOR_NAME, &ctor_identifier, 1 },
6126     { "__base_ctor", &base_ctor_identifier, 1 },
6127     { "__comp_ctor", &complete_ctor_identifier, 1 },
6128     { DTOR_NAME, &dtor_identifier, 1 },
6129     { "__comp_dtor", &complete_dtor_identifier, 1 },
6130     { "__base_dtor", &base_dtor_identifier, 1 },
6131     { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6132     { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6133     { "nelts", &nelts_identifier, 0 },
6134     { THIS_NAME, &this_identifier, 0 },
6135     { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6136     { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6137     { "_vptr", &vptr_identifier, 0 },
6138     { "__vtt_parm", &vtt_parm_identifier, 0 },
6139     { "std", &std_identifier, 0 },
6140     { NULL, NULL, 0 }
6141   };
6142
6143   for (pid = predefined_identifiers; pid->name; ++pid)
6144     {
6145       *pid->node = get_identifier (pid->name);
6146       if (pid->ctor_or_dtor_p)
6147         IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6148     }
6149 }
6150
6151 /* Create the predefined scalar types of C,
6152    and some nodes representing standard constants (0, 1, (void *)0).
6153    Initialize the global binding level.
6154    Make definitions for built-in primitive functions.  */
6155
6156 void
6157 cxx_init_decl_processing (void)
6158 {
6159   tree void_ftype;
6160   tree void_ftype_ptr;
6161
6162   /* Create all the identifiers we need.  */
6163   initialize_predefined_identifiers ();
6164
6165   /* Fill in back-end hooks.  */
6166   lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6167
6168   /* Create the global variables.  */
6169   push_to_top_level ();
6170
6171   /* Enter the global namespace.  */
6172   my_friendly_assert (global_namespace == NULL_TREE, 375);
6173   push_namespace (get_identifier ("::"));
6174   global_namespace = current_namespace;
6175   current_lang_name = NULL_TREE;
6176
6177   /* Adjust various flags based on command-line settings.  */
6178   if (! flag_permissive && ! pedantic)
6179     flag_pedantic_errors = 1;
6180   if (!flag_no_inline)
6181     {
6182       flag_inline_trees = 1;
6183       flag_no_inline = 1;
6184     }
6185   if (flag_inline_functions)
6186     {
6187       flag_inline_trees = 2;
6188       flag_inline_functions = 0;
6189     }
6190
6191   /* Force minimum function alignment if using the least significant
6192      bit of function pointers to store the virtual bit.  */
6193   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6194       && force_align_functions_log < 1)
6195     force_align_functions_log = 1;
6196
6197   /* Initially, C.  */
6198   current_lang_name = lang_name_c;
6199
6200   current_function_decl = NULL_TREE;
6201   current_binding_level = NULL_BINDING_LEVEL;
6202   free_binding_level = NULL_BINDING_LEVEL;
6203
6204   build_common_tree_nodes (flag_signed_char);
6205
6206   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6207   TREE_TYPE (error_mark_list) = error_mark_node;
6208
6209   /* Make the binding_level structure for global names.  */
6210   pushlevel (0);
6211   global_binding_level = current_binding_level;
6212   /* The global level is the namespace level of ::.  */
6213   NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6214   declare_namespace_level ();
6215
6216   VARRAY_TREE_INIT (global_binding_level->static_decls,
6217                     200,
6218                     "Static declarations");
6219
6220   /* Create the `std' namespace.  */
6221   push_namespace (std_identifier);
6222   std_node = current_namespace;
6223   pop_namespace ();
6224
6225   c_common_nodes_and_builtins ();
6226
6227   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6228   java_short_type_node = record_builtin_java_type ("__java_short", 16);
6229   java_int_type_node = record_builtin_java_type ("__java_int", 32);
6230   java_long_type_node = record_builtin_java_type ("__java_long", 64);
6231   java_float_type_node = record_builtin_java_type ("__java_float", -32);
6232   java_double_type_node = record_builtin_java_type ("__java_double", -64);
6233   java_char_type_node = record_builtin_java_type ("__java_char", -16);
6234   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6235
6236   integer_two_node = build_int_2 (2, 0);
6237   TREE_TYPE (integer_two_node) = integer_type_node;
6238   integer_three_node = build_int_2 (3, 0);
6239   TREE_TYPE (integer_three_node) = integer_type_node;
6240
6241   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6242   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6243   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6244   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6245   TYPE_PRECISION (boolean_type_node) = 1;
6246   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6247   boolean_false_node = build_int_2 (0, 0);
6248   TREE_TYPE (boolean_false_node) = boolean_type_node;
6249   boolean_true_node = build_int_2 (1, 0);
6250   TREE_TYPE (boolean_true_node) = boolean_type_node;
6251
6252   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6253
6254 #if 0
6255   record_builtin_type (RID_MAX, NULL, string_type_node);
6256 #endif
6257
6258   delta_type_node = ptrdiff_type_node;
6259   vtable_index_type = ptrdiff_type_node;
6260
6261   vtt_parm_type = build_pointer_type (const_ptr_type_node);
6262   void_ftype = build_function_type (void_type_node, void_list_node);
6263   void_ftype_ptr = build_function_type (void_type_node,
6264                                         tree_cons (NULL_TREE,
6265                                                    ptr_type_node, 
6266                                                    void_list_node));
6267   void_ftype_ptr
6268     = build_exception_variant (void_ftype_ptr, empty_except_spec);
6269
6270   /* C++ extensions */
6271
6272   unknown_type_node = make_node (UNKNOWN_TYPE);
6273   record_unknown_type (unknown_type_node, "unknown type");
6274
6275   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6276   TREE_TYPE (unknown_type_node) = unknown_type_node;
6277
6278   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6279      result.  */
6280   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6281   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6282
6283   {
6284     /* Make sure we get a unique function type, so we can give
6285        its pointer type a name.  (This wins for gdb.) */
6286     tree vfunc_type = make_node (FUNCTION_TYPE);
6287     TREE_TYPE (vfunc_type) = integer_type_node;
6288     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6289     layout_type (vfunc_type);
6290
6291     vtable_entry_type = build_pointer_type (vfunc_type);
6292   }
6293   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6294
6295   vtbl_type_node
6296     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6297   layout_type (vtbl_type_node);
6298   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6299   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6300   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6301   layout_type (vtbl_ptr_type_node);
6302   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6303
6304   push_namespace (get_identifier ("__cxxabiv1"));
6305   abi_node = current_namespace;
6306   pop_namespace ();
6307
6308   global_type_node = make_node (LANG_TYPE);
6309   record_unknown_type (global_type_node, "global type");
6310
6311   /* Now, C++.  */
6312   current_lang_name = lang_name_cplusplus;
6313
6314   {
6315     tree bad_alloc_type_node, newtype, deltype;
6316     tree ptr_ftype_sizetype;
6317
6318     push_namespace (std_identifier);
6319     bad_alloc_type_node 
6320       = xref_tag (class_type, get_identifier ("bad_alloc"), 
6321                   /*attributes=*/NULL_TREE, 1);
6322     pop_namespace ();
6323     ptr_ftype_sizetype 
6324       = build_function_type (ptr_type_node,
6325                              tree_cons (NULL_TREE,
6326                                         size_type_node,
6327                                         void_list_node));
6328     newtype = build_exception_variant
6329       (ptr_ftype_sizetype, add_exception_specifier
6330        (NULL_TREE, bad_alloc_type_node, -1));
6331     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6332     push_cp_library_fn (NEW_EXPR, newtype);
6333     push_cp_library_fn (VEC_NEW_EXPR, newtype);
6334     global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6335     push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6336   }
6337
6338   abort_fndecl
6339     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6340
6341   /* Perform other language dependent initializations.  */
6342   init_class_processing ();
6343   init_search_processing ();
6344   init_rtti_processing ();
6345
6346   if (flag_exceptions)
6347     init_exception_processing ();
6348
6349   if (! supports_one_only ())
6350     flag_weak = 0;
6351
6352   make_fname_decl = cp_make_fname_decl;
6353   start_fname_decls ();
6354
6355   /* Show we use EH for cleanups.  */
6356   using_eh_for_cleanups ();
6357
6358   /* Maintain consistency.  Perhaps we should just complain if they
6359      say -fwritable-strings?  */
6360   if (flag_writable_strings)
6361     flag_const_strings = 0;
6362 }
6363
6364 /* Generate an initializer for a function naming variable from
6365    NAME. NAME may be NULL, in which case we generate a special
6366    ERROR_MARK node which should be replaced later.  */
6367
6368 tree
6369 cp_fname_init (const char* name)
6370 {
6371   tree domain = NULL_TREE;
6372   tree type;
6373   tree init = NULL_TREE;
6374   size_t length = 0;
6375
6376   if (name)
6377     {
6378       length = strlen (name);
6379       domain = build_index_type (size_int (length));
6380       init = build_string (length + 1, name);
6381     }
6382   
6383   type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6384   type = build_cplus_array_type (type, domain);
6385
6386   if (init)
6387     TREE_TYPE (init) = type;
6388   else
6389     /* We don't know the value until instantiation time. Make
6390        something which will be digested now, but replaced later.  */
6391     init = build (ERROR_MARK, type);
6392   
6393   return init;
6394 }
6395
6396 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6397    decl, NAME is the initialization string and TYPE_DEP indicates whether
6398    NAME depended on the type of the function. We make use of that to detect
6399    __PRETTY_FUNCTION__ inside a template fn. This is being done
6400    lazily at the point of first use, so we musn't push the decl now.  */
6401
6402 static tree
6403 cp_make_fname_decl (tree id, int type_dep)
6404 {
6405   const char *const name = (type_dep && processing_template_decl
6406                       ? NULL : fname_as_string (type_dep));
6407   tree init = cp_fname_init (name);
6408   tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6409
6410   /* As we're using pushdecl_with_scope, we must set the context.  */
6411   DECL_CONTEXT (decl) = current_function_decl;
6412   DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6413       
6414   TREE_STATIC (decl) = 1;
6415   TREE_READONLY (decl) = 1;
6416   DECL_ARTIFICIAL (decl) = 1;
6417   DECL_INITIAL (decl) = init;
6418   
6419   TREE_USED (decl) = 1;
6420
6421   if (current_function_decl)
6422     {
6423       struct cp_binding_level *b = current_binding_level;
6424       while (b->level_chain->parm_flag == 0)
6425         b = b->level_chain;
6426       pushdecl_with_scope (decl, b);
6427     }   
6428
6429   cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6430       
6431   return decl;
6432 }
6433
6434 /* Make a definition for a builtin function named NAME in the current
6435    namespace, whose data type is TYPE and whose context is CONTEXT.
6436    TYPE should be a function type with argument types.
6437
6438    CLASS and CODE tell later passes how to compile calls to this function.
6439    See tree.h for possible values.
6440
6441    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6442    the name to be called if we can't opencode the function.
6443    If ATTRS is nonzero, use that for the function's attribute
6444    list.  */
6445
6446 static tree
6447 builtin_function_1 (const char* name,
6448                     tree type,
6449                     tree context,
6450                     int code,
6451                     enum built_in_class class,
6452                     const char* libname,
6453                     tree attrs)
6454 {
6455   tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6456   DECL_BUILT_IN_CLASS (decl) = class;
6457   DECL_FUNCTION_CODE (decl) = code;
6458   DECL_CONTEXT (decl) = context;
6459
6460   pushdecl (decl);
6461
6462   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6463      we cannot change DECL_ASSEMBLER_NAME until we have installed this
6464      function in the namespace.  */
6465   if (libname)
6466     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6467   make_decl_rtl (decl, NULL);
6468
6469   /* Warn if a function in the namespace for users
6470      is used without an occasion to consider it declared.  */
6471   if (name[0] != '_' || name[1] != '_')
6472     DECL_ANTICIPATED (decl) = 1;
6473
6474   /* Possibly apply some default attributes to this built-in function.  */
6475   if (attrs)
6476     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6477   else
6478     decl_attributes (&decl, NULL_TREE, 0);
6479
6480   return decl;
6481 }
6482
6483 /* Entry point for the benefit of c_common_nodes_and_builtins.
6484
6485    Make a defintion for a builtin function named NAME and whose data type
6486    is TYPE.  TYPE should be a function type with argument types.  This
6487    function places the anticipated declaration in the global namespace
6488    and additionally in the std namespace if appropriate.
6489
6490    CLASS and CODE tell later passes how to compile calls to this function.
6491    See tree.h for possible values.
6492
6493    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6494    the name to be called if we can't opencode the function.
6495
6496    If ATTRS is nonzero, use that for the function's attribute
6497    list.  */
6498
6499 tree
6500 builtin_function (const char* name,
6501                   tree type,
6502                   int code,
6503                   enum built_in_class class,
6504                   const char* libname,
6505                   tree attrs)
6506 {
6507   /* All builtins that don't begin with an '_' should additionally
6508      go in the 'std' namespace.  */
6509   if (name[0] != '_')
6510     {
6511       push_namespace (std_identifier);
6512       builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6513       pop_namespace ();
6514     }
6515
6516   return builtin_function_1 (name, type, NULL_TREE, code,
6517                              class, libname, attrs);
6518 }
6519
6520 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6521    function.  Not called directly.  */
6522
6523 static tree
6524 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6525 {
6526   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6527   DECL_EXTERNAL (fn) = 1;
6528   TREE_PUBLIC (fn) = 1;
6529   DECL_ARTIFICIAL (fn) = 1;
6530   TREE_NOTHROW (fn) = 1;
6531   SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6532   SET_DECL_LANGUAGE (fn, lang_c);
6533   return fn;
6534 }
6535
6536 /* Returns the _DECL for a library function with C linkage.
6537    We assume that such functions never throw; if this is incorrect,
6538    callers should unset TREE_NOTHROW.  */
6539
6540 tree
6541 build_library_fn (tree name, tree type)
6542 {
6543   return build_library_fn_1 (name, ERROR_MARK, type);
6544 }
6545
6546 /* Returns the _DECL for a library function with C++ linkage.  */
6547
6548 static tree
6549 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6550 {
6551   tree fn = build_library_fn_1 (name, operator_code, type);
6552   TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6553   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6554   SET_DECL_LANGUAGE (fn, lang_cplusplus);
6555   set_mangled_name_for_decl (fn);
6556   return fn;
6557 }
6558
6559 /* Like build_library_fn, but takes a C string instead of an
6560    IDENTIFIER_NODE.  */
6561
6562 tree
6563 build_library_fn_ptr (const char* name, tree type)
6564 {
6565   return build_library_fn (get_identifier (name), type);
6566 }
6567
6568 /* Like build_cp_library_fn, but takes a C string instead of an
6569    IDENTIFIER_NODE.  */
6570
6571 tree
6572 build_cp_library_fn_ptr (const char* name, tree type)
6573 {
6574   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6575 }
6576
6577 /* Like build_library_fn, but also pushes the function so that we will
6578    be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
6579
6580 tree
6581 push_library_fn (tree name, tree type)
6582 {
6583   tree fn = build_library_fn (name, type);
6584   pushdecl_top_level (fn);
6585   return fn;
6586 }
6587
6588 /* Like build_cp_library_fn, but also pushes the function so that it
6589    will be found by normal lookup.  */
6590
6591 static tree
6592 push_cp_library_fn (enum tree_code operator_code, tree type)
6593 {
6594   tree fn = build_cp_library_fn (ansi_opname (operator_code),
6595                                  operator_code,
6596                                  type);
6597   pushdecl (fn);
6598   return fn;
6599 }
6600
6601 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6602    a FUNCTION_TYPE.  */
6603
6604 tree
6605 push_void_library_fn (tree name, tree parmtypes)
6606 {
6607   tree type = build_function_type (void_type_node, parmtypes);
6608   return push_library_fn (name, type);
6609 }
6610
6611 /* Like push_library_fn, but also note that this function throws
6612    and does not return.  Used for __throw_foo and the like.  */
6613
6614 tree
6615 push_throw_library_fn (tree name, tree type)
6616 {
6617   tree fn = push_library_fn (name, type);
6618   TREE_THIS_VOLATILE (fn) = 1;
6619   TREE_NOTHROW (fn) = 0;
6620   return fn;
6621 }
6622
6623 /* Apply default attributes to a function, if a system function with default
6624    attributes.  */
6625
6626 void
6627 cxx_insert_default_attributes (tree decl)
6628 {
6629   if (!DECL_EXTERN_C_FUNCTION_P (decl))
6630     return;
6631   if (!TREE_PUBLIC (decl))
6632     return;
6633   c_common_insert_default_attributes (decl);
6634 }
6635 \f
6636 /* When we call finish_struct for an anonymous union, we create
6637    default copy constructors and such.  But, an anonymous union
6638    shouldn't have such things; this function undoes the damage to the
6639    anonymous union type T.
6640
6641    (The reason that we create the synthesized methods is that we don't
6642    distinguish `union { int i; }' from `typedef union { int i; } U'.
6643    The first is an anonymous union; the second is just an ordinary
6644    union type.)  */
6645
6646 void
6647 fixup_anonymous_aggr (tree t)
6648 {
6649   tree *q;
6650
6651   /* Wipe out memory of synthesized methods */
6652   TYPE_HAS_CONSTRUCTOR (t) = 0;
6653   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6654   TYPE_HAS_INIT_REF (t) = 0;
6655   TYPE_HAS_CONST_INIT_REF (t) = 0;
6656   TYPE_HAS_ASSIGN_REF (t) = 0;
6657   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6658
6659   /* Splice the implicitly generated functions out of the TYPE_METHODS
6660      list.  */
6661   q = &TYPE_METHODS (t);
6662   while (*q)
6663     {
6664       if (DECL_ARTIFICIAL (*q))
6665         *q = TREE_CHAIN (*q);
6666       else
6667         q = &TREE_CHAIN (*q);
6668     }
6669
6670   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
6671   if (TYPE_METHODS (t))
6672     cp_error_at ("an anonymous union cannot have function members", t);
6673
6674   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6675      assignment operators (because they cannot have these methods themselves).
6676      For anonymous unions this is already checked because they are not allowed
6677      in any union, otherwise we have to check it.  */
6678   if (TREE_CODE (t) != UNION_TYPE)
6679     {
6680       tree field, type;
6681
6682       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6683         if (TREE_CODE (field) == FIELD_DECL)
6684           {
6685             type = TREE_TYPE (field);
6686             if (CLASS_TYPE_P (type))
6687               {
6688                 if (TYPE_NEEDS_CONSTRUCTING (type))
6689                   cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6690                                field);
6691                 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6692                   cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6693                                field);
6694                 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6695                   cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6696                                field);
6697               }
6698           }
6699     }
6700 }
6701
6702 /* Make sure that a declaration with no declarator is well-formed, i.e.
6703    just declares a tagged type or anonymous union.
6704
6705    Returns the type declared; or NULL_TREE if none.  */
6706
6707 tree
6708 check_tag_decl (tree declspecs)
6709 {
6710   int found_type = 0;
6711   int saw_friend = 0;
6712   int saw_typedef = 0;
6713   tree ob_modifier = NULL_TREE;
6714   register tree link;
6715   /* If a class, struct, or enum type is declared by the DECLSPECS
6716      (i.e, if a class-specifier, enum-specifier, or non-typename
6717      elaborated-type-specifier appears in the DECLSPECS),
6718      DECLARED_TYPE is set to the corresponding type.  */
6719   tree declared_type = NULL_TREE;
6720   bool error_p = false;
6721
6722   for (link = declspecs; link; link = TREE_CHAIN (link))
6723     {
6724       tree value = TREE_VALUE (link);
6725
6726       if (TYPE_P (value)
6727           || TREE_CODE (value) == TYPE_DECL
6728           || (TREE_CODE (value) == IDENTIFIER_NODE
6729               && IDENTIFIER_GLOBAL_VALUE (value)
6730               && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6731         {
6732           ++found_type;
6733
6734           if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6735             {
6736               if (! in_system_header)
6737                 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6738               return NULL_TREE;
6739             }
6740
6741           if (TYPE_P (value)
6742               && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6743                   || TREE_CODE (value) == ENUMERAL_TYPE))
6744             {
6745               my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6746               declared_type = value;
6747             }
6748         }
6749       else if (value == ridpointers[(int) RID_TYPEDEF])
6750         saw_typedef = 1;
6751       else if (value == ridpointers[(int) RID_FRIEND])
6752         {
6753           if (current_class_type == NULL_TREE
6754               || current_scope () != current_class_type)
6755             ob_modifier = value;
6756           else
6757             saw_friend = 1;
6758         }
6759       else if (value == ridpointers[(int) RID_STATIC]
6760                || value == ridpointers[(int) RID_EXTERN]
6761                || value == ridpointers[(int) RID_AUTO]
6762                || value == ridpointers[(int) RID_REGISTER]
6763                || value == ridpointers[(int) RID_INLINE]
6764                || value == ridpointers[(int) RID_VIRTUAL]
6765                || value == ridpointers[(int) RID_CONST]
6766                || value == ridpointers[(int) RID_VOLATILE]
6767                || value == ridpointers[(int) RID_EXPLICIT]
6768                || value == ridpointers[(int) RID_THREAD])
6769         ob_modifier = value;
6770       else if (value == error_mark_node)
6771         error_p = true;
6772     }
6773
6774   if (found_type > 1)
6775     error ("multiple types in one declaration");
6776
6777   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6778     pedwarn ("declaration does not declare anything");
6779   /* Check for an anonymous union.  */
6780   else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6781            && TYPE_ANONYMOUS_P (declared_type))
6782     {
6783       /* 7/3 In a simple-declaration, the optional init-declarator-list
6784          can be omitted only when declaring a class (clause 9) or
6785          enumeration (7.2), that is, when the decl-specifier-seq contains
6786          either a class-specifier, an elaborated-type-specifier with
6787          a class-key (9.1), or an enum-specifier.  In these cases and
6788          whenever a class-specifier or enum-specifier is present in the
6789          decl-specifier-seq, the identifiers in these specifiers are among
6790          the names being declared by the declaration (as class-name,
6791          enum-names, or enumerators, depending on the syntax).  In such
6792          cases, and except for the declaration of an unnamed bit-field (9.6),
6793          the decl-specifier-seq shall introduce one or more names into the
6794          program, or shall redeclare a name introduced by a previous
6795          declaration.  [Example:
6796              enum { };            // ill-formed
6797              typedef class { };   // ill-formed
6798          --end example]  */
6799       if (saw_typedef)
6800         {
6801           error ("missing type-name in typedef-declaration");
6802           return NULL_TREE;
6803         }
6804       /* Anonymous unions are objects, so they can have specifiers.  */;
6805       SET_ANON_AGGR_TYPE_P (declared_type);
6806
6807       if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 
6808           && !in_system_header)
6809         pedwarn ("ISO C++ prohibits anonymous structs");
6810     }
6811
6812   else if (ob_modifier)
6813     {
6814       if (ob_modifier == ridpointers[(int) RID_INLINE]
6815           || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6816         error ("`%D' can only be specified for functions", ob_modifier);
6817       else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6818         error ("`%D' can only be specified inside a class", ob_modifier);
6819       else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6820         error ("`%D' can only be specified for constructors",
6821                   ob_modifier);
6822       else
6823         error ("`%D' can only be specified for objects and functions",
6824                   ob_modifier);
6825     }
6826
6827   return declared_type;
6828 }
6829
6830 /* Called when a declaration is seen that contains no names to declare.
6831    If its type is a reference to a structure, union or enum inherited
6832    from a containing scope, shadow that tag name for the current scope
6833    with a forward reference.
6834    If its type defines a new named structure or union
6835    or defines an enum, it is valid but we need not do anything here.
6836    Otherwise, it is an error.
6837
6838    C++: may have to grok the declspecs to learn about static,
6839    complain for anonymous unions.  
6840
6841    Returns the TYPE declared -- or NULL_TREE if none.  */
6842
6843 tree
6844 shadow_tag (tree declspecs)
6845 {
6846   tree t = check_tag_decl (declspecs);
6847
6848   if (!t)
6849     return NULL_TREE;
6850
6851   maybe_process_partial_specialization (t);
6852
6853   /* This is where the variables in an anonymous union are
6854      declared.  An anonymous union declaration looks like:
6855      union { ... } ;
6856      because there is no declarator after the union, the parser
6857      sends that declaration here.  */
6858   if (ANON_AGGR_TYPE_P (t))
6859     {
6860       fixup_anonymous_aggr (t);
6861
6862       if (TYPE_FIELDS (t))
6863         {
6864           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6865                                       NULL);
6866           finish_anon_union (decl);
6867         }
6868     }
6869
6870   return t;
6871 }
6872 \f
6873 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
6874
6875 tree
6876 groktypename (tree typename)
6877 {
6878   tree specs, attrs;
6879   tree type;
6880   if (TREE_CODE (typename) != TREE_LIST)
6881     return typename;
6882   split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6883   type = grokdeclarator (TREE_VALUE (typename), specs,
6884                          TYPENAME, 0, &attrs);
6885   if (attrs)
6886     cplus_decl_attributes (&type, attrs, 0);
6887   return type;
6888 }
6889
6890 /* Decode a declarator in an ordinary declaration or data definition.
6891    This is called as soon as the type information and variable name
6892    have been parsed, before parsing the initializer if any.
6893    Here we create the ..._DECL node, fill in its type,
6894    and put it on the list of decls for the current context.
6895    The ..._DECL node is returned as the value.
6896
6897    Exception: for arrays where the length is not specified,
6898    the type is left null, to be filled in by `cp_finish_decl'.
6899
6900    Function definitions do not come here; they go to start_function
6901    instead.  However, external and forward declarations of functions
6902    do go through here.  Structure field declarations are done by
6903    grokfield and not through here.  */
6904
6905 tree
6906 start_decl (tree declarator, 
6907             tree declspecs, 
6908             int initialized, 
6909             tree attributes, 
6910             tree prefix_attributes)
6911 {
6912   tree decl;
6913   register tree type, tem;
6914   tree context;
6915
6916   /* This should only be done once on the top most decl.  */
6917   if (have_extern_spec)
6918     {
6919       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6920                              declspecs);
6921       have_extern_spec = false;
6922     }
6923
6924   /* An object declared as __attribute__((deprecated)) suppresses
6925      warnings of uses of other deprecated items.  */
6926   if (lookup_attribute ("deprecated", attributes))
6927     deprecated_state = DEPRECATED_SUPPRESS;
6928
6929   attributes = chainon (attributes, prefix_attributes);
6930
6931   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6932                          &attributes);
6933
6934   deprecated_state = DEPRECATED_NORMAL;
6935
6936   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6937     return NULL_TREE;
6938
6939   type = TREE_TYPE (decl);
6940
6941   if (type == error_mark_node)
6942     return NULL_TREE;
6943
6944   context = DECL_CONTEXT (decl);
6945
6946   if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6947       && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6948     {
6949       /* When parsing the initializer, lookup should use the object's
6950          namespace.  */
6951       push_decl_namespace (context);
6952     }
6953
6954   /* We are only interested in class contexts, later.  */
6955   if (context && TREE_CODE (context) == NAMESPACE_DECL)
6956     context = NULL_TREE;
6957
6958   if (initialized)
6959     /* Is it valid for this decl to have an initializer at all?
6960        If not, set INITIALIZED to zero, which will indirectly
6961        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
6962     switch (TREE_CODE (decl))
6963       {
6964       case TYPE_DECL:
6965         error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6966         initialized = 0;
6967         break;
6968
6969       case FUNCTION_DECL:
6970         error ("function `%#D' is initialized like a variable", decl);
6971         initialized = 0;
6972         break;
6973
6974       default:
6975         break;
6976       }
6977
6978   if (initialized)
6979     {
6980       if (! toplevel_bindings_p ()
6981           && DECL_EXTERNAL (decl))
6982         warning ("declaration of `%#D' has `extern' and is initialized",
6983                     decl);
6984       DECL_EXTERNAL (decl) = 0;
6985       if (toplevel_bindings_p ())
6986         TREE_STATIC (decl) = 1;
6987
6988       /* Tell `pushdecl' this is an initialized decl
6989          even though we don't yet have the initializer expression.
6990          Also tell `cp_finish_decl' it may store the real initializer.  */
6991       DECL_INITIAL (decl) = error_mark_node;
6992     }
6993
6994   /* Set attributes here so if duplicate decl, will have proper attributes.  */
6995   cplus_decl_attributes (&decl, attributes, 0);
6996
6997   /* If #pragma weak was used, mark the decl weak now.  */
6998   if (current_binding_level == global_binding_level)
6999     maybe_apply_pragma_weak (decl);
7000
7001   if (TREE_CODE (decl) == FUNCTION_DECL
7002       && DECL_DECLARED_INLINE_P (decl)
7003       && DECL_UNINLINABLE (decl)
7004       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7005     warning ("%Hinline function '%D' given attribute noinline",
7006              &DECL_SOURCE_LOCATION (decl), decl);
7007
7008   if (context && COMPLETE_TYPE_P (complete_type (context)))
7009     {
7010       push_nested_class (context);
7011
7012       if (TREE_CODE (decl) == VAR_DECL)
7013         {
7014           tree field = lookup_field (context, DECL_NAME (decl), 0, false);
7015           if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7016             error ("`%#D' is not a static member of `%#T'", decl, context);
7017           else
7018             {
7019               if (DECL_CONTEXT (field) != context)
7020                 {
7021                   if (!same_type_p (DECL_CONTEXT (field), context))
7022                     pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7023                              DECL_CONTEXT (field), DECL_NAME (decl),
7024                              context, DECL_NAME (decl));
7025                   DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7026                 }
7027               /* Static data member are tricky; an in-class initialization
7028                  still doesn't provide a definition, so the in-class
7029                  declaration will have DECL_EXTERNAL set, but will have an
7030                  initialization.  Thus, duplicate_decls won't warn
7031                  about this situation, and so we check here.  */
7032               if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7033                 error ("duplicate initialization of %D", decl);
7034               if (duplicate_decls (decl, field))
7035                 decl = field;
7036             }
7037         }
7038       else
7039         {
7040           tree field = check_classfn (context, decl);
7041           if (field && duplicate_decls (decl, field))
7042             decl = field;
7043         }
7044
7045       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7046       DECL_IN_AGGR_P (decl) = 0;
7047       if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7048           || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7049         {
7050           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7051           /* [temp.expl.spec] An explicit specialization of a static data
7052              member of a template is a definition if the declaration
7053              includes an initializer; otherwise, it is a declaration.
7054
7055              We check for processing_specialization so this only applies
7056              to the new specialization syntax.  */
7057           if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7058             DECL_EXTERNAL (decl) = 1;
7059         }
7060
7061       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7062         pedwarn ("declaration of `%#D' outside of class is not definition",
7063                     decl);
7064     }
7065
7066   /* Enter this declaration into the symbol table.  */
7067   tem = maybe_push_decl (decl);
7068
7069   if (processing_template_decl)
7070     tem = push_template_decl (tem);
7071
7072 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7073   /* Tell the back-end to use or not use .common as appropriate.  If we say
7074      -fconserve-space, we want this to save .data space, at the expense of
7075      wrong semantics.  If we say -fno-conserve-space, we want this to
7076      produce errors about redefs; to do this we force variables into the
7077      data segment.  */
7078   DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7079                         || !DECL_THREAD_LOCAL (tem))
7080                        && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7081 #endif
7082
7083   if (! processing_template_decl)
7084     start_decl_1 (tem);
7085
7086   return tem;
7087 }
7088
7089 void
7090 start_decl_1 (tree decl)
7091 {
7092   tree type = TREE_TYPE (decl);
7093   int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7094
7095   if (type == error_mark_node)
7096     return;
7097
7098   maybe_push_cleanup_level (type);
7099
7100   if (initialized)
7101     /* Is it valid for this decl to have an initializer at all?
7102        If not, set INITIALIZED to zero, which will indirectly
7103        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7104     {
7105       /* Don't allow initializations for incomplete types except for
7106          arrays which might be completed by the initialization.  */
7107       if (COMPLETE_TYPE_P (complete_type (type)))
7108         ;                       /* A complete type is ok.  */
7109       else if (TREE_CODE (type) != ARRAY_TYPE)
7110         {
7111           error ("variable `%#D' has initializer but incomplete type",
7112                     decl);
7113           initialized = 0;
7114           type = TREE_TYPE (decl) = error_mark_node;
7115         }
7116       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7117         {
7118           if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7119             error ("elements of array `%#D' have incomplete type", decl);
7120           /* else we already gave an error in start_decl.  */
7121           initialized = 0;
7122         }
7123     }
7124
7125   if (!initialized
7126       && TREE_CODE (decl) != TYPE_DECL
7127       && TREE_CODE (decl) != TEMPLATE_DECL
7128       && type != error_mark_node
7129       && IS_AGGR_TYPE (type)
7130       && ! DECL_EXTERNAL (decl))
7131     {
7132       if ((! processing_template_decl || ! uses_template_parms (type))
7133           && !COMPLETE_TYPE_P (complete_type (type)))
7134         {
7135           error ("aggregate `%#D' has incomplete type and cannot be defined",
7136                  decl);
7137           /* Change the type so that assemble_variable will give
7138              DECL an rtl we can live with: (mem (const_int 0)).  */
7139           type = TREE_TYPE (decl) = error_mark_node;
7140         }
7141       else
7142         {
7143           /* If any base type in the hierarchy of TYPE needs a constructor,
7144              then we set initialized to 1.  This way any nodes which are
7145              created for the purposes of initializing this aggregate
7146              will live as long as it does.  This is necessary for global
7147              aggregates which do not have their initializers processed until
7148              the end of the file.  */
7149           initialized = TYPE_NEEDS_CONSTRUCTING (type);
7150         }
7151     }
7152
7153   if (! initialized)
7154     DECL_INITIAL (decl) = NULL_TREE;
7155 }
7156
7157 /* Handle initialization of references.
7158    These three arguments are from `cp_finish_decl', and have the
7159    same meaning here that they do there.
7160
7161    Quotes on semantics can be found in ARM 8.4.3.  */
7162
7163 static tree
7164 grok_reference_init (tree decl, tree type, tree init)
7165 {
7166   tree tmp;
7167
7168   if (init == NULL_TREE)
7169     {
7170       if ((DECL_LANG_SPECIFIC (decl) == 0
7171            || DECL_IN_AGGR_P (decl) == 0)
7172           && ! DECL_THIS_EXTERN (decl))
7173         error ("`%D' declared as reference but not initialized", decl);
7174       return NULL_TREE;
7175     }
7176
7177   if (TREE_CODE (init) == CONSTRUCTOR)
7178     {
7179       error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7180       return NULL_TREE;
7181     }
7182
7183   if (TREE_CODE (init) == TREE_LIST)
7184     init = build_compound_expr (init);
7185
7186   if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7187     init = convert_from_reference (init);
7188
7189   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7190       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7191     {
7192       /* Note: default conversion is only called in very special cases.  */
7193       init = default_conversion (init);
7194     }
7195
7196   /* Convert INIT to the reference type TYPE.  This may involve the
7197      creation of a temporary, whose lifetime must be the same as that
7198      of the reference.  If so, a DECL_STMT for the temporary will be
7199      added just after the DECL_STMT for DECL.  That's why we don't set
7200      DECL_INITIAL for local references (instead assigning to them
7201      explicitly); we need to allow the temporary to be initialized
7202      first.  */
7203   tmp = initialize_reference (type, init, decl);
7204
7205   if (tmp == error_mark_node)
7206     return NULL_TREE;
7207   else if (tmp == NULL_TREE)
7208     {
7209       error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7210       return NULL_TREE;
7211     }
7212
7213   if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7214     return tmp;
7215
7216   DECL_INITIAL (decl) = tmp;
7217
7218   return NULL_TREE;
7219 }
7220
7221 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7222    array until we finish parsing the initializer.  If that's the
7223    situation we're in, update DECL accordingly.  */
7224
7225 static void
7226 maybe_deduce_size_from_array_init (tree decl, tree init)
7227 {
7228   tree type = TREE_TYPE (decl);
7229
7230   if (TREE_CODE (type) == ARRAY_TYPE
7231       && TYPE_DOMAIN (type) == NULL_TREE
7232       && TREE_CODE (decl) != TYPE_DECL)
7233     {
7234       /* do_default is really a C-ism to deal with tentative definitions.
7235          But let's leave it here to ease the eventual merge.  */
7236       int do_default = !DECL_EXTERNAL (decl);
7237       tree initializer = init ? init : DECL_INITIAL (decl);
7238       int failure = complete_array_type (type, initializer, do_default);
7239
7240       if (failure == 1)
7241         error ("initializer fails to determine size of `%D'", decl);
7242
7243       if (failure == 2)
7244         {
7245           if (do_default)
7246             error ("array size missing in `%D'", decl);
7247           /* If a `static' var's size isn't known, make it extern as
7248              well as static, so it does not get allocated.  If it's not
7249              `static', then don't mark it extern; finish_incomplete_decl
7250              will give it a default size and it will get allocated.  */
7251           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7252             DECL_EXTERNAL (decl) = 1;
7253         }
7254
7255       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7256           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7257                               integer_zero_node))
7258         error ("zero-size array `%D'", decl);
7259
7260       layout_decl (decl, 0);
7261     }
7262 }
7263
7264 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7265    any appropriate error messages regarding the layout.  */
7266
7267 static void
7268 layout_var_decl (tree decl)
7269 {
7270   tree type = TREE_TYPE (decl);
7271 #if 0
7272   tree ttype = target_type (type);
7273 #endif
7274
7275   /* If we haven't already layed out this declaration, do so now.
7276      Note that we must not call complete type for an external object
7277      because it's type might involve templates that we are not
7278      supposed to isntantiate yet.  (And it's perfectly valid to say
7279      `extern X x' for some incomplete type `X'.)  */
7280   if (!DECL_EXTERNAL (decl))
7281     complete_type (type);
7282   if (!DECL_SIZE (decl) 
7283       && TREE_TYPE (decl) != error_mark_node
7284       && (COMPLETE_TYPE_P (type)
7285           || (TREE_CODE (type) == ARRAY_TYPE 
7286               && !TYPE_DOMAIN (type)
7287               && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7288     layout_decl (decl, 0);
7289
7290   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7291     {
7292       /* An automatic variable with an incomplete type: that is an error.
7293          Don't talk about array types here, since we took care of that
7294          message in grokdeclarator.  */
7295       error ("storage size of `%D' isn't known", decl);
7296       TREE_TYPE (decl) = error_mark_node;
7297     }
7298 #if 0
7299   /* Keep this code around in case we later want to control debug info
7300      based on whether a type is "used".  (jason 1999-11-11) */
7301
7302   else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7303     /* Let debugger know it should output info for this type.  */
7304     note_debug_info_needed (ttype);
7305
7306   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7307     note_debug_info_needed (DECL_CONTEXT (decl));
7308 #endif
7309
7310   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7311       && DECL_SIZE (decl) != NULL_TREE
7312       && ! TREE_CONSTANT (DECL_SIZE (decl)))
7313     {
7314       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7315         constant_expression_warning (DECL_SIZE (decl));
7316       else
7317         error ("storage size of `%D' isn't constant", decl);
7318     }
7319
7320   if (TREE_STATIC (decl)
7321       && !DECL_ARTIFICIAL (decl)
7322       && current_function_decl
7323       && DECL_CONTEXT (decl) == current_function_decl)
7324     push_local_name (decl);
7325 }
7326
7327 /* If a local static variable is declared in an inline function, or if
7328    we have a weak definition, we must endeavor to create only one
7329    instance of the variable at link-time.  */
7330
7331 static void
7332 maybe_commonize_var (tree decl)
7333 {
7334   /* Static data in a function with comdat linkage also has comdat
7335      linkage.  */
7336   if (TREE_STATIC (decl)
7337       /* Don't mess with __FUNCTION__.  */
7338       && ! DECL_ARTIFICIAL (decl)
7339       && DECL_FUNCTION_SCOPE_P (decl)
7340       /* Unfortunately, import_export_decl has not always been called
7341          before the function is processed, so we cannot simply check
7342          DECL_COMDAT.  */ 
7343       && (DECL_COMDAT (DECL_CONTEXT (decl))
7344           || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7345                || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7346               && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7347     {
7348       if (flag_weak)
7349         {
7350           /* With weak symbols, we simply make the variable COMDAT;
7351              that will cause copies in multiple translations units to
7352              be merged.  */
7353           comdat_linkage (decl);
7354         }
7355       else
7356         {
7357           if (DECL_INITIAL (decl) == NULL_TREE
7358               || DECL_INITIAL (decl) == error_mark_node)
7359             {
7360               /* Without weak symbols, we can use COMMON to merge
7361                  uninitialized variables.  */
7362               TREE_PUBLIC (decl) = 1;
7363               DECL_COMMON (decl) = 1;
7364             }
7365           else
7366             {
7367               /* While for initialized variables, we must use internal
7368                  linkage -- which means that multiple copies will not
7369                  be merged.  */
7370               TREE_PUBLIC (decl) = 0;
7371               DECL_COMMON (decl) = 0;
7372               cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7373               cp_warning_at ("  you can work around this by removing the initializer", decl);
7374             }
7375         }
7376     }
7377   else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7378     /* Set it up again; we might have set DECL_INITIAL since the last
7379        time.  */
7380     comdat_linkage (decl);
7381 }
7382
7383 /* Issue an error message if DECL is an uninitialized const variable.  */
7384
7385 static void
7386 check_for_uninitialized_const_var (tree decl)
7387 {
7388   tree type = TREE_TYPE (decl);
7389
7390   /* ``Unless explicitly declared extern, a const object does not have
7391      external linkage and must be initialized. ($8.4; $12.1)'' ARM
7392      7.1.6 */
7393   if (TREE_CODE (decl) == VAR_DECL
7394       && TREE_CODE (type) != REFERENCE_TYPE
7395       && CP_TYPE_CONST_P (type)
7396       && !TYPE_NEEDS_CONSTRUCTING (type)
7397       && !DECL_INITIAL (decl))
7398     error ("uninitialized const `%D'", decl);
7399 }
7400
7401 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
7402    returned is the next FIELD_DECL (possibly FIELD itself) that can be
7403    initialized.  If there are no more such fields, the return value
7404    will be NULL.  */
7405
7406 static tree
7407 next_initializable_field (tree field)
7408 {
7409   while (field
7410          && (TREE_CODE (field) != FIELD_DECL
7411              || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7412              || DECL_ARTIFICIAL (field)))
7413     field = TREE_CHAIN (field);
7414
7415   return field;
7416 }
7417
7418 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7419    brace-enclosed aggregate initializer.
7420
7421    *INITP is one of a list of initializers describing a brace-enclosed
7422    initializer for an entity of the indicated aggregate TYPE.  It may
7423    not presently match the shape of the TYPE; for example:
7424    
7425      struct S { int a; int b; };
7426      struct S a[] = { 1, 2, 3, 4 };
7427
7428    Here *INITP will point to TREE_LIST of four elements, rather than a
7429    list of two elements, each itself a list of two elements.  This
7430    routine transforms INIT from the former form into the latter.  The
7431    revised initializer is returned.  */
7432
7433 static tree
7434 reshape_init (tree type, tree *initp)
7435 {
7436   tree inits;
7437   tree old_init;
7438   tree old_init_value;
7439   tree new_init;
7440   bool brace_enclosed_p;
7441
7442   old_init = *initp;
7443   old_init_value = (TREE_CODE (*initp) == TREE_LIST
7444                     ? TREE_VALUE (*initp) : old_init);
7445
7446   /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
7447   if (!old_init_value)
7448     {
7449       my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7450       TREE_VALUE (old_init) = error_mark_node;
7451       return old_init;
7452     }
7453
7454   /* If the initializer is brace-enclosed, pull initializers from the
7455      enclosed elements.  Advance past the brace-enclosed initializer
7456      now.  */
7457   if (TREE_CODE (old_init_value) == CONSTRUCTOR 
7458       && TREE_HAS_CONSTRUCTOR (old_init_value))
7459     {
7460       *initp = TREE_CHAIN (old_init);
7461       TREE_CHAIN (old_init) = NULL_TREE;
7462       inits = CONSTRUCTOR_ELTS (old_init_value);
7463       initp = &inits;
7464       brace_enclosed_p = true;
7465     }
7466   else
7467     {
7468       inits = NULL_TREE;
7469       brace_enclosed_p = false;
7470     }
7471
7472   /* A non-aggregate type is always initialized with a single
7473      initializer.  */
7474   if (!CP_AGGREGATE_TYPE_P (type))
7475       {
7476         *initp = TREE_CHAIN (old_init);
7477         TREE_CHAIN (old_init) = NULL_TREE;
7478         /* It is invalid to initialize a non-aggregate type with a
7479            brace-enclosed initializer.  */
7480         if (brace_enclosed_p)
7481           {
7482             error ("brace-enclosed initializer used to initialize `%T'",
7483                    type);
7484             if (TREE_CODE (old_init) == TREE_LIST)
7485               TREE_VALUE (old_init) = error_mark_node;
7486             else
7487               old_init = error_mark_node;
7488           }
7489         
7490         return old_init;
7491       }
7492
7493   /* [dcl.init.aggr]
7494
7495      All implicit type conversions (clause _conv_) are considered when
7496      initializing the aggregate member with an initializer from an
7497      initializer-list.  If the initializer can initialize a member,
7498      the member is initialized.  Otherwise, if the member is itself a
7499      non-empty subaggregate, brace elision is assumed and the
7500      initializer is considered for the initialization of the first
7501      member of the subaggregate.  */
7502   if (CLASS_TYPE_P (type) 
7503       && !brace_enclosed_p
7504       && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7505     {
7506       *initp = TREE_CHAIN (old_init);
7507       TREE_CHAIN (old_init) = NULL_TREE;
7508       return old_init;
7509     }
7510
7511   if (TREE_CODE (old_init_value) == STRING_CST
7512       && TREE_CODE (type) == ARRAY_TYPE
7513       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7514     {
7515       /* [dcl.init.string]
7516
7517          A char array (whether plain char, signed char, or unsigned char)
7518          can be initialized by a string-literal (optionally enclosed in
7519          braces); a wchar_t array can be initialized by a wide
7520          string-literal (optionally enclosed in braces).  */
7521       new_init = old_init;
7522       /* Move past the initializer.  */
7523       *initp = TREE_CHAIN (old_init);
7524       TREE_CHAIN (old_init) = NULL_TREE;
7525     }
7526   else
7527     {
7528       /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */  
7529       new_init = build_constructor (type, NULL_TREE);
7530       TREE_HAS_CONSTRUCTOR (new_init) = 1;
7531
7532       if (CLASS_TYPE_P (type))
7533         {
7534           tree field;
7535
7536           field = next_initializable_field (TYPE_FIELDS (type));
7537
7538           if (!field)
7539             {
7540               /* [dcl.init.aggr]
7541               
7542                  An initializer for an aggregate member that is an
7543                  empty class shall have the form of an empty
7544                  initializer-list {}.  */
7545               if (!brace_enclosed_p)
7546                 error ("initializer for `%T' must be brace-enclosed",
7547                        type);
7548             }
7549           else
7550             {
7551               /* Loop through the initializable fields, gathering
7552                  initializers.  */
7553               while (*initp)
7554                 {
7555                   tree field_init;
7556
7557                   /* Handle designated initializers, as an extension.  */
7558                   if (TREE_PURPOSE (*initp))
7559                     {
7560                       if (pedantic)
7561                         pedwarn ("ISO C++ does not allow designated initializers");
7562                       field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7563                                               /*want_type=*/false);
7564                       if (!field || TREE_CODE (field) != FIELD_DECL)
7565                         error ("`%T' has no non-static data member named `%D'",
7566                                type, TREE_PURPOSE (*initp));
7567                     }
7568                   if (!field)
7569                     break;
7570
7571                   field_init = reshape_init (TREE_TYPE (field), initp);
7572                   TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7573                   CONSTRUCTOR_ELTS (new_init) = field_init;
7574                   /* [dcl.init.aggr] 
7575
7576                      When a union  is  initialized with a brace-enclosed
7577                      initializer, the braces shall only contain an
7578                      initializer for the first member of the union.  */
7579                   if (TREE_CODE (type) == UNION_TYPE)
7580                     break;
7581                   field = next_initializable_field (TREE_CHAIN (field));
7582                 }
7583             }
7584         }
7585       else if (TREE_CODE (type) == ARRAY_TYPE)
7586         {
7587           tree index;
7588           tree max_index;
7589
7590           /* If the bound of the array is known, take no more initializers
7591              than are allowed.  */
7592           max_index = (TYPE_DOMAIN (type) 
7593                        ? array_type_nelts (type) : NULL_TREE);
7594           /* Loop through the array elements, gathering initializers.  */
7595           for (index = size_zero_node;
7596                *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7597                index = size_binop (PLUS_EXPR, index, size_one_node))
7598             {
7599               tree element_init;
7600
7601               element_init = reshape_init (TREE_TYPE (type), initp);
7602               TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7603               CONSTRUCTOR_ELTS (new_init) = element_init;
7604               if (TREE_PURPOSE (element_init))
7605                 index = TREE_PURPOSE (element_init);
7606             }
7607         }
7608       else
7609         abort ();
7610
7611       /* The initializers were placed in reverse order in the
7612          CONSTRUCTOR.  */
7613       CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7614
7615       if (TREE_CODE (old_init) == TREE_LIST)
7616         new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7617     }
7618
7619   /* If this was a brace-enclosed initializer and all of the
7620      initializers were not used up, there is a problem.  */
7621   if (brace_enclosed_p && *initp)
7622     error ("too many initializers for `%T'", type);
7623
7624   return new_init;
7625 }
7626
7627 /* Verify INIT (the initializer for DECL), and record the
7628    initialization in DECL_INITIAL, if appropriate.  
7629
7630    If the return value is non-NULL, it is an expression that must be
7631    evaluated dynamically to initialize DECL.  */
7632
7633 static tree
7634 check_initializer (tree decl, tree init, int flags)
7635 {
7636   tree type = TREE_TYPE (decl);
7637
7638   /* If `start_decl' didn't like having an initialization, ignore it now.  */
7639   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7640     init = NULL_TREE;
7641
7642   /* If an initializer is present, DECL_INITIAL has been
7643      error_mark_node, to indicate that an as-of-yet unevaluated
7644      initialization will occur.  From now on, DECL_INITIAL reflects
7645      the static initialization -- if any -- of DECL.  */
7646   DECL_INITIAL (decl) = NULL_TREE;
7647
7648   /* Things that are going to be initialized need to have complete
7649      type.  */
7650   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7651
7652   if (type == error_mark_node)
7653     /* We will have already complained.  */
7654     init = NULL_TREE;
7655   else if (init && COMPLETE_TYPE_P (type) 
7656            && !TREE_CONSTANT (TYPE_SIZE (type)))
7657     {
7658       error ("variable-sized object `%D' may not be initialized", decl);
7659       init = NULL_TREE;
7660     }
7661   else if (TREE_CODE (type) == ARRAY_TYPE
7662            && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7663     {
7664       error ("elements of array `%#D' have incomplete type", decl);
7665       init = NULL_TREE;
7666     }
7667   else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7668     {
7669       error ("`%D' has incomplete type", decl);
7670       TREE_TYPE (decl) = error_mark_node;
7671       init = NULL_TREE;
7672     }
7673
7674   if (TREE_CODE (decl) == CONST_DECL)
7675     {
7676       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7677
7678       DECL_INITIAL (decl) = init;
7679
7680       my_friendly_assert (init != NULL_TREE, 149);
7681       init = NULL_TREE;
7682     }
7683   else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7684     init = grok_reference_init (decl, type, init);
7685   else if (init)
7686     {
7687       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7688         {
7689           /* [dcl.init] paragraph 13,
7690              If T is a scalar type, then a declaration of the form
7691              T x = { a };
7692              is equivalent to
7693              T x = a;
7694              
7695              reshape_init will complain about the extra braces,
7696              and doesn't do anything useful in the case where TYPE is
7697              scalar, so just don't call it.  */
7698           if (CP_AGGREGATE_TYPE_P (type))
7699             init = reshape_init (type, &init);
7700
7701           if ((*targetm.vector_opaque_p) (type))
7702             {
7703               error ("opaque vector types cannot be initialized");
7704               init = error_mark_node;
7705             }
7706         }
7707
7708       /* If DECL has an array type without a specific bound, deduce the
7709          array size from the initializer.  */
7710       maybe_deduce_size_from_array_init (decl, init);
7711       type = TREE_TYPE (decl);
7712       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7713         TREE_TYPE (init) = type;
7714
7715       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7716         {
7717           if (TREE_CODE (type) == ARRAY_TYPE)
7718             goto initialize_aggr;
7719           else if (TREE_CODE (init) == CONSTRUCTOR
7720                    && TREE_HAS_CONSTRUCTOR (init))
7721             {
7722               if (TYPE_NON_AGGREGATE_CLASS (type))
7723                 {
7724                   error ("`%D' must be initialized by constructor, not by `{...}'",
7725                          decl);
7726                   init = error_mark_node;
7727                 }
7728               else
7729                 goto dont_use_constructor;
7730             }
7731           else
7732             {
7733               int saved_stmts_are_full_exprs_p;
7734
7735             initialize_aggr:
7736               saved_stmts_are_full_exprs_p = 0;
7737               if (building_stmt_tree ())
7738                 {
7739                   saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7740                   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7741                 }
7742               init = build_aggr_init (decl, init, flags);
7743               if (building_stmt_tree ())
7744                 current_stmt_tree ()->stmts_are_full_exprs_p =
7745                   saved_stmts_are_full_exprs_p;
7746               return init;
7747             }
7748         }
7749       else
7750         {
7751         dont_use_constructor:
7752           if (TREE_CODE (init) != TREE_VEC)
7753             init = store_init_value (decl, init);
7754         }
7755     }
7756   else if (DECL_EXTERNAL (decl))
7757     ;
7758   else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7759     goto initialize_aggr;
7760   else if (IS_AGGR_TYPE (type))
7761     {
7762       tree core_type = strip_array_types (type);
7763
7764       if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7765         error ("structure `%D' with uninitialized const members", decl);
7766       if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7767         error ("structure `%D' with uninitialized reference members",
7768                decl);
7769
7770       check_for_uninitialized_const_var (decl);
7771     }
7772   else
7773     check_for_uninitialized_const_var (decl);
7774
7775   if (init && init != error_mark_node)
7776     init = build (INIT_EXPR, type, decl, init);
7777
7778   return init;
7779 }
7780
7781 /* If DECL is not a local variable, give it RTL.  */
7782
7783 static void
7784 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7785 {
7786   int toplev = toplevel_bindings_p ();
7787   int defer_p;
7788
7789   /* Handle non-variables up front.  */
7790   if (TREE_CODE (decl) != VAR_DECL)
7791     {
7792       rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7793       return;
7794     }
7795
7796   /* If we see a class member here, it should be a static data
7797      member.  */
7798   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7799     {
7800       my_friendly_assert (TREE_STATIC (decl), 19990828);
7801       /* An in-class declaration of a static data member should be
7802          external; it is only a declaration, and not a definition.  */
7803       if (init == NULL_TREE)
7804         my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7805     }
7806
7807   /* Set the DECL_ASSEMBLER_NAME for the variable.  */
7808   if (asmspec)
7809     {
7810       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7811       /* The `register' keyword, when used together with an
7812          asm-specification, indicates that the variable should be
7813          placed in a particular register.  */
7814       if (DECL_REGISTER (decl))
7815         DECL_C_HARD_REGISTER (decl) = 1;
7816     }
7817
7818   /* We don't create any RTL for local variables.  */
7819   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7820     return;
7821
7822   /* We defer emission of local statics until the corresponding
7823      DECL_STMT is expanded.  */
7824   defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7825
7826   /* We try to defer namespace-scope static constants so that they are
7827      not emitted into the object file unnecessarily.  */
7828   if (!DECL_VIRTUAL_P (decl)
7829       && TREE_READONLY (decl)
7830       && DECL_INITIAL (decl) != NULL_TREE
7831       && DECL_INITIAL (decl) != error_mark_node
7832       && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7833       && toplev
7834       && !TREE_PUBLIC (decl))
7835     {
7836       /* Fool with the linkage of static consts according to #pragma
7837          interface.  */
7838       if (!interface_unknown && !TREE_PUBLIC (decl))
7839         {
7840           TREE_PUBLIC (decl) = 1;
7841           DECL_EXTERNAL (decl) = interface_only;
7842         }
7843
7844       defer_p = 1;
7845     }
7846   /* Likewise for template instantiations.  */
7847   else if (DECL_COMDAT (decl))
7848     defer_p = 1;
7849
7850   /* If we're deferring the variable, we only need to make RTL if
7851      there's an ASMSPEC.  Otherwise, we'll lazily create it later when
7852      we need it.  (There's no way to lazily create RTL for things that
7853      have assembly specs because the information about the specifier
7854      isn't stored in the tree, yet)  */
7855   if (defer_p && asmspec)
7856     make_decl_rtl (decl, asmspec);
7857   /* If we're not deferring, go ahead and assemble the variable.  */
7858   else if (!defer_p)
7859     rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7860 }
7861
7862 /* The old ARM scoping rules injected variables declared in the
7863    initialization statement of a for-statement into the surrounding
7864    scope.  We support this usage, in order to be backward-compatible.
7865    DECL is a just-declared VAR_DECL; if necessary inject its
7866    declaration into the surrounding scope.  */
7867
7868 void
7869 maybe_inject_for_scope_var (tree decl)
7870 {
7871   timevar_push (TV_NAME_LOOKUP);
7872   if (!DECL_NAME (decl))
7873     {
7874       timevar_pop (TV_NAME_LOOKUP);
7875       return;
7876     }
7877   
7878   /* Declarations of __FUNCTION__ and its ilk appear magically when
7879      the variable is first used.  If that happens to be inside a
7880      for-loop, we don't want to do anything special.  */
7881   if (DECL_PRETTY_FUNCTION_P (decl))
7882     {
7883       timevar_pop (TV_NAME_LOOKUP);
7884       return;
7885     }
7886
7887   if (current_binding_level->is_for_scope)
7888     {
7889       struct cp_binding_level *outer
7890         = current_binding_level->level_chain;
7891
7892       /* Check to see if the same name is already bound at the outer
7893          level, either because it was directly declared, or because a
7894          dead for-decl got preserved.  In either case, the code would
7895          not have been valid under the ARM scope rules, so clear
7896          is_for_scope for the current_binding_level.
7897
7898          Otherwise, we need to preserve the temp slot for decl to last
7899          into the outer binding level.  */
7900
7901       cxx_binding *outer_binding
7902         = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7903
7904       if (outer_binding && BINDING_SCOPE (outer_binding) == outer
7905           && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7906           && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7907         {
7908           BINDING_VALUE (outer_binding)
7909             = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7910           current_binding_level->is_for_scope = 0;
7911         }
7912     }
7913   timevar_pop (TV_NAME_LOOKUP);
7914 }
7915
7916 /* Generate code to initialize DECL (a local variable).  */
7917
7918 static void
7919 initialize_local_var (tree decl, tree init)
7920 {
7921   tree type = TREE_TYPE (decl);
7922
7923   my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7924                       || TREE_CODE (decl) == RESULT_DECL, 
7925                       20021010);
7926   my_friendly_assert (!TREE_STATIC (decl), 20021010);
7927
7928   if (DECL_SIZE (decl) == NULL_TREE)
7929     {
7930       /* If we used it already as memory, it must stay in memory.  */
7931       DECL_INITIAL (decl) = NULL_TREE;
7932       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7933     }
7934
7935   if (DECL_SIZE (decl) && type != error_mark_node)
7936     {
7937       int already_used;
7938
7939       /* Compute and store the initial value.  */
7940       already_used = TREE_USED (decl) || TREE_USED (type);
7941
7942       /* Perform the initialization.  */
7943       if (init)
7944         {
7945           int saved_stmts_are_full_exprs_p;
7946
7947           my_friendly_assert (building_stmt_tree (), 20000906);
7948           saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7949           current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7950           finish_expr_stmt (init);
7951           current_stmt_tree ()->stmts_are_full_exprs_p =
7952             saved_stmts_are_full_exprs_p;
7953         }
7954
7955       /* Set this to 0 so we can tell whether an aggregate which was
7956          initialized was ever used.  Don't do this if it has a
7957          destructor, so we don't complain about the 'resource
7958          allocation is initialization' idiom.  Now set
7959          attribute((unused)) on types so decls of that type will be
7960          marked used. (see TREE_USED, above.)  */
7961       if (TYPE_NEEDS_CONSTRUCTING (type)
7962           && ! already_used
7963           && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7964           && DECL_NAME (decl))
7965         TREE_USED (decl) = 0;
7966       else if (already_used)
7967         TREE_USED (decl) = 1;
7968     }
7969
7970   /* Generate a cleanup, if necessary.  */
7971   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7972     {
7973       tree cleanup;
7974
7975       /* Compute the cleanup.  */
7976       cleanup = cxx_maybe_build_cleanup (decl);
7977       
7978       /* Record the cleanup required for this declaration.  */
7979       if (DECL_SIZE (decl) && cleanup)
7980         finish_decl_cleanup (decl, cleanup);
7981     }
7982 }
7983
7984 /* Finish processing of a declaration;
7985    install its line number and initial value.
7986    If the length of an array type is not known before,
7987    it must be determined now, from the initial value, or it is an error.
7988
7989    INIT holds the value of an initializer that should be allowed to escape
7990    the normal rules.
7991
7992    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7993    if the (init) syntax was used.  */
7994
7995 void
7996 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
7997 {
7998   register tree type;
7999   tree ttype = NULL_TREE;
8000   const char *asmspec = NULL;
8001   int was_readonly = 0;
8002
8003   if (! decl)
8004     {
8005       if (init)
8006         error ("assignment (not initialization) in declaration");
8007       return;
8008     }
8009
8010   /* If a name was specified, get the string.  */
8011   if (current_binding_level == global_binding_level)
8012     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8013   if (asmspec_tree)
8014     asmspec = TREE_STRING_POINTER (asmspec_tree);
8015
8016   if (init && TREE_CODE (init) == NAMESPACE_DECL)
8017     {
8018       error ("cannot initialize `%D' to namespace `%D'",
8019                 decl, init);
8020       init = NULL_TREE;
8021     }
8022
8023   if (current_class_type
8024       && CP_DECL_CONTEXT (decl) == current_class_type
8025       && TYPE_BEING_DEFINED (current_class_type)
8026       && (DECL_INITIAL (decl) || init))
8027     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8028
8029   if (TREE_CODE (decl) == VAR_DECL
8030       && DECL_CONTEXT (decl)
8031       && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8032       && DECL_CONTEXT (decl) != current_namespace
8033       && init)
8034     {
8035       /* Leave the namespace of the object.  */
8036       pop_decl_namespace ();
8037     }
8038
8039   type = TREE_TYPE (decl);
8040
8041   if (type == error_mark_node)
8042     return;
8043
8044   if (TYPE_HAS_MUTABLE_P (type))
8045     TREE_READONLY (decl) = 0;
8046
8047   if (processing_template_decl)
8048     {
8049       /* Add this declaration to the statement-tree.  */
8050       if (at_function_scope_p ()
8051           && TREE_CODE (decl) != RESULT_DECL)
8052         add_decl_stmt (decl);
8053
8054       if (init && DECL_INITIAL (decl))
8055         DECL_INITIAL (decl) = init;
8056       goto finish_end0;
8057     }
8058
8059   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8060   my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8061
8062   /* Take care of TYPE_DECLs up front.  */
8063   if (TREE_CODE (decl) == TYPE_DECL)
8064     {
8065       if (type != error_mark_node
8066           && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8067         {
8068           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8069             warning ("shadowing previous type declaration of `%#D'", decl);
8070           set_identifier_type_value (DECL_NAME (decl), type);
8071           CLASSTYPE_GOT_SEMICOLON (type) = 1;
8072         }
8073
8074       /* If we have installed this as the canonical typedef for this
8075          type, and that type has not been defined yet, delay emitting
8076          the debug information for it, as we will emit it later.  */
8077       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8078           && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8079         TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8080
8081       rest_of_decl_compilation (decl, NULL,
8082                                 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8083       goto finish_end;
8084     }
8085
8086   if (TREE_CODE (decl) != FUNCTION_DECL)
8087     ttype = target_type (type);
8088
8089   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8090       && TYPE_NEEDS_CONSTRUCTING (type))
8091     {
8092       /* Currently, GNU C++ puts constants in text space, making them
8093          impossible to initialize.  In the future, one would hope for
8094          an operating system which understood the difference between
8095          initialization and the running of a program.  */
8096       was_readonly = 1;
8097       TREE_READONLY (decl) = 0;
8098     }
8099
8100   if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8101     {
8102       /* This must override the asm specifier which was placed by
8103          grokclassfn.  Lay this out fresh.  */
8104       SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8105       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8106       make_decl_rtl (decl, asmspec);
8107     }
8108   else if (TREE_CODE (decl) == RESULT_DECL)
8109     init = check_initializer (decl, init, flags);
8110   else if (TREE_CODE (decl) == VAR_DECL)
8111     {
8112       /* Only PODs can have thread-local storage.  Other types may require
8113          various kinds of non-trivial initialization.  */
8114       if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8115         error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8116                decl, TREE_TYPE (decl));
8117       /* Convert the initializer to the type of DECL, if we have not
8118          already initialized DECL.  */
8119       if (!DECL_INITIALIZED_P (decl)
8120           /* If !DECL_EXTERNAL then DECL is being defined.  In the
8121              case of a static data member initialized inside the
8122              class-specifier, there can be an initializer even if DECL
8123              is *not* defined.  */
8124           && (!DECL_EXTERNAL (decl) || init))
8125         {
8126           init = check_initializer (decl, init, flags);
8127           /* Thread-local storage cannot be dynamically initialized.  */
8128           if (DECL_THREAD_LOCAL (decl) && init)
8129             {
8130               error ("`%D' is thread-local and so cannot be dynamically "
8131                      "initialized", decl);
8132               init = NULL_TREE;
8133             }
8134           /* Handle:
8135              
8136              [dcl.init]
8137              
8138              The memory occupied by any object of static storage
8139              duration is zero-initialized at program startup before
8140              any other initialization takes place.
8141              
8142              We cannot create an appropriate initializer until after
8143              the type of DECL is finalized.  If DECL_INITIAL is set,
8144              then the DECL is statically initialized, and any
8145              necessary zero-initialization has already been performed.  */
8146           if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8147             DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8148                                                    /*nelts=*/NULL_TREE,
8149                                                    /*static_storage_p=*/true);
8150           /* Remember that the initialization for this variable has
8151              taken place.  */
8152           DECL_INITIALIZED_P (decl) = 1;
8153         }
8154       /* If the variable has an array type, lay out the type, even if
8155          there is no initializer.  It is valid to index through the
8156          array, and we must get TYPE_ALIGN set correctly on the array
8157          type.  */
8158       else if (TREE_CODE (type) == ARRAY_TYPE)
8159         layout_type (type);
8160     }
8161
8162   /* Add this declaration to the statement-tree.  This needs to happen
8163      after the call to check_initializer so that the DECL_STMT for a
8164      reference temp is added before the DECL_STMT for the reference itself.  */
8165   if (building_stmt_tree ()
8166       && at_function_scope_p ()
8167       && TREE_CODE (decl) != RESULT_DECL)
8168     add_decl_stmt (decl);
8169
8170   if (TREE_CODE (decl) == VAR_DECL)
8171     layout_var_decl (decl);
8172
8173   /* Output the assembler code and/or RTL code for variables and functions,
8174      unless the type is an undefined structure or union.
8175      If not, it will get done when the type is completed.  */
8176   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8177       || TREE_CODE (decl) == RESULT_DECL)
8178     {
8179       if (TREE_CODE (decl) == VAR_DECL)
8180         maybe_commonize_var (decl);
8181
8182       make_rtl_for_nonlocal_decl (decl, init, asmspec);
8183
8184       if (TREE_CODE (type) == FUNCTION_TYPE
8185           || TREE_CODE (type) == METHOD_TYPE)
8186         abstract_virtuals_error (decl,
8187                                  strip_array_types (TREE_TYPE (type)));
8188       else
8189         abstract_virtuals_error (decl, strip_array_types (type));
8190
8191       if (TREE_CODE (decl) == FUNCTION_DECL 
8192           || TREE_TYPE (decl) == error_mark_node)
8193         /* No initialization required.  */
8194         ;
8195       else if (DECL_EXTERNAL (decl)
8196                && ! (DECL_LANG_SPECIFIC (decl)
8197                      && DECL_NOT_REALLY_EXTERN (decl)))
8198         {
8199           if (init)
8200             DECL_INITIAL (decl) = init;
8201         }
8202       else
8203         {
8204           /* A variable definition.  */
8205           if (DECL_FUNCTION_SCOPE_P (decl))
8206             {
8207               /* This is a local declaration.  */
8208               if (doing_semantic_analysis_p ())
8209                 maybe_inject_for_scope_var (decl);
8210               /* Initialize the local variable.  */
8211               if (processing_template_decl)
8212                 {
8213                   if (init || DECL_INITIAL (decl) == error_mark_node)
8214                     DECL_INITIAL (decl) = init;
8215                 }
8216               else if (!TREE_STATIC (decl))
8217                 initialize_local_var (decl, init);
8218             }
8219
8220           if (TREE_STATIC (decl))
8221             expand_static_init (decl, init);
8222         }
8223     finish_end0:
8224
8225       /* Undo call to `pushclass' that was done in `start_decl'
8226          due to initialization of qualified member variable.
8227          I.e., Foo::x = 10;  */
8228       {
8229         tree context = CP_DECL_CONTEXT (decl);
8230         if (context
8231             && TYPE_P (context)
8232             && (TREE_CODE (decl) == VAR_DECL
8233                 /* We also have a pushclass done that we need to undo here
8234                    if we're at top level and declare a method.  */
8235                 || TREE_CODE (decl) == FUNCTION_DECL)
8236             /* If size hasn't been set, we're still defining it,
8237                and therefore inside the class body; don't pop
8238                the binding level..  */
8239             && COMPLETE_TYPE_P (context)
8240             && context == current_class_type)
8241           pop_nested_class ();
8242       }
8243     }
8244
8245  finish_end:
8246
8247   if (was_readonly)
8248     TREE_READONLY (decl) = 1;
8249 }
8250
8251 /* This is here for a midend callback from c-common.c */
8252
8253 void
8254 finish_decl (tree decl, tree init, tree asmspec_tree)
8255 {
8256   cp_finish_decl (decl, init, asmspec_tree, 0);
8257 }
8258
8259 /* Returns a declaration for a VAR_DECL as if:
8260
8261      extern "C" TYPE NAME;
8262
8263    had been seen.  Used to create compiler-generated global
8264    variables.  */
8265
8266 tree
8267 declare_global_var (tree name, tree type)
8268 {
8269   tree decl;
8270
8271   push_to_top_level ();
8272   decl = build_decl (VAR_DECL, name, type);
8273   TREE_PUBLIC (decl) = 1;
8274   DECL_EXTERNAL (decl) = 1;
8275   DECL_ARTIFICIAL (decl) = 1;
8276   pushdecl (decl);
8277   cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8278   pop_from_top_level ();
8279
8280   return decl;
8281 }
8282
8283 /* Returns a pointer to the `atexit' function.  Note that if
8284    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8285    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
8286
8287 static tree
8288 get_atexit_node (void)
8289 {
8290   tree atexit_fndecl;
8291   tree arg_types;
8292   tree fn_type;
8293   tree fn_ptr_type;
8294   const char *name;
8295
8296   if (atexit_node)
8297     return atexit_node;
8298
8299   if (flag_use_cxa_atexit)
8300     {
8301       /* The declaration for `__cxa_atexit' is:
8302
8303            int __cxa_atexit (void (*)(void *), void *, void *)
8304
8305          We build up the argument types and then then function type
8306          itself.  */
8307
8308       /* First, build the pointer-to-function type for the first
8309          argument.  */
8310       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8311       fn_type = build_function_type (void_type_node, arg_types);
8312       fn_ptr_type = build_pointer_type (fn_type);
8313       /* Then, build the rest of the argument types.  */
8314       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8315       arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8316       arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8317       /* And the final __cxa_atexit type.  */
8318       fn_type = build_function_type (integer_type_node, arg_types);
8319       fn_ptr_type = build_pointer_type (fn_type);
8320       name = "__cxa_atexit";
8321     }
8322   else
8323     {
8324       /* The declaration for `atexit' is:
8325
8326            int atexit (void (*)());
8327
8328          We build up the argument types and then then function type
8329          itself.  */
8330       fn_type = build_function_type (void_type_node, void_list_node);
8331       fn_ptr_type = build_pointer_type (fn_type);
8332       arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8333       /* Build the final atexit type.  */
8334       fn_type = build_function_type (integer_type_node, arg_types);
8335       name = "atexit";
8336     }
8337
8338   /* Now, build the function declaration.  */
8339   push_lang_context (lang_name_c);
8340   atexit_fndecl = build_library_fn_ptr (name, fn_type);
8341   mark_used (atexit_fndecl);
8342   pop_lang_context ();
8343   atexit_node = default_conversion (atexit_fndecl);
8344
8345   return atexit_node;
8346 }
8347
8348 /* Returns the __dso_handle VAR_DECL.  */
8349
8350 static tree
8351 get_dso_handle_node (void)
8352 {
8353   if (dso_handle_node)
8354     return dso_handle_node;
8355
8356   /* Declare the variable.  */
8357   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8358                                         ptr_type_node);
8359
8360   return dso_handle_node;
8361 }
8362
8363 /* Begin a new function with internal linkage whose job will be simply
8364    to destroy some particular variable.  */
8365
8366 static tree
8367 start_cleanup_fn (void)
8368 {
8369   static int counter = 0;
8370   int old_interface_only = interface_only;
8371   int old_interface_unknown = interface_unknown;
8372   char name[32];
8373   tree parmtypes;
8374   tree fntype;
8375   tree fndecl;
8376
8377   push_to_top_level ();
8378
8379   /* No need to mangle this.  */
8380   push_lang_context (lang_name_c);
8381
8382   interface_only = 0;
8383   interface_unknown = 1;
8384
8385   /* Build the parameter-types.  */
8386   parmtypes = void_list_node;
8387   /* Functions passed to __cxa_atexit take an additional parameter.
8388      We'll just ignore it.  After we implement the new calling
8389      convention for destructors, we can eliminate the use of
8390      additional cleanup functions entirely in the -fnew-abi case.  */
8391   if (flag_use_cxa_atexit)
8392     parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8393   /* Build the function type itself.  */
8394   fntype = build_function_type (void_type_node, parmtypes);
8395   /* Build the name of the function.  */
8396   sprintf (name, "__tcf_%d", counter++);
8397   /* Build the function declaration.  */
8398   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8399   /* It's a function with internal linkage, generated by the
8400      compiler.  */
8401   TREE_PUBLIC (fndecl) = 0;
8402   DECL_ARTIFICIAL (fndecl) = 1;
8403   /* Make the function `inline' so that it is only emitted if it is
8404      actually needed.  It is unlikely that it will be inlined, since
8405      it is only called via a function pointer, but we avoid unnecessary
8406      emissions this way.  */
8407   DECL_INLINE (fndecl) = 1;
8408   /* Build the parameter.  */
8409   if (flag_use_cxa_atexit)
8410     {
8411       tree parmdecl;
8412
8413       parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8414       DECL_CONTEXT (parmdecl) = fndecl;
8415       TREE_USED (parmdecl) = 1;
8416       DECL_ARGUMENTS (fndecl) = parmdecl;
8417     }
8418
8419   pushdecl (fndecl);
8420   start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8421
8422   interface_unknown = old_interface_unknown;
8423   interface_only = old_interface_only;
8424
8425   pop_lang_context ();
8426
8427   return current_function_decl;
8428 }
8429
8430 /* Finish the cleanup function begun by start_cleanup_fn.  */
8431
8432 static void
8433 end_cleanup_fn (void)
8434 {
8435   expand_body (finish_function (0));
8436
8437   pop_from_top_level ();
8438 }
8439
8440 /* Generate code to handle the destruction of DECL, an object with
8441    static storage duration.  */
8442
8443 void
8444 register_dtor_fn (tree decl)
8445 {
8446   tree cleanup;
8447   tree compound_stmt;
8448   tree args;
8449   tree fcall;
8450   int saved_flag_access_control;
8451
8452   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8453     return;
8454
8455   /* Call build_cleanup before we enter the anonymous function so that
8456      any access checks will be done relative to the current scope,
8457      rather than the scope of the anonymous function.  */
8458   build_cleanup (decl);
8459
8460   /* Now start the function.  */
8461   cleanup = start_cleanup_fn ();
8462
8463   /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8464      to the original function, rather than the anonymous one.  That
8465      will make the back-end think that nested functions are in use,
8466      which causes confusion.  */
8467   saved_flag_access_control = flag_access_control;
8468   scope_chain->check_access = flag_access_control = 0;
8469   fcall = build_cleanup (decl);
8470   scope_chain->check_access = flag_access_control = saved_flag_access_control;
8471
8472   /* Create the body of the anonymous function.  */
8473   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8474   finish_expr_stmt (fcall);
8475   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8476   end_cleanup_fn ();
8477
8478   /* Call atexit with the cleanup function.  */
8479   cxx_mark_addressable (cleanup);
8480   cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8481   if (flag_use_cxa_atexit)
8482     {
8483       args = tree_cons (NULL_TREE, 
8484                         build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8485                         NULL_TREE);
8486       args = tree_cons (NULL_TREE, null_pointer_node, args);
8487       args = tree_cons (NULL_TREE, cleanup, args);
8488     }
8489   else
8490     args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8491   finish_expr_stmt (build_function_call (get_atexit_node (), args));
8492 }
8493
8494 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8495    is its initializer.  Generate code to handle the construction
8496    and destruction of DECL.  */
8497
8498 static void
8499 expand_static_init (tree decl, tree init)
8500 {
8501   tree oldstatic;
8502
8503   my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8504   my_friendly_assert (TREE_STATIC (decl), 20021010);
8505
8506   /* Some variables require no initialization.  */
8507   if (!init 
8508       && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8509       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8510     return;
8511
8512   oldstatic = value_member (decl, static_aggregates);
8513
8514   if (oldstatic)
8515     {
8516       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8517         error ("multiple initializations given for `%D'", decl);
8518     }
8519   else if (! toplevel_bindings_p ())
8520     {
8521       /* Emit code to perform this initialization but once.  */
8522       tree if_stmt;
8523       tree then_clause;
8524       tree assignment;
8525       tree guard;
8526       tree guard_init;
8527
8528       /* Emit code to perform this initialization but once.  This code
8529          looks like:
8530
8531            static int guard = 0;
8532            if (!guard) {
8533              // Do initialization.
8534              guard = 1;
8535              // Register variable for destruction at end of program.
8536            }
8537
8538          Note that the `temp' variable is only set to 1 *after* the
8539          initialization is complete.  This ensures that an exception,
8540          thrown during the construction, will cause the variable to
8541          reinitialized when we pass through this code again, as per:
8542
8543            [stmt.dcl]
8544
8545            If the initialization exits by throwing an exception, the
8546            initialization is not complete, so it will be tried again
8547            the next time control enters the declaration.
8548
8549          In theory, this process should be thread-safe, too; multiple
8550          threads should not be able to initialize the variable more
8551          than once.  We don't yet attempt to ensure thread-safety.  */
8552
8553       /* Create the guard variable.  */
8554       guard = get_guard (decl);
8555
8556       /* Begin the conditional initialization.  */
8557       if_stmt = begin_if_stmt ();
8558       finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8559       then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8560
8561       /* Do the initialization itself.  */
8562       assignment = init ? init : NULL_TREE;
8563
8564       /* Once the assignment is complete, set TEMP to 1.  Since the
8565          construction of the static object is complete at this point,
8566          we want to make sure TEMP is set to 1 even if a temporary
8567          constructed during the initialization throws an exception
8568          when it is destroyed.  So, we combine the initialization and
8569          the assignment to TEMP into a single expression, ensuring
8570          that when we call finish_expr_stmt the cleanups will not be
8571          run until after TEMP is set to 1.  */
8572       guard_init = set_guard (guard);
8573       if (assignment)
8574         {
8575           assignment = tree_cons (NULL_TREE, assignment,
8576                                   build_tree_list (NULL_TREE,
8577                                                    guard_init));
8578           assignment = build_compound_expr (assignment);
8579         }
8580       else
8581         assignment = guard_init;
8582       finish_expr_stmt (assignment);
8583
8584       /* Use atexit to register a function for destroying this static
8585          variable.  */
8586       register_dtor_fn (decl);
8587
8588       finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8589       finish_then_clause (if_stmt);
8590       finish_if_stmt ();
8591     }
8592   else
8593     static_aggregates = tree_cons (init, decl, static_aggregates);
8594 }
8595
8596 /* Finish the declaration of a catch-parameter.  */
8597
8598 tree
8599 start_handler_parms (tree declspecs, tree declarator)
8600 {
8601   tree decl;
8602   if (declspecs)
8603     {
8604       decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8605                              1, NULL);
8606       if (decl == NULL_TREE)
8607         error ("invalid catch parameter");
8608     }
8609   else
8610     decl = NULL_TREE;
8611
8612   return decl;
8613 }
8614
8615 \f
8616 /* Make TYPE a complete type based on INITIAL_VALUE.
8617    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8618    2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8619
8620 int
8621 complete_array_type (tree type, tree initial_value, int do_default)
8622 {
8623   register tree maxindex = NULL_TREE;
8624   int value = 0;
8625
8626   if (initial_value)
8627     {
8628       /* An array of character type can be initialized from a
8629          brace-enclosed string constant.  */
8630       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8631           && TREE_CODE (initial_value) == CONSTRUCTOR
8632           && CONSTRUCTOR_ELTS (initial_value)
8633           && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8634               == STRING_CST)
8635           && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8636         initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8637
8638       /* Note MAXINDEX is really the maximum index, one less than the
8639          size.  */
8640       if (TREE_CODE (initial_value) == STRING_CST)
8641         {
8642           int eltsize
8643             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8644           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8645                                    / eltsize) - 1, 0);
8646         }
8647       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8648         {
8649           tree elts = CONSTRUCTOR_ELTS (initial_value);
8650
8651           maxindex = ssize_int (-1);
8652           for (; elts; elts = TREE_CHAIN (elts))
8653             {
8654               if (TREE_PURPOSE (elts))
8655                 maxindex = TREE_PURPOSE (elts);
8656               else
8657                 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8658             }
8659           maxindex = copy_node (maxindex);
8660         }
8661       else
8662         {
8663           /* Make an error message unless that happened already.  */
8664           if (initial_value != error_mark_node)
8665             value = 1;
8666           else
8667             initial_value = NULL_TREE;
8668
8669           /* Prevent further error messages.  */
8670           maxindex = build_int_2 (0, 0);
8671         }
8672     }
8673
8674   if (!maxindex)
8675     {
8676       if (do_default)
8677         maxindex = build_int_2 (0, 0);
8678       value = 2;
8679     }
8680
8681   if (maxindex)
8682     {
8683       tree itype;
8684       tree domain;
8685
8686       domain = build_index_type (maxindex);
8687       TYPE_DOMAIN (type) = domain;
8688
8689       if (! TREE_TYPE (maxindex))
8690         TREE_TYPE (maxindex) = domain;
8691       if (initial_value)
8692         itype = TREE_TYPE (initial_value);
8693       else
8694         itype = NULL;
8695       if (itype && !TYPE_DOMAIN (itype))
8696         TYPE_DOMAIN (itype) = domain;
8697       /* The type of the main variant should never be used for arrays
8698          of different sizes.  It should only ever be completed with the
8699          size of the array.  */
8700       if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8701         TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8702     }
8703
8704   /* Lay out the type now that we can get the real answer.  */
8705
8706   layout_type (type);
8707
8708   return value;
8709 }
8710 \f
8711 /* Return zero if something is declared to be a member of type
8712    CTYPE when in the context of CUR_TYPE.  STRING is the error
8713    message to print in that case.  Otherwise, quietly return 1.  */
8714
8715 static int
8716 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8717 {
8718   if (ctype && ctype != cur_type)
8719     {
8720       if (flags == DTOR_FLAG)
8721         error ("destructor for alien class `%T' cannot be a member",
8722                   ctype);
8723       else
8724         error ("constructor for alien class `%T' cannot be a member",
8725                   ctype);
8726       return 0;
8727     }
8728   return 1;
8729 }
8730 \f
8731 /* Subroutine of `grokdeclarator'.  */
8732
8733 /* Generate errors possibly applicable for a given set of specifiers.
8734    This is for ARM $7.1.2.  */
8735
8736 static void
8737 bad_specifiers (tree object,
8738                 const char* type,
8739                 int virtualp,
8740                 int quals,
8741                 int inlinep,
8742                 int friendp,
8743                 int raises)
8744 {
8745   if (virtualp)
8746     error ("`%D' declared as a `virtual' %s", object, type);
8747   if (inlinep)
8748     error ("`%D' declared as an `inline' %s", object, type);
8749   if (quals)
8750     error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8751               object, type);
8752   if (friendp)
8753     cp_error_at ("`%D' declared as a friend", object);
8754   if (raises
8755       && (TREE_CODE (object) == TYPE_DECL
8756           || (!TYPE_PTRFN_P (TREE_TYPE (object))
8757               && !TYPE_REFFN_P (TREE_TYPE (object))
8758               && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8759     cp_error_at ("`%D' declared with an exception specification", object);
8760 }
8761
8762 /* CTYPE is class type, or null if non-class.
8763    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8764    or METHOD_TYPE.
8765    DECLARATOR is the function's name.
8766    VIRTUALP is truthvalue of whether the function is virtual or not.
8767    FLAGS are to be passed through to `grokclassfn'.
8768    QUALS are qualifiers indicating whether the function is `const'
8769    or `volatile'.
8770    RAISES is a list of exceptions that this function can raise.
8771    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8772    not look, and -1 if we should not call `grokclassfn' at all.
8773
8774    Returns `NULL_TREE' if something goes wrong, after issuing
8775    applicable error messages.  */
8776
8777 static tree
8778 grokfndecl (tree ctype, 
8779             tree type,
8780             tree declarator,
8781             tree orig_declarator,
8782             int virtualp,
8783             enum overload_flags flags,
8784             tree quals, 
8785             tree raises,
8786             int check, 
8787             int friendp, 
8788             int publicp, 
8789             int inlinep, 
8790             int funcdef_flag, 
8791             int template_count,
8792             tree in_namespace)
8793 {
8794   tree decl;
8795   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8796   int has_default_arg = 0;
8797   tree t;
8798
8799   if (raises)
8800     type = build_exception_variant (type, raises);
8801
8802   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8803   /* Propagate volatile out from type to decl.  */
8804   if (TYPE_VOLATILE (type))
8805     TREE_THIS_VOLATILE (decl) = 1;
8806
8807   /* If this decl has namespace scope, set that up.  */
8808   if (in_namespace)
8809     set_decl_namespace (decl, in_namespace, friendp);
8810   else if (!ctype)
8811     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8812
8813   /* `main' and builtins have implicit 'C' linkage.  */
8814   if ((MAIN_NAME_P (declarator)
8815        || (IDENTIFIER_LENGTH (declarator) > 10
8816            && IDENTIFIER_POINTER (declarator)[0] == '_'
8817            && IDENTIFIER_POINTER (declarator)[1] == '_'
8818            && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8819       && current_lang_name == lang_name_cplusplus
8820       && ctype == NULL_TREE
8821       /* NULL_TREE means global namespace.  */
8822       && DECL_CONTEXT (decl) == NULL_TREE)
8823     SET_DECL_LANGUAGE (decl, lang_c);
8824
8825   /* Should probably propagate const out from type to decl I bet (mrs).  */
8826   if (staticp)
8827     {
8828       DECL_STATIC_FUNCTION_P (decl) = 1;
8829       DECL_CONTEXT (decl) = ctype;
8830     }
8831
8832   if (ctype)
8833     DECL_CONTEXT (decl) = ctype;
8834
8835   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8836     {
8837       if (processing_template_decl)
8838         error ("cannot declare `::main' to be a template");
8839       if (inlinep)
8840         error ("cannot declare `::main' to be inline");
8841       if (!publicp)
8842         error ("cannot declare `::main' to be static");
8843       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8844                         integer_type_node))
8845         error ("`main' must return `int'");
8846       inlinep = 0;
8847       publicp = 1;
8848     }
8849
8850   /* Members of anonymous types and local classes have no linkage; make
8851      them internal.  */
8852   /* FIXME what if it gets a name from typedef?  */
8853   if (ctype && (TYPE_ANONYMOUS_P (ctype)
8854                 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8855     publicp = 0;
8856
8857   if (publicp)
8858     {
8859       /* [basic.link]: A name with no linkage (notably, the name of a class
8860          or enumeration declared in a local scope) shall not be used to
8861          declare an entity with linkage.
8862
8863          Only check this for public decls for now.  */
8864       t = no_linkage_check (TREE_TYPE (decl));
8865       if (t)
8866         {
8867           if (TYPE_ANONYMOUS_P (t))
8868             {
8869               if (DECL_EXTERN_C_P (decl))
8870                 /* Allow this; it's pretty common in C.  */;
8871               else
8872                 {
8873                   pedwarn ("non-local function `%#D' uses anonymous type",
8874                               decl);
8875                   if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8876                     cp_pedwarn_at ("\
8877 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8878                                 TYPE_NAME (t));
8879                 }
8880             }
8881           else
8882             pedwarn ("non-local function `%#D' uses local type `%T'",
8883                         decl, t);
8884         }
8885     }
8886
8887   TREE_PUBLIC (decl) = publicp;
8888   if (! publicp)
8889     {
8890       DECL_INTERFACE_KNOWN (decl) = 1;
8891       DECL_NOT_REALLY_EXTERN (decl) = 1;
8892     }
8893
8894   DID_INLINE_FUNC (decl) = 0;
8895   /* If the declaration was declared inline, mark it as such.  */
8896   if (inlinep)
8897     DECL_DECLARED_INLINE_P (decl) = 1;
8898   /* We inline functions that are explicitly declared inline, or, when
8899      the user explicitly asks us to, all functions.  */
8900   if (DECL_DECLARED_INLINE_P (decl))
8901     DECL_INLINE (decl) = 1;
8902   if (flag_inline_trees == 2 && !DECL_INLINE (decl))
8903     {
8904       DID_INLINE_FUNC (decl) = 1;
8905       DECL_INLINE (decl) = 1;
8906     }
8907
8908   DECL_EXTERNAL (decl) = 1;
8909   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8910     {
8911       error ("%smember function `%D' cannot have `%T' method qualifier",
8912                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8913       quals = NULL_TREE;
8914     }
8915
8916   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8917     grok_op_properties (decl, friendp);
8918
8919   if (ctype && decl_function_context (decl))
8920     DECL_NO_STATIC_CHAIN (decl) = 1;
8921
8922   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8923     if (TREE_PURPOSE (t)
8924         && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8925       {
8926         has_default_arg = 1;
8927         break;
8928       }
8929
8930   if (friendp
8931       && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8932     {
8933       if (funcdef_flag)
8934         error
8935           ("defining explicit specialization `%D' in friend declaration",
8936            orig_declarator);
8937       else
8938         {
8939           tree fns = TREE_OPERAND (orig_declarator, 0);
8940           tree args = TREE_OPERAND (orig_declarator, 1);
8941
8942           if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8943             {
8944               /* Something like `template <class T> friend void f<T>()'.  */
8945               error ("invalid use of template-id `%D' in declaration of primary template",
8946                         orig_declarator);
8947               return NULL_TREE;
8948             }
8949
8950
8951           /* A friend declaration of the form friend void f<>().  Record
8952              the information in the TEMPLATE_ID_EXPR.  */
8953           SET_DECL_IMPLICIT_INSTANTIATION (decl);
8954
8955           if (TREE_CODE (fns) == COMPONENT_REF)
8956             {
8957               /* Due to bison parser ickiness, we will have already looked
8958                  up an operator_name or PFUNCNAME within the current class
8959                  (see template_id in parse.y). If the current class contains
8960                  such a name, we'll get a COMPONENT_REF here. Undo that.  */
8961
8962               my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8963                                   == current_class_type, 20001120);
8964               fns = TREE_OPERAND (fns, 1);
8965             }
8966           my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8967                               || TREE_CODE (fns) == LOOKUP_EXPR
8968                               || TREE_CODE (fns) == OVERLOAD, 20001120);
8969           DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8970
8971           if (has_default_arg)
8972             {
8973               error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8974                         decl);
8975               return NULL_TREE;
8976             }
8977
8978           if (inlinep)
8979             {
8980               error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8981                         decl);
8982               return NULL_TREE;
8983             }
8984         }
8985     }
8986
8987   if (funcdef_flag)
8988     /* Make the init_value nonzero so pushdecl knows this is not
8989        tentative.  error_mark_node is replaced later with the BLOCK.  */
8990     DECL_INITIAL (decl) = error_mark_node;
8991
8992   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8993     TREE_NOTHROW (decl) = 1;
8994
8995   /* Caller will do the rest of this.  */
8996   if (check < 0)
8997     return decl;
8998
8999   if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9000     DECL_CONSTRUCTOR_P (decl) = 1;
9001
9002   /* Function gets the ugly name, field gets the nice one.  This call
9003      may change the type of the function (because of default
9004      parameters)!  */
9005   if (ctype != NULL_TREE)
9006     grokclassfn (ctype, decl, flags, quals);
9007
9008   decl = check_explicit_specialization (orig_declarator, decl,
9009                                         template_count,
9010                                         2 * (funcdef_flag != 0) +
9011                                         4 * (friendp != 0));
9012   if (decl == error_mark_node)
9013     return NULL_TREE;
9014
9015   if (ctype != NULL_TREE
9016       && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9017       && check)
9018     {
9019       tree old_decl;
9020
9021       old_decl = check_classfn (ctype, decl);
9022
9023       if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9024         /* Because grokfndecl is always supposed to return a
9025            FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9026            here.  We depend on our callers to figure out that its
9027            really a template that's being returned.  */
9028         old_decl = DECL_TEMPLATE_RESULT (old_decl);
9029
9030       if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9031           && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9032         {
9033           /* Remove the `this' parm added by grokclassfn.
9034              XXX Isn't this done in start_function, too?  */
9035           revert_static_member_fn (decl);
9036           last_function_parms = TREE_CHAIN (last_function_parms);
9037         }
9038       if (old_decl && DECL_ARTIFICIAL (old_decl))
9039         error ("definition of implicitly-declared `%D'", old_decl);
9040
9041       if (old_decl)
9042         {
9043           bool ok;
9044
9045           /* Since we've smashed OLD_DECL to its
9046              DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9047           if (TREE_CODE (decl) == TEMPLATE_DECL)
9048             decl = DECL_TEMPLATE_RESULT (decl);
9049
9050           /* Attempt to merge the declarations.  This can fail, in
9051              the case of some invalid specialization declarations.  */
9052           push_scope (ctype);
9053           ok = duplicate_decls (decl, old_decl);
9054           pop_scope (ctype);
9055           if (!ok)
9056             {
9057               error ("no `%#D' member function declared in class `%T'",
9058                      decl, ctype);
9059               return NULL_TREE;
9060             }
9061           return old_decl;
9062         }
9063     }
9064
9065   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9066     return NULL_TREE;
9067
9068   if (ctype == NULL_TREE || check)
9069     return decl;
9070
9071   if (virtualp)
9072     DECL_VIRTUAL_P (decl) = 1;
9073
9074   return decl;
9075 }
9076
9077 /* Create a VAR_DECL named NAME with the indicated TYPE.  
9078
9079    If SCOPE is non-NULL, it is the class type or namespace containing
9080    the variable.  If SCOPE is NULL, the variable should is created in
9081    the innermost enclosings scope.  */
9082
9083 static tree
9084 grokvardecl (tree type,
9085              tree name,
9086              RID_BIT_TYPE * specbits_in,
9087              int initialized,
9088              int constp,
9089              tree scope)
9090 {
9091   tree decl;
9092   RID_BIT_TYPE specbits;
9093
9094   my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, 
9095                       20020808);
9096
9097   specbits = *specbits_in;
9098
9099   /* Compute the scope in which to place the variable.  */
9100   if (!scope)
9101     {
9102       /* An explicit "extern" specifier indicates a namespace-scope
9103          variable.  */
9104       if (RIDBIT_SETP (RID_EXTERN, specbits))
9105         scope = current_namespace;
9106       else if (!at_function_scope_p ())
9107         {
9108           scope = current_scope ();
9109           if (!scope)
9110             scope = current_namespace;
9111         }
9112     }
9113
9114   if (scope
9115       && (/* If the variable is a namespace-scope variable declared in a
9116              template, we need DECL_LANG_SPECIFIC.  */
9117           (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9118           /* Similarly for namespace-scope variables with language linkage
9119              other than C++.  */
9120           || (TREE_CODE (scope) == NAMESPACE_DECL 
9121               && current_lang_name != lang_name_cplusplus)
9122           /* Similarly for static data members.  */
9123           || TYPE_P (scope)))
9124     decl = build_lang_decl (VAR_DECL, name, type);
9125   else
9126     decl = build_decl (VAR_DECL, name, type);
9127
9128   if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9129     set_decl_namespace (decl, scope, 0);
9130   else
9131     DECL_CONTEXT (decl) = scope;
9132
9133   if (name && scope && current_lang_name != lang_name_c)
9134     /* We can't mangle lazily here because we don't have any
9135        way to recover whether or not a variable was `extern
9136        "C"' later.  */
9137     mangle_decl (decl);
9138
9139   if (RIDBIT_SETP (RID_EXTERN, specbits))
9140     {
9141       DECL_THIS_EXTERN (decl) = 1;
9142       DECL_EXTERNAL (decl) = !initialized;
9143     }
9144
9145   /* In class context, static means one per class,
9146      public access, and static storage.  */
9147   if (DECL_CLASS_SCOPE_P (decl))
9148     {
9149       TREE_PUBLIC (decl) = 1;
9150       TREE_STATIC (decl) = 1;
9151       DECL_EXTERNAL (decl) = 0;
9152     }
9153   /* At top level, either `static' or no s.c. makes a definition
9154      (perhaps tentative), and absence of `static' makes it public.  */
9155   else if (toplevel_bindings_p ())
9156     {
9157       TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9158                             && (DECL_THIS_EXTERN (decl) || ! constp));
9159       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9160     }
9161   /* Not at top level, only `static' makes a static definition.  */
9162   else
9163     {
9164       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9165       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9166     }
9167
9168   if (RIDBIT_SETP (RID_THREAD, specbits))
9169     {
9170       if (targetm.have_tls)
9171         DECL_THREAD_LOCAL (decl) = 1;
9172       else
9173         /* A mere warning is sure to result in improper semantics
9174            at runtime.  Don't bother to allow this to compile.  */
9175         error ("thread-local storage not supported for this target");
9176     }
9177
9178   if (TREE_PUBLIC (decl))
9179     {
9180       /* [basic.link]: A name with no linkage (notably, the name of a class
9181          or enumeration declared in a local scope) shall not be used to
9182          declare an entity with linkage.
9183
9184          Only check this for public decls for now.  */
9185       tree t = no_linkage_check (TREE_TYPE (decl));
9186       if (t)
9187         {
9188           if (TYPE_ANONYMOUS_P (t))
9189             /* Ignore for now; `enum { foo } e' is pretty common.  */;
9190           else
9191             pedwarn ("non-local variable `%#D' uses local type `%T'",
9192                         decl, t);
9193         }
9194     }
9195
9196   return decl;
9197 }
9198
9199 /* Create and return a canonical pointer to member function type, for
9200    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9201
9202 tree
9203 build_ptrmemfunc_type (tree type)
9204 {
9205   tree field, fields;
9206   tree t;
9207   tree unqualified_variant = NULL_TREE;
9208
9209   if (type == error_mark_node)
9210     return type;
9211
9212   /* If a canonical type already exists for this type, use it.  We use
9213      this method instead of type_hash_canon, because it only does a
9214      simple equality check on the list of field members.  */
9215
9216   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9217     return t;
9218
9219   /* Make sure that we always have the unqualified pointer-to-member
9220      type first.  */
9221   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9222     unqualified_variant
9223       = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9224
9225   t = make_aggr_type (RECORD_TYPE);
9226   /* Let the front-end know this is a pointer to member function...  */
9227   TYPE_PTRMEMFUNC_FLAG (t) = 1;
9228   /* ... and not really an aggregate.  */
9229   SET_IS_AGGR_TYPE (t, 0);
9230
9231   field = build_decl (FIELD_DECL, pfn_identifier, type);
9232   fields = field;
9233   
9234   field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9235   TREE_CHAIN (field) = fields;
9236   fields = field;
9237   
9238   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9239
9240   /* Zap out the name so that the back-end will give us the debugging
9241      information for this anonymous RECORD_TYPE.  */
9242   TYPE_NAME (t) = NULL_TREE;
9243
9244   /* If this is not the unqualified form of this pointer-to-member
9245      type, set the TYPE_MAIN_VARIANT for this type to be the
9246      unqualified type.  Since they are actually RECORD_TYPEs that are
9247      not variants of each other, we must do this manually.  */
9248   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9249     {
9250       t = build_qualified_type (t, cp_type_quals (type));
9251       TYPE_MAIN_VARIANT (t) = unqualified_variant;
9252       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9253       TYPE_NEXT_VARIANT (unqualified_variant) = t;
9254     }
9255
9256   /* Cache this pointer-to-member type so that we can find it again
9257      later.  */
9258   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9259
9260   /* Seems to be wanted.  */
9261   CLASSTYPE_GOT_SEMICOLON (t) = 1;
9262
9263   return t;
9264 }
9265
9266 /* Create and return a pointer to data member type.  */
9267
9268 tree
9269 build_ptrmem_type (tree class_type, tree member_type)
9270 {
9271   return build_pointer_type (build_offset_type (class_type, member_type));
9272 }
9273
9274 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9275    Check to see that the definition is valid.  Issue appropriate error
9276    messages.  Return 1 if the definition is particularly bad, or 0
9277    otherwise.  */
9278
9279 int
9280 check_static_variable_definition (tree decl, tree type)
9281 {
9282   /* Motion 10 at San Diego: If a static const integral data member is
9283      initialized with an integral constant expression, the initializer
9284      may appear either in the declaration (within the class), or in
9285      the definition, but not both.  If it appears in the class, the
9286      member is a member constant.  The file-scope definition is always
9287      required.  */
9288   if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9289     {
9290       error ("invalid in-class initialization of static data member of non-integral type `%T'",
9291              type);
9292       /* If we just return the declaration, crashes will sometimes
9293          occur.  We therefore return void_type_node, as if this was a
9294          friend declaration, to cause callers to completely ignore
9295          this declaration.  */
9296       return 1;
9297     }
9298   else if (!CP_TYPE_CONST_P (type))
9299     error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9300               decl);
9301   else if (pedantic && !INTEGRAL_TYPE_P (type))
9302     pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9303
9304   return 0;
9305 }
9306
9307 /* Given the SIZE (i.e., number of elements) in an array, compute an
9308    appropriate index type for the array.  If non-NULL, NAME is the
9309    name of the thing being declared.  */
9310
9311 tree
9312 compute_array_index_type (tree name, tree size)
9313 {
9314   tree itype;
9315
9316   /* If this involves a template parameter, it will be a constant at
9317      instantiation time, but we don't know what the value is yet.
9318      Even if no template parameters are involved, we may an expression
9319      that is not a constant; we don't even simplify `1 + 2' when
9320      processing a template.  */
9321   if (processing_template_decl)
9322     {
9323       /* Resolve a qualified reference to an enumerator or static
9324          const data member of ours.  */
9325       if (TREE_CODE (size) == SCOPE_REF
9326           && TREE_OPERAND (size, 0) == current_class_type)
9327         {
9328           tree t = lookup_field (current_class_type,
9329                                  TREE_OPERAND (size, 1), 0, false);
9330           if (t)
9331             size = t;
9332         }
9333
9334       return build_index_type (build_min (MINUS_EXPR, sizetype,
9335                                           size, integer_one_node));
9336     }
9337
9338   /* The size might be the result of a cast.  */
9339   STRIP_TYPE_NOPS (size);
9340
9341   /* It might be a const variable or enumeration constant.  */
9342   size = decl_constant_value (size);
9343
9344   /* The array bound must be an integer type.  */
9345   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9346       && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9347       && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9348     {
9349       if (name)
9350         error ("size of array `%D' has non-integer type", name);
9351       else
9352         error ("size of array has non-integer type");
9353       size = integer_one_node;
9354     }
9355
9356   /* Normally, the array-bound will be a constant.  */
9357   if (TREE_CODE (size) == INTEGER_CST)
9358     {
9359       /* Check to see if the array bound overflowed.  Make that an
9360          error, no matter how generous we're being.  */
9361       int old_flag_pedantic_errors = flag_pedantic_errors;
9362       int old_pedantic = pedantic;
9363       pedantic = flag_pedantic_errors = 1;
9364       constant_expression_warning (size);
9365       pedantic = old_pedantic;
9366       flag_pedantic_errors = old_flag_pedantic_errors;
9367
9368       /* An array must have a positive number of elements.  */
9369       if (INT_CST_LT (size, integer_zero_node))
9370         {
9371           if (name)
9372             error ("size of array `%D' is negative", name);
9373           else
9374             error ("size of array is negative");
9375           size = integer_one_node;
9376         }
9377       /* Except that an extension we allow zero-sized arrays.  We
9378          always allow them in system headers because glibc uses
9379          them.  */
9380       else if (integer_zerop (size) && pedantic && !in_system_header)
9381         {
9382           if (name)
9383             pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9384           else
9385             pedwarn ("ISO C++ forbids zero-size array");
9386         }
9387     }
9388   else if (TREE_CONSTANT (size))
9389     {
9390       /* `(int) &fn' is not a valid array bound.  */
9391       if (name)
9392         error ("size of array `%D' is not an integral constant-expression",
9393                   name);
9394       else
9395         error ("size of array is not an integral constant-expression");
9396     }
9397
9398   /* Compute the index of the largest element in the array.  It is
9399      one less than the number of elements in the array.  */
9400   itype
9401     = fold (cp_build_binary_op (MINUS_EXPR,
9402                                 cp_convert (ssizetype, size),
9403                                 cp_convert (ssizetype,
9404                                             integer_one_node)));
9405
9406   /* Check for variable-sized arrays.  We allow such things as an
9407      extension, even though they are not allowed in ANSI/ISO C++.  */
9408   if (!TREE_CONSTANT (itype))
9409     {
9410       if (pedantic)
9411         {
9412           if (name)
9413             pedwarn ("ISO C++ forbids variable-size array `%D'",
9414                         name);
9415           else
9416             pedwarn ("ISO C++ forbids variable-size array");
9417         }
9418
9419       /* Create a variable-sized array index type.  */
9420       itype = variable_size (itype);
9421     }
9422   /* Make sure that there was no overflow when creating to a signed
9423      index type.  (For example, on a 32-bit machine, an array with
9424      size 2^32 - 1 is too big.)  */
9425   else if (TREE_OVERFLOW (itype))
9426     {
9427       error ("overflow in array dimension");
9428       TREE_OVERFLOW (itype) = 0;
9429     }
9430
9431   /* Create and return the appropriate index type.  */
9432   return build_index_type (itype);
9433 }
9434
9435 /* Returns the scope (if any) in which the entity declared by
9436    DECLARATOR will be located.  If the entity was declared with an
9437    unqualified name, NULL_TREE is returned.  */
9438
9439 tree
9440 get_scope_of_declarator (tree declarator)
9441 {
9442   if (!declarator)
9443     return NULL_TREE;
9444   
9445   switch (TREE_CODE (declarator))
9446     {
9447     case CALL_EXPR:
9448     case ARRAY_REF:
9449     case INDIRECT_REF:
9450     case ADDR_EXPR:
9451       /* For any of these, the main declarator is the first operand.  */
9452       return get_scope_of_declarator (TREE_OPERAND
9453                                       (declarator, 0));
9454
9455     case SCOPE_REF:
9456       /* For a pointer-to-member, continue descending.  */
9457       if (TREE_CODE (TREE_OPERAND (declarator, 1))
9458           == INDIRECT_REF)
9459         return get_scope_of_declarator (TREE_OPERAND
9460                                         (declarator, 1));
9461       /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9462          which the declaration occurs is the first operand.  */
9463       return TREE_OPERAND (declarator, 0);
9464
9465     case TREE_LIST:
9466       /* Attributes to be applied. The declarator is TREE_VALUE.  */
9467       return get_scope_of_declarator (TREE_VALUE (declarator));
9468       
9469     default:
9470       /* Otherwise, we have a declarator-id which is not a qualified
9471          name; the entity will be declared in the current scope.  */
9472       return NULL_TREE;
9473     }
9474 }
9475
9476 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9477    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
9478    with this type.  */
9479
9480 static tree
9481 create_array_type_for_decl (tree name, tree type, tree size)
9482 {
9483   tree itype = NULL_TREE;
9484   const char* error_msg;
9485
9486   /* If things have already gone awry, bail now.  */
9487   if (type == error_mark_node || size == error_mark_node)
9488     return error_mark_node;
9489
9490   /* Assume that everything will go OK.  */
9491   error_msg = NULL;
9492
9493   /* There are some types which cannot be array elements.  */
9494   switch (TREE_CODE (type))
9495     {
9496     case VOID_TYPE:
9497       error_msg = "array of void";
9498       break;
9499
9500     case FUNCTION_TYPE:
9501       error_msg = "array of functions";
9502       break;
9503
9504     case REFERENCE_TYPE:
9505       error_msg = "array of references";
9506       break;
9507
9508     case OFFSET_TYPE:
9509       error_msg = "array of data members";
9510       break;
9511
9512     case METHOD_TYPE:
9513       error_msg = "array of function members";
9514       break;
9515
9516     default:
9517       break;
9518     }
9519
9520   /* If something went wrong, issue an error-message and return.  */
9521   if (error_msg)
9522     {
9523       if (name)
9524         error ("declaration of `%D' as %s", name, error_msg);
9525       else
9526         error ("creating %s", error_msg);
9527
9528       return error_mark_node;
9529     }
9530
9531   /* [dcl.array]
9532
9533      The constant expressions that specify the bounds of the arrays
9534      can be omitted only for the first member of the sequence.  */
9535   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9536     {
9537       if (name)
9538         error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9539                   name);
9540       else
9541         error ("multidimensional array must have bounds for all dimensions except the first");
9542
9543       return error_mark_node;
9544     }
9545
9546   /* Figure out the index type for the array.  */
9547   if (size)
9548     itype = compute_array_index_type (name, size);
9549
9550   return build_cplus_array_type (type, itype);
9551 }
9552
9553 /* Check that it's OK to declare a function with the indicated TYPE.
9554    SFK indicates the kind of special function (if any) that this
9555    function is.  OPTYPE is the type given in a conversion operator
9556    declaration.  Returns the actual return type of the function; that
9557    may be different than TYPE if an error occurs, or for certain
9558    special functions.  */
9559
9560 static tree
9561 check_special_function_return_type (special_function_kind sfk,
9562                                     tree type,
9563                                     tree optype)
9564 {
9565   switch (sfk)
9566     {
9567     case sfk_constructor:
9568       if (type)
9569         error ("return type specification for constructor invalid");
9570
9571       type = void_type_node;
9572       break;
9573
9574     case sfk_destructor:
9575       if (type)
9576         error ("return type specification for destructor invalid");
9577       type = void_type_node;
9578       break;
9579
9580     case sfk_conversion:
9581       if (type && !same_type_p (type, optype))
9582         error ("operator `%T' declared to return `%T'", optype, type);
9583       else if (type)
9584         pedwarn ("return type specified for `operator %T'",  optype);
9585       type = optype;
9586       break;
9587
9588     default:
9589       abort ();
9590       break;
9591     }
9592
9593   return type;
9594 }
9595
9596 /* Given declspecs and a declarator (abstract or otherwise), determine
9597    the name and type of the object declared and construct a DECL node
9598    for it.
9599
9600    DECLSPECS is a chain of tree_list nodes whose value fields
9601     are the storage classes and type specifiers.
9602
9603    DECL_CONTEXT says which syntactic context this declaration is in:
9604      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9605      FUNCDEF for a function definition.  Like NORMAL but a few different
9606       error messages in each case.  Return value may be zero meaning
9607       this definition is too screwy to try to parse.
9608      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
9609       handle member functions (which have FIELD context).
9610       Return value may be zero meaning this definition is too screwy to
9611       try to parse.
9612      PARM for a parameter declaration (either within a function prototype
9613       or before a function body).  Make a PARM_DECL, or return void_type_node.
9614      CATCHPARM for a parameter declaration before a catch clause.
9615      TYPENAME if for a typename (in a cast or sizeof).
9616       Don't make a DECL node; just return the ..._TYPE node.
9617      FIELD for a struct or union field; make a FIELD_DECL.
9618      BITFIELD for a field with specified width.
9619    INITIALIZED is 1 if the decl has an initializer.
9620
9621    ATTRLIST is a pointer to the list of attributes, which may be NULL
9622    if there are none; *ATTRLIST may be modified if attributes from inside
9623    the declarator should be applied to the declaration.
9624
9625    When this function is called, scoping variables (such as
9626    CURRENT_CLASS_TYPE) should reflect the scope in which the
9627    declaration occurs, not the scope in which the new declaration will
9628    be placed.  For example, on:
9629
9630      void S::f() { ... }
9631
9632    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9633    should not be `S'.  */
9634
9635 tree
9636 grokdeclarator (tree declarator,
9637                 tree declspecs,
9638                 enum decl_context decl_context,
9639                 int initialized,
9640                 tree* attrlist)
9641 {
9642   RID_BIT_TYPE specbits;
9643   int nclasses = 0;
9644   tree spec;
9645   tree type = NULL_TREE;
9646   int longlong = 0;
9647   int type_quals;
9648   int virtualp, explicitp, friendp, inlinep, staticp;
9649   int explicit_int = 0;
9650   int explicit_char = 0;
9651   int defaulted_int = 0;
9652   int extern_langp = 0;
9653   tree dependant_name = NULL_TREE;
9654   
9655   tree typedef_decl = NULL_TREE;
9656   const char *name;
9657   tree typedef_type = NULL_TREE;
9658   int funcdef_flag = 0;
9659   enum tree_code innermost_code = ERROR_MARK;
9660   int bitfield = 0;
9661 #if 0
9662   /* See the code below that used this.  */
9663   tree decl_attr = NULL_TREE;
9664 #endif
9665
9666   /* Keep track of what sort of function is being processed
9667      so that we can warn about default return values, or explicit
9668      return values which do not match prescribed defaults.  */
9669   special_function_kind sfk = sfk_none;
9670
9671   tree dname = NULL_TREE;
9672   tree ctype = current_class_type;
9673   tree ctor_return_type = NULL_TREE;
9674   enum overload_flags flags = NO_SPECIAL;
9675   tree quals = NULL_TREE;
9676   tree raises = NULL_TREE;
9677   int template_count = 0;
9678   tree in_namespace = NULL_TREE;
9679   tree returned_attrs = NULL_TREE;
9680   tree scope = NULL_TREE;
9681
9682   RIDBIT_RESET_ALL (specbits);
9683   if (decl_context == FUNCDEF)
9684     funcdef_flag = 1, decl_context = NORMAL;
9685   else if (decl_context == MEMFUNCDEF)
9686     funcdef_flag = -1, decl_context = FIELD;
9687   else if (decl_context == BITFIELD)
9688     bitfield = 1, decl_context = FIELD;
9689
9690   /* Look inside a declarator for the name being declared
9691      and get it as a string, for an error message.  */
9692   {
9693     tree *next = &declarator;
9694     register tree decl;
9695     name = NULL;
9696
9697     while (next && *next)
9698       {
9699         decl = *next;
9700         switch (TREE_CODE (decl))
9701           {
9702           case TREE_LIST:
9703             /* For attributes.  */
9704             next = &TREE_VALUE (decl);
9705             break;
9706
9707           case COND_EXPR:
9708             ctype = NULL_TREE;
9709             next = &TREE_OPERAND (decl, 0);
9710             break;
9711
9712           case BIT_NOT_EXPR:    /* For C++ destructors!  */
9713             {
9714               tree name = TREE_OPERAND (decl, 0);
9715               tree rename = NULL_TREE;
9716
9717               my_friendly_assert (flags == NO_SPECIAL, 152);
9718               flags = DTOR_FLAG;
9719               sfk = sfk_destructor;
9720               if (TYPE_P (name))
9721                 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9722               my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9723               if (ctype == NULL_TREE)
9724                 {
9725                   if (current_class_type == NULL_TREE)
9726                     {
9727                       error ("destructors must be member functions");
9728                       flags = NO_SPECIAL;
9729                     }
9730                   else
9731                     {
9732                       tree t = constructor_name (current_class_type);
9733                       if (t != name)
9734                         rename = t;
9735                     }
9736                 }
9737               else
9738                 {
9739                   tree t = constructor_name (ctype);
9740                   if (t != name)
9741                     rename = t;
9742                 }
9743
9744               if (rename)
9745                 {
9746                   error ("destructor `%T' must match class name `%T'",
9747                             name, rename);
9748                   TREE_OPERAND (decl, 0) = rename;
9749                 }
9750               next = &name;
9751             }
9752             break;
9753
9754           case ADDR_EXPR:       /* C++ reference declaration */
9755             /* Fall through.  */
9756           case ARRAY_REF:
9757           case INDIRECT_REF:
9758             ctype = NULL_TREE;
9759             innermost_code = TREE_CODE (decl);
9760             next = &TREE_OPERAND (decl, 0);
9761             break;
9762
9763           case CALL_EXPR:
9764             innermost_code = TREE_CODE (decl);
9765             if (decl_context == FIELD && ctype == NULL_TREE)
9766               ctype = current_class_type;
9767             if (ctype
9768                 && TREE_OPERAND (decl, 0)
9769                 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9770                     && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9771                                            ctype)))
9772               TREE_OPERAND (decl, 0) = constructor_name (ctype);
9773             next = &TREE_OPERAND (decl, 0);
9774             decl = *next;
9775             if (ctype != NULL_TREE
9776                 && decl != NULL_TREE && flags != DTOR_FLAG
9777                 && constructor_name_p (decl, ctype))
9778               {
9779                 sfk = sfk_constructor;
9780                 ctor_return_type = ctype;
9781               }
9782             ctype = NULL_TREE;
9783             break;
9784
9785           case TEMPLATE_ID_EXPR:
9786               {
9787                 tree fns = TREE_OPERAND (decl, 0);
9788
9789                 if (TREE_CODE (fns) == LOOKUP_EXPR)
9790                   fns = TREE_OPERAND (fns, 0);
9791
9792                 dname = fns;
9793                 if (TREE_CODE (dname) == COMPONENT_REF)
9794                   dname = TREE_OPERAND (dname, 1);
9795                 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9796                   {
9797                     my_friendly_assert (is_overloaded_fn (dname),
9798                                         19990331);
9799                     dname = DECL_NAME (get_first_fn (dname));
9800                   }
9801               }
9802           /* Fall through.  */
9803
9804           case IDENTIFIER_NODE:
9805             if (TREE_CODE (decl) == IDENTIFIER_NODE)
9806               dname = decl;
9807
9808             next = 0;
9809
9810             if (C_IS_RESERVED_WORD (dname))
9811               {
9812                 error ("declarator-id missing; using reserved word `%D'",
9813                           dname);
9814                 name = IDENTIFIER_POINTER (dname);
9815               }
9816             else if (!IDENTIFIER_TYPENAME_P (dname))
9817               name = IDENTIFIER_POINTER (dname);
9818             else
9819               {
9820                 my_friendly_assert (flags == NO_SPECIAL, 154);
9821                 flags = TYPENAME_FLAG;
9822                 ctor_return_type = TREE_TYPE (dname);
9823                 sfk = sfk_conversion;
9824                 if (IDENTIFIER_GLOBAL_VALUE (dname)
9825                     && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9826                         == TYPE_DECL))
9827                   name = IDENTIFIER_POINTER (dname);
9828                 else
9829                   name = "<invalid operator>";
9830               }
9831             break;
9832
9833             /* C++ extension */
9834           case SCOPE_REF:
9835             {
9836               /* Perform error checking, and decide on a ctype.  */
9837               tree cname = TREE_OPERAND (decl, 0);
9838               if (cname == NULL_TREE)
9839                 ctype = NULL_TREE;
9840               else if (TREE_CODE (cname) == NAMESPACE_DECL)
9841                 {
9842                   ctype = NULL_TREE;
9843                   in_namespace = TREE_OPERAND (decl, 0);
9844                 }
9845               else if (! is_aggr_type (cname, 1))
9846                 ctype = NULL_TREE;
9847               /* Must test TREE_OPERAND (decl, 1), in case user gives
9848                  us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9849               else if (TREE_OPERAND (decl, 1)
9850                        && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9851                 ctype = cname;
9852               else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9853                        || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9854                 {
9855                   /* This might be declaring a member of a template
9856                      parm to be a friend.  */
9857                   ctype = cname;
9858                   dependant_name = TREE_OPERAND (decl, 1);
9859                 }
9860               else if (ctype == NULL_TREE)
9861                 ctype = cname;
9862               else if (TREE_COMPLEXITY (decl) == current_class_depth)
9863                 ;
9864               else
9865                 {
9866                   if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9867                     {
9868                       error ("type `%T' is not derived from type `%T'",
9869                                 cname, ctype);
9870                       ctype = NULL_TREE;
9871                     }
9872                   else
9873                     ctype = cname;
9874                 }
9875
9876               /* It is valid to write:
9877
9878                    class C { void f(); };
9879                    typedef C D;
9880                    void D::f();
9881
9882                  The standard is not clear about whether `typedef const C D' is
9883                  legal; as of 2002-09-15 the committee is considering
9884                  that question.  EDG 3.0 allows that syntax.
9885                  Therefore, we do as well.  */
9886               if (ctype)
9887                 ctype = TYPE_MAIN_VARIANT (ctype);
9888               /* Update the declarator so that when we process it
9889                  again the correct type is present.  */
9890               TREE_OPERAND (decl, 0) = ctype;
9891
9892               if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9893                   && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9894                                          ctype))
9895                 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9896               next = &TREE_OPERAND (decl, 1);
9897               decl = *next;
9898               if (ctype)
9899                 {
9900                   if (TREE_CODE (decl) == IDENTIFIER_NODE
9901                       && constructor_name_p (decl, ctype))
9902                     {
9903                       sfk = sfk_constructor;
9904                       ctor_return_type = ctype;
9905                     }
9906                   else if (TREE_CODE (decl) == BIT_NOT_EXPR
9907                            && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9908                            && constructor_name_p (TREE_OPERAND (decl, 0),
9909                                                   ctype))
9910                     {
9911                       sfk = sfk_destructor;
9912                       ctor_return_type = ctype;
9913                       flags = DTOR_FLAG;
9914                       TREE_OPERAND (decl, 0) = constructor_name (ctype);
9915                       next = &TREE_OPERAND (decl, 0);
9916                     }
9917                 }
9918             }
9919             break;
9920
9921           case ERROR_MARK:
9922             next = 0;
9923             break;
9924
9925           case TYPE_DECL:
9926             /* Parse error puts this typespec where
9927                a declarator should go.  */
9928             error ("`%T' specified as declarator-id", DECL_NAME (decl));
9929             if (TREE_TYPE (decl) == current_class_type)
9930               error ("  perhaps you want `%T' for a constructor",
9931                         current_class_name);
9932             dname = DECL_NAME (decl);
9933             name = IDENTIFIER_POINTER (dname);
9934
9935             /* Avoid giving two errors for this.  */
9936             IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9937
9938             declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9939             *next = dname;
9940             next = 0;
9941             break;
9942
9943           case BASELINK:
9944             next = &BASELINK_FUNCTIONS (decl);
9945             break;
9946
9947           case TEMPLATE_DECL:
9948             /* Sometimes, we see a template-name used as part of a 
9949                decl-specifier like in 
9950                   std::allocator alloc;
9951                Handle that gracefully.  */
9952             error ("invalid use of template-name '%E' in a declarator", decl);
9953             return error_mark_node;
9954             break;
9955             
9956           default:
9957             my_friendly_assert (0, 20020917);
9958           }
9959       }
9960   }
9961
9962   /* A function definition's declarator must have the form of
9963      a function declarator.  */
9964
9965   if (funcdef_flag && innermost_code != CALL_EXPR)
9966     return 0;
9967
9968   if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9969       && innermost_code != CALL_EXPR
9970       && ! (ctype && declspecs == NULL_TREE))
9971     {
9972       error ("declaration of `%D' as non-function", dname);
9973       return void_type_node;
9974     }
9975
9976   /* Anything declared one level down from the top level
9977      must be one of the parameters of a function
9978      (because the body is at least two levels down).  */
9979
9980   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9981      by not allowing C++ class definitions to specify their parameters
9982      with xdecls (must be spec.d in the parmlist).
9983
9984      Since we now wait to push a class scope until we are sure that
9985      we are in a legitimate method context, we must set oldcname
9986      explicitly (since current_class_name is not yet alive).
9987
9988      We also want to avoid calling this a PARM if it is in a namespace.  */
9989
9990   if (decl_context == NORMAL && !toplevel_bindings_p ())
9991     {
9992       struct cp_binding_level *b = current_binding_level;
9993       current_binding_level = b->level_chain;
9994       if (current_binding_level != 0 && toplevel_bindings_p ())
9995         decl_context = PARM;
9996       current_binding_level = b;
9997     }
9998
9999   if (name == NULL)
10000     name = decl_context == PARM ? "parameter" : "type name";
10001
10002   /* Look through the decl specs and record which ones appear.
10003      Some typespecs are defined as built-in typenames.
10004      Others, the ones that are modifiers of other types,
10005      are represented by bits in SPECBITS: set the bits for
10006      the modifiers that appear.  Storage class keywords are also in SPECBITS.
10007
10008      If there is a typedef name or a type, store the type in TYPE.
10009      This includes builtin typedefs such as `int'.
10010
10011      Set EXPLICIT_INT if the type is `int' or `char' and did not
10012      come from a user typedef.
10013
10014      Set LONGLONG if `long' is mentioned twice.
10015
10016      For C++, constructors and destructors have their own fast treatment.  */
10017
10018   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10019     {
10020       register int i;
10021       register tree id;
10022
10023       /* Certain parse errors slip through.  For example,
10024          `int class;' is not caught by the parser. Try
10025          weakly to recover here.  */
10026       if (TREE_CODE (spec) != TREE_LIST)
10027         return 0;
10028
10029       id = TREE_VALUE (spec);
10030
10031       /* If the entire declaration is itself tagged as deprecated then
10032          suppress reports of deprecated items.  */
10033       if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10034         {
10035           if (deprecated_state != DEPRECATED_SUPPRESS)
10036             warn_deprecated_use (id);
10037         }
10038
10039       if (TREE_CODE (id) == IDENTIFIER_NODE)
10040         {
10041           if (id == ridpointers[(int) RID_INT]
10042               || id == ridpointers[(int) RID_CHAR]
10043               || id == ridpointers[(int) RID_BOOL]
10044               || id == ridpointers[(int) RID_WCHAR])
10045             {
10046               if (type)
10047                 {
10048                   if (id == ridpointers[(int) RID_BOOL])
10049                     error ("`bool' is now a keyword");
10050                   else
10051                     error ("extraneous `%T' ignored", id);
10052                 }
10053               else
10054                 {
10055                   if (id == ridpointers[(int) RID_INT])
10056                     explicit_int = 1;
10057                   else if (id == ridpointers[(int) RID_CHAR])
10058                     explicit_char = 1;
10059                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10060                 }
10061               goto found;
10062             }
10063           /* C++ aggregate types.  */
10064           if (IDENTIFIER_HAS_TYPE_VALUE (id))
10065             {
10066               if (type)
10067                 error ("multiple declarations `%T' and `%T'", type, id);
10068               else
10069                 type = IDENTIFIER_TYPE_VALUE (id);
10070               goto found;
10071             }
10072
10073           for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10074             {
10075               if (ridpointers[i] == id)
10076                 {
10077                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10078                     {
10079                       if (pedantic && ! in_system_header && warn_long_long)
10080                         pedwarn ("ISO C++ does not support `long long'");
10081                       if (longlong)
10082                         error ("`long long long' is too long for GCC");
10083                       else
10084                         longlong = 1;
10085                     }
10086                   else if (RIDBIT_SETP (i, specbits))
10087                     pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10088
10089                   /* Diagnose "__thread extern" or "__thread static".  */
10090                   if (RIDBIT_SETP (RID_THREAD, specbits))
10091                     {
10092                       if (i == (int)RID_EXTERN)
10093                         error ("`__thread' before `extern'");
10094                       else if (i == (int)RID_STATIC)
10095                         error ("`__thread' before `static'");
10096                     }
10097
10098                   if (i == (int)RID_EXTERN
10099                       && TREE_PURPOSE (spec) == error_mark_node)
10100                     /* This extern was part of a language linkage.  */
10101                     extern_langp = 1;
10102
10103                   RIDBIT_SET (i, specbits);
10104                   goto found;
10105                 }
10106             }
10107         }
10108       else if (TREE_CODE (id) == TYPE_DECL)
10109         {
10110           if (type)
10111             error ("multiple declarations `%T' and `%T'", type,
10112                       TREE_TYPE (id));
10113           else
10114             {
10115               type = TREE_TYPE (id);
10116               TREE_VALUE (spec) = type;
10117               typedef_decl = id;
10118             }
10119           goto found;
10120         }
10121       if (type)
10122         error ("two or more data types in declaration of `%s'", name);
10123       else if (TREE_CODE (id) == IDENTIFIER_NODE)
10124         {
10125           register tree t = lookup_name (id, 1);
10126           if (!t || TREE_CODE (t) != TYPE_DECL)
10127             error ("`%s' fails to be a typedef or built in type",
10128                    IDENTIFIER_POINTER (id));
10129           else
10130             {
10131               type = TREE_TYPE (t);
10132               typedef_decl = t;
10133             }
10134         }
10135       else if (id != error_mark_node)
10136         /* Can't change CLASS nodes into RECORD nodes here!  */
10137         type = id;
10138
10139     found: ;
10140     }
10141
10142 #if 0
10143   /* See the code below that used this.  */
10144   if (typedef_decl)
10145     decl_attr = DECL_ATTRIBUTES (typedef_decl);
10146 #endif
10147   typedef_type = type;
10148
10149   /* No type at all: default to `int', and set DEFAULTED_INT
10150      because it was not a user-defined typedef.  */
10151
10152   if (type == NULL_TREE
10153       && (RIDBIT_SETP (RID_SIGNED, specbits)
10154           || RIDBIT_SETP (RID_UNSIGNED, specbits)
10155           || RIDBIT_SETP (RID_LONG, specbits)
10156           || RIDBIT_SETP (RID_SHORT, specbits)))
10157     {
10158       /* These imply 'int'.  */
10159       type = integer_type_node;
10160       defaulted_int = 1;
10161     }
10162
10163   if (sfk != sfk_none)
10164     type = check_special_function_return_type (sfk, type,
10165                                                ctor_return_type);
10166   else if (type == NULL_TREE)
10167     {
10168       int is_main;
10169
10170       explicit_int = -1;
10171
10172       /* We handle `main' specially here, because 'main () { }' is so
10173          common.  With no options, it is allowed.  With -Wreturn-type,
10174          it is a warning.  It is only an error with -pedantic-errors.  */
10175       is_main = (funcdef_flag
10176                  && dname && MAIN_NAME_P (dname)
10177                  && ctype == NULL_TREE
10178                  && in_namespace == NULL_TREE
10179                  && current_namespace == global_namespace);
10180
10181       if (in_system_header || flag_ms_extensions)
10182         /* Allow it, sigh.  */;
10183       else if (pedantic || ! is_main)
10184         pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10185                     name);
10186       else if (warn_return_type)
10187         warning ("ISO C++ forbids declaration of `%s' with no type",
10188                     name);
10189
10190       type = integer_type_node;
10191     }
10192   
10193   ctype = NULL_TREE;
10194
10195   /* Now process the modifiers that were specified
10196      and check for invalid combinations.  */
10197
10198   /* Long double is a special combination.  */
10199
10200   if (RIDBIT_SETP (RID_LONG, specbits)
10201       && TYPE_MAIN_VARIANT (type) == double_type_node)
10202     {
10203       RIDBIT_RESET (RID_LONG, specbits);
10204       type = build_qualified_type (long_double_type_node,
10205                                    cp_type_quals (type));
10206     }
10207
10208   /* Check all other uses of type modifiers.  */
10209
10210   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10211       || RIDBIT_SETP (RID_SIGNED, specbits)
10212       || RIDBIT_SETP (RID_LONG, specbits)
10213       || RIDBIT_SETP (RID_SHORT, specbits))
10214     {
10215       int ok = 0;
10216
10217       if (TREE_CODE (type) == REAL_TYPE)
10218         error ("short, signed or unsigned invalid for `%s'", name);
10219       else if (TREE_CODE (type) != INTEGER_TYPE)
10220         error ("long, short, signed or unsigned invalid for `%s'", name);
10221       else if (RIDBIT_SETP (RID_LONG, specbits)
10222                && RIDBIT_SETP (RID_SHORT, specbits))
10223         error ("long and short specified together for `%s'", name);
10224       else if ((RIDBIT_SETP (RID_LONG, specbits)
10225                 || RIDBIT_SETP (RID_SHORT, specbits))
10226                && explicit_char)
10227         error ("long or short specified with char for `%s'", name);
10228       else if ((RIDBIT_SETP (RID_LONG, specbits)
10229                 || RIDBIT_SETP (RID_SHORT, specbits))
10230                && TREE_CODE (type) == REAL_TYPE)
10231         error ("long or short specified with floating type for `%s'", name);
10232       else if (RIDBIT_SETP (RID_SIGNED, specbits)
10233                && RIDBIT_SETP (RID_UNSIGNED, specbits))
10234         error ("signed and unsigned given together for `%s'", name);
10235       else
10236         {
10237           ok = 1;
10238           if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10239             {
10240               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10241                        name);
10242               if (flag_pedantic_errors)
10243                 ok = 0;
10244             }
10245         }
10246
10247       /* Discard the type modifiers if they are invalid.  */
10248       if (! ok)
10249         {
10250           RIDBIT_RESET (RID_UNSIGNED, specbits);
10251           RIDBIT_RESET (RID_SIGNED, specbits);
10252           RIDBIT_RESET (RID_LONG, specbits);
10253           RIDBIT_RESET (RID_SHORT, specbits);
10254           longlong = 0;
10255         }
10256     }
10257
10258   if (RIDBIT_SETP (RID_COMPLEX, specbits)
10259       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10260     {
10261       error ("complex invalid for `%s'", name);
10262       RIDBIT_RESET (RID_COMPLEX, specbits);
10263     }
10264
10265   /* Decide whether an integer type is signed or not.
10266      Optionally treat bitfields as signed by default.  */
10267   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10268       /* [class.bit]
10269
10270          It is implementation-defined whether a plain (neither
10271          explicitly signed or unsigned) char, short, int, or long
10272          bit-field is signed or unsigned.
10273
10274          Naturally, we extend this to long long as well.  Note that
10275          this does not include wchar_t.  */
10276       || (bitfield && !flag_signed_bitfields
10277           && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10278           /* A typedef for plain `int' without `signed' can be
10279              controlled just like plain `int', but a typedef for
10280              `signed int' cannot be so controlled.  */
10281           && !(typedef_decl
10282                && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10283           && (TREE_CODE (type) == INTEGER_TYPE
10284               || TREE_CODE (type) == CHAR_TYPE)
10285           && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10286     {
10287       if (longlong)
10288         type = long_long_unsigned_type_node;
10289       else if (RIDBIT_SETP (RID_LONG, specbits))
10290         type = long_unsigned_type_node;
10291       else if (RIDBIT_SETP (RID_SHORT, specbits))
10292         type = short_unsigned_type_node;
10293       else if (type == char_type_node)
10294         type = unsigned_char_type_node;
10295       else if (typedef_decl)
10296         type = c_common_unsigned_type (type);
10297       else
10298         type = unsigned_type_node;
10299     }
10300   else if (RIDBIT_SETP (RID_SIGNED, specbits)
10301            && type == char_type_node)
10302     type = signed_char_type_node;
10303   else if (longlong)
10304     type = long_long_integer_type_node;
10305   else if (RIDBIT_SETP (RID_LONG, specbits))
10306     type = long_integer_type_node;
10307   else if (RIDBIT_SETP (RID_SHORT, specbits))
10308     type = short_integer_type_node;
10309
10310   if (RIDBIT_SETP (RID_COMPLEX, specbits))
10311     {
10312       /* If we just have "complex", it is equivalent to
10313          "complex double", but if any modifiers at all are specified it is
10314          the complex form of TYPE.  E.g, "complex short" is
10315          "complex short int".  */
10316
10317       if (defaulted_int && ! longlong
10318           && ! (RIDBIT_SETP (RID_LONG, specbits)
10319                 || RIDBIT_SETP (RID_SHORT, specbits)
10320                 || RIDBIT_SETP (RID_SIGNED, specbits)
10321                 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10322         type = complex_double_type_node;
10323       else if (type == integer_type_node)
10324         type = complex_integer_type_node;
10325       else if (type == float_type_node)
10326         type = complex_float_type_node;
10327       else if (type == double_type_node)
10328         type = complex_double_type_node;
10329       else if (type == long_double_type_node)
10330         type = complex_long_double_type_node;
10331       else
10332         type = build_complex_type (type);
10333     }
10334
10335   type_quals = TYPE_UNQUALIFIED;
10336   if (RIDBIT_SETP (RID_CONST, specbits))
10337     type_quals |= TYPE_QUAL_CONST;
10338   if (RIDBIT_SETP (RID_VOLATILE, specbits))
10339     type_quals |= TYPE_QUAL_VOLATILE;
10340   if (RIDBIT_SETP (RID_RESTRICT, specbits))
10341     type_quals |= TYPE_QUAL_RESTRICT;
10342   if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10343     error ("qualifiers are not allowed on declaration of `operator %T'",
10344               ctor_return_type);
10345
10346   type_quals |= cp_type_quals (type);
10347   type = cp_build_qualified_type_real
10348     (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10349                          ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10350   /* We might have ignored or rejected some of the qualifiers.  */
10351   type_quals = cp_type_quals (type);
10352   
10353   staticp = 0;
10354   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10355   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10356   RIDBIT_RESET (RID_VIRTUAL, specbits);
10357   explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10358   RIDBIT_RESET (RID_EXPLICIT, specbits);
10359
10360   if (RIDBIT_SETP (RID_STATIC, specbits))
10361     staticp = 1 + (decl_context == FIELD);
10362
10363   if (virtualp && staticp == 2)
10364     {
10365       error ("member `%D' cannot be declared both virtual and static",
10366                 dname);
10367       staticp = 0;
10368     }
10369   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10370   RIDBIT_RESET (RID_FRIEND, specbits);
10371
10372   if (dependant_name && !friendp)
10373     {
10374       error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10375       return void_type_node;
10376     }
10377   
10378   /* Warn if two storage classes are given. Default to `auto'.  */
10379
10380   if (RIDBIT_ANY_SET (specbits))
10381     {
10382       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10383       if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10384       if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10385       if (decl_context == PARM && nclasses > 0)
10386         error ("storage class specifiers invalid in parameter declarations");
10387       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10388         {
10389           if (decl_context == PARM)
10390             error ("typedef declaration invalid in parameter declaration");
10391           nclasses++;
10392         }
10393       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10394       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10395       if (!nclasses && !friendp && extern_langp)
10396         nclasses++;
10397     }
10398
10399   /* Give error if `virtual' is used outside of class declaration.  */
10400   if (virtualp
10401       && (current_class_name == NULL_TREE || decl_context != FIELD))
10402     {
10403       error ("virtual outside class declaration");
10404       virtualp = 0;
10405     }
10406
10407   /* Static anonymous unions are dealt with here.  */
10408   if (staticp && decl_context == TYPENAME
10409       && TREE_CODE (declspecs) == TREE_LIST
10410       && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10411     decl_context = FIELD;
10412
10413   /* Warn about storage classes that are invalid for certain
10414      kinds of declarations (parameters, typenames, etc.).  */
10415
10416   /* "static __thread" and "extern __thread" are allowed.  */
10417   if (nclasses == 2
10418       && RIDBIT_SETP (RID_THREAD, specbits)
10419       && (RIDBIT_SETP (RID_EXTERN, specbits)
10420           || RIDBIT_SETP (RID_STATIC, specbits)))
10421     nclasses = 1;
10422     
10423   if (nclasses > 1)
10424     error ("multiple storage classes in declaration of `%s'", name);
10425   else if (decl_context != NORMAL && nclasses > 0)
10426     {
10427       if ((decl_context == PARM || decl_context == CATCHPARM)
10428           && (RIDBIT_SETP (RID_REGISTER, specbits)
10429               || RIDBIT_SETP (RID_AUTO, specbits)))
10430         ;
10431       else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10432         ;
10433       else if (decl_context == FIELD
10434                /* C++ allows static class elements  */
10435                && RIDBIT_SETP (RID_STATIC, specbits))
10436         /* C++ also allows inlines and signed and unsigned elements,
10437            but in those cases we don't come in here.  */
10438         ;
10439       else
10440         {
10441           if (decl_context == FIELD)
10442             {
10443               tree tmp = NULL_TREE;
10444               register int op = 0;
10445
10446               if (declarator)
10447                 {
10448                   /* Avoid trying to get an operand off an identifier node.  */
10449                   if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10450                     tmp = declarator;
10451                   else
10452                     tmp = TREE_OPERAND (declarator, 0);
10453                   op = IDENTIFIER_OPNAME_P (tmp);
10454                   if (IDENTIFIER_TYPENAME_P (tmp))
10455                     {
10456                       if (IDENTIFIER_GLOBAL_VALUE (tmp)
10457                           && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10458                               == TYPE_DECL))
10459                         name = IDENTIFIER_POINTER (tmp);
10460                       else
10461                         name = "<invalid operator>";
10462                     }
10463                 }
10464               error ("storage class specified for %s `%s'",
10465                      op ? "member operator" : "field",
10466                      name);
10467             }
10468           else
10469             {
10470               if (decl_context == PARM || decl_context == CATCHPARM)
10471                 error ("storage class specified for parameter `%s'", name);
10472               else
10473                 error ("storage class specified for typename");
10474             }
10475           RIDBIT_RESET (RID_REGISTER, specbits);
10476           RIDBIT_RESET (RID_AUTO, specbits);
10477           RIDBIT_RESET (RID_EXTERN, specbits);
10478           RIDBIT_RESET (RID_THREAD, specbits);
10479         }
10480     }
10481   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10482     {
10483       if (toplevel_bindings_p ())
10484         {
10485           /* It's common practice (and completely valid) to have a const
10486              be initialized and declared extern.  */
10487           if (!(type_quals & TYPE_QUAL_CONST))
10488             warning ("`%s' initialized and declared `extern'", name);
10489         }
10490       else
10491         error ("`%s' has both `extern' and initializer", name);
10492     }
10493   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10494            && ! toplevel_bindings_p ())
10495     error ("nested function `%s' declared `extern'", name);
10496   else if (toplevel_bindings_p ())
10497     {
10498       if (RIDBIT_SETP (RID_AUTO, specbits))
10499         error ("top-level declaration of `%s' specifies `auto'", name);
10500     }
10501   else if (RIDBIT_SETP (RID_THREAD, specbits)
10502            && !RIDBIT_SETP (RID_EXTERN, specbits)
10503            && !RIDBIT_SETP (RID_STATIC, specbits))
10504     {
10505       error ("function-scope `%s' implicitly auto and declared `__thread'",
10506              name);
10507       RIDBIT_RESET (RID_THREAD, specbits);
10508     }
10509
10510   if (nclasses > 0 && friendp)
10511     error ("storage class specifiers invalid in friend function declarations");
10512
10513   scope = get_scope_of_declarator (declarator);
10514
10515   /* Now figure out the structure of the declarator proper.
10516      Descend through it, creating more complex types, until we reach
10517      the declared identifier (or NULL_TREE, in an abstract declarator).  */
10518
10519   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10520          && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10521     {
10522       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10523          an INDIRECT_REF (for *...),
10524          a CALL_EXPR (for ...(...)),
10525          an identifier (for the name being declared)
10526          or a null pointer (for the place in an absolute declarator
10527          where the name was omitted).
10528          For the last two cases, we have just exited the loop.
10529
10530          For C++ it could also be
10531          a SCOPE_REF (for class :: ...).  In this case, we have converted
10532          sensible names to types, and those are the values we use to
10533          qualify the member name.
10534          an ADDR_EXPR (for &...),
10535          a BIT_NOT_EXPR (for destructors)
10536
10537          At this point, TYPE is the type of elements of an array,
10538          or for a function to return, or for a pointer to point to.
10539          After this sequence of ifs, TYPE is the type of the
10540          array or function or pointer, and DECLARATOR has had its
10541          outermost layer removed.  */
10542
10543       if (type == error_mark_node)
10544         {
10545           if (TREE_CODE (declarator) == SCOPE_REF)
10546             declarator = TREE_OPERAND (declarator, 1);
10547           else
10548             declarator = TREE_OPERAND (declarator, 0);
10549           continue;
10550         }
10551       if (quals != NULL_TREE
10552           && (declarator == NULL_TREE
10553               || TREE_CODE (declarator) != SCOPE_REF))
10554         {
10555           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10556             ctype = TYPE_METHOD_BASETYPE (type);
10557           if (ctype != NULL_TREE)
10558             {
10559               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10560               grok_method_quals (ctype, dummy, quals);
10561               type = TREE_TYPE (dummy);
10562               quals = NULL_TREE;
10563             }
10564         }
10565
10566       switch (TREE_CODE (declarator))
10567         {
10568         case TREE_LIST:
10569           {
10570             /* We encode a declarator with embedded attributes using
10571                a TREE_LIST.  */
10572             tree attrs = TREE_PURPOSE (declarator);
10573             tree inner_decl;
10574             int attr_flags;
10575
10576             declarator = TREE_VALUE (declarator);
10577             inner_decl = declarator;
10578             while (inner_decl != NULL_TREE
10579                    && TREE_CODE (inner_decl) == TREE_LIST)
10580               inner_decl = TREE_VALUE (inner_decl);
10581             attr_flags = 0;
10582             if (inner_decl == NULL_TREE
10583                 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10584               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10585             if (TREE_CODE (inner_decl) == CALL_EXPR)
10586               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10587             if (TREE_CODE (inner_decl) == ARRAY_REF)
10588               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10589             returned_attrs = decl_attributes (&type,
10590                                               chainon (returned_attrs, attrs),
10591                                               attr_flags);
10592           }
10593           break;
10594
10595         case ARRAY_REF:
10596           {
10597             register tree size;
10598
10599             size = TREE_OPERAND (declarator, 1);
10600
10601             /* VC++ spells a zero-sized array with [].  */
10602             if (size == NULL_TREE && decl_context == FIELD && ! staticp
10603                 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10604               size = integer_zero_node;
10605
10606             declarator = TREE_OPERAND (declarator, 0);
10607
10608             type = create_array_type_for_decl (dname, type, size);
10609
10610             ctype = NULL_TREE;
10611           }
10612           break;
10613
10614         case CALL_EXPR:
10615           {
10616             tree arg_types;
10617             int funcdecl_p;
10618             tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10619             tree inner_decl = TREE_OPERAND (declarator, 0);
10620
10621             /* Declaring a function type.
10622                Make sure we have a valid type for the function to return.  */
10623
10624             /* We now know that the TYPE_QUALS don't apply to the
10625                decl, but to its return type.  */
10626             type_quals = TYPE_UNQUALIFIED;
10627
10628             /* Warn about some types functions can't return.  */
10629
10630             if (TREE_CODE (type) == FUNCTION_TYPE)
10631               {
10632                 error ("`%s' declared as function returning a function", name);
10633                 type = integer_type_node;
10634               }
10635             if (TREE_CODE (type) == ARRAY_TYPE)
10636               {
10637                 error ("`%s' declared as function returning an array", name);
10638                 type = integer_type_node;
10639               }
10640
10641             if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10642               inner_decl = TREE_OPERAND (inner_decl, 1);
10643
10644             if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10645               inner_decl = dname;
10646
10647             /* Pick up type qualifiers which should be applied to `this'.  */
10648             quals = CALL_DECLARATOR_QUALS (declarator);
10649
10650             /* Pick up the exception specifications.  */
10651             raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10652
10653             /* Say it's a definition only for the CALL_EXPR
10654                closest to the identifier.  */
10655             funcdecl_p
10656               = inner_decl
10657               && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10658                   || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10659                   || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10660
10661             if (ctype == NULL_TREE
10662                 && decl_context == FIELD
10663                 && funcdecl_p
10664                 && (friendp == 0 || dname == current_class_name))
10665               ctype = current_class_type;
10666
10667             if (ctype && sfk == sfk_conversion)
10668               TYPE_HAS_CONVERSION (ctype) = 1;
10669             if (ctype && constructor_name_p (dname, ctype))
10670               {
10671                 /* We are within a class's scope. If our declarator name
10672                    is the same as the class name, and we are defining
10673                    a function, then it is a constructor/destructor, and
10674                    therefore returns a void type.  */
10675
10676                 if (flags == DTOR_FLAG)
10677                   {
10678                     /* ISO C++ 12.4/2.  A destructor may not be
10679                        declared const or volatile.  A destructor may
10680                        not be static.  */
10681                     if (staticp == 2)
10682                       error ("destructor cannot be static member function");
10683                     if (quals)
10684                       {
10685                         error ("destructors may not be `%s'",
10686                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10687                         quals = NULL_TREE;
10688                       }
10689                     if (decl_context == FIELD)
10690                       {
10691                         if (! member_function_or_else (ctype,
10692                                                        current_class_type,
10693                                                        flags))
10694                           return void_type_node;
10695                       }
10696                   }
10697                 else            /* It's a constructor.  */
10698                   {
10699                     if (explicitp == 1)
10700                       explicitp = 2;
10701                     /* ISO C++ 12.1.  A constructor may not be
10702                        declared const or volatile.  A constructor may
10703                        not be virtual.  A constructor may not be
10704                        static.  */
10705                     if (staticp == 2)
10706                       error ("constructor cannot be static member function");
10707                     if (virtualp)
10708                       {
10709                         pedwarn ("constructors cannot be declared virtual");
10710                         virtualp = 0;
10711                       }
10712                     if (quals)
10713                       {
10714                         error ("constructors may not be `%s'",
10715                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10716                         quals = NULL_TREE;
10717                       }
10718                     {
10719                       RID_BIT_TYPE tmp_bits;
10720                       memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10721                       RIDBIT_RESET (RID_INLINE, tmp_bits);
10722                       RIDBIT_RESET (RID_STATIC, tmp_bits);
10723                       if (RIDBIT_ANY_SET (tmp_bits))
10724                         error ("return value type specifier for constructor ignored");
10725                     }
10726                     if (decl_context == FIELD)
10727                       {
10728                         if (! member_function_or_else (ctype,
10729                                                        current_class_type,
10730                                                        flags))
10731                           return void_type_node;
10732                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10733                         if (sfk != sfk_constructor)
10734                           return NULL_TREE;
10735                       }
10736                   }
10737                 if (decl_context == FIELD)
10738                   staticp = 0;
10739               }
10740             else if (friendp)
10741               {
10742                 if (initialized)
10743                   error ("can't initialize friend function `%s'", name);
10744                 if (virtualp)
10745                   {
10746                     /* Cannot be both friend and virtual.  */
10747                     error ("virtual functions cannot be friends");
10748                     RIDBIT_RESET (RID_FRIEND, specbits);
10749                     friendp = 0;
10750                   }
10751                 if (decl_context == NORMAL)
10752                   error ("friend declaration not in class definition");
10753                 if (current_function_decl && funcdef_flag)
10754                   error ("can't define friend function `%s' in a local class definition",
10755                             name);
10756               }
10757
10758             /* Construct the function type and go to the next
10759                inner layer of declarator.  */
10760
10761             declarator = TREE_OPERAND (declarator, 0);
10762
10763             /* FIXME: This is where default args should be fully
10764                processed.  */
10765
10766             arg_types = grokparms (inner_parms);
10767
10768             if (declarator && flags == DTOR_FLAG)
10769               {
10770                 /* A destructor declared in the body of a class will
10771                    be represented as a BIT_NOT_EXPR.  But, we just
10772                    want the underlying IDENTIFIER.  */
10773                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10774                   declarator = TREE_OPERAND (declarator, 0);
10775
10776                 if (arg_types != void_list_node)
10777                   {
10778                     error ("destructors may not have parameters");
10779                     arg_types = void_list_node;
10780                     last_function_parms = NULL_TREE;
10781                   }
10782               }
10783
10784             /* ANSI says that `const int foo ();'
10785                does not make the function foo const.  */
10786             type = build_function_type (type, arg_types);
10787           }
10788           break;
10789
10790         case ADDR_EXPR:
10791         case INDIRECT_REF:
10792           /* Filter out pointers-to-references and references-to-references.
10793              We can get these if a TYPE_DECL is used.  */
10794
10795           if (TREE_CODE (type) == REFERENCE_TYPE)
10796             {
10797               error (TREE_CODE (declarator) == ADDR_EXPR
10798                      ? "cannot declare reference to `%#T'"
10799                      : "cannot declare pointer to `%#T'", type);
10800               type = TREE_TYPE (type);
10801             }
10802           else if (VOID_TYPE_P (type)
10803                    && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10804             error (ctype ? "cannot declare pointer to `%#T' member"
10805                      : "cannot declare reference to `%#T'", type);
10806
10807           /* Merge any constancy or volatility into the target type
10808              for the pointer.  */
10809
10810           /* We now know that the TYPE_QUALS don't apply to the decl,
10811              but to the target of the pointer.  */
10812           type_quals = TYPE_UNQUALIFIED;
10813
10814           if (TREE_CODE (declarator) == ADDR_EXPR)
10815             {
10816               if (!VOID_TYPE_P (type))
10817                 type = build_reference_type (type);
10818             }
10819           else if (TREE_CODE (type) == METHOD_TYPE)
10820             type = build_ptrmemfunc_type (build_pointer_type (type));
10821           else if (ctype)
10822             type = build_ptrmem_type (ctype, type);
10823           else
10824             type = build_pointer_type (type);
10825
10826           /* Process a list of type modifier keywords (such as
10827              const or volatile) that were given inside the `*' or `&'.  */
10828
10829           if (TREE_TYPE (declarator))
10830             {
10831               register tree typemodlist;
10832               int erred = 0;
10833               int constp = 0;
10834               int volatilep = 0;
10835               int restrictp = 0;
10836               
10837               for (typemodlist = TREE_TYPE (declarator); typemodlist;
10838                    typemodlist = TREE_CHAIN (typemodlist))
10839                 {
10840                   tree qualifier = TREE_VALUE (typemodlist);
10841
10842                   if (qualifier == ridpointers[(int) RID_CONST])
10843                     {
10844                       constp++;
10845                       type_quals |= TYPE_QUAL_CONST;
10846                     }
10847                   else if (qualifier == ridpointers[(int) RID_VOLATILE])
10848                     {
10849                       volatilep++;
10850                       type_quals |= TYPE_QUAL_VOLATILE;
10851                     }
10852                   else if (qualifier == ridpointers[(int) RID_RESTRICT])
10853                     {
10854                       restrictp++;
10855                       type_quals |= TYPE_QUAL_RESTRICT;
10856                     }
10857                   else if (!erred)
10858                     {
10859                       erred = 1;
10860                       error ("invalid type modifier within pointer declarator");
10861                     }
10862                 }
10863               if (constp > 1)
10864                 pedwarn ("duplicate `const'");
10865               if (volatilep > 1)
10866                 pedwarn ("duplicate `volatile'");
10867               if (restrictp > 1)
10868                 pedwarn ("duplicate `restrict'");
10869               type = cp_build_qualified_type (type, type_quals);
10870               type_quals = cp_type_quals (type);
10871             }
10872           declarator = TREE_OPERAND (declarator, 0);
10873           ctype = NULL_TREE;
10874           break;
10875
10876         case SCOPE_REF:
10877           {
10878             /* We have converted type names to NULL_TREE if the
10879                name was bogus, or to a _TYPE node, if not.
10880
10881                The variable CTYPE holds the type we will ultimately
10882                resolve to.  The code here just needs to build
10883                up appropriate member types.  */
10884             tree sname = TREE_OPERAND (declarator, 1);
10885             tree t;
10886
10887             /* Destructors can have their visibilities changed as well.  */
10888             if (TREE_CODE (sname) == BIT_NOT_EXPR)
10889               sname = TREE_OPERAND (sname, 0);
10890
10891             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10892               {
10893                 /* We had a reference to a global decl, or
10894                    perhaps we were given a non-aggregate typedef,
10895                    in which case we cleared this out, and should just
10896                    keep going as though it wasn't there.  */
10897                 declarator = sname;
10898                 continue;
10899               }
10900             ctype = TREE_OPERAND (declarator, 0);
10901
10902             t = ctype;
10903             while (t != NULL_TREE && CLASS_TYPE_P (t))
10904               {
10905                 /* You're supposed to have one `template <...>'
10906                    for every template class, but you don't need one
10907                    for a full specialization.  For example:
10908
10909                      template <class T> struct S{};
10910                      template <> struct S<int> { void f(); };
10911                      void S<int>::f () {}
10912
10913                    is correct; there shouldn't be a `template <>' for
10914                    the definition of `S<int>::f'.  */
10915                 if (CLASSTYPE_TEMPLATE_INFO (t)
10916                     && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10917                         || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10918                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10919                   template_count += 1;
10920
10921                 t = TYPE_MAIN_DECL (t);
10922                 t = DECL_CONTEXT (t);
10923               }
10924
10925             if (sname == NULL_TREE)
10926               goto done_scoping;
10927
10928             if (TREE_CODE (sname) == IDENTIFIER_NODE)
10929               {
10930                 /* This is the `standard' use of the scoping operator:
10931                    basetype :: member .  */
10932
10933                 if (ctype == current_class_type)
10934                   {
10935                     /* class A {
10936                          void A::f ();
10937                        };
10938
10939                        Is this ill-formed?  */
10940
10941                     if (pedantic)
10942                       pedwarn ("extra qualification `%T::' on member `%s' ignored",
10943                                   ctype, name);
10944                   }
10945                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10946                   {
10947                     if (current_class_type == NULL_TREE || friendp)
10948                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
10949                                                       TYPE_ARG_TYPES (type));
10950                     else
10951                       {
10952                         error ("cannot declare member function `%T::%s' within `%T'",
10953                                   ctype, name, current_class_type);
10954                         return error_mark_node;
10955                       }
10956                   }
10957                 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10958                          || COMPLETE_TYPE_P (complete_type (ctype)))
10959                   {
10960                     /* Have to move this code elsewhere in this function.
10961                        this code is used for i.e., typedef int A::M; M *pm;
10962
10963                        It is?  How? jason 10/2/94 */
10964
10965                     if (current_class_type)
10966                       {
10967                         error ("cannot declare member `%T::%s' within `%T'",
10968                                   ctype, name, current_class_type);
10969                         return void_type_node;
10970                       }
10971                   }
10972                 else
10973                   {
10974                     cxx_incomplete_type_error (NULL_TREE, ctype);
10975                     return error_mark_node;
10976                   }
10977
10978                 declarator = sname;
10979               }
10980             else if (TREE_CODE (sname) == SCOPE_REF)
10981               abort ();
10982             else
10983               {
10984               done_scoping:
10985                 declarator = TREE_OPERAND (declarator, 1);
10986                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10987                   /* In this case, we will deal with it later.  */
10988                   ;
10989                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10990                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
10991                                                   TYPE_ARG_TYPES (type));
10992               }
10993           }
10994           break;
10995
10996         case BIT_NOT_EXPR:
10997           declarator = TREE_OPERAND (declarator, 0);
10998           break;
10999
11000         case BASELINK:
11001           declarator = BASELINK_FUNCTIONS (declarator);
11002           break;
11003
11004         case RECORD_TYPE:
11005         case UNION_TYPE:
11006         case ENUMERAL_TYPE:
11007           declarator = NULL_TREE;
11008           break;
11009
11010         case ERROR_MARK:
11011           declarator = NULL_TREE;
11012           break;
11013
11014         default:
11015           abort ();
11016         }
11017     }
11018
11019   if (returned_attrs)
11020     {
11021       if (attrlist)
11022         *attrlist = chainon (returned_attrs, *attrlist);
11023       else
11024         attrlist = &returned_attrs;
11025     }
11026
11027   /* Now TYPE has the actual type.  */
11028
11029   /* Did array size calculations overflow?  */
11030
11031   if (TREE_CODE (type) == ARRAY_TYPE
11032       && COMPLETE_TYPE_P (type)
11033       && TREE_OVERFLOW (TYPE_SIZE (type)))
11034     {
11035       error ("size of array `%s' is too large", name);
11036       /* If we proceed with the array type as it is, we'll eventually
11037          crash in tree_low_cst().  */
11038       type = error_mark_node;
11039     }
11040
11041   if ((decl_context == FIELD || decl_context == PARM)
11042       && !processing_template_decl 
11043       && variably_modified_type_p (type))
11044     {
11045       if (decl_context == FIELD)
11046         error ("data member may not have variably modified type `%T'", type);
11047       else
11048         error ("parameter may not have variably modified type `%T'", type);
11049       type = error_mark_node;
11050     }
11051
11052   if (explicitp == 1 || (explicitp && friendp))
11053     {
11054       /* [dcl.fct.spec] The explicit specifier shall only be used in
11055          declarations of constructors within a class definition.  */
11056       error ("only declarations of constructors can be `explicit'");
11057       explicitp = 0;
11058     }
11059
11060   if (RIDBIT_SETP (RID_MUTABLE, specbits))
11061     {
11062       if (decl_context != FIELD || friendp)
11063         {
11064           error ("non-member `%s' cannot be declared `mutable'", name);
11065           RIDBIT_RESET (RID_MUTABLE, specbits);
11066         }
11067       else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11068         {
11069           error ("non-object member `%s' cannot be declared `mutable'", name);
11070           RIDBIT_RESET (RID_MUTABLE, specbits);
11071         }
11072       else if (TREE_CODE (type) == FUNCTION_TYPE
11073                || TREE_CODE (type) == METHOD_TYPE)
11074         {
11075           error ("function `%s' cannot be declared `mutable'", name);
11076           RIDBIT_RESET (RID_MUTABLE, specbits);
11077         }
11078       else if (staticp)
11079         {
11080           error ("static `%s' cannot be declared `mutable'", name);
11081           RIDBIT_RESET (RID_MUTABLE, specbits);
11082         }
11083       else if (type_quals & TYPE_QUAL_CONST)
11084         {
11085           error ("const `%s' cannot be declared `mutable'", name);
11086           RIDBIT_RESET (RID_MUTABLE, specbits);
11087         }
11088     }
11089
11090   if (declarator == NULL_TREE
11091       || TREE_CODE (declarator) == IDENTIFIER_NODE
11092       || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11093           && (TREE_CODE (type) == FUNCTION_TYPE
11094               || TREE_CODE (type) == METHOD_TYPE)))
11095     /* OK */;
11096   else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11097     {
11098       error ("template-id `%D' used as a declarator", declarator);
11099       declarator = dname;
11100     }
11101   else
11102     /* Unexpected declarator format.  */
11103     abort ();
11104
11105   /* If this is declaring a typedef name, return a TYPE_DECL.  */
11106
11107   if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11108     {
11109       tree decl;
11110
11111       /* Note that the grammar rejects storage classes
11112          in typenames, fields or parameters.  */
11113       if (current_lang_name == lang_name_java)
11114         TYPE_FOR_JAVA (type) = 1;
11115
11116       if (decl_context == FIELD)
11117         {
11118           if (constructor_name_p (declarator, current_class_type))
11119             pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11120                         declarator);
11121           decl = build_lang_decl (TYPE_DECL, declarator, type);
11122         }
11123       else
11124         {
11125           decl = build_decl (TYPE_DECL, declarator, type);
11126           if (in_namespace || ctype)
11127             cp_error_at ("typedef name may not be a nested-name-specifier",
11128                          decl);
11129           if (!current_function_decl)
11130             DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11131         }
11132       
11133       /* If the user declares "typedef struct {...} foo" then the
11134          struct will have an anonymous name.  Fill that name in now.
11135          Nothing can refer to it, so nothing needs know about the name
11136          change.  */
11137       if (type != error_mark_node
11138           && declarator
11139           && TYPE_NAME (type)
11140           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11141           && TYPE_ANONYMOUS_P (type)
11142           /* Don't do this if there are attributes.  */
11143           && (!attrlist || !*attrlist)
11144           && cp_type_quals (type) == TYPE_UNQUALIFIED)
11145         {
11146           tree oldname = TYPE_NAME (type);
11147           tree t;
11148
11149           /* Replace the anonymous name with the real name everywhere.  */
11150           lookup_tag_reverse (type, declarator);
11151           for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11152             if (TYPE_NAME (t) == oldname)
11153               TYPE_NAME (t) = decl;
11154
11155           if (TYPE_LANG_SPECIFIC (type))
11156             TYPE_WAS_ANONYMOUS (type) = 1;
11157
11158           /* If this is a typedef within a template class, the nested
11159              type is a (non-primary) template.  The name for the
11160              template needs updating as well.  */
11161           if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11162             DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11163               = TYPE_IDENTIFIER (type);
11164
11165           /* FIXME remangle member functions; member functions of a
11166              type with external linkage have external linkage.  */
11167         }
11168
11169       if (quals)
11170         {
11171           if (ctype == NULL_TREE)
11172             {
11173               if (TREE_CODE (type) != METHOD_TYPE)
11174                 cp_error_at ("invalid type qualifier for non-member function type", decl);
11175               else
11176                 ctype = TYPE_METHOD_BASETYPE (type);
11177             }
11178           if (ctype != NULL_TREE)
11179             grok_method_quals (ctype, decl, quals);
11180         }
11181
11182       if (RIDBIT_SETP (RID_SIGNED, specbits)
11183           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11184         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11185
11186       bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11187                       inlinep, friendp, raises != NULL_TREE);
11188
11189       return decl;
11190     }
11191
11192   /* Detect the case of an array type of unspecified size
11193      which came, as such, direct from a typedef name.
11194      We must copy the type, so that the array's domain can be
11195      individually set by the object's initializer.  */
11196
11197   if (type && typedef_type
11198       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11199       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11200     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11201
11202   /* Detect where we're using a typedef of function type to declare a
11203      function. last_function_parms will not be set, so we must create
11204      it now.  */
11205   
11206   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11207     {
11208       tree decls = NULL_TREE;
11209       tree args;
11210
11211       for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11212         {
11213           tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11214
11215           TREE_CHAIN (decl) = decls;
11216           decls = decl;
11217         }
11218       
11219       last_function_parms = nreverse (decls);
11220     }
11221
11222   /* If this is a type name (such as, in a cast or sizeof),
11223      compute the type and return it now.  */
11224
11225   if (decl_context == TYPENAME)
11226     {
11227       /* Note that the grammar rejects storage classes
11228          in typenames, fields or parameters.  */
11229       if (type_quals != TYPE_UNQUALIFIED)
11230         type_quals = TYPE_UNQUALIFIED;
11231
11232       /* Special case: "friend class foo" looks like a TYPENAME context.  */
11233       if (friendp)
11234         {
11235           if (type_quals != TYPE_UNQUALIFIED)
11236             {
11237               error ("type qualifiers specified for friend class declaration");
11238               type_quals = TYPE_UNQUALIFIED;
11239             }
11240           if (inlinep)
11241             {
11242               error ("`inline' specified for friend class declaration");
11243               inlinep = 0;
11244             }
11245
11246           if (!current_aggr)
11247             {
11248               /* Don't allow friend declaration without a class-key.  */
11249               if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11250                 pedwarn ("template parameters cannot be friends");
11251               else if (TREE_CODE (type) == TYPENAME_TYPE)
11252                 pedwarn ("friend declaration requires class-key, "
11253                          "i.e. `friend class %T::%D'",
11254                          TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11255               else
11256                 pedwarn ("friend declaration requires class-key, "
11257                          "i.e. `friend %#T'",
11258                          type);
11259             }
11260
11261           /* Only try to do this stuff if we didn't already give up.  */
11262           if (type != integer_type_node)
11263             {
11264               /* A friendly class?  */
11265               if (current_class_type)
11266                 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11267               else
11268                 error ("trying to make class `%T' a friend of global scope",
11269                           type);
11270
11271               type = void_type_node;
11272             }
11273         }
11274       else if (quals)
11275         {
11276           if (ctype == NULL_TREE)
11277             {
11278               if (TREE_CODE (type) != METHOD_TYPE)
11279                 error ("invalid qualifiers on non-member function type");
11280               else
11281                 ctype = TYPE_METHOD_BASETYPE (type);
11282             }
11283           if (ctype)
11284             {
11285               tree dummy = build_decl (TYPE_DECL, declarator, type);
11286               grok_method_quals (ctype, dummy, quals);
11287               type = TREE_TYPE (dummy);
11288             }
11289         }
11290
11291       return type;
11292     }
11293   else if (declarator == NULL_TREE && decl_context != PARM
11294            && decl_context != CATCHPARM
11295            && TREE_CODE (type) != UNION_TYPE
11296            && ! bitfield)
11297     {
11298       error ("abstract declarator `%T' used as declaration", type);
11299       declarator = make_anon_name ();
11300     }
11301
11302   /* `void' at top level (not within pointer)
11303      is allowed only in typedefs or type names.
11304      We don't complain about parms either, but that is because
11305      a better error message can be made later.  */
11306
11307   if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11308     {
11309       if (! declarator)
11310         error ("unnamed variable or field declared void");
11311       else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11312         {
11313           if (IDENTIFIER_OPNAME_P (declarator))
11314             abort ();
11315           else
11316             error ("variable or field `%s' declared void", name);
11317         }
11318       else
11319         error ("variable or field declared void");
11320       type = integer_type_node;
11321     }
11322
11323   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11324      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
11325
11326   if (decl_context == PARM || decl_context == CATCHPARM)
11327     {
11328       if (ctype || in_namespace)
11329         error ("cannot use `::' in parameter declaration");
11330
11331       /* A parameter declared as an array of T is really a pointer to T.
11332          One declared as a function is really a pointer to a function.
11333          One declared as a member is really a pointer to member.  */
11334
11335       if (TREE_CODE (type) == ARRAY_TYPE)
11336         {
11337           /* Transfer const-ness of array into that of type pointed to.  */
11338           type = build_pointer_type (TREE_TYPE (type));
11339           type_quals = TYPE_UNQUALIFIED;
11340         }
11341       else if (TREE_CODE (type) == FUNCTION_TYPE)
11342         type = build_pointer_type (type);
11343       else if (TREE_CODE (type) == OFFSET_TYPE)
11344         type = build_pointer_type (type);
11345     }
11346
11347   {
11348     register tree decl;
11349
11350     if (decl_context == PARM)
11351       {
11352         decl = cp_build_parm_decl (declarator, type);
11353
11354         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11355                         inlinep, friendp, raises != NULL_TREE);
11356       }
11357     else if (decl_context == FIELD)
11358       {
11359         if (type == error_mark_node)
11360           {
11361             /* Happens when declaring arrays of sizes which
11362                are error_mark_node, for example.  */
11363             decl = NULL_TREE;
11364           }
11365         else if (in_namespace && !friendp)
11366           {
11367             /* Something like struct S { int N::j; };  */
11368             error ("invalid use of `::'");
11369             decl = NULL_TREE;
11370           }
11371         else if (TREE_CODE (type) == FUNCTION_TYPE)
11372           {
11373             int publicp = 0;
11374             tree function_context;
11375
11376             /* We catch the others as conflicts with the builtin
11377                typedefs.  */
11378             if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11379               {
11380                 error ("function `%D' cannot be declared friend",
11381                           declarator);
11382                 friendp = 0;
11383               }
11384
11385             if (friendp == 0)
11386               {
11387                 if (ctype == NULL_TREE)
11388                   ctype = current_class_type;
11389
11390                 if (ctype == NULL_TREE)
11391                   {
11392                     error ("can't make `%D' into a method -- not in a class",
11393                               declarator);
11394                     return void_type_node;
11395                   }
11396
11397                 /* ``A union may [ ... ] not [ have ] virtual functions.''
11398                    ARM 9.5 */
11399                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11400                   {
11401                     error ("function `%D' declared virtual inside a union",
11402                               declarator);
11403                     return void_type_node;
11404                   }
11405
11406                 if (declarator == ansi_opname (NEW_EXPR)
11407                     || declarator == ansi_opname (VEC_NEW_EXPR)
11408                     || declarator == ansi_opname (DELETE_EXPR)
11409                     || declarator == ansi_opname (VEC_DELETE_EXPR))
11410                   {
11411                     if (virtualp)
11412                       {
11413                         error ("`%D' cannot be declared virtual, since it is always static",
11414                                   declarator);
11415                         virtualp = 0;
11416                       }
11417                   }
11418                 else if (staticp < 2)
11419                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11420                                                   TYPE_ARG_TYPES (type));
11421               }
11422
11423             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
11424             function_context = (ctype != NULL_TREE) ?
11425               decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11426             publicp = (! friendp || ! staticp)
11427               && function_context == NULL_TREE;
11428             decl = grokfndecl (ctype, type,
11429                                TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11430                                ? declarator : dname,
11431                                declarator,
11432                                virtualp, flags, quals, raises,
11433                                friendp ? -1 : 0, friendp, publicp, inlinep,
11434                                funcdef_flag, template_count, in_namespace);
11435             if (decl == NULL_TREE)
11436               return decl;
11437 #if 0
11438             /* This clobbers the attrs stored in `decl' from `attrlist'.  */
11439             /* The decl and setting of decl_attr is also turned off.  */
11440             decl = build_decl_attribute_variant (decl, decl_attr);
11441 #endif
11442
11443             /* [class.conv.ctor]
11444
11445                A constructor declared without the function-specifier
11446                explicit that can be called with a single parameter
11447                specifies a conversion from the type of its first
11448                parameter to the type of its class.  Such a constructor
11449                is called a converting constructor.  */
11450             if (explicitp == 2)
11451               DECL_NONCONVERTING_P (decl) = 1;
11452             else if (DECL_CONSTRUCTOR_P (decl))
11453               {
11454                 /* The constructor can be called with exactly one
11455                    parameter if there is at least one parameter, and
11456                    any subsequent parameters have default arguments.
11457                    Ignore any compiler-added parms.  */
11458                 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11459
11460                 if (arg_types == void_list_node
11461                     || (arg_types
11462                         && TREE_CHAIN (arg_types)
11463                         && TREE_CHAIN (arg_types) != void_list_node
11464                         && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11465                   DECL_NONCONVERTING_P (decl) = 1;
11466               }
11467           }
11468         else if (TREE_CODE (type) == METHOD_TYPE)
11469           {
11470             /* We only get here for friend declarations of
11471                members of other classes.  */
11472             /* All method decls are public, so tell grokfndecl to set
11473                TREE_PUBLIC, also.  */
11474             decl = grokfndecl (ctype, type, declarator, declarator,
11475                                virtualp, flags, quals, raises,
11476                                friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11477                                template_count, in_namespace);
11478             if (decl == NULL_TREE)
11479               return NULL_TREE;
11480           }
11481         else if (!staticp && ! processing_template_decl
11482                  && !COMPLETE_TYPE_P (complete_type (type))
11483                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11484           {
11485             if (declarator)
11486               error ("field `%D' has incomplete type", declarator);
11487             else
11488               error ("name `%T' has incomplete type", type);
11489
11490             /* If we're instantiating a template, tell them which
11491                instantiation made the field's type be incomplete.  */
11492             if (current_class_type
11493                 && TYPE_NAME (current_class_type)
11494                 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11495                 && declspecs && TREE_VALUE (declspecs)
11496                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11497               error ("  in instantiation of template `%T'",
11498                         current_class_type);
11499
11500             type = error_mark_node;
11501             decl = NULL_TREE;
11502           }
11503         else
11504           {
11505             if (friendp)
11506               {
11507                 error ("`%s' is neither function nor member function; cannot be declared friend",
11508                        IDENTIFIER_POINTER (declarator));
11509                 friendp = 0;
11510               }
11511             decl = NULL_TREE;
11512           }
11513
11514         if (friendp)
11515           {
11516             /* Friends are treated specially.  */
11517             if (ctype == current_class_type)
11518               warning ("member functions are implicitly friends of their class");
11519             else
11520               {
11521                 tree t = NULL_TREE;
11522                 if (decl && DECL_NAME (decl))
11523                   {
11524                     if (template_class_depth (current_class_type) == 0)
11525                       {
11526                         decl
11527                           = check_explicit_specialization
11528                           (declarator, decl,
11529                            template_count, 2 * (funcdef_flag != 0) + 4);
11530                         if (decl == error_mark_node)
11531                           return error_mark_node;
11532                       }
11533                     
11534                     t = do_friend (ctype, declarator, decl,
11535                                    last_function_parms, *attrlist,
11536                                    flags, quals, funcdef_flag);
11537                   }
11538                 if (t && funcdef_flag)
11539                   return t;
11540   
11541                 return void_type_node;
11542               }
11543           }
11544
11545         /* Structure field.  It may not be a function, except for C++ */
11546
11547         if (decl == NULL_TREE)
11548           {
11549             if (initialized)
11550               {
11551                 if (!staticp)
11552                   {
11553                     /* An attempt is being made to initialize a non-static
11554                        member.  But, from [class.mem]:
11555
11556                        4 A member-declarator can contain a
11557                        constant-initializer only if it declares a static
11558                        member (_class.static_) of integral or enumeration
11559                        type, see _class.static.data_.
11560
11561                        This used to be relatively common practice, but
11562                        the rest of the compiler does not correctly
11563                        handle the initialization unless the member is
11564                        static so we make it static below.  */
11565                     pedwarn ("ISO C++ forbids initialization of member `%D'",
11566                                 declarator);
11567                     pedwarn ("making `%D' static", declarator);
11568                     staticp = 1;
11569                   }
11570
11571                 if (uses_template_parms (type))
11572                   /* We'll check at instantiation time.  */
11573                   ;
11574                 else if (check_static_variable_definition (declarator,
11575                                                            type))
11576                   /* If we just return the declaration, crashes
11577                      will sometimes occur.  We therefore return
11578                      void_type_node, as if this was a friend
11579                      declaration, to cause callers to completely
11580                      ignore this declaration.  */
11581                   return void_type_node;
11582               }
11583
11584             if (staticp)
11585               {
11586                 /* [class.mem] forbids static data members with the
11587                    same name as the enclosing class.  Non-static data
11588                    members are checked in check_field_decls.  */
11589                 if (constructor_name_p (declarator, current_class_type))
11590                   pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11591                            declarator);
11592                   
11593                 /* C++ allows static class members.  All other work
11594                    for this is done by grokfield.  */
11595                 decl = build_lang_decl (VAR_DECL, declarator, type);
11596                 TREE_STATIC (decl) = 1;
11597                 /* In class context, 'static' means public access.  */
11598                 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11599               }
11600             else
11601               {
11602                 decl = build_decl (FIELD_DECL, declarator, type);
11603                 DECL_NONADDRESSABLE_P (decl) = bitfield;
11604                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11605                   {
11606                     DECL_MUTABLE_P (decl) = 1;
11607                     RIDBIT_RESET (RID_MUTABLE, specbits);
11608                   }
11609               }
11610
11611             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11612                             inlinep, friendp, raises != NULL_TREE);
11613           }
11614       }
11615     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11616       {
11617         tree original_name;
11618         int publicp = 0;
11619
11620         if (! declarator)
11621           return NULL_TREE;
11622
11623         if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11624           original_name = dname;
11625         else
11626           original_name = declarator;
11627
11628         if (RIDBIT_SETP (RID_AUTO, specbits))
11629           error ("storage class `auto' invalid for function `%s'", name);
11630         else if (RIDBIT_SETP (RID_REGISTER, specbits))
11631           error ("storage class `register' invalid for function `%s'", name);
11632         else if (RIDBIT_SETP (RID_THREAD, specbits))
11633           error ("storage class `__thread' invalid for function `%s'", name);
11634
11635         /* Function declaration not at top level.
11636            Storage classes other than `extern' are not allowed
11637            and `extern' makes no difference.  */
11638         if (! toplevel_bindings_p ()
11639             && (RIDBIT_SETP (RID_STATIC, specbits)
11640                 || RIDBIT_SETP (RID_INLINE, specbits))
11641             && pedantic)
11642           {
11643             if (RIDBIT_SETP (RID_STATIC, specbits))
11644               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11645             else
11646               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11647           }
11648
11649         if (ctype == NULL_TREE)
11650           {
11651             if (virtualp)
11652               {
11653                 error ("virtual non-class function `%s'", name);
11654                 virtualp = 0;
11655               }
11656           }
11657         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11658           type = build_cplus_method_type (ctype, TREE_TYPE (type),
11659                                           TYPE_ARG_TYPES (type));
11660
11661         /* Record presence of `static'.  */
11662         publicp = (ctype != NULL_TREE
11663                    || RIDBIT_SETP (RID_EXTERN, specbits)
11664                    || !RIDBIT_SETP (RID_STATIC, specbits));
11665
11666         decl = grokfndecl (ctype, type, original_name, declarator,
11667                            virtualp, flags, quals, raises,
11668                            1, friendp,
11669                            publicp, inlinep, funcdef_flag,
11670                            template_count, in_namespace);
11671         if (decl == NULL_TREE)
11672           return NULL_TREE;
11673
11674         if (staticp == 1)
11675           {
11676             int invalid_static = 0;
11677
11678             /* Don't allow a static member function in a class, and forbid
11679                declaring main to be static.  */
11680             if (TREE_CODE (type) == METHOD_TYPE)
11681               {
11682                 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11683                 invalid_static = 1;
11684               }
11685             else if (current_function_decl)
11686               {
11687                 /* FIXME need arm citation */
11688                 error ("cannot declare static function inside another function");
11689                 invalid_static = 1;
11690               }
11691
11692             if (invalid_static)
11693               {
11694                 staticp = 0;
11695                 RIDBIT_RESET (RID_STATIC, specbits);
11696               }
11697           }
11698       }
11699     else
11700       {
11701         /* It's a variable.  */
11702
11703         /* An uninitialized decl with `extern' is a reference.  */
11704         decl = grokvardecl (type, declarator, &specbits,
11705                             initialized,
11706                             (type_quals & TYPE_QUAL_CONST) != 0,
11707                             ctype ? ctype : in_namespace);
11708         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11709                         inlinep, friendp, raises != NULL_TREE);
11710
11711         if (ctype)
11712           {
11713             DECL_CONTEXT (decl) = ctype;
11714             if (staticp == 1)
11715               {
11716                 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11717                 staticp = 0;
11718                 RIDBIT_RESET (RID_STATIC, specbits);
11719               }
11720             if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11721               {
11722                 error ("static member `%D' declared `register'", decl);
11723                 RIDBIT_RESET (RID_REGISTER, specbits);
11724               }
11725             if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11726               {
11727                 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11728                             decl);
11729                 RIDBIT_RESET (RID_EXTERN, specbits);
11730               }
11731           }
11732       }
11733
11734     my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11735
11736     /* Record `register' declaration for warnings on &
11737        and in case doing stupid register allocation.  */
11738
11739     if (RIDBIT_SETP (RID_REGISTER, specbits))
11740       DECL_REGISTER (decl) = 1;
11741
11742     if (RIDBIT_SETP (RID_EXTERN, specbits))
11743       DECL_THIS_EXTERN (decl) = 1;
11744
11745     if (RIDBIT_SETP (RID_STATIC, specbits))
11746       DECL_THIS_STATIC (decl) = 1;
11747
11748     /* Record constancy and volatility.  There's no need to do this
11749        when processing a template; we'll do this for the instantiated
11750        declaration based on the type of DECL.  */
11751     if (!processing_template_decl)
11752       c_apply_type_quals_to_decl (type_quals, decl);
11753
11754     return decl;
11755   }
11756 }
11757 \f
11758 /* Subroutine of start_function.  Ensure that each of the parameter
11759    types (as listed in PARMS) is complete, as is required for a
11760    function definition.  */
11761
11762 static void
11763 require_complete_types_for_parms (tree parms)
11764 {
11765   for (; parms; parms = TREE_CHAIN (parms))
11766     {
11767       if (VOID_TYPE_P (TREE_TYPE (parms)))
11768         /* grokparms will have already issued an error */
11769         TREE_TYPE (parms) = error_mark_node;
11770       else if (complete_type_or_else (TREE_TYPE (parms), parms))
11771         {
11772           layout_decl (parms, 0);
11773           DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11774         }
11775       else
11776         TREE_TYPE (parms) = error_mark_node;
11777     }
11778 }
11779
11780 /* Returns nonzero if T is a local variable.  */
11781
11782 int
11783 local_variable_p (tree t)
11784 {
11785   if ((TREE_CODE (t) == VAR_DECL
11786        /* A VAR_DECL with a context that is a _TYPE is a static data
11787           member.  */
11788        && !TYPE_P (CP_DECL_CONTEXT (t))
11789        /* Any other non-local variable must be at namespace scope.  */
11790        && !DECL_NAMESPACE_SCOPE_P (t))
11791       || (TREE_CODE (t) == PARM_DECL))
11792     return 1;
11793
11794   return 0;
11795 }
11796
11797 /* Returns nonzero if T is an automatic local variable or a label.
11798    (These are the declarations that need to be remapped when the code
11799    containing them is duplicated.)  */
11800
11801 int
11802 nonstatic_local_decl_p (tree t)
11803 {
11804   return ((local_variable_p (t) && !TREE_STATIC (t))
11805           || TREE_CODE (t) == LABEL_DECL
11806           || TREE_CODE (t) == RESULT_DECL);
11807 }
11808
11809 /* Like local_variable_p, but suitable for use as a tree-walking
11810    function.  */
11811
11812 static tree
11813 local_variable_p_walkfn (tree* tp,
11814                          int* walk_subtrees ATTRIBUTE_UNUSED ,
11815                          void* data ATTRIBUTE_UNUSED )
11816 {
11817   return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11818           ? *tp : NULL_TREE);
11819 }
11820
11821 /* Check that ARG, which is a default-argument expression for a
11822    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
11823    something goes wrong.  DECL may also be a _TYPE node, rather than a
11824    DECL, if there is no DECL available.  */
11825
11826 tree
11827 check_default_argument (tree decl, tree arg)
11828 {
11829   tree var;
11830   tree decl_type;
11831
11832   if (TREE_CODE (arg) == DEFAULT_ARG)
11833     /* We get a DEFAULT_ARG when looking at an in-class declaration
11834        with a default argument.  Ignore the argument for now; we'll
11835        deal with it after the class is complete.  */
11836     return arg;
11837
11838   if (processing_template_decl || uses_template_parms (arg))
11839     /* We don't do anything checking until instantiation-time.  Note
11840        that there may be uninstantiated arguments even for an
11841        instantiated function, since default arguments are not
11842        instantiated until they are needed.  */
11843     return arg;
11844
11845   if (TYPE_P (decl))
11846     {
11847       decl_type = decl;
11848       decl = NULL_TREE;
11849     }
11850   else
11851     decl_type = TREE_TYPE (decl);
11852
11853   if (arg == error_mark_node
11854       || decl == error_mark_node
11855       || TREE_TYPE (arg) == error_mark_node
11856       || decl_type == error_mark_node)
11857     /* Something already went wrong.  There's no need to check
11858        further.  */
11859     return error_mark_node;
11860
11861   /* [dcl.fct.default]
11862
11863      A default argument expression is implicitly converted to the
11864      parameter type.  */
11865   if (!TREE_TYPE (arg)
11866       || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11867     {
11868       if (decl)
11869         error ("default argument for `%#D' has type `%T'",
11870                   decl, TREE_TYPE (arg));
11871       else
11872         error ("default argument for parameter of type `%T' has type `%T'",
11873                   decl_type, TREE_TYPE (arg));
11874
11875       return error_mark_node;
11876     }
11877
11878   /* [dcl.fct.default]
11879
11880      Local variables shall not be used in default argument
11881      expressions.
11882
11883      The keyword `this' shall not be used in a default argument of a
11884      member function.  */
11885   var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11886                                       NULL);
11887   if (var)
11888     {
11889       error ("default argument `%E' uses local variable `%D'",
11890                 arg, var);
11891       return error_mark_node;
11892     }
11893
11894   /* All is well.  */
11895   return arg;
11896 }
11897
11898 /* Decode the list of parameter types for a function type.
11899    Given the list of things declared inside the parens,
11900    return a list of types.
11901
11902    We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11903    flag. If unset, we append void_list_node. A parmlist declared
11904    as `(void)' is accepted as the empty parmlist.
11905
11906    Also set last_function_parms to the chain of PARM_DECLs.  */
11907
11908 static tree
11909 grokparms (tree first_parm)
11910 {
11911   tree result = NULL_TREE;
11912   tree decls = NULL_TREE;
11913   int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11914   tree parm, chain;
11915   int any_error = 0;
11916
11917   my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11918
11919   for (parm = first_parm; parm != NULL_TREE; parm = chain)
11920     {
11921       tree type = NULL_TREE;
11922       tree decl = TREE_VALUE (parm);
11923       tree init = TREE_PURPOSE (parm);
11924       tree specs, attrs;
11925
11926       chain = TREE_CHAIN (parm);
11927       /* @@ weak defense against parse errors.  */
11928       if (TREE_CODE (decl) != VOID_TYPE
11929           && TREE_CODE (decl) != TREE_LIST)
11930         {
11931           /* Give various messages as the need arises.  */
11932           if (TREE_CODE (decl) == STRING_CST)
11933             error ("invalid string constant `%E'", decl);
11934           else if (TREE_CODE (decl) == INTEGER_CST)
11935             error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11936           continue;
11937         }
11938
11939       if (parm == void_list_node)
11940         break;
11941
11942       split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11943       decl = grokdeclarator (TREE_VALUE (decl), specs,
11944                              PARM, init != NULL_TREE, &attrs);
11945       if (! decl || TREE_TYPE (decl) == error_mark_node)
11946         continue;
11947
11948       if (attrs)
11949         cplus_decl_attributes (&decl, attrs, 0);
11950
11951       type = TREE_TYPE (decl);
11952       if (VOID_TYPE_P (type))
11953         {
11954           if (same_type_p (type, void_type_node)
11955               && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11956             /* this is a parmlist of `(void)', which is ok.  */
11957             break;
11958           cxx_incomplete_type_error (decl, type);
11959           /* It's not a good idea to actually create parameters of
11960              type `void'; other parts of the compiler assume that a
11961              void type terminates the parameter list.  */
11962           type = error_mark_node;
11963           TREE_TYPE (decl) = error_mark_node;
11964         }
11965
11966       if (type != error_mark_node)
11967         {
11968           /* Top-level qualifiers on the parameters are
11969              ignored for function types.  */
11970           type = TYPE_MAIN_VARIANT (type);
11971           if (TREE_CODE (type) == METHOD_TYPE)
11972             {
11973               error ("parameter `%D' invalidly declared method type", decl);
11974               type = build_pointer_type (type);
11975               TREE_TYPE (decl) = type;
11976             }
11977           else if (TREE_CODE (type) == OFFSET_TYPE)
11978             {
11979               error ("parameter `%D' invalidly declared offset type", decl);
11980               type = build_pointer_type (type);
11981               TREE_TYPE (decl) = type;
11982             }
11983           else if (abstract_virtuals_error (decl, type))
11984             any_error = 1;  /* Seems like a good idea.  */
11985           else if (POINTER_TYPE_P (type))
11986             {
11987               /* [dcl.fct]/6, parameter types cannot contain pointers
11988                  (references) to arrays of unknown bound.  */
11989               tree t = TREE_TYPE (type);
11990               int ptr = TYPE_PTR_P (type);
11991
11992               while (1)
11993                 {
11994                   if (TYPE_PTR_P (t))
11995                     ptr = 1;
11996                   else if (TREE_CODE (t) != ARRAY_TYPE)
11997                     break;
11998                   else if (!TYPE_DOMAIN (t))
11999                     break;
12000                   t = TREE_TYPE (t);
12001                 }
12002               if (TREE_CODE (t) == ARRAY_TYPE)
12003                 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12004                           decl, ptr ? "pointer" : "reference", t);
12005             }
12006
12007           if (!any_error && init)
12008             init = check_default_argument (decl, init);
12009           else
12010             init = NULL_TREE;
12011         }
12012
12013       TREE_CHAIN (decl) = decls;
12014       decls = decl;
12015       result = tree_cons (init, type, result);
12016     }
12017   decls = nreverse (decls);
12018   result = nreverse (result);
12019   if (!ellipsis)
12020     result = chainon (result, void_list_node);
12021   last_function_parms = decls;
12022
12023   return result;
12024 }
12025
12026 \f
12027 /* D is a constructor or overloaded `operator='.
12028
12029    Let T be the class in which D is declared. Then, this function
12030    returns:
12031
12032    -1 if D's is an ill-formed constructor or copy assignment operator
12033       whose first parameter is of type `T'.
12034    0  if D is not a copy constructor or copy assignment
12035       operator.
12036    1  if D is a copy constructor or copy assignment operator whose
12037       first parameter is a reference to const qualified T.
12038    2  if D is a copy constructor or copy assignment operator whose
12039       first parameter is a reference to non-const qualified T.
12040
12041    This function can be used as a predicate. Positive values indicate
12042    a copy constructor and nonzero values indicate a copy assignment
12043    operator.  */
12044
12045 int
12046 copy_fn_p (tree d)
12047 {
12048   tree args;
12049   tree arg_type;
12050   int result = 1;
12051   
12052   my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12053
12054   if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12055     /* Instantiations of template member functions are never copy
12056        functions.  Note that member functions of templated classes are
12057        represented as template functions internally, and we must
12058        accept those as copy functions.  */
12059     return 0;
12060     
12061   args = FUNCTION_FIRST_USER_PARMTYPE (d);
12062   if (!args)
12063     return 0;
12064
12065   arg_type = TREE_VALUE (args);
12066
12067   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12068     {
12069       /* Pass by value copy assignment operator.  */
12070       result = -1;
12071     }
12072   else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12073            && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12074     {
12075       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12076         result = 2;
12077     }
12078   else
12079     return 0;
12080   
12081   args = TREE_CHAIN (args);
12082
12083   if (args && args != void_list_node && !TREE_PURPOSE (args))
12084     /* There are more non-optional args.  */
12085     return 0;
12086
12087   return result;
12088 }
12089
12090 /* Remember any special properties of member function DECL.  */
12091
12092 void grok_special_member_properties (tree decl)
12093 {
12094   if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12095     ; /* Not special.  */
12096   else if (DECL_CONSTRUCTOR_P (decl))
12097     {
12098       int ctor = copy_fn_p (decl);
12099       
12100       if (ctor > 0)
12101         {
12102           /* [class.copy]
12103               
12104              A non-template constructor for class X is a copy
12105              constructor if its first parameter is of type X&, const
12106              X&, volatile X& or const volatile X&, and either there
12107              are no other parameters or else all other parameters have
12108              default arguments.  */
12109           TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12110           if (ctor > 1)
12111             TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12112         }
12113       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12114         TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12115     }
12116   else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12117     {
12118       /* [class.copy]
12119           
12120          A non-template assignment operator for class X is a copy
12121          assignment operator if its parameter is of type X, X&, const
12122          X&, volatile X& or const volatile X&.  */
12123       
12124       int assop = copy_fn_p (decl);
12125       
12126       if (assop)
12127         {
12128           TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12129           if (assop != 1)
12130             TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12131           if (DECL_PURE_VIRTUAL_P (decl))
12132             TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12133         }
12134     }
12135 }
12136
12137 /* Check a constructor DECL has the correct form.  Complains
12138    if the class has a constructor of the form X(X).  */
12139
12140 int
12141 grok_ctor_properties (tree ctype, tree decl)
12142 {
12143   int ctor_parm = copy_fn_p (decl);
12144
12145   if (ctor_parm < 0)
12146     {
12147       /* [class.copy]
12148           
12149          A declaration of a constructor for a class X is ill-formed if
12150          its first parameter is of type (optionally cv-qualified) X
12151          and either there are no other parameters or else all other
12152          parameters have default arguments.
12153           
12154          We *don't* complain about member template instantiations that
12155          have this form, though; they can occur as we try to decide
12156          what constructor to use during overload resolution.  Since
12157          overload resolution will never prefer such a constructor to
12158          the non-template copy constructor (which is either explicitly
12159          or implicitly defined), there's no need to worry about their
12160          existence.  Theoretically, they should never even be
12161          instantiated, but that's hard to forestall.  */
12162       error ("invalid constructor; you probably meant `%T (const %T&)'",
12163                 ctype, ctype);
12164       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12165       return 0;
12166     }
12167   
12168   return 1;
12169 }
12170
12171 /* An operator with this code is unary, but can also be binary.  */
12172
12173 static int
12174 ambi_op_p (enum tree_code code)
12175 {
12176   return (code == INDIRECT_REF
12177           || code == ADDR_EXPR
12178           || code == CONVERT_EXPR
12179           || code == NEGATE_EXPR
12180           || code == PREINCREMENT_EXPR
12181           || code == PREDECREMENT_EXPR);
12182 }
12183
12184 /* An operator with this name can only be unary.  */
12185
12186 static int
12187 unary_op_p (enum tree_code code)
12188 {
12189   return (code == TRUTH_NOT_EXPR
12190           || code == BIT_NOT_EXPR
12191           || code == COMPONENT_REF
12192           || code == TYPE_EXPR);
12193 }
12194
12195 /* Do a little sanity-checking on how they declared their operator.  */
12196
12197 void
12198 grok_op_properties (tree decl, int friendp)
12199 {
12200   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12201   tree argtype;
12202   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12203   tree name = DECL_NAME (decl);
12204   enum tree_code operator_code;
12205   int arity;
12206
12207   /* Count the number of arguments.  */
12208   for (argtype = argtypes, arity = 0;
12209        argtype && argtype != void_list_node;
12210        argtype = TREE_CHAIN (argtype))
12211     ++arity;
12212
12213   if (current_class_type == NULL_TREE)
12214     friendp = 1;
12215
12216   if (DECL_CONV_FN_P (decl))
12217     operator_code = TYPE_EXPR;
12218   else
12219     do
12220       {
12221 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)       \
12222         if (ansi_opname (CODE) == name)                         \
12223           {                                                     \
12224             operator_code = (CODE);                             \
12225             break;                                              \
12226           }                                                     \
12227         else if (ansi_assopname (CODE) == name)                 \
12228           {                                                     \
12229             operator_code = (CODE);                             \
12230             DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;              \
12231             break;                                              \
12232           }
12233
12234 #include "operators.def"
12235 #undef DEF_OPERATOR
12236
12237         abort ();
12238       }
12239     while (0);
12240   my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12241   SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12242
12243   if (! friendp)
12244     {
12245       switch (operator_code)
12246         {
12247         case CALL_EXPR:
12248           TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12249           break;
12250
12251         case ARRAY_REF:
12252           TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12253           break;
12254
12255         case COMPONENT_REF:
12256         case MEMBER_REF:
12257           TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12258           break;
12259
12260         case NEW_EXPR:
12261           TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12262           break;
12263
12264         case DELETE_EXPR:
12265           TYPE_GETS_DELETE (current_class_type) |= 1;
12266           break;
12267
12268         case VEC_NEW_EXPR:
12269           TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12270           break;
12271
12272         case VEC_DELETE_EXPR:
12273           TYPE_GETS_DELETE (current_class_type) |= 2;
12274           break;
12275
12276         default:
12277           break;
12278         }
12279     }
12280
12281   if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12282     {
12283       /* When the compiler encounters the definition of A::operator new, it
12284          doesn't look at the class declaration to find out if it's static.  */
12285       if (methodp)
12286         revert_static_member_fn (decl);
12287
12288       TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12289     }
12290   else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12291     {
12292       if (methodp)
12293         revert_static_member_fn (decl);
12294
12295       TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12296     }
12297   else
12298     {
12299       /* An operator function must either be a non-static member function
12300          or have at least one parameter of a class, a reference to a class,
12301          an enumeration, or a reference to an enumeration.  13.4.0.6 */
12302       if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12303         {
12304           if (operator_code == TYPE_EXPR
12305               || operator_code == CALL_EXPR
12306               || operator_code == COMPONENT_REF
12307               || operator_code == ARRAY_REF
12308               || operator_code == NOP_EXPR)
12309             error ("`%D' must be a nonstatic member function", decl);
12310           else
12311             {
12312               tree p = argtypes;
12313
12314               if (DECL_STATIC_FUNCTION_P (decl))
12315                 error ("`%D' must be either a non-static member function or a non-member function", decl);
12316
12317               if (p)
12318                 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12319                   {
12320                     tree arg = TREE_VALUE (p);
12321                     if (TREE_CODE (arg) == REFERENCE_TYPE)
12322                       arg = TREE_TYPE (arg);
12323
12324                     /* This lets bad template code slip through.  */
12325                     if (IS_AGGR_TYPE (arg)
12326                         || TREE_CODE (arg) == ENUMERAL_TYPE
12327                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12328                         || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12329                       goto foundaggr;
12330                   }
12331               error
12332                 ("`%D' must have an argument of class or enumerated type",
12333                  decl);
12334             foundaggr:
12335               ;
12336             }
12337         }
12338
12339       if (operator_code == CALL_EXPR)
12340         return;                 /* No restrictions on args.  */
12341
12342       if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12343         {
12344           tree t = TREE_TYPE (name);
12345           if (! friendp)
12346             {
12347               int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12348               const char *what = 0;
12349
12350               if (ref)
12351                 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12352
12353               if (TREE_CODE (t) == VOID_TYPE)
12354                 what = "void";
12355               else if (t == current_class_type)
12356                 what = "the same type";
12357               /* Don't force t to be complete here.  */
12358               else if (IS_AGGR_TYPE (t)
12359                        && COMPLETE_TYPE_P (t)
12360                        && DERIVED_FROM_P (t, current_class_type))
12361                 what = "a base class";
12362
12363               if (what)
12364                 warning ("conversion to %s%s will never use a type conversion operator",
12365                          ref ? "a reference to " : "", what);
12366             }
12367         }
12368       if (operator_code == COND_EXPR)
12369         {
12370           /* 13.4.0.3 */
12371           error ("ISO C++ prohibits overloading operator ?:");
12372         }
12373       else if (ambi_op_p (operator_code))
12374         {
12375           if (arity == 1)
12376             /* We pick the one-argument operator codes by default, so
12377                we don't have to change anything.  */
12378             ;
12379           else if (arity == 2)
12380             {
12381               /* If we thought this was a unary operator, we now know
12382                  it to be a binary operator.  */
12383               switch (operator_code)
12384                 {
12385                 case INDIRECT_REF:
12386                   operator_code = MULT_EXPR;
12387                   break;
12388
12389                 case ADDR_EXPR:
12390                   operator_code = BIT_AND_EXPR;
12391                   break;
12392
12393                 case CONVERT_EXPR:
12394                   operator_code = PLUS_EXPR;
12395                   break;
12396
12397                 case NEGATE_EXPR:
12398                   operator_code = MINUS_EXPR;
12399                   break;
12400
12401                 case PREINCREMENT_EXPR:
12402                   operator_code = POSTINCREMENT_EXPR;
12403                   break;
12404
12405                 case PREDECREMENT_EXPR:
12406                   operator_code = POSTDECREMENT_EXPR;
12407                   break;
12408
12409                 default:
12410                   abort ();
12411                 }
12412
12413               SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12414
12415               if ((operator_code == POSTINCREMENT_EXPR
12416                    || operator_code == POSTDECREMENT_EXPR)
12417                   && ! processing_template_decl
12418                   && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12419                 {
12420                   if (methodp)
12421                     error ("postfix `%D' must take `int' as its argument",
12422                               decl);
12423                   else
12424                     error
12425                       ("postfix `%D' must take `int' as its second argument",
12426                        decl);
12427                 }
12428             }
12429           else
12430             {
12431               if (methodp)
12432                 error ("`%D' must take either zero or one argument", decl);
12433               else
12434                 error ("`%D' must take either one or two arguments", decl);
12435             }
12436
12437           /* More Effective C++ rule 6.  */
12438           if (warn_ecpp
12439               && (operator_code == POSTINCREMENT_EXPR
12440                   || operator_code == POSTDECREMENT_EXPR
12441                   || operator_code == PREINCREMENT_EXPR
12442                   || operator_code == PREDECREMENT_EXPR))
12443             {
12444               tree arg = TREE_VALUE (argtypes);
12445               tree ret = TREE_TYPE (TREE_TYPE (decl));
12446               if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12447                 arg = TREE_TYPE (arg);
12448               arg = TYPE_MAIN_VARIANT (arg);
12449               if (operator_code == PREINCREMENT_EXPR
12450                   || operator_code == PREDECREMENT_EXPR)
12451                 {
12452                   if (TREE_CODE (ret) != REFERENCE_TYPE
12453                       || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12454                                        arg))
12455                     warning ("prefix `%D' should return `%T'", decl,
12456                                 build_reference_type (arg));
12457                 }
12458               else
12459                 {
12460                   if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12461                     warning ("postfix `%D' should return `%T'", decl, arg);
12462                 }
12463             }
12464         }
12465       else if (unary_op_p (operator_code))
12466         {
12467           if (arity != 1)
12468             {
12469               if (methodp)
12470                 error ("`%D' must take `void'", decl);
12471               else
12472                 error ("`%D' must take exactly one argument", decl);
12473             }
12474         }
12475       else /* if (binary_op_p (operator_code)) */
12476         {
12477           if (arity != 2)
12478             {
12479               if (methodp)
12480                 error ("`%D' must take exactly one argument", decl);
12481               else
12482                 error ("`%D' must take exactly two arguments", decl);
12483             }
12484
12485           /* More Effective C++ rule 7.  */
12486           if (warn_ecpp
12487               && (operator_code == TRUTH_ANDIF_EXPR
12488                   || operator_code == TRUTH_ORIF_EXPR
12489                   || operator_code == COMPOUND_EXPR))
12490             warning ("user-defined `%D' always evaluates both arguments",
12491                         decl);
12492         }
12493
12494       /* Effective C++ rule 23.  */
12495       if (warn_ecpp
12496           && arity == 2
12497           && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12498           && (operator_code == PLUS_EXPR
12499               || operator_code == MINUS_EXPR
12500               || operator_code == TRUNC_DIV_EXPR
12501               || operator_code == MULT_EXPR
12502               || operator_code == TRUNC_MOD_EXPR)
12503           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12504         warning ("`%D' should return by value", decl);
12505
12506       /* [over.oper]/8 */
12507       for (; argtypes && argtypes != void_list_node;
12508           argtypes = TREE_CHAIN (argtypes))
12509         if (TREE_PURPOSE (argtypes))
12510           {
12511             TREE_PURPOSE (argtypes) = NULL_TREE;
12512             if (operator_code == POSTINCREMENT_EXPR
12513                 || operator_code == POSTDECREMENT_EXPR)
12514               {
12515                 if (pedantic)
12516                   pedwarn ("`%D' cannot have default arguments", decl);
12517               }
12518             else
12519               error ("`%D' cannot have default arguments", decl);
12520           }
12521
12522     }
12523 }
12524 \f
12525 static const char *
12526 tag_name (enum tag_types code)
12527 {
12528   switch (code)
12529     {
12530     case record_type:
12531       return "struct";
12532     case class_type:
12533       return "class";
12534     case union_type:
12535       return "union ";
12536     case enum_type:
12537       return "enum";
12538     default:
12539       abort ();
12540     }
12541 }
12542
12543 /* Name lookup in an elaborated-type-specifier (after the keyword
12544    indicated by TAG_CODE) has found TYPE.  If the
12545    elaborated-type-specifier is invalid, issue a diagnostic and return
12546    error_mark_node; otherwise, return TYPE itself.  */
12547
12548 static tree
12549 check_elaborated_type_specifier (enum tag_types tag_code,
12550                                  tree type)
12551 {
12552   tree t;
12553
12554   t = follow_tag_typedef (type);
12555
12556   /* [dcl.type.elab] If the identifier resolves to a typedef-name or a
12557      template type-parameter, the elaborated-type-specifier is
12558      ill-formed.  */
12559   if (!t)
12560     {
12561       error ("using typedef-name `%D' after `%s'",
12562              TYPE_NAME (type), tag_name (tag_code));
12563       t = error_mark_node;
12564     }
12565   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12566     {
12567       error ("using template type parameter `%T' after `%s'",
12568              type, tag_name (tag_code));
12569       t = error_mark_node;
12570     }
12571
12572   return t;
12573 }
12574
12575 /* Get the struct, enum or union (CODE says which) with tag NAME.
12576    Define the tag as a forward-reference if it is not defined.
12577
12578    C++: If a class derivation is given, process it here, and report
12579    an error if multiple derivation declarations are not identical.
12580
12581    If this is a definition, come in through xref_tag and only look in
12582    the current frame for the name (since C++ allows new names in any
12583    scope.)  */
12584
12585 tree
12586 xref_tag (enum tag_types tag_code, tree name, tree attributes, 
12587           bool globalize)
12588 {
12589   enum tree_code code;
12590   register tree ref, t;
12591   struct cp_binding_level *b = current_binding_level;
12592   tree context = NULL_TREE;
12593
12594   timevar_push (TV_NAME_LOOKUP);
12595   switch (tag_code)
12596     {
12597     case record_type:
12598     case class_type:
12599       code = RECORD_TYPE;
12600       break;
12601     case union_type:
12602       code = UNION_TYPE;
12603       break;
12604     case enum_type:
12605       code = ENUMERAL_TYPE;
12606       break;
12607     default:
12608       abort ();
12609     }
12610
12611   /* If a cross reference is requested, look up the type
12612      already defined for this tag and return it.  */
12613   if (TYPE_P (name))
12614     {
12615       t = name;
12616       name = TYPE_IDENTIFIER (t);
12617     }
12618   else
12619     t = IDENTIFIER_TYPE_VALUE (name);
12620
12621   /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
12622   if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12623     {
12624       static int explained;
12625       tree shadowed;
12626
12627       warning ("`%s %T' declares a new type at namespace scope",
12628                   tag_name (tag_code), name);
12629       if (!explained++)
12630         warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12631                     tag_name (tag_code),
12632                     constructor_name (current_class_type),
12633                     TYPE_IDENTIFIER (t));
12634
12635       /* We need to remove the class scope binding for the
12636          TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
12637       for (shadowed = b->class_shadowed;
12638            shadowed;
12639            shadowed = TREE_CHAIN (shadowed))
12640         if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12641           {
12642             TREE_PURPOSE (shadowed) = NULL_TREE;
12643             break;
12644           }
12645     }
12646
12647   if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12648       && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12649     t = NULL_TREE;
12650
12651   if (! globalize)
12652     {
12653       /* If we know we are defining this tag, only look it up in
12654          this scope and don't try to find it as a type.  */
12655       ref = lookup_tag (code, name, b, 1);
12656     }
12657   else
12658     {
12659       if (t)
12660         {
12661           ref = check_elaborated_type_specifier (tag_code, t);
12662           if (ref == error_mark_node)
12663             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12664         }
12665       else
12666         ref = lookup_tag (code, name, b, 0);
12667
12668       if (! ref)
12669         {
12670           /* Try finding it as a type declaration.  If that wins,
12671              use it.  */
12672           ref = lookup_name (name, 1);
12673
12674           if (ref != NULL_TREE
12675               && processing_template_decl
12676               && DECL_CLASS_TEMPLATE_P (ref)
12677               && template_class_depth (current_class_type) == 0)
12678             /* Since GLOBALIZE is true, we're declaring a global
12679                template, so we want this type.  */
12680             ref = DECL_TEMPLATE_RESULT (ref);
12681
12682           if (ref && TREE_CODE (ref) == TYPE_DECL)
12683             {
12684               ref = check_elaborated_type_specifier (tag_code, 
12685                                                      TREE_TYPE (ref));
12686               if (ref == error_mark_node)
12687                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12688               if (ref && TREE_CODE (ref) != code)
12689                 ref = NULL_TREE;
12690             }
12691           else
12692             ref = NULL_TREE;
12693         }
12694
12695       if (ref && current_class_type
12696           && template_class_depth (current_class_type)
12697           && PROCESSING_REAL_TEMPLATE_DECL_P ())
12698         {
12699           /* Since GLOBALIZE is nonzero, we are not looking at a
12700              definition of this tag.  Since, in addition, we are currently
12701              processing a (member) template declaration of a template
12702              class, we must be very careful; consider:
12703
12704                template <class X>
12705                struct S1
12706
12707                template <class U>
12708                struct S2
12709                { template <class V>
12710                friend struct S1; };
12711
12712              Here, the S2::S1 declaration should not be confused with the
12713              outer declaration.  In particular, the inner version should
12714              have a template parameter of level 2, not level 1.  This
12715              would be particularly important if the member declaration
12716              were instead:
12717
12718                template <class V = U> friend struct S1;
12719
12720              say, when we should tsubst into `U' when instantiating
12721              S2.  On the other hand, when presented with:
12722
12723                  template <class T>
12724                  struct S1 {
12725                    template <class U>
12726                    struct S2 {};
12727                    template <class U>
12728                    friend struct S2;
12729                  };
12730
12731               we must find the inner binding eventually.  We
12732               accomplish this by making sure that the new type we
12733               create to represent this declaration has the right
12734               TYPE_CONTEXT.  */
12735           context = TYPE_CONTEXT (ref);
12736           ref = NULL_TREE;
12737         }
12738     }
12739
12740   if (! ref)
12741     {
12742       /* If no such tag is yet defined, create a forward-reference node
12743          and record it as the "definition".
12744          When a real declaration of this type is found,
12745          the forward-reference will be altered into a real type.  */
12746       if (code == ENUMERAL_TYPE)
12747         {
12748           error ("use of enum `%#D' without previous declaration", name);
12749
12750           ref = make_node (ENUMERAL_TYPE);
12751
12752           /* Give the type a default layout like unsigned int
12753              to avoid crashing if it does not get defined.  */
12754           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12755           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12756           TYPE_USER_ALIGN (ref) = 0;
12757           TREE_UNSIGNED (ref) = 1;
12758           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12759           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12760           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12761
12762           /* Enable us to recognize when a type is created in class context.
12763              To do nested classes correctly, this should probably be cleared
12764              out when we leave this classes scope.  Currently this in only
12765              done in `start_enum'.  */
12766
12767           pushtag (name, ref, globalize);
12768         }
12769       else
12770         {
12771           struct cp_binding_level *old_b = class_binding_level;
12772
12773           ref = make_aggr_type (code);
12774           TYPE_CONTEXT (ref) = context;
12775
12776 #ifdef NONNESTED_CLASSES
12777           /* Class types don't nest the way enums do.  */
12778           class_binding_level = (struct cp_binding_level *)0;
12779 #endif
12780           pushtag (name, ref, globalize);
12781           class_binding_level = old_b;
12782         }
12783     }
12784   else
12785     {
12786       if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12787         redeclare_class_template (ref, current_template_parms);
12788     }
12789
12790   TYPE_ATTRIBUTES (ref) = attributes;
12791
12792   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12793 }
12794
12795 tree
12796 xref_tag_from_type (tree old, tree id, int globalize)
12797 {
12798   enum tag_types tag_kind;
12799
12800   if (TREE_CODE (old) == RECORD_TYPE)
12801     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12802   else
12803     tag_kind  = union_type;
12804
12805   if (id == NULL_TREE)
12806     id = TYPE_IDENTIFIER (old);
12807
12808   return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12809 }
12810
12811 /* REF is a type (named NAME), for which we have just seen some
12812    baseclasses.  BASE_LIST is a list of those baseclasses; the
12813    TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12814    the base-class.  TREE_VIA_VIRTUAL indicates virtual
12815    inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12816    struct, or union.  */
12817
12818 void
12819 xref_basetypes (tree ref, tree base_list)
12820 {
12821   /* In the declaration `A : X, Y, ... Z' we mark all the types
12822      (A, X, Y, ..., Z) so we can check for duplicates.  */
12823   tree *basep;
12824
12825   int i;
12826   enum tag_types tag_code;
12827
12828   if (TREE_CODE (ref) == UNION_TYPE)
12829     {
12830       error ("derived union `%T' invalid", ref);
12831       return;
12832     }
12833
12834   tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12835
12836   /* First, make sure that any templates in base-classes are
12837      instantiated.  This ensures that if we call ourselves recursively
12838      we do not get confused about which classes are marked and which
12839      are not.  */
12840   basep = &base_list; 
12841   while (*basep) 
12842     {
12843       tree basetype = TREE_VALUE (*basep);
12844       if (!(processing_template_decl && uses_template_parms (basetype))
12845           && !complete_type_or_else (basetype, NULL))
12846         /* An incomplete type.  Remove it from the list.  */
12847         *basep = TREE_CHAIN (*basep);
12848       else
12849         basep = &TREE_CHAIN (*basep);
12850     }
12851
12852   SET_CLASSTYPE_MARKED (ref);
12853   i = list_length (base_list);
12854   if (i)
12855     {
12856       tree binfo = TYPE_BINFO (ref);
12857       tree binfos = make_tree_vec (i);
12858       tree accesses = make_tree_vec (i);
12859       
12860       BINFO_BASETYPES (binfo) = binfos;
12861       BINFO_BASEACCESSES (binfo) = accesses;
12862   
12863       for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12864         {
12865           tree access = TREE_PURPOSE (base_list);
12866           int via_virtual = TREE_VIA_VIRTUAL (base_list);
12867           tree basetype = TREE_VALUE (base_list);
12868           tree base_binfo;
12869           
12870           if (access == access_default_node)
12871             /* The base of a derived struct is public by default.  */
12872             access = (tag_code == class_type
12873                       ? access_private_node : access_public_node);
12874           
12875           if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12876             basetype = TREE_TYPE (basetype);
12877           if (!basetype
12878               || (TREE_CODE (basetype) != RECORD_TYPE
12879                   && TREE_CODE (basetype) != TYPENAME_TYPE
12880                   && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12881                   && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12882             {
12883               error ("base type `%T' fails to be a struct or class type",
12884                      basetype);
12885               continue;
12886             }
12887           
12888           if (CLASSTYPE_MARKED (basetype))
12889             {
12890               if (basetype == ref)
12891                 error ("recursive type `%T' undefined", basetype);
12892               else
12893                 error ("duplicate base type `%T' invalid", basetype);
12894               continue;
12895             }
12896           
12897           if (TYPE_FOR_JAVA (basetype)
12898               && (current_lang_depth () == 0))
12899             TYPE_FOR_JAVA (ref) = 1;
12900           
12901           if (CLASS_TYPE_P (basetype))
12902             {
12903               base_binfo = TYPE_BINFO (basetype);
12904               /* This flag will be in the binfo of the base type, we must
12905                  clear it after copying the base binfos.  */
12906               BINFO_DEPENDENT_BASE_P (base_binfo)
12907                 = dependent_type_p (basetype);
12908             }
12909           else
12910             base_binfo = make_binfo (size_zero_node, basetype,
12911                                      NULL_TREE, NULL_TREE);
12912           
12913           TREE_VEC_ELT (binfos, i) = base_binfo;
12914           TREE_VEC_ELT (accesses, i) = access;
12915           /* This flag will be in the binfo of the base type, we must
12916              clear it after copying the base binfos.  */
12917           TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12918           
12919           SET_CLASSTYPE_MARKED (basetype);
12920           
12921           /* We are free to modify these bits because they are meaningless
12922              at top level, and BASETYPE is a top-level type.  */
12923           if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12924             {
12925               TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12926               /* Converting to a virtual base class requires looking
12927                  up the offset of the virtual base.  */
12928               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12929             }
12930           
12931           if (CLASS_TYPE_P (basetype))
12932             {
12933               TYPE_HAS_NEW_OPERATOR (ref)
12934                 |= TYPE_HAS_NEW_OPERATOR (basetype);
12935               TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12936                 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12937               TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12938               /* If the base-class uses multiple inheritance, so do we.  */
12939               TYPE_USES_MULTIPLE_INHERITANCE (ref)
12940                 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12941               /* Likewise, if converting to a base of the base may require
12942                  code, then we may need to generate code to convert to a
12943                  base as well.  */
12944               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12945                 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12946             }
12947           i++;
12948         }
12949       if (i)
12950         TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12951       else
12952         BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12953       
12954       if (i > 1)
12955         {
12956           TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12957           /* If there is more than one non-empty they cannot be at the same
12958              address.  */
12959           TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12960         }
12961     }
12962   
12963   /* Copy the base binfos, collect the virtual bases and set the
12964      inheritance order chain.  */
12965   copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12966   CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12967   
12968   /* Unmark all the types.  */
12969   while (i--)
12970     {
12971       tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12972       
12973       CLEAR_CLASSTYPE_MARKED (basetype);
12974       if (CLASS_TYPE_P (basetype))
12975         {
12976           TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12977           BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12978         }
12979     }
12980   CLEAR_CLASSTYPE_MARKED (ref);
12981 }
12982
12983 \f
12984 /* Begin compiling the definition of an enumeration type.
12985    NAME is its name (or null if anonymous).
12986    Returns the type object, as yet incomplete.
12987    Also records info about it so that build_enumerator
12988    may be used to declare the individual values as they are read.  */
12989
12990 tree
12991 start_enum (tree name)
12992 {
12993   register tree enumtype = NULL_TREE;
12994   struct cp_binding_level *b = current_binding_level;
12995
12996   /* If this is the real definition for a previous forward reference,
12997      fill in the contents in the same object that used to be the
12998      forward reference.  */
12999
13000   if (name != NULL_TREE)
13001     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13002
13003   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13004     {
13005       error ("multiple definition of `%#T'", enumtype);
13006       cp_error_at ("previous definition here", enumtype);
13007       /* Clear out TYPE_VALUES, and start again.  */
13008       TYPE_VALUES (enumtype) = NULL_TREE;
13009     }
13010   else
13011     {
13012       enumtype = make_node (ENUMERAL_TYPE);
13013       pushtag (name, enumtype, 0);
13014     }
13015
13016   return enumtype;
13017 }
13018
13019 /* After processing and defining all the values of an enumeration type,
13020    install their decls in the enumeration type and finish it off.
13021    ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
13022
13023 void
13024 finish_enum (tree enumtype)
13025 {
13026   tree pair;
13027   tree minnode;
13028   tree maxnode;
13029   tree t;
13030   bool unsignedp;
13031   int lowprec;
13032   int highprec; 
13033   int precision;
13034
13035   /* We built up the VALUES in reverse order.  */
13036   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13037
13038   /* For an enum defined in a template, just set the type of the values;
13039      all further processing is postponed until the template is
13040      instantiated.  We need to set the type so that tsubst of a CONST_DECL
13041      works.  */
13042   if (processing_template_decl)
13043     {
13044       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13045         TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13046       if (at_function_scope_p ())
13047         add_stmt (build_min (TAG_DEFN, enumtype));
13048       return;
13049     }
13050
13051   if (TYPE_VALUES (enumtype))
13052     {
13053       minnode = maxnode = NULL_TREE;
13054
13055       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13056         {
13057           tree decl = TREE_VALUE (pair);
13058           tree value = DECL_INITIAL (decl);
13059
13060           /* [dcl.enum]: Following the closing brace of an enum-specifier,
13061              each enumerator has the type of its enumeration.  Prior to the
13062              closing brace, the type of each enumerator is the type of its
13063              initializing value.  */
13064           TREE_TYPE (decl) = enumtype;
13065
13066           /* Figure out what the minimum and maximum values of the
13067              enumerators are.  */
13068           if (!minnode)
13069             minnode = maxnode = value;
13070           else if (tree_int_cst_lt (maxnode, value))
13071             maxnode = value;
13072           else if (tree_int_cst_lt (value, minnode))
13073             minnode = value;
13074
13075           /* Set the TREE_TYPE for the values as well.  That's so that when
13076              we call decl_constant_value we get an entity of the right type
13077              (but with the constant value).  But first make a copy so we
13078              don't clobber shared INTEGER_CSTs.  */
13079           if (TREE_TYPE (value) != enumtype)
13080             {
13081               value = DECL_INITIAL (decl) = copy_node (value);
13082               TREE_TYPE (value) = enumtype;
13083             }
13084
13085           /* In addition, transform the TYPE_VALUES list to contain the
13086              values, rather than the CONST_DECLs for them.  */
13087           TREE_VALUE (pair) = value;
13088         }
13089     }
13090   else
13091     minnode = maxnode = integer_zero_node;
13092
13093   /* Compute the number of bits require to represent all values of the
13094      enumeration.  We must do this before the type of MINNODE and
13095      MAXNODE are transformed, since min_precision relies on the
13096      TREE_TYPE of the value it is passed.  */
13097   unsignedp = tree_int_cst_sgn (minnode) >= 0;
13098   lowprec = min_precision (minnode, unsignedp);
13099   highprec = min_precision (maxnode, unsignedp);
13100   precision = MAX (lowprec, highprec);
13101
13102   /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
13103   TYPE_SIZE (enumtype) = NULL_TREE;
13104   TYPE_PRECISION (enumtype) = precision;
13105   if (unsignedp)
13106     fixup_unsigned_type (enumtype);
13107   else
13108     fixup_signed_type (enumtype);
13109
13110   if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13111     /* Use the width of the narrowest normal C type which is wide
13112        enough.  */
13113     TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13114                                                 (precision, 1));
13115   else
13116     TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13117
13118   TYPE_SIZE (enumtype) = NULL_TREE;
13119   layout_type (enumtype);
13120
13121   /* Fix up all variant types of this enum type.  */
13122   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13123     {
13124       TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13125       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13126       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13127       TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13128       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13129       TYPE_MODE (t) = TYPE_MODE (enumtype);
13130       TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13131       TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13132       TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13133       TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13134     }
13135
13136   /* Finish debugging output for this type.  */
13137   rest_of_type_compilation (enumtype, namespace_bindings_p ());
13138 }
13139
13140 /* Build and install a CONST_DECL for an enumeration constant of the
13141    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13142    Assignment of sequential values by default is handled here.  */
13143
13144 void
13145 build_enumerator (tree name, tree value, tree enumtype)
13146 {
13147   tree decl;
13148   tree context;
13149   tree type;
13150
13151   /* Remove no-op casts from the value.  */
13152   if (value)
13153     STRIP_TYPE_NOPS (value);
13154
13155   if (! processing_template_decl)
13156     {
13157       /* Validate and default VALUE.  */
13158       if (value != NULL_TREE)
13159         {
13160           value = decl_constant_value (value);
13161
13162           if (TREE_CODE (value) == INTEGER_CST)
13163             {
13164               value = default_conversion (value);
13165               constant_expression_warning (value);
13166             }
13167           else
13168             {
13169               error ("enumerator value for `%D' not integer constant", name);
13170               value = NULL_TREE;
13171             }
13172         }
13173
13174       /* Default based on previous value.  */
13175       if (value == NULL_TREE)
13176         {
13177           tree prev_value;
13178
13179           if (TYPE_VALUES (enumtype))
13180             {
13181               /* The next value is the previous value ...  */
13182               prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13183               /* ... plus one.  */
13184               value = cp_build_binary_op (PLUS_EXPR,
13185                                           prev_value,
13186                                           integer_one_node);
13187
13188               if (tree_int_cst_lt (value, prev_value))
13189                 error ("overflow in enumeration values at `%D'", name);
13190             }
13191           else
13192             value = integer_zero_node;
13193         }
13194
13195       /* Remove no-op casts from the value.  */
13196       STRIP_TYPE_NOPS (value);
13197     }
13198
13199   /* C++ associates enums with global, function, or class declarations.  */
13200   context = current_scope ();
13201
13202   /* Build the actual enumeration constant.  Note that the enumeration
13203     constants have the type of their initializers until the
13204     enumeration is complete:
13205
13206       [ dcl.enum ]
13207
13208       Following the closing brace of an enum-specifier, each enumer-
13209       ator has the type of its enumeration.  Prior to the closing
13210       brace, the type of each enumerator is the type of its
13211       initializing value.
13212
13213     In finish_enum we will reset the type.  Of course, if we're
13214     processing a template, there may be no value.  */
13215   type = value ? TREE_TYPE (value) : NULL_TREE;
13216
13217   if (context && context == current_class_type)
13218     /* This enum declaration is local to the class.  We need the full
13219        lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
13220     decl = build_lang_decl (CONST_DECL, name, type);
13221   else
13222     /* It's a global enum, or it's local to a function.  (Note local to
13223       a function could mean local to a class method.  */
13224     decl = build_decl (CONST_DECL, name, type);
13225
13226   DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13227   TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13228   DECL_INITIAL (decl) = value;
13229
13230   if (context && context == current_class_type)
13231     /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13232       on the TYPE_FIELDS list for `S'.  (That's so that you can say
13233       things like `S::i' later.)  */
13234     finish_member_declaration (decl);
13235   else
13236     pushdecl (decl);
13237
13238   /* Add this enumeration constant to the list for this type.  */
13239   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13240 }
13241
13242 \f
13243 /* We're defining DECL.  Make sure that it's type is OK.  */
13244
13245 static void
13246 check_function_type (tree decl, tree current_function_parms)
13247 {
13248   tree fntype = TREE_TYPE (decl);
13249   tree return_type = complete_type (TREE_TYPE (fntype));
13250
13251   /* In a function definition, arg types must be complete.  */
13252   require_complete_types_for_parms (current_function_parms);
13253
13254   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13255     {
13256       error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13257
13258       /* Make it return void instead, but don't change the
13259          type of the DECL_RESULT, in case we have a named return value.  */
13260       if (TREE_CODE (fntype) == METHOD_TYPE)
13261         {
13262           tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13263           TREE_TYPE (decl)
13264             = build_cplus_method_type (ctype,
13265                                        void_type_node,
13266                                        FUNCTION_ARG_CHAIN (decl));
13267         }
13268       else
13269         TREE_TYPE (decl)
13270           = build_function_type (void_type_node,
13271                                  TYPE_ARG_TYPES (TREE_TYPE (decl)));
13272       TREE_TYPE (decl)
13273         = build_exception_variant (fntype,
13274                                    TYPE_RAISES_EXCEPTIONS (fntype));
13275     }
13276   else
13277     abstract_virtuals_error (decl, TREE_TYPE (fntype));
13278 }
13279
13280 /* Create the FUNCTION_DECL for a function definition.
13281    DECLSPECS and DECLARATOR are the parts of the declaration;
13282    they describe the function's name and the type it returns,
13283    but twisted together in a fashion that parallels the syntax of C.
13284
13285    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13286    DECLARATOR is really the DECL for the function we are about to
13287    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13288    indicating that the function is an inline defined in-class.
13289
13290    This function creates a binding context for the function body
13291    as well as setting up the FUNCTION_DECL in current_function_decl.
13292
13293    Returns 1 on success.  If the DECLARATOR is not suitable for a function
13294    (it defines a datum instead), we return 0, which tells
13295    yyparse to report a parse error.
13296
13297    For C++, we must first check whether that datum makes any sense.
13298    For example, "class A local_a(1,2);" means that variable local_a
13299    is an aggregate of type A, which should have a constructor
13300    applied to it with the argument list [1, 2].  */
13301
13302 int
13303 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13304 {
13305   tree decl1;
13306   tree ctype = NULL_TREE;
13307   tree fntype;
13308   tree restype;
13309   int doing_friend = 0;
13310   struct cp_binding_level *bl;
13311   tree current_function_parms;
13312
13313   /* Sanity check.  */
13314   my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13315   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13316
13317   /* This should only be done once on the top most decl.  */
13318   if (have_extern_spec)
13319     {
13320       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13321       have_extern_spec = false;
13322     }
13323
13324   if (flags & SF_PRE_PARSED)
13325     {
13326       decl1 = declarator;
13327
13328       fntype = TREE_TYPE (decl1);
13329       if (TREE_CODE (fntype) == METHOD_TYPE)
13330         ctype = TYPE_METHOD_BASETYPE (fntype);
13331
13332       /* ISO C++ 11.4/5.  A friend function defined in a class is in
13333          the (lexical) scope of the class in which it is defined.  */
13334       if (!ctype && DECL_FRIEND_P (decl1))
13335         {
13336           ctype = DECL_FRIEND_CONTEXT (decl1);
13337
13338           /* CTYPE could be null here if we're dealing with a template;
13339              for example, `inline friend float foo()' inside a template
13340              will have no CTYPE set.  */
13341           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13342             ctype = NULL_TREE;
13343           else
13344             doing_friend = 1;
13345         }
13346
13347       last_function_parms = DECL_ARGUMENTS (decl1);
13348     }
13349   else
13350     {
13351       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13352       /* If the declarator is not suitable for a function definition,
13353          cause a syntax error.  */
13354       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13355         return 0;
13356
13357       cplus_decl_attributes (&decl1, attrs, 0);
13358
13359       /* If #pragma weak was used, mark the decl weak now.  */
13360       if (current_binding_level == global_binding_level)
13361         maybe_apply_pragma_weak (decl1);
13362
13363       fntype = TREE_TYPE (decl1);
13364
13365       restype = TREE_TYPE (fntype);
13366       if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13367         {
13368           error ("semicolon missing after declaration of `%#T'", restype);
13369           shadow_tag (build_tree_list (NULL_TREE, restype));
13370           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13371           if (TREE_CODE (fntype) == FUNCTION_TYPE)
13372             fntype = build_function_type (integer_type_node,
13373                                           TYPE_ARG_TYPES (fntype));
13374           else
13375             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13376                                               integer_type_node,
13377                                               TYPE_ARG_TYPES (fntype));
13378           TREE_TYPE (decl1) = fntype;
13379         }
13380
13381       if (TREE_CODE (fntype) == METHOD_TYPE)
13382         ctype = TYPE_METHOD_BASETYPE (fntype);
13383       else if (DECL_MAIN_P (decl1))
13384         {
13385           /* If this doesn't return integer_type, complain.  */
13386           if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13387             {
13388               if (pedantic || warn_return_type)
13389                 pedwarn ("return type for `main' changed to `int'");
13390               TREE_TYPE (decl1) = fntype = default_function_type;
13391             }
13392         }
13393     }
13394
13395   if (DECL_DECLARED_INLINE_P (decl1)
13396       && lookup_attribute ("noinline", attrs))
13397     warning ("%Hinline function '%D' given attribute noinline",
13398              &DECL_SOURCE_LOCATION (decl1), decl1);
13399
13400   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13401     /* This is a constructor, we must ensure that any default args
13402        introduced by this definition are propagated to the clones
13403        now. The clones are used directly in overload resolution.  */
13404     adjust_clone_args (decl1);
13405
13406   /* Sometimes we don't notice that a function is a static member, and
13407      build a METHOD_TYPE for it.  Fix that up now.  */
13408   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13409       && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13410     {
13411       revert_static_member_fn (decl1);
13412       last_function_parms = TREE_CHAIN (last_function_parms);
13413       ctype = NULL_TREE;
13414     }
13415
13416   /* Warn if function was previously implicitly declared
13417      (but not if we warned then).  */
13418   if (! warn_implicit
13419       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13420     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13421
13422   /* Set up current_class_type, and enter the scope of the class, if
13423      appropriate.  */
13424   if (ctype)
13425     push_nested_class (ctype);
13426   else if (DECL_STATIC_FUNCTION_P (decl1))
13427     push_nested_class (DECL_CONTEXT (decl1));
13428
13429   /* Now that we have entered the scope of the class, we must restore
13430      the bindings for any template parameters surrounding DECL1, if it
13431      is an inline member template.  (Order is important; consider the
13432      case where a template parameter has the same name as a field of
13433      the class.)  It is not until after this point that
13434      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
13435   if (flags & SF_INCLASS_INLINE)
13436     maybe_begin_member_template_processing (decl1);
13437
13438   /* Effective C++ rule 15.  */
13439   if (warn_ecpp
13440       && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13441       && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13442     warning ("`operator=' should return a reference to `*this'");
13443
13444   /* Make the init_value nonzero so pushdecl knows this is not tentative.
13445      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
13446   if (!DECL_INITIAL (decl1))
13447     DECL_INITIAL (decl1) = error_mark_node;
13448
13449   /* This function exists in static storage.
13450      (This does not mean `static' in the C sense!)  */
13451   TREE_STATIC (decl1) = 1;
13452
13453   /* We must call push_template_decl after current_class_type is set
13454      up.  (If we are processing inline definitions after exiting a
13455      class scope, current_class_type will be NULL_TREE until set above
13456      by push_nested_class.)  */
13457   if (processing_template_decl)
13458     decl1 = push_template_decl (decl1);
13459
13460   /* We are now in the scope of the function being defined.  */
13461   current_function_decl = decl1;
13462
13463   /* Save the parm names or decls from this function's declarator
13464      where store_parm_decls will find them.  */
13465   current_function_parms = last_function_parms;
13466
13467   /* Make sure the parameter and return types are reasonable.  When
13468      you declare a function, these types can be incomplete, but they
13469      must be complete when you define the function.  */
13470   if (! processing_template_decl)
13471     check_function_type (decl1, current_function_parms);
13472
13473   /* Build the return declaration for the function.  */
13474   restype = TREE_TYPE (fntype);
13475   /* Promote the value to int before returning it.  */
13476   if (c_promoting_integer_type_p (restype))
13477     restype = type_promotes_to (restype);
13478   if (DECL_RESULT (decl1) == NULL_TREE)
13479     {
13480       DECL_RESULT (decl1)
13481         = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13482       c_apply_type_quals_to_decl (cp_type_quals (restype),
13483                                   DECL_RESULT (decl1));
13484     }
13485
13486   /* Initialize RTL machinery.  We cannot do this until
13487      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
13488      even when processing a template; this is how we get
13489      CFUN set up, and our per-function variables initialized.
13490      FIXME factor out the non-RTL stuff.  */
13491   bl = current_binding_level;
13492   init_function_start (decl1, input_filename, input_line);
13493   current_binding_level = bl;
13494
13495   /* Even though we're inside a function body, we still don't want to
13496      call expand_expr to calculate the size of a variable-sized array.
13497      We haven't necessarily assigned RTL to all variables yet, so it's
13498      not safe to try to expand expressions involving them.  */
13499   immediate_size_expand = 0;
13500   cfun->x_dont_save_pending_sizes_p = 1;
13501
13502   /* Start the statement-tree, start the tree now.  */
13503   begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13504
13505   /* Don't double-count statements in templates.  */
13506   DECL_NUM_STMTS (decl1) = 0;
13507
13508   /* Let the user know we're compiling this function.  */
13509   announce_function (decl1);
13510
13511   /* Record the decl so that the function name is defined.
13512      If we already have a decl for this name, and it is a FUNCTION_DECL,
13513      use the old decl.  */
13514   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13515     {
13516       /* A specialization is not used to guide overload resolution.  */
13517       if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13518           && ! DECL_FUNCTION_MEMBER_P (decl1))
13519         decl1 = pushdecl (decl1);
13520       else
13521         {
13522           /* We need to set the DECL_CONTEXT.  */
13523           if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13524             DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13525           /* And make sure we have enough default args.  */
13526           check_default_args (decl1);
13527         }
13528       fntype = TREE_TYPE (decl1);
13529     }
13530
13531   /* Reset these in case the call to pushdecl changed them.  */
13532   current_function_decl = decl1;
13533   cfun->decl = decl1;
13534
13535   /* If we are (erroneously) defining a function that we have already
13536      defined before, wipe out what we knew before.  */
13537   if (!DECL_PENDING_INLINE_P (decl1))
13538     DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13539
13540   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13541     {
13542       /* We know that this was set up by `grokclassfn'.  We do not
13543          wait until `store_parm_decls', since evil parse errors may
13544          never get us to that point.  Here we keep the consistency
13545          between `current_class_type' and `current_class_ptr'.  */
13546       tree t = DECL_ARGUMENTS (decl1);
13547
13548       my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13549                           162);
13550       my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13551                           19990811);
13552
13553       cp_function_chain->x_current_class_ref
13554         = build_indirect_ref (t, NULL);
13555       cp_function_chain->x_current_class_ptr = t;
13556
13557       /* Constructors and destructors need to know whether they're "in
13558          charge" of initializing virtual base classes.  */
13559       t = TREE_CHAIN (t);
13560       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13561         {
13562           current_in_charge_parm = t;
13563           t = TREE_CHAIN (t);
13564         }
13565       if (DECL_HAS_VTT_PARM_P (decl1))
13566         {
13567           if (DECL_NAME (t) != vtt_parm_identifier)
13568             abort ();
13569           current_vtt_parm = t;
13570         }
13571     }
13572
13573   if (DECL_INTERFACE_KNOWN (decl1))
13574     {
13575       tree ctx = decl_function_context (decl1);
13576
13577       if (DECL_NOT_REALLY_EXTERN (decl1))
13578         DECL_EXTERNAL (decl1) = 0;
13579
13580       if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13581           && TREE_PUBLIC (ctx))
13582         /* This is a function in a local class in an extern inline
13583            function.  */
13584         comdat_linkage (decl1);
13585     }
13586   /* If this function belongs to an interface, it is public.
13587      If it belongs to someone else's interface, it is also external.
13588      This only affects inlines and template instantiations.  */
13589   else if (interface_unknown == 0
13590            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13591                || flag_alt_external_templates))
13592     {
13593       if (DECL_DECLARED_INLINE_P (decl1) 
13594           || DECL_TEMPLATE_INSTANTIATION (decl1)
13595           || processing_template_decl)
13596         {
13597           DECL_EXTERNAL (decl1)
13598             = (interface_only
13599                || (DECL_DECLARED_INLINE_P (decl1) 
13600                    && ! flag_implement_inlines
13601                    && !DECL_VINDEX (decl1)));
13602
13603           /* For WIN32 we also want to put these in linkonce sections.  */
13604           maybe_make_one_only (decl1);
13605         }
13606       else
13607         DECL_EXTERNAL (decl1) = 0;
13608       DECL_NOT_REALLY_EXTERN (decl1) = 0;
13609       DECL_INTERFACE_KNOWN (decl1) = 1;
13610     }
13611   else if (interface_unknown && interface_only
13612            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13613                || flag_alt_external_templates))
13614     {
13615       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13616          interface, we will have interface_only set but not
13617          interface_known.  In that case, we don't want to use the normal
13618          heuristics because someone will supply a #pragma implementation
13619          elsewhere, and deducing it here would produce a conflict.  */
13620       comdat_linkage (decl1);
13621       DECL_EXTERNAL (decl1) = 0;
13622       DECL_INTERFACE_KNOWN (decl1) = 1;
13623       DECL_DEFER_OUTPUT (decl1) = 1;
13624     }
13625   else
13626     {
13627       /* This is a definition, not a reference.
13628          So clear DECL_EXTERNAL.  */
13629       DECL_EXTERNAL (decl1) = 0;
13630
13631       if ((DECL_DECLARED_INLINE_P (decl1) 
13632            || DECL_TEMPLATE_INSTANTIATION (decl1))
13633           && ! DECL_INTERFACE_KNOWN (decl1)
13634           /* Don't try to defer nested functions for now.  */
13635           && ! decl_function_context (decl1))
13636         DECL_DEFER_OUTPUT (decl1) = 1;
13637       else
13638         DECL_INTERFACE_KNOWN (decl1) = 1;
13639     }
13640
13641   pushlevel (0);
13642   current_binding_level->parm_flag = 1;
13643
13644   ++function_depth;
13645
13646   if (DECL_DESTRUCTOR_P (decl1))
13647     {
13648       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13649       DECL_CONTEXT (dtor_label) = current_function_decl;
13650     }
13651
13652   start_fname_decls ();
13653   
13654   store_parm_decls (current_function_parms);
13655
13656   return 1;
13657 }
13658 \f
13659 /* Store the parameter declarations into the current function declaration.
13660    This is called after parsing the parameter declarations, before
13661    digesting the body of the function.
13662
13663    Also install to binding contour return value identifier, if any.  */
13664
13665 static void
13666 store_parm_decls (tree current_function_parms)
13667 {
13668   register tree fndecl = current_function_decl;
13669   register tree parm;
13670
13671   /* This is a chain of any other decls that came in among the parm
13672      declarations.  If a parm is declared with  enum {foo, bar} x;
13673      then CONST_DECLs for foo and bar are put here.  */
13674   tree nonparms = NULL_TREE;
13675
13676   if (current_function_parms)
13677     {
13678       /* This case is when the function was defined with an ANSI prototype.
13679          The parms already have decls, so we need not do anything here
13680          except record them as in effect
13681          and complain if any redundant old-style parm decls were written.  */
13682
13683       tree specparms = current_function_parms;
13684       tree next;
13685
13686       /* Must clear this because it might contain TYPE_DECLs declared
13687              at class level.  */
13688       storedecls (NULL_TREE);
13689
13690       /* If we're doing semantic analysis, then we'll call pushdecl
13691              for each of these.  We must do them in reverse order so that
13692              they end in the correct forward order.  */
13693       specparms = nreverse (specparms);
13694
13695       for (parm = specparms; parm; parm = next)
13696         {
13697           next = TREE_CHAIN (parm);
13698           if (TREE_CODE (parm) == PARM_DECL)
13699             {
13700               if (DECL_NAME (parm) == NULL_TREE
13701                   || TREE_CODE (parm) != VOID_TYPE)
13702                 pushdecl (parm);
13703               else
13704                 error ("parameter `%D' declared void", parm);
13705             }
13706           else
13707             {
13708               /* If we find an enum constant or a type tag,
13709                  put it aside for the moment.  */
13710               TREE_CHAIN (parm) = NULL_TREE;
13711               nonparms = chainon (nonparms, parm);
13712             }
13713         }
13714
13715       /* Get the decls in their original chain order and record in the
13716          function.  This is all and only the PARM_DECLs that were
13717          pushed into scope by the loop above.  */
13718       DECL_ARGUMENTS (fndecl) = getdecls ();
13719       storetags (gettags ());
13720     }
13721   else
13722     DECL_ARGUMENTS (fndecl) = NULL_TREE;
13723
13724   /* Now store the final chain of decls for the arguments
13725      as the decl-chain of the current lexical scope.
13726      Put the enumerators in as well, at the front so that
13727      DECL_ARGUMENTS is not modified.  */
13728   storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13729
13730   /* Do the starting of the exception specifications, if we have any.  */
13731   if (flag_exceptions && !processing_template_decl
13732       && flag_enforce_eh_specs
13733       && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13734     current_eh_spec_block = begin_eh_spec_block ();
13735 }
13736
13737 \f
13738 /* We have finished doing semantic analysis on DECL, but have not yet
13739    generated RTL for its body.  Save away our current state, so that
13740    when we want to generate RTL later we know what to do.  */
13741
13742 static void
13743 save_function_data (tree decl)
13744 {
13745   struct language_function *f;
13746
13747   /* Save the language-specific per-function data so that we can
13748      get it back when we really expand this function.  */
13749   my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13750                       19990908);
13751
13752   /* Make a copy.  */
13753   f = ((struct language_function *)
13754        ggc_alloc (sizeof (struct language_function)));
13755   memcpy (f, cp_function_chain, sizeof (struct language_function));
13756   DECL_SAVED_FUNCTION_DATA (decl) = f;
13757
13758   /* Clear out the bits we don't need.  */
13759   f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13760   f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13761   f->x_named_label_uses = NULL;
13762   f->bindings = NULL;
13763   f->x_local_names = NULL;
13764
13765   /* When we get back here again, we will be expanding.  */
13766   f->x_expanding_p = 1;
13767
13768   /* If we've already decided that we cannot inline this function, we
13769      must remember that fact when we actually go to expand the
13770      function.  */
13771   if (current_function_cannot_inline)
13772     {
13773       f->cannot_inline = current_function_cannot_inline;
13774       DECL_INLINE (decl) = 0;
13775     }
13776 }
13777
13778 /* Add a note to mark the beginning of the main body of the constructor.
13779    This is used to set up the data structures for the cleanup regions for
13780    fully-constructed bases and members.  */
13781
13782 static void
13783 begin_constructor_body (void)
13784 {
13785 }
13786
13787 /* Add a note to mark the end of the main body of the constructor.  This is
13788    used to end the cleanup regions for fully-constructed bases and
13789    members.  */
13790
13791 static void
13792 finish_constructor_body (void)
13793 {
13794 }
13795
13796 /* Do all the processing for the beginning of a destructor; set up the
13797    vtable pointers and cleanups for bases and members.  */
13798
13799 static void
13800 begin_destructor_body (void)
13801 {
13802   tree if_stmt;
13803   tree compound_stmt;
13804
13805   /* If the dtor is empty, and we know there is not any possible
13806      way we could use any vtable entries, before they are possibly
13807      set by a base class dtor, we don't have to setup the vtables,
13808      as we know that any base class dtor will set up any vtables
13809      it needs.  We avoid MI, because one base class dtor can do a
13810      virtual dispatch to an overridden function that would need to
13811      have a non-related vtable set up, we cannot avoid setting up
13812      vtables in that case.  We could change this to see if there
13813      is just one vtable.
13814
13815      ??? In the destructor for a class, the vtables are set
13816      appropriately for that class.  There will be no non-related
13817      vtables.  jason 2001-12-11.  */
13818   if_stmt = begin_if_stmt ();
13819
13820   /* If it is not safe to avoid setting up the vtables, then
13821      someone will change the condition to be boolean_true_node.  
13822      (Actually, for now, we do not have code to set the condition
13823      appropriately, so we just assume that we always need to
13824      initialize the vtables.)  */
13825   finish_if_stmt_cond (boolean_true_node, if_stmt);
13826
13827   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13828
13829   /* Make all virtual function table pointers in non-virtual base
13830      classes point to CURRENT_CLASS_TYPE's virtual function
13831      tables.  */
13832   initialize_vtbl_ptrs (current_class_ptr);
13833
13834   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13835   finish_then_clause (if_stmt);
13836   finish_if_stmt ();
13837
13838   /* And insert cleanups for our bases and members so that they
13839      will be properly destroyed if we throw.  */
13840   push_base_cleanups ();
13841 }
13842
13843 /* At the end of every destructor we generate code to delete the object if
13844    necessary.  Do that now.  */
13845
13846 static void
13847 finish_destructor_body (void)
13848 {
13849   tree exprstmt;
13850
13851   /* Any return from a destructor will end up here; that way all base
13852      and member cleanups will be run when the function returns.  */
13853   add_stmt (build_stmt (LABEL_STMT, dtor_label));
13854
13855   /* In a virtual destructor, we must call delete.  */
13856   if (DECL_VIRTUAL_P (current_function_decl))
13857     {
13858       tree if_stmt;
13859       tree virtual_size = cxx_sizeof (current_class_type);
13860
13861       /* [class.dtor]
13862
13863       At the point of definition of a virtual destructor (including
13864       an implicit definition), non-placement operator delete shall
13865       be looked up in the scope of the destructor's class and if
13866       found shall be accessible and unambiguous.  */
13867       exprstmt = build_op_delete_call
13868         (DELETE_EXPR, current_class_ptr, virtual_size,
13869          LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13870
13871       if_stmt = begin_if_stmt ();
13872       finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13873                                   current_in_charge_parm,
13874                                   integer_one_node),
13875                            if_stmt);
13876       finish_expr_stmt (exprstmt);
13877       finish_then_clause (if_stmt);
13878       finish_if_stmt ();
13879     }
13880 }
13881
13882 /* Do the necessary processing for the beginning of a function body, which
13883    in this case includes member-initializers, but not the catch clauses of
13884    a function-try-block.  Currently, this means opening a binding level
13885    for the member-initializers (in a ctor) and member cleanups (in a dtor).
13886    In other functions, this isn't necessary, but it doesn't hurt.  */
13887
13888 tree
13889 begin_function_body (void)
13890 {
13891   tree stmt;
13892
13893   if (processing_template_decl)
13894     /* Do nothing now.  */;
13895   else
13896     /* Always keep the BLOCK node associated with the outermost pair of
13897        curly braces of a function.  These are needed for correct
13898        operation of dwarfout.c.  */
13899     keep_next_level (1);
13900
13901   stmt = begin_compound_stmt (0);
13902   COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13903
13904   if (processing_template_decl)
13905     /* Do nothing now.  */;
13906   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13907     begin_constructor_body ();
13908   else if (DECL_DESTRUCTOR_P (current_function_decl))
13909     begin_destructor_body ();
13910
13911   return stmt;
13912 }
13913
13914 /* Do the processing for the end of a function body.  Currently, this means
13915    closing out the cleanups for fully-constructed bases and members, and in
13916    the case of the destructor, deleting the object if desired.  Again, this
13917    is only meaningful for [cd]tors, since they are the only functions where
13918    there is a significant distinction between the main body and any
13919    function catch clauses.  Handling, say, main() return semantics here
13920    would be wrong, as flowing off the end of a function catch clause for
13921    main() would also need to return 0.  */
13922
13923 void
13924 finish_function_body (tree compstmt)
13925 {
13926   /* Close the block.  */
13927   finish_compound_stmt (0, compstmt);
13928
13929   if (processing_template_decl)
13930     /* Do nothing now.  */;
13931   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13932     finish_constructor_body ();
13933   else if (DECL_DESTRUCTOR_P (current_function_decl))
13934     finish_destructor_body ();
13935 }  
13936
13937 /* Finish up a function declaration and compile that function
13938    all the way to assembler language output.  The free the storage
13939    for the function definition.
13940
13941    FLAGS is a bitwise or of the following values:
13942      2 - INCLASS_INLINE
13943        We just finished processing the body of an in-class inline
13944        function definition.  (This processing will have taken place
13945        after the class definition is complete.)  */
13946
13947 tree
13948 finish_function (int flags)
13949 {
13950   register tree fndecl = current_function_decl;
13951   tree fntype, ctype = NULL_TREE;
13952   int inclass_inline = (flags & 2) != 0;
13953   int nested;
13954
13955   /* When we get some parse errors, we can end up without a
13956      current_function_decl, so cope.  */
13957   if (fndecl == NULL_TREE)
13958     return error_mark_node;
13959
13960   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13961       && DECL_VIRTUAL_P (fndecl)
13962       && !processing_template_decl)
13963     {
13964       tree fnclass = DECL_CONTEXT (fndecl);
13965       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13966         keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13967     }
13968
13969   nested = function_depth > 1;
13970   fntype = TREE_TYPE (fndecl);
13971
13972   /*  TREE_READONLY (fndecl) = 1;
13973       This caused &foo to be of type ptr-to-const-function
13974       which then got a warning when stored in a ptr-to-function variable.  */
13975
13976   my_friendly_assert (building_stmt_tree (), 20000911);
13977
13978   finish_fname_decls ();
13979   
13980   /* For a cloned function, we've already got all the code we need;
13981      there's no need to add any extra bits.  */
13982   if (!DECL_CLONED_FUNCTION_P (fndecl))
13983     {
13984       if (DECL_MAIN_P (current_function_decl))
13985         {
13986           /* Make it so that `main' always returns 0 by default.  */
13987 #if VMS_TARGET
13988           finish_return_stmt (integer_one_node);
13989 #else
13990           finish_return_stmt (integer_zero_node);
13991 #endif
13992         }
13993
13994       /* Finish dealing with exception specifiers.  */
13995       if (flag_exceptions && !processing_template_decl
13996           && flag_enforce_eh_specs
13997           && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13998         finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13999                               (TREE_TYPE (current_function_decl)),
14000                               current_eh_spec_block);
14001     }
14002
14003   /* If we're saving up tree structure, tie off the function now.  */
14004   finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14005
14006   /* If this function can't throw any exceptions, remember that.  */
14007   if (!processing_template_decl
14008       && !cp_function_chain->can_throw
14009       && !flag_non_call_exceptions)
14010     TREE_NOTHROW (fndecl) = 1;
14011
14012   /* This must come after expand_function_end because cleanups might
14013      have declarations (from inline functions) that need to go into
14014      this function's blocks.  */
14015   
14016   /* If the current binding level isn't the outermost binding level
14017      for this function, either there is a bug, or we have experienced
14018      syntax errors and the statement tree is malformed.  */
14019   if (current_binding_level->parm_flag != 1)
14020     {
14021       /* Make sure we have already experienced errors.  */
14022       if (errorcount == 0)
14023         abort ();
14024
14025       /* Throw away the broken statement tree and extra binding
14026          levels.  */
14027       DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14028
14029       while (current_binding_level->parm_flag != 1)
14030         {
14031           if (current_binding_level->parm_flag == 2)
14032             pop_nested_class ();
14033           else
14034             poplevel (0, 0, 0);
14035         }
14036     }
14037   poplevel (1, 0, 1);
14038
14039   /* Set up the named return value optimization, if we can.  Here, we
14040      eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14041      for the nrv.  genrtl_start_function and declare_return_variable
14042      handle making the nrv and RESULT_DECL share space.  */
14043   if (current_function_return_value)
14044     {
14045       tree r = current_function_return_value;
14046       tree outer;
14047
14048       if (r != error_mark_node
14049           /* This is only worth doing for fns that return in memory--and
14050              simpler, since we don't have to worry about promoted modes.  */
14051           && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
14052           /* Only allow this for variables declared in the outer scope of
14053              the function so we know that their lifetime always ends with a
14054              return; see g++.dg/opt/nrv6.C.  We could be more flexible if
14055              we were to do this optimization in tree-ssa.  */
14056           /* Skip the artificial function body block.  */
14057           && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
14058               chain_member (r, BLOCK_VARS (outer))))
14059         {
14060           
14061           DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14062           walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14063                                         nullify_returns_r, r);
14064         }
14065       else
14066         /* Clear it so genrtl_start_function and declare_return_variable
14067            know we're not optimizing.  */
14068         current_function_return_value = NULL_TREE;
14069     }
14070
14071   /* Remember that we were in class scope.  */
14072   if (current_class_name)
14073     ctype = current_class_type;
14074
14075   /* Must mark the RESULT_DECL as being in this function.  */
14076   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14077
14078   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14079      to the FUNCTION_DECL node itself.  */
14080   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14081
14082   /* Save away current state, if appropriate.  */
14083   if (!processing_template_decl)
14084     save_function_data (fndecl);
14085
14086   /* If this function calls `setjmp' it cannot be inlined.  When
14087      `longjmp' is called it is not guaranteed to restore the value of
14088      local variables that have been modified since the call to
14089      `setjmp'.  So, if were to inline this function into some caller
14090      `c', then when we `longjmp', we might not restore all variables
14091      in `c'.  (It might seem, at first blush, that there's no way for
14092      this function to modify local variables in `c', but their
14093      addresses may have been stored somewhere accessible to this
14094      function.)  */
14095   if (!processing_template_decl && calls_setjmp_p (fndecl))
14096     DECL_UNINLINABLE (fndecl) = 1;
14097
14098   /* Complain if there's just no return statement.  */
14099   if (warn_return_type
14100       && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14101       && !current_function_returns_value && !current_function_returns_null
14102       /* Don't complain if we abort or throw.  */
14103       && !current_function_returns_abnormally
14104       && !DECL_NAME (DECL_RESULT (fndecl))
14105       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
14106          inline function, as we might never be compiled separately.  */
14107       && (DECL_INLINE (fndecl) || processing_template_decl))
14108     warning ("no return statement in function returning non-void");
14109     
14110   /* Clear out memory we no longer need.  */
14111   free_after_parsing (cfun);
14112   /* Since we never call rest_of_compilation, we never clear
14113      CFUN.  Do so explicitly.  */
14114   free_after_compilation (cfun);
14115   cfun = NULL;
14116
14117   /* If this is an in-class inline definition, we may have to pop the
14118      bindings for the template parameters that we added in
14119      maybe_begin_member_template_processing when start_function was
14120      called.  */
14121   if (inclass_inline)
14122     maybe_end_member_template_processing ();
14123
14124   /* Leave the scope of the class.  */
14125   if (ctype)
14126     pop_nested_class ();
14127
14128   --function_depth;
14129
14130   /* Clean up.  */
14131   if (! nested)
14132     /* Let the error reporting routines know that we're outside a
14133        function.  For a nested function, this value is used in
14134        cxx_pop_function_context and then reset via pop_function_context.  */
14135     current_function_decl = NULL_TREE;
14136
14137   return fndecl;
14138 }
14139 \f
14140 /* Create the FUNCTION_DECL for a function definition.
14141    DECLSPECS and DECLARATOR are the parts of the declaration;
14142    they describe the return type and the name of the function,
14143    but twisted together in a fashion that parallels the syntax of C.
14144
14145    This function creates a binding context for the function body
14146    as well as setting up the FUNCTION_DECL in current_function_decl.
14147
14148    Returns a FUNCTION_DECL on success.
14149
14150    If the DECLARATOR is not suitable for a function (it defines a datum
14151    instead), we return 0, which tells yyparse to report a parse error.
14152
14153    May return void_type_node indicating that this method is actually
14154    a friend.  See grokfield for more details.
14155
14156    Came here with a `.pushlevel' .
14157
14158    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14159    CHANGES TO CODE IN `grokfield'.  */
14160
14161 tree
14162 start_method (tree declspecs, tree declarator, tree attrlist)
14163 {
14164   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14165                                 &attrlist);
14166
14167   if (fndecl == error_mark_node)
14168     return error_mark_node;
14169
14170   if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14171     {
14172       error ("invalid member function declaration");
14173       return error_mark_node;
14174     }
14175
14176   if (attrlist)
14177     cplus_decl_attributes (&fndecl, attrlist, 0);
14178
14179   /* Pass friends other than inline friend functions back.  */
14180   if (fndecl == void_type_node)
14181     return fndecl;
14182
14183   if (DECL_IN_AGGR_P (fndecl))
14184     {
14185       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14186         {
14187           if (DECL_CONTEXT (fndecl)
14188               && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14189             error ("`%D' is already defined in class `%T'", fndecl,
14190                       DECL_CONTEXT (fndecl));
14191         }
14192       return void_type_node;
14193     }
14194
14195   check_template_shadow (fndecl);
14196
14197   DECL_DECLARED_INLINE_P (fndecl) = 1;
14198
14199   DID_INLINE_FUNC (fndecl) = 0;
14200   if (flag_default_inline)
14201     DECL_INLINE (fndecl) = 1;
14202
14203   /* We process method specializations in finish_struct_1.  */
14204   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14205     fndecl = push_template_decl (fndecl);
14206
14207   if (! DECL_FRIEND_P (fndecl))
14208     {
14209       if (TREE_CHAIN (fndecl))
14210         {
14211           fndecl = copy_node (fndecl);
14212           TREE_CHAIN (fndecl) = NULL_TREE;
14213         }
14214       grok_special_member_properties (fndecl);
14215     }
14216
14217   cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14218
14219   /* Make a place for the parms */
14220   pushlevel (0);
14221   current_binding_level->parm_flag = 1;
14222
14223   DECL_IN_AGGR_P (fndecl) = 1;
14224   return fndecl;
14225 }
14226
14227 /* Go through the motions of finishing a function definition.
14228    We don't compile this method until after the whole class has
14229    been processed.
14230
14231    FINISH_METHOD must return something that looks as though it
14232    came from GROKFIELD (since we are defining a method, after all).
14233
14234    This is called after parsing the body of the function definition.
14235    STMTS is the chain of statements that makes up the function body.
14236
14237    DECL is the ..._DECL that `start_method' provided.  */
14238
14239 tree
14240 finish_method (tree decl)
14241 {
14242   register tree fndecl = decl;
14243   tree old_initial;
14244
14245   register tree link;
14246
14247   if (decl == void_type_node)
14248     return decl;
14249
14250   old_initial = DECL_INITIAL (fndecl);
14251
14252   /* Undo the level for the parms (from start_method).
14253      This is like poplevel, but it causes nothing to be
14254      saved.  Saving information here confuses symbol-table
14255      output routines.  Besides, this information will
14256      be correctly output when this method is actually
14257      compiled.  */
14258
14259   /* Clear out the meanings of the local variables of this level;
14260      also record in each decl which block it belongs to.  */
14261
14262   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14263     {
14264       if (DECL_NAME (link) != NULL_TREE)
14265         pop_binding (DECL_NAME (link), link);
14266       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14267       DECL_CONTEXT (link) = NULL_TREE;
14268     }
14269
14270   poplevel (0, 0, 0);
14271
14272   DECL_INITIAL (fndecl) = old_initial;
14273
14274   /* We used to check if the context of FNDECL was different from
14275      current_class_type as another way to get inside here.  This didn't work
14276      for String.cc in libg++.  */
14277   if (DECL_FRIEND_P (fndecl))
14278     {
14279       CLASSTYPE_INLINE_FRIENDS (current_class_type)
14280         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14281       decl = void_type_node;
14282     }
14283
14284   return decl;
14285 }
14286 \f
14287
14288 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
14289    we can lay it out later, when and if its type becomes complete.  */
14290
14291 void
14292 maybe_register_incomplete_var (tree var)
14293 {
14294   my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14295
14296   /* Keep track of variables with incomplete types.  */
14297   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 
14298       && DECL_EXTERNAL (var))
14299     {
14300       tree inner_type = TREE_TYPE (var);
14301       
14302       while (TREE_CODE (inner_type) == ARRAY_TYPE)
14303         inner_type = TREE_TYPE (inner_type);
14304       inner_type = TYPE_MAIN_VARIANT (inner_type);
14305       
14306       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14307           /* RTTI TD entries are created while defining the type_info.  */
14308           || (TYPE_LANG_SPECIFIC (inner_type)
14309               && TYPE_BEING_DEFINED (inner_type)))
14310         incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14311     }
14312 }
14313
14314 /* Called when a class type (given by TYPE) is defined.  If there are
14315    any existing VAR_DECLs whose type hsa been completed by this
14316    declaration, update them now.  */
14317
14318 void
14319 complete_vars (tree type)
14320 {
14321   tree *list = &incomplete_vars;
14322
14323   my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14324   while (*list) 
14325     {
14326       if (same_type_p (type, TREE_PURPOSE (*list)))
14327         {
14328           tree var = TREE_VALUE (*list);
14329           /* Complete the type of the variable.  The VAR_DECL itself
14330              will be laid out in expand_expr.  */
14331           complete_type (TREE_TYPE (var));
14332           /* Remove this entry from the list.  */
14333           *list = TREE_CHAIN (*list);
14334         }
14335       else
14336         list = &TREE_CHAIN (*list);
14337     }
14338 }
14339
14340 /* If DECL is of a type which needs a cleanup, build that cleanup
14341    here.  */
14342
14343 tree
14344 cxx_maybe_build_cleanup (tree decl)
14345 {
14346   tree type = TREE_TYPE (decl);
14347
14348   if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14349     {
14350       int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14351       tree rval;
14352
14353       if (TREE_CODE (type) == ARRAY_TYPE)
14354         rval = decl;
14355       else
14356         {
14357           cxx_mark_addressable (decl);
14358           rval = build_unary_op (ADDR_EXPR, decl, 0);
14359         }
14360
14361       /* Optimize for space over speed here.  */
14362       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14363           || flag_expensive_optimizations)
14364         flags |= LOOKUP_NONVIRTUAL;
14365
14366       rval = build_delete (TREE_TYPE (rval), rval,
14367                            sfk_complete_destructor, flags, 0);
14368
14369       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14370           && ! TYPE_HAS_DESTRUCTOR (type))
14371         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14372                                                build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14373
14374       return rval;
14375     }
14376   return NULL_TREE;
14377 }
14378 \f
14379 /* When a stmt has been parsed, this function is called.  */
14380
14381 void
14382 finish_stmt (void)
14383 {
14384   /* Always assume this statement was not an expression statement.  If
14385      it actually was an expression statement, its our callers
14386      responsibility to fix this up.  */
14387   last_expr_type = NULL_TREE;
14388 }
14389
14390 /* DECL was originally constructed as a non-static member function,
14391    but turned out to be static.  Update it accordingly.  */
14392
14393 void
14394 revert_static_member_fn (tree decl)
14395 {
14396   tree tmp;
14397   tree function = TREE_TYPE (decl);
14398   tree args = TYPE_ARG_TYPES (function);
14399
14400   if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14401       != TYPE_UNQUALIFIED)
14402     error ("static member function `%#D' declared with type qualifiers",
14403               decl);
14404
14405   args = TREE_CHAIN (args);
14406   tmp = build_function_type (TREE_TYPE (function), args);
14407   tmp = build_qualified_type (tmp, cp_type_quals (function));
14408   tmp = build_exception_variant (tmp,
14409                                  TYPE_RAISES_EXCEPTIONS (function));
14410   TREE_TYPE (decl) = tmp;
14411   if (DECL_ARGUMENTS (decl))
14412     DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14413   DECL_STATIC_FUNCTION_P (decl) = 1;
14414 }
14415
14416 /* Initialize the variables used during compilation of a C++
14417    function.  */
14418
14419 void
14420 cxx_push_function_context (struct function * f)
14421 {
14422   struct language_function *p
14423     = ((struct language_function *)
14424        ggc_alloc_cleared (sizeof (struct language_function)));
14425   f->language = p;
14426
14427   /* It takes an explicit call to expand_body to generate RTL for a
14428      function.  */
14429   expanding_p = 0;
14430
14431   /* Whenever we start a new function, we destroy temporaries in the
14432      usual way.  */
14433   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14434 }
14435
14436 /* Free the language-specific parts of F, now that we've finished
14437    compiling the function.  */
14438
14439 void
14440 cxx_pop_function_context (struct function * f)
14441 {
14442   f->language = 0;
14443 }
14444
14445 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14446    one of the language-independent trees.  */
14447
14448 enum cp_tree_node_structure_enum
14449 cp_tree_node_structure (union lang_tree_node * t)
14450 {
14451   switch (TREE_CODE (&t->generic))
14452     {
14453     case DEFAULT_ARG:           return TS_CP_DEFAULT_ARG;
14454     case IDENTIFIER_NODE:       return TS_CP_IDENTIFIER;
14455     case OVERLOAD:              return TS_CP_OVERLOAD;
14456     case TEMPLATE_PARM_INDEX:   return TS_CP_TPI;
14457     case PTRMEM_CST:            return TS_CP_PTRMEM;
14458     case BASELINK:              return TS_CP_BASELINK;
14459     case WRAPPER:               return TS_CP_WRAPPER;
14460     default:                    return TS_CP_GENERIC;
14461     }
14462 }
14463
14464 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14465    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
14466
14467 tree
14468 identifier_global_value (tree t)
14469 {
14470   return IDENTIFIER_GLOBAL_VALUE (t);
14471 }
14472
14473 /* Build the void_list_node (void_type_node having been created).  */
14474 tree
14475 build_void_list_node (void)
14476 {
14477   tree t = build_tree_list (NULL_TREE, void_type_node);
14478   TREE_PARMLIST (t) = 1;
14479   return t;
14480 }
14481
14482 static int
14483 cp_missing_noreturn_ok_p (tree decl)
14484 {
14485   /* A missing noreturn is ok for the `main' function.  */
14486   return DECL_MAIN_P (decl);
14487 }
14488
14489 #include "gt-cp-decl.h"
14490 #include "gtype-cp.h"