OSDN Git Service

PR c++/9252
[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 have happened 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
2246   scope_chain = s;
2247   current_function_decl = NULL_TREE;
2248   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2249   current_lang_name = lang_name_cplusplus;
2250   current_namespace = global_namespace;
2251   timevar_pop (TV_NAME_LOOKUP);
2252 }
2253
2254 void
2255 push_to_top_level (void)
2256 {
2257   maybe_push_to_top_level (0);
2258 }
2259
2260 void
2261 pop_from_top_level (void)
2262 {
2263   struct saved_scope *s = scope_chain;
2264   cxx_saved_binding *saved;
2265
2266   timevar_push (TV_NAME_LOOKUP); 
2267   /* Clear out class-level bindings cache.  */
2268   if (previous_class_type)
2269     invalidate_class_lookup_cache ();
2270
2271   current_lang_base = 0;
2272
2273   scope_chain = s->prev;
2274   for (saved = s->old_bindings; saved; saved = saved->previous)
2275     {
2276       tree id = saved->identifier;
2277
2278       IDENTIFIER_BINDING (id) = saved->binding;
2279       IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2280       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2281     }
2282
2283   /* If we were in the middle of compiling a function, restore our
2284      state.  */
2285   if (s->need_pop_function_context)
2286     pop_function_context_from (NULL_TREE);
2287   current_function_decl = s->function_decl;
2288   last_function_parms = s->last_parms;
2289   timevar_pop (TV_NAME_LOOKUP);
2290 }
2291 \f
2292 /* Push a definition of struct, union or enum tag "name".
2293    into binding_level "b".   "type" should be the type node,
2294    We assume that the tag "name" is not already defined.
2295
2296    Note that the definition may really be just a forward reference.
2297    In that case, the TYPE_SIZE will be a NULL_TREE.
2298
2299    C++ gratuitously puts all these tags in the name space.  */
2300
2301 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2302    record the shadowed value for this binding contour.  TYPE is
2303    the type that ID maps to.  */
2304
2305 static void
2306 set_identifier_type_value_with_scope (tree id, 
2307                                       tree type, 
2308                                       struct cp_binding_level* b)
2309 {
2310   if (!b->namespace_p)
2311     {
2312       /* Shadow the marker, not the real thing, so that the marker
2313          gets restored later.  */
2314       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2315       b->type_shadowed
2316         = tree_cons (id, old_type_value, b->type_shadowed);
2317     }
2318   else
2319     {
2320       cxx_binding *binding =
2321          binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2322       BINDING_TYPE (binding) = type;
2323       /* Store marker instead of real type.  */
2324       type = global_type_node;
2325     }
2326   SET_IDENTIFIER_TYPE_VALUE (id, type);
2327 }
2328
2329 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2330
2331 void
2332 set_identifier_type_value (tree id, tree type)
2333 {
2334   set_identifier_type_value_with_scope (id, type, current_binding_level);
2335 }
2336
2337 /* Return the type associated with id.  */
2338
2339 tree
2340 identifier_type_value (tree id)
2341 {
2342   timevar_push (TV_NAME_LOOKUP);
2343   /* There is no type with that name, anywhere.  */
2344   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2345     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2346   /* This is not the type marker, but the real thing.  */
2347   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2348     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2349   /* Have to search for it. It must be on the global level, now.
2350      Ask lookup_name not to return non-types. */
2351   id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2352   if (id)
2353     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2354   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2355 }
2356
2357 /* Pop off extraneous binding levels left over due to syntax errors.
2358
2359    We don't pop past namespaces, as they might be valid.  */
2360
2361 void
2362 pop_everything (void)
2363 {
2364 #ifdef DEBUG_BINDING_LEVELS
2365   fprintf (stderr, "XXX entering pop_everything ()\n");
2366 #endif
2367   while (!toplevel_bindings_p ())
2368     {
2369       if (current_binding_level->parm_flag == 2)
2370         pop_nested_class ();
2371       else
2372         poplevel (0, 0, 0);
2373     }
2374 #ifdef DEBUG_BINDING_LEVELS
2375   fprintf (stderr, "XXX leaving pop_everything ()\n");
2376 #endif
2377 }
2378
2379 /* The type TYPE is being declared.  If it is a class template, or a
2380    specialization of a class template, do any processing required and
2381    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2382    being declared a friend.  B is the binding level at which this TYPE
2383    should be bound.
2384
2385    Returns the TYPE_DECL for TYPE, which may have been altered by this
2386    processing.  */
2387
2388 static tree
2389 maybe_process_template_type_declaration (tree type, 
2390                                          int globalize, 
2391                                          struct cp_binding_level* b)
2392 {
2393   tree decl = TYPE_NAME (type);
2394
2395   if (processing_template_parmlist)
2396     /* You can't declare a new template type in a template parameter
2397        list.  But, you can declare a non-template type:
2398
2399          template <class A*> struct S;
2400
2401        is a forward-declaration of `A'.  */
2402     ;
2403   else
2404     {
2405       maybe_check_template_type (type);
2406
2407       my_friendly_assert (IS_AGGR_TYPE (type)
2408                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2409
2410
2411       if (processing_template_decl)
2412         {
2413           /* This may change after the call to
2414              push_template_decl_real, but we want the original value.  */
2415           tree name = DECL_NAME (decl);
2416
2417           decl = push_template_decl_real (decl, globalize);
2418           /* If the current binding level is the binding level for the
2419              template parameters (see the comment in
2420              begin_template_parm_list) and the enclosing level is a class
2421              scope, and we're not looking at a friend, push the
2422              declaration of the member class into the class scope.  In the
2423              friend case, push_template_decl will already have put the
2424              friend into global scope, if appropriate.  */
2425           if (TREE_CODE (type) != ENUMERAL_TYPE
2426               && !globalize && b->template_parms_p
2427               && b->level_chain->parm_flag == 2)
2428             {
2429               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2430               /* Put this tag on the list of tags for the class, since
2431                  that won't happen below because B is not the class
2432                  binding level, but is instead the pseudo-global level.  */
2433               b->level_chain->tags =
2434                 tree_cons (name, type, b->level_chain->tags);
2435               if (!COMPLETE_TYPE_P (current_class_type))
2436                 {
2437                   maybe_add_class_template_decl_list (current_class_type,
2438                                                       type, /*friend_p=*/0);
2439                   CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2440                 }
2441             }
2442         }
2443     }
2444
2445   return decl;
2446 }
2447
2448 /* In C++, you don't have to write `struct S' to refer to `S'; you
2449    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2450    if the user had written `typedef struct S S'.  Create and return
2451    the TYPE_DECL for TYPE.  */
2452
2453 tree
2454 create_implicit_typedef (tree name, tree type)
2455 {
2456   tree decl;
2457
2458   decl = build_decl (TYPE_DECL, name, type);
2459   DECL_ARTIFICIAL (decl) = 1;
2460   /* There are other implicit type declarations, like the one *within*
2461      a class that allows you to write `S::S'.  We must distinguish
2462      amongst these.  */
2463   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2464   TYPE_NAME (type) = decl;
2465
2466   return decl;
2467 }
2468
2469 /* Remember a local name for name-mangling purposes.  */
2470
2471 static void
2472 push_local_name (tree decl)
2473 {
2474   size_t i, nelts;
2475   tree t, name;
2476
2477   timevar_push (TV_NAME_LOOKUP);
2478   if (!local_names)
2479     VARRAY_TREE_INIT (local_names, 8, "local_names");
2480
2481   name = DECL_NAME (decl);
2482
2483   nelts = VARRAY_ACTIVE_SIZE (local_names);
2484   for (i = 0; i < nelts; i++)
2485     {
2486       t = VARRAY_TREE (local_names, i);
2487       if (DECL_NAME (t) == name)
2488         {
2489           if (!DECL_LANG_SPECIFIC (decl))
2490             retrofit_lang_decl (decl);
2491           DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2492           if (DECL_LANG_SPECIFIC (t))
2493             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2494           else
2495             DECL_DISCRIMINATOR (decl) = 1;
2496
2497           VARRAY_TREE (local_names, i) = decl;
2498           timevar_pop (TV_NAME_LOOKUP);
2499           return;
2500         }
2501     }
2502
2503   VARRAY_PUSH_TREE (local_names, decl);
2504   timevar_pop (TV_NAME_LOOKUP);
2505 }
2506
2507 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2508    Normally put it into the inner-most non-tag-transparent scope,
2509    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2510    The latter is needed for implicit declarations.  */
2511
2512 void
2513 pushtag (tree name, tree type, int globalize)
2514 {
2515   register struct cp_binding_level *b;
2516
2517   timevar_push (TV_NAME_LOOKUP);
2518   b = current_binding_level;
2519   while (b->tag_transparent
2520          || (b->parm_flag == 2
2521              && (globalize
2522                  /* We may be defining a new type in the initializer
2523                     of a static member variable. We allow this when
2524                     not pedantic, and it is particularly useful for
2525                     type punning via an anonymous union.  */
2526                  || COMPLETE_TYPE_P (b->this_class))))
2527     b = b->level_chain;
2528
2529   b->tags = tree_cons (name, type, b->tags);
2530
2531   if (name)
2532     {
2533       /* Do C++ gratuitous typedefing.  */
2534       if (IDENTIFIER_TYPE_VALUE (name) != type)
2535         {
2536           register tree d = NULL_TREE;
2537           int in_class = 0;
2538           tree context = TYPE_CONTEXT (type);
2539
2540           if (! context)
2541             {
2542               tree cs = current_scope ();
2543
2544               if (! globalize)
2545                 context = cs;
2546               else if (cs != NULL_TREE && TYPE_P (cs))
2547                 /* When declaring a friend class of a local class, we want
2548                    to inject the newly named class into the scope
2549                    containing the local class, not the namespace scope.  */
2550                 context = decl_function_context (get_type_decl (cs));
2551             }
2552           if (!context)
2553             context = current_namespace;
2554
2555           if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2556               || b->parm_flag == 2)
2557             in_class = 1;
2558
2559           if (current_lang_name == lang_name_java)
2560             TYPE_FOR_JAVA (type) = 1;
2561
2562           d = create_implicit_typedef (name, type);
2563           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2564           if (! in_class)
2565             set_identifier_type_value_with_scope (name, type, b);
2566
2567           d = maybe_process_template_type_declaration (type,
2568                                                        globalize, b);
2569
2570           if (b->parm_flag == 2)
2571             {
2572               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2573                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2574                    class.  But if it's a member template class, we
2575                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2576                    is done later.  */
2577                 finish_member_declaration (d);
2578               else
2579                 pushdecl_class_level (d);
2580             }
2581           else
2582             d = pushdecl_with_scope (d, b);
2583
2584           /* FIXME what if it gets a name from typedef?  */
2585           if (ANON_AGGRNAME_P (name))
2586             DECL_IGNORED_P (d) = 1;
2587
2588           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2589
2590           /* If this is a local class, keep track of it.  We need this
2591              information for name-mangling, and so that it is possible to find
2592              all function definitions in a translation unit in a convenient
2593              way.  (It's otherwise tricky to find a member function definition
2594              it's only pointed to from within a local class.)  */
2595           if (TYPE_CONTEXT (type)
2596               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2597               && !processing_template_decl)
2598             VARRAY_PUSH_TREE (local_classes, type);
2599         }
2600       if (b->parm_flag == 2)
2601         {
2602           if (!COMPLETE_TYPE_P (current_class_type))
2603             {
2604               maybe_add_class_template_decl_list (current_class_type,
2605                                                   type, /*friend_p=*/0);
2606               CLASSTYPE_TAGS (current_class_type) = b->tags;
2607             }
2608         }
2609     }
2610
2611   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2612     /* Use the canonical TYPE_DECL for this node.  */
2613     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2614   else
2615     {
2616       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2617          will be the tagged type we just added to the current
2618          binding level.  This fake NULL-named TYPE_DECL node helps
2619          dwarfout.c to know when it needs to output a
2620          representation of a tagged type, and it also gives us a
2621          convenient place to record the "scope start" address for
2622          the tagged type.  */
2623
2624       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2625       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2626     }
2627   timevar_pop (TV_NAME_LOOKUP);
2628 }
2629
2630 /* Counter used to create anonymous type names.  */
2631
2632 static GTY(()) int anon_cnt;
2633
2634 /* Return an IDENTIFIER which can be used as a name for
2635    anonymous structs and unions.  */
2636
2637 tree
2638 make_anon_name (void)
2639 {
2640   char buf[32];
2641
2642   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2643   return get_identifier (buf);
2644 }
2645
2646 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2647    This keeps dbxout from getting confused.  */
2648
2649 void
2650 clear_anon_tags (void)
2651 {
2652   register struct cp_binding_level *b;
2653   register tree tags;
2654   static int last_cnt = 0;
2655
2656   /* Fast out if no new anon names were declared.  */
2657   if (last_cnt == anon_cnt)
2658     return;
2659
2660   b = current_binding_level;
2661   while (b->tag_transparent)
2662     b = b->level_chain;
2663   tags = b->tags;
2664   while (tags)
2665     {
2666       /* A NULL purpose means we have already processed all tags
2667          from here to the end of the list.  */
2668       if (TREE_PURPOSE (tags) == NULL_TREE)
2669         break;
2670       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2671         TREE_PURPOSE (tags) = NULL_TREE;
2672       tags = TREE_CHAIN (tags);
2673     }
2674   last_cnt = anon_cnt;
2675 }
2676 \f
2677 /* Subroutine of duplicate_decls: return truthvalue of whether
2678    or not types of these decls match.
2679
2680    For C++, we must compare the parameter list so that `int' can match
2681    `int&' in a parameter position, but `int&' is not confused with
2682    `const int&'.  */
2683
2684 int
2685 decls_match (tree newdecl, tree olddecl)
2686 {
2687   int types_match;
2688
2689   if (newdecl == olddecl)
2690     return 1;
2691
2692   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2693     /* If the two DECLs are not even the same kind of thing, we're not
2694        interested in their types.  */
2695     return 0;
2696
2697   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2698     {
2699       tree f1 = TREE_TYPE (newdecl);
2700       tree f2 = TREE_TYPE (olddecl);
2701       tree p1 = TYPE_ARG_TYPES (f1);
2702       tree p2 = TYPE_ARG_TYPES (f2);
2703
2704       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2705           && ! (DECL_EXTERN_C_P (newdecl)
2706                 && DECL_EXTERN_C_P (olddecl)))
2707         return 0;
2708
2709       if (TREE_CODE (f1) != TREE_CODE (f2))
2710         return 0;
2711
2712       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2713         {
2714           if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2715               && (DECL_BUILT_IN (olddecl)
2716 #ifndef NO_IMPLICIT_EXTERN_C
2717                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2718                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2719 #endif
2720               ))
2721             {
2722               types_match = self_promoting_args_p (p1);
2723               if (p1 == void_list_node)
2724                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2725             }
2726 #ifndef NO_IMPLICIT_EXTERN_C
2727           else if (p1 == NULL_TREE
2728                    && (DECL_EXTERN_C_P (olddecl)
2729                        && DECL_IN_SYSTEM_HEADER (olddecl)
2730                        && !DECL_CLASS_SCOPE_P (olddecl))
2731                    && (DECL_EXTERN_C_P (newdecl)
2732                        && DECL_IN_SYSTEM_HEADER (newdecl)
2733                        && !DECL_CLASS_SCOPE_P (newdecl)))
2734             {
2735               types_match = self_promoting_args_p (p2);
2736               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2737             }
2738 #endif
2739           else
2740             types_match = compparms (p1, p2);
2741         }
2742       else
2743         types_match = 0;
2744     }
2745   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2746     {
2747       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2748                                 DECL_TEMPLATE_PARMS (olddecl)))
2749         return 0;
2750
2751       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2752           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2753         return 0;
2754
2755       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2756         types_match = 1;
2757       else
2758         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2759                                    DECL_TEMPLATE_RESULT (newdecl));
2760     }
2761   else
2762     {
2763       if (TREE_TYPE (newdecl) == error_mark_node)
2764         types_match = TREE_TYPE (olddecl) == error_mark_node;
2765       else if (TREE_TYPE (olddecl) == NULL_TREE)
2766         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2767       else if (TREE_TYPE (newdecl) == NULL_TREE)
2768         types_match = 0;
2769       else
2770         types_match = comptypes (TREE_TYPE (newdecl),
2771                                  TREE_TYPE (olddecl),
2772                                  COMPARE_REDECLARATION);
2773     }
2774
2775   return types_match;
2776 }
2777
2778 /* If NEWDECL is `static' and an `extern' was seen previously,
2779    warn about it.  OLDDECL is the previous declaration.
2780
2781    Note that this does not apply to the C++ case of declaring
2782    a variable `extern const' and then later `const'.
2783
2784    Don't complain about built-in functions, since they are beyond
2785    the user's control.  */
2786
2787 static void
2788 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2789 {
2790   static const char *const explicit_extern_static_warning
2791     = "`%D' was declared `extern' and later `static'";
2792   static const char *const implicit_extern_static_warning
2793     = "`%D' was declared implicitly `extern' and later `static'";
2794
2795   tree name;
2796
2797   if (TREE_CODE (newdecl) == TYPE_DECL
2798       || TREE_CODE (newdecl) == TEMPLATE_DECL
2799       || TREE_CODE (newdecl) == CONST_DECL)
2800     return;
2801
2802   /* Don't get confused by static member functions; that's a different
2803      use of `static'.  */
2804   if (TREE_CODE (newdecl) == FUNCTION_DECL
2805       && DECL_STATIC_FUNCTION_P (newdecl))
2806     return;
2807
2808   /* If the old declaration was `static', or the new one isn't, then
2809      then everything is OK.  */
2810   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2811     return;
2812
2813   /* It's OK to declare a builtin function as `static'.  */
2814   if (TREE_CODE (olddecl) == FUNCTION_DECL
2815       && DECL_ARTIFICIAL (olddecl))
2816     return;
2817
2818   name = DECL_ASSEMBLER_NAME (newdecl);
2819   pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2820               ? implicit_extern_static_warning
2821               : explicit_extern_static_warning, newdecl);
2822   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2823 }
2824
2825 /* Handle when a new declaration NEWDECL has the same name as an old
2826    one OLDDECL in the same binding contour.  Prints an error message
2827    if appropriate.
2828
2829    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2830    Otherwise, return 0.  */
2831
2832 int
2833 duplicate_decls (tree newdecl, tree olddecl)
2834 {
2835   unsigned olddecl_uid = DECL_UID (olddecl);
2836   int olddecl_friend = 0, types_match = 0;
2837   int new_defines_function = 0;
2838
2839   if (newdecl == olddecl)
2840     return 1;
2841
2842   types_match = decls_match (newdecl, olddecl);
2843
2844   /* If either the type of the new decl or the type of the old decl is an
2845      error_mark_node, then that implies that we have already issued an
2846      error (earlier) for some bogus type specification, and in that case,
2847      it is rather pointless to harass the user with yet more error message
2848      about the same declaration, so just pretend the types match here.  */
2849   if (TREE_TYPE (newdecl) == error_mark_node
2850       || TREE_TYPE (olddecl) == error_mark_node)
2851     types_match = 1;
2852
2853   if (DECL_P (olddecl)
2854       && TREE_CODE (newdecl) == FUNCTION_DECL
2855       && TREE_CODE (olddecl) == FUNCTION_DECL
2856       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2857     {
2858       if (DECL_DECLARED_INLINE_P (newdecl)
2859           && DECL_UNINLINABLE (newdecl)
2860           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2861         /* Already warned elsewhere.  */;
2862       else if (DECL_DECLARED_INLINE_P (olddecl)
2863                && DECL_UNINLINABLE (olddecl)
2864                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2865         /* Already warned.  */;
2866       else if (DECL_DECLARED_INLINE_P (newdecl)
2867                && DECL_UNINLINABLE (olddecl)
2868                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2869         {
2870           warning ("%Hfunction '%D' redeclared as inline",
2871                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2872           warning ("%Hprevious declaration of '%D' with attribute noinline",
2873                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2874         }
2875       else if (DECL_DECLARED_INLINE_P (olddecl)
2876                && DECL_UNINLINABLE (newdecl)
2877                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2878         {
2879           warning ("%Hfunction '%D' redeclared with attribute noinline",
2880                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2881           warning ("%Hprevious declaration of '%D' was inline",
2882                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2883         }
2884     }
2885
2886   /* Check for redeclaration and other discrepancies.  */
2887   if (TREE_CODE (olddecl) == FUNCTION_DECL
2888       && DECL_ARTIFICIAL (olddecl))
2889     {
2890       if (TREE_CODE (newdecl) != FUNCTION_DECL)
2891         {
2892           /* Avoid warnings redeclaring anticipated built-ins.  */
2893           if (DECL_ANTICIPATED (olddecl))
2894             return 0;
2895
2896           /* If you declare a built-in or predefined function name as static,
2897              the old definition is overridden, but optionally warn this was a
2898              bad choice of name.  */
2899           if (! TREE_PUBLIC (newdecl))
2900             {
2901               if (warn_shadow)
2902                 warning ("shadowing %s function `%#D'",
2903                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2904                             olddecl);
2905               /* Discard the old built-in function.  */
2906               return 0;
2907             }
2908           /* If the built-in is not ansi, then programs can override
2909              it even globally without an error.  */
2910           else if (! DECL_BUILT_IN (olddecl))
2911             warning ("library function `%#D' redeclared as non-function `%#D'",
2912                         olddecl, newdecl);
2913           else
2914             {
2915               error ("declaration of `%#D'", newdecl);
2916               error ("conflicts with built-in declaration `%#D'",
2917                         olddecl);
2918             }
2919           return 0;
2920         }
2921       else if (!types_match)
2922         {
2923           /* Avoid warnings redeclaring anticipated built-ins.  */
2924           if (DECL_ANTICIPATED (olddecl))
2925             ;  /* Do nothing yet.  */
2926           else if ((DECL_EXTERN_C_P (newdecl)
2927                && DECL_EXTERN_C_P (olddecl))
2928               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2929                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2930             {
2931               /* A near match; override the builtin.  */
2932
2933               if (TREE_PUBLIC (newdecl))
2934                 {
2935                   warning ("new declaration `%#D'", newdecl);
2936                   warning ("ambiguates built-in declaration `%#D'",
2937                               olddecl);
2938                 }
2939               else if (warn_shadow)
2940                 warning ("shadowing %s function `%#D'",
2941                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2942                             olddecl);
2943             }
2944           else
2945             /* Discard the old built-in function.  */
2946             return 0;
2947
2948           /* Replace the old RTL to avoid problems with inlining.  */
2949           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2950         }
2951       /* Even if the types match, prefer the new declarations type
2952          for anticipated built-ins, for exception lists, etc...  */
2953       else if (DECL_ANTICIPATED (olddecl))
2954         TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2955
2956       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2957         {
2958           /* If a builtin function is redeclared as `static', merge
2959              the declarations, but make the original one static.  */
2960           DECL_THIS_STATIC (olddecl) = 1;
2961           TREE_PUBLIC (olddecl) = 0;
2962
2963           /* Make the old declaration consistent with the new one so
2964              that all remnants of the builtin-ness of this function
2965              will be banished.  */
2966           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2967           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2968         }
2969     }
2970   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2971     {
2972       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2973            && TREE_CODE (newdecl) != TYPE_DECL
2974            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2975                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2976           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2977               && TREE_CODE (olddecl) != TYPE_DECL
2978               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2979                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2980                         == TYPE_DECL))))
2981         {
2982           /* We do nothing special here, because C++ does such nasty
2983              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
2984              get shadowed, and know that if we need to find a TYPE_DECL
2985              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2986              slot of the identifier.  */
2987           return 0;
2988         }
2989
2990       if ((TREE_CODE (newdecl) == FUNCTION_DECL
2991            && DECL_FUNCTION_TEMPLATE_P (olddecl))
2992           || (TREE_CODE (olddecl) == FUNCTION_DECL
2993               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
2994         return 0;
2995
2996       error ("`%#D' redeclared as different kind of symbol", newdecl);
2997       if (TREE_CODE (olddecl) == TREE_LIST)
2998         olddecl = TREE_VALUE (olddecl);
2999       cp_error_at ("previous declaration of `%#D'", olddecl);
3000
3001       /* New decl is completely inconsistent with the old one =>
3002          tell caller to replace the old one.  */
3003
3004       return 0;
3005     }
3006   else if (!types_match)
3007     {
3008       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3009         /* These are certainly not duplicate declarations; they're
3010            from different scopes.  */
3011         return 0;
3012
3013       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3014         {
3015           /* The name of a class template may not be declared to refer to
3016              any other template, class, function, object, namespace, value,
3017              or type in the same scope.  */
3018           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3019               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3020             {
3021               error ("declaration of template `%#D'", newdecl);
3022               cp_error_at ("conflicts with previous declaration `%#D'",
3023                            olddecl);
3024             }
3025           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3026                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3027                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3028                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3029                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3030                                            DECL_TEMPLATE_PARMS (olddecl))
3031                    /* Template functions can be disambiguated by
3032                       return type.  */
3033                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3034                                    TREE_TYPE (TREE_TYPE (olddecl))))
3035             {
3036               error ("new declaration `%#D'", newdecl);
3037               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3038             }
3039           return 0;
3040         }
3041       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3042         {
3043           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3044             {
3045               error ("declaration of C function `%#D' conflicts with",
3046                         newdecl);
3047               cp_error_at ("previous declaration `%#D' here", olddecl);
3048             }
3049           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3050                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3051             {
3052               error ("new declaration `%#D'", newdecl);
3053               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3054             }
3055           else
3056             return 0;
3057         }
3058
3059       /* Already complained about this, so don't do so again.  */
3060       else if (current_class_type == NULL_TREE
3061           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3062         {
3063           error ("conflicting types for `%#D'", newdecl);
3064           cp_error_at ("previous declaration as `%#D'", olddecl);
3065         }
3066     }
3067   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3068             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3069                  && (!DECL_TEMPLATE_INFO (newdecl)
3070                      || (DECL_TI_TEMPLATE (newdecl)
3071                          != DECL_TI_TEMPLATE (olddecl))))
3072                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3073                     && (!DECL_TEMPLATE_INFO (olddecl)
3074                         || (DECL_TI_TEMPLATE (olddecl)
3075                             != DECL_TI_TEMPLATE (newdecl))))))
3076     /* It's OK to have a template specialization and a non-template
3077        with the same type, or to have specializations of two
3078        different templates with the same type.  Note that if one is a
3079        specialization, and the other is an instantiation of the same
3080        template, that we do not exit at this point.  That situation
3081        can occur if we instantiate a template class, and then
3082        specialize one of its methods.  This situation is valid, but
3083        the declarations must be merged in the usual way.  */
3084     return 0;
3085   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3086            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3087                 && !DECL_USE_TEMPLATE (newdecl))
3088                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3089                    && !DECL_USE_TEMPLATE (olddecl))))
3090     /* One of the declarations is a template instantiation, and the
3091        other is not a template at all.  That's OK.  */
3092     return 0;
3093   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3094            && DECL_NAMESPACE_ALIAS (newdecl)
3095            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3096     /* Redeclaration of namespace alias, ignore it.  */
3097     return 1;
3098   else
3099     {
3100       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3101       if (errmsg)
3102         {
3103           error (errmsg, newdecl);
3104           if (DECL_NAME (olddecl) != NULL_TREE)
3105             cp_error_at ((DECL_INITIAL (olddecl)
3106                           && namespace_bindings_p ())
3107                          ? "`%#D' previously defined here"
3108                          : "`%#D' previously declared here", olddecl);
3109           return 0;
3110         }
3111       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3112                && DECL_INITIAL (olddecl) != NULL_TREE
3113                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3114                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3115         {
3116           /* Prototype decl follows defn w/o prototype.  */
3117           cp_warning_at ("prototype for `%#D'", newdecl);
3118           cp_warning_at ("follows non-prototype definition here", olddecl);
3119         }
3120       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3121                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3122         {
3123           /* extern "C" int foo ();
3124              int foo () { bar (); }
3125              is OK.  */
3126           if (current_lang_depth () == 0)
3127             SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3128           else
3129             {
3130               cp_error_at ("previous declaration of `%#D' with %L linkage",
3131                            olddecl, DECL_LANGUAGE (olddecl));
3132               error ("conflicts with new declaration with %L linkage",
3133                         DECL_LANGUAGE (newdecl));
3134             }
3135         }
3136
3137       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3138         ;
3139       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3140         {
3141           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3142           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3143           int i = 1;
3144
3145           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3146             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3147
3148           for (; t1 && t1 != void_list_node;
3149                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3150             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3151               {
3152                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3153                                            TREE_PURPOSE (t2)))
3154                   {
3155                     pedwarn ("default argument given for parameter %d of `%#D'",
3156                              i, newdecl);
3157                     cp_pedwarn_at ("after previous specification in `%#D'",
3158                                    olddecl);
3159                   }
3160                 else
3161                   {
3162                     error ("default argument given for parameter %d of `%#D'",
3163                               i, newdecl);
3164                     cp_error_at ("after previous specification in `%#D'",
3165                                  olddecl);
3166                   }
3167               }
3168
3169           if (DECL_DECLARED_INLINE_P (newdecl) 
3170               && ! DECL_DECLARED_INLINE_P (olddecl)
3171               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3172             {
3173               warning ("`%#D' was used before it was declared inline",
3174                           newdecl);
3175               cp_warning_at ("previous non-inline declaration here",
3176                              olddecl);
3177             }
3178         }
3179     }
3180
3181   /* Do not merge an implicit typedef with an explicit one.  In:
3182
3183        class A;
3184        ...
3185        typedef class A A __attribute__ ((foo));
3186
3187      the attribute should apply only to the typedef.  */
3188   if (TREE_CODE (olddecl) == TYPE_DECL
3189       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3190           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3191     return 0;
3192
3193   /* If new decl is `static' and an `extern' was seen previously,
3194      warn about it.  */
3195   warn_extern_redeclared_static (newdecl, olddecl);
3196
3197   /* We have committed to returning 1 at this point.  */
3198   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3199     {
3200       /* Now that functions must hold information normally held
3201          by field decls, there is extra work to do so that
3202          declaration information does not get destroyed during
3203          definition.  */
3204       if (DECL_VINDEX (olddecl))
3205         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3206       if (DECL_CONTEXT (olddecl))
3207         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3208       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3209       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3210       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3211       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3212       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3213       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3214       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3215         SET_OVERLOADED_OPERATOR_CODE
3216           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3217       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3218
3219       /* Optionally warn about more than one declaration for the same
3220          name, but don't warn about a function declaration followed by a
3221          definition.  */
3222       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3223           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3224           /* Don't warn about extern decl followed by definition.  */
3225           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3226           /* Don't warn about friends, let add_friend take care of it.  */
3227           && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3228         {
3229           warning ("redundant redeclaration of `%D' in same scope", newdecl);
3230           cp_warning_at ("previous declaration of `%D'", olddecl);
3231         }
3232     }
3233
3234   /* Deal with C++: must preserve virtual function table size.  */
3235   if (TREE_CODE (olddecl) == TYPE_DECL)
3236     {
3237       register tree newtype = TREE_TYPE (newdecl);
3238       register tree oldtype = TREE_TYPE (olddecl);
3239
3240       if (newtype != error_mark_node && oldtype != error_mark_node
3241           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3242         CLASSTYPE_FRIEND_CLASSES (newtype)
3243           = CLASSTYPE_FRIEND_CLASSES (oldtype);
3244
3245       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3246     }
3247
3248   /* Copy all the DECL_... slots specified in the new decl
3249      except for any that we copy here from the old type.  */
3250   DECL_ATTRIBUTES (newdecl)
3251     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3252
3253   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3254     {
3255       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3256       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3257         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3258                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3259
3260       /* If the new declaration is a definition, update the file and
3261          line information on the declaration.  */
3262       if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3263           && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3264         {
3265           DECL_SOURCE_LOCATION (olddecl) 
3266             = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3267             = DECL_SOURCE_LOCATION (newdecl);
3268         }
3269
3270       return 1;
3271     }
3272
3273   if (types_match)
3274     {
3275       /* Automatically handles default parameters.  */
3276       tree oldtype = TREE_TYPE (olddecl);
3277       tree newtype;
3278
3279       /* Merge the data types specified in the two decls.  */
3280       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3281
3282       /* If merge_types produces a non-typedef type, just use the old type.  */
3283       if (TREE_CODE (newdecl) == TYPE_DECL
3284           && newtype == DECL_ORIGINAL_TYPE (newdecl))
3285         newtype = oldtype;
3286
3287       if (TREE_CODE (newdecl) == VAR_DECL)
3288         {
3289           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3290           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3291         }
3292
3293       /* Do this after calling `merge_types' so that default
3294          parameters don't confuse us.  */
3295       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3296           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3297               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3298         {
3299           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3300                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3301           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3302                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3303
3304           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3305               && DECL_SOURCE_LINE (olddecl) != 0
3306               && flag_exceptions
3307               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3308                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3309             {
3310               error ("declaration of `%F' throws different exceptions",
3311                         newdecl);
3312               cp_error_at ("than previous declaration `%F'", olddecl);
3313             }
3314         }
3315       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3316
3317       /* Lay the type out, unless already done.  */
3318       if (! same_type_p (newtype, oldtype)
3319           && TREE_TYPE (newdecl) != error_mark_node
3320           && !(processing_template_decl && uses_template_parms (newdecl)))
3321         layout_type (TREE_TYPE (newdecl));
3322
3323       if ((TREE_CODE (newdecl) == VAR_DECL
3324            || TREE_CODE (newdecl) == PARM_DECL
3325            || TREE_CODE (newdecl) == RESULT_DECL
3326            || TREE_CODE (newdecl) == FIELD_DECL
3327            || TREE_CODE (newdecl) == TYPE_DECL)
3328           && !(processing_template_decl && uses_template_parms (newdecl)))
3329         layout_decl (newdecl, 0);
3330
3331       /* Merge the type qualifiers.  */
3332       if (TREE_READONLY (newdecl))
3333         TREE_READONLY (olddecl) = 1;
3334       if (TREE_THIS_VOLATILE (newdecl))
3335         TREE_THIS_VOLATILE (olddecl) = 1;
3336
3337       /* Merge the initialization information.  */
3338       if (DECL_INITIAL (newdecl) == NULL_TREE
3339           && DECL_INITIAL (olddecl) != NULL_TREE)
3340         {
3341           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3342           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3343           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3344               && DECL_LANG_SPECIFIC (newdecl)
3345               && DECL_LANG_SPECIFIC (olddecl))
3346             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3347         }
3348
3349       /* Merge the section attribute.
3350          We want to issue an error if the sections conflict but that must be
3351          done later in decl_attributes since we are called before attributes
3352          are assigned.  */
3353       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3354         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3355
3356       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3357         {
3358           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3359             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3360           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3361           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3362           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3363           TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3364           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3365           DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3366           /* Keep the old RTL.  */
3367           COPY_DECL_RTL (olddecl, newdecl);
3368         }
3369       else if (TREE_CODE (newdecl) == VAR_DECL 
3370                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3371         {
3372           /* Keep the old RTL.  We cannot keep the old RTL if the old
3373              declaration was for an incomplete object and the new
3374              declaration is not since many attributes of the RTL will
3375              change.  */
3376           COPY_DECL_RTL (olddecl, newdecl);
3377         }
3378     }
3379   /* If cannot merge, then use the new type and qualifiers,
3380      and don't preserve the old rtl.  */
3381   else
3382     {
3383       /* Clean out any memory we had of the old declaration.  */
3384       tree oldstatic = value_member (olddecl, static_aggregates);
3385       if (oldstatic)
3386         TREE_VALUE (oldstatic) = error_mark_node;
3387
3388       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3389       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3390       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3391       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3392     }
3393
3394   /* Merge the storage class information.  */
3395   merge_weak (newdecl, olddecl);
3396
3397   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3398   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3399   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3400   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3401   if (! DECL_EXTERNAL (olddecl))
3402     DECL_EXTERNAL (newdecl) = 0;
3403
3404   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3405     {
3406       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3407       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3408       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3409       DECL_TEMPLATE_INSTANTIATED (newdecl)
3410         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3411       /* Don't really know how much of the language-specific
3412          values we should copy from old to new.  */
3413       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3414       DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 
3415         DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3416       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3417       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3418       DECL_INITIALIZED_IN_CLASS_P (newdecl)
3419         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3420       olddecl_friend = DECL_FRIEND_P (olddecl);
3421
3422       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3423       if (TREE_CODE (newdecl) == FUNCTION_DECL
3424           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3425         {
3426           DECL_BEFRIENDING_CLASSES (newdecl)
3427             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3428                        DECL_BEFRIENDING_CLASSES (olddecl));
3429           /* DECL_THUNKS is only valid for virtual functions,
3430              otherwise it is a DECL_FRIEND_CONTEXT.  */
3431           if (DECL_VIRTUAL_P (newdecl))
3432             DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3433         }
3434     }
3435
3436   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3437     {
3438       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3439           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3440         {
3441           /* If newdecl is not a specialization, then it is not a
3442              template-related function at all.  And that means that we
3443              should have exited above, returning 0.  */
3444           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3445                               0);
3446
3447           if (TREE_USED (olddecl))
3448             /* From [temp.expl.spec]:
3449
3450                If a template, a member template or the member of a class
3451                template is explicitly specialized then that
3452                specialization shall be declared before the first use of
3453                that specialization that would cause an implicit
3454                instantiation to take place, in every translation unit in
3455                which such a use occurs.  */
3456             error ("explicit specialization of %D after first use",
3457                       olddecl);
3458
3459           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3460
3461           /* [temp.expl.spec/14] We don't inline explicit specialization
3462              just because the primary template says so.  */
3463         }
3464       else
3465         {
3466           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3467             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3468
3469           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3470
3471           /* If either decl says `inline', this fn is inline, unless 
3472              its definition was passed already.  */
3473           if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3474             DECL_INLINE (olddecl) = 1;
3475           DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3476
3477           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3478             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3479         }
3480
3481       /* Preserve abstractness on cloned [cd]tors.  */
3482       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3483
3484       if (! types_match)
3485         {
3486           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3487           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3488           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3489         }
3490       if (! types_match || new_defines_function)
3491         {
3492           /* These need to be copied so that the names are available.
3493              Note that if the types do match, we'll preserve inline
3494              info and other bits, but if not, we won't.  */
3495           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3496           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3497         }
3498       if (new_defines_function)
3499         /* If defining a function declared with other language
3500            linkage, use the previously declared language linkage.  */
3501         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3502       else if (types_match)
3503         {
3504           /* If redeclaring a builtin function, and not a definition,
3505              it stays built in.  */
3506           if (DECL_BUILT_IN (olddecl))
3507             {
3508               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3509               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3510               /* If we're keeping the built-in definition, keep the rtl,
3511                  regardless of declaration matches.  */
3512               SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3513             }
3514           else
3515             DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3516
3517           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3518           /* Don't clear out the arguments if we're redefining a function.  */
3519           if (DECL_ARGUMENTS (olddecl))
3520             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3521         }
3522     }
3523   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3524     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3525
3526   /* Now preserve various other info from the definition.  */
3527   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3528   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3529   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3530   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3531
3532   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3533     {
3534       int function_size;
3535
3536       function_size = sizeof (struct tree_decl);
3537
3538       memcpy ((char *) olddecl + sizeof (struct tree_common),
3539               (char *) newdecl + sizeof (struct tree_common),
3540               function_size - sizeof (struct tree_common));
3541
3542       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3543         {
3544           /* If newdecl is a template instantiation, it is possible that
3545              the following sequence of events has occurred:
3546
3547              o A friend function was declared in a class template.  The
3548              class template was instantiated.
3549
3550              o The instantiation of the friend declaration was
3551              recorded on the instantiation list, and is newdecl.
3552
3553              o Later, however, instantiate_class_template called pushdecl
3554              on the newdecl to perform name injection.  But, pushdecl in
3555              turn called duplicate_decls when it discovered that another
3556              declaration of a global function with the same name already
3557              existed.
3558
3559              o Here, in duplicate_decls, we decided to clobber newdecl.
3560
3561              If we're going to do that, we'd better make sure that
3562              olddecl, and not newdecl, is on the list of
3563              instantiations so that if we try to do the instantiation
3564              again we won't get the clobbered declaration.  */
3565
3566           tree tmpl = DECL_TI_TEMPLATE (newdecl);
3567           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3568
3569           for (; decls; decls = TREE_CHAIN (decls))
3570             if (TREE_VALUE (decls) == newdecl)
3571               TREE_VALUE (decls) = olddecl;
3572         }
3573     }
3574   else
3575     {
3576       memcpy ((char *) olddecl + sizeof (struct tree_common),
3577               (char *) newdecl + sizeof (struct tree_common),
3578               sizeof (struct tree_decl) - sizeof (struct tree_common)
3579               + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3580     }
3581
3582   DECL_UID (olddecl) = olddecl_uid;
3583   if (olddecl_friend)
3584     DECL_FRIEND_P (olddecl) = 1;
3585
3586   /* NEWDECL contains the merged attribute lists.
3587      Update OLDDECL to be the same.  */
3588   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3589
3590   return 1;
3591 }
3592
3593 /* Record a decl-node X as belonging to the current lexical scope.
3594    Check for errors (such as an incompatible declaration for the same
3595    name already seen in the same scope).
3596
3597    Returns either X or an old decl for the same name.
3598    If an old decl is returned, it may have been smashed
3599    to agree with what X says.  */
3600
3601 tree
3602 pushdecl (tree x)
3603 {
3604   register tree t;
3605   register tree name;
3606   int need_new_binding;
3607
3608   timevar_push (TV_NAME_LOOKUP);
3609   /* We shouldn't be calling pushdecl when we're generating RTL for a
3610      function that we already did semantic analysis on previously.  */
3611   my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3612                       19990913);
3613
3614   need_new_binding = 1;
3615
3616   if (DECL_TEMPLATE_PARM_P (x))
3617     /* Template parameters have no context; they are not X::T even
3618        when declared within a class or namespace.  */
3619     ;
3620   else
3621     {
3622       if (current_function_decl && x != current_function_decl
3623           /* A local declaration for a function doesn't constitute
3624              nesting.  */
3625           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3626           /* A local declaration for an `extern' variable is in the
3627              scope of the current namespace, not the current
3628              function.  */
3629           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3630           && !DECL_CONTEXT (x))
3631         DECL_CONTEXT (x) = current_function_decl;
3632
3633       /* If this is the declaration for a namespace-scope function,
3634          but the declaration itself is in a local scope, mark the
3635          declaration.  */
3636       if (TREE_CODE (x) == FUNCTION_DECL
3637           && DECL_NAMESPACE_SCOPE_P (x)
3638           && current_function_decl
3639           && x != current_function_decl)
3640         DECL_LOCAL_FUNCTION_P (x) = 1;
3641     }
3642
3643   name = DECL_NAME (x);
3644   if (name)
3645     {
3646       int different_binding_level = 0;
3647
3648       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3649         name = TREE_OPERAND (name, 0);
3650
3651       /* In case this decl was explicitly namespace-qualified, look it
3652          up in its namespace context.  */
3653       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3654           && namespace_bindings_p ())
3655         t = namespace_binding (name, DECL_CONTEXT (x));
3656       else
3657         t = lookup_name_current_level (name);
3658
3659       /* [basic.link] If there is a visible declaration of an entity
3660          with linkage having the same name and type, ignoring entities
3661          declared outside the innermost enclosing namespace scope, the
3662          block scope declaration declares that same entity and
3663          receives the linkage of the previous declaration.  */
3664       if (! t && current_function_decl && x != current_function_decl
3665           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3666           && DECL_EXTERNAL (x))
3667         {
3668           /* Look in block scope.  */
3669           t = IDENTIFIER_VALUE (name);
3670           /* Or in the innermost namespace.  */
3671           if (! t)
3672             t = namespace_binding (name, DECL_CONTEXT (x));
3673           /* Does it have linkage?  Note that if this isn't a DECL, it's an
3674              OVERLOAD, which is OK.  */
3675           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3676             t = NULL_TREE;
3677           if (t)
3678             different_binding_level = 1;
3679         }
3680
3681       /* If we are declaring a function, and the result of name-lookup
3682          was an OVERLOAD, look for an overloaded instance that is
3683          actually the same as the function we are declaring.  (If
3684          there is one, we have to merge our declaration with the
3685          previous declaration.)  */
3686       if (t && TREE_CODE (t) == OVERLOAD)
3687         {
3688           tree match;
3689
3690           if (TREE_CODE (x) == FUNCTION_DECL)
3691             for (match = t; match; match = OVL_NEXT (match))
3692               {
3693                 if (decls_match (OVL_CURRENT (match), x))
3694                   break;
3695               }
3696           else
3697             /* Just choose one.  */
3698             match = t;
3699
3700           if (match)
3701             t = OVL_CURRENT (match);
3702           else
3703             t = NULL_TREE;
3704         }
3705
3706       if (t == error_mark_node)
3707         {
3708           /* error_mark_node is 0 for a while during initialization!  */
3709           t = NULL_TREE;
3710           cp_error_at ("`%#D' used prior to declaration", x);
3711         }
3712       else if (t != NULL_TREE)
3713         {
3714           if (different_binding_level)
3715             {
3716               if (decls_match (x, t))
3717                 /* The standard only says that the local extern
3718                    inherits linkage from the previous decl; in
3719                    particular, default args are not shared.  It would
3720                    be nice to propagate inlining info, though.  FIXME.  */
3721                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3722             }
3723           else if (TREE_CODE (t) == PARM_DECL)
3724             {
3725               if (DECL_CONTEXT (t) == NULL_TREE)
3726                 /* This is probaby caused by too many errors, but calling
3727                    abort will say that if errors have occurred.  */
3728                 abort ();
3729
3730               /* Check for duplicate params.  */
3731               if (duplicate_decls (x, t))
3732                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3733             }
3734           else if ((DECL_EXTERN_C_FUNCTION_P (x)
3735                     || DECL_FUNCTION_TEMPLATE_P (x))
3736                    && is_overloaded_fn (t))
3737             /* Don't do anything just yet.  */;
3738           else if (t == wchar_decl_node)
3739             {
3740               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3741                 pedwarn ("redeclaration of `wchar_t' as `%T'",
3742                             TREE_TYPE (x));
3743
3744               /* Throw away the redeclaration.  */
3745               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3746             }
3747           else if (TREE_CODE (t) != TREE_CODE (x))
3748             {
3749               if (duplicate_decls (x, t))
3750                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3751             }
3752           else if (duplicate_decls (x, t))
3753             {
3754               if (TREE_CODE (t) == TYPE_DECL)
3755                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3756               else if (TREE_CODE (t) == FUNCTION_DECL)
3757                 check_default_args (t);
3758
3759               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3760             }
3761           else if (DECL_MAIN_P (x))
3762             {
3763               /* A redeclaration of main, but not a duplicate of the
3764                  previous one.
3765
3766                  [basic.start.main]
3767
3768                  This function shall not be overloaded.  */
3769               cp_error_at ("invalid redeclaration of `%D'", t);
3770               error ("as `%D'", x);
3771               /* We don't try to push this declaration since that
3772                  causes a crash.  */
3773               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3774             }
3775         }
3776
3777       check_template_shadow (x);
3778
3779       /* If this is a function conjured up by the backend, massage it
3780          so it looks friendly.  */
3781       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3782         {
3783           retrofit_lang_decl (x);
3784           SET_DECL_LANGUAGE (x, lang_c);
3785         }
3786
3787       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3788         {
3789           t = push_overloaded_decl (x, PUSH_LOCAL);
3790           if (t != x)
3791             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3792           if (!namespace_bindings_p ())
3793             /* We do not need to create a binding for this name;
3794                push_overloaded_decl will have already done so if
3795                necessary.  */
3796             need_new_binding = 0;
3797         }
3798       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3799         {
3800           t = push_overloaded_decl (x, PUSH_GLOBAL);
3801           if (t == x)
3802             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3803           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3804         }
3805
3806       /* If declaring a type as a typedef, copy the type (unless we're
3807          at line 0), and install this TYPE_DECL as the new type's typedef
3808          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
3809       if (TREE_CODE (x) == TYPE_DECL)
3810         {
3811           tree type = TREE_TYPE (x);
3812           if (DECL_SOURCE_LINE (x) == 0)
3813             {
3814               if (TYPE_NAME (type) == 0)
3815                 TYPE_NAME (type) = x;
3816             }
3817           else if (type != error_mark_node && TYPE_NAME (type) != x
3818                    /* We don't want to copy the type when all we're
3819                       doing is making a TYPE_DECL for the purposes of
3820                       inlining.  */
3821                    && (!TYPE_NAME (type)
3822                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3823             {
3824               DECL_ORIGINAL_TYPE (x) = type;
3825               type = build_type_copy (type);
3826               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3827               TYPE_NAME (type) = x;
3828               TREE_TYPE (x) = type;
3829             }
3830
3831           if (type != error_mark_node
3832               && TYPE_NAME (type)
3833               && TYPE_IDENTIFIER (type))
3834             set_identifier_type_value_with_scope (DECL_NAME (x), type,
3835                                                   current_binding_level);
3836
3837         }
3838
3839       /* Multiple external decls of the same identifier ought to match.
3840
3841          We get warnings about inline functions where they are defined.
3842          We get warnings about other functions from push_overloaded_decl.
3843
3844          Avoid duplicate warnings where they are used.  */
3845       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3846         {
3847           tree decl;
3848
3849           decl = IDENTIFIER_NAMESPACE_VALUE (name);
3850           if (decl && TREE_CODE (decl) == OVERLOAD)
3851             decl = OVL_FUNCTION (decl);
3852
3853           if (decl && decl != error_mark_node
3854               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3855               /* If different sort of thing, we already gave an error.  */
3856               && TREE_CODE (decl) == TREE_CODE (x)
3857               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3858             {
3859               pedwarn ("type mismatch with previous external decl", x);
3860               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3861             }
3862         }
3863
3864       /* This name is new in its binding level.
3865          Install the new declaration and return it.  */
3866       if (namespace_bindings_p ())
3867         {
3868           /* Install a global value.  */
3869
3870           /* If the first global decl has external linkage,
3871              warn if we later see static one.  */
3872           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3873             TREE_PUBLIC (name) = 1;
3874
3875           /* Bind the name for the entity.  */
3876           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3877                 && t != NULL_TREE)
3878               && (TREE_CODE (x) == TYPE_DECL
3879                   || TREE_CODE (x) == VAR_DECL
3880                   || TREE_CODE (x) == ALIAS_DECL
3881                   || TREE_CODE (x) == NAMESPACE_DECL
3882                   || TREE_CODE (x) == CONST_DECL
3883                   || TREE_CODE (x) == TEMPLATE_DECL))
3884             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3885
3886           /* Don't forget if the function was used via an implicit decl.  */
3887           if (IDENTIFIER_IMPLICIT_DECL (name)
3888               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3889             TREE_USED (x) = 1;
3890
3891           /* Don't forget if its address was taken in that way.  */
3892           if (IDENTIFIER_IMPLICIT_DECL (name)
3893               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3894             TREE_ADDRESSABLE (x) = 1;
3895
3896           /* Warn about mismatches against previous implicit decl.  */
3897           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3898               /* If this real decl matches the implicit, don't complain.  */
3899               && ! (TREE_CODE (x) == FUNCTION_DECL
3900                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3901             warning
3902               ("`%D' was previously implicitly declared to return `int'", x);
3903
3904           /* If new decl is `static' and an `extern' was seen previously,
3905              warn about it.  */
3906           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3907             warn_extern_redeclared_static (x, t);
3908         }
3909       else
3910         {
3911           /* Here to install a non-global value.  */
3912           tree oldlocal = IDENTIFIER_VALUE (name);
3913           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3914
3915           if (need_new_binding)
3916             {
3917               push_local_binding (name, x, 0);
3918               /* Because push_local_binding will hook X on to the
3919                  current_binding_level's name list, we don't want to
3920                  do that again below.  */
3921               need_new_binding = 0;
3922             }
3923
3924           /* If this is a TYPE_DECL, push it into the type value slot.  */
3925           if (TREE_CODE (x) == TYPE_DECL)
3926             set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3927                                                   current_binding_level);
3928
3929           /* Clear out any TYPE_DECL shadowed by a namespace so that
3930              we won't think this is a type.  The C struct hack doesn't
3931              go through namespaces.  */
3932           if (TREE_CODE (x) == NAMESPACE_DECL)
3933             set_identifier_type_value_with_scope (name, NULL_TREE,
3934                                                   current_binding_level);
3935
3936           if (oldlocal)
3937             {
3938               tree d = oldlocal;
3939
3940               while (oldlocal
3941                      && TREE_CODE (oldlocal) == VAR_DECL
3942                      && DECL_DEAD_FOR_LOCAL (oldlocal))
3943                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3944
3945               if (oldlocal == NULL_TREE)
3946                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3947             }
3948
3949           /* If this is an extern function declaration, see if we
3950              have a global definition or declaration for the function.  */
3951           if (oldlocal == NULL_TREE
3952               && DECL_EXTERNAL (x)
3953               && oldglobal != NULL_TREE
3954               && TREE_CODE (x) == FUNCTION_DECL
3955               && TREE_CODE (oldglobal) == FUNCTION_DECL)
3956             {
3957               /* We have one.  Their types must agree.  */
3958               if (decls_match (x, oldglobal))
3959                 /* OK */;
3960               else
3961                 {
3962                   warning ("extern declaration of `%#D' doesn't match", x);
3963                   cp_warning_at ("global declaration `%#D'", oldglobal);
3964                 }
3965             }
3966           /* If we have a local external declaration,
3967              and no file-scope declaration has yet been seen,
3968              then if we later have a file-scope decl it must not be static.  */
3969           if (oldlocal == NULL_TREE
3970               && oldglobal == NULL_TREE
3971               && DECL_EXTERNAL (x)
3972               && TREE_PUBLIC (x))
3973             TREE_PUBLIC (name) = 1;
3974
3975           /* Warn if shadowing an argument at the top level of the body.  */
3976           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3977               /* Inline decls shadow nothing.  */
3978               && !DECL_FROM_INLINE (x)
3979               && TREE_CODE (oldlocal) == PARM_DECL
3980               /* Don't check the `this' parameter.  */
3981               && !DECL_ARTIFICIAL (oldlocal))
3982             {
3983               bool err = false;
3984
3985               /* Don't complain if it's from an enclosing function.  */
3986               if (DECL_CONTEXT (oldlocal) == current_function_decl
3987                   && TREE_CODE (x) != PARM_DECL)
3988                 {
3989                   /* Go to where the parms should be and see if we find
3990                      them there.  */
3991                   struct cp_binding_level *b = current_binding_level->level_chain;
3992
3993                   /* Skip the ctor/dtor cleanup level.  */
3994                   b = b->level_chain;
3995
3996                   /* ARM $8.3 */
3997                   if (b->parm_flag == 1)
3998                     {
3999                       error ("declaration of `%#D' shadows a parameter",
4000                                 name);
4001                       err = true;
4002                     }
4003                 }
4004
4005               if (warn_shadow && !err)
4006                 shadow_warning (SW_PARAM,
4007                                 IDENTIFIER_POINTER (name), oldlocal);
4008             }
4009
4010           /* Maybe warn if shadowing something else.  */
4011           else if (warn_shadow && !DECL_EXTERNAL (x)
4012               /* No shadow warnings for internally generated vars.  */
4013               && ! DECL_ARTIFICIAL (x)
4014               /* No shadow warnings for vars made for inlining.  */
4015               && ! DECL_FROM_INLINE (x))
4016             {
4017               if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4018                        && current_class_ptr
4019                        && !TREE_STATIC (name))
4020                 warning ("declaration of `%s' shadows a member of `this'",
4021                             IDENTIFIER_POINTER (name));
4022               else if (oldlocal != NULL_TREE
4023                        && TREE_CODE (oldlocal) == VAR_DECL)
4024                 shadow_warning (SW_LOCAL,
4025                                 IDENTIFIER_POINTER (name), oldlocal);
4026               else if (oldglobal != NULL_TREE
4027                        && TREE_CODE (oldglobal) == VAR_DECL)
4028                 /* XXX shadow warnings in outer-more namespaces */
4029                 shadow_warning (SW_GLOBAL,
4030                                 IDENTIFIER_POINTER (name), oldglobal);
4031             }
4032         }
4033
4034       if (TREE_CODE (x) == FUNCTION_DECL)
4035         check_default_args (x);
4036
4037       if (TREE_CODE (x) == VAR_DECL)
4038         maybe_register_incomplete_var (x);
4039     }
4040
4041   if (need_new_binding)
4042     add_decl_to_level (x,
4043                        DECL_NAMESPACE_SCOPE_P (x)
4044                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4045                        : current_binding_level);
4046
4047   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4048 }
4049
4050 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4051    caller to set DECL_CONTEXT properly.  */
4052
4053 static tree
4054 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4055 {
4056   register struct cp_binding_level *b;
4057   tree function_decl = current_function_decl;
4058
4059   timevar_push (TV_NAME_LOOKUP);
4060   current_function_decl = NULL_TREE;
4061   if (level->parm_flag == 2)
4062     {
4063       b = class_binding_level;
4064       class_binding_level = level;
4065       pushdecl_class_level (x);
4066       class_binding_level = b;
4067     }
4068   else
4069     {
4070       b = current_binding_level;
4071       current_binding_level = level;
4072       x = pushdecl (x);
4073       current_binding_level = b;
4074     }
4075   current_function_decl = function_decl;
4076   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4077 }
4078
4079 /* Like pushdecl, only it places X in the current namespace,
4080    if appropriate.  */
4081
4082 tree
4083 pushdecl_namespace_level (tree x)
4084 {
4085   register struct cp_binding_level *b = current_binding_level;
4086   register tree t;
4087
4088   timevar_push (TV_NAME_LOOKUP);
4089   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4090
4091   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4092      what we want.  */
4093   if (TREE_CODE (x) == TYPE_DECL)
4094     {
4095       tree name = DECL_NAME (x);
4096       tree newval;
4097       tree *ptr = (tree *)0;
4098       for (; b != global_binding_level; b = b->level_chain)
4099         {
4100           tree shadowed = b->type_shadowed;
4101           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4102             if (TREE_PURPOSE (shadowed) == name)
4103               {
4104                 ptr = &TREE_VALUE (shadowed);
4105                 /* Can't break out of the loop here because sometimes
4106                    a binding level will have duplicate bindings for
4107                    PT names.  It's gross, but I haven't time to fix it.  */
4108               }
4109         }
4110       newval = TREE_TYPE (x);
4111       if (ptr == (tree *)0)
4112         {
4113           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4114              up here if this is changed to an assertion.  --KR  */
4115           SET_IDENTIFIER_TYPE_VALUE (name, newval);
4116         }
4117       else
4118         {
4119           *ptr = newval;
4120         }
4121     }
4122   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4123 }
4124
4125 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4126    if appropriate.  */
4127
4128 tree
4129 pushdecl_top_level (tree x)
4130 {
4131   timevar_push (TV_NAME_LOOKUP);
4132   push_to_top_level ();
4133   x = pushdecl_namespace_level (x);
4134   pop_from_top_level ();
4135   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4136 }
4137
4138 /* Make the declaration of X appear in CLASS scope.  */
4139
4140 void
4141 pushdecl_class_level (tree x)
4142 {
4143   tree name;
4144
4145   timevar_push (TV_NAME_LOOKUP);
4146   /* Get the name of X.  */
4147   if (TREE_CODE (x) == OVERLOAD)
4148     name = DECL_NAME (get_first_fn (x));
4149   else
4150     name = DECL_NAME (x);
4151
4152   if (name)
4153     {
4154       push_class_level_binding (name, x);
4155       if (TREE_CODE (x) == TYPE_DECL)
4156         set_identifier_type_value (name, TREE_TYPE (x));
4157     }
4158   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4159     {
4160       /* If X is an anonymous aggregate, all of its members are
4161          treated as if they were members of the class containing the
4162          aggregate, for naming purposes.  */
4163       tree f;
4164
4165       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4166         pushdecl_class_level (f);
4167     }
4168   timevar_pop (TV_NAME_LOOKUP);
4169 }
4170
4171 /* Enter DECL into the symbol table, if that's appropriate.  Returns
4172    DECL, or a modified version thereof.  */
4173
4174 tree
4175 maybe_push_decl (tree decl)
4176 {
4177   tree type = TREE_TYPE (decl);
4178
4179   /* Add this decl to the current binding level, but not if it comes
4180      from another scope, e.g. a static member variable.  TEM may equal
4181      DECL or it may be a previous decl of the same name.  */
4182   if (decl == error_mark_node
4183       || (TREE_CODE (decl) != PARM_DECL
4184           && DECL_CONTEXT (decl) != NULL_TREE
4185           /* Definitions of namespace members outside their namespace are
4186              possible.  */
4187           && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4188       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4189       || TREE_CODE (type) == UNKNOWN_TYPE
4190       /* The declaration of a template specialization does not affect
4191          the functions available for overload resolution, so we do not
4192          call pushdecl.  */
4193       || (TREE_CODE (decl) == FUNCTION_DECL
4194           && DECL_TEMPLATE_SPECIALIZATION (decl)))
4195     return decl;
4196   else
4197     return pushdecl (decl);
4198 }
4199
4200 /* Make the declaration(s) of X appear in CLASS scope
4201    under the name NAME.  */
4202
4203 void
4204 push_class_level_binding (tree name, tree x)
4205 {
4206   cxx_binding *binding;
4207   timevar_push (TV_NAME_LOOKUP);
4208   /* The class_binding_level will be NULL if x is a template
4209      parameter name in a member template.  */
4210   if (!class_binding_level)
4211     {
4212       timevar_pop (TV_NAME_LOOKUP);
4213       return;
4214     }
4215
4216   /* Make sure that this new member does not have the same name
4217      as a template parameter.  */
4218   if (TYPE_BEING_DEFINED (current_class_type))
4219     check_template_shadow (x);
4220
4221   /* If this declaration shadows a declaration from an enclosing
4222      class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4223      we leave this class.  Record the shadowed declaration here.  */
4224   binding = IDENTIFIER_BINDING (name);
4225   if (binding
4226       && ((TREE_CODE (x) == OVERLOAD
4227            && BINDING_VALUE (binding)
4228            && is_overloaded_fn (BINDING_VALUE (binding)))
4229           || INHERITED_VALUE_BINDING_P (binding)))
4230     {
4231       tree shadow;
4232       tree old_decl;
4233
4234       /* If the old binding was from a base class, and was for a tag
4235          name, slide it over to make room for the new binding.  The
4236          old binding is still visible if explicitly qualified with a
4237          class-key.  */
4238       if (INHERITED_VALUE_BINDING_P (binding)
4239           && BINDING_VALUE (binding)
4240           && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4241           && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4242           && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4243         {
4244           old_decl = BINDING_TYPE (binding);
4245           BINDING_TYPE (binding) = BINDING_VALUE (binding);
4246           BINDING_VALUE (binding) = NULL_TREE;
4247           INHERITED_VALUE_BINDING_P (binding) = 0;
4248         }
4249       else
4250         old_decl = BINDING_VALUE (binding);
4251
4252       /* Find the previous binding of name on the class-shadowed
4253          list, and update it.  */
4254       for (shadow = class_binding_level->class_shadowed;
4255            shadow;
4256            shadow = TREE_CHAIN (shadow))
4257         if (TREE_PURPOSE (shadow) == name
4258             && TREE_TYPE (shadow) == old_decl)
4259           {
4260             BINDING_VALUE (binding) = x;
4261             INHERITED_VALUE_BINDING_P (binding) = 0;
4262             TREE_TYPE (shadow) = x;
4263             IDENTIFIER_CLASS_VALUE (name) = x;
4264             timevar_pop (TV_NAME_LOOKUP);
4265             return;
4266           }
4267     }
4268
4269   /* If we didn't replace an existing binding, put the binding on the
4270      stack of bindings for the identifier, and update the shadowed list.  */
4271   if (push_class_binding (name, x))
4272     {
4273       class_binding_level->class_shadowed
4274         = tree_cons (name, NULL,
4275                      class_binding_level->class_shadowed);
4276       /* Record the value we are binding NAME to so that we can know
4277          what to pop later.  */
4278       TREE_TYPE (class_binding_level->class_shadowed) = x;
4279     }
4280   timevar_pop (TV_NAME_LOOKUP);
4281 }
4282
4283 /* Insert another USING_DECL into the current binding level, returning
4284    this declaration. If this is a redeclaration, do nothing, and
4285    return NULL_TREE if this not in namespace scope (in namespace
4286    scope, a using decl might extend any previous bindings).  */
4287
4288 tree
4289 push_using_decl (tree scope, tree name)
4290 {
4291   tree decl;
4292
4293   timevar_push (TV_NAME_LOOKUP);
4294   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4295   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4296   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4297     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4298       break;
4299   if (decl)
4300     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4301                             namespace_bindings_p () ? decl : NULL_TREE);
4302   decl = build_lang_decl (USING_DECL, name, void_type_node);
4303   DECL_INITIAL (decl) = scope;
4304   TREE_CHAIN (decl) = current_binding_level->usings;
4305   current_binding_level->usings = decl;
4306   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4307 }
4308
4309 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4310    changed (i.e. there was already a directive), or the fresh
4311    TREE_LIST otherwise.  */
4312
4313 tree
4314 push_using_directive (tree used)
4315 {
4316   tree ud = current_binding_level->using_directives;
4317   tree iter, ancestor;
4318
4319   timevar_push (TV_NAME_LOOKUP);
4320   /* Check if we already have this.  */
4321   if (purpose_member (used, ud) != NULL_TREE)
4322     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4323
4324   ancestor = namespace_ancestor (current_decl_namespace (), used);
4325   ud = current_binding_level->using_directives;
4326   ud = tree_cons (used, ancestor, ud);
4327   current_binding_level->using_directives = ud;
4328
4329   /* Recursively add all namespaces used.  */
4330   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4331     push_using_directive (TREE_PURPOSE (iter));
4332
4333   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4334 }
4335
4336 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4337    other definitions already in place.  We get around this by making
4338    the value of the identifier point to a list of all the things that
4339    want to be referenced by that name.  It is then up to the users of
4340    that name to decide what to do with that list.
4341
4342    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4343    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4344
4345    FLAGS is a bitwise-or of the following values:
4346      PUSH_LOCAL: Bind DECL in the current scope, rather than at
4347                  namespace scope.
4348      PUSH_USING: DECL is being pushed as the result of a using
4349                  declaration.
4350
4351    The value returned may be a previous declaration if we guessed wrong
4352    about what language DECL should belong to (C or C++).  Otherwise,
4353    it's always DECL (and never something that's not a _DECL).  */
4354
4355 tree
4356 push_overloaded_decl (tree decl, int flags)
4357 {
4358   tree name = DECL_NAME (decl);
4359   tree old;
4360   tree new_binding;
4361   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4362
4363   timevar_push (TV_NAME_LOOKUP);
4364   if (doing_global)
4365     old = namespace_binding (name, DECL_CONTEXT (decl));
4366   else
4367     old = lookup_name_current_level (name);
4368
4369   if (old)
4370     {
4371       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4372         {
4373           tree t = TREE_TYPE (old);
4374           if (IS_AGGR_TYPE (t) && warn_shadow
4375               && (! DECL_IN_SYSTEM_HEADER (decl)
4376                   || ! DECL_IN_SYSTEM_HEADER (old)))
4377             warning ("`%#D' hides constructor for `%#T'", decl, t);
4378           old = NULL_TREE;
4379         }
4380       else if (is_overloaded_fn (old))
4381         {
4382           tree tmp;
4383
4384           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4385             {
4386               tree fn = OVL_CURRENT (tmp);
4387
4388               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4389                   && !(flags & PUSH_USING)
4390                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4391                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4392                 error ("`%#D' conflicts with previous using declaration `%#D'",
4393                           decl, fn);
4394
4395               if (duplicate_decls (decl, fn))
4396                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4397             }
4398         }
4399       else if (old == error_mark_node)
4400         /* Ignore the undefined symbol marker.  */
4401         old = NULL_TREE;
4402       else
4403         {
4404           cp_error_at ("previous non-function declaration `%#D'", old);
4405           error ("conflicts with function declaration `%#D'", decl);
4406           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4407         }
4408     }
4409
4410   if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4411     {
4412       if (old && TREE_CODE (old) != OVERLOAD)
4413         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4414       else
4415         new_binding = ovl_cons (decl, old);
4416       if (flags & PUSH_USING)
4417         OVL_USED (new_binding) = 1;
4418     }
4419   else
4420     /* NAME is not ambiguous.  */
4421     new_binding = decl;
4422
4423   if (doing_global)
4424     set_namespace_binding (name, current_namespace, new_binding);
4425   else
4426     {
4427       /* We only create an OVERLOAD if there was a previous binding at
4428          this level, or if decl is a template. In the former case, we
4429          need to remove the old binding and replace it with the new
4430          binding.  We must also run through the NAMES on the binding
4431          level where the name was bound to update the chain.  */
4432
4433       if (TREE_CODE (new_binding) == OVERLOAD && old)
4434         {
4435           tree *d;
4436
4437           for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names;
4438                *d;
4439                d = &TREE_CHAIN (*d))
4440             if (*d == old
4441                 || (TREE_CODE (*d) == TREE_LIST
4442                     && TREE_VALUE (*d) == old))
4443               {
4444                 if (TREE_CODE (*d) == TREE_LIST)
4445                   /* Just replace the old binding with the new.  */
4446                   TREE_VALUE (*d) = new_binding;
4447                 else
4448                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
4449                   *d = tree_cons (NULL_TREE, new_binding,
4450                                   TREE_CHAIN (*d));
4451
4452                 /* And update the cxx_binding node.  */
4453                 BINDING_VALUE (IDENTIFIER_BINDING (name))
4454                   = new_binding;
4455                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4456               }
4457
4458           /* We should always find a previous binding in this case.  */
4459           abort ();
4460         }
4461
4462       /* Install the new binding.  */
4463       push_local_binding (name, new_binding, flags);
4464     }
4465
4466   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4467 }
4468 \f
4469 /* Generate an implicit declaration for identifier FUNCTIONID
4470    as a function of type int ().  Print a warning if appropriate.  */
4471
4472 tree
4473 implicitly_declare (tree functionid)
4474 {
4475   register tree decl;
4476
4477   /* We used to reuse an old implicit decl here,
4478      but this loses with inline functions because it can clobber
4479      the saved decl chains.  */
4480   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4481
4482   DECL_EXTERNAL (decl) = 1;
4483   TREE_PUBLIC (decl) = 1;
4484
4485   /* ISO standard says implicit declarations are in the innermost block.
4486      So we record the decl in the standard fashion.  */
4487   pushdecl (decl);
4488   rest_of_decl_compilation (decl, NULL, 0, 0);
4489
4490   if (warn_implicit
4491       /* Only one warning per identifier.  */
4492       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4493     {
4494       pedwarn ("implicit declaration of function `%#D'", decl);
4495     }
4496
4497   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4498
4499   return decl;
4500 }
4501
4502 /* Return zero if the declaration NEWDECL is valid
4503    when the declaration OLDDECL (assumed to be for the same name)
4504    has already been seen.
4505    Otherwise return an error message format string with a %s
4506    where the identifier should go.  */
4507
4508 static const char *
4509 redeclaration_error_message (tree newdecl, tree olddecl)
4510 {
4511   if (TREE_CODE (newdecl) == TYPE_DECL)
4512     {
4513       /* Because C++ can put things into name space for free,
4514          constructs like "typedef struct foo { ... } foo"
4515          would look like an erroneous redeclaration.  */
4516       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4517         return 0;
4518       else
4519         return "redefinition of `%#D'";
4520     }
4521   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4522     {
4523       /* If this is a pure function, its olddecl will actually be
4524          the original initialization to `0' (which we force to call
4525          abort()).  Don't complain about redefinition in this case.  */
4526       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4527         return 0;
4528
4529       /* If both functions come from different namespaces, this is not
4530          a redeclaration - this is a conflict with a used function.  */
4531       if (DECL_NAMESPACE_SCOPE_P (olddecl)
4532           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4533         return "`%D' conflicts with used function";
4534
4535       /* We'll complain about linkage mismatches in
4536          warn_extern_redeclared_static.  */
4537
4538       /* Defining the same name twice is no good.  */
4539       if (DECL_INITIAL (olddecl) != NULL_TREE
4540           && DECL_INITIAL (newdecl) != NULL_TREE)
4541         {
4542           if (DECL_NAME (olddecl) == NULL_TREE)
4543             return "`%#D' not declared in class";
4544           else
4545             return "redefinition of `%#D'";
4546         }
4547       return 0;
4548     }
4549   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4550     {
4551       if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4552            && (DECL_TEMPLATE_RESULT (newdecl)
4553                != DECL_TEMPLATE_RESULT (olddecl))
4554            && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4555            && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4556           || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4557               && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4558               && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4559         return "redefinition of `%#D'";
4560       return 0;
4561     }
4562   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4563     {
4564       /* Objects declared at top level:  */
4565       /* If at least one is a reference, it's ok.  */
4566       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4567         return 0;
4568       /* Reject two definitions.  */
4569       return "redefinition of `%#D'";
4570     }
4571   else
4572     {
4573       /* Objects declared with block scope:  */
4574       /* Reject two definitions, and reject a definition
4575          together with an external reference.  */
4576       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4577         return "redeclaration of `%#D'";
4578       return 0;
4579     }
4580 }
4581 \f
4582 /* Create a new label, named ID.  */
4583
4584 static tree
4585 make_label_decl (tree id, int local_p)
4586 {
4587   tree decl;
4588
4589   decl = build_decl (LABEL_DECL, id, void_type_node);
4590   if (expanding_p)
4591     /* Make sure every label has an rtx.  */
4592     label_rtx (decl);
4593
4594   DECL_CONTEXT (decl) = current_function_decl;
4595   DECL_MODE (decl) = VOIDmode;
4596   C_DECLARED_LABEL_FLAG (decl) = local_p;
4597
4598   /* Say where one reference is to the label, for the sake of the
4599      error if it is not defined.  */
4600   DECL_SOURCE_LOCATION (decl) = input_location;
4601
4602   /* Record the fact that this identifier is bound to this label.  */
4603   SET_IDENTIFIER_LABEL_VALUE (id, decl);
4604
4605   return decl;
4606 }
4607
4608 /* Record this label on the list of used labels so that we can check
4609    at the end of the function to see whether or not the label was
4610    actually defined, and so we can check when the label is defined whether
4611    this use is valid.  */
4612
4613 static void
4614 use_label (tree decl)
4615 {
4616   if (named_label_uses == NULL
4617       || named_label_uses->names_in_scope != current_binding_level->names
4618       || named_label_uses->label_decl != decl)
4619     {
4620       struct named_label_use_list *new_ent;
4621       new_ent = ((struct named_label_use_list *)
4622                  ggc_alloc (sizeof (struct named_label_use_list)));
4623       new_ent->label_decl = decl;
4624       new_ent->names_in_scope = current_binding_level->names;
4625       new_ent->binding_level = current_binding_level;
4626       new_ent->o_goto_locus = input_location;
4627       new_ent->next = named_label_uses;
4628       named_label_uses = new_ent;
4629     }
4630 }
4631
4632 /* Look for a label named ID in the current function.  If one cannot
4633    be found, create one.  (We keep track of used, but undefined,
4634    labels, and complain about them at the end of a function.)  */
4635
4636 tree
4637 lookup_label (tree id)
4638 {
4639   tree decl;
4640   struct named_label_list *ent;
4641
4642   timevar_push (TV_NAME_LOOKUP);
4643   /* You can't use labels at global scope.  */
4644   if (current_function_decl == NULL_TREE)
4645     {
4646       error ("label `%s' referenced outside of any function",
4647              IDENTIFIER_POINTER (id));
4648       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4649     }
4650
4651   /* See if we've already got this label.  */
4652   decl = IDENTIFIER_LABEL_VALUE (id);
4653   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4654     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4655
4656   /* Record this label on the list of labels used in this function.
4657      We do this before calling make_label_decl so that we get the
4658      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
4659   ent = ((struct named_label_list *)
4660          ggc_alloc_cleared (sizeof (struct named_label_list)));
4661   ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4662   ent->next = named_labels;
4663   named_labels = ent;
4664
4665   /* We need a new label.  */
4666   decl = make_label_decl (id, /*local_p=*/0);
4667
4668   /* Now fill in the information we didn't have before.  */
4669   ent->label_decl = decl;
4670
4671   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4672 }
4673
4674 /* Declare a local label named ID.  */
4675
4676 tree
4677 declare_local_label (tree id)
4678 {
4679   tree decl;
4680
4681   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4682      this scope we can restore the old value of
4683      IDENTIFIER_TYPE_VALUE.  */
4684   current_binding_level->shadowed_labels
4685     = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4686                  current_binding_level->shadowed_labels);
4687   /* Look for the label.  */
4688   decl = make_label_decl (id, /*local_p=*/1);
4689   /* Now fill in the information we didn't have before.  */
4690   TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4691
4692   return decl;
4693 }
4694
4695 /* Returns nonzero if it is ill-formed to jump past the declaration of
4696    DECL.  Returns 2 if it's also a real problem.  */
4697
4698 static int
4699 decl_jump_unsafe (tree decl)
4700 {
4701   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4702     return 0;
4703
4704   if (DECL_INITIAL (decl) == NULL_TREE
4705       && pod_type_p (TREE_TYPE (decl)))
4706     return 0;
4707
4708   /* This is really only important if we're crossing an initialization.
4709      The POD stuff is just pedantry; why should it matter if the class
4710      contains a field of pointer to member type?  */
4711   if (DECL_INITIAL (decl)
4712       || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4713     return 2;
4714   return 1;
4715 }
4716
4717 /* Check that a single previously seen jump to a newly defined label
4718    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4719    the jump context; NAMES are the names in scope in LEVEL at the jump
4720    context; FILE and LINE are the source position of the jump or 0.  */
4721
4722 static void
4723 check_previous_goto_1 (tree decl,
4724                        struct cp_binding_level* level,
4725                        tree names, const location_t *locus)
4726 {
4727   int identified = 0;
4728   int saw_eh = 0;
4729   struct cp_binding_level *b = current_binding_level;
4730   for (; b; b = b->level_chain)
4731     {
4732       tree new_decls = b->names;
4733       tree old_decls = (b == level ? names : NULL_TREE);
4734       for (; new_decls != old_decls;
4735            new_decls = TREE_CHAIN (new_decls))
4736         {
4737           int problem = decl_jump_unsafe (new_decls);
4738           if (! problem)
4739             continue;
4740
4741           if (! identified)
4742             {
4743               if (decl)
4744                 pedwarn ("jump to label `%D'", decl);
4745               else
4746                 pedwarn ("jump to case label");
4747
4748               if (locus)
4749                 pedwarn ("%H  from here", locus);
4750               identified = 1;
4751             }
4752
4753           if (problem > 1)
4754             cp_error_at ("  crosses initialization of `%#D'",
4755                          new_decls);
4756           else
4757             cp_pedwarn_at ("  enters scope of non-POD `%#D'",
4758                            new_decls);
4759         }
4760
4761       if (b == level)
4762         break;
4763       if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4764         {
4765           if (! identified)
4766             {
4767               if (decl)
4768                 pedwarn ("jump to label `%D'", decl);
4769               else
4770                 pedwarn ("jump to case label");
4771
4772               if (locus)
4773                 pedwarn ("%H  from here", locus);
4774               identified = 1;
4775             }
4776           if (b->is_try_scope)
4777             error ("  enters try block");
4778           else
4779             error ("  enters catch block");
4780           saw_eh = 1;
4781         }
4782     }
4783 }
4784
4785 static void
4786 check_previous_goto (struct named_label_use_list* use)
4787 {
4788   check_previous_goto_1 (use->label_decl, use->binding_level,
4789                          use->names_in_scope, &use->o_goto_locus);
4790 }
4791
4792 static void
4793 check_switch_goto (struct cp_binding_level* level)
4794 {
4795   check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
4796 }
4797
4798 /* Check that any previously seen jumps to a newly defined label DECL
4799    are OK.  Called by define_label.  */
4800
4801 static void
4802 check_previous_gotos (tree decl)
4803 {
4804   struct named_label_use_list **usep;
4805
4806   if (! TREE_USED (decl))
4807     return;
4808
4809   for (usep = &named_label_uses; *usep; )
4810     {
4811       struct named_label_use_list *use = *usep;
4812       if (use->label_decl == decl)
4813         {
4814           check_previous_goto (use);
4815           *usep = use->next;
4816         }
4817       else
4818         usep = &(use->next);
4819     }
4820 }
4821
4822 /* Check that a new jump to a label DECL is OK.  Called by
4823    finish_goto_stmt.  */
4824
4825 void
4826 check_goto (tree decl)
4827 {
4828   int identified = 0;
4829   tree bad;
4830   struct named_label_list *lab;
4831
4832   /* We can't know where a computed goto is jumping.  So we assume
4833      that it's OK.  */
4834   if (! DECL_P (decl))
4835     return;
4836
4837   /* If the label hasn't been defined yet, defer checking.  */
4838   if (! DECL_INITIAL (decl))
4839     {
4840       use_label (decl);
4841       return;
4842     }
4843
4844   for (lab = named_labels; lab; lab = lab->next)
4845     if (decl == lab->label_decl)
4846       break;
4847
4848   /* If the label is not on named_labels it's a gcc local label, so
4849      it must be in an outer scope, so jumping to it is always OK.  */
4850   if (lab == 0)
4851     return;
4852
4853   if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4854       && !identified)
4855     {
4856       cp_pedwarn_at ("jump to label `%D'", decl);
4857       pedwarn ("  from here");
4858       identified = 1;
4859     }
4860
4861   for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4862     {
4863       tree b = TREE_VALUE (bad);
4864       int u = decl_jump_unsafe (b);
4865
4866       if (u > 1 && DECL_ARTIFICIAL (b))
4867         /* Can't skip init of __exception_info.  */
4868         cp_error_at ("  enters catch block", b);
4869       else if (u > 1)
4870         cp_error_at ("  skips initialization of `%#D'", b);
4871       else
4872         cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
4873     }
4874
4875   if (lab->in_try_scope)
4876     error ("  enters try block");
4877   else if (lab->in_catch_scope)
4878     error ("  enters catch block");
4879 }
4880
4881 /* Define a label, specifying the location in the source file.
4882    Return the LABEL_DECL node for the label, if the definition is valid.
4883    Otherwise return 0.  */
4884
4885 tree
4886 define_label (const char* filename, int line, tree name)
4887 {
4888   tree decl = lookup_label (name);
4889   struct named_label_list *ent;
4890   register struct cp_binding_level *p;
4891
4892   timevar_push (TV_NAME_LOOKUP);
4893   for (ent = named_labels; ent; ent = ent->next)
4894     if (ent->label_decl == decl)
4895       break;
4896
4897   /* After labels, make any new cleanups in the function go into their
4898      own new (temporary) binding contour.  */
4899   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4900     p->more_cleanups_ok = 0;
4901
4902   if (name == get_identifier ("wchar_t"))
4903     pedwarn ("label named wchar_t");
4904
4905   if (DECL_INITIAL (decl) != NULL_TREE)
4906     {
4907       error ("duplicate label `%D'", decl);
4908       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4909     }
4910   else
4911     {
4912       /* Mark label as having been defined.  */
4913       DECL_INITIAL (decl) = error_mark_node;
4914       /* Say where in the source.  */
4915       DECL_SOURCE_FILE (decl) = filename;
4916       DECL_SOURCE_LINE (decl) = line;
4917       if (ent)
4918         {
4919           ent->names_in_scope = current_binding_level->names;
4920           ent->binding_level = current_binding_level;
4921         }
4922       check_previous_gotos (decl);
4923       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4924     }
4925   timevar_pop (TV_NAME_LOOKUP);
4926 }
4927
4928 struct cp_switch
4929 {
4930   struct cp_binding_level *level;
4931   struct cp_switch *next;
4932   /* The SWITCH_STMT being built.  */
4933   tree switch_stmt;
4934   /* A splay-tree mapping the low element of a case range to the high
4935      element, or NULL_TREE if there is no high element.  Used to
4936      determine whether or not a new case label duplicates an old case
4937      label.  We need a tree, rather than simply a hash table, because
4938      of the GNU case range extension.  */
4939   splay_tree cases;
4940 };
4941
4942 /* A stack of the currently active switch statements.  The innermost
4943    switch statement is on the top of the stack.  There is no need to
4944    mark the stack for garbage collection because it is only active
4945    during the processing of the body of a function, and we never
4946    collect at that point.  */
4947
4948 static struct cp_switch *switch_stack;
4949
4950 /* Called right after a switch-statement condition is parsed.
4951    SWITCH_STMT is the switch statement being parsed.  */
4952
4953 void
4954 push_switch (tree switch_stmt)
4955 {
4956   struct cp_switch *p
4957     = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
4958   p->level = current_binding_level;
4959   p->next = switch_stack;
4960   p->switch_stmt = switch_stmt;
4961   p->cases = splay_tree_new (case_compare, NULL, NULL);
4962   switch_stack = p;
4963 }
4964
4965 void
4966 pop_switch (void)
4967 {
4968   struct cp_switch *cs;
4969
4970   cs = switch_stack;
4971   splay_tree_delete (cs->cases);
4972   switch_stack = switch_stack->next;
4973   free (cs);
4974 }
4975
4976 /* Note that we've seen a definition of a case label, and complain if this
4977    is a bad place for one.  */
4978
4979 tree
4980 finish_case_label (tree low_value, tree high_value)
4981 {
4982   tree cond, r;
4983   register struct cp_binding_level *p;
4984
4985   if (! switch_stack)
4986     {
4987       if (high_value)
4988         error ("case label not within a switch statement");
4989       else if (low_value)
4990         error ("case label `%E' not within a switch statement",
4991                   low_value);
4992       else
4993         error ("`default' label not within a switch statement");
4994       return NULL_TREE;
4995     }
4996
4997   if (processing_template_decl)
4998     {
4999       tree label;
5000
5001       /* For templates, just add the case label; we'll do semantic
5002          analysis at instantiation-time.  */
5003       label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5004       return add_stmt (build_case_label (low_value, high_value, label));
5005     }
5006
5007   /* Find the condition on which this switch statement depends.  */
5008   cond = SWITCH_COND (switch_stack->switch_stmt);
5009   if (cond && TREE_CODE (cond) == TREE_LIST)
5010     cond = TREE_VALUE (cond);
5011
5012   r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5013
5014   check_switch_goto (switch_stack->level);
5015
5016   /* After labels, make any new cleanups in the function go into their
5017      own new (temporary) binding contour.  */
5018   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5019     p->more_cleanups_ok = 0;
5020
5021   return r;
5022 }
5023 \f
5024 /* Return the list of declarations of the current level.
5025    Note that this list is in reverse order unless/until
5026    you nreverse it; and when you do nreverse it, you must
5027    store the result back using `storedecls' or you will lose.  */
5028
5029 tree
5030 getdecls (void)
5031 {
5032   return current_binding_level->names;
5033 }
5034
5035 /* Return the list of type-tags (for structs, etc) of the current level.  */
5036
5037 tree
5038 gettags (void)
5039 {
5040   return current_binding_level->tags;
5041 }
5042
5043 /* Store the list of declarations of the current level.
5044    This is done for the parameter declarations of a function being defined,
5045    after they are modified in the light of any missing parameters.  */
5046
5047 static void
5048 storedecls (tree decls)
5049 {
5050   current_binding_level->names = decls;
5051 }
5052
5053 /* Similarly, store the list of tags of the current level.  */
5054
5055 void
5056 storetags (tree tags)
5057 {
5058   current_binding_level->tags = tags;
5059 }
5060 \f
5061 /* Return the type that should be used when TYPE's name is preceded
5062    by a tag such as 'struct' or 'union', or null if the name cannot
5063    be used in this way.
5064
5065    For example, when processing the third line of:
5066
5067         struct A;
5068         typedef struct A A;
5069         struct A;
5070
5071    lookup of A will find the typedef.  Given A's typedef, this function
5072    will return the type associated with "struct A".  For the tag to be
5073    anything other than TYPE, TYPE must be a typedef whose original type
5074    has the same name and context as TYPE itself.
5075
5076    It is not valid for a typedef of an anonymous type to be used with
5077    an explicit tag:
5078
5079        typedef struct { ... } B;
5080        struct B;
5081
5082    Return null for this case.  */
5083
5084 static tree
5085 follow_tag_typedef (tree type)
5086 {
5087   tree original;
5088
5089   original = original_type (type);
5090   if (! TYPE_NAME (original))
5091     return NULL_TREE;
5092   if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5093       && (CP_DECL_CONTEXT (TYPE_NAME (original))
5094           == CP_DECL_CONTEXT (TYPE_NAME (type)))
5095       && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5096     return original;
5097   else
5098     return NULL_TREE;
5099 }
5100
5101 /* Given NAME, an IDENTIFIER_NODE,
5102    return the structure (or union or enum) definition for that name.
5103    Searches binding levels from BINDING_SCOPE up to the global level.
5104    If THISLEVEL_ONLY is nonzero, searches only the specified context
5105    (but skips any tag-transparent contexts to find one that is
5106    meaningful for tags).
5107    FORM says which kind of type the caller wants;
5108    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5109    If the wrong kind of type is found, and it's not a template, an error is
5110    reported.  */
5111
5112 static tree
5113 lookup_tag (enum tree_code form, tree name,
5114             struct cp_binding_level* binding_level, int thislevel_only)
5115 {
5116   register struct cp_binding_level *level;
5117   /* Nonzero if, we should look past a template parameter level, even
5118      if THISLEVEL_ONLY.  */
5119   int allow_template_parms_p = 1;
5120
5121   timevar_push (TV_NAME_LOOKUP);
5122   for (level = binding_level; level; level = level->level_chain)
5123     {
5124       register tree tail;
5125       if (ANON_AGGRNAME_P (name))
5126         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5127           {
5128             /* There's no need for error checking here, because
5129                anon names are unique throughout the compilation.  */
5130             if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5131               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5132           }
5133       else if (level->namespace_p)
5134         /* Do namespace lookup.  */
5135         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5136           {
5137             cxx_binding *binding =
5138               cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
5139             tree old;
5140
5141             /* If we just skipped past a template parameter level,
5142                even though THISLEVEL_ONLY, and we find a template
5143                class declaration, then we use the _TYPE node for the
5144                template.  See the example below.  */
5145             if (thislevel_only && !allow_template_parms_p
5146                 && binding && BINDING_VALUE (binding)
5147                 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5148               old = TREE_TYPE (BINDING_VALUE (binding));
5149             else if (binding)
5150               old = BINDING_TYPE (binding);
5151             else
5152               old = NULL_TREE;
5153
5154             if (old)
5155               {
5156                 /* We've found something at this binding level.  If it is
5157                    a typedef, extract the tag it refers to.  Lookup fails
5158                    if the typedef doesn't refer to a taggable type.  */
5159                 old = follow_tag_typedef (old);
5160                 if (!old)
5161                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5162                 if (TREE_CODE (old) != form
5163                     && (form == ENUMERAL_TYPE
5164                         || TREE_CODE (old) == ENUMERAL_TYPE))
5165                   {
5166                     error ("`%#D' redeclared as %C", old, form);
5167                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5168                   }
5169                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5170               }
5171             if (thislevel_only || tail == global_namespace)
5172               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5173           }
5174       else
5175         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5176           {
5177             if (TREE_PURPOSE (tail) == name)
5178               {
5179                 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5180                 
5181                 if (code != form
5182                     && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5183                   {
5184                     /* Definition isn't the kind we were looking for.  */
5185                     error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5186                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5187                   }
5188                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5189               }
5190           }
5191       if (thislevel_only && ! level->tag_transparent)
5192         {
5193           if (level->template_parms_p && allow_template_parms_p)
5194             {
5195               /* We must deal with cases like this:
5196
5197                    template <class T> struct S;
5198                    template <class T> struct S {};
5199
5200                  When looking up `S', for the second declaration, we
5201                  would like to find the first declaration.  But, we
5202                  are in the pseudo-global level created for the
5203                  template parameters, rather than the (surrounding)
5204                  namespace level.  Thus, we keep going one more level,
5205                  even though THISLEVEL_ONLY is nonzero.  */
5206               allow_template_parms_p = 0;
5207               continue;
5208             }
5209           else
5210             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5211         }
5212     }
5213   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5214 }
5215
5216 /* Given a type, find the tag that was defined for it and return the tag name.
5217    Otherwise return 0.  However, the value can never be 0
5218    in the cases in which this is used.
5219
5220    C++: If NAME is nonzero, this is the new name to install.  This is
5221    done when replacing anonymous tags with real tag names.  */
5222
5223 static tree
5224 lookup_tag_reverse (tree type, tree name)
5225 {
5226   register struct cp_binding_level *level;
5227
5228   timevar_push (TV_NAME_LOOKUP);
5229   for (level = current_binding_level; level; level = level->level_chain)
5230     {
5231       register tree tail;
5232       for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5233         {
5234           if (TREE_VALUE (tail) == type)
5235             {
5236               if (name)
5237                 TREE_PURPOSE (tail) = name;
5238               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_PURPOSE (tail));
5239             }
5240         }
5241     }
5242   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5243 }
5244 \f
5245 /* Look up NAME in the NAMESPACE.  */
5246
5247 tree
5248 lookup_namespace_name (tree namespace, tree name)
5249 {
5250   tree val;
5251   tree template_id = NULL_TREE;
5252   cxx_binding binding;
5253
5254   timevar_push (TV_NAME_LOOKUP);
5255   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5256
5257   if (TREE_CODE (name) == NAMESPACE_DECL)
5258     /* This happens for A::B<int> when B is a namespace.  */
5259     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5260   else if (TREE_CODE (name) == TEMPLATE_DECL)
5261     {
5262       /* This happens for A::B where B is a template, and there are no
5263          template arguments.  */
5264       error ("invalid use of `%D'", name);
5265       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5266     }
5267
5268   namespace = ORIGINAL_NAMESPACE (namespace);
5269
5270   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5271     {
5272       template_id = name;
5273       name = TREE_OPERAND (name, 0);
5274       if (TREE_CODE (name) == OVERLOAD)
5275         name = DECL_NAME (OVL_CURRENT (name));
5276       else if (DECL_P (name))
5277         name = DECL_NAME (name);
5278     }
5279
5280   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5281
5282   cxx_binding_clear (&binding);
5283   if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5284     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5285
5286   if (binding.value)
5287     {
5288       val = binding.value;
5289
5290       if (template_id)
5291         {
5292           if (DECL_CLASS_TEMPLATE_P (val))
5293             val = lookup_template_class (val,
5294                                          TREE_OPERAND (template_id, 1),
5295                                          /*in_decl=*/NULL_TREE,
5296                                          /*context=*/NULL_TREE,
5297                                          /*entering_scope=*/0,
5298                                          tf_error | tf_warning);
5299           else if (DECL_FUNCTION_TEMPLATE_P (val)
5300                    || TREE_CODE (val) == OVERLOAD)
5301             val = lookup_template_function (val,
5302                                             TREE_OPERAND (template_id, 1));
5303           else
5304             {
5305               error ("`%D::%D' is not a template",
5306                         namespace, name);
5307               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5308             }
5309         }
5310
5311       /* If we have a single function from a using decl, pull it out.  */
5312       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5313         val = OVL_FUNCTION (val);
5314
5315       /* Ignore built-in functions that haven't been prototyped yet.  */
5316       if (!val || !DECL_P(val)
5317           || !DECL_LANG_SPECIFIC(val)
5318           || !DECL_ANTICIPATED (val))
5319         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5320     }
5321
5322   error ("`%D' undeclared in namespace `%D'", name, namespace);
5323   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5324 }
5325
5326 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5327
5328 static hashval_t
5329 typename_hash (const void* k)
5330 {
5331   hashval_t hash;
5332   tree t = (tree) k;
5333
5334   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5335           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5336
5337   return hash;
5338 }
5339
5340 /* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5341
5342 static int
5343 typename_compare (const void * k1, const void * k2)
5344 {
5345   tree t1;
5346   tree t2;
5347   tree d1;
5348   tree d2;
5349
5350   t1 = (tree) k1;
5351   t2 = (tree) k2;
5352   d1 = TYPE_NAME (t1);
5353   d2 = TYPE_NAME (t2);
5354
5355   return (DECL_NAME (d1) == DECL_NAME (d2)
5356           && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5357           && ((TREE_TYPE (t1) != NULL_TREE)
5358               == (TREE_TYPE (t2) != NULL_TREE))
5359           && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5360           && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5361 }
5362
5363 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5364    the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5365    is non-NULL, this type is being created by the implicit typename
5366    extension, and BASE_TYPE is a type named `t' in some base class of
5367    `T' which depends on template parameters.
5368
5369    Returns the new TYPENAME_TYPE.  */
5370
5371 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5372
5373 tree
5374 build_typename_type (tree context, tree name, tree fullname)
5375 {
5376   tree t;
5377   tree d;
5378   PTR *e;
5379
5380   if (typename_htab == NULL)
5381     {
5382       typename_htab = htab_create_ggc (61, &typename_hash, 
5383                                        &typename_compare, NULL);
5384     }
5385
5386   /* Build the TYPENAME_TYPE.  */
5387   t = make_aggr_type (TYPENAME_TYPE);
5388   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5389   TYPENAME_TYPE_FULLNAME (t) = fullname;
5390
5391   /* Build the corresponding TYPE_DECL.  */
5392   d = build_decl (TYPE_DECL, name, t);
5393   TYPE_NAME (TREE_TYPE (d)) = d;
5394   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5395   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5396   DECL_ARTIFICIAL (d) = 1;
5397
5398   /* See if we already have this type.  */
5399   e = htab_find_slot (typename_htab, t, INSERT);
5400   if (*e)
5401     t = (tree) *e;
5402   else
5403     *e = t;
5404
5405   return t;
5406 }
5407
5408 /* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5409    unless an error occurs, in which case error_mark_node is returned.
5410    If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5411    set, we return that, rather than the _TYPE it corresponds to, in
5412    other cases we look through the type decl.  If TF_ERROR is set,
5413    complain about errors, otherwise be quiet.  */
5414
5415 tree
5416 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5417 {
5418   tree fullname;
5419
5420   if (name == error_mark_node
5421       || context == NULL_TREE
5422       || context == error_mark_node)
5423     return error_mark_node;
5424
5425   if (TYPE_P (name))
5426     {
5427       if (!(TYPE_LANG_SPECIFIC (name)
5428             && (CLASSTYPE_IS_TEMPLATE (name)
5429                 || CLASSTYPE_USE_TEMPLATE (name))))
5430         name = TYPE_IDENTIFIER (name);
5431       else
5432         /* Create a TEMPLATE_ID_EXPR for the type.  */
5433         name = build_nt (TEMPLATE_ID_EXPR,
5434                          CLASSTYPE_TI_TEMPLATE (name),
5435                          CLASSTYPE_TI_ARGS (name));
5436     }
5437   else if (TREE_CODE (name) == TYPE_DECL)
5438     name = DECL_NAME (name);
5439
5440   fullname = name;
5441
5442   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5443     {
5444       name = TREE_OPERAND (name, 0);
5445       if (TREE_CODE (name) == TEMPLATE_DECL)
5446         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5447     }
5448   if (TREE_CODE (name) == TEMPLATE_DECL)
5449     {
5450       error ("`%D' used without template parameters", name);
5451       return error_mark_node;
5452     }
5453   if (TREE_CODE (name) != IDENTIFIER_NODE)
5454     abort ();
5455
5456   if (TREE_CODE (context) == NAMESPACE_DECL)
5457     {
5458       /* We can get here from typename_sub0 in the explicit_template_type
5459          expansion.  Just fail.  */
5460       if (complain & tf_error)
5461         error ("no class template named `%#T' in `%#T'",
5462                   name, context);
5463       return error_mark_node;
5464     }
5465
5466   if (! uses_template_parms (context)
5467       || currently_open_class (context))
5468     {
5469       if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5470         {
5471           tree tmpl = NULL_TREE;
5472           if (IS_AGGR_TYPE (context))
5473             tmpl = lookup_field (context, name, 0, false);
5474           if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5475             {
5476               if (complain & tf_error)
5477                 error ("no class template named `%#T' in `%#T'",
5478                           name, context);
5479               return error_mark_node;
5480             }
5481
5482           if (complain & tf_error)
5483             perform_or_defer_access_check (context, tmpl);
5484
5485           return lookup_template_class (tmpl,
5486                                         TREE_OPERAND (fullname, 1),
5487                                         NULL_TREE, context,
5488                                         /*entering_scope=*/0,
5489                                         tf_error | tf_warning);
5490         }
5491       else
5492         {
5493           tree t;
5494
5495           if (!IS_AGGR_TYPE (context))
5496             {
5497               if (complain & tf_error)
5498                 error ("no type named `%#T' in `%#T'", name, context);
5499               return error_mark_node;
5500             }
5501
5502           t = lookup_field (context, name, 0, true);
5503           if (t)
5504             {
5505               if (TREE_CODE (t) != TYPE_DECL)
5506                 {
5507                   if (complain & tf_error)
5508                     error ("no type named `%#T' in `%#T'", name, context);
5509                   return error_mark_node;
5510                 }
5511
5512               if (complain & tf_error)
5513                 perform_or_defer_access_check (context, t);
5514
5515               if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5516                 t = TREE_TYPE (t);
5517               
5518               return t;
5519             }
5520         }
5521     }
5522
5523   /* If the CONTEXT is not a template type, then either the field is
5524      there now or its never going to be.  */
5525   if (!uses_template_parms (context))
5526     {
5527       if (complain & tf_error)
5528         error ("no type named `%#T' in `%#T'", name, context);
5529       return error_mark_node;
5530     }
5531
5532   return build_typename_type (context, name, fullname);
5533 }
5534
5535 /* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
5536    unless an error occurs, in which case error_mark_node is returned.
5537    If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5538    corresponds to.  If COMPLAIN zero, don't complain about any errors
5539    that occur.  */
5540
5541 tree
5542 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5543 {
5544   tree t;
5545   tree d;
5546
5547   if (TYPE_P (name))
5548     name = TYPE_IDENTIFIER (name);
5549   else if (DECL_P (name))
5550     name = DECL_NAME (name);
5551   if (TREE_CODE (name) != IDENTIFIER_NODE)
5552     abort ();
5553
5554   if (!uses_template_parms (context)
5555       || currently_open_class (context))
5556     {
5557       tree tmpl = NULL_TREE;
5558
5559       if (IS_AGGR_TYPE (context))
5560         tmpl = lookup_field (context, name, 0, false);
5561
5562       if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5563         {
5564           if (complain & tf_error)
5565             error ("no class template named `%#T' in `%#T'", name, context);
5566           return error_mark_node;
5567         }
5568       
5569       if (complain & tf_error)
5570         perform_or_defer_access_check (context, tmpl);
5571
5572       return tmpl;
5573     }
5574
5575   /* Build the UNBOUND_CLASS_TEMPLATE.  */
5576   t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5577   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5578   TREE_TYPE (t) = NULL_TREE;
5579
5580   /* Build the corresponding TEMPLATE_DECL.  */
5581   d = build_decl (TEMPLATE_DECL, name, t);
5582   TYPE_NAME (TREE_TYPE (d)) = d;
5583   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5584   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5585   DECL_ARTIFICIAL (d) = 1;
5586
5587   return t;
5588 }
5589
5590 /* Select the right _DECL from multiple choices.  */
5591
5592 static tree
5593 select_decl (cxx_binding *binding, int flags)
5594 {
5595   tree val;
5596   val = BINDING_VALUE (binding);
5597
5598   timevar_push (TV_NAME_LOOKUP);
5599   if (LOOKUP_NAMESPACES_ONLY (flags))
5600     {
5601       /* We are not interested in types.  */
5602       if (val && TREE_CODE (val) == NAMESPACE_DECL)
5603         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5604       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5605     }
5606
5607   /* If we could have a type and
5608      we have nothing or we need a type and have none.  */
5609   if (BINDING_TYPE (binding)
5610       && (!val || ((flags & LOOKUP_PREFER_TYPES)
5611                    && TREE_CODE (val) != TYPE_DECL)))
5612     val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5613   /* Don't return non-types if we really prefer types.  */
5614   else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5615            && (TREE_CODE (val) != TEMPLATE_DECL
5616                || !DECL_CLASS_TEMPLATE_P (val)))
5617     val = NULL_TREE;
5618
5619   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5620 }
5621
5622 /* Unscoped lookup of a global: iterate over current namespaces,
5623    considering using-directives.  If SPACESP is non-NULL, store a list
5624    of the namespaces we've considered in it.  */
5625
5626 tree
5627 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5628 {
5629   tree initial = current_decl_namespace ();
5630   tree scope = initial;
5631   tree siter;
5632   struct cp_binding_level *level;
5633   tree val = NULL_TREE;
5634   cxx_binding binding;
5635
5636   timevar_push (TV_NAME_LOOKUP);
5637   cxx_binding_clear (&binding);
5638   if (spacesp)
5639     *spacesp = NULL_TREE;
5640
5641   for (; !val; scope = CP_DECL_CONTEXT (scope))
5642     {
5643       cxx_binding *b =
5644          cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5645       if (spacesp)
5646         *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5647
5648       /* Ignore anticipated built-in functions.  */
5649       if (b && BINDING_VALUE (b)
5650           && DECL_P (BINDING_VALUE (b))
5651           && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5652           && DECL_ANTICIPATED (BINDING_VALUE (b)))
5653         /* Keep binding cleared.  */;
5654       else if (b)
5655         {
5656           /* Initialize binding for this context.  */
5657           binding.value = BINDING_VALUE (b);
5658           binding.type = BINDING_TYPE (b);
5659         }
5660
5661       /* Add all _DECLs seen through local using-directives.  */
5662       for (level = current_binding_level;
5663            !level->namespace_p;
5664            level = level->level_chain)
5665         if (!lookup_using_namespace (name, &binding, level->using_directives,
5666                                      scope, flags, spacesp))
5667           /* Give up because of error.  */
5668           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5669
5670       /* Add all _DECLs seen through global using-directives.  */
5671       /* XXX local and global using lists should work equally.  */
5672       siter = initial;
5673       while (1)
5674         {
5675           if (!lookup_using_namespace (name, &binding,
5676                                        DECL_NAMESPACE_USING (siter),
5677                                        scope, flags, spacesp))
5678             /* Give up because of error.  */
5679             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5680           if (siter == scope) break;
5681           siter = CP_DECL_CONTEXT (siter);
5682         }
5683
5684       val = select_decl (&binding, flags);
5685       if (scope == global_namespace)
5686         break;
5687     }
5688   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5689 }
5690
5691 /* Combine prefer_type and namespaces_only into flags.  */
5692
5693 static int
5694 lookup_flags (int prefer_type, int namespaces_only)
5695 {
5696   if (namespaces_only)
5697     return LOOKUP_PREFER_NAMESPACES;
5698   if (prefer_type > 1)
5699     return LOOKUP_PREFER_TYPES;
5700   if (prefer_type > 0)
5701     return LOOKUP_PREFER_BOTH;
5702   return 0;
5703 }
5704
5705 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5706    ignore it or not.  Subroutine of lookup_name_real.  */
5707
5708 static tree
5709 qualify_lookup (tree val, int flags)
5710 {
5711   if (val == NULL_TREE)
5712     return val;
5713   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5714     return val;
5715   if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5716     return val;
5717   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5718     return NULL_TREE;
5719   return val;
5720 }
5721
5722 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5723    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
5724    bindings.  
5725
5726    Returns a DECL (or OVERLOAD, or BASELINK) representing the
5727    declaration found.  */
5728
5729 tree
5730 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5731 {
5732   if (TREE_CODE (scope) == NAMESPACE_DECL)
5733     {
5734       cxx_binding binding;
5735
5736       cxx_binding_clear (&binding);
5737       flags |= LOOKUP_COMPLAIN;
5738       if (is_type_p)
5739         flags |= LOOKUP_PREFER_TYPES;
5740       if (!qualified_lookup_using_namespace (name, scope, &binding, flags))
5741         return NULL_TREE;
5742       return select_decl (&binding, flags);
5743     }
5744   else
5745     return lookup_member (scope, name, 0, is_type_p);
5746 }
5747
5748 /* Check to see whether or not DECL is a variable that would have been
5749    in scope under the ARM, but is not in scope under the ANSI/ISO
5750    standard.  If so, issue an error message.  If name lookup would
5751    work in both cases, but return a different result, this function
5752    returns the result of ANSI/ISO lookup.  Otherwise, it returns
5753    DECL.  */
5754
5755 tree
5756 check_for_out_of_scope_variable (tree decl)
5757 {
5758   tree shadowed;
5759
5760   /* We only care about out of scope variables.  */
5761   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5762     return decl;
5763
5764   shadowed = DECL_SHADOWED_FOR_VAR (decl);
5765   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5766          && DECL_DEAD_FOR_LOCAL (shadowed))
5767     shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5768   if (!shadowed)
5769     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5770   if (shadowed)
5771     {
5772       if (!DECL_ERROR_REPORTED (decl))
5773         {
5774           warning ("name lookup of `%D' changed",
5775                       DECL_NAME (decl));
5776           cp_warning_at ("  matches this `%D' under ISO standard rules",
5777                          shadowed);
5778           cp_warning_at ("  matches this `%D' under old rules", decl);
5779           DECL_ERROR_REPORTED (decl) = 1;
5780         }
5781       return shadowed;
5782     }
5783
5784   /* If we have already complained about this declaration, there's no
5785      need to do it again.  */
5786   if (DECL_ERROR_REPORTED (decl))
5787     return decl;
5788
5789   DECL_ERROR_REPORTED (decl) = 1;
5790   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5791     {
5792       error ("name lookup of `%D' changed for new ISO `for' scoping",
5793              DECL_NAME (decl));
5794       cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
5795       return error_mark_node;
5796     }
5797   else
5798     {
5799       pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5800                DECL_NAME (decl));
5801       cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
5802     }
5803
5804   return decl;
5805 }
5806
5807 /* Look up NAME in the current binding level and its superiors in the
5808    namespace of variables, functions and typedefs.  Return a ..._DECL
5809    node of some kind representing its definition if there is only one
5810    such declaration, or return a TREE_LIST with all the overloaded
5811    definitions if there are many, or return 0 if it is undefined.
5812
5813    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5814    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5815    Otherwise we prefer non-TYPE_DECLs.
5816
5817    If NONCLASS is nonzero, we don't look for the NAME in class scope,
5818    using IDENTIFIER_CLASS_VALUE.  */
5819
5820 tree
5821 lookup_name_real (tree name, int prefer_type, int nonclass, 
5822                   int namespaces_only, int flags)
5823 {
5824   cxx_binding *iter;
5825   tree val = NULL_TREE;
5826
5827   timevar_push (TV_NAME_LOOKUP);
5828   /* Conversion operators are handled specially because ordinary
5829      unqualified name lookup will not find template conversion
5830      operators.  */
5831   if (IDENTIFIER_TYPENAME_P (name)) 
5832     {
5833       struct cp_binding_level *level;
5834
5835       for (level = current_binding_level; 
5836            level && !level->namespace_p; 
5837            level = level->level_chain)
5838         {
5839           tree class_type;
5840           tree operators;
5841           
5842           /* A conversion operator can only be declared in a class 
5843              scope.  */
5844           if (level->parm_flag != 2)
5845             continue;
5846           
5847           /* Lookup the conversion operator in the class.  */
5848           class_type = level->this_class;
5849           operators = lookup_fnfields (class_type, name, /*protect=*/0);
5850           if (operators)
5851             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5852         }
5853
5854       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5855     }
5856
5857   flags |= lookup_flags (prefer_type, namespaces_only);
5858
5859   /* First, look in non-namespace scopes.  */
5860
5861   if (current_class_type == NULL_TREE)
5862     nonclass = 1;
5863
5864   for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5865     {
5866       tree binding;
5867
5868       if (!LOCAL_BINDING_P (iter) && nonclass)
5869         /* We're not looking for class-scoped bindings, so keep going.  */
5870         continue;
5871
5872       /* If this is the kind of thing we're looking for, we're done.  */
5873       if (qualify_lookup (BINDING_VALUE (iter), flags))
5874         binding = BINDING_VALUE (iter);
5875       else if ((flags & LOOKUP_PREFER_TYPES)
5876                && qualify_lookup (BINDING_TYPE (iter), flags))
5877         binding = BINDING_TYPE (iter);
5878       else
5879         binding = NULL_TREE;
5880
5881       if (binding)
5882         {
5883           val = binding;
5884           break;
5885         }
5886     }
5887
5888   /* Now lookup in namespace scopes.  */
5889   if (!val)
5890     {
5891       tree t = unqualified_namespace_lookup (name, flags, 0);
5892       if (t)
5893         val = t;
5894     }
5895
5896   if (val)
5897     {
5898       /* If we have a single function from a using decl, pull it out.  */
5899       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5900         val = OVL_FUNCTION (val);
5901     }
5902
5903   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5904 }
5905
5906 tree
5907 lookup_name_nonclass (tree name)
5908 {
5909   return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5910 }
5911
5912 tree
5913 lookup_function_nonclass (tree name, tree args)
5914 {
5915   return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5916 }
5917
5918 tree
5919 lookup_name (tree name, int prefer_type)
5920 {
5921   return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5922 }
5923
5924 /* Similar to `lookup_name' but look only in the innermost non-class
5925    binding level.  */
5926
5927 tree
5928 lookup_name_current_level (tree name)
5929 {
5930   struct cp_binding_level *b;
5931   tree t = NULL_TREE;
5932
5933   timevar_push (TV_NAME_LOOKUP);
5934   b = current_binding_level;
5935   while (b->parm_flag == 2)
5936     b = b->level_chain;
5937
5938   if (b->namespace_p)
5939     {
5940       t = IDENTIFIER_NAMESPACE_VALUE (name);
5941
5942       /* extern "C" function() */
5943       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5944         t = TREE_VALUE (t);
5945     }
5946   else if (IDENTIFIER_BINDING (name)
5947            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5948     {
5949       while (1)
5950         {
5951           if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b)
5952             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
5953
5954           if (b->keep == 2)
5955             b = b->level_chain;
5956           else
5957             break;
5958         }
5959     }
5960
5961   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5962 }
5963
5964 /* Like lookup_name_current_level, but for types.  */
5965
5966 tree
5967 lookup_type_current_level (tree name)
5968 {
5969   register tree t = NULL_TREE;
5970
5971   timevar_push (TV_NAME_LOOKUP);
5972   my_friendly_assert (! current_binding_level->namespace_p, 980716);
5973
5974   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5975       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5976     {
5977       struct cp_binding_level *b = current_binding_level;
5978       while (1)
5979         {
5980           if (purpose_member (name, b->type_shadowed))
5981             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5982                                     REAL_IDENTIFIER_TYPE_VALUE (name));
5983           if (b->keep == 2)
5984             b = b->level_chain;
5985           else
5986             break;
5987         }
5988     }
5989
5990   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5991 }
5992
5993 \f
5994 /* Push the declarations of builtin types into the namespace.
5995    RID_INDEX is the index of the builtin type
5996    in the array RID_POINTERS.  NAME is the name used when looking
5997    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
5998
5999 void
6000 record_builtin_type (enum rid rid_index, 
6001                      const char* name, 
6002                      tree type)
6003 {
6004   tree rname = NULL_TREE, tname = NULL_TREE;
6005   tree tdecl = NULL_TREE;
6006
6007   if ((int) rid_index < (int) RID_MAX)
6008     rname = ridpointers[(int) rid_index];
6009   if (name)
6010     tname = get_identifier (name);
6011
6012   if (tname)
6013     {
6014       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6015       set_identifier_type_value (tname, NULL_TREE);
6016       if ((int) rid_index < (int) RID_MAX)
6017         /* Built-in types live in the global namespace.  */
6018         SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6019     }
6020   if (rname != NULL_TREE)
6021     {
6022       if (tname != NULL_TREE)
6023         {
6024           set_identifier_type_value (rname, NULL_TREE);
6025           SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6026         }
6027       else
6028         {
6029           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6030           set_identifier_type_value (rname, NULL_TREE);
6031         }
6032     }
6033 }
6034
6035 /* Record one of the standard Java types.
6036  * Declare it as having the given NAME.
6037  * If SIZE > 0, it is the size of one of the integral types;
6038  * otherwise it is the negative of the size of one of the other types.  */
6039
6040 static tree
6041 record_builtin_java_type (const char* name, int size)
6042 {
6043   tree type, decl;
6044   if (size > 0)
6045     type = make_signed_type (size);
6046   else if (size > -32)
6047     { /* "__java_char" or ""__java_boolean".  */
6048       type = make_unsigned_type (-size);
6049       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6050     }
6051   else
6052     { /* "__java_float" or ""__java_double".  */
6053       type = make_node (REAL_TYPE);
6054       TYPE_PRECISION (type) = - size;
6055       layout_type (type);
6056     }
6057   record_builtin_type (RID_MAX, name, type);
6058   decl = TYPE_NAME (type);
6059
6060   /* Suppress generate debug symbol entries for these types,
6061      since for normal C++ they are just clutter.
6062      However, push_lang_context undoes this if extern "Java" is seen.  */
6063   DECL_IGNORED_P (decl) = 1;
6064
6065   TYPE_FOR_JAVA (type) = 1;
6066   return type;
6067 }
6068
6069 /* Push a type into the namespace so that the back-ends ignore it.  */
6070
6071 static void
6072 record_unknown_type (tree type, const char* name)
6073 {
6074   tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6075   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6076   DECL_IGNORED_P (decl) = 1;
6077   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6078   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6079   TYPE_ALIGN (type) = 1;
6080   TYPE_USER_ALIGN (type) = 0;
6081   TYPE_MODE (type) = TYPE_MODE (void_type_node);
6082 }
6083
6084 /* An string for which we should create an IDENTIFIER_NODE at
6085    startup.  */
6086
6087 typedef struct predefined_identifier
6088 {
6089   /* The name of the identifier.  */
6090   const char *const name;
6091   /* The place where the IDENTIFIER_NODE should be stored.  */
6092   tree *const node;
6093   /* Nonzero if this is the name of a constructor or destructor.  */
6094   const int ctor_or_dtor_p;
6095 } predefined_identifier;
6096
6097 /* Create all the predefined identifiers.  */
6098
6099 static void
6100 initialize_predefined_identifiers (void)
6101 {
6102   const predefined_identifier *pid;
6103
6104   /* A table of identifiers to create at startup.  */
6105   static const predefined_identifier predefined_identifiers[] = {
6106     { "C++", &lang_name_cplusplus, 0 },
6107     { "C", &lang_name_c, 0 },
6108     { "Java", &lang_name_java, 0 },
6109     { CTOR_NAME, &ctor_identifier, 1 },
6110     { "__base_ctor", &base_ctor_identifier, 1 },
6111     { "__comp_ctor", &complete_ctor_identifier, 1 },
6112     { DTOR_NAME, &dtor_identifier, 1 },
6113     { "__comp_dtor", &complete_dtor_identifier, 1 },
6114     { "__base_dtor", &base_dtor_identifier, 1 },
6115     { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6116     { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6117     { "nelts", &nelts_identifier, 0 },
6118     { THIS_NAME, &this_identifier, 0 },
6119     { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6120     { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6121     { "_vptr", &vptr_identifier, 0 },
6122     { "__vtt_parm", &vtt_parm_identifier, 0 },
6123     { "std", &std_identifier, 0 },
6124     { NULL, NULL, 0 }
6125   };
6126
6127   for (pid = predefined_identifiers; pid->name; ++pid)
6128     {
6129       *pid->node = get_identifier (pid->name);
6130       if (pid->ctor_or_dtor_p)
6131         IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6132     }
6133 }
6134
6135 /* Create the predefined scalar types of C,
6136    and some nodes representing standard constants (0, 1, (void *)0).
6137    Initialize the global binding level.
6138    Make definitions for built-in primitive functions.  */
6139
6140 void
6141 cxx_init_decl_processing (void)
6142 {
6143   tree void_ftype;
6144   tree void_ftype_ptr;
6145
6146   /* Create all the identifiers we need.  */
6147   initialize_predefined_identifiers ();
6148
6149   /* Fill in back-end hooks.  */
6150   lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6151
6152   /* Create the global variables.  */
6153   push_to_top_level ();
6154
6155   /* Enter the global namespace.  */
6156   my_friendly_assert (global_namespace == NULL_TREE, 375);
6157   push_namespace (get_identifier ("::"));
6158   global_namespace = current_namespace;
6159   current_lang_name = NULL_TREE;
6160
6161   /* Adjust various flags based on command-line settings.  */
6162   if (! flag_permissive && ! pedantic)
6163     flag_pedantic_errors = 1;
6164   if (!flag_no_inline)
6165     {
6166       flag_inline_trees = 1;
6167       flag_no_inline = 1;
6168     }
6169   if (flag_inline_functions)
6170     {
6171       flag_inline_trees = 2;
6172       flag_inline_functions = 0;
6173     }
6174
6175   /* Force minimum function alignment if using the least significant
6176      bit of function pointers to store the virtual bit.  */
6177   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6178       && force_align_functions_log < 1)
6179     force_align_functions_log = 1;
6180
6181   /* Initially, C.  */
6182   current_lang_name = lang_name_c;
6183
6184   current_function_decl = NULL_TREE;
6185   current_binding_level = NULL_BINDING_LEVEL;
6186   free_binding_level = NULL_BINDING_LEVEL;
6187
6188   build_common_tree_nodes (flag_signed_char);
6189
6190   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6191   TREE_TYPE (error_mark_list) = error_mark_node;
6192
6193   /* Make the binding_level structure for global names.  */
6194   pushlevel (0);
6195   global_binding_level = current_binding_level;
6196   /* The global level is the namespace level of ::.  */
6197   NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6198   declare_namespace_level ();
6199
6200   VARRAY_TREE_INIT (global_binding_level->static_decls,
6201                     200,
6202                     "Static declarations");
6203
6204   /* Create the `std' namespace.  */
6205   push_namespace (std_identifier);
6206   std_node = current_namespace;
6207   pop_namespace ();
6208
6209   c_common_nodes_and_builtins ();
6210
6211   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6212   java_short_type_node = record_builtin_java_type ("__java_short", 16);
6213   java_int_type_node = record_builtin_java_type ("__java_int", 32);
6214   java_long_type_node = record_builtin_java_type ("__java_long", 64);
6215   java_float_type_node = record_builtin_java_type ("__java_float", -32);
6216   java_double_type_node = record_builtin_java_type ("__java_double", -64);
6217   java_char_type_node = record_builtin_java_type ("__java_char", -16);
6218   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6219
6220   integer_two_node = build_int_2 (2, 0);
6221   TREE_TYPE (integer_two_node) = integer_type_node;
6222   integer_three_node = build_int_2 (3, 0);
6223   TREE_TYPE (integer_three_node) = integer_type_node;
6224
6225   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6226   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6227   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6228   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6229   TYPE_PRECISION (boolean_type_node) = 1;
6230   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6231   boolean_false_node = build_int_2 (0, 0);
6232   TREE_TYPE (boolean_false_node) = boolean_type_node;
6233   boolean_true_node = build_int_2 (1, 0);
6234   TREE_TYPE (boolean_true_node) = boolean_type_node;
6235
6236   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6237
6238 #if 0
6239   record_builtin_type (RID_MAX, NULL, string_type_node);
6240 #endif
6241
6242   delta_type_node = ptrdiff_type_node;
6243   vtable_index_type = ptrdiff_type_node;
6244
6245   vtt_parm_type = build_pointer_type (const_ptr_type_node);
6246   void_ftype = build_function_type (void_type_node, void_list_node);
6247   void_ftype_ptr = build_function_type (void_type_node,
6248                                         tree_cons (NULL_TREE,
6249                                                    ptr_type_node, 
6250                                                    void_list_node));
6251   void_ftype_ptr
6252     = build_exception_variant (void_ftype_ptr, empty_except_spec);
6253
6254   /* C++ extensions */
6255
6256   unknown_type_node = make_node (UNKNOWN_TYPE);
6257   record_unknown_type (unknown_type_node, "unknown type");
6258
6259   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6260   TREE_TYPE (unknown_type_node) = unknown_type_node;
6261
6262   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6263      result.  */
6264   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6265   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6266
6267   {
6268     /* Make sure we get a unique function type, so we can give
6269        its pointer type a name.  (This wins for gdb.) */
6270     tree vfunc_type = make_node (FUNCTION_TYPE);
6271     TREE_TYPE (vfunc_type) = integer_type_node;
6272     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6273     layout_type (vfunc_type);
6274
6275     vtable_entry_type = build_pointer_type (vfunc_type);
6276   }
6277   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6278
6279   vtbl_type_node
6280     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6281   layout_type (vtbl_type_node);
6282   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6283   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6284   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6285   layout_type (vtbl_ptr_type_node);
6286   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6287
6288   push_namespace (get_identifier ("__cxxabiv1"));
6289   abi_node = current_namespace;
6290   pop_namespace ();
6291
6292   global_type_node = make_node (LANG_TYPE);
6293   record_unknown_type (global_type_node, "global type");
6294
6295   /* Now, C++.  */
6296   current_lang_name = lang_name_cplusplus;
6297
6298   {
6299     tree bad_alloc_type_node, newtype, deltype;
6300     tree ptr_ftype_sizetype;
6301
6302     push_namespace (std_identifier);
6303     bad_alloc_type_node 
6304       = xref_tag (class_type, get_identifier ("bad_alloc"), 
6305                   /*attributes=*/NULL_TREE, 1);
6306     pop_namespace ();
6307     ptr_ftype_sizetype 
6308       = build_function_type (ptr_type_node,
6309                              tree_cons (NULL_TREE,
6310                                         size_type_node,
6311                                         void_list_node));
6312     newtype = build_exception_variant
6313       (ptr_ftype_sizetype, add_exception_specifier
6314        (NULL_TREE, bad_alloc_type_node, -1));
6315     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6316     push_cp_library_fn (NEW_EXPR, newtype);
6317     push_cp_library_fn (VEC_NEW_EXPR, newtype);
6318     global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6319     push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6320   }
6321
6322   abort_fndecl
6323     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6324
6325   /* Perform other language dependent initializations.  */
6326   init_class_processing ();
6327   init_search_processing ();
6328   init_rtti_processing ();
6329
6330   if (flag_exceptions)
6331     init_exception_processing ();
6332
6333   if (! supports_one_only ())
6334     flag_weak = 0;
6335
6336   make_fname_decl = cp_make_fname_decl;
6337   start_fname_decls ();
6338
6339   /* Show we use EH for cleanups.  */
6340   using_eh_for_cleanups ();
6341
6342   /* Maintain consistency.  Perhaps we should just complain if they
6343      say -fwritable-strings?  */
6344   if (flag_writable_strings)
6345     flag_const_strings = 0;
6346 }
6347
6348 /* Generate an initializer for a function naming variable from
6349    NAME. NAME may be NULL, in which case we generate a special
6350    ERROR_MARK node which should be replaced later.  */
6351
6352 tree
6353 cp_fname_init (const char* name)
6354 {
6355   tree domain = NULL_TREE;
6356   tree type;
6357   tree init = NULL_TREE;
6358   size_t length = 0;
6359
6360   if (name)
6361     {
6362       length = strlen (name);
6363       domain = build_index_type (size_int (length));
6364       init = build_string (length + 1, name);
6365     }
6366   
6367   type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6368   type = build_cplus_array_type (type, domain);
6369
6370   if (init)
6371     TREE_TYPE (init) = type;
6372   else
6373     /* We don't know the value until instantiation time. Make
6374        something which will be digested now, but replaced later.  */
6375     init = build (ERROR_MARK, type);
6376   
6377   return init;
6378 }
6379
6380 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6381    decl, NAME is the initialization string and TYPE_DEP indicates whether
6382    NAME depended on the type of the function. We make use of that to detect
6383    __PRETTY_FUNCTION__ inside a template fn. This is being done
6384    lazily at the point of first use, so we musn't push the decl now.  */
6385
6386 static tree
6387 cp_make_fname_decl (tree id, int type_dep)
6388 {
6389   const char *const name = (type_dep && processing_template_decl
6390                       ? NULL : fname_as_string (type_dep));
6391   tree init = cp_fname_init (name);
6392   tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6393
6394   /* As we're using pushdecl_with_scope, we must set the context.  */
6395   DECL_CONTEXT (decl) = current_function_decl;
6396   DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6397       
6398   TREE_STATIC (decl) = 1;
6399   TREE_READONLY (decl) = 1;
6400   DECL_ARTIFICIAL (decl) = 1;
6401   DECL_INITIAL (decl) = init;
6402   
6403   TREE_USED (decl) = 1;
6404
6405   if (current_function_decl)
6406     {
6407       struct cp_binding_level *b = current_binding_level;
6408       while (b->level_chain->parm_flag == 0)
6409         b = b->level_chain;
6410       pushdecl_with_scope (decl, b);
6411     }   
6412
6413   cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6414       
6415   return decl;
6416 }
6417
6418 /* Make a definition for a builtin function named NAME in the current
6419    namespace, whose data type is TYPE and whose context is CONTEXT.
6420    TYPE should be a function type with argument types.
6421
6422    CLASS and CODE tell later passes how to compile calls to this function.
6423    See tree.h for possible values.
6424
6425    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6426    the name to be called if we can't opencode the function.
6427    If ATTRS is nonzero, use that for the function's attribute
6428    list.  */
6429
6430 static tree
6431 builtin_function_1 (const char* name,
6432                     tree type,
6433                     tree context,
6434                     int code,
6435                     enum built_in_class class,
6436                     const char* libname,
6437                     tree attrs)
6438 {
6439   tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6440   DECL_BUILT_IN_CLASS (decl) = class;
6441   DECL_FUNCTION_CODE (decl) = code;
6442   DECL_CONTEXT (decl) = context;
6443
6444   pushdecl (decl);
6445
6446   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6447      we cannot change DECL_ASSEMBLER_NAME until we have installed this
6448      function in the namespace.  */
6449   if (libname)
6450     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6451   make_decl_rtl (decl, NULL);
6452
6453   /* Warn if a function in the namespace for users
6454      is used without an occasion to consider it declared.  */
6455   if (name[0] != '_' || name[1] != '_')
6456     DECL_ANTICIPATED (decl) = 1;
6457
6458   /* Possibly apply some default attributes to this built-in function.  */
6459   if (attrs)
6460     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6461   else
6462     decl_attributes (&decl, NULL_TREE, 0);
6463
6464   return decl;
6465 }
6466
6467 /* Entry point for the benefit of c_common_nodes_and_builtins.
6468
6469    Make a defintion for a builtin function named NAME and whose data type
6470    is TYPE.  TYPE should be a function type with argument types.  This
6471    function places the anticipated declaration in the global namespace
6472    and additionally in the std namespace if appropriate.
6473
6474    CLASS and CODE tell later passes how to compile calls to this function.
6475    See tree.h for possible values.
6476
6477    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6478    the name to be called if we can't opencode the function.
6479
6480    If ATTRS is nonzero, use that for the function's attribute
6481    list.  */
6482
6483 tree
6484 builtin_function (const char* name,
6485                   tree type,
6486                   int code,
6487                   enum built_in_class class,
6488                   const char* libname,
6489                   tree attrs)
6490 {
6491   /* All builtins that don't begin with an '_' should additionally
6492      go in the 'std' namespace.  */
6493   if (name[0] != '_')
6494     {
6495       push_namespace (std_identifier);
6496       builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6497       pop_namespace ();
6498     }
6499
6500   return builtin_function_1 (name, type, NULL_TREE, code,
6501                              class, libname, attrs);
6502 }
6503
6504 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6505    function.  Not called directly.  */
6506
6507 static tree
6508 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6509 {
6510   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6511   DECL_EXTERNAL (fn) = 1;
6512   TREE_PUBLIC (fn) = 1;
6513   DECL_ARTIFICIAL (fn) = 1;
6514   TREE_NOTHROW (fn) = 1;
6515   SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6516   SET_DECL_LANGUAGE (fn, lang_c);
6517   return fn;
6518 }
6519
6520 /* Returns the _DECL for a library function with C linkage.
6521    We assume that such functions never throw; if this is incorrect,
6522    callers should unset TREE_NOTHROW.  */
6523
6524 tree
6525 build_library_fn (tree name, tree type)
6526 {
6527   return build_library_fn_1 (name, ERROR_MARK, type);
6528 }
6529
6530 /* Returns the _DECL for a library function with C++ linkage.  */
6531
6532 static tree
6533 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6534 {
6535   tree fn = build_library_fn_1 (name, operator_code, type);
6536   TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6537   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6538   SET_DECL_LANGUAGE (fn, lang_cplusplus);
6539   set_mangled_name_for_decl (fn);
6540   return fn;
6541 }
6542
6543 /* Like build_library_fn, but takes a C string instead of an
6544    IDENTIFIER_NODE.  */
6545
6546 tree
6547 build_library_fn_ptr (const char* name, tree type)
6548 {
6549   return build_library_fn (get_identifier (name), type);
6550 }
6551
6552 /* Like build_cp_library_fn, but takes a C string instead of an
6553    IDENTIFIER_NODE.  */
6554
6555 tree
6556 build_cp_library_fn_ptr (const char* name, tree type)
6557 {
6558   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6559 }
6560
6561 /* Like build_library_fn, but also pushes the function so that we will
6562    be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
6563
6564 tree
6565 push_library_fn (tree name, tree type)
6566 {
6567   tree fn = build_library_fn (name, type);
6568   pushdecl_top_level (fn);
6569   return fn;
6570 }
6571
6572 /* Like build_cp_library_fn, but also pushes the function so that it
6573    will be found by normal lookup.  */
6574
6575 static tree
6576 push_cp_library_fn (enum tree_code operator_code, tree type)
6577 {
6578   tree fn = build_cp_library_fn (ansi_opname (operator_code),
6579                                  operator_code,
6580                                  type);
6581   pushdecl (fn);
6582   return fn;
6583 }
6584
6585 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6586    a FUNCTION_TYPE.  */
6587
6588 tree
6589 push_void_library_fn (tree name, tree parmtypes)
6590 {
6591   tree type = build_function_type (void_type_node, parmtypes);
6592   return push_library_fn (name, type);
6593 }
6594
6595 /* Like push_library_fn, but also note that this function throws
6596    and does not return.  Used for __throw_foo and the like.  */
6597
6598 tree
6599 push_throw_library_fn (tree name, tree type)
6600 {
6601   tree fn = push_library_fn (name, type);
6602   TREE_THIS_VOLATILE (fn) = 1;
6603   TREE_NOTHROW (fn) = 0;
6604   return fn;
6605 }
6606
6607 /* Apply default attributes to a function, if a system function with default
6608    attributes.  */
6609
6610 void
6611 cxx_insert_default_attributes (tree decl)
6612 {
6613   if (!DECL_EXTERN_C_FUNCTION_P (decl))
6614     return;
6615   if (!TREE_PUBLIC (decl))
6616     return;
6617   c_common_insert_default_attributes (decl);
6618 }
6619 \f
6620 /* When we call finish_struct for an anonymous union, we create
6621    default copy constructors and such.  But, an anonymous union
6622    shouldn't have such things; this function undoes the damage to the
6623    anonymous union type T.
6624
6625    (The reason that we create the synthesized methods is that we don't
6626    distinguish `union { int i; }' from `typedef union { int i; } U'.
6627    The first is an anonymous union; the second is just an ordinary
6628    union type.)  */
6629
6630 void
6631 fixup_anonymous_aggr (tree t)
6632 {
6633   tree *q;
6634
6635   /* Wipe out memory of synthesized methods */
6636   TYPE_HAS_CONSTRUCTOR (t) = 0;
6637   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6638   TYPE_HAS_INIT_REF (t) = 0;
6639   TYPE_HAS_CONST_INIT_REF (t) = 0;
6640   TYPE_HAS_ASSIGN_REF (t) = 0;
6641   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6642
6643   /* Splice the implicitly generated functions out of the TYPE_METHODS
6644      list.  */
6645   q = &TYPE_METHODS (t);
6646   while (*q)
6647     {
6648       if (DECL_ARTIFICIAL (*q))
6649         *q = TREE_CHAIN (*q);
6650       else
6651         q = &TREE_CHAIN (*q);
6652     }
6653
6654   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
6655   if (TYPE_METHODS (t))
6656     cp_error_at ("an anonymous union cannot have function members", t);
6657
6658   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6659      assignment operators (because they cannot have these methods themselves).
6660      For anonymous unions this is already checked because they are not allowed
6661      in any union, otherwise we have to check it.  */
6662   if (TREE_CODE (t) != UNION_TYPE)
6663     {
6664       tree field, type;
6665
6666       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6667         if (TREE_CODE (field) == FIELD_DECL)
6668           {
6669             type = TREE_TYPE (field);
6670             if (CLASS_TYPE_P (type))
6671               {
6672                 if (TYPE_NEEDS_CONSTRUCTING (type))
6673                   cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6674                                field);
6675                 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6676                   cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6677                                field);
6678                 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6679                   cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6680                                field);
6681               }
6682           }
6683     }
6684 }
6685
6686 /* Make sure that a declaration with no declarator is well-formed, i.e.
6687    just declares a tagged type or anonymous union.
6688
6689    Returns the type declared; or NULL_TREE if none.  */
6690
6691 tree
6692 check_tag_decl (tree declspecs)
6693 {
6694   int found_type = 0;
6695   int saw_friend = 0;
6696   int saw_typedef = 0;
6697   tree ob_modifier = NULL_TREE;
6698   register tree link;
6699   /* If a class, struct, or enum type is declared by the DECLSPECS
6700      (i.e, if a class-specifier, enum-specifier, or non-typename
6701      elaborated-type-specifier appears in the DECLSPECS),
6702      DECLARED_TYPE is set to the corresponding type.  */
6703   tree declared_type = NULL_TREE;
6704   bool error_p = false;
6705
6706   for (link = declspecs; link; link = TREE_CHAIN (link))
6707     {
6708       tree value = TREE_VALUE (link);
6709
6710       if (TYPE_P (value)
6711           || TREE_CODE (value) == TYPE_DECL
6712           || (TREE_CODE (value) == IDENTIFIER_NODE
6713               && IDENTIFIER_GLOBAL_VALUE (value)
6714               && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6715         {
6716           ++found_type;
6717
6718           if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6719             {
6720               if (! in_system_header)
6721                 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6722               return NULL_TREE;
6723             }
6724
6725           if (TYPE_P (value)
6726               && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6727                   || TREE_CODE (value) == ENUMERAL_TYPE))
6728             {
6729               my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6730               declared_type = value;
6731             }
6732         }
6733       else if (value == ridpointers[(int) RID_TYPEDEF])
6734         saw_typedef = 1;
6735       else if (value == ridpointers[(int) RID_FRIEND])
6736         {
6737           if (current_class_type == NULL_TREE
6738               || current_scope () != current_class_type)
6739             ob_modifier = value;
6740           else
6741             saw_friend = 1;
6742         }
6743       else if (value == ridpointers[(int) RID_STATIC]
6744                || value == ridpointers[(int) RID_EXTERN]
6745                || value == ridpointers[(int) RID_AUTO]
6746                || value == ridpointers[(int) RID_REGISTER]
6747                || value == ridpointers[(int) RID_INLINE]
6748                || value == ridpointers[(int) RID_VIRTUAL]
6749                || value == ridpointers[(int) RID_CONST]
6750                || value == ridpointers[(int) RID_VOLATILE]
6751                || value == ridpointers[(int) RID_EXPLICIT]
6752                || value == ridpointers[(int) RID_THREAD])
6753         ob_modifier = value;
6754       else if (value == error_mark_node)
6755         error_p = true;
6756     }
6757
6758   if (found_type > 1)
6759     error ("multiple types in one declaration");
6760
6761   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6762     pedwarn ("declaration does not declare anything");
6763   /* Check for an anonymous union.  */
6764   else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6765            && TYPE_ANONYMOUS_P (declared_type))
6766     {
6767       /* 7/3 In a simple-declaration, the optional init-declarator-list
6768          can be omitted only when declaring a class (clause 9) or
6769          enumeration (7.2), that is, when the decl-specifier-seq contains
6770          either a class-specifier, an elaborated-type-specifier with
6771          a class-key (9.1), or an enum-specifier.  In these cases and
6772          whenever a class-specifier or enum-specifier is present in the
6773          decl-specifier-seq, the identifiers in these specifiers are among
6774          the names being declared by the declaration (as class-name,
6775          enum-names, or enumerators, depending on the syntax).  In such
6776          cases, and except for the declaration of an unnamed bit-field (9.6),
6777          the decl-specifier-seq shall introduce one or more names into the
6778          program, or shall redeclare a name introduced by a previous
6779          declaration.  [Example:
6780              enum { };            // ill-formed
6781              typedef class { };   // ill-formed
6782          --end example]  */
6783       if (saw_typedef)
6784         {
6785           error ("missing type-name in typedef-declaration");
6786           return NULL_TREE;
6787         }
6788       /* Anonymous unions are objects, so they can have specifiers.  */;
6789       SET_ANON_AGGR_TYPE_P (declared_type);
6790
6791       if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 
6792           && !in_system_header)
6793         pedwarn ("ISO C++ prohibits anonymous structs");
6794     }
6795
6796   else if (ob_modifier)
6797     {
6798       if (ob_modifier == ridpointers[(int) RID_INLINE]
6799           || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6800         error ("`%D' can only be specified for functions", ob_modifier);
6801       else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6802         error ("`%D' can only be specified inside a class", ob_modifier);
6803       else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6804         error ("`%D' can only be specified for constructors",
6805                   ob_modifier);
6806       else
6807         error ("`%D' can only be specified for objects and functions",
6808                   ob_modifier);
6809     }
6810
6811   return declared_type;
6812 }
6813
6814 /* Called when a declaration is seen that contains no names to declare.
6815    If its type is a reference to a structure, union or enum inherited
6816    from a containing scope, shadow that tag name for the current scope
6817    with a forward reference.
6818    If its type defines a new named structure or union
6819    or defines an enum, it is valid but we need not do anything here.
6820    Otherwise, it is an error.
6821
6822    C++: may have to grok the declspecs to learn about static,
6823    complain for anonymous unions.  
6824
6825    Returns the TYPE declared -- or NULL_TREE if none.  */
6826
6827 tree
6828 shadow_tag (tree declspecs)
6829 {
6830   tree t = check_tag_decl (declspecs);
6831
6832   if (!t)
6833     return NULL_TREE;
6834
6835   maybe_process_partial_specialization (t);
6836
6837   /* This is where the variables in an anonymous union are
6838      declared.  An anonymous union declaration looks like:
6839      union { ... } ;
6840      because there is no declarator after the union, the parser
6841      sends that declaration here.  */
6842   if (ANON_AGGR_TYPE_P (t))
6843     {
6844       fixup_anonymous_aggr (t);
6845
6846       if (TYPE_FIELDS (t))
6847         {
6848           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6849                                       NULL);
6850           finish_anon_union (decl);
6851         }
6852     }
6853
6854   return t;
6855 }
6856 \f
6857 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
6858
6859 tree
6860 groktypename (tree typename)
6861 {
6862   tree specs, attrs;
6863   tree type;
6864   if (TREE_CODE (typename) != TREE_LIST)
6865     return typename;
6866   split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6867   type = grokdeclarator (TREE_VALUE (typename), specs,
6868                          TYPENAME, 0, &attrs);
6869   if (attrs)
6870     cplus_decl_attributes (&type, attrs, 0);
6871   return type;
6872 }
6873
6874 /* Decode a declarator in an ordinary declaration or data definition.
6875    This is called as soon as the type information and variable name
6876    have been parsed, before parsing the initializer if any.
6877    Here we create the ..._DECL node, fill in its type,
6878    and put it on the list of decls for the current context.
6879    The ..._DECL node is returned as the value.
6880
6881    Exception: for arrays where the length is not specified,
6882    the type is left null, to be filled in by `cp_finish_decl'.
6883
6884    Function definitions do not come here; they go to start_function
6885    instead.  However, external and forward declarations of functions
6886    do go through here.  Structure field declarations are done by
6887    grokfield and not through here.  */
6888
6889 tree
6890 start_decl (tree declarator, 
6891             tree declspecs, 
6892             int initialized, 
6893             tree attributes, 
6894             tree prefix_attributes)
6895 {
6896   tree decl;
6897   register tree type, tem;
6898   tree context;
6899
6900   /* This should only be done once on the top most decl.  */
6901   if (have_extern_spec)
6902     {
6903       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6904                              declspecs);
6905       have_extern_spec = false;
6906     }
6907
6908   /* An object declared as __attribute__((deprecated)) suppresses
6909      warnings of uses of other deprecated items.  */
6910   if (lookup_attribute ("deprecated", attributes))
6911     deprecated_state = DEPRECATED_SUPPRESS;
6912
6913   attributes = chainon (attributes, prefix_attributes);
6914
6915   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6916                          &attributes);
6917
6918   deprecated_state = DEPRECATED_NORMAL;
6919
6920   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6921     return NULL_TREE;
6922
6923   type = TREE_TYPE (decl);
6924
6925   if (type == error_mark_node)
6926     return NULL_TREE;
6927
6928   context = DECL_CONTEXT (decl);
6929
6930   if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6931       && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6932     {
6933       /* When parsing the initializer, lookup should use the object's
6934          namespace.  */
6935       push_decl_namespace (context);
6936     }
6937
6938   /* We are only interested in class contexts, later.  */
6939   if (context && TREE_CODE (context) == NAMESPACE_DECL)
6940     context = NULL_TREE;
6941
6942   if (initialized)
6943     /* Is it valid for this decl to have an initializer at all?
6944        If not, set INITIALIZED to zero, which will indirectly
6945        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
6946     switch (TREE_CODE (decl))
6947       {
6948       case TYPE_DECL:
6949         error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6950         initialized = 0;
6951         break;
6952
6953       case FUNCTION_DECL:
6954         error ("function `%#D' is initialized like a variable", decl);
6955         initialized = 0;
6956         break;
6957
6958       default:
6959         break;
6960       }
6961
6962   if (initialized)
6963     {
6964       if (! toplevel_bindings_p ()
6965           && DECL_EXTERNAL (decl))
6966         warning ("declaration of `%#D' has `extern' and is initialized",
6967                     decl);
6968       DECL_EXTERNAL (decl) = 0;
6969       if (toplevel_bindings_p ())
6970         TREE_STATIC (decl) = 1;
6971
6972       /* Tell `pushdecl' this is an initialized decl
6973          even though we don't yet have the initializer expression.
6974          Also tell `cp_finish_decl' it may store the real initializer.  */
6975       DECL_INITIAL (decl) = error_mark_node;
6976     }
6977
6978   /* Set attributes here so if duplicate decl, will have proper attributes.  */
6979   cplus_decl_attributes (&decl, attributes, 0);
6980
6981   /* If #pragma weak was used, mark the decl weak now.  */
6982   if (current_binding_level == global_binding_level)
6983     maybe_apply_pragma_weak (decl);
6984
6985   if (TREE_CODE (decl) == FUNCTION_DECL
6986       && DECL_DECLARED_INLINE_P (decl)
6987       && DECL_UNINLINABLE (decl)
6988       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6989     warning ("%Hinline function '%D' given attribute noinline",
6990              &DECL_SOURCE_LOCATION (decl), decl);
6991
6992   if (context && COMPLETE_TYPE_P (complete_type (context)))
6993     {
6994       push_nested_class (context);
6995
6996       if (TREE_CODE (decl) == VAR_DECL)
6997         {
6998           tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6999           if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7000             error ("`%#D' is not a static member of `%#T'", decl, context);
7001           else
7002             {
7003               if (DECL_CONTEXT (field) != context)
7004                 {
7005                   if (!same_type_p (DECL_CONTEXT (field), context))
7006                     pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7007                              DECL_CONTEXT (field), DECL_NAME (decl),
7008                              context, DECL_NAME (decl));
7009                   DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7010                 }
7011               /* Static data member are tricky; an in-class initialization
7012                  still doesn't provide a definition, so the in-class
7013                  declaration will have DECL_EXTERNAL set, but will have an
7014                  initialization.  Thus, duplicate_decls won't warn
7015                  about this situation, and so we check here.  */
7016               if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7017                 error ("duplicate initialization of %D", decl);
7018               if (duplicate_decls (decl, field))
7019                 decl = field;
7020             }
7021         }
7022       else
7023         {
7024           tree field = check_classfn (context, decl);
7025           if (field && duplicate_decls (decl, field))
7026             decl = field;
7027         }
7028
7029       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7030       DECL_IN_AGGR_P (decl) = 0;
7031       if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7032           || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7033         {
7034           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7035           /* [temp.expl.spec] An explicit specialization of a static data
7036              member of a template is a definition if the declaration
7037              includes an initializer; otherwise, it is a declaration.
7038
7039              We check for processing_specialization so this only applies
7040              to the new specialization syntax.  */
7041           if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7042             DECL_EXTERNAL (decl) = 1;
7043         }
7044
7045       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7046         pedwarn ("declaration of `%#D' outside of class is not definition",
7047                     decl);
7048     }
7049
7050   /* Enter this declaration into the symbol table.  */
7051   tem = maybe_push_decl (decl);
7052
7053   if (processing_template_decl)
7054     tem = push_template_decl (tem);
7055
7056 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7057   /* Tell the back-end to use or not use .common as appropriate.  If we say
7058      -fconserve-space, we want this to save .data space, at the expense of
7059      wrong semantics.  If we say -fno-conserve-space, we want this to
7060      produce errors about redefs; to do this we force variables into the
7061      data segment.  */
7062   DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7063                         || !DECL_THREAD_LOCAL (tem))
7064                        && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7065 #endif
7066
7067   if (! processing_template_decl)
7068     start_decl_1 (tem);
7069
7070   return tem;
7071 }
7072
7073 void
7074 start_decl_1 (tree decl)
7075 {
7076   tree type = TREE_TYPE (decl);
7077   int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7078
7079   if (type == error_mark_node)
7080     return;
7081
7082   maybe_push_cleanup_level (type);
7083
7084   if (initialized)
7085     /* Is it valid for this decl to have an initializer at all?
7086        If not, set INITIALIZED to zero, which will indirectly
7087        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7088     {
7089       /* Don't allow initializations for incomplete types except for
7090          arrays which might be completed by the initialization.  */
7091       if (COMPLETE_TYPE_P (complete_type (type)))
7092         ;                       /* A complete type is ok.  */
7093       else if (TREE_CODE (type) != ARRAY_TYPE)
7094         {
7095           error ("variable `%#D' has initializer but incomplete type",
7096                     decl);
7097           initialized = 0;
7098           type = TREE_TYPE (decl) = error_mark_node;
7099         }
7100       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7101         {
7102           if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7103             error ("elements of array `%#D' have incomplete type", decl);
7104           /* else we already gave an error in start_decl.  */
7105           initialized = 0;
7106         }
7107     }
7108
7109   if (!initialized
7110       && TREE_CODE (decl) != TYPE_DECL
7111       && TREE_CODE (decl) != TEMPLATE_DECL
7112       && type != error_mark_node
7113       && IS_AGGR_TYPE (type)
7114       && ! DECL_EXTERNAL (decl))
7115     {
7116       if ((! processing_template_decl || ! uses_template_parms (type))
7117           && !COMPLETE_TYPE_P (complete_type (type)))
7118         {
7119           error ("aggregate `%#D' has incomplete type and cannot be defined",
7120                  decl);
7121           /* Change the type so that assemble_variable will give
7122              DECL an rtl we can live with: (mem (const_int 0)).  */
7123           type = TREE_TYPE (decl) = error_mark_node;
7124         }
7125       else
7126         {
7127           /* If any base type in the hierarchy of TYPE needs a constructor,
7128              then we set initialized to 1.  This way any nodes which are
7129              created for the purposes of initializing this aggregate
7130              will live as long as it does.  This is necessary for global
7131              aggregates which do not have their initializers processed until
7132              the end of the file.  */
7133           initialized = TYPE_NEEDS_CONSTRUCTING (type);
7134         }
7135     }
7136
7137   if (! initialized)
7138     DECL_INITIAL (decl) = NULL_TREE;
7139 }
7140
7141 /* Handle initialization of references.
7142    These three arguments are from `cp_finish_decl', and have the
7143    same meaning here that they do there.
7144
7145    Quotes on semantics can be found in ARM 8.4.3.  */
7146
7147 static tree
7148 grok_reference_init (tree decl, tree type, tree init)
7149 {
7150   tree tmp;
7151
7152   if (init == NULL_TREE)
7153     {
7154       if ((DECL_LANG_SPECIFIC (decl) == 0
7155            || DECL_IN_AGGR_P (decl) == 0)
7156           && ! DECL_THIS_EXTERN (decl))
7157         error ("`%D' declared as reference but not initialized", decl);
7158       return NULL_TREE;
7159     }
7160
7161   if (TREE_CODE (init) == CONSTRUCTOR)
7162     {
7163       error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7164       return NULL_TREE;
7165     }
7166
7167   if (TREE_CODE (init) == TREE_LIST)
7168     init = build_compound_expr (init);
7169
7170   if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7171     init = convert_from_reference (init);
7172
7173   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7174       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7175     {
7176       /* Note: default conversion is only called in very special cases.  */
7177       init = default_conversion (init);
7178     }
7179
7180   /* Convert INIT to the reference type TYPE.  This may involve the
7181      creation of a temporary, whose lifetime must be the same as that
7182      of the reference.  If so, a DECL_STMT for the temporary will be
7183      added just after the DECL_STMT for DECL.  That's why we don't set
7184      DECL_INITIAL for local references (instead assigning to them
7185      explicitly); we need to allow the temporary to be initialized
7186      first.  */
7187   tmp = initialize_reference (type, init, decl);
7188
7189   if (tmp == error_mark_node)
7190     return NULL_TREE;
7191   else if (tmp == NULL_TREE)
7192     {
7193       error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7194       return NULL_TREE;
7195     }
7196
7197   if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7198     return tmp;
7199
7200   DECL_INITIAL (decl) = tmp;
7201
7202   return NULL_TREE;
7203 }
7204
7205 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7206    array until we finish parsing the initializer.  If that's the
7207    situation we're in, update DECL accordingly.  */
7208
7209 static void
7210 maybe_deduce_size_from_array_init (tree decl, tree init)
7211 {
7212   tree type = TREE_TYPE (decl);
7213
7214   if (TREE_CODE (type) == ARRAY_TYPE
7215       && TYPE_DOMAIN (type) == NULL_TREE
7216       && TREE_CODE (decl) != TYPE_DECL)
7217     {
7218       /* do_default is really a C-ism to deal with tentative definitions.
7219          But let's leave it here to ease the eventual merge.  */
7220       int do_default = !DECL_EXTERNAL (decl);
7221       tree initializer = init ? init : DECL_INITIAL (decl);
7222       int failure = complete_array_type (type, initializer, do_default);
7223
7224       if (failure == 1)
7225         error ("initializer fails to determine size of `%D'", decl);
7226
7227       if (failure == 2)
7228         {
7229           if (do_default)
7230             error ("array size missing in `%D'", decl);
7231           /* If a `static' var's size isn't known, make it extern as
7232              well as static, so it does not get allocated.  If it's not
7233              `static', then don't mark it extern; finish_incomplete_decl
7234              will give it a default size and it will get allocated.  */
7235           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7236             DECL_EXTERNAL (decl) = 1;
7237         }
7238
7239       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7240           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7241                               integer_zero_node))
7242         error ("zero-size array `%D'", decl);
7243
7244       layout_decl (decl, 0);
7245     }
7246 }
7247
7248 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7249    any appropriate error messages regarding the layout.  */
7250
7251 static void
7252 layout_var_decl (tree decl)
7253 {
7254   tree type = TREE_TYPE (decl);
7255 #if 0
7256   tree ttype = target_type (type);
7257 #endif
7258
7259   /* If we haven't already layed out this declaration, do so now.
7260      Note that we must not call complete type for an external object
7261      because it's type might involve templates that we are not
7262      supposed to isntantiate yet.  (And it's perfectly valid to say
7263      `extern X x' for some incomplete type `X'.)  */
7264   if (!DECL_EXTERNAL (decl))
7265     complete_type (type);
7266   if (!DECL_SIZE (decl) 
7267       && TREE_TYPE (decl) != error_mark_node
7268       && (COMPLETE_TYPE_P (type)
7269           || (TREE_CODE (type) == ARRAY_TYPE 
7270               && !TYPE_DOMAIN (type)
7271               && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7272     layout_decl (decl, 0);
7273
7274   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7275     {
7276       /* An automatic variable with an incomplete type: that is an error.
7277          Don't talk about array types here, since we took care of that
7278          message in grokdeclarator.  */
7279       error ("storage size of `%D' isn't known", decl);
7280       TREE_TYPE (decl) = error_mark_node;
7281     }
7282 #if 0
7283   /* Keep this code around in case we later want to control debug info
7284      based on whether a type is "used".  (jason 1999-11-11) */
7285
7286   else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7287     /* Let debugger know it should output info for this type.  */
7288     note_debug_info_needed (ttype);
7289
7290   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7291     note_debug_info_needed (DECL_CONTEXT (decl));
7292 #endif
7293
7294   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7295       && DECL_SIZE (decl) != NULL_TREE
7296       && ! TREE_CONSTANT (DECL_SIZE (decl)))
7297     {
7298       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7299         constant_expression_warning (DECL_SIZE (decl));
7300       else
7301         error ("storage size of `%D' isn't constant", decl);
7302     }
7303
7304   if (TREE_STATIC (decl)
7305       && !DECL_ARTIFICIAL (decl)
7306       && current_function_decl
7307       && DECL_CONTEXT (decl) == current_function_decl)
7308     push_local_name (decl);
7309 }
7310
7311 /* If a local static variable is declared in an inline function, or if
7312    we have a weak definition, we must endeavor to create only one
7313    instance of the variable at link-time.  */
7314
7315 static void
7316 maybe_commonize_var (tree decl)
7317 {
7318   /* Static data in a function with comdat linkage also has comdat
7319      linkage.  */
7320   if (TREE_STATIC (decl)
7321       /* Don't mess with __FUNCTION__.  */
7322       && ! DECL_ARTIFICIAL (decl)
7323       && DECL_FUNCTION_SCOPE_P (decl)
7324       /* Unfortunately, import_export_decl has not always been called
7325          before the function is processed, so we cannot simply check
7326          DECL_COMDAT.  */ 
7327       && (DECL_COMDAT (DECL_CONTEXT (decl))
7328           || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7329                || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7330               && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7331     {
7332       if (flag_weak)
7333         {
7334           /* With weak symbols, we simply make the variable COMDAT;
7335              that will cause copies in multiple translations units to
7336              be merged.  */
7337           comdat_linkage (decl);
7338         }
7339       else
7340         {
7341           if (DECL_INITIAL (decl) == NULL_TREE
7342               || DECL_INITIAL (decl) == error_mark_node)
7343             {
7344               /* Without weak symbols, we can use COMMON to merge
7345                  uninitialized variables.  */
7346               TREE_PUBLIC (decl) = 1;
7347               DECL_COMMON (decl) = 1;
7348             }
7349           else
7350             {
7351               /* While for initialized variables, we must use internal
7352                  linkage -- which means that multiple copies will not
7353                  be merged.  */
7354               TREE_PUBLIC (decl) = 0;
7355               DECL_COMMON (decl) = 0;
7356               cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7357               cp_warning_at ("  you can work around this by removing the initializer", decl);
7358             }
7359         }
7360     }
7361   else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7362     /* Set it up again; we might have set DECL_INITIAL since the last
7363        time.  */
7364     comdat_linkage (decl);
7365 }
7366
7367 /* Issue an error message if DECL is an uninitialized const variable.  */
7368
7369 static void
7370 check_for_uninitialized_const_var (tree decl)
7371 {
7372   tree type = TREE_TYPE (decl);
7373
7374   /* ``Unless explicitly declared extern, a const object does not have
7375      external linkage and must be initialized. ($8.4; $12.1)'' ARM
7376      7.1.6 */
7377   if (TREE_CODE (decl) == VAR_DECL
7378       && TREE_CODE (type) != REFERENCE_TYPE
7379       && CP_TYPE_CONST_P (type)
7380       && !TYPE_NEEDS_CONSTRUCTING (type)
7381       && !DECL_INITIAL (decl))
7382     error ("uninitialized const `%D'", decl);
7383 }
7384
7385 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
7386    returned is the next FIELD_DECL (possibly FIELD itself) that can be
7387    initialized.  If there are no more such fields, the return value
7388    will be NULL.  */
7389
7390 static tree
7391 next_initializable_field (tree field)
7392 {
7393   while (field
7394          && (TREE_CODE (field) != FIELD_DECL
7395              || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7396              || DECL_ARTIFICIAL (field)))
7397     field = TREE_CHAIN (field);
7398
7399   return field;
7400 }
7401
7402 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7403    brace-enclosed aggregate initializer.
7404
7405    *INITP is one of a list of initializers describing a brace-enclosed
7406    initializer for an entity of the indicated aggregate TYPE.  It may
7407    not presently match the shape of the TYPE; for example:
7408    
7409      struct S { int a; int b; };
7410      struct S a[] = { 1, 2, 3, 4 };
7411
7412    Here *INITP will point to TREE_LIST of four elements, rather than a
7413    list of two elements, each itself a list of two elements.  This
7414    routine transforms INIT from the former form into the latter.  The
7415    revised initializer is returned.  */
7416
7417 static tree
7418 reshape_init (tree type, tree *initp)
7419 {
7420   tree inits;
7421   tree old_init;
7422   tree old_init_value;
7423   tree new_init;
7424   bool brace_enclosed_p;
7425
7426   old_init = *initp;
7427   old_init_value = (TREE_CODE (*initp) == TREE_LIST
7428                     ? TREE_VALUE (*initp) : old_init);
7429
7430   /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
7431   if (!old_init_value)
7432     {
7433       my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7434       TREE_VALUE (old_init) = error_mark_node;
7435       return old_init;
7436     }
7437
7438   /* If the initializer is brace-enclosed, pull initializers from the
7439      enclosed elements.  Advance past the brace-enclosed initializer
7440      now.  */
7441   if (TREE_CODE (old_init_value) == CONSTRUCTOR 
7442       && TREE_HAS_CONSTRUCTOR (old_init_value))
7443     {
7444       *initp = TREE_CHAIN (old_init);
7445       TREE_CHAIN (old_init) = NULL_TREE;
7446       inits = CONSTRUCTOR_ELTS (old_init_value);
7447       initp = &inits;
7448       brace_enclosed_p = true;
7449     }
7450   else
7451     {
7452       inits = NULL_TREE;
7453       brace_enclosed_p = false;
7454     }
7455
7456   /* A non-aggregate type is always initialized with a single
7457      initializer.  */
7458   if (!CP_AGGREGATE_TYPE_P (type))
7459       {
7460         *initp = TREE_CHAIN (old_init);
7461         TREE_CHAIN (old_init) = NULL_TREE;
7462         /* It is invalid to initialize a non-aggregate type with a
7463            brace-enclosed initializer.  */
7464         if (brace_enclosed_p)
7465           {
7466             error ("brace-enclosed initializer used to initialize `%T'",
7467                    type);
7468             if (TREE_CODE (old_init) == TREE_LIST)
7469               TREE_VALUE (old_init) = error_mark_node;
7470             else
7471               old_init = error_mark_node;
7472           }
7473         
7474         return old_init;
7475       }
7476
7477   /* [dcl.init.aggr]
7478
7479      All implicit type conversions (clause _conv_) are considered when
7480      initializing the aggregate member with an initializer from an
7481      initializer-list.  If the initializer can initialize a member,
7482      the member is initialized.  Otherwise, if the member is itself a
7483      non-empty subaggregate, brace elision is assumed and the
7484      initializer is considered for the initialization of the first
7485      member of the subaggregate.  */
7486   if (CLASS_TYPE_P (type) 
7487       && !brace_enclosed_p
7488       && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7489     {
7490       *initp = TREE_CHAIN (old_init);
7491       TREE_CHAIN (old_init) = NULL_TREE;
7492       return old_init;
7493     }
7494
7495   if (TREE_CODE (old_init_value) == STRING_CST
7496       && TREE_CODE (type) == ARRAY_TYPE
7497       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7498     {
7499       /* [dcl.init.string]
7500
7501          A char array (whether plain char, signed char, or unsigned char)
7502          can be initialized by a string-literal (optionally enclosed in
7503          braces); a wchar_t array can be initialized by a wide
7504          string-literal (optionally enclosed in braces).  */
7505       new_init = old_init;
7506       /* Move past the initializer.  */
7507       *initp = TREE_CHAIN (old_init);
7508       TREE_CHAIN (old_init) = NULL_TREE;
7509     }
7510   else
7511     {
7512       /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */  
7513       new_init = build_constructor (type, NULL_TREE);
7514       TREE_HAS_CONSTRUCTOR (new_init) = 1;
7515
7516       if (CLASS_TYPE_P (type))
7517         {
7518           tree field;
7519
7520           field = next_initializable_field (TYPE_FIELDS (type));
7521
7522           if (!field)
7523             {
7524               /* [dcl.init.aggr]
7525               
7526                  An initializer for an aggregate member that is an
7527                  empty class shall have the form of an empty
7528                  initializer-list {}.  */
7529               if (!brace_enclosed_p)
7530                 error ("initializer for `%T' must be brace-enclosed",
7531                        type);
7532             }
7533           else
7534             {
7535               /* Loop through the initializable fields, gathering
7536                  initializers.  */
7537               while (*initp)
7538                 {
7539                   tree field_init;
7540
7541                   /* Handle designated initializers, as an extension.  */
7542                   if (TREE_PURPOSE (*initp))
7543                     {
7544                       if (pedantic)
7545                         pedwarn ("ISO C++ does not allow designated initializers");
7546                       field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7547                                               /*want_type=*/false);
7548                       if (!field || TREE_CODE (field) != FIELD_DECL)
7549                         error ("`%T' has no non-static data member named `%D'",
7550                                type, TREE_PURPOSE (*initp));
7551                     }
7552                   if (!field)
7553                     break;
7554
7555                   field_init = reshape_init (TREE_TYPE (field), initp);
7556                   TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7557                   CONSTRUCTOR_ELTS (new_init) = field_init;
7558                   /* [dcl.init.aggr] 
7559
7560                      When a union  is  initialized with a brace-enclosed
7561                      initializer, the braces shall only contain an
7562                      initializer for the first member of the union.  */
7563                   if (TREE_CODE (type) == UNION_TYPE)
7564                     break;
7565                   field = next_initializable_field (TREE_CHAIN (field));
7566                 }
7567             }
7568         }
7569       else if (TREE_CODE (type) == ARRAY_TYPE)
7570         {
7571           tree index;
7572           tree max_index;
7573
7574           /* If the bound of the array is known, take no more initializers
7575              than are allowed.  */
7576           max_index = (TYPE_DOMAIN (type) 
7577                        ? array_type_nelts (type) : NULL_TREE);
7578           /* Loop through the array elements, gathering initializers.  */
7579           for (index = size_zero_node;
7580                *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7581                index = size_binop (PLUS_EXPR, index, size_one_node))
7582             {
7583               tree element_init;
7584
7585               element_init = reshape_init (TREE_TYPE (type), initp);
7586               TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7587               CONSTRUCTOR_ELTS (new_init) = element_init;
7588               if (TREE_PURPOSE (element_init))
7589                 index = TREE_PURPOSE (element_init);
7590             }
7591         }
7592       else
7593         abort ();
7594
7595       /* The initializers were placed in reverse order in the
7596          CONSTRUCTOR.  */
7597       CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7598
7599       if (TREE_CODE (old_init) == TREE_LIST)
7600         new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7601     }
7602
7603   /* If this was a brace-enclosed initializer and all of the
7604      initializers were not used up, there is a problem.  */
7605   if (brace_enclosed_p && *initp)
7606     error ("too many initializers for `%T'", type);
7607
7608   return new_init;
7609 }
7610
7611 /* Verify INIT (the initializer for DECL), and record the
7612    initialization in DECL_INITIAL, if appropriate.  
7613
7614    If the return value is non-NULL, it is an expression that must be
7615    evaluated dynamically to initialize DECL.  */
7616
7617 static tree
7618 check_initializer (tree decl, tree init, int flags)
7619 {
7620   tree type = TREE_TYPE (decl);
7621
7622   /* If `start_decl' didn't like having an initialization, ignore it now.  */
7623   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7624     init = NULL_TREE;
7625
7626   /* If an initializer is present, DECL_INITIAL has been
7627      error_mark_node, to indicate that an as-of-yet unevaluated
7628      initialization will occur.  From now on, DECL_INITIAL reflects
7629      the static initialization -- if any -- of DECL.  */
7630   DECL_INITIAL (decl) = NULL_TREE;
7631
7632   /* Things that are going to be initialized need to have complete
7633      type.  */
7634   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7635
7636   if (type == error_mark_node)
7637     /* We will have already complained.  */
7638     init = NULL_TREE;
7639   else if (init && COMPLETE_TYPE_P (type) 
7640            && !TREE_CONSTANT (TYPE_SIZE (type)))
7641     {
7642       error ("variable-sized object `%D' may not be initialized", decl);
7643       init = NULL_TREE;
7644     }
7645   else if (TREE_CODE (type) == ARRAY_TYPE
7646            && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7647     {
7648       error ("elements of array `%#D' have incomplete type", decl);
7649       init = NULL_TREE;
7650     }
7651   else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7652     {
7653       error ("`%D' has incomplete type", decl);
7654       TREE_TYPE (decl) = error_mark_node;
7655       init = NULL_TREE;
7656     }
7657
7658   if (TREE_CODE (decl) == CONST_DECL)
7659     {
7660       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7661
7662       DECL_INITIAL (decl) = init;
7663
7664       my_friendly_assert (init != NULL_TREE, 149);
7665       init = NULL_TREE;
7666     }
7667   else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7668     init = grok_reference_init (decl, type, init);
7669   else if (init)
7670     {
7671       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7672         {
7673           /* [dcl.init] paragraph 13,
7674              If T is a scalar type, then a declaration of the form
7675              T x = { a };
7676              is equivalent to
7677              T x = a;
7678              
7679              reshape_init will complain about the extra braces,
7680              and doesn't do anything useful in the case where TYPE is
7681              scalar, so just don't call it.  */
7682           if (CP_AGGREGATE_TYPE_P (type))
7683             init = reshape_init (type, &init);
7684
7685           if ((*targetm.vector_opaque_p) (type))
7686             {
7687               error ("opaque vector types cannot be initialized");
7688               init = error_mark_node;
7689             }
7690         }
7691
7692       /* If DECL has an array type without a specific bound, deduce the
7693          array size from the initializer.  */
7694       maybe_deduce_size_from_array_init (decl, init);
7695       type = TREE_TYPE (decl);
7696       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7697         TREE_TYPE (init) = type;
7698
7699       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7700         {
7701           if (TREE_CODE (type) == ARRAY_TYPE)
7702             goto initialize_aggr;
7703           else if (TREE_CODE (init) == CONSTRUCTOR
7704                    && TREE_HAS_CONSTRUCTOR (init))
7705             {
7706               if (TYPE_NON_AGGREGATE_CLASS (type))
7707                 {
7708                   error ("`%D' must be initialized by constructor, not by `{...}'",
7709                          decl);
7710                   init = error_mark_node;
7711                 }
7712               else
7713                 goto dont_use_constructor;
7714             }
7715           else
7716             {
7717               int saved_stmts_are_full_exprs_p;
7718
7719             initialize_aggr:
7720               saved_stmts_are_full_exprs_p = 0;
7721               if (building_stmt_tree ())
7722                 {
7723                   saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7724                   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7725                 }
7726               init = build_aggr_init (decl, init, flags);
7727               if (building_stmt_tree ())
7728                 current_stmt_tree ()->stmts_are_full_exprs_p =
7729                   saved_stmts_are_full_exprs_p;
7730               return init;
7731             }
7732         }
7733       else
7734         {
7735         dont_use_constructor:
7736           if (TREE_CODE (init) != TREE_VEC)
7737             init = store_init_value (decl, init);
7738         }
7739     }
7740   else if (DECL_EXTERNAL (decl))
7741     ;
7742   else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7743     goto initialize_aggr;
7744   else if (IS_AGGR_TYPE (type))
7745     {
7746       tree core_type = strip_array_types (type);
7747
7748       if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7749         error ("structure `%D' with uninitialized const members", decl);
7750       if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7751         error ("structure `%D' with uninitialized reference members",
7752                decl);
7753
7754       check_for_uninitialized_const_var (decl);
7755     }
7756   else
7757     check_for_uninitialized_const_var (decl);
7758
7759   if (init && init != error_mark_node)
7760     init = build (INIT_EXPR, type, decl, init);
7761
7762   return init;
7763 }
7764
7765 /* If DECL is not a local variable, give it RTL.  */
7766
7767 static void
7768 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7769 {
7770   int toplev = toplevel_bindings_p ();
7771   int defer_p;
7772
7773   /* Handle non-variables up front.  */
7774   if (TREE_CODE (decl) != VAR_DECL)
7775     {
7776       rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7777       return;
7778     }
7779
7780   /* If we see a class member here, it should be a static data
7781      member.  */
7782   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7783     {
7784       my_friendly_assert (TREE_STATIC (decl), 19990828);
7785       /* An in-class declaration of a static data member should be
7786          external; it is only a declaration, and not a definition.  */
7787       if (init == NULL_TREE)
7788         my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7789     }
7790
7791   /* Set the DECL_ASSEMBLER_NAME for the variable.  */
7792   if (asmspec)
7793     {
7794       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7795       /* The `register' keyword, when used together with an
7796          asm-specification, indicates that the variable should be
7797          placed in a particular register.  */
7798       if (DECL_REGISTER (decl))
7799         DECL_C_HARD_REGISTER (decl) = 1;
7800     }
7801
7802   /* We don't create any RTL for local variables.  */
7803   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7804     return;
7805
7806   /* We defer emission of local statics until the corresponding
7807      DECL_STMT is expanded.  */
7808   defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7809
7810   /* We try to defer namespace-scope static constants so that they are
7811      not emitted into the object file unnecessarily.  */
7812   if (!DECL_VIRTUAL_P (decl)
7813       && TREE_READONLY (decl)
7814       && DECL_INITIAL (decl) != NULL_TREE
7815       && DECL_INITIAL (decl) != error_mark_node
7816       && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7817       && toplev
7818       && !TREE_PUBLIC (decl))
7819     {
7820       /* Fool with the linkage of static consts according to #pragma
7821          interface.  */
7822       if (!interface_unknown && !TREE_PUBLIC (decl))
7823         {
7824           TREE_PUBLIC (decl) = 1;
7825           DECL_EXTERNAL (decl) = interface_only;
7826         }
7827
7828       defer_p = 1;
7829     }
7830   /* Likewise for template instantiations.  */
7831   else if (DECL_COMDAT (decl))
7832     defer_p = 1;
7833
7834   /* If we're deferring the variable, we only need to make RTL if
7835      there's an ASMSPEC.  Otherwise, we'll lazily create it later when
7836      we need it.  (There's no way to lazily create RTL for things that
7837      have assembly specs because the information about the specifier
7838      isn't stored in the tree, yet)  */
7839   if (defer_p && asmspec)
7840     make_decl_rtl (decl, asmspec);
7841   /* If we're not deferring, go ahead and assemble the variable.  */
7842   else if (!defer_p)
7843     rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7844 }
7845
7846 /* The old ARM scoping rules injected variables declared in the
7847    initialization statement of a for-statement into the surrounding
7848    scope.  We support this usage, in order to be backward-compatible.
7849    DECL is a just-declared VAR_DECL; if necessary inject its
7850    declaration into the surrounding scope.  */
7851
7852 void
7853 maybe_inject_for_scope_var (tree decl)
7854 {
7855   timevar_push (TV_NAME_LOOKUP);
7856   if (!DECL_NAME (decl))
7857     {
7858       timevar_pop (TV_NAME_LOOKUP);
7859       return;
7860     }
7861   
7862   /* Declarations of __FUNCTION__ and its ilk appear magically when
7863      the variable is first used.  If that happens to be inside a
7864      for-loop, we don't want to do anything special.  */
7865   if (DECL_PRETTY_FUNCTION_P (decl))
7866     {
7867       timevar_pop (TV_NAME_LOOKUP);
7868       return;
7869     }
7870
7871   if (current_binding_level->is_for_scope)
7872     {
7873       struct cp_binding_level *outer
7874         = current_binding_level->level_chain;
7875
7876       /* Check to see if the same name is already bound at the outer
7877          level, either because it was directly declared, or because a
7878          dead for-decl got preserved.  In either case, the code would
7879          not have been valid under the ARM scope rules, so clear
7880          is_for_scope for the current_binding_level.
7881
7882          Otherwise, we need to preserve the temp slot for decl to last
7883          into the outer binding level.  */
7884
7885       cxx_binding *outer_binding
7886         = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7887
7888       if (outer_binding && BINDING_SCOPE (outer_binding) == outer
7889           && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7890           && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7891         {
7892           BINDING_VALUE (outer_binding)
7893             = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7894           current_binding_level->is_for_scope = 0;
7895         }
7896     }
7897   timevar_pop (TV_NAME_LOOKUP);
7898 }
7899
7900 /* Generate code to initialize DECL (a local variable).  */
7901
7902 static void
7903 initialize_local_var (tree decl, tree init)
7904 {
7905   tree type = TREE_TYPE (decl);
7906
7907   my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7908                       || TREE_CODE (decl) == RESULT_DECL, 
7909                       20021010);
7910   my_friendly_assert (!TREE_STATIC (decl), 20021010);
7911
7912   if (DECL_SIZE (decl) == NULL_TREE)
7913     {
7914       /* If we used it already as memory, it must stay in memory.  */
7915       DECL_INITIAL (decl) = NULL_TREE;
7916       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7917     }
7918
7919   if (DECL_SIZE (decl) && type != error_mark_node)
7920     {
7921       int already_used;
7922
7923       /* Compute and store the initial value.  */
7924       already_used = TREE_USED (decl) || TREE_USED (type);
7925
7926       /* Perform the initialization.  */
7927       if (init)
7928         {
7929           int saved_stmts_are_full_exprs_p;
7930
7931           my_friendly_assert (building_stmt_tree (), 20000906);
7932           saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7933           current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7934           finish_expr_stmt (init);
7935           current_stmt_tree ()->stmts_are_full_exprs_p =
7936             saved_stmts_are_full_exprs_p;
7937         }
7938
7939       /* Set this to 0 so we can tell whether an aggregate which was
7940          initialized was ever used.  Don't do this if it has a
7941          destructor, so we don't complain about the 'resource
7942          allocation is initialization' idiom.  Now set
7943          attribute((unused)) on types so decls of that type will be
7944          marked used. (see TREE_USED, above.)  */
7945       if (TYPE_NEEDS_CONSTRUCTING (type)
7946           && ! already_used
7947           && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7948           && DECL_NAME (decl))
7949         TREE_USED (decl) = 0;
7950       else if (already_used)
7951         TREE_USED (decl) = 1;
7952     }
7953
7954   /* Generate a cleanup, if necessary.  */
7955   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7956     {
7957       tree cleanup;
7958
7959       /* Compute the cleanup.  */
7960       cleanup = cxx_maybe_build_cleanup (decl);
7961       
7962       /* Record the cleanup required for this declaration.  */
7963       if (DECL_SIZE (decl) && cleanup)
7964         finish_decl_cleanup (decl, cleanup);
7965     }
7966 }
7967
7968 /* Finish processing of a declaration;
7969    install its line number and initial value.
7970    If the length of an array type is not known before,
7971    it must be determined now, from the initial value, or it is an error.
7972
7973    INIT holds the value of an initializer that should be allowed to escape
7974    the normal rules.
7975
7976    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7977    if the (init) syntax was used.  */
7978
7979 void
7980 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
7981 {
7982   register tree type;
7983   tree ttype = NULL_TREE;
7984   const char *asmspec = NULL;
7985   int was_readonly = 0;
7986
7987   if (! decl)
7988     {
7989       if (init)
7990         error ("assignment (not initialization) in declaration");
7991       return;
7992     }
7993
7994   /* If a name was specified, get the string.  */
7995   if (current_binding_level == global_binding_level)
7996     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7997   if (asmspec_tree)
7998     asmspec = TREE_STRING_POINTER (asmspec_tree);
7999
8000   if (init && TREE_CODE (init) == NAMESPACE_DECL)
8001     {
8002       error ("cannot initialize `%D' to namespace `%D'",
8003                 decl, init);
8004       init = NULL_TREE;
8005     }
8006
8007   if (current_class_type
8008       && CP_DECL_CONTEXT (decl) == current_class_type
8009       && TYPE_BEING_DEFINED (current_class_type)
8010       && (DECL_INITIAL (decl) || init))
8011     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8012
8013   if (TREE_CODE (decl) == VAR_DECL
8014       && DECL_CONTEXT (decl)
8015       && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8016       && DECL_CONTEXT (decl) != current_namespace
8017       && init)
8018     {
8019       /* Leave the namespace of the object.  */
8020       pop_decl_namespace ();
8021     }
8022
8023   type = TREE_TYPE (decl);
8024
8025   if (type == error_mark_node)
8026     return;
8027
8028   if (TYPE_HAS_MUTABLE_P (type))
8029     TREE_READONLY (decl) = 0;
8030
8031   if (processing_template_decl)
8032     {
8033       /* Add this declaration to the statement-tree.  */
8034       if (at_function_scope_p ()
8035           && TREE_CODE (decl) != RESULT_DECL)
8036         add_decl_stmt (decl);
8037
8038       if (init && DECL_INITIAL (decl))
8039         DECL_INITIAL (decl) = init;
8040       goto finish_end0;
8041     }
8042
8043   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8044   my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8045
8046   /* Take care of TYPE_DECLs up front.  */
8047   if (TREE_CODE (decl) == TYPE_DECL)
8048     {
8049       if (type != error_mark_node
8050           && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8051         {
8052           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8053             warning ("shadowing previous type declaration of `%#D'", decl);
8054           set_identifier_type_value (DECL_NAME (decl), type);
8055           CLASSTYPE_GOT_SEMICOLON (type) = 1;
8056         }
8057
8058       /* If we have installed this as the canonical typedef for this
8059          type, and that type has not been defined yet, delay emitting
8060          the debug information for it, as we will emit it later.  */
8061       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8062           && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8063         TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8064
8065       rest_of_decl_compilation (decl, NULL,
8066                                 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8067       goto finish_end;
8068     }
8069
8070   if (TREE_CODE (decl) != FUNCTION_DECL)
8071     ttype = target_type (type);
8072
8073   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8074       && TYPE_NEEDS_CONSTRUCTING (type))
8075     {
8076       /* Currently, GNU C++ puts constants in text space, making them
8077          impossible to initialize.  In the future, one would hope for
8078          an operating system which understood the difference between
8079          initialization and the running of a program.  */
8080       was_readonly = 1;
8081       TREE_READONLY (decl) = 0;
8082     }
8083
8084   if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8085     {
8086       /* This must override the asm specifier which was placed by
8087          grokclassfn.  Lay this out fresh.  */
8088       SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8089       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8090       make_decl_rtl (decl, asmspec);
8091     }
8092   else if (TREE_CODE (decl) == RESULT_DECL)
8093     init = check_initializer (decl, init, flags);
8094   else if (TREE_CODE (decl) == VAR_DECL)
8095     {
8096       /* Only PODs can have thread-local storage.  Other types may require
8097          various kinds of non-trivial initialization.  */
8098       if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8099         error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8100                decl, TREE_TYPE (decl));
8101       /* Convert the initializer to the type of DECL, if we have not
8102          already initialized DECL.  */
8103       if (!DECL_INITIALIZED_P (decl)
8104           /* If !DECL_EXTERNAL then DECL is being defined.  In the
8105              case of a static data member initialized inside the
8106              class-specifier, there can be an initializer even if DECL
8107              is *not* defined.  */
8108           && (!DECL_EXTERNAL (decl) || init))
8109         {
8110           init = check_initializer (decl, init, flags);
8111           /* Thread-local storage cannot be dynamically initialized.  */
8112           if (DECL_THREAD_LOCAL (decl) && init)
8113             {
8114               error ("`%D' is thread-local and so cannot be dynamically "
8115                      "initialized", decl);
8116               init = NULL_TREE;
8117             }
8118           /* Handle:
8119              
8120              [dcl.init]
8121              
8122              The memory occupied by any object of static storage
8123              duration is zero-initialized at program startup before
8124              any other initialization takes place.
8125              
8126              We cannot create an appropriate initializer until after
8127              the type of DECL is finalized.  If DECL_INITIAL is set,
8128              then the DECL is statically initialized, and any
8129              necessary zero-initialization has already been performed.  */
8130           if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8131             DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8132                                                    /*nelts=*/NULL_TREE,
8133                                                    /*static_storage_p=*/true);
8134           /* Remember that the initialization for this variable has
8135              taken place.  */
8136           DECL_INITIALIZED_P (decl) = 1;
8137         }
8138       /* If the variable has an array type, lay out the type, even if
8139          there is no initializer.  It is valid to index through the
8140          array, and we must get TYPE_ALIGN set correctly on the array
8141          type.  */
8142       else if (TREE_CODE (type) == ARRAY_TYPE)
8143         layout_type (type);
8144     }
8145
8146   /* Add this declaration to the statement-tree.  This needs to happen
8147      after the call to check_initializer so that the DECL_STMT for a
8148      reference temp is added before the DECL_STMT for the reference itself.  */
8149   if (building_stmt_tree ()
8150       && at_function_scope_p ()
8151       && TREE_CODE (decl) != RESULT_DECL)
8152     add_decl_stmt (decl);
8153
8154   if (TREE_CODE (decl) == VAR_DECL)
8155     layout_var_decl (decl);
8156
8157   /* Output the assembler code and/or RTL code for variables and functions,
8158      unless the type is an undefined structure or union.
8159      If not, it will get done when the type is completed.  */
8160   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8161       || TREE_CODE (decl) == RESULT_DECL)
8162     {
8163       if (TREE_CODE (decl) == VAR_DECL)
8164         maybe_commonize_var (decl);
8165
8166       make_rtl_for_nonlocal_decl (decl, init, asmspec);
8167
8168       if (TREE_CODE (type) == FUNCTION_TYPE
8169           || TREE_CODE (type) == METHOD_TYPE)
8170         abstract_virtuals_error (decl,
8171                                  strip_array_types (TREE_TYPE (type)));
8172       else
8173         abstract_virtuals_error (decl, strip_array_types (type));
8174
8175       if (TREE_CODE (decl) == FUNCTION_DECL 
8176           || TREE_TYPE (decl) == error_mark_node)
8177         /* No initialization required.  */
8178         ;
8179       else if (DECL_EXTERNAL (decl)
8180                && ! (DECL_LANG_SPECIFIC (decl)
8181                      && DECL_NOT_REALLY_EXTERN (decl)))
8182         {
8183           if (init)
8184             DECL_INITIAL (decl) = init;
8185         }
8186       else
8187         {
8188           /* A variable definition.  */
8189           if (DECL_FUNCTION_SCOPE_P (decl))
8190             {
8191               /* This is a local declaration.  */
8192               if (doing_semantic_analysis_p ())
8193                 maybe_inject_for_scope_var (decl);
8194               /* Initialize the local variable.  */
8195               if (processing_template_decl)
8196                 {
8197                   if (init || DECL_INITIAL (decl) == error_mark_node)
8198                     DECL_INITIAL (decl) = init;
8199                 }
8200               else if (!TREE_STATIC (decl))
8201                 initialize_local_var (decl, init);
8202             }
8203
8204           if (TREE_STATIC (decl))
8205             expand_static_init (decl, init);
8206         }
8207     finish_end0:
8208
8209       /* Undo call to `pushclass' that was done in `start_decl'
8210          due to initialization of qualified member variable.
8211          I.e., Foo::x = 10;  */
8212       {
8213         tree context = CP_DECL_CONTEXT (decl);
8214         if (context
8215             && TYPE_P (context)
8216             && (TREE_CODE (decl) == VAR_DECL
8217                 /* We also have a pushclass done that we need to undo here
8218                    if we're at top level and declare a method.  */
8219                 || TREE_CODE (decl) == FUNCTION_DECL)
8220             /* If size hasn't been set, we're still defining it,
8221                and therefore inside the class body; don't pop
8222                the binding level..  */
8223             && COMPLETE_TYPE_P (context)
8224             && context == current_class_type)
8225           pop_nested_class ();
8226       }
8227     }
8228
8229  finish_end:
8230
8231   if (was_readonly)
8232     TREE_READONLY (decl) = 1;
8233 }
8234
8235 /* This is here for a midend callback from c-common.c */
8236
8237 void
8238 finish_decl (tree decl, tree init, tree asmspec_tree)
8239 {
8240   cp_finish_decl (decl, init, asmspec_tree, 0);
8241 }
8242
8243 /* Returns a declaration for a VAR_DECL as if:
8244
8245      extern "C" TYPE NAME;
8246
8247    had been seen.  Used to create compiler-generated global
8248    variables.  */
8249
8250 tree
8251 declare_global_var (tree name, tree type)
8252 {
8253   tree decl;
8254
8255   push_to_top_level ();
8256   decl = build_decl (VAR_DECL, name, type);
8257   TREE_PUBLIC (decl) = 1;
8258   DECL_EXTERNAL (decl) = 1;
8259   DECL_ARTIFICIAL (decl) = 1;
8260   pushdecl (decl);
8261   cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8262   pop_from_top_level ();
8263
8264   return decl;
8265 }
8266
8267 /* Returns a pointer to the `atexit' function.  Note that if
8268    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8269    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
8270
8271 static tree
8272 get_atexit_node (void)
8273 {
8274   tree atexit_fndecl;
8275   tree arg_types;
8276   tree fn_type;
8277   tree fn_ptr_type;
8278   const char *name;
8279
8280   if (atexit_node)
8281     return atexit_node;
8282
8283   if (flag_use_cxa_atexit)
8284     {
8285       /* The declaration for `__cxa_atexit' is:
8286
8287            int __cxa_atexit (void (*)(void *), void *, void *)
8288
8289          We build up the argument types and then then function type
8290          itself.  */
8291
8292       /* First, build the pointer-to-function type for the first
8293          argument.  */
8294       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8295       fn_type = build_function_type (void_type_node, arg_types);
8296       fn_ptr_type = build_pointer_type (fn_type);
8297       /* Then, build the rest of the argument types.  */
8298       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8299       arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8300       arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8301       /* And the final __cxa_atexit type.  */
8302       fn_type = build_function_type (integer_type_node, arg_types);
8303       fn_ptr_type = build_pointer_type (fn_type);
8304       name = "__cxa_atexit";
8305     }
8306   else
8307     {
8308       /* The declaration for `atexit' is:
8309
8310            int atexit (void (*)());
8311
8312          We build up the argument types and then then function type
8313          itself.  */
8314       fn_type = build_function_type (void_type_node, void_list_node);
8315       fn_ptr_type = build_pointer_type (fn_type);
8316       arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8317       /* Build the final atexit type.  */
8318       fn_type = build_function_type (integer_type_node, arg_types);
8319       name = "atexit";
8320     }
8321
8322   /* Now, build the function declaration.  */
8323   push_lang_context (lang_name_c);
8324   atexit_fndecl = build_library_fn_ptr (name, fn_type);
8325   mark_used (atexit_fndecl);
8326   pop_lang_context ();
8327   atexit_node = default_conversion (atexit_fndecl);
8328
8329   return atexit_node;
8330 }
8331
8332 /* Returns the __dso_handle VAR_DECL.  */
8333
8334 static tree
8335 get_dso_handle_node (void)
8336 {
8337   if (dso_handle_node)
8338     return dso_handle_node;
8339
8340   /* Declare the variable.  */
8341   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8342                                         ptr_type_node);
8343
8344   return dso_handle_node;
8345 }
8346
8347 /* Begin a new function with internal linkage whose job will be simply
8348    to destroy some particular variable.  */
8349
8350 static tree
8351 start_cleanup_fn (void)
8352 {
8353   static int counter = 0;
8354   int old_interface_only = interface_only;
8355   int old_interface_unknown = interface_unknown;
8356   char name[32];
8357   tree parmtypes;
8358   tree fntype;
8359   tree fndecl;
8360
8361   push_to_top_level ();
8362
8363   /* No need to mangle this.  */
8364   push_lang_context (lang_name_c);
8365
8366   interface_only = 0;
8367   interface_unknown = 1;
8368
8369   /* Build the parameter-types.  */
8370   parmtypes = void_list_node;
8371   /* Functions passed to __cxa_atexit take an additional parameter.
8372      We'll just ignore it.  After we implement the new calling
8373      convention for destructors, we can eliminate the use of
8374      additional cleanup functions entirely in the -fnew-abi case.  */
8375   if (flag_use_cxa_atexit)
8376     parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8377   /* Build the function type itself.  */
8378   fntype = build_function_type (void_type_node, parmtypes);
8379   /* Build the name of the function.  */
8380   sprintf (name, "__tcf_%d", counter++);
8381   /* Build the function declaration.  */
8382   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8383   /* It's a function with internal linkage, generated by the
8384      compiler.  */
8385   TREE_PUBLIC (fndecl) = 0;
8386   DECL_ARTIFICIAL (fndecl) = 1;
8387   /* Make the function `inline' so that it is only emitted if it is
8388      actually needed.  It is unlikely that it will be inlined, since
8389      it is only called via a function pointer, but we avoid unnecessary
8390      emissions this way.  */
8391   DECL_INLINE (fndecl) = 1;
8392   /* Build the parameter.  */
8393   if (flag_use_cxa_atexit)
8394     {
8395       tree parmdecl;
8396
8397       parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8398       DECL_CONTEXT (parmdecl) = fndecl;
8399       TREE_USED (parmdecl) = 1;
8400       DECL_ARGUMENTS (fndecl) = parmdecl;
8401     }
8402
8403   pushdecl (fndecl);
8404   start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8405
8406   interface_unknown = old_interface_unknown;
8407   interface_only = old_interface_only;
8408
8409   pop_lang_context ();
8410
8411   return current_function_decl;
8412 }
8413
8414 /* Finish the cleanup function begun by start_cleanup_fn.  */
8415
8416 static void
8417 end_cleanup_fn (void)
8418 {
8419   expand_body (finish_function (0));
8420
8421   pop_from_top_level ();
8422 }
8423
8424 /* Generate code to handle the destruction of DECL, an object with
8425    static storage duration.  */
8426
8427 void
8428 register_dtor_fn (tree decl)
8429 {
8430   tree cleanup;
8431   tree compound_stmt;
8432   tree args;
8433   tree fcall;
8434
8435   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8436     return;
8437
8438   /* Call build_cleanup before we enter the anonymous function so that
8439      any access checks will be done relative to the current scope,
8440      rather than the scope of the anonymous function.  */
8441   build_cleanup (decl);
8442
8443   /* Now start the function.  */
8444   cleanup = start_cleanup_fn ();
8445
8446   /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8447      to the original function, rather than the anonymous one.  That
8448      will make the back-end think that nested functions are in use,
8449      which causes confusion.  */
8450   
8451   push_deferring_access_checks (dk_no_check);
8452   fcall = build_cleanup (decl);
8453   pop_deferring_access_checks ();
8454
8455   /* Create the body of the anonymous function.  */
8456   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8457   finish_expr_stmt (fcall);
8458   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8459   end_cleanup_fn ();
8460
8461   /* Call atexit with the cleanup function.  */
8462   cxx_mark_addressable (cleanup);
8463   cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8464   if (flag_use_cxa_atexit)
8465     {
8466       args = tree_cons (NULL_TREE, 
8467                         build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8468                         NULL_TREE);
8469       args = tree_cons (NULL_TREE, null_pointer_node, args);
8470       args = tree_cons (NULL_TREE, cleanup, args);
8471     }
8472   else
8473     args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8474   finish_expr_stmt (build_function_call (get_atexit_node (), args));
8475 }
8476
8477 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8478    is its initializer.  Generate code to handle the construction
8479    and destruction of DECL.  */
8480
8481 static void
8482 expand_static_init (tree decl, tree init)
8483 {
8484   tree oldstatic;
8485
8486   my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8487   my_friendly_assert (TREE_STATIC (decl), 20021010);
8488
8489   /* Some variables require no initialization.  */
8490   if (!init 
8491       && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8492       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8493     return;
8494
8495   oldstatic = value_member (decl, static_aggregates);
8496
8497   if (oldstatic)
8498     {
8499       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8500         error ("multiple initializations given for `%D'", decl);
8501     }
8502   else if (! toplevel_bindings_p ())
8503     {
8504       /* Emit code to perform this initialization but once.  */
8505       tree if_stmt;
8506       tree then_clause;
8507       tree assignment;
8508       tree guard;
8509       tree guard_init;
8510
8511       /* Emit code to perform this initialization but once.  This code
8512          looks like:
8513
8514            static int guard = 0;
8515            if (!guard) {
8516              // Do initialization.
8517              guard = 1;
8518              // Register variable for destruction at end of program.
8519            }
8520
8521          Note that the `temp' variable is only set to 1 *after* the
8522          initialization is complete.  This ensures that an exception,
8523          thrown during the construction, will cause the variable to
8524          reinitialized when we pass through this code again, as per:
8525
8526            [stmt.dcl]
8527
8528            If the initialization exits by throwing an exception, the
8529            initialization is not complete, so it will be tried again
8530            the next time control enters the declaration.
8531
8532          In theory, this process should be thread-safe, too; multiple
8533          threads should not be able to initialize the variable more
8534          than once.  We don't yet attempt to ensure thread-safety.  */
8535
8536       /* Create the guard variable.  */
8537       guard = get_guard (decl);
8538
8539       /* Begin the conditional initialization.  */
8540       if_stmt = begin_if_stmt ();
8541       finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8542       then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8543
8544       /* Do the initialization itself.  */
8545       assignment = init ? init : NULL_TREE;
8546
8547       /* Once the assignment is complete, set TEMP to 1.  Since the
8548          construction of the static object is complete at this point,
8549          we want to make sure TEMP is set to 1 even if a temporary
8550          constructed during the initialization throws an exception
8551          when it is destroyed.  So, we combine the initialization and
8552          the assignment to TEMP into a single expression, ensuring
8553          that when we call finish_expr_stmt the cleanups will not be
8554          run until after TEMP is set to 1.  */
8555       guard_init = set_guard (guard);
8556       if (assignment)
8557         {
8558           assignment = tree_cons (NULL_TREE, assignment,
8559                                   build_tree_list (NULL_TREE,
8560                                                    guard_init));
8561           assignment = build_compound_expr (assignment);
8562         }
8563       else
8564         assignment = guard_init;
8565       finish_expr_stmt (assignment);
8566
8567       /* Use atexit to register a function for destroying this static
8568          variable.  */
8569       register_dtor_fn (decl);
8570
8571       finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8572       finish_then_clause (if_stmt);
8573       finish_if_stmt ();
8574     }
8575   else
8576     static_aggregates = tree_cons (init, decl, static_aggregates);
8577 }
8578
8579 /* Finish the declaration of a catch-parameter.  */
8580
8581 tree
8582 start_handler_parms (tree declspecs, tree declarator)
8583 {
8584   tree decl;
8585   if (declspecs)
8586     {
8587       decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8588                              1, NULL);
8589       if (decl == NULL_TREE)
8590         error ("invalid catch parameter");
8591     }
8592   else
8593     decl = NULL_TREE;
8594
8595   return decl;
8596 }
8597
8598 \f
8599 /* Make TYPE a complete type based on INITIAL_VALUE.
8600    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8601    2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8602
8603 int
8604 complete_array_type (tree type, tree initial_value, int do_default)
8605 {
8606   register tree maxindex = NULL_TREE;
8607   int value = 0;
8608
8609   if (initial_value)
8610     {
8611       /* An array of character type can be initialized from a
8612          brace-enclosed string constant.  */
8613       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8614           && TREE_CODE (initial_value) == CONSTRUCTOR
8615           && CONSTRUCTOR_ELTS (initial_value)
8616           && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8617               == STRING_CST)
8618           && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8619         initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8620
8621       /* Note MAXINDEX is really the maximum index, one less than the
8622          size.  */
8623       if (TREE_CODE (initial_value) == STRING_CST)
8624         {
8625           int eltsize
8626             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8627           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8628                                    / eltsize) - 1, 0);
8629         }
8630       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8631         {
8632           tree elts = CONSTRUCTOR_ELTS (initial_value);
8633
8634           maxindex = ssize_int (-1);
8635           for (; elts; elts = TREE_CHAIN (elts))
8636             {
8637               if (TREE_PURPOSE (elts))
8638                 maxindex = TREE_PURPOSE (elts);
8639               else
8640                 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8641             }
8642           maxindex = copy_node (maxindex);
8643         }
8644       else
8645         {
8646           /* Make an error message unless that happened already.  */
8647           if (initial_value != error_mark_node)
8648             value = 1;
8649           else
8650             initial_value = NULL_TREE;
8651
8652           /* Prevent further error messages.  */
8653           maxindex = build_int_2 (0, 0);
8654         }
8655     }
8656
8657   if (!maxindex)
8658     {
8659       if (do_default)
8660         maxindex = build_int_2 (0, 0);
8661       value = 2;
8662     }
8663
8664   if (maxindex)
8665     {
8666       tree itype;
8667       tree domain;
8668
8669       domain = build_index_type (maxindex);
8670       TYPE_DOMAIN (type) = domain;
8671
8672       if (! TREE_TYPE (maxindex))
8673         TREE_TYPE (maxindex) = domain;
8674       if (initial_value)
8675         itype = TREE_TYPE (initial_value);
8676       else
8677         itype = NULL;
8678       if (itype && !TYPE_DOMAIN (itype))
8679         TYPE_DOMAIN (itype) = domain;
8680       /* The type of the main variant should never be used for arrays
8681          of different sizes.  It should only ever be completed with the
8682          size of the array.  */
8683       if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8684         TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8685     }
8686
8687   /* Lay out the type now that we can get the real answer.  */
8688
8689   layout_type (type);
8690
8691   return value;
8692 }
8693 \f
8694 /* Return zero if something is declared to be a member of type
8695    CTYPE when in the context of CUR_TYPE.  STRING is the error
8696    message to print in that case.  Otherwise, quietly return 1.  */
8697
8698 static int
8699 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8700 {
8701   if (ctype && ctype != cur_type)
8702     {
8703       if (flags == DTOR_FLAG)
8704         error ("destructor for alien class `%T' cannot be a member",
8705                   ctype);
8706       else
8707         error ("constructor for alien class `%T' cannot be a member",
8708                   ctype);
8709       return 0;
8710     }
8711   return 1;
8712 }
8713 \f
8714 /* Subroutine of `grokdeclarator'.  */
8715
8716 /* Generate errors possibly applicable for a given set of specifiers.
8717    This is for ARM $7.1.2.  */
8718
8719 static void
8720 bad_specifiers (tree object,
8721                 const char* type,
8722                 int virtualp,
8723                 int quals,
8724                 int inlinep,
8725                 int friendp,
8726                 int raises)
8727 {
8728   if (virtualp)
8729     error ("`%D' declared as a `virtual' %s", object, type);
8730   if (inlinep)
8731     error ("`%D' declared as an `inline' %s", object, type);
8732   if (quals)
8733     error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8734               object, type);
8735   if (friendp)
8736     cp_error_at ("`%D' declared as a friend", object);
8737   if (raises
8738       && (TREE_CODE (object) == TYPE_DECL
8739           || (!TYPE_PTRFN_P (TREE_TYPE (object))
8740               && !TYPE_REFFN_P (TREE_TYPE (object))
8741               && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8742     cp_error_at ("`%D' declared with an exception specification", object);
8743 }
8744
8745 /* CTYPE is class type, or null if non-class.
8746    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8747    or METHOD_TYPE.
8748    DECLARATOR is the function's name.
8749    VIRTUALP is truthvalue of whether the function is virtual or not.
8750    FLAGS are to be passed through to `grokclassfn'.
8751    QUALS are qualifiers indicating whether the function is `const'
8752    or `volatile'.
8753    RAISES is a list of exceptions that this function can raise.
8754    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8755    not look, and -1 if we should not call `grokclassfn' at all.
8756
8757    Returns `NULL_TREE' if something goes wrong, after issuing
8758    applicable error messages.  */
8759
8760 static tree
8761 grokfndecl (tree ctype, 
8762             tree type,
8763             tree declarator,
8764             tree orig_declarator,
8765             int virtualp,
8766             enum overload_flags flags,
8767             tree quals, 
8768             tree raises,
8769             int check, 
8770             int friendp, 
8771             int publicp, 
8772             int inlinep, 
8773             int funcdef_flag, 
8774             int template_count,
8775             tree in_namespace)
8776 {
8777   tree decl;
8778   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8779   int has_default_arg = 0;
8780   tree t;
8781
8782   if (raises)
8783     type = build_exception_variant (type, raises);
8784
8785   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8786   /* Propagate volatile out from type to decl.  */
8787   if (TYPE_VOLATILE (type))
8788     TREE_THIS_VOLATILE (decl) = 1;
8789
8790   /* If this decl has namespace scope, set that up.  */
8791   if (in_namespace)
8792     set_decl_namespace (decl, in_namespace, friendp);
8793   else if (!ctype)
8794     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8795
8796   /* `main' and builtins have implicit 'C' linkage.  */
8797   if ((MAIN_NAME_P (declarator)
8798        || (IDENTIFIER_LENGTH (declarator) > 10
8799            && IDENTIFIER_POINTER (declarator)[0] == '_'
8800            && IDENTIFIER_POINTER (declarator)[1] == '_'
8801            && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8802       && current_lang_name == lang_name_cplusplus
8803       && ctype == NULL_TREE
8804       /* NULL_TREE means global namespace.  */
8805       && DECL_CONTEXT (decl) == NULL_TREE)
8806     SET_DECL_LANGUAGE (decl, lang_c);
8807
8808   /* Should probably propagate const out from type to decl I bet (mrs).  */
8809   if (staticp)
8810     {
8811       DECL_STATIC_FUNCTION_P (decl) = 1;
8812       DECL_CONTEXT (decl) = ctype;
8813     }
8814
8815   if (ctype)
8816     DECL_CONTEXT (decl) = ctype;
8817
8818   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8819     {
8820       if (processing_template_decl)
8821         error ("cannot declare `::main' to be a template");
8822       if (inlinep)
8823         error ("cannot declare `::main' to be inline");
8824       if (!publicp)
8825         error ("cannot declare `::main' to be static");
8826       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8827                         integer_type_node))
8828         error ("`main' must return `int'");
8829       inlinep = 0;
8830       publicp = 1;
8831     }
8832
8833   /* Members of anonymous types and local classes have no linkage; make
8834      them internal.  */
8835   /* FIXME what if it gets a name from typedef?  */
8836   if (ctype && (TYPE_ANONYMOUS_P (ctype)
8837                 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8838     publicp = 0;
8839
8840   if (publicp)
8841     {
8842       /* [basic.link]: A name with no linkage (notably, the name of a class
8843          or enumeration declared in a local scope) shall not be used to
8844          declare an entity with linkage.
8845
8846          Only check this for public decls for now.  See core 319, 389.  */
8847       t = no_linkage_check (TREE_TYPE (decl));
8848       if (t)
8849         {
8850           if (TYPE_ANONYMOUS_P (t))
8851             {
8852               if (DECL_EXTERN_C_P (decl))
8853                 /* Allow this; it's pretty common in C.  */;
8854               else
8855                 {
8856                   pedwarn ("non-local function `%#D' uses anonymous type",
8857                               decl);
8858                   if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8859                     cp_pedwarn_at ("\
8860 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8861                                 TYPE_NAME (t));
8862                 }
8863             }
8864           else
8865             pedwarn ("non-local function `%#D' uses local type `%T'",
8866                         decl, t);
8867         }
8868     }
8869
8870   TREE_PUBLIC (decl) = publicp;
8871   if (! publicp)
8872     {
8873       DECL_INTERFACE_KNOWN (decl) = 1;
8874       DECL_NOT_REALLY_EXTERN (decl) = 1;
8875     }
8876
8877   DID_INLINE_FUNC (decl) = 0;
8878   /* If the declaration was declared inline, mark it as such.  */
8879   if (inlinep)
8880     DECL_DECLARED_INLINE_P (decl) = 1;
8881   /* We inline functions that are explicitly declared inline, or, when
8882      the user explicitly asks us to, all functions.  */
8883   if (DECL_DECLARED_INLINE_P (decl))
8884     DECL_INLINE (decl) = 1;
8885   if (flag_inline_trees == 2 && !DECL_INLINE (decl))
8886     {
8887       DID_INLINE_FUNC (decl) = 1;
8888       DECL_INLINE (decl) = 1;
8889     }
8890
8891   DECL_EXTERNAL (decl) = 1;
8892   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8893     {
8894       error ("%smember function `%D' cannot have `%T' method qualifier",
8895                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8896       quals = NULL_TREE;
8897     }
8898
8899   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8900     grok_op_properties (decl, friendp);
8901
8902   if (ctype && decl_function_context (decl))
8903     DECL_NO_STATIC_CHAIN (decl) = 1;
8904
8905   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8906     if (TREE_PURPOSE (t)
8907         && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8908       {
8909         has_default_arg = 1;
8910         break;
8911       }
8912
8913   if (friendp
8914       && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8915     {
8916       if (funcdef_flag)
8917         error
8918           ("defining explicit specialization `%D' in friend declaration",
8919            orig_declarator);
8920       else
8921         {
8922           tree fns = TREE_OPERAND (orig_declarator, 0);
8923           tree args = TREE_OPERAND (orig_declarator, 1);
8924
8925           if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8926             {
8927               /* Something like `template <class T> friend void f<T>()'.  */
8928               error ("invalid use of template-id `%D' in declaration of primary template",
8929                         orig_declarator);
8930               return NULL_TREE;
8931             }
8932
8933
8934           /* A friend declaration of the form friend void f<>().  Record
8935              the information in the TEMPLATE_ID_EXPR.  */
8936           SET_DECL_IMPLICIT_INSTANTIATION (decl);
8937
8938           if (TREE_CODE (fns) == COMPONENT_REF)
8939             {
8940               /* Due to bison parser ickiness, we will have already looked
8941                  up an operator_name or PFUNCNAME within the current class
8942                  (see template_id in parse.y). If the current class contains
8943                  such a name, we'll get a COMPONENT_REF here. Undo that.  */
8944
8945               my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8946                                   == current_class_type, 20001120);
8947               fns = TREE_OPERAND (fns, 1);
8948             }
8949           my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8950                               || TREE_CODE (fns) == LOOKUP_EXPR
8951                               || TREE_CODE (fns) == OVERLOAD, 20001120);
8952           DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8953
8954           if (has_default_arg)
8955             {
8956               error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8957                         decl);
8958               return NULL_TREE;
8959             }
8960
8961           if (inlinep)
8962             {
8963               error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8964                         decl);
8965               return NULL_TREE;
8966             }
8967         }
8968     }
8969
8970   if (funcdef_flag)
8971     /* Make the init_value nonzero so pushdecl knows this is not
8972        tentative.  error_mark_node is replaced later with the BLOCK.  */
8973     DECL_INITIAL (decl) = error_mark_node;
8974
8975   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8976     TREE_NOTHROW (decl) = 1;
8977
8978   /* Caller will do the rest of this.  */
8979   if (check < 0)
8980     return decl;
8981
8982   if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
8983     DECL_CONSTRUCTOR_P (decl) = 1;
8984
8985   /* Function gets the ugly name, field gets the nice one.  This call
8986      may change the type of the function (because of default
8987      parameters)!  */
8988   if (ctype != NULL_TREE)
8989     grokclassfn (ctype, decl, flags, quals);
8990
8991   decl = check_explicit_specialization (orig_declarator, decl,
8992                                         template_count,
8993                                         2 * (funcdef_flag != 0) +
8994                                         4 * (friendp != 0));
8995   if (decl == error_mark_node)
8996     return NULL_TREE;
8997
8998   if (ctype != NULL_TREE
8999       && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9000       && check)
9001     {
9002       tree old_decl;
9003
9004       old_decl = check_classfn (ctype, decl);
9005
9006       if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9007         /* Because grokfndecl is always supposed to return a
9008            FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9009            here.  We depend on our callers to figure out that its
9010            really a template that's being returned.  */
9011         old_decl = DECL_TEMPLATE_RESULT (old_decl);
9012
9013       if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9014           && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9015         {
9016           /* Remove the `this' parm added by grokclassfn.
9017              XXX Isn't this done in start_function, too?  */
9018           revert_static_member_fn (decl);
9019           last_function_parms = TREE_CHAIN (last_function_parms);
9020         }
9021       if (old_decl && DECL_ARTIFICIAL (old_decl))
9022         error ("definition of implicitly-declared `%D'", old_decl);
9023
9024       if (old_decl)
9025         {
9026           bool ok;
9027
9028           /* Since we've smashed OLD_DECL to its
9029              DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9030           if (TREE_CODE (decl) == TEMPLATE_DECL)
9031             decl = DECL_TEMPLATE_RESULT (decl);
9032
9033           /* Attempt to merge the declarations.  This can fail, in
9034              the case of some invalid specialization declarations.  */
9035           push_scope (ctype);
9036           ok = duplicate_decls (decl, old_decl);
9037           pop_scope (ctype);
9038           if (!ok)
9039             {
9040               error ("no `%#D' member function declared in class `%T'",
9041                      decl, ctype);
9042               return NULL_TREE;
9043             }
9044           return old_decl;
9045         }
9046     }
9047
9048   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9049     return NULL_TREE;
9050
9051   if (ctype == NULL_TREE || check)
9052     return decl;
9053
9054   if (virtualp)
9055     DECL_VIRTUAL_P (decl) = 1;
9056
9057   return decl;
9058 }
9059
9060 /* Create a VAR_DECL named NAME with the indicated TYPE.  
9061
9062    If SCOPE is non-NULL, it is the class type or namespace containing
9063    the variable.  If SCOPE is NULL, the variable should is created in
9064    the innermost enclosings scope.  */
9065
9066 static tree
9067 grokvardecl (tree type,
9068              tree name,
9069              RID_BIT_TYPE * specbits_in,
9070              int initialized,
9071              int constp,
9072              tree scope)
9073 {
9074   tree decl;
9075   RID_BIT_TYPE specbits;
9076
9077   my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, 
9078                       20020808);
9079
9080   specbits = *specbits_in;
9081
9082   /* Compute the scope in which to place the variable.  */
9083   if (!scope)
9084     {
9085       /* An explicit "extern" specifier indicates a namespace-scope
9086          variable.  */
9087       if (RIDBIT_SETP (RID_EXTERN, specbits))
9088         scope = current_namespace;
9089       else if (!at_function_scope_p ())
9090         {
9091           scope = current_scope ();
9092           if (!scope)
9093             scope = current_namespace;
9094         }
9095     }
9096
9097   if (scope
9098       && (/* If the variable is a namespace-scope variable declared in a
9099              template, we need DECL_LANG_SPECIFIC.  */
9100           (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9101           /* Similarly for namespace-scope variables with language linkage
9102              other than C++.  */
9103           || (TREE_CODE (scope) == NAMESPACE_DECL 
9104               && current_lang_name != lang_name_cplusplus)
9105           /* Similarly for static data members.  */
9106           || TYPE_P (scope)))
9107     decl = build_lang_decl (VAR_DECL, name, type);
9108   else
9109     decl = build_decl (VAR_DECL, name, type);
9110
9111   if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9112     set_decl_namespace (decl, scope, 0);
9113   else
9114     DECL_CONTEXT (decl) = scope;
9115
9116   if (name && scope && current_lang_name != lang_name_c)
9117     /* We can't mangle lazily here because we don't have any
9118        way to recover whether or not a variable was `extern
9119        "C"' later.  */
9120     mangle_decl (decl);
9121
9122   if (RIDBIT_SETP (RID_EXTERN, specbits))
9123     {
9124       DECL_THIS_EXTERN (decl) = 1;
9125       DECL_EXTERNAL (decl) = !initialized;
9126     }
9127
9128   /* In class context, static means one per class,
9129      public access, and static storage.  */
9130   if (DECL_CLASS_SCOPE_P (decl))
9131     {
9132       TREE_PUBLIC (decl) = 1;
9133       TREE_STATIC (decl) = 1;
9134       DECL_EXTERNAL (decl) = 0;
9135     }
9136   /* At top level, either `static' or no s.c. makes a definition
9137      (perhaps tentative), and absence of `static' makes it public.  */
9138   else if (toplevel_bindings_p ())
9139     {
9140       TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9141                             && (DECL_THIS_EXTERN (decl) || ! constp));
9142       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9143     }
9144   /* Not at top level, only `static' makes a static definition.  */
9145   else
9146     {
9147       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9148       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9149     }
9150
9151   if (RIDBIT_SETP (RID_THREAD, specbits))
9152     {
9153       if (targetm.have_tls)
9154         DECL_THREAD_LOCAL (decl) = 1;
9155       else
9156         /* A mere warning is sure to result in improper semantics
9157            at runtime.  Don't bother to allow this to compile.  */
9158         error ("thread-local storage not supported for this target");
9159     }
9160
9161   if (TREE_PUBLIC (decl))
9162     {
9163       /* [basic.link]: A name with no linkage (notably, the name of a class
9164          or enumeration declared in a local scope) shall not be used to
9165          declare an entity with linkage.
9166
9167          Only check this for public decls for now.  */
9168       tree t = no_linkage_check (TREE_TYPE (decl));
9169       if (t)
9170         {
9171           if (TYPE_ANONYMOUS_P (t))
9172             /* Ignore for now; `enum { foo } e' is pretty common.  */;
9173           else
9174             pedwarn ("non-local variable `%#D' uses local type `%T'",
9175                         decl, t);
9176         }
9177     }
9178
9179   return decl;
9180 }
9181
9182 /* Create and return a canonical pointer to member function type, for
9183    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9184
9185 tree
9186 build_ptrmemfunc_type (tree type)
9187 {
9188   tree field, fields;
9189   tree t;
9190   tree unqualified_variant = NULL_TREE;
9191
9192   if (type == error_mark_node)
9193     return type;
9194
9195   /* If a canonical type already exists for this type, use it.  We use
9196      this method instead of type_hash_canon, because it only does a
9197      simple equality check on the list of field members.  */
9198
9199   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9200     return t;
9201
9202   /* Make sure that we always have the unqualified pointer-to-member
9203      type first.  */
9204   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9205     unqualified_variant
9206       = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9207
9208   t = make_aggr_type (RECORD_TYPE);
9209   /* Let the front-end know this is a pointer to member function...  */
9210   TYPE_PTRMEMFUNC_FLAG (t) = 1;
9211   /* ... and not really an aggregate.  */
9212   SET_IS_AGGR_TYPE (t, 0);
9213
9214   field = build_decl (FIELD_DECL, pfn_identifier, type);
9215   fields = field;
9216   
9217   field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9218   TREE_CHAIN (field) = fields;
9219   fields = field;
9220   
9221   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9222
9223   /* Zap out the name so that the back-end will give us the debugging
9224      information for this anonymous RECORD_TYPE.  */
9225   TYPE_NAME (t) = NULL_TREE;
9226
9227   /* If this is not the unqualified form of this pointer-to-member
9228      type, set the TYPE_MAIN_VARIANT for this type to be the
9229      unqualified type.  Since they are actually RECORD_TYPEs that are
9230      not variants of each other, we must do this manually.  */
9231   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9232     {
9233       t = build_qualified_type (t, cp_type_quals (type));
9234       TYPE_MAIN_VARIANT (t) = unqualified_variant;
9235       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9236       TYPE_NEXT_VARIANT (unqualified_variant) = t;
9237     }
9238
9239   /* Cache this pointer-to-member type so that we can find it again
9240      later.  */
9241   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9242
9243   /* Seems to be wanted.  */
9244   CLASSTYPE_GOT_SEMICOLON (t) = 1;
9245
9246   return t;
9247 }
9248
9249 /* Create and return a pointer to data member type.  */
9250
9251 tree
9252 build_ptrmem_type (tree class_type, tree member_type)
9253 {
9254   return build_pointer_type (build_offset_type (class_type, member_type));
9255 }
9256
9257 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9258    Check to see that the definition is valid.  Issue appropriate error
9259    messages.  Return 1 if the definition is particularly bad, or 0
9260    otherwise.  */
9261
9262 int
9263 check_static_variable_definition (tree decl, tree type)
9264 {
9265   /* Motion 10 at San Diego: If a static const integral data member is
9266      initialized with an integral constant expression, the initializer
9267      may appear either in the declaration (within the class), or in
9268      the definition, but not both.  If it appears in the class, the
9269      member is a member constant.  The file-scope definition is always
9270      required.  */
9271   if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9272     {
9273       error ("invalid in-class initialization of static data member of non-integral type `%T'",
9274              type);
9275       /* If we just return the declaration, crashes will sometimes
9276          occur.  We therefore return void_type_node, as if this was a
9277          friend declaration, to cause callers to completely ignore
9278          this declaration.  */
9279       return 1;
9280     }
9281   else if (!CP_TYPE_CONST_P (type))
9282     error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9283               decl);
9284   else if (pedantic && !INTEGRAL_TYPE_P (type))
9285     pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9286
9287   return 0;
9288 }
9289
9290 /* Given the SIZE (i.e., number of elements) in an array, compute an
9291    appropriate index type for the array.  If non-NULL, NAME is the
9292    name of the thing being declared.  */
9293
9294 tree
9295 compute_array_index_type (tree name, tree size)
9296 {
9297   tree itype;
9298
9299   /* If this involves a template parameter, it will be a constant at
9300      instantiation time, but we don't know what the value is yet.
9301      Even if no template parameters are involved, we may an expression
9302      that is not a constant; we don't even simplify `1 + 2' when
9303      processing a template.  */
9304   if (processing_template_decl)
9305     {
9306       /* Resolve a qualified reference to an enumerator or static
9307          const data member of ours.  */
9308       if (TREE_CODE (size) == SCOPE_REF
9309           && TREE_OPERAND (size, 0) == current_class_type)
9310         {
9311           tree t = lookup_field (current_class_type,
9312                                  TREE_OPERAND (size, 1), 0, false);
9313           if (t)
9314             size = t;
9315         }
9316
9317       return build_index_type (build_min (MINUS_EXPR, sizetype,
9318                                           size, integer_one_node));
9319     }
9320
9321   /* The size might be the result of a cast.  */
9322   STRIP_TYPE_NOPS (size);
9323
9324   /* It might be a const variable or enumeration constant.  */
9325   size = decl_constant_value (size);
9326
9327   /* The array bound must be an integer type.  */
9328   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9329       && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9330       && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9331     {
9332       if (name)
9333         error ("size of array `%D' has non-integer type", name);
9334       else
9335         error ("size of array has non-integer type");
9336       size = integer_one_node;
9337     }
9338
9339   /* Normally, the array-bound will be a constant.  */
9340   if (TREE_CODE (size) == INTEGER_CST)
9341     {
9342       /* Check to see if the array bound overflowed.  Make that an
9343          error, no matter how generous we're being.  */
9344       int old_flag_pedantic_errors = flag_pedantic_errors;
9345       int old_pedantic = pedantic;
9346       pedantic = flag_pedantic_errors = 1;
9347       constant_expression_warning (size);
9348       pedantic = old_pedantic;
9349       flag_pedantic_errors = old_flag_pedantic_errors;
9350
9351       /* An array must have a positive number of elements.  */
9352       if (INT_CST_LT (size, integer_zero_node))
9353         {
9354           if (name)
9355             error ("size of array `%D' is negative", name);
9356           else
9357             error ("size of array is negative");
9358           size = integer_one_node;
9359         }
9360       /* Except that an extension we allow zero-sized arrays.  We
9361          always allow them in system headers because glibc uses
9362          them.  */
9363       else if (integer_zerop (size) && pedantic && !in_system_header)
9364         {
9365           if (name)
9366             pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9367           else
9368             pedwarn ("ISO C++ forbids zero-size array");
9369         }
9370     }
9371   else if (TREE_CONSTANT (size))
9372     {
9373       /* `(int) &fn' is not a valid array bound.  */
9374       if (name)
9375         error ("size of array `%D' is not an integral constant-expression",
9376                   name);
9377       else
9378         error ("size of array is not an integral constant-expression");
9379     }
9380
9381   /* Compute the index of the largest element in the array.  It is
9382      one less than the number of elements in the array.  */
9383   itype
9384     = fold (cp_build_binary_op (MINUS_EXPR,
9385                                 cp_convert (ssizetype, size),
9386                                 cp_convert (ssizetype,
9387                                             integer_one_node)));
9388
9389   /* Check for variable-sized arrays.  We allow such things as an
9390      extension, even though they are not allowed in ANSI/ISO C++.  */
9391   if (!TREE_CONSTANT (itype))
9392     {
9393       if (pedantic)
9394         {
9395           if (name)
9396             pedwarn ("ISO C++ forbids variable-size array `%D'",
9397                         name);
9398           else
9399             pedwarn ("ISO C++ forbids variable-size array");
9400         }
9401
9402       /* Create a variable-sized array index type.  */
9403       itype = variable_size (itype);
9404     }
9405   /* Make sure that there was no overflow when creating to a signed
9406      index type.  (For example, on a 32-bit machine, an array with
9407      size 2^32 - 1 is too big.)  */
9408   else if (TREE_OVERFLOW (itype))
9409     {
9410       error ("overflow in array dimension");
9411       TREE_OVERFLOW (itype) = 0;
9412     }
9413
9414   /* Create and return the appropriate index type.  */
9415   return build_index_type (itype);
9416 }
9417
9418 /* Returns the scope (if any) in which the entity declared by
9419    DECLARATOR will be located.  If the entity was declared with an
9420    unqualified name, NULL_TREE is returned.  */
9421
9422 tree
9423 get_scope_of_declarator (tree declarator)
9424 {
9425   if (!declarator)
9426     return NULL_TREE;
9427   
9428   switch (TREE_CODE (declarator))
9429     {
9430     case CALL_EXPR:
9431     case ARRAY_REF:
9432     case INDIRECT_REF:
9433     case ADDR_EXPR:
9434       /* For any of these, the main declarator is the first operand.  */
9435       return get_scope_of_declarator (TREE_OPERAND
9436                                       (declarator, 0));
9437
9438     case SCOPE_REF:
9439       /* For a pointer-to-member, continue descending.  */
9440       if (TREE_CODE (TREE_OPERAND (declarator, 1))
9441           == INDIRECT_REF)
9442         return get_scope_of_declarator (TREE_OPERAND
9443                                         (declarator, 1));
9444       /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9445          which the declaration occurs is the first operand.  */
9446       return TREE_OPERAND (declarator, 0);
9447
9448     case TREE_LIST:
9449       /* Attributes to be applied. The declarator is TREE_VALUE.  */
9450       return get_scope_of_declarator (TREE_VALUE (declarator));
9451       
9452     default:
9453       /* Otherwise, we have a declarator-id which is not a qualified
9454          name; the entity will be declared in the current scope.  */
9455       return NULL_TREE;
9456     }
9457 }
9458
9459 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9460    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
9461    with this type.  */
9462
9463 static tree
9464 create_array_type_for_decl (tree name, tree type, tree size)
9465 {
9466   tree itype = NULL_TREE;
9467   const char* error_msg;
9468
9469   /* If things have already gone awry, bail now.  */
9470   if (type == error_mark_node || size == error_mark_node)
9471     return error_mark_node;
9472
9473   /* Assume that everything will go OK.  */
9474   error_msg = NULL;
9475
9476   /* There are some types which cannot be array elements.  */
9477   switch (TREE_CODE (type))
9478     {
9479     case VOID_TYPE:
9480       error_msg = "array of void";
9481       break;
9482
9483     case FUNCTION_TYPE:
9484       error_msg = "array of functions";
9485       break;
9486
9487     case REFERENCE_TYPE:
9488       error_msg = "array of references";
9489       break;
9490
9491     case OFFSET_TYPE:
9492       error_msg = "array of data members";
9493       break;
9494
9495     case METHOD_TYPE:
9496       error_msg = "array of function members";
9497       break;
9498
9499     default:
9500       break;
9501     }
9502
9503   /* If something went wrong, issue an error-message and return.  */
9504   if (error_msg)
9505     {
9506       if (name)
9507         error ("declaration of `%D' as %s", name, error_msg);
9508       else
9509         error ("creating %s", error_msg);
9510
9511       return error_mark_node;
9512     }
9513
9514   /* [dcl.array]
9515
9516      The constant expressions that specify the bounds of the arrays
9517      can be omitted only for the first member of the sequence.  */
9518   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9519     {
9520       if (name)
9521         error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9522                   name);
9523       else
9524         error ("multidimensional array must have bounds for all dimensions except the first");
9525
9526       return error_mark_node;
9527     }
9528
9529   /* Figure out the index type for the array.  */
9530   if (size)
9531     itype = compute_array_index_type (name, size);
9532
9533   return build_cplus_array_type (type, itype);
9534 }
9535
9536 /* Check that it's OK to declare a function with the indicated TYPE.
9537    SFK indicates the kind of special function (if any) that this
9538    function is.  OPTYPE is the type given in a conversion operator
9539    declaration.  Returns the actual return type of the function; that
9540    may be different than TYPE if an error occurs, or for certain
9541    special functions.  */
9542
9543 static tree
9544 check_special_function_return_type (special_function_kind sfk,
9545                                     tree type,
9546                                     tree optype)
9547 {
9548   switch (sfk)
9549     {
9550     case sfk_constructor:
9551       if (type)
9552         error ("return type specification for constructor invalid");
9553
9554       type = void_type_node;
9555       break;
9556
9557     case sfk_destructor:
9558       if (type)
9559         error ("return type specification for destructor invalid");
9560       type = void_type_node;
9561       break;
9562
9563     case sfk_conversion:
9564       if (type && !same_type_p (type, optype))
9565         error ("operator `%T' declared to return `%T'", optype, type);
9566       else if (type)
9567         pedwarn ("return type specified for `operator %T'",  optype);
9568       type = optype;
9569       break;
9570
9571     default:
9572       abort ();
9573       break;
9574     }
9575
9576   return type;
9577 }
9578
9579 /* Given declspecs and a declarator (abstract or otherwise), determine
9580    the name and type of the object declared and construct a DECL node
9581    for it.
9582
9583    DECLSPECS is a chain of tree_list nodes whose value fields
9584     are the storage classes and type specifiers.
9585
9586    DECL_CONTEXT says which syntactic context this declaration is in:
9587      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9588      FUNCDEF for a function definition.  Like NORMAL but a few different
9589       error messages in each case.  Return value may be zero meaning
9590       this definition is too screwy to try to parse.
9591      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
9592       handle member functions (which have FIELD context).
9593       Return value may be zero meaning this definition is too screwy to
9594       try to parse.
9595      PARM for a parameter declaration (either within a function prototype
9596       or before a function body).  Make a PARM_DECL, or return void_type_node.
9597      CATCHPARM for a parameter declaration before a catch clause.
9598      TYPENAME if for a typename (in a cast or sizeof).
9599       Don't make a DECL node; just return the ..._TYPE node.
9600      FIELD for a struct or union field; make a FIELD_DECL.
9601      BITFIELD for a field with specified width.
9602    INITIALIZED is 1 if the decl has an initializer.
9603
9604    ATTRLIST is a pointer to the list of attributes, which may be NULL
9605    if there are none; *ATTRLIST may be modified if attributes from inside
9606    the declarator should be applied to the declaration.
9607
9608    When this function is called, scoping variables (such as
9609    CURRENT_CLASS_TYPE) should reflect the scope in which the
9610    declaration occurs, not the scope in which the new declaration will
9611    be placed.  For example, on:
9612
9613      void S::f() { ... }
9614
9615    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9616    should not be `S'.  */
9617
9618 tree
9619 grokdeclarator (tree declarator,
9620                 tree declspecs,
9621                 enum decl_context decl_context,
9622                 int initialized,
9623                 tree* attrlist)
9624 {
9625   RID_BIT_TYPE specbits;
9626   int nclasses = 0;
9627   tree spec;
9628   tree type = NULL_TREE;
9629   int longlong = 0;
9630   int type_quals;
9631   int virtualp, explicitp, friendp, inlinep, staticp;
9632   int explicit_int = 0;
9633   int explicit_char = 0;
9634   int defaulted_int = 0;
9635   int extern_langp = 0;
9636   tree dependant_name = NULL_TREE;
9637   
9638   tree typedef_decl = NULL_TREE;
9639   const char *name;
9640   tree typedef_type = NULL_TREE;
9641   int funcdef_flag = 0;
9642   enum tree_code innermost_code = ERROR_MARK;
9643   int bitfield = 0;
9644 #if 0
9645   /* See the code below that used this.  */
9646   tree decl_attr = NULL_TREE;
9647 #endif
9648
9649   /* Keep track of what sort of function is being processed
9650      so that we can warn about default return values, or explicit
9651      return values which do not match prescribed defaults.  */
9652   special_function_kind sfk = sfk_none;
9653
9654   tree dname = NULL_TREE;
9655   tree ctype = current_class_type;
9656   tree ctor_return_type = NULL_TREE;
9657   enum overload_flags flags = NO_SPECIAL;
9658   tree quals = NULL_TREE;
9659   tree raises = NULL_TREE;
9660   int template_count = 0;
9661   tree in_namespace = NULL_TREE;
9662   tree returned_attrs = NULL_TREE;
9663   tree scope = NULL_TREE;
9664
9665   RIDBIT_RESET_ALL (specbits);
9666   if (decl_context == FUNCDEF)
9667     funcdef_flag = 1, decl_context = NORMAL;
9668   else if (decl_context == MEMFUNCDEF)
9669     funcdef_flag = -1, decl_context = FIELD;
9670   else if (decl_context == BITFIELD)
9671     bitfield = 1, decl_context = FIELD;
9672
9673   /* Look inside a declarator for the name being declared
9674      and get it as a string, for an error message.  */
9675   {
9676     tree *next = &declarator;
9677     register tree decl;
9678     name = NULL;
9679
9680     while (next && *next)
9681       {
9682         decl = *next;
9683         switch (TREE_CODE (decl))
9684           {
9685           case TREE_LIST:
9686             /* For attributes.  */
9687             next = &TREE_VALUE (decl);
9688             break;
9689
9690           case COND_EXPR:
9691             ctype = NULL_TREE;
9692             next = &TREE_OPERAND (decl, 0);
9693             break;
9694
9695           case BIT_NOT_EXPR:    /* For C++ destructors!  */
9696             {
9697               tree name = TREE_OPERAND (decl, 0);
9698               tree rename = NULL_TREE;
9699
9700               my_friendly_assert (flags == NO_SPECIAL, 152);
9701               flags = DTOR_FLAG;
9702               sfk = sfk_destructor;
9703               if (TYPE_P (name))
9704                 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9705               my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9706               if (ctype == NULL_TREE)
9707                 {
9708                   if (current_class_type == NULL_TREE)
9709                     {
9710                       error ("destructors must be member functions");
9711                       flags = NO_SPECIAL;
9712                     }
9713                   else
9714                     {
9715                       tree t = constructor_name (current_class_type);
9716                       if (t != name)
9717                         rename = t;
9718                     }
9719                 }
9720               else
9721                 {
9722                   tree t = constructor_name (ctype);
9723                   if (t != name)
9724                     rename = t;
9725                 }
9726
9727               if (rename)
9728                 {
9729                   error ("destructor `%T' must match class name `%T'",
9730                             name, rename);
9731                   TREE_OPERAND (decl, 0) = rename;
9732                 }
9733               next = &name;
9734             }
9735             break;
9736
9737           case ADDR_EXPR:       /* C++ reference declaration */
9738             /* Fall through.  */
9739           case ARRAY_REF:
9740           case INDIRECT_REF:
9741             ctype = NULL_TREE;
9742             innermost_code = TREE_CODE (decl);
9743             next = &TREE_OPERAND (decl, 0);
9744             break;
9745
9746           case CALL_EXPR:
9747             innermost_code = TREE_CODE (decl);
9748             if (decl_context == FIELD && ctype == NULL_TREE)
9749               ctype = current_class_type;
9750             if (ctype
9751                 && TREE_OPERAND (decl, 0)
9752                 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9753                     && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9754                                            ctype)))
9755               TREE_OPERAND (decl, 0) = constructor_name (ctype);
9756             next = &TREE_OPERAND (decl, 0);
9757             decl = *next;
9758             if (ctype != NULL_TREE
9759                 && decl != NULL_TREE && flags != DTOR_FLAG
9760                 && constructor_name_p (decl, ctype))
9761               {
9762                 sfk = sfk_constructor;
9763                 ctor_return_type = ctype;
9764               }
9765             ctype = NULL_TREE;
9766             break;
9767
9768           case TEMPLATE_ID_EXPR:
9769               {
9770                 tree fns = TREE_OPERAND (decl, 0);
9771
9772                 if (TREE_CODE (fns) == LOOKUP_EXPR)
9773                   fns = TREE_OPERAND (fns, 0);
9774
9775                 dname = fns;
9776                 if (TREE_CODE (dname) == COMPONENT_REF)
9777                   dname = TREE_OPERAND (dname, 1);
9778                 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9779                   {
9780                     my_friendly_assert (is_overloaded_fn (dname),
9781                                         19990331);
9782                     dname = DECL_NAME (get_first_fn (dname));
9783                   }
9784               }
9785           /* Fall through.  */
9786
9787           case IDENTIFIER_NODE:
9788             if (TREE_CODE (decl) == IDENTIFIER_NODE)
9789               dname = decl;
9790
9791             next = 0;
9792
9793             if (C_IS_RESERVED_WORD (dname))
9794               {
9795                 error ("declarator-id missing; using reserved word `%D'",
9796                           dname);
9797                 name = IDENTIFIER_POINTER (dname);
9798               }
9799             else if (!IDENTIFIER_TYPENAME_P (dname))
9800               name = IDENTIFIER_POINTER (dname);
9801             else
9802               {
9803                 my_friendly_assert (flags == NO_SPECIAL, 154);
9804                 flags = TYPENAME_FLAG;
9805                 ctor_return_type = TREE_TYPE (dname);
9806                 sfk = sfk_conversion;
9807                 if (IDENTIFIER_GLOBAL_VALUE (dname)
9808                     && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9809                         == TYPE_DECL))
9810                   name = IDENTIFIER_POINTER (dname);
9811                 else
9812                   name = "<invalid operator>";
9813               }
9814             break;
9815
9816             /* C++ extension */
9817           case SCOPE_REF:
9818             {
9819               /* Perform error checking, and decide on a ctype.  */
9820               tree cname = TREE_OPERAND (decl, 0);
9821               if (cname == NULL_TREE)
9822                 ctype = NULL_TREE;
9823               else if (TREE_CODE (cname) == NAMESPACE_DECL)
9824                 {
9825                   ctype = NULL_TREE;
9826                   in_namespace = TREE_OPERAND (decl, 0);
9827                 }
9828               else if (! is_aggr_type (cname, 1))
9829                 ctype = NULL_TREE;
9830               /* Must test TREE_OPERAND (decl, 1), in case user gives
9831                  us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9832               else if (TREE_OPERAND (decl, 1)
9833                        && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9834                 ctype = cname;
9835               else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9836                        || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9837                 {
9838                   /* This might be declaring a member of a template
9839                      parm to be a friend.  */
9840                   ctype = cname;
9841                   dependant_name = TREE_OPERAND (decl, 1);
9842                 }
9843               else if (ctype == NULL_TREE)
9844                 ctype = cname;
9845               else if (TREE_COMPLEXITY (decl) == current_class_depth)
9846                 ;
9847               else
9848                 {
9849                   if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9850                     {
9851                       error ("type `%T' is not derived from type `%T'",
9852                                 cname, ctype);
9853                       ctype = NULL_TREE;
9854                     }
9855                   else
9856                     ctype = cname;
9857                 }
9858
9859               /* It is valid to write:
9860
9861                    class C { void f(); };
9862                    typedef C D;
9863                    void D::f();
9864
9865                  The standard is not clear about whether `typedef const C D' is
9866                  legal; as of 2002-09-15 the committee is considering
9867                  that question.  EDG 3.0 allows that syntax.
9868                  Therefore, we do as well.  */
9869               if (ctype)
9870                 ctype = TYPE_MAIN_VARIANT (ctype);
9871               /* Update the declarator so that when we process it
9872                  again the correct type is present.  */
9873               TREE_OPERAND (decl, 0) = ctype;
9874
9875               if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9876                   && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9877                                          ctype))
9878                 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9879               next = &TREE_OPERAND (decl, 1);
9880               decl = *next;
9881               if (ctype)
9882                 {
9883                   if (TREE_CODE (decl) == IDENTIFIER_NODE
9884                       && constructor_name_p (decl, ctype))
9885                     {
9886                       sfk = sfk_constructor;
9887                       ctor_return_type = ctype;
9888                     }
9889                   else if (TREE_CODE (decl) == BIT_NOT_EXPR
9890                            && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9891                            && constructor_name_p (TREE_OPERAND (decl, 0),
9892                                                   ctype))
9893                     {
9894                       sfk = sfk_destructor;
9895                       ctor_return_type = ctype;
9896                       flags = DTOR_FLAG;
9897                       TREE_OPERAND (decl, 0) = constructor_name (ctype);
9898                       next = &TREE_OPERAND (decl, 0);
9899                     }
9900                 }
9901             }
9902             break;
9903
9904           case ERROR_MARK:
9905             next = 0;
9906             break;
9907
9908           case TYPE_DECL:
9909             /* Parse error puts this typespec where
9910                a declarator should go.  */
9911             error ("`%T' specified as declarator-id", DECL_NAME (decl));
9912             if (TREE_TYPE (decl) == current_class_type)
9913               error ("  perhaps you want `%T' for a constructor",
9914                         current_class_name);
9915             dname = DECL_NAME (decl);
9916             name = IDENTIFIER_POINTER (dname);
9917
9918             /* Avoid giving two errors for this.  */
9919             IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9920
9921             declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9922             *next = dname;
9923             next = 0;
9924             break;
9925
9926           case BASELINK:
9927             next = &BASELINK_FUNCTIONS (decl);
9928             break;
9929
9930           case TEMPLATE_DECL:
9931             /* Sometimes, we see a template-name used as part of a 
9932                decl-specifier like in 
9933                   std::allocator alloc;
9934                Handle that gracefully.  */
9935             error ("invalid use of template-name '%E' in a declarator", decl);
9936             return error_mark_node;
9937             break;
9938             
9939           default:
9940             my_friendly_assert (0, 20020917);
9941           }
9942       }
9943   }
9944
9945   /* A function definition's declarator must have the form of
9946      a function declarator.  */
9947
9948   if (funcdef_flag && innermost_code != CALL_EXPR)
9949     return 0;
9950
9951   if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9952       && innermost_code != CALL_EXPR
9953       && ! (ctype && declspecs == NULL_TREE))
9954     {
9955       error ("declaration of `%D' as non-function", dname);
9956       return void_type_node;
9957     }
9958
9959   /* Anything declared one level down from the top level
9960      must be one of the parameters of a function
9961      (because the body is at least two levels down).  */
9962
9963   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9964      by not allowing C++ class definitions to specify their parameters
9965      with xdecls (must be spec.d in the parmlist).
9966
9967      Since we now wait to push a class scope until we are sure that
9968      we are in a legitimate method context, we must set oldcname
9969      explicitly (since current_class_name is not yet alive).
9970
9971      We also want to avoid calling this a PARM if it is in a namespace.  */
9972
9973   if (decl_context == NORMAL && !toplevel_bindings_p ())
9974     {
9975       struct cp_binding_level *b = current_binding_level;
9976       current_binding_level = b->level_chain;
9977       if (current_binding_level != 0 && toplevel_bindings_p ())
9978         decl_context = PARM;
9979       current_binding_level = b;
9980     }
9981
9982   if (name == NULL)
9983     name = decl_context == PARM ? "parameter" : "type name";
9984
9985   /* Look through the decl specs and record which ones appear.
9986      Some typespecs are defined as built-in typenames.
9987      Others, the ones that are modifiers of other types,
9988      are represented by bits in SPECBITS: set the bits for
9989      the modifiers that appear.  Storage class keywords are also in SPECBITS.
9990
9991      If there is a typedef name or a type, store the type in TYPE.
9992      This includes builtin typedefs such as `int'.
9993
9994      Set EXPLICIT_INT if the type is `int' or `char' and did not
9995      come from a user typedef.
9996
9997      Set LONGLONG if `long' is mentioned twice.
9998
9999      For C++, constructors and destructors have their own fast treatment.  */
10000
10001   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10002     {
10003       register int i;
10004       register tree id;
10005
10006       /* Certain parse errors slip through.  For example,
10007          `int class;' is not caught by the parser. Try
10008          weakly to recover here.  */
10009       if (TREE_CODE (spec) != TREE_LIST)
10010         return 0;
10011
10012       id = TREE_VALUE (spec);
10013
10014       /* If the entire declaration is itself tagged as deprecated then
10015          suppress reports of deprecated items.  */
10016       if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10017         {
10018           if (deprecated_state != DEPRECATED_SUPPRESS)
10019             warn_deprecated_use (id);
10020         }
10021
10022       if (TREE_CODE (id) == IDENTIFIER_NODE)
10023         {
10024           if (id == ridpointers[(int) RID_INT]
10025               || id == ridpointers[(int) RID_CHAR]
10026               || id == ridpointers[(int) RID_BOOL]
10027               || id == ridpointers[(int) RID_WCHAR])
10028             {
10029               if (type)
10030                 {
10031                   if (id == ridpointers[(int) RID_BOOL])
10032                     error ("`bool' is now a keyword");
10033                   else
10034                     error ("extraneous `%T' ignored", id);
10035                 }
10036               else
10037                 {
10038                   if (id == ridpointers[(int) RID_INT])
10039                     explicit_int = 1;
10040                   else if (id == ridpointers[(int) RID_CHAR])
10041                     explicit_char = 1;
10042                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10043                 }
10044               goto found;
10045             }
10046           /* C++ aggregate types.  */
10047           if (IDENTIFIER_HAS_TYPE_VALUE (id))
10048             {
10049               if (type)
10050                 error ("multiple declarations `%T' and `%T'", type, id);
10051               else
10052                 type = IDENTIFIER_TYPE_VALUE (id);
10053               goto found;
10054             }
10055
10056           for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10057             {
10058               if (ridpointers[i] == id)
10059                 {
10060                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10061                     {
10062                       if (pedantic && ! in_system_header && warn_long_long)
10063                         pedwarn ("ISO C++ does not support `long long'");
10064                       if (longlong)
10065                         error ("`long long long' is too long for GCC");
10066                       else
10067                         longlong = 1;
10068                     }
10069                   else if (RIDBIT_SETP (i, specbits))
10070                     pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10071
10072                   /* Diagnose "__thread extern" or "__thread static".  */
10073                   if (RIDBIT_SETP (RID_THREAD, specbits))
10074                     {
10075                       if (i == (int)RID_EXTERN)
10076                         error ("`__thread' before `extern'");
10077                       else if (i == (int)RID_STATIC)
10078                         error ("`__thread' before `static'");
10079                     }
10080
10081                   if (i == (int)RID_EXTERN
10082                       && TREE_PURPOSE (spec) == error_mark_node)
10083                     /* This extern was part of a language linkage.  */
10084                     extern_langp = 1;
10085
10086                   RIDBIT_SET (i, specbits);
10087                   goto found;
10088                 }
10089             }
10090         }
10091       else if (TREE_CODE (id) == TYPE_DECL)
10092         {
10093           if (type)
10094             error ("multiple declarations `%T' and `%T'", type,
10095                       TREE_TYPE (id));
10096           else
10097             {
10098               type = TREE_TYPE (id);
10099               TREE_VALUE (spec) = type;
10100               typedef_decl = id;
10101             }
10102           goto found;
10103         }
10104       if (type)
10105         error ("two or more data types in declaration of `%s'", name);
10106       else if (TREE_CODE (id) == IDENTIFIER_NODE)
10107         {
10108           register tree t = lookup_name (id, 1);
10109           if (!t || TREE_CODE (t) != TYPE_DECL)
10110             error ("`%s' fails to be a typedef or built in type",
10111                    IDENTIFIER_POINTER (id));
10112           else
10113             {
10114               type = TREE_TYPE (t);
10115               typedef_decl = t;
10116             }
10117         }
10118       else if (id != error_mark_node)
10119         /* Can't change CLASS nodes into RECORD nodes here!  */
10120         type = id;
10121
10122     found: ;
10123     }
10124
10125 #if 0
10126   /* See the code below that used this.  */
10127   if (typedef_decl)
10128     decl_attr = DECL_ATTRIBUTES (typedef_decl);
10129 #endif
10130   typedef_type = type;
10131
10132   /* No type at all: default to `int', and set DEFAULTED_INT
10133      because it was not a user-defined typedef.  */
10134
10135   if (type == NULL_TREE
10136       && (RIDBIT_SETP (RID_SIGNED, specbits)
10137           || RIDBIT_SETP (RID_UNSIGNED, specbits)
10138           || RIDBIT_SETP (RID_LONG, specbits)
10139           || RIDBIT_SETP (RID_SHORT, specbits)))
10140     {
10141       /* These imply 'int'.  */
10142       type = integer_type_node;
10143       defaulted_int = 1;
10144     }
10145
10146   if (sfk != sfk_none)
10147     type = check_special_function_return_type (sfk, type,
10148                                                ctor_return_type);
10149   else if (type == NULL_TREE)
10150     {
10151       int is_main;
10152
10153       explicit_int = -1;
10154
10155       /* We handle `main' specially here, because 'main () { }' is so
10156          common.  With no options, it is allowed.  With -Wreturn-type,
10157          it is a warning.  It is only an error with -pedantic-errors.  */
10158       is_main = (funcdef_flag
10159                  && dname && MAIN_NAME_P (dname)
10160                  && ctype == NULL_TREE
10161                  && in_namespace == NULL_TREE
10162                  && current_namespace == global_namespace);
10163
10164       if (in_system_header || flag_ms_extensions)
10165         /* Allow it, sigh.  */;
10166       else if (pedantic || ! is_main)
10167         pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10168                     name);
10169       else if (warn_return_type)
10170         warning ("ISO C++ forbids declaration of `%s' with no type",
10171                     name);
10172
10173       type = integer_type_node;
10174     }
10175   
10176   ctype = NULL_TREE;
10177
10178   /* Now process the modifiers that were specified
10179      and check for invalid combinations.  */
10180
10181   /* Long double is a special combination.  */
10182
10183   if (RIDBIT_SETP (RID_LONG, specbits)
10184       && TYPE_MAIN_VARIANT (type) == double_type_node)
10185     {
10186       RIDBIT_RESET (RID_LONG, specbits);
10187       type = build_qualified_type (long_double_type_node,
10188                                    cp_type_quals (type));
10189     }
10190
10191   /* Check all other uses of type modifiers.  */
10192
10193   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10194       || RIDBIT_SETP (RID_SIGNED, specbits)
10195       || RIDBIT_SETP (RID_LONG, specbits)
10196       || RIDBIT_SETP (RID_SHORT, specbits))
10197     {
10198       int ok = 0;
10199
10200       if (TREE_CODE (type) == REAL_TYPE)
10201         error ("short, signed or unsigned invalid for `%s'", name);
10202       else if (TREE_CODE (type) != INTEGER_TYPE)
10203         error ("long, short, signed or unsigned invalid for `%s'", name);
10204       else if (RIDBIT_SETP (RID_LONG, specbits)
10205                && RIDBIT_SETP (RID_SHORT, specbits))
10206         error ("long and short specified together for `%s'", name);
10207       else if ((RIDBIT_SETP (RID_LONG, specbits)
10208                 || RIDBIT_SETP (RID_SHORT, specbits))
10209                && explicit_char)
10210         error ("long or short specified with char for `%s'", name);
10211       else if ((RIDBIT_SETP (RID_LONG, specbits)
10212                 || RIDBIT_SETP (RID_SHORT, specbits))
10213                && TREE_CODE (type) == REAL_TYPE)
10214         error ("long or short specified with floating type for `%s'", name);
10215       else if (RIDBIT_SETP (RID_SIGNED, specbits)
10216                && RIDBIT_SETP (RID_UNSIGNED, specbits))
10217         error ("signed and unsigned given together for `%s'", name);
10218       else
10219         {
10220           ok = 1;
10221           if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10222             {
10223               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10224                        name);
10225               if (flag_pedantic_errors)
10226                 ok = 0;
10227             }
10228         }
10229
10230       /* Discard the type modifiers if they are invalid.  */
10231       if (! ok)
10232         {
10233           RIDBIT_RESET (RID_UNSIGNED, specbits);
10234           RIDBIT_RESET (RID_SIGNED, specbits);
10235           RIDBIT_RESET (RID_LONG, specbits);
10236           RIDBIT_RESET (RID_SHORT, specbits);
10237           longlong = 0;
10238         }
10239     }
10240
10241   if (RIDBIT_SETP (RID_COMPLEX, specbits)
10242       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10243     {
10244       error ("complex invalid for `%s'", name);
10245       RIDBIT_RESET (RID_COMPLEX, specbits);
10246     }
10247
10248   /* Decide whether an integer type is signed or not.
10249      Optionally treat bitfields as signed by default.  */
10250   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10251       /* [class.bit]
10252
10253          It is implementation-defined whether a plain (neither
10254          explicitly signed or unsigned) char, short, int, or long
10255          bit-field is signed or unsigned.
10256
10257          Naturally, we extend this to long long as well.  Note that
10258          this does not include wchar_t.  */
10259       || (bitfield && !flag_signed_bitfields
10260           && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10261           /* A typedef for plain `int' without `signed' can be
10262              controlled just like plain `int', but a typedef for
10263              `signed int' cannot be so controlled.  */
10264           && !(typedef_decl
10265                && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10266           && (TREE_CODE (type) == INTEGER_TYPE
10267               || TREE_CODE (type) == CHAR_TYPE)
10268           && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10269     {
10270       if (longlong)
10271         type = long_long_unsigned_type_node;
10272       else if (RIDBIT_SETP (RID_LONG, specbits))
10273         type = long_unsigned_type_node;
10274       else if (RIDBIT_SETP (RID_SHORT, specbits))
10275         type = short_unsigned_type_node;
10276       else if (type == char_type_node)
10277         type = unsigned_char_type_node;
10278       else if (typedef_decl)
10279         type = c_common_unsigned_type (type);
10280       else
10281         type = unsigned_type_node;
10282     }
10283   else if (RIDBIT_SETP (RID_SIGNED, specbits)
10284            && type == char_type_node)
10285     type = signed_char_type_node;
10286   else if (longlong)
10287     type = long_long_integer_type_node;
10288   else if (RIDBIT_SETP (RID_LONG, specbits))
10289     type = long_integer_type_node;
10290   else if (RIDBIT_SETP (RID_SHORT, specbits))
10291     type = short_integer_type_node;
10292
10293   if (RIDBIT_SETP (RID_COMPLEX, specbits))
10294     {
10295       /* If we just have "complex", it is equivalent to
10296          "complex double", but if any modifiers at all are specified it is
10297          the complex form of TYPE.  E.g, "complex short" is
10298          "complex short int".  */
10299
10300       if (defaulted_int && ! longlong
10301           && ! (RIDBIT_SETP (RID_LONG, specbits)
10302                 || RIDBIT_SETP (RID_SHORT, specbits)
10303                 || RIDBIT_SETP (RID_SIGNED, specbits)
10304                 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10305         type = complex_double_type_node;
10306       else if (type == integer_type_node)
10307         type = complex_integer_type_node;
10308       else if (type == float_type_node)
10309         type = complex_float_type_node;
10310       else if (type == double_type_node)
10311         type = complex_double_type_node;
10312       else if (type == long_double_type_node)
10313         type = complex_long_double_type_node;
10314       else
10315         type = build_complex_type (type);
10316     }
10317
10318   type_quals = TYPE_UNQUALIFIED;
10319   if (RIDBIT_SETP (RID_CONST, specbits))
10320     type_quals |= TYPE_QUAL_CONST;
10321   if (RIDBIT_SETP (RID_VOLATILE, specbits))
10322     type_quals |= TYPE_QUAL_VOLATILE;
10323   if (RIDBIT_SETP (RID_RESTRICT, specbits))
10324     type_quals |= TYPE_QUAL_RESTRICT;
10325   if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10326     error ("qualifiers are not allowed on declaration of `operator %T'",
10327               ctor_return_type);
10328
10329   type_quals |= cp_type_quals (type);
10330   type = cp_build_qualified_type_real
10331     (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10332                          ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10333   /* We might have ignored or rejected some of the qualifiers.  */
10334   type_quals = cp_type_quals (type);
10335   
10336   staticp = 0;
10337   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10338   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10339   RIDBIT_RESET (RID_VIRTUAL, specbits);
10340   explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10341   RIDBIT_RESET (RID_EXPLICIT, specbits);
10342
10343   if (RIDBIT_SETP (RID_STATIC, specbits))
10344     staticp = 1 + (decl_context == FIELD);
10345
10346   if (virtualp && staticp == 2)
10347     {
10348       error ("member `%D' cannot be declared both virtual and static",
10349                 dname);
10350       staticp = 0;
10351     }
10352   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10353   RIDBIT_RESET (RID_FRIEND, specbits);
10354
10355   if (dependant_name && !friendp)
10356     {
10357       error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10358       return void_type_node;
10359     }
10360   
10361   /* Warn if two storage classes are given. Default to `auto'.  */
10362
10363   if (RIDBIT_ANY_SET (specbits))
10364     {
10365       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10366       if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10367       if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10368       if (decl_context == PARM && nclasses > 0)
10369         error ("storage class specifiers invalid in parameter declarations");
10370       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10371         {
10372           if (decl_context == PARM)
10373             error ("typedef declaration invalid in parameter declaration");
10374           nclasses++;
10375         }
10376       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10377       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10378       if (!nclasses && !friendp && extern_langp)
10379         nclasses++;
10380     }
10381
10382   /* Give error if `virtual' is used outside of class declaration.  */
10383   if (virtualp
10384       && (current_class_name == NULL_TREE || decl_context != FIELD))
10385     {
10386       error ("virtual outside class declaration");
10387       virtualp = 0;
10388     }
10389
10390   /* Static anonymous unions are dealt with here.  */
10391   if (staticp && decl_context == TYPENAME
10392       && TREE_CODE (declspecs) == TREE_LIST
10393       && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10394     decl_context = FIELD;
10395
10396   /* Warn about storage classes that are invalid for certain
10397      kinds of declarations (parameters, typenames, etc.).  */
10398
10399   /* "static __thread" and "extern __thread" are allowed.  */
10400   if (nclasses == 2
10401       && RIDBIT_SETP (RID_THREAD, specbits)
10402       && (RIDBIT_SETP (RID_EXTERN, specbits)
10403           || RIDBIT_SETP (RID_STATIC, specbits)))
10404     nclasses = 1;
10405     
10406   if (nclasses > 1)
10407     error ("multiple storage classes in declaration of `%s'", name);
10408   else if (decl_context != NORMAL && nclasses > 0)
10409     {
10410       if ((decl_context == PARM || decl_context == CATCHPARM)
10411           && (RIDBIT_SETP (RID_REGISTER, specbits)
10412               || RIDBIT_SETP (RID_AUTO, specbits)))
10413         ;
10414       else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10415         ;
10416       else if (decl_context == FIELD
10417                /* C++ allows static class elements  */
10418                && RIDBIT_SETP (RID_STATIC, specbits))
10419         /* C++ also allows inlines and signed and unsigned elements,
10420            but in those cases we don't come in here.  */
10421         ;
10422       else
10423         {
10424           if (decl_context == FIELD)
10425             {
10426               tree tmp = NULL_TREE;
10427               register int op = 0;
10428
10429               if (declarator)
10430                 {
10431                   /* Avoid trying to get an operand off an identifier node.  */
10432                   if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10433                     tmp = declarator;
10434                   else
10435                     tmp = TREE_OPERAND (declarator, 0);
10436                   op = IDENTIFIER_OPNAME_P (tmp);
10437                   if (IDENTIFIER_TYPENAME_P (tmp))
10438                     {
10439                       if (IDENTIFIER_GLOBAL_VALUE (tmp)
10440                           && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10441                               == TYPE_DECL))
10442                         name = IDENTIFIER_POINTER (tmp);
10443                       else
10444                         name = "<invalid operator>";
10445                     }
10446                 }
10447               error ("storage class specified for %s `%s'",
10448                      op ? "member operator" : "field",
10449                      name);
10450             }
10451           else
10452             {
10453               if (decl_context == PARM || decl_context == CATCHPARM)
10454                 error ("storage class specified for parameter `%s'", name);
10455               else
10456                 error ("storage class specified for typename");
10457             }
10458           RIDBIT_RESET (RID_REGISTER, specbits);
10459           RIDBIT_RESET (RID_AUTO, specbits);
10460           RIDBIT_RESET (RID_EXTERN, specbits);
10461           RIDBIT_RESET (RID_THREAD, specbits);
10462         }
10463     }
10464   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10465     {
10466       if (toplevel_bindings_p ())
10467         {
10468           /* It's common practice (and completely valid) to have a const
10469              be initialized and declared extern.  */
10470           if (!(type_quals & TYPE_QUAL_CONST))
10471             warning ("`%s' initialized and declared `extern'", name);
10472         }
10473       else
10474         error ("`%s' has both `extern' and initializer", name);
10475     }
10476   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10477            && ! toplevel_bindings_p ())
10478     error ("nested function `%s' declared `extern'", name);
10479   else if (toplevel_bindings_p ())
10480     {
10481       if (RIDBIT_SETP (RID_AUTO, specbits))
10482         error ("top-level declaration of `%s' specifies `auto'", name);
10483     }
10484   else if (RIDBIT_SETP (RID_THREAD, specbits)
10485            && !RIDBIT_SETP (RID_EXTERN, specbits)
10486            && !RIDBIT_SETP (RID_STATIC, specbits))
10487     {
10488       error ("function-scope `%s' implicitly auto and declared `__thread'",
10489              name);
10490       RIDBIT_RESET (RID_THREAD, specbits);
10491     }
10492
10493   if (nclasses > 0 && friendp)
10494     error ("storage class specifiers invalid in friend function declarations");
10495
10496   scope = get_scope_of_declarator (declarator);
10497
10498   /* Now figure out the structure of the declarator proper.
10499      Descend through it, creating more complex types, until we reach
10500      the declared identifier (or NULL_TREE, in an abstract declarator).  */
10501
10502   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10503          && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10504     {
10505       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10506          an INDIRECT_REF (for *...),
10507          a CALL_EXPR (for ...(...)),
10508          an identifier (for the name being declared)
10509          or a null pointer (for the place in an absolute declarator
10510          where the name was omitted).
10511          For the last two cases, we have just exited the loop.
10512
10513          For C++ it could also be
10514          a SCOPE_REF (for class :: ...).  In this case, we have converted
10515          sensible names to types, and those are the values we use to
10516          qualify the member name.
10517          an ADDR_EXPR (for &...),
10518          a BIT_NOT_EXPR (for destructors)
10519
10520          At this point, TYPE is the type of elements of an array,
10521          or for a function to return, or for a pointer to point to.
10522          After this sequence of ifs, TYPE is the type of the
10523          array or function or pointer, and DECLARATOR has had its
10524          outermost layer removed.  */
10525
10526       if (type == error_mark_node)
10527         {
10528           if (TREE_CODE (declarator) == SCOPE_REF)
10529             declarator = TREE_OPERAND (declarator, 1);
10530           else
10531             declarator = TREE_OPERAND (declarator, 0);
10532           continue;
10533         }
10534       if (quals != NULL_TREE
10535           && (declarator == NULL_TREE
10536               || TREE_CODE (declarator) != SCOPE_REF))
10537         {
10538           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10539             ctype = TYPE_METHOD_BASETYPE (type);
10540           if (ctype != NULL_TREE)
10541             {
10542               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10543               grok_method_quals (ctype, dummy, quals);
10544               type = TREE_TYPE (dummy);
10545               quals = NULL_TREE;
10546             }
10547         }
10548
10549       switch (TREE_CODE (declarator))
10550         {
10551         case TREE_LIST:
10552           {
10553             /* We encode a declarator with embedded attributes using
10554                a TREE_LIST.  */
10555             tree attrs = TREE_PURPOSE (declarator);
10556             tree inner_decl;
10557             int attr_flags;
10558
10559             declarator = TREE_VALUE (declarator);
10560             inner_decl = declarator;
10561             while (inner_decl != NULL_TREE
10562                    && TREE_CODE (inner_decl) == TREE_LIST)
10563               inner_decl = TREE_VALUE (inner_decl);
10564             attr_flags = 0;
10565             if (inner_decl == NULL_TREE
10566                 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10567               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10568             if (TREE_CODE (inner_decl) == CALL_EXPR)
10569               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10570             if (TREE_CODE (inner_decl) == ARRAY_REF)
10571               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10572             returned_attrs = decl_attributes (&type,
10573                                               chainon (returned_attrs, attrs),
10574                                               attr_flags);
10575           }
10576           break;
10577
10578         case ARRAY_REF:
10579           {
10580             register tree size;
10581
10582             size = TREE_OPERAND (declarator, 1);
10583
10584             /* VC++ spells a zero-sized array with [].  */
10585             if (size == NULL_TREE && decl_context == FIELD && ! staticp
10586                 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10587               size = integer_zero_node;
10588
10589             declarator = TREE_OPERAND (declarator, 0);
10590
10591             type = create_array_type_for_decl (dname, type, size);
10592
10593             ctype = NULL_TREE;
10594           }
10595           break;
10596
10597         case CALL_EXPR:
10598           {
10599             tree arg_types;
10600             int funcdecl_p;
10601             tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10602             tree inner_decl = TREE_OPERAND (declarator, 0);
10603
10604             /* Declaring a function type.
10605                Make sure we have a valid type for the function to return.  */
10606
10607             /* We now know that the TYPE_QUALS don't apply to the
10608                decl, but to its return type.  */
10609             type_quals = TYPE_UNQUALIFIED;
10610
10611             /* Warn about some types functions can't return.  */
10612
10613             if (TREE_CODE (type) == FUNCTION_TYPE)
10614               {
10615                 error ("`%s' declared as function returning a function", name);
10616                 type = integer_type_node;
10617               }
10618             if (TREE_CODE (type) == ARRAY_TYPE)
10619               {
10620                 error ("`%s' declared as function returning an array", name);
10621                 type = integer_type_node;
10622               }
10623
10624             if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10625               inner_decl = TREE_OPERAND (inner_decl, 1);
10626
10627             if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10628               inner_decl = dname;
10629
10630             /* Pick up type qualifiers which should be applied to `this'.  */
10631             quals = CALL_DECLARATOR_QUALS (declarator);
10632
10633             /* Pick up the exception specifications.  */
10634             raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10635
10636             /* Say it's a definition only for the CALL_EXPR
10637                closest to the identifier.  */
10638             funcdecl_p
10639               = inner_decl
10640               && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10641                   || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10642                   || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10643
10644             if (ctype == NULL_TREE
10645                 && decl_context == FIELD
10646                 && funcdecl_p
10647                 && (friendp == 0 || dname == current_class_name))
10648               ctype = current_class_type;
10649
10650             if (ctype && sfk == sfk_conversion)
10651               TYPE_HAS_CONVERSION (ctype) = 1;
10652             if (ctype && constructor_name_p (dname, ctype))
10653               {
10654                 /* We are within a class's scope. If our declarator name
10655                    is the same as the class name, and we are defining
10656                    a function, then it is a constructor/destructor, and
10657                    therefore returns a void type.  */
10658
10659                 if (flags == DTOR_FLAG)
10660                   {
10661                     /* ISO C++ 12.4/2.  A destructor may not be
10662                        declared const or volatile.  A destructor may
10663                        not be static.  */
10664                     if (staticp == 2)
10665                       error ("destructor cannot be static member function");
10666                     if (quals)
10667                       {
10668                         error ("destructors may not be `%s'",
10669                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10670                         quals = NULL_TREE;
10671                       }
10672                     if (decl_context == FIELD)
10673                       {
10674                         if (! member_function_or_else (ctype,
10675                                                        current_class_type,
10676                                                        flags))
10677                           return void_type_node;
10678                       }
10679                   }
10680                 else            /* It's a constructor.  */
10681                   {
10682                     if (explicitp == 1)
10683                       explicitp = 2;
10684                     /* ISO C++ 12.1.  A constructor may not be
10685                        declared const or volatile.  A constructor may
10686                        not be virtual.  A constructor may not be
10687                        static.  */
10688                     if (staticp == 2)
10689                       error ("constructor cannot be static member function");
10690                     if (virtualp)
10691                       {
10692                         pedwarn ("constructors cannot be declared virtual");
10693                         virtualp = 0;
10694                       }
10695                     if (quals)
10696                       {
10697                         error ("constructors may not be `%s'",
10698                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10699                         quals = NULL_TREE;
10700                       }
10701                     {
10702                       RID_BIT_TYPE tmp_bits;
10703                       memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10704                       RIDBIT_RESET (RID_INLINE, tmp_bits);
10705                       RIDBIT_RESET (RID_STATIC, tmp_bits);
10706                       if (RIDBIT_ANY_SET (tmp_bits))
10707                         error ("return value type specifier for constructor ignored");
10708                     }
10709                     if (decl_context == FIELD)
10710                       {
10711                         if (! member_function_or_else (ctype,
10712                                                        current_class_type,
10713                                                        flags))
10714                           return void_type_node;
10715                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10716                         if (sfk != sfk_constructor)
10717                           return NULL_TREE;
10718                       }
10719                   }
10720                 if (decl_context == FIELD)
10721                   staticp = 0;
10722               }
10723             else if (friendp)
10724               {
10725                 if (initialized)
10726                   error ("can't initialize friend function `%s'", name);
10727                 if (virtualp)
10728                   {
10729                     /* Cannot be both friend and virtual.  */
10730                     error ("virtual functions cannot be friends");
10731                     RIDBIT_RESET (RID_FRIEND, specbits);
10732                     friendp = 0;
10733                   }
10734                 if (decl_context == NORMAL)
10735                   error ("friend declaration not in class definition");
10736                 if (current_function_decl && funcdef_flag)
10737                   error ("can't define friend function `%s' in a local class definition",
10738                             name);
10739               }
10740
10741             /* Construct the function type and go to the next
10742                inner layer of declarator.  */
10743
10744             declarator = TREE_OPERAND (declarator, 0);
10745
10746             /* FIXME: This is where default args should be fully
10747                processed.  */
10748
10749             arg_types = grokparms (inner_parms);
10750
10751             if (declarator && flags == DTOR_FLAG)
10752               {
10753                 /* A destructor declared in the body of a class will
10754                    be represented as a BIT_NOT_EXPR.  But, we just
10755                    want the underlying IDENTIFIER.  */
10756                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10757                   declarator = TREE_OPERAND (declarator, 0);
10758
10759                 if (arg_types != void_list_node)
10760                   {
10761                     error ("destructors may not have parameters");
10762                     arg_types = void_list_node;
10763                     last_function_parms = NULL_TREE;
10764                   }
10765               }
10766
10767             /* ANSI says that `const int foo ();'
10768                does not make the function foo const.  */
10769             type = build_function_type (type, arg_types);
10770           }
10771           break;
10772
10773         case ADDR_EXPR:
10774         case INDIRECT_REF:
10775           /* Filter out pointers-to-references and references-to-references.
10776              We can get these if a TYPE_DECL is used.  */
10777
10778           if (TREE_CODE (type) == REFERENCE_TYPE)
10779             {
10780               error (TREE_CODE (declarator) == ADDR_EXPR
10781                      ? "cannot declare reference to `%#T'"
10782                      : "cannot declare pointer to `%#T'", type);
10783               type = TREE_TYPE (type);
10784             }
10785           else if (VOID_TYPE_P (type)
10786                    && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10787             error (ctype ? "cannot declare pointer to `%#T' member"
10788                      : "cannot declare reference to `%#T'", type);
10789
10790           /* Merge any constancy or volatility into the target type
10791              for the pointer.  */
10792
10793           /* We now know that the TYPE_QUALS don't apply to the decl,
10794              but to the target of the pointer.  */
10795           type_quals = TYPE_UNQUALIFIED;
10796
10797           if (TREE_CODE (declarator) == ADDR_EXPR)
10798             {
10799               if (!VOID_TYPE_P (type))
10800                 type = build_reference_type (type);
10801             }
10802           else if (TREE_CODE (type) == METHOD_TYPE)
10803             type = build_ptrmemfunc_type (build_pointer_type (type));
10804           else if (ctype)
10805             type = build_ptrmem_type (ctype, type);
10806           else
10807             type = build_pointer_type (type);
10808
10809           /* Process a list of type modifier keywords (such as
10810              const or volatile) that were given inside the `*' or `&'.  */
10811
10812           if (TREE_TYPE (declarator))
10813             {
10814               register tree typemodlist;
10815               int erred = 0;
10816               int constp = 0;
10817               int volatilep = 0;
10818               int restrictp = 0;
10819               
10820               for (typemodlist = TREE_TYPE (declarator); typemodlist;
10821                    typemodlist = TREE_CHAIN (typemodlist))
10822                 {
10823                   tree qualifier = TREE_VALUE (typemodlist);
10824
10825                   if (qualifier == ridpointers[(int) RID_CONST])
10826                     {
10827                       constp++;
10828                       type_quals |= TYPE_QUAL_CONST;
10829                     }
10830                   else if (qualifier == ridpointers[(int) RID_VOLATILE])
10831                     {
10832                       volatilep++;
10833                       type_quals |= TYPE_QUAL_VOLATILE;
10834                     }
10835                   else if (qualifier == ridpointers[(int) RID_RESTRICT])
10836                     {
10837                       restrictp++;
10838                       type_quals |= TYPE_QUAL_RESTRICT;
10839                     }
10840                   else if (!erred)
10841                     {
10842                       erred = 1;
10843                       error ("invalid type modifier within pointer declarator");
10844                     }
10845                 }
10846               if (constp > 1)
10847                 pedwarn ("duplicate `const'");
10848               if (volatilep > 1)
10849                 pedwarn ("duplicate `volatile'");
10850               if (restrictp > 1)
10851                 pedwarn ("duplicate `restrict'");
10852               type = cp_build_qualified_type (type, type_quals);
10853               type_quals = cp_type_quals (type);
10854             }
10855           declarator = TREE_OPERAND (declarator, 0);
10856           ctype = NULL_TREE;
10857           break;
10858
10859         case SCOPE_REF:
10860           {
10861             /* We have converted type names to NULL_TREE if the
10862                name was bogus, or to a _TYPE node, if not.
10863
10864                The variable CTYPE holds the type we will ultimately
10865                resolve to.  The code here just needs to build
10866                up appropriate member types.  */
10867             tree sname = TREE_OPERAND (declarator, 1);
10868             tree t;
10869
10870             /* Destructors can have their visibilities changed as well.  */
10871             if (TREE_CODE (sname) == BIT_NOT_EXPR)
10872               sname = TREE_OPERAND (sname, 0);
10873
10874             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10875               {
10876                 /* We had a reference to a global decl, or
10877                    perhaps we were given a non-aggregate typedef,
10878                    in which case we cleared this out, and should just
10879                    keep going as though it wasn't there.  */
10880                 declarator = sname;
10881                 continue;
10882               }
10883             ctype = TREE_OPERAND (declarator, 0);
10884
10885             t = ctype;
10886             while (t != NULL_TREE && CLASS_TYPE_P (t))
10887               {
10888                 /* You're supposed to have one `template <...>'
10889                    for every template class, but you don't need one
10890                    for a full specialization.  For example:
10891
10892                      template <class T> struct S{};
10893                      template <> struct S<int> { void f(); };
10894                      void S<int>::f () {}
10895
10896                    is correct; there shouldn't be a `template <>' for
10897                    the definition of `S<int>::f'.  */
10898                 if (CLASSTYPE_TEMPLATE_INFO (t)
10899                     && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10900                         || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10901                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10902                   template_count += 1;
10903
10904                 t = TYPE_MAIN_DECL (t);
10905                 t = DECL_CONTEXT (t);
10906               }
10907
10908             if (sname == NULL_TREE)
10909               goto done_scoping;
10910
10911             if (TREE_CODE (sname) == IDENTIFIER_NODE)
10912               {
10913                 /* This is the `standard' use of the scoping operator:
10914                    basetype :: member .  */
10915
10916                 if (ctype == current_class_type)
10917                   {
10918                     /* class A {
10919                          void A::f ();
10920                        };
10921
10922                        Is this ill-formed?  */
10923
10924                     if (pedantic)
10925                       pedwarn ("extra qualification `%T::' on member `%s' ignored",
10926                                   ctype, name);
10927                   }
10928                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10929                   {
10930                     if (current_class_type == NULL_TREE || friendp)
10931                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
10932                                                       TYPE_ARG_TYPES (type));
10933                     else
10934                       {
10935                         error ("cannot declare member function `%T::%s' within `%T'",
10936                                   ctype, name, current_class_type);
10937                         return error_mark_node;
10938                       }
10939                   }
10940                 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10941                          || COMPLETE_TYPE_P (complete_type (ctype)))
10942                   {
10943                     /* Have to move this code elsewhere in this function.
10944                        this code is used for i.e., typedef int A::M; M *pm;
10945
10946                        It is?  How? jason 10/2/94 */
10947
10948                     if (current_class_type)
10949                       {
10950                         error ("cannot declare member `%T::%s' within `%T'",
10951                                   ctype, name, current_class_type);
10952                         return void_type_node;
10953                       }
10954                   }
10955                 else
10956                   {
10957                     cxx_incomplete_type_error (NULL_TREE, ctype);
10958                     return error_mark_node;
10959                   }
10960
10961                 declarator = sname;
10962               }
10963             else if (TREE_CODE (sname) == SCOPE_REF)
10964               abort ();
10965             else
10966               {
10967               done_scoping:
10968                 declarator = TREE_OPERAND (declarator, 1);
10969                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10970                   /* In this case, we will deal with it later.  */
10971                   ;
10972                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10973                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
10974                                                   TYPE_ARG_TYPES (type));
10975               }
10976           }
10977           break;
10978
10979         case BIT_NOT_EXPR:
10980           declarator = TREE_OPERAND (declarator, 0);
10981           break;
10982
10983         case BASELINK:
10984           declarator = BASELINK_FUNCTIONS (declarator);
10985           break;
10986
10987         case RECORD_TYPE:
10988         case UNION_TYPE:
10989         case ENUMERAL_TYPE:
10990           declarator = NULL_TREE;
10991           break;
10992
10993         case ERROR_MARK:
10994           declarator = NULL_TREE;
10995           break;
10996
10997         default:
10998           abort ();
10999         }
11000     }
11001
11002   if (returned_attrs)
11003     {
11004       if (attrlist)
11005         *attrlist = chainon (returned_attrs, *attrlist);
11006       else
11007         attrlist = &returned_attrs;
11008     }
11009
11010   /* Now TYPE has the actual type.  */
11011
11012   /* Did array size calculations overflow?  */
11013
11014   if (TREE_CODE (type) == ARRAY_TYPE
11015       && COMPLETE_TYPE_P (type)
11016       && TREE_OVERFLOW (TYPE_SIZE (type)))
11017     {
11018       error ("size of array `%s' is too large", name);
11019       /* If we proceed with the array type as it is, we'll eventually
11020          crash in tree_low_cst().  */
11021       type = error_mark_node;
11022     }
11023
11024   if ((decl_context == FIELD || decl_context == PARM)
11025       && !processing_template_decl 
11026       && variably_modified_type_p (type))
11027     {
11028       if (decl_context == FIELD)
11029         error ("data member may not have variably modified type `%T'", type);
11030       else
11031         error ("parameter may not have variably modified type `%T'", type);
11032       type = error_mark_node;
11033     }
11034
11035   if (explicitp == 1 || (explicitp && friendp))
11036     {
11037       /* [dcl.fct.spec] The explicit specifier shall only be used in
11038          declarations of constructors within a class definition.  */
11039       error ("only declarations of constructors can be `explicit'");
11040       explicitp = 0;
11041     }
11042
11043   if (RIDBIT_SETP (RID_MUTABLE, specbits))
11044     {
11045       if (decl_context != FIELD || friendp)
11046         {
11047           error ("non-member `%s' cannot be declared `mutable'", name);
11048           RIDBIT_RESET (RID_MUTABLE, specbits);
11049         }
11050       else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11051         {
11052           error ("non-object member `%s' cannot be declared `mutable'", name);
11053           RIDBIT_RESET (RID_MUTABLE, specbits);
11054         }
11055       else if (TREE_CODE (type) == FUNCTION_TYPE
11056                || TREE_CODE (type) == METHOD_TYPE)
11057         {
11058           error ("function `%s' cannot be declared `mutable'", name);
11059           RIDBIT_RESET (RID_MUTABLE, specbits);
11060         }
11061       else if (staticp)
11062         {
11063           error ("static `%s' cannot be declared `mutable'", name);
11064           RIDBIT_RESET (RID_MUTABLE, specbits);
11065         }
11066       else if (type_quals & TYPE_QUAL_CONST)
11067         {
11068           error ("const `%s' cannot be declared `mutable'", name);
11069           RIDBIT_RESET (RID_MUTABLE, specbits);
11070         }
11071     }
11072
11073   if (declarator == NULL_TREE
11074       || TREE_CODE (declarator) == IDENTIFIER_NODE
11075       || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11076           && (TREE_CODE (type) == FUNCTION_TYPE
11077               || TREE_CODE (type) == METHOD_TYPE)))
11078     /* OK */;
11079   else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11080     {
11081       error ("template-id `%D' used as a declarator", declarator);
11082       declarator = dname;
11083     }
11084   else
11085     /* Unexpected declarator format.  */
11086     abort ();
11087
11088   /* If this is declaring a typedef name, return a TYPE_DECL.  */
11089
11090   if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11091     {
11092       tree decl;
11093
11094       /* Note that the grammar rejects storage classes
11095          in typenames, fields or parameters.  */
11096       if (current_lang_name == lang_name_java)
11097         TYPE_FOR_JAVA (type) = 1;
11098
11099       if (decl_context == FIELD)
11100         {
11101           if (constructor_name_p (declarator, current_class_type))
11102             pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11103                         declarator);
11104           decl = build_lang_decl (TYPE_DECL, declarator, type);
11105         }
11106       else
11107         {
11108           decl = build_decl (TYPE_DECL, declarator, type);
11109           if (in_namespace || ctype)
11110             cp_error_at ("typedef name may not be a nested-name-specifier",
11111                          decl);
11112           if (!current_function_decl)
11113             DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11114         }
11115       
11116       /* If the user declares "typedef struct {...} foo" then the
11117          struct will have an anonymous name.  Fill that name in now.
11118          Nothing can refer to it, so nothing needs know about the name
11119          change.  */
11120       if (type != error_mark_node
11121           && declarator
11122           && TYPE_NAME (type)
11123           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11124           && TYPE_ANONYMOUS_P (type)
11125           /* Don't do this if there are attributes.  */
11126           && (!attrlist || !*attrlist)
11127           && cp_type_quals (type) == TYPE_UNQUALIFIED)
11128         {
11129           tree oldname = TYPE_NAME (type);
11130           tree t;
11131
11132           /* Replace the anonymous name with the real name everywhere.  */
11133           lookup_tag_reverse (type, declarator);
11134           for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11135             if (TYPE_NAME (t) == oldname)
11136               TYPE_NAME (t) = decl;
11137
11138           if (TYPE_LANG_SPECIFIC (type))
11139             TYPE_WAS_ANONYMOUS (type) = 1;
11140
11141           /* If this is a typedef within a template class, the nested
11142              type is a (non-primary) template.  The name for the
11143              template needs updating as well.  */
11144           if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11145             DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11146               = TYPE_IDENTIFIER (type);
11147
11148           /* FIXME remangle member functions; member functions of a
11149              type with external linkage have external linkage.  */
11150         }
11151
11152       if (quals)
11153         {
11154           if (ctype == NULL_TREE)
11155             {
11156               if (TREE_CODE (type) != METHOD_TYPE)
11157                 cp_error_at ("invalid type qualifier for non-member function type", decl);
11158               else
11159                 ctype = TYPE_METHOD_BASETYPE (type);
11160             }
11161           if (ctype != NULL_TREE)
11162             grok_method_quals (ctype, decl, quals);
11163         }
11164
11165       if (RIDBIT_SETP (RID_SIGNED, specbits)
11166           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11167         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11168
11169       bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11170                       inlinep, friendp, raises != NULL_TREE);
11171
11172       return decl;
11173     }
11174
11175   /* Detect the case of an array type of unspecified size
11176      which came, as such, direct from a typedef name.
11177      We must copy the type, so that the array's domain can be
11178      individually set by the object's initializer.  */
11179
11180   if (type && typedef_type
11181       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11182       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11183     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11184
11185   /* Detect where we're using a typedef of function type to declare a
11186      function. last_function_parms will not be set, so we must create
11187      it now.  */
11188   
11189   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11190     {
11191       tree decls = NULL_TREE;
11192       tree args;
11193
11194       for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11195         {
11196           tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11197
11198           TREE_CHAIN (decl) = decls;
11199           decls = decl;
11200         }
11201       
11202       last_function_parms = nreverse (decls);
11203     }
11204
11205   /* If this is a type name (such as, in a cast or sizeof),
11206      compute the type and return it now.  */
11207
11208   if (decl_context == TYPENAME)
11209     {
11210       /* Note that the grammar rejects storage classes
11211          in typenames, fields or parameters.  */
11212       if (type_quals != TYPE_UNQUALIFIED)
11213         type_quals = TYPE_UNQUALIFIED;
11214
11215       /* Special case: "friend class foo" looks like a TYPENAME context.  */
11216       if (friendp)
11217         {
11218           if (type_quals != TYPE_UNQUALIFIED)
11219             {
11220               error ("type qualifiers specified for friend class declaration");
11221               type_quals = TYPE_UNQUALIFIED;
11222             }
11223           if (inlinep)
11224             {
11225               error ("`inline' specified for friend class declaration");
11226               inlinep = 0;
11227             }
11228
11229           if (!current_aggr)
11230             {
11231               /* Don't allow friend declaration without a class-key.  */
11232               if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11233                 pedwarn ("template parameters cannot be friends");
11234               else if (TREE_CODE (type) == TYPENAME_TYPE)
11235                 pedwarn ("friend declaration requires class-key, "
11236                          "i.e. `friend class %T::%D'",
11237                          TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11238               else
11239                 pedwarn ("friend declaration requires class-key, "
11240                          "i.e. `friend %#T'",
11241                          type);
11242             }
11243
11244           /* Only try to do this stuff if we didn't already give up.  */
11245           if (type != integer_type_node)
11246             {
11247               /* A friendly class?  */
11248               if (current_class_type)
11249                 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11250               else
11251                 error ("trying to make class `%T' a friend of global scope",
11252                           type);
11253
11254               type = void_type_node;
11255             }
11256         }
11257       else if (quals)
11258         {
11259           if (ctype == NULL_TREE)
11260             {
11261               if (TREE_CODE (type) != METHOD_TYPE)
11262                 error ("invalid qualifiers on non-member function type");
11263               else
11264                 ctype = TYPE_METHOD_BASETYPE (type);
11265             }
11266           if (ctype)
11267             {
11268               tree dummy = build_decl (TYPE_DECL, declarator, type);
11269               grok_method_quals (ctype, dummy, quals);
11270               type = TREE_TYPE (dummy);
11271             }
11272         }
11273
11274       return type;
11275     }
11276   else if (declarator == NULL_TREE && decl_context != PARM
11277            && decl_context != CATCHPARM
11278            && TREE_CODE (type) != UNION_TYPE
11279            && ! bitfield)
11280     {
11281       error ("abstract declarator `%T' used as declaration", type);
11282       declarator = make_anon_name ();
11283     }
11284
11285   /* `void' at top level (not within pointer)
11286      is allowed only in typedefs or type names.
11287      We don't complain about parms either, but that is because
11288      a better error message can be made later.  */
11289
11290   if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11291     {
11292       if (! declarator)
11293         error ("unnamed variable or field declared void");
11294       else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11295         {
11296           if (IDENTIFIER_OPNAME_P (declarator))
11297             abort ();
11298           else
11299             error ("variable or field `%s' declared void", name);
11300         }
11301       else
11302         error ("variable or field declared void");
11303       type = integer_type_node;
11304     }
11305
11306   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11307      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
11308
11309   if (decl_context == PARM || decl_context == CATCHPARM)
11310     {
11311       if (ctype || in_namespace)
11312         error ("cannot use `::' in parameter declaration");
11313
11314       /* A parameter declared as an array of T is really a pointer to T.
11315          One declared as a function is really a pointer to a function.
11316          One declared as a member is really a pointer to member.  */
11317
11318       if (TREE_CODE (type) == ARRAY_TYPE)
11319         {
11320           /* Transfer const-ness of array into that of type pointed to.  */
11321           type = build_pointer_type (TREE_TYPE (type));
11322           type_quals = TYPE_UNQUALIFIED;
11323         }
11324       else if (TREE_CODE (type) == FUNCTION_TYPE)
11325         type = build_pointer_type (type);
11326       else if (TREE_CODE (type) == OFFSET_TYPE)
11327         type = build_pointer_type (type);
11328     }
11329
11330   {
11331     register tree decl;
11332
11333     if (decl_context == PARM)
11334       {
11335         decl = cp_build_parm_decl (declarator, type);
11336
11337         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11338                         inlinep, friendp, raises != NULL_TREE);
11339       }
11340     else if (decl_context == FIELD)
11341       {
11342         if (type == error_mark_node)
11343           {
11344             /* Happens when declaring arrays of sizes which
11345                are error_mark_node, for example.  */
11346             decl = NULL_TREE;
11347           }
11348         else if (in_namespace && !friendp)
11349           {
11350             /* Something like struct S { int N::j; };  */
11351             error ("invalid use of `::'");
11352             decl = NULL_TREE;
11353           }
11354         else if (TREE_CODE (type) == FUNCTION_TYPE)
11355           {
11356             int publicp = 0;
11357             tree function_context;
11358
11359             /* We catch the others as conflicts with the builtin
11360                typedefs.  */
11361             if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11362               {
11363                 error ("function `%D' cannot be declared friend",
11364                           declarator);
11365                 friendp = 0;
11366               }
11367
11368             if (friendp == 0)
11369               {
11370                 if (ctype == NULL_TREE)
11371                   ctype = current_class_type;
11372
11373                 if (ctype == NULL_TREE)
11374                   {
11375                     error ("can't make `%D' into a method -- not in a class",
11376                               declarator);
11377                     return void_type_node;
11378                   }
11379
11380                 /* ``A union may [ ... ] not [ have ] virtual functions.''
11381                    ARM 9.5 */
11382                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11383                   {
11384                     error ("function `%D' declared virtual inside a union",
11385                               declarator);
11386                     return void_type_node;
11387                   }
11388
11389                 if (declarator == ansi_opname (NEW_EXPR)
11390                     || declarator == ansi_opname (VEC_NEW_EXPR)
11391                     || declarator == ansi_opname (DELETE_EXPR)
11392                     || declarator == ansi_opname (VEC_DELETE_EXPR))
11393                   {
11394                     if (virtualp)
11395                       {
11396                         error ("`%D' cannot be declared virtual, since it is always static",
11397                                   declarator);
11398                         virtualp = 0;
11399                       }
11400                   }
11401                 else if (staticp < 2)
11402                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11403                                                   TYPE_ARG_TYPES (type));
11404               }
11405
11406             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
11407             function_context = (ctype != NULL_TREE) ?
11408               decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11409             publicp = (! friendp || ! staticp)
11410               && function_context == NULL_TREE;
11411             decl = grokfndecl (ctype, type,
11412                                TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11413                                ? declarator : dname,
11414                                declarator,
11415                                virtualp, flags, quals, raises,
11416                                friendp ? -1 : 0, friendp, publicp, inlinep,
11417                                funcdef_flag, template_count, in_namespace);
11418             if (decl == NULL_TREE)
11419               return decl;
11420 #if 0
11421             /* This clobbers the attrs stored in `decl' from `attrlist'.  */
11422             /* The decl and setting of decl_attr is also turned off.  */
11423             decl = build_decl_attribute_variant (decl, decl_attr);
11424 #endif
11425
11426             /* [class.conv.ctor]
11427
11428                A constructor declared without the function-specifier
11429                explicit that can be called with a single parameter
11430                specifies a conversion from the type of its first
11431                parameter to the type of its class.  Such a constructor
11432                is called a converting constructor.  */
11433             if (explicitp == 2)
11434               DECL_NONCONVERTING_P (decl) = 1;
11435             else if (DECL_CONSTRUCTOR_P (decl))
11436               {
11437                 /* The constructor can be called with exactly one
11438                    parameter if there is at least one parameter, and
11439                    any subsequent parameters have default arguments.
11440                    Ignore any compiler-added parms.  */
11441                 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11442
11443                 if (arg_types == void_list_node
11444                     || (arg_types
11445                         && TREE_CHAIN (arg_types)
11446                         && TREE_CHAIN (arg_types) != void_list_node
11447                         && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11448                   DECL_NONCONVERTING_P (decl) = 1;
11449               }
11450           }
11451         else if (TREE_CODE (type) == METHOD_TYPE)
11452           {
11453             /* We only get here for friend declarations of
11454                members of other classes.  */
11455             /* All method decls are public, so tell grokfndecl to set
11456                TREE_PUBLIC, also.  */
11457             decl = grokfndecl (ctype, type, declarator, declarator,
11458                                virtualp, flags, quals, raises,
11459                                friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11460                                template_count, in_namespace);
11461             if (decl == NULL_TREE)
11462               return NULL_TREE;
11463           }
11464         else if (!staticp && ! processing_template_decl
11465                  && !COMPLETE_TYPE_P (complete_type (type))
11466                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11467           {
11468             if (declarator)
11469               error ("field `%D' has incomplete type", declarator);
11470             else
11471               error ("name `%T' has incomplete type", type);
11472
11473             /* If we're instantiating a template, tell them which
11474                instantiation made the field's type be incomplete.  */
11475             if (current_class_type
11476                 && TYPE_NAME (current_class_type)
11477                 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11478                 && declspecs && TREE_VALUE (declspecs)
11479                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11480               error ("  in instantiation of template `%T'",
11481                         current_class_type);
11482
11483             type = error_mark_node;
11484             decl = NULL_TREE;
11485           }
11486         else
11487           {
11488             if (friendp)
11489               {
11490                 error ("`%s' is neither function nor member function; cannot be declared friend",
11491                        IDENTIFIER_POINTER (declarator));
11492                 friendp = 0;
11493               }
11494             decl = NULL_TREE;
11495           }
11496
11497         if (friendp)
11498           {
11499             /* Friends are treated specially.  */
11500             if (ctype == current_class_type)
11501               warning ("member functions are implicitly friends of their class");
11502             else
11503               {
11504                 tree t = NULL_TREE;
11505                 if (decl && DECL_NAME (decl))
11506                   {
11507                     if (template_class_depth (current_class_type) == 0)
11508                       {
11509                         decl
11510                           = check_explicit_specialization
11511                           (declarator, decl,
11512                            template_count, 2 * (funcdef_flag != 0) + 4);
11513                         if (decl == error_mark_node)
11514                           return error_mark_node;
11515                       }
11516                     
11517                     t = do_friend (ctype, declarator, decl,
11518                                    last_function_parms, *attrlist,
11519                                    flags, quals, funcdef_flag);
11520                   }
11521                 if (t && funcdef_flag)
11522                   return t;
11523   
11524                 return void_type_node;
11525               }
11526           }
11527
11528         /* Structure field.  It may not be a function, except for C++ */
11529
11530         if (decl == NULL_TREE)
11531           {
11532             if (initialized)
11533               {
11534                 if (!staticp)
11535                   {
11536                     /* An attempt is being made to initialize a non-static
11537                        member.  But, from [class.mem]:
11538
11539                        4 A member-declarator can contain a
11540                        constant-initializer only if it declares a static
11541                        member (_class.static_) of integral or enumeration
11542                        type, see _class.static.data_.
11543
11544                        This used to be relatively common practice, but
11545                        the rest of the compiler does not correctly
11546                        handle the initialization unless the member is
11547                        static so we make it static below.  */
11548                     pedwarn ("ISO C++ forbids initialization of member `%D'",
11549                                 declarator);
11550                     pedwarn ("making `%D' static", declarator);
11551                     staticp = 1;
11552                   }
11553
11554                 if (uses_template_parms (type))
11555                   /* We'll check at instantiation time.  */
11556                   ;
11557                 else if (check_static_variable_definition (declarator,
11558                                                            type))
11559                   /* If we just return the declaration, crashes
11560                      will sometimes occur.  We therefore return
11561                      void_type_node, as if this was a friend
11562                      declaration, to cause callers to completely
11563                      ignore this declaration.  */
11564                   return void_type_node;
11565               }
11566
11567             if (staticp)
11568               {
11569                 /* [class.mem] forbids static data members with the
11570                    same name as the enclosing class.  Non-static data
11571                    members are checked in check_field_decls.  */
11572                 if (constructor_name_p (declarator, current_class_type))
11573                   pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11574                            declarator);
11575                   
11576                 /* C++ allows static class members.  All other work
11577                    for this is done by grokfield.  */
11578                 decl = build_lang_decl (VAR_DECL, declarator, type);
11579                 TREE_STATIC (decl) = 1;
11580                 /* In class context, 'static' means public access.  */
11581                 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11582               }
11583             else
11584               {
11585                 decl = build_decl (FIELD_DECL, declarator, type);
11586                 DECL_NONADDRESSABLE_P (decl) = bitfield;
11587                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11588                   {
11589                     DECL_MUTABLE_P (decl) = 1;
11590                     RIDBIT_RESET (RID_MUTABLE, specbits);
11591                   }
11592               }
11593
11594             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11595                             inlinep, friendp, raises != NULL_TREE);
11596           }
11597       }
11598     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11599       {
11600         tree original_name;
11601         int publicp = 0;
11602
11603         if (! declarator)
11604           return NULL_TREE;
11605
11606         if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11607           original_name = dname;
11608         else
11609           original_name = declarator;
11610
11611         if (RIDBIT_SETP (RID_AUTO, specbits))
11612           error ("storage class `auto' invalid for function `%s'", name);
11613         else if (RIDBIT_SETP (RID_REGISTER, specbits))
11614           error ("storage class `register' invalid for function `%s'", name);
11615         else if (RIDBIT_SETP (RID_THREAD, specbits))
11616           error ("storage class `__thread' invalid for function `%s'", name);
11617
11618         /* Function declaration not at top level.
11619            Storage classes other than `extern' are not allowed
11620            and `extern' makes no difference.  */
11621         if (! toplevel_bindings_p ()
11622             && (RIDBIT_SETP (RID_STATIC, specbits)
11623                 || RIDBIT_SETP (RID_INLINE, specbits))
11624             && pedantic)
11625           {
11626             if (RIDBIT_SETP (RID_STATIC, specbits))
11627               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11628             else
11629               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11630           }
11631
11632         if (ctype == NULL_TREE)
11633           {
11634             if (virtualp)
11635               {
11636                 error ("virtual non-class function `%s'", name);
11637                 virtualp = 0;
11638               }
11639           }
11640         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11641           type = build_cplus_method_type (ctype, TREE_TYPE (type),
11642                                           TYPE_ARG_TYPES (type));
11643
11644         /* Record presence of `static'.  */
11645         publicp = (ctype != NULL_TREE
11646                    || RIDBIT_SETP (RID_EXTERN, specbits)
11647                    || !RIDBIT_SETP (RID_STATIC, specbits));
11648
11649         decl = grokfndecl (ctype, type, original_name, declarator,
11650                            virtualp, flags, quals, raises,
11651                            1, friendp,
11652                            publicp, inlinep, funcdef_flag,
11653                            template_count, in_namespace);
11654         if (decl == NULL_TREE)
11655           return NULL_TREE;
11656
11657         if (staticp == 1)
11658           {
11659             int invalid_static = 0;
11660
11661             /* Don't allow a static member function in a class, and forbid
11662                declaring main to be static.  */
11663             if (TREE_CODE (type) == METHOD_TYPE)
11664               {
11665                 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11666                 invalid_static = 1;
11667               }
11668             else if (current_function_decl)
11669               {
11670                 /* FIXME need arm citation */
11671                 error ("cannot declare static function inside another function");
11672                 invalid_static = 1;
11673               }
11674
11675             if (invalid_static)
11676               {
11677                 staticp = 0;
11678                 RIDBIT_RESET (RID_STATIC, specbits);
11679               }
11680           }
11681       }
11682     else
11683       {
11684         /* It's a variable.  */
11685
11686         /* An uninitialized decl with `extern' is a reference.  */
11687         decl = grokvardecl (type, declarator, &specbits,
11688                             initialized,
11689                             (type_quals & TYPE_QUAL_CONST) != 0,
11690                             ctype ? ctype : in_namespace);
11691         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11692                         inlinep, friendp, raises != NULL_TREE);
11693
11694         if (ctype)
11695           {
11696             DECL_CONTEXT (decl) = ctype;
11697             if (staticp == 1)
11698               {
11699                 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11700                 staticp = 0;
11701                 RIDBIT_RESET (RID_STATIC, specbits);
11702               }
11703             if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11704               {
11705                 error ("static member `%D' declared `register'", decl);
11706                 RIDBIT_RESET (RID_REGISTER, specbits);
11707               }
11708             if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11709               {
11710                 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11711                             decl);
11712                 RIDBIT_RESET (RID_EXTERN, specbits);
11713               }
11714           }
11715       }
11716
11717     my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11718
11719     /* Record `register' declaration for warnings on &
11720        and in case doing stupid register allocation.  */
11721
11722     if (RIDBIT_SETP (RID_REGISTER, specbits))
11723       DECL_REGISTER (decl) = 1;
11724
11725     if (RIDBIT_SETP (RID_EXTERN, specbits))
11726       DECL_THIS_EXTERN (decl) = 1;
11727
11728     if (RIDBIT_SETP (RID_STATIC, specbits))
11729       DECL_THIS_STATIC (decl) = 1;
11730
11731     /* Record constancy and volatility.  There's no need to do this
11732        when processing a template; we'll do this for the instantiated
11733        declaration based on the type of DECL.  */
11734     if (!processing_template_decl)
11735       c_apply_type_quals_to_decl (type_quals, decl);
11736
11737     return decl;
11738   }
11739 }
11740 \f
11741 /* Subroutine of start_function.  Ensure that each of the parameter
11742    types (as listed in PARMS) is complete, as is required for a
11743    function definition.  */
11744
11745 static void
11746 require_complete_types_for_parms (tree parms)
11747 {
11748   for (; parms; parms = TREE_CHAIN (parms))
11749     {
11750       if (VOID_TYPE_P (TREE_TYPE (parms)))
11751         /* grokparms will have already issued an error */
11752         TREE_TYPE (parms) = error_mark_node;
11753       else if (complete_type_or_else (TREE_TYPE (parms), parms))
11754         {
11755           layout_decl (parms, 0);
11756           DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11757         }
11758       else
11759         TREE_TYPE (parms) = error_mark_node;
11760     }
11761 }
11762
11763 /* Returns nonzero if T is a local variable.  */
11764
11765 int
11766 local_variable_p (tree t)
11767 {
11768   if ((TREE_CODE (t) == VAR_DECL
11769        /* A VAR_DECL with a context that is a _TYPE is a static data
11770           member.  */
11771        && !TYPE_P (CP_DECL_CONTEXT (t))
11772        /* Any other non-local variable must be at namespace scope.  */
11773        && !DECL_NAMESPACE_SCOPE_P (t))
11774       || (TREE_CODE (t) == PARM_DECL))
11775     return 1;
11776
11777   return 0;
11778 }
11779
11780 /* Returns nonzero if T is an automatic local variable or a label.
11781    (These are the declarations that need to be remapped when the code
11782    containing them is duplicated.)  */
11783
11784 int
11785 nonstatic_local_decl_p (tree t)
11786 {
11787   return ((local_variable_p (t) && !TREE_STATIC (t))
11788           || TREE_CODE (t) == LABEL_DECL
11789           || TREE_CODE (t) == RESULT_DECL);
11790 }
11791
11792 /* Like local_variable_p, but suitable for use as a tree-walking
11793    function.  */
11794
11795 static tree
11796 local_variable_p_walkfn (tree* tp,
11797                          int* walk_subtrees ATTRIBUTE_UNUSED ,
11798                          void* data ATTRIBUTE_UNUSED )
11799 {
11800   return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11801           ? *tp : NULL_TREE);
11802 }
11803
11804 /* Check that ARG, which is a default-argument expression for a
11805    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
11806    something goes wrong.  DECL may also be a _TYPE node, rather than a
11807    DECL, if there is no DECL available.  */
11808
11809 tree
11810 check_default_argument (tree decl, tree arg)
11811 {
11812   tree var;
11813   tree decl_type;
11814
11815   if (TREE_CODE (arg) == DEFAULT_ARG)
11816     /* We get a DEFAULT_ARG when looking at an in-class declaration
11817        with a default argument.  Ignore the argument for now; we'll
11818        deal with it after the class is complete.  */
11819     return arg;
11820
11821   if (processing_template_decl || uses_template_parms (arg))
11822     /* We don't do anything checking until instantiation-time.  Note
11823        that there may be uninstantiated arguments even for an
11824        instantiated function, since default arguments are not
11825        instantiated until they are needed.  */
11826     return arg;
11827
11828   if (TYPE_P (decl))
11829     {
11830       decl_type = decl;
11831       decl = NULL_TREE;
11832     }
11833   else
11834     decl_type = TREE_TYPE (decl);
11835
11836   if (arg == error_mark_node
11837       || decl == error_mark_node
11838       || TREE_TYPE (arg) == error_mark_node
11839       || decl_type == error_mark_node)
11840     /* Something already went wrong.  There's no need to check
11841        further.  */
11842     return error_mark_node;
11843
11844   /* [dcl.fct.default]
11845
11846      A default argument expression is implicitly converted to the
11847      parameter type.  */
11848   if (!TREE_TYPE (arg)
11849       || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11850     {
11851       if (decl)
11852         error ("default argument for `%#D' has type `%T'",
11853                   decl, TREE_TYPE (arg));
11854       else
11855         error ("default argument for parameter of type `%T' has type `%T'",
11856                   decl_type, TREE_TYPE (arg));
11857
11858       return error_mark_node;
11859     }
11860
11861   /* [dcl.fct.default]
11862
11863      Local variables shall not be used in default argument
11864      expressions.
11865
11866      The keyword `this' shall not be used in a default argument of a
11867      member function.  */
11868   var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11869                                       NULL);
11870   if (var)
11871     {
11872       error ("default argument `%E' uses local variable `%D'",
11873                 arg, var);
11874       return error_mark_node;
11875     }
11876
11877   /* All is well.  */
11878   return arg;
11879 }
11880
11881 /* Decode the list of parameter types for a function type.
11882    Given the list of things declared inside the parens,
11883    return a list of types.
11884
11885    We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11886    flag. If unset, we append void_list_node. A parmlist declared
11887    as `(void)' is accepted as the empty parmlist.
11888
11889    Also set last_function_parms to the chain of PARM_DECLs.  */
11890
11891 static tree
11892 grokparms (tree first_parm)
11893 {
11894   tree result = NULL_TREE;
11895   tree decls = NULL_TREE;
11896   int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11897   tree parm, chain;
11898   int any_error = 0;
11899
11900   my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11901
11902   for (parm = first_parm; parm != NULL_TREE; parm = chain)
11903     {
11904       tree type = NULL_TREE;
11905       tree decl = TREE_VALUE (parm);
11906       tree init = TREE_PURPOSE (parm);
11907       tree specs, attrs;
11908
11909       chain = TREE_CHAIN (parm);
11910       /* @@ weak defense against parse errors.  */
11911       if (TREE_CODE (decl) != VOID_TYPE
11912           && TREE_CODE (decl) != TREE_LIST)
11913         {
11914           /* Give various messages as the need arises.  */
11915           if (TREE_CODE (decl) == STRING_CST)
11916             error ("invalid string constant `%E'", decl);
11917           else if (TREE_CODE (decl) == INTEGER_CST)
11918             error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11919           continue;
11920         }
11921
11922       if (parm == void_list_node)
11923         break;
11924
11925       split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11926       decl = grokdeclarator (TREE_VALUE (decl), specs,
11927                              PARM, init != NULL_TREE, &attrs);
11928       if (! decl || TREE_TYPE (decl) == error_mark_node)
11929         continue;
11930
11931       if (attrs)
11932         cplus_decl_attributes (&decl, attrs, 0);
11933
11934       type = TREE_TYPE (decl);
11935       if (VOID_TYPE_P (type))
11936         {
11937           if (same_type_p (type, void_type_node)
11938               && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11939             /* this is a parmlist of `(void)', which is ok.  */
11940             break;
11941           cxx_incomplete_type_error (decl, type);
11942           /* It's not a good idea to actually create parameters of
11943              type `void'; other parts of the compiler assume that a
11944              void type terminates the parameter list.  */
11945           type = error_mark_node;
11946           TREE_TYPE (decl) = error_mark_node;
11947         }
11948
11949       if (type != error_mark_node)
11950         {
11951           /* Top-level qualifiers on the parameters are
11952              ignored for function types.  */
11953           type = TYPE_MAIN_VARIANT (type);
11954           if (TREE_CODE (type) == METHOD_TYPE)
11955             {
11956               error ("parameter `%D' invalidly declared method type", decl);
11957               type = build_pointer_type (type);
11958               TREE_TYPE (decl) = type;
11959             }
11960           else if (TREE_CODE (type) == OFFSET_TYPE)
11961             {
11962               error ("parameter `%D' invalidly declared offset type", decl);
11963               type = build_pointer_type (type);
11964               TREE_TYPE (decl) = type;
11965             }
11966           else if (abstract_virtuals_error (decl, type))
11967             any_error = 1;  /* Seems like a good idea.  */
11968           else if (POINTER_TYPE_P (type))
11969             {
11970               /* [dcl.fct]/6, parameter types cannot contain pointers
11971                  (references) to arrays of unknown bound.  */
11972               tree t = TREE_TYPE (type);
11973               int ptr = TYPE_PTR_P (type);
11974
11975               while (1)
11976                 {
11977                   if (TYPE_PTR_P (t))
11978                     ptr = 1;
11979                   else if (TREE_CODE (t) != ARRAY_TYPE)
11980                     break;
11981                   else if (!TYPE_DOMAIN (t))
11982                     break;
11983                   t = TREE_TYPE (t);
11984                 }
11985               if (TREE_CODE (t) == ARRAY_TYPE)
11986                 error ("parameter `%D' includes %s to array of unknown bound `%T'",
11987                           decl, ptr ? "pointer" : "reference", t);
11988             }
11989
11990           if (!any_error && init)
11991             init = check_default_argument (decl, init);
11992           else
11993             init = NULL_TREE;
11994         }
11995
11996       TREE_CHAIN (decl) = decls;
11997       decls = decl;
11998       result = tree_cons (init, type, result);
11999     }
12000   decls = nreverse (decls);
12001   result = nreverse (result);
12002   if (!ellipsis)
12003     result = chainon (result, void_list_node);
12004   last_function_parms = decls;
12005
12006   return result;
12007 }
12008
12009 \f
12010 /* D is a constructor or overloaded `operator='.
12011
12012    Let T be the class in which D is declared. Then, this function
12013    returns:
12014
12015    -1 if D's is an ill-formed constructor or copy assignment operator
12016       whose first parameter is of type `T'.
12017    0  if D is not a copy constructor or copy assignment
12018       operator.
12019    1  if D is a copy constructor or copy assignment operator whose
12020       first parameter is a reference to const qualified T.
12021    2  if D is a copy constructor or copy assignment operator whose
12022       first parameter is a reference to non-const qualified T.
12023
12024    This function can be used as a predicate. Positive values indicate
12025    a copy constructor and nonzero values indicate a copy assignment
12026    operator.  */
12027
12028 int
12029 copy_fn_p (tree d)
12030 {
12031   tree args;
12032   tree arg_type;
12033   int result = 1;
12034   
12035   my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12036
12037   if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12038     /* Instantiations of template member functions are never copy
12039        functions.  Note that member functions of templated classes are
12040        represented as template functions internally, and we must
12041        accept those as copy functions.  */
12042     return 0;
12043     
12044   args = FUNCTION_FIRST_USER_PARMTYPE (d);
12045   if (!args)
12046     return 0;
12047
12048   arg_type = TREE_VALUE (args);
12049
12050   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12051     {
12052       /* Pass by value copy assignment operator.  */
12053       result = -1;
12054     }
12055   else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12056            && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12057     {
12058       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12059         result = 2;
12060     }
12061   else
12062     return 0;
12063   
12064   args = TREE_CHAIN (args);
12065
12066   if (args && args != void_list_node && !TREE_PURPOSE (args))
12067     /* There are more non-optional args.  */
12068     return 0;
12069
12070   return result;
12071 }
12072
12073 /* Remember any special properties of member function DECL.  */
12074
12075 void grok_special_member_properties (tree decl)
12076 {
12077   if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12078     ; /* Not special.  */
12079   else if (DECL_CONSTRUCTOR_P (decl))
12080     {
12081       int ctor = copy_fn_p (decl);
12082       
12083       if (ctor > 0)
12084         {
12085           /* [class.copy]
12086               
12087              A non-template constructor for class X is a copy
12088              constructor if its first parameter is of type X&, const
12089              X&, volatile X& or const volatile X&, and either there
12090              are no other parameters or else all other parameters have
12091              default arguments.  */
12092           TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12093           if (ctor > 1)
12094             TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12095         }
12096       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12097         TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12098     }
12099   else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12100     {
12101       /* [class.copy]
12102           
12103          A non-template assignment operator for class X is a copy
12104          assignment operator if its parameter is of type X, X&, const
12105          X&, volatile X& or const volatile X&.  */
12106       
12107       int assop = copy_fn_p (decl);
12108       
12109       if (assop)
12110         {
12111           TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12112           if (assop != 1)
12113             TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12114           if (DECL_PURE_VIRTUAL_P (decl))
12115             TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12116         }
12117     }
12118 }
12119
12120 /* Check a constructor DECL has the correct form.  Complains
12121    if the class has a constructor of the form X(X).  */
12122
12123 int
12124 grok_ctor_properties (tree ctype, tree decl)
12125 {
12126   int ctor_parm = copy_fn_p (decl);
12127
12128   if (ctor_parm < 0)
12129     {
12130       /* [class.copy]
12131           
12132          A declaration of a constructor for a class X is ill-formed if
12133          its first parameter is of type (optionally cv-qualified) X
12134          and either there are no other parameters or else all other
12135          parameters have default arguments.
12136           
12137          We *don't* complain about member template instantiations that
12138          have this form, though; they can occur as we try to decide
12139          what constructor to use during overload resolution.  Since
12140          overload resolution will never prefer such a constructor to
12141          the non-template copy constructor (which is either explicitly
12142          or implicitly defined), there's no need to worry about their
12143          existence.  Theoretically, they should never even be
12144          instantiated, but that's hard to forestall.  */
12145       error ("invalid constructor; you probably meant `%T (const %T&)'",
12146                 ctype, ctype);
12147       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12148       return 0;
12149     }
12150   
12151   return 1;
12152 }
12153
12154 /* An operator with this code is unary, but can also be binary.  */
12155
12156 static int
12157 ambi_op_p (enum tree_code code)
12158 {
12159   return (code == INDIRECT_REF
12160           || code == ADDR_EXPR
12161           || code == CONVERT_EXPR
12162           || code == NEGATE_EXPR
12163           || code == PREINCREMENT_EXPR
12164           || code == PREDECREMENT_EXPR);
12165 }
12166
12167 /* An operator with this name can only be unary.  */
12168
12169 static int
12170 unary_op_p (enum tree_code code)
12171 {
12172   return (code == TRUTH_NOT_EXPR
12173           || code == BIT_NOT_EXPR
12174           || code == COMPONENT_REF
12175           || code == TYPE_EXPR);
12176 }
12177
12178 /* Do a little sanity-checking on how they declared their operator.  */
12179
12180 void
12181 grok_op_properties (tree decl, int friendp)
12182 {
12183   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12184   tree argtype;
12185   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12186   tree name = DECL_NAME (decl);
12187   enum tree_code operator_code;
12188   int arity;
12189
12190   /* Count the number of arguments.  */
12191   for (argtype = argtypes, arity = 0;
12192        argtype && argtype != void_list_node;
12193        argtype = TREE_CHAIN (argtype))
12194     ++arity;
12195
12196   if (current_class_type == NULL_TREE)
12197     friendp = 1;
12198
12199   if (DECL_CONV_FN_P (decl))
12200     operator_code = TYPE_EXPR;
12201   else
12202     do
12203       {
12204 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)       \
12205         if (ansi_opname (CODE) == name)                         \
12206           {                                                     \
12207             operator_code = (CODE);                             \
12208             break;                                              \
12209           }                                                     \
12210         else if (ansi_assopname (CODE) == name)                 \
12211           {                                                     \
12212             operator_code = (CODE);                             \
12213             DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;              \
12214             break;                                              \
12215           }
12216
12217 #include "operators.def"
12218 #undef DEF_OPERATOR
12219
12220         abort ();
12221       }
12222     while (0);
12223   my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12224   SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12225
12226   if (! friendp)
12227     {
12228       switch (operator_code)
12229         {
12230         case CALL_EXPR:
12231           TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12232           break;
12233
12234         case ARRAY_REF:
12235           TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12236           break;
12237
12238         case COMPONENT_REF:
12239         case MEMBER_REF:
12240           TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12241           break;
12242
12243         case NEW_EXPR:
12244           TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12245           break;
12246
12247         case DELETE_EXPR:
12248           TYPE_GETS_DELETE (current_class_type) |= 1;
12249           break;
12250
12251         case VEC_NEW_EXPR:
12252           TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12253           break;
12254
12255         case VEC_DELETE_EXPR:
12256           TYPE_GETS_DELETE (current_class_type) |= 2;
12257           break;
12258
12259         default:
12260           break;
12261         }
12262     }
12263
12264   if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12265     {
12266       /* When the compiler encounters the definition of A::operator new, it
12267          doesn't look at the class declaration to find out if it's static.  */
12268       if (methodp)
12269         revert_static_member_fn (decl);
12270
12271       TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12272     }
12273   else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12274     {
12275       if (methodp)
12276         revert_static_member_fn (decl);
12277
12278       TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12279     }
12280   else
12281     {
12282       /* An operator function must either be a non-static member function
12283          or have at least one parameter of a class, a reference to a class,
12284          an enumeration, or a reference to an enumeration.  13.4.0.6 */
12285       if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12286         {
12287           if (operator_code == TYPE_EXPR
12288               || operator_code == CALL_EXPR
12289               || operator_code == COMPONENT_REF
12290               || operator_code == ARRAY_REF
12291               || operator_code == NOP_EXPR)
12292             error ("`%D' must be a nonstatic member function", decl);
12293           else
12294             {
12295               tree p = argtypes;
12296
12297               if (DECL_STATIC_FUNCTION_P (decl))
12298                 error ("`%D' must be either a non-static member function or a non-member function", decl);
12299
12300               if (p)
12301                 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12302                   {
12303                     tree arg = TREE_VALUE (p);
12304                     if (TREE_CODE (arg) == REFERENCE_TYPE)
12305                       arg = TREE_TYPE (arg);
12306
12307                     /* This lets bad template code slip through.  */
12308                     if (IS_AGGR_TYPE (arg)
12309                         || TREE_CODE (arg) == ENUMERAL_TYPE
12310                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12311                         || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12312                       goto foundaggr;
12313                   }
12314               error
12315                 ("`%D' must have an argument of class or enumerated type",
12316                  decl);
12317             foundaggr:
12318               ;
12319             }
12320         }
12321
12322       if (operator_code == CALL_EXPR)
12323         return;                 /* No restrictions on args.  */
12324
12325       if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12326         {
12327           tree t = TREE_TYPE (name);
12328           if (! friendp)
12329             {
12330               int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12331               const char *what = 0;
12332
12333               if (ref)
12334                 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12335
12336               if (TREE_CODE (t) == VOID_TYPE)
12337                 what = "void";
12338               else if (t == current_class_type)
12339                 what = "the same type";
12340               /* Don't force t to be complete here.  */
12341               else if (IS_AGGR_TYPE (t)
12342                        && COMPLETE_TYPE_P (t)
12343                        && DERIVED_FROM_P (t, current_class_type))
12344                 what = "a base class";
12345
12346               if (what)
12347                 warning ("conversion to %s%s will never use a type conversion operator",
12348                          ref ? "a reference to " : "", what);
12349             }
12350         }
12351       if (operator_code == COND_EXPR)
12352         {
12353           /* 13.4.0.3 */
12354           error ("ISO C++ prohibits overloading operator ?:");
12355         }
12356       else if (ambi_op_p (operator_code))
12357         {
12358           if (arity == 1)
12359             /* We pick the one-argument operator codes by default, so
12360                we don't have to change anything.  */
12361             ;
12362           else if (arity == 2)
12363             {
12364               /* If we thought this was a unary operator, we now know
12365                  it to be a binary operator.  */
12366               switch (operator_code)
12367                 {
12368                 case INDIRECT_REF:
12369                   operator_code = MULT_EXPR;
12370                   break;
12371
12372                 case ADDR_EXPR:
12373                   operator_code = BIT_AND_EXPR;
12374                   break;
12375
12376                 case CONVERT_EXPR:
12377                   operator_code = PLUS_EXPR;
12378                   break;
12379
12380                 case NEGATE_EXPR:
12381                   operator_code = MINUS_EXPR;
12382                   break;
12383
12384                 case PREINCREMENT_EXPR:
12385                   operator_code = POSTINCREMENT_EXPR;
12386                   break;
12387
12388                 case PREDECREMENT_EXPR:
12389                   operator_code = POSTDECREMENT_EXPR;
12390                   break;
12391
12392                 default:
12393                   abort ();
12394                 }
12395
12396               SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12397
12398               if ((operator_code == POSTINCREMENT_EXPR
12399                    || operator_code == POSTDECREMENT_EXPR)
12400                   && ! processing_template_decl
12401                   && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12402                 {
12403                   if (methodp)
12404                     error ("postfix `%D' must take `int' as its argument",
12405                               decl);
12406                   else
12407                     error
12408                       ("postfix `%D' must take `int' as its second argument",
12409                        decl);
12410                 }
12411             }
12412           else
12413             {
12414               if (methodp)
12415                 error ("`%D' must take either zero or one argument", decl);
12416               else
12417                 error ("`%D' must take either one or two arguments", decl);
12418             }
12419
12420           /* More Effective C++ rule 6.  */
12421           if (warn_ecpp
12422               && (operator_code == POSTINCREMENT_EXPR
12423                   || operator_code == POSTDECREMENT_EXPR
12424                   || operator_code == PREINCREMENT_EXPR
12425                   || operator_code == PREDECREMENT_EXPR))
12426             {
12427               tree arg = TREE_VALUE (argtypes);
12428               tree ret = TREE_TYPE (TREE_TYPE (decl));
12429               if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12430                 arg = TREE_TYPE (arg);
12431               arg = TYPE_MAIN_VARIANT (arg);
12432               if (operator_code == PREINCREMENT_EXPR
12433                   || operator_code == PREDECREMENT_EXPR)
12434                 {
12435                   if (TREE_CODE (ret) != REFERENCE_TYPE
12436                       || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12437                                        arg))
12438                     warning ("prefix `%D' should return `%T'", decl,
12439                                 build_reference_type (arg));
12440                 }
12441               else
12442                 {
12443                   if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12444                     warning ("postfix `%D' should return `%T'", decl, arg);
12445                 }
12446             }
12447         }
12448       else if (unary_op_p (operator_code))
12449         {
12450           if (arity != 1)
12451             {
12452               if (methodp)
12453                 error ("`%D' must take `void'", decl);
12454               else
12455                 error ("`%D' must take exactly one argument", decl);
12456             }
12457         }
12458       else /* if (binary_op_p (operator_code)) */
12459         {
12460           if (arity != 2)
12461             {
12462               if (methodp)
12463                 error ("`%D' must take exactly one argument", decl);
12464               else
12465                 error ("`%D' must take exactly two arguments", decl);
12466             }
12467
12468           /* More Effective C++ rule 7.  */
12469           if (warn_ecpp
12470               && (operator_code == TRUTH_ANDIF_EXPR
12471                   || operator_code == TRUTH_ORIF_EXPR
12472                   || operator_code == COMPOUND_EXPR))
12473             warning ("user-defined `%D' always evaluates both arguments",
12474                         decl);
12475         }
12476
12477       /* Effective C++ rule 23.  */
12478       if (warn_ecpp
12479           && arity == 2
12480           && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12481           && (operator_code == PLUS_EXPR
12482               || operator_code == MINUS_EXPR
12483               || operator_code == TRUNC_DIV_EXPR
12484               || operator_code == MULT_EXPR
12485               || operator_code == TRUNC_MOD_EXPR)
12486           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12487         warning ("`%D' should return by value", decl);
12488
12489       /* [over.oper]/8 */
12490       for (; argtypes && argtypes != void_list_node;
12491           argtypes = TREE_CHAIN (argtypes))
12492         if (TREE_PURPOSE (argtypes))
12493           {
12494             TREE_PURPOSE (argtypes) = NULL_TREE;
12495             if (operator_code == POSTINCREMENT_EXPR
12496                 || operator_code == POSTDECREMENT_EXPR)
12497               {
12498                 if (pedantic)
12499                   pedwarn ("`%D' cannot have default arguments", decl);
12500               }
12501             else
12502               error ("`%D' cannot have default arguments", decl);
12503           }
12504
12505     }
12506 }
12507 \f
12508 static const char *
12509 tag_name (enum tag_types code)
12510 {
12511   switch (code)
12512     {
12513     case record_type:
12514       return "struct";
12515     case class_type:
12516       return "class";
12517     case union_type:
12518       return "union ";
12519     case enum_type:
12520       return "enum";
12521     default:
12522       abort ();
12523     }
12524 }
12525
12526 /* Name lookup in an elaborated-type-specifier (after the keyword
12527    indicated by TAG_CODE) has found TYPE.  If the
12528    elaborated-type-specifier is invalid, issue a diagnostic and return
12529    error_mark_node; otherwise, return TYPE itself.  */
12530
12531 static tree
12532 check_elaborated_type_specifier (enum tag_types tag_code,
12533                                  tree type)
12534 {
12535   tree t;
12536
12537   t = follow_tag_typedef (type);
12538
12539   /* [dcl.type.elab] If the identifier resolves to a typedef-name or a
12540      template type-parameter, the elaborated-type-specifier is
12541      ill-formed.  */
12542   if (!t)
12543     {
12544       error ("using typedef-name `%D' after `%s'",
12545              TYPE_NAME (type), tag_name (tag_code));
12546       t = error_mark_node;
12547     }
12548   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12549     {
12550       error ("using template type parameter `%T' after `%s'",
12551              type, tag_name (tag_code));
12552       t = error_mark_node;
12553     }
12554
12555   return t;
12556 }
12557
12558 /* Get the struct, enum or union (CODE says which) with tag NAME.
12559    Define the tag as a forward-reference if it is not defined.
12560
12561    C++: If a class derivation is given, process it here, and report
12562    an error if multiple derivation declarations are not identical.
12563
12564    If this is a definition, come in through xref_tag and only look in
12565    the current frame for the name (since C++ allows new names in any
12566    scope.)  */
12567
12568 tree
12569 xref_tag (enum tag_types tag_code, tree name, tree attributes, 
12570           bool globalize)
12571 {
12572   enum tree_code code;
12573   register tree ref, t;
12574   struct cp_binding_level *b = current_binding_level;
12575   tree context = NULL_TREE;
12576
12577   timevar_push (TV_NAME_LOOKUP);
12578   switch (tag_code)
12579     {
12580     case record_type:
12581     case class_type:
12582       code = RECORD_TYPE;
12583       break;
12584     case union_type:
12585       code = UNION_TYPE;
12586       break;
12587     case enum_type:
12588       code = ENUMERAL_TYPE;
12589       break;
12590     default:
12591       abort ();
12592     }
12593
12594   /* If a cross reference is requested, look up the type
12595      already defined for this tag and return it.  */
12596   if (TYPE_P (name))
12597     {
12598       t = name;
12599       name = TYPE_IDENTIFIER (t);
12600     }
12601   else
12602     t = IDENTIFIER_TYPE_VALUE (name);
12603
12604   /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
12605   if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12606     {
12607       static int explained;
12608       tree shadowed;
12609
12610       warning ("`%s %T' declares a new type at namespace scope",
12611                   tag_name (tag_code), name);
12612       if (!explained++)
12613         warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12614                     tag_name (tag_code),
12615                     constructor_name (current_class_type),
12616                     TYPE_IDENTIFIER (t));
12617
12618       /* We need to remove the class scope binding for the
12619          TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
12620       for (shadowed = b->class_shadowed;
12621            shadowed;
12622            shadowed = TREE_CHAIN (shadowed))
12623         if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12624           {
12625             TREE_PURPOSE (shadowed) = NULL_TREE;
12626             break;
12627           }
12628     }
12629
12630   if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12631       && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12632     t = NULL_TREE;
12633
12634   if (! globalize)
12635     {
12636       /* If we know we are defining this tag, only look it up in
12637          this scope and don't try to find it as a type.  */
12638       ref = lookup_tag (code, name, b, 1);
12639     }
12640   else
12641     {
12642       if (t)
12643         {
12644           ref = check_elaborated_type_specifier (tag_code, t);
12645           if (ref == error_mark_node)
12646             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12647         }
12648       else
12649         ref = lookup_tag (code, name, b, 0);
12650
12651       if (! ref)
12652         {
12653           /* Try finding it as a type declaration.  If that wins,
12654              use it.  */
12655           ref = lookup_name (name, 1);
12656
12657           if (ref != NULL_TREE
12658               && processing_template_decl
12659               && DECL_CLASS_TEMPLATE_P (ref)
12660               && template_class_depth (current_class_type) == 0)
12661             /* Since GLOBALIZE is true, we're declaring a global
12662                template, so we want this type.  */
12663             ref = DECL_TEMPLATE_RESULT (ref);
12664
12665           if (ref && TREE_CODE (ref) == TYPE_DECL)
12666             {
12667               ref = check_elaborated_type_specifier (tag_code, 
12668                                                      TREE_TYPE (ref));
12669               if (ref == error_mark_node)
12670                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12671               if (ref && TREE_CODE (ref) != code)
12672                 ref = NULL_TREE;
12673             }
12674           else
12675             ref = NULL_TREE;
12676         }
12677
12678       if (ref && current_class_type
12679           && template_class_depth (current_class_type)
12680           && PROCESSING_REAL_TEMPLATE_DECL_P ())
12681         {
12682           /* Since GLOBALIZE is nonzero, we are not looking at a
12683              definition of this tag.  Since, in addition, we are currently
12684              processing a (member) template declaration of a template
12685              class, we must be very careful; consider:
12686
12687                template <class X>
12688                struct S1
12689
12690                template <class U>
12691                struct S2
12692                { template <class V>
12693                friend struct S1; };
12694
12695              Here, the S2::S1 declaration should not be confused with the
12696              outer declaration.  In particular, the inner version should
12697              have a template parameter of level 2, not level 1.  This
12698              would be particularly important if the member declaration
12699              were instead:
12700
12701                template <class V = U> friend struct S1;
12702
12703              say, when we should tsubst into `U' when instantiating
12704              S2.  On the other hand, when presented with:
12705
12706                  template <class T>
12707                  struct S1 {
12708                    template <class U>
12709                    struct S2 {};
12710                    template <class U>
12711                    friend struct S2;
12712                  };
12713
12714               we must find the inner binding eventually.  We
12715               accomplish this by making sure that the new type we
12716               create to represent this declaration has the right
12717               TYPE_CONTEXT.  */
12718           context = TYPE_CONTEXT (ref);
12719           ref = NULL_TREE;
12720         }
12721     }
12722
12723   if (! ref)
12724     {
12725       /* If no such tag is yet defined, create a forward-reference node
12726          and record it as the "definition".
12727          When a real declaration of this type is found,
12728          the forward-reference will be altered into a real type.  */
12729       if (code == ENUMERAL_TYPE)
12730         {
12731           error ("use of enum `%#D' without previous declaration", name);
12732
12733           ref = make_node (ENUMERAL_TYPE);
12734
12735           /* Give the type a default layout like unsigned int
12736              to avoid crashing if it does not get defined.  */
12737           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12738           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12739           TYPE_USER_ALIGN (ref) = 0;
12740           TREE_UNSIGNED (ref) = 1;
12741           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12742           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12743           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12744
12745           /* Enable us to recognize when a type is created in class context.
12746              To do nested classes correctly, this should probably be cleared
12747              out when we leave this classes scope.  Currently this in only
12748              done in `start_enum'.  */
12749
12750           pushtag (name, ref, globalize);
12751         }
12752       else
12753         {
12754           struct cp_binding_level *old_b = class_binding_level;
12755
12756           ref = make_aggr_type (code);
12757           TYPE_CONTEXT (ref) = context;
12758
12759 #ifdef NONNESTED_CLASSES
12760           /* Class types don't nest the way enums do.  */
12761           class_binding_level = (struct cp_binding_level *)0;
12762 #endif
12763           pushtag (name, ref, globalize);
12764           class_binding_level = old_b;
12765         }
12766     }
12767   else
12768     {
12769       if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12770         redeclare_class_template (ref, current_template_parms);
12771     }
12772
12773   TYPE_ATTRIBUTES (ref) = attributes;
12774
12775   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12776 }
12777
12778 tree
12779 xref_tag_from_type (tree old, tree id, int globalize)
12780 {
12781   enum tag_types tag_kind;
12782
12783   if (TREE_CODE (old) == RECORD_TYPE)
12784     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12785   else
12786     tag_kind  = union_type;
12787
12788   if (id == NULL_TREE)
12789     id = TYPE_IDENTIFIER (old);
12790
12791   return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12792 }
12793
12794 /* REF is a type (named NAME), for which we have just seen some
12795    baseclasses.  BASE_LIST is a list of those baseclasses; the
12796    TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12797    the base-class.  TREE_VIA_VIRTUAL indicates virtual
12798    inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12799    struct, or union.  */
12800
12801 void
12802 xref_basetypes (tree ref, tree base_list)
12803 {
12804   /* In the declaration `A : X, Y, ... Z' we mark all the types
12805      (A, X, Y, ..., Z) so we can check for duplicates.  */
12806   tree *basep;
12807
12808   int i;
12809   enum tag_types tag_code;
12810
12811   if (TREE_CODE (ref) == UNION_TYPE)
12812     {
12813       error ("derived union `%T' invalid", ref);
12814       return;
12815     }
12816
12817   tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12818
12819   /* First, make sure that any templates in base-classes are
12820      instantiated.  This ensures that if we call ourselves recursively
12821      we do not get confused about which classes are marked and which
12822      are not.  */
12823   basep = &base_list; 
12824   while (*basep) 
12825     {
12826       tree basetype = TREE_VALUE (*basep);
12827       if (!(processing_template_decl && uses_template_parms (basetype))
12828           && !complete_type_or_else (basetype, NULL))
12829         /* An incomplete type.  Remove it from the list.  */
12830         *basep = TREE_CHAIN (*basep);
12831       else
12832         basep = &TREE_CHAIN (*basep);
12833     }
12834
12835   SET_CLASSTYPE_MARKED (ref);
12836   i = list_length (base_list);
12837   if (i)
12838     {
12839       tree binfo = TYPE_BINFO (ref);
12840       tree binfos = make_tree_vec (i);
12841       tree accesses = make_tree_vec (i);
12842       
12843       BINFO_BASETYPES (binfo) = binfos;
12844       BINFO_BASEACCESSES (binfo) = accesses;
12845   
12846       for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12847         {
12848           tree access = TREE_PURPOSE (base_list);
12849           int via_virtual = TREE_VIA_VIRTUAL (base_list);
12850           tree basetype = TREE_VALUE (base_list);
12851           tree base_binfo;
12852           
12853           if (access == access_default_node)
12854             /* The base of a derived struct is public by default.  */
12855             access = (tag_code == class_type
12856                       ? access_private_node : access_public_node);
12857           
12858           if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12859             basetype = TREE_TYPE (basetype);
12860           if (!basetype
12861               || (TREE_CODE (basetype) != RECORD_TYPE
12862                   && TREE_CODE (basetype) != TYPENAME_TYPE
12863                   && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12864                   && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12865             {
12866               error ("base type `%T' fails to be a struct or class type",
12867                      basetype);
12868               continue;
12869             }
12870           
12871           if (CLASSTYPE_MARKED (basetype))
12872             {
12873               if (basetype == ref)
12874                 error ("recursive type `%T' undefined", basetype);
12875               else
12876                 error ("duplicate base type `%T' invalid", basetype);
12877               continue;
12878             }
12879           
12880           if (TYPE_FOR_JAVA (basetype)
12881               && (current_lang_depth () == 0))
12882             TYPE_FOR_JAVA (ref) = 1;
12883           
12884           if (CLASS_TYPE_P (basetype))
12885             {
12886               base_binfo = TYPE_BINFO (basetype);
12887               /* This flag will be in the binfo of the base type, we must
12888                  clear it after copying the base binfos.  */
12889               BINFO_DEPENDENT_BASE_P (base_binfo)
12890                 = dependent_type_p (basetype);
12891             }
12892           else
12893             base_binfo = make_binfo (size_zero_node, basetype,
12894                                      NULL_TREE, NULL_TREE);
12895           
12896           TREE_VEC_ELT (binfos, i) = base_binfo;
12897           TREE_VEC_ELT (accesses, i) = access;
12898           /* This flag will be in the binfo of the base type, we must
12899              clear it after copying the base binfos.  */
12900           TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12901           
12902           SET_CLASSTYPE_MARKED (basetype);
12903           
12904           /* We are free to modify these bits because they are meaningless
12905              at top level, and BASETYPE is a top-level type.  */
12906           if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12907             {
12908               TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12909               /* Converting to a virtual base class requires looking
12910                  up the offset of the virtual base.  */
12911               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12912             }
12913           
12914           if (CLASS_TYPE_P (basetype))
12915             {
12916               TYPE_HAS_NEW_OPERATOR (ref)
12917                 |= TYPE_HAS_NEW_OPERATOR (basetype);
12918               TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12919                 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12920               TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12921               /* If the base-class uses multiple inheritance, so do we.  */
12922               TYPE_USES_MULTIPLE_INHERITANCE (ref)
12923                 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12924               /* Likewise, if converting to a base of the base may require
12925                  code, then we may need to generate code to convert to a
12926                  base as well.  */
12927               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12928                 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12929             }
12930           i++;
12931         }
12932       if (i)
12933         TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12934       else
12935         BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12936       
12937       if (i > 1)
12938         {
12939           TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12940           /* If there is more than one non-empty they cannot be at the same
12941              address.  */
12942           TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12943         }
12944     }
12945   
12946   /* Copy the base binfos, collect the virtual bases and set the
12947      inheritance order chain.  */
12948   copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12949   CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12950   
12951   /* Unmark all the types.  */
12952   while (i--)
12953     {
12954       tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12955       
12956       CLEAR_CLASSTYPE_MARKED (basetype);
12957       if (CLASS_TYPE_P (basetype))
12958         {
12959           TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12960           BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12961         }
12962     }
12963   CLEAR_CLASSTYPE_MARKED (ref);
12964 }
12965
12966 \f
12967 /* Begin compiling the definition of an enumeration type.
12968    NAME is its name (or null if anonymous).
12969    Returns the type object, as yet incomplete.
12970    Also records info about it so that build_enumerator
12971    may be used to declare the individual values as they are read.  */
12972
12973 tree
12974 start_enum (tree name)
12975 {
12976   register tree enumtype = NULL_TREE;
12977   struct cp_binding_level *b = current_binding_level;
12978
12979   /* If this is the real definition for a previous forward reference,
12980      fill in the contents in the same object that used to be the
12981      forward reference.  */
12982
12983   if (name != NULL_TREE)
12984     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12985
12986   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12987     {
12988       error ("multiple definition of `%#T'", enumtype);
12989       cp_error_at ("previous definition here", enumtype);
12990       /* Clear out TYPE_VALUES, and start again.  */
12991       TYPE_VALUES (enumtype) = NULL_TREE;
12992     }
12993   else
12994     {
12995       enumtype = make_node (ENUMERAL_TYPE);
12996       pushtag (name, enumtype, 0);
12997     }
12998
12999   return enumtype;
13000 }
13001
13002 /* After processing and defining all the values of an enumeration type,
13003    install their decls in the enumeration type and finish it off.
13004    ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
13005
13006 void
13007 finish_enum (tree enumtype)
13008 {
13009   tree pair;
13010   tree minnode;
13011   tree maxnode;
13012   tree t;
13013   bool unsignedp;
13014   int lowprec;
13015   int highprec; 
13016   int precision;
13017
13018   /* We built up the VALUES in reverse order.  */
13019   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13020
13021   /* For an enum defined in a template, just set the type of the values;
13022      all further processing is postponed until the template is
13023      instantiated.  We need to set the type so that tsubst of a CONST_DECL
13024      works.  */
13025   if (processing_template_decl)
13026     {
13027       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13028         TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13029       if (at_function_scope_p ())
13030         add_stmt (build_min (TAG_DEFN, enumtype));
13031       return;
13032     }
13033
13034   if (TYPE_VALUES (enumtype))
13035     {
13036       minnode = maxnode = NULL_TREE;
13037
13038       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13039         {
13040           tree decl = TREE_VALUE (pair);
13041           tree value = DECL_INITIAL (decl);
13042
13043           /* [dcl.enum]: Following the closing brace of an enum-specifier,
13044              each enumerator has the type of its enumeration.  Prior to the
13045              closing brace, the type of each enumerator is the type of its
13046              initializing value.  */
13047           TREE_TYPE (decl) = enumtype;
13048
13049           /* Figure out what the minimum and maximum values of the
13050              enumerators are.  */
13051           if (!minnode)
13052             minnode = maxnode = value;
13053           else if (tree_int_cst_lt (maxnode, value))
13054             maxnode = value;
13055           else if (tree_int_cst_lt (value, minnode))
13056             minnode = value;
13057
13058           /* Set the TREE_TYPE for the values as well.  That's so that when
13059              we call decl_constant_value we get an entity of the right type
13060              (but with the constant value).  But first make a copy so we
13061              don't clobber shared INTEGER_CSTs.  */
13062           if (TREE_TYPE (value) != enumtype)
13063             {
13064               value = DECL_INITIAL (decl) = copy_node (value);
13065               TREE_TYPE (value) = enumtype;
13066             }
13067
13068           /* In addition, transform the TYPE_VALUES list to contain the
13069              values, rather than the CONST_DECLs for them.  */
13070           TREE_VALUE (pair) = value;
13071         }
13072     }
13073   else
13074     minnode = maxnode = integer_zero_node;
13075
13076   /* Compute the number of bits require to represent all values of the
13077      enumeration.  We must do this before the type of MINNODE and
13078      MAXNODE are transformed, since min_precision relies on the
13079      TREE_TYPE of the value it is passed.  */
13080   unsignedp = tree_int_cst_sgn (minnode) >= 0;
13081   lowprec = min_precision (minnode, unsignedp);
13082   highprec = min_precision (maxnode, unsignedp);
13083   precision = MAX (lowprec, highprec);
13084
13085   /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
13086   TYPE_SIZE (enumtype) = NULL_TREE;
13087   TYPE_PRECISION (enumtype) = precision;
13088   if (unsignedp)
13089     fixup_unsigned_type (enumtype);
13090   else
13091     fixup_signed_type (enumtype);
13092
13093   if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13094     /* Use the width of the narrowest normal C type which is wide
13095        enough.  */
13096     TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13097                                                 (precision, 1));
13098   else
13099     TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13100
13101   TYPE_SIZE (enumtype) = NULL_TREE;
13102   layout_type (enumtype);
13103
13104   /* Fix up all variant types of this enum type.  */
13105   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13106     {
13107       TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13108       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13109       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13110       TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13111       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13112       TYPE_MODE (t) = TYPE_MODE (enumtype);
13113       TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13114       TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13115       TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13116       TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13117     }
13118
13119   /* Finish debugging output for this type.  */
13120   rest_of_type_compilation (enumtype, namespace_bindings_p ());
13121 }
13122
13123 /* Build and install a CONST_DECL for an enumeration constant of the
13124    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13125    Assignment of sequential values by default is handled here.  */
13126
13127 void
13128 build_enumerator (tree name, tree value, tree enumtype)
13129 {
13130   tree decl;
13131   tree context;
13132   tree type;
13133
13134   /* Remove no-op casts from the value.  */
13135   if (value)
13136     STRIP_TYPE_NOPS (value);
13137
13138   if (! processing_template_decl)
13139     {
13140       /* Validate and default VALUE.  */
13141       if (value != NULL_TREE)
13142         {
13143           value = decl_constant_value (value);
13144
13145           if (TREE_CODE (value) == INTEGER_CST)
13146             {
13147               value = default_conversion (value);
13148               constant_expression_warning (value);
13149             }
13150           else
13151             {
13152               error ("enumerator value for `%D' not integer constant", name);
13153               value = NULL_TREE;
13154             }
13155         }
13156
13157       /* Default based on previous value.  */
13158       if (value == NULL_TREE)
13159         {
13160           tree prev_value;
13161
13162           if (TYPE_VALUES (enumtype))
13163             {
13164               /* The next value is the previous value ...  */
13165               prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13166               /* ... plus one.  */
13167               value = cp_build_binary_op (PLUS_EXPR,
13168                                           prev_value,
13169                                           integer_one_node);
13170
13171               if (tree_int_cst_lt (value, prev_value))
13172                 error ("overflow in enumeration values at `%D'", name);
13173             }
13174           else
13175             value = integer_zero_node;
13176         }
13177
13178       /* Remove no-op casts from the value.  */
13179       STRIP_TYPE_NOPS (value);
13180     }
13181
13182   /* C++ associates enums with global, function, or class declarations.  */
13183   context = current_scope ();
13184
13185   /* Build the actual enumeration constant.  Note that the enumeration
13186     constants have the type of their initializers until the
13187     enumeration is complete:
13188
13189       [ dcl.enum ]
13190
13191       Following the closing brace of an enum-specifier, each enumer-
13192       ator has the type of its enumeration.  Prior to the closing
13193       brace, the type of each enumerator is the type of its
13194       initializing value.
13195
13196     In finish_enum we will reset the type.  Of course, if we're
13197     processing a template, there may be no value.  */
13198   type = value ? TREE_TYPE (value) : NULL_TREE;
13199
13200   if (context && context == current_class_type)
13201     /* This enum declaration is local to the class.  We need the full
13202        lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
13203     decl = build_lang_decl (CONST_DECL, name, type);
13204   else
13205     /* It's a global enum, or it's local to a function.  (Note local to
13206       a function could mean local to a class method.  */
13207     decl = build_decl (CONST_DECL, name, type);
13208
13209   DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13210   TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13211   DECL_INITIAL (decl) = value;
13212
13213   if (context && context == current_class_type)
13214     /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13215       on the TYPE_FIELDS list for `S'.  (That's so that you can say
13216       things like `S::i' later.)  */
13217     finish_member_declaration (decl);
13218   else
13219     pushdecl (decl);
13220
13221   /* Add this enumeration constant to the list for this type.  */
13222   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13223 }
13224
13225 \f
13226 /* We're defining DECL.  Make sure that it's type is OK.  */
13227
13228 static void
13229 check_function_type (tree decl, tree current_function_parms)
13230 {
13231   tree fntype = TREE_TYPE (decl);
13232   tree return_type = complete_type (TREE_TYPE (fntype));
13233
13234   /* In a function definition, arg types must be complete.  */
13235   require_complete_types_for_parms (current_function_parms);
13236
13237   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13238     {
13239       error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13240
13241       /* Make it return void instead, but don't change the
13242          type of the DECL_RESULT, in case we have a named return value.  */
13243       if (TREE_CODE (fntype) == METHOD_TYPE)
13244         {
13245           tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13246           TREE_TYPE (decl)
13247             = build_cplus_method_type (ctype,
13248                                        void_type_node,
13249                                        FUNCTION_ARG_CHAIN (decl));
13250         }
13251       else
13252         TREE_TYPE (decl)
13253           = build_function_type (void_type_node,
13254                                  TYPE_ARG_TYPES (TREE_TYPE (decl)));
13255       TREE_TYPE (decl)
13256         = build_exception_variant (fntype,
13257                                    TYPE_RAISES_EXCEPTIONS (fntype));
13258     }
13259   else
13260     abstract_virtuals_error (decl, TREE_TYPE (fntype));
13261 }
13262
13263 /* Create the FUNCTION_DECL for a function definition.
13264    DECLSPECS and DECLARATOR are the parts of the declaration;
13265    they describe the function's name and the type it returns,
13266    but twisted together in a fashion that parallels the syntax of C.
13267
13268    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13269    DECLARATOR is really the DECL for the function we are about to
13270    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13271    indicating that the function is an inline defined in-class.
13272
13273    This function creates a binding context for the function body
13274    as well as setting up the FUNCTION_DECL in current_function_decl.
13275
13276    Returns 1 on success.  If the DECLARATOR is not suitable for a function
13277    (it defines a datum instead), we return 0, which tells
13278    yyparse to report a parse error.
13279
13280    For C++, we must first check whether that datum makes any sense.
13281    For example, "class A local_a(1,2);" means that variable local_a
13282    is an aggregate of type A, which should have a constructor
13283    applied to it with the argument list [1, 2].  */
13284
13285 int
13286 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13287 {
13288   tree decl1;
13289   tree ctype = NULL_TREE;
13290   tree fntype;
13291   tree restype;
13292   int doing_friend = 0;
13293   struct cp_binding_level *bl;
13294   tree current_function_parms;
13295
13296   /* Sanity check.  */
13297   my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13298   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13299
13300   /* This should only be done once on the top most decl.  */
13301   if (have_extern_spec)
13302     {
13303       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13304       have_extern_spec = false;
13305     }
13306
13307   if (flags & SF_PRE_PARSED)
13308     {
13309       decl1 = declarator;
13310
13311       fntype = TREE_TYPE (decl1);
13312       if (TREE_CODE (fntype) == METHOD_TYPE)
13313         ctype = TYPE_METHOD_BASETYPE (fntype);
13314
13315       /* ISO C++ 11.4/5.  A friend function defined in a class is in
13316          the (lexical) scope of the class in which it is defined.  */
13317       if (!ctype && DECL_FRIEND_P (decl1))
13318         {
13319           ctype = DECL_FRIEND_CONTEXT (decl1);
13320
13321           /* CTYPE could be null here if we're dealing with a template;
13322              for example, `inline friend float foo()' inside a template
13323              will have no CTYPE set.  */
13324           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13325             ctype = NULL_TREE;
13326           else
13327             doing_friend = 1;
13328         }
13329
13330       last_function_parms = DECL_ARGUMENTS (decl1);
13331     }
13332   else
13333     {
13334       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13335       /* If the declarator is not suitable for a function definition,
13336          cause a syntax error.  */
13337       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13338         return 0;
13339
13340       cplus_decl_attributes (&decl1, attrs, 0);
13341
13342       /* If #pragma weak was used, mark the decl weak now.  */
13343       if (current_binding_level == global_binding_level)
13344         maybe_apply_pragma_weak (decl1);
13345
13346       fntype = TREE_TYPE (decl1);
13347
13348       restype = TREE_TYPE (fntype);
13349       if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13350         {
13351           error ("semicolon missing after declaration of `%#T'", restype);
13352           shadow_tag (build_tree_list (NULL_TREE, restype));
13353           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13354           if (TREE_CODE (fntype) == FUNCTION_TYPE)
13355             fntype = build_function_type (integer_type_node,
13356                                           TYPE_ARG_TYPES (fntype));
13357           else
13358             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13359                                               integer_type_node,
13360                                               TYPE_ARG_TYPES (fntype));
13361           TREE_TYPE (decl1) = fntype;
13362         }
13363
13364       if (TREE_CODE (fntype) == METHOD_TYPE)
13365         ctype = TYPE_METHOD_BASETYPE (fntype);
13366       else if (DECL_MAIN_P (decl1))
13367         {
13368           /* If this doesn't return integer_type, or a typedef to
13369              integer_type, complain.  */
13370           if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node))
13371             {
13372               if (pedantic || warn_return_type)
13373                 pedwarn ("return type for `main' changed to `int'");
13374               TREE_TYPE (decl1) = fntype = default_function_type;
13375             }
13376         }
13377     }
13378
13379   if (DECL_DECLARED_INLINE_P (decl1)
13380       && lookup_attribute ("noinline", attrs))
13381     warning ("%Hinline function '%D' given attribute noinline",
13382              &DECL_SOURCE_LOCATION (decl1), decl1);
13383
13384   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13385     /* This is a constructor, we must ensure that any default args
13386        introduced by this definition are propagated to the clones
13387        now. The clones are used directly in overload resolution.  */
13388     adjust_clone_args (decl1);
13389
13390   /* Sometimes we don't notice that a function is a static member, and
13391      build a METHOD_TYPE for it.  Fix that up now.  */
13392   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13393       && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13394     {
13395       revert_static_member_fn (decl1);
13396       last_function_parms = TREE_CHAIN (last_function_parms);
13397       ctype = NULL_TREE;
13398     }
13399
13400   /* Warn if function was previously implicitly declared
13401      (but not if we warned then).  */
13402   if (! warn_implicit
13403       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13404     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13405
13406   /* Set up current_class_type, and enter the scope of the class, if
13407      appropriate.  */
13408   if (ctype)
13409     push_nested_class (ctype);
13410   else if (DECL_STATIC_FUNCTION_P (decl1))
13411     push_nested_class (DECL_CONTEXT (decl1));
13412
13413   /* Now that we have entered the scope of the class, we must restore
13414      the bindings for any template parameters surrounding DECL1, if it
13415      is an inline member template.  (Order is important; consider the
13416      case where a template parameter has the same name as a field of
13417      the class.)  It is not until after this point that
13418      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
13419   if (flags & SF_INCLASS_INLINE)
13420     maybe_begin_member_template_processing (decl1);
13421
13422   /* Effective C++ rule 15.  */
13423   if (warn_ecpp
13424       && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13425       && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13426     warning ("`operator=' should return a reference to `*this'");
13427
13428   /* Make the init_value nonzero so pushdecl knows this is not tentative.
13429      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
13430   if (!DECL_INITIAL (decl1))
13431     DECL_INITIAL (decl1) = error_mark_node;
13432
13433   /* This function exists in static storage.
13434      (This does not mean `static' in the C sense!)  */
13435   TREE_STATIC (decl1) = 1;
13436
13437   /* We must call push_template_decl after current_class_type is set
13438      up.  (If we are processing inline definitions after exiting a
13439      class scope, current_class_type will be NULL_TREE until set above
13440      by push_nested_class.)  */
13441   if (processing_template_decl)
13442     decl1 = push_template_decl (decl1);
13443
13444   /* We are now in the scope of the function being defined.  */
13445   current_function_decl = decl1;
13446
13447   /* Save the parm names or decls from this function's declarator
13448      where store_parm_decls will find them.  */
13449   current_function_parms = last_function_parms;
13450
13451   /* Make sure the parameter and return types are reasonable.  When
13452      you declare a function, these types can be incomplete, but they
13453      must be complete when you define the function.  */
13454   if (! processing_template_decl)
13455     check_function_type (decl1, current_function_parms);
13456
13457   /* Build the return declaration for the function.  */
13458   restype = TREE_TYPE (fntype);
13459   /* Promote the value to int before returning it.  */
13460   if (c_promoting_integer_type_p (restype))
13461     restype = type_promotes_to (restype);
13462   if (DECL_RESULT (decl1) == NULL_TREE)
13463     {
13464       DECL_RESULT (decl1)
13465         = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13466       c_apply_type_quals_to_decl (cp_type_quals (restype),
13467                                   DECL_RESULT (decl1));
13468     }
13469
13470   /* Initialize RTL machinery.  We cannot do this until
13471      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
13472      even when processing a template; this is how we get
13473      CFUN set up, and our per-function variables initialized.
13474      FIXME factor out the non-RTL stuff.  */
13475   bl = current_binding_level;
13476   init_function_start (decl1, input_filename, input_line);
13477   current_binding_level = bl;
13478
13479   /* Even though we're inside a function body, we still don't want to
13480      call expand_expr to calculate the size of a variable-sized array.
13481      We haven't necessarily assigned RTL to all variables yet, so it's
13482      not safe to try to expand expressions involving them.  */
13483   immediate_size_expand = 0;
13484   cfun->x_dont_save_pending_sizes_p = 1;
13485
13486   /* Start the statement-tree, start the tree now.  */
13487   begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13488
13489   /* Don't double-count statements in templates.  */
13490   DECL_NUM_STMTS (decl1) = 0;
13491
13492   /* Let the user know we're compiling this function.  */
13493   announce_function (decl1);
13494
13495   /* Record the decl so that the function name is defined.
13496      If we already have a decl for this name, and it is a FUNCTION_DECL,
13497      use the old decl.  */
13498   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13499     {
13500       /* A specialization is not used to guide overload resolution.  */
13501       if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13502           && ! DECL_FUNCTION_MEMBER_P (decl1))
13503         decl1 = pushdecl (decl1);
13504       else
13505         {
13506           /* We need to set the DECL_CONTEXT.  */
13507           if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13508             DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13509           /* And make sure we have enough default args.  */
13510           check_default_args (decl1);
13511         }
13512       fntype = TREE_TYPE (decl1);
13513     }
13514
13515   /* Reset these in case the call to pushdecl changed them.  */
13516   current_function_decl = decl1;
13517   cfun->decl = decl1;
13518
13519   /* If we are (erroneously) defining a function that we have already
13520      defined before, wipe out what we knew before.  */
13521   if (!DECL_PENDING_INLINE_P (decl1))
13522     DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13523
13524   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13525     {
13526       /* We know that this was set up by `grokclassfn'.  We do not
13527          wait until `store_parm_decls', since evil parse errors may
13528          never get us to that point.  Here we keep the consistency
13529          between `current_class_type' and `current_class_ptr'.  */
13530       tree t = DECL_ARGUMENTS (decl1);
13531
13532       my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13533                           162);
13534       my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13535                           19990811);
13536
13537       cp_function_chain->x_current_class_ref
13538         = build_indirect_ref (t, NULL);
13539       cp_function_chain->x_current_class_ptr = t;
13540
13541       /* Constructors and destructors need to know whether they're "in
13542          charge" of initializing virtual base classes.  */
13543       t = TREE_CHAIN (t);
13544       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13545         {
13546           current_in_charge_parm = t;
13547           t = TREE_CHAIN (t);
13548         }
13549       if (DECL_HAS_VTT_PARM_P (decl1))
13550         {
13551           if (DECL_NAME (t) != vtt_parm_identifier)
13552             abort ();
13553           current_vtt_parm = t;
13554         }
13555     }
13556
13557   if (DECL_INTERFACE_KNOWN (decl1))
13558     {
13559       tree ctx = decl_function_context (decl1);
13560
13561       if (DECL_NOT_REALLY_EXTERN (decl1))
13562         DECL_EXTERNAL (decl1) = 0;
13563
13564       if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13565           && TREE_PUBLIC (ctx))
13566         /* This is a function in a local class in an extern inline
13567            function.  */
13568         comdat_linkage (decl1);
13569     }
13570   /* If this function belongs to an interface, it is public.
13571      If it belongs to someone else's interface, it is also external.
13572      This only affects inlines and template instantiations.  */
13573   else if (interface_unknown == 0
13574            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13575                || flag_alt_external_templates))
13576     {
13577       if (DECL_DECLARED_INLINE_P (decl1) 
13578           || DECL_TEMPLATE_INSTANTIATION (decl1)
13579           || processing_template_decl)
13580         {
13581           DECL_EXTERNAL (decl1)
13582             = (interface_only
13583                || (DECL_DECLARED_INLINE_P (decl1) 
13584                    && ! flag_implement_inlines
13585                    && !DECL_VINDEX (decl1)));
13586
13587           /* For WIN32 we also want to put these in linkonce sections.  */
13588           maybe_make_one_only (decl1);
13589         }
13590       else
13591         DECL_EXTERNAL (decl1) = 0;
13592       DECL_NOT_REALLY_EXTERN (decl1) = 0;
13593       DECL_INTERFACE_KNOWN (decl1) = 1;
13594     }
13595   else if (interface_unknown && interface_only
13596            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13597                || flag_alt_external_templates))
13598     {
13599       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13600          interface, we will have interface_only set but not
13601          interface_known.  In that case, we don't want to use the normal
13602          heuristics because someone will supply a #pragma implementation
13603          elsewhere, and deducing it here would produce a conflict.  */
13604       comdat_linkage (decl1);
13605       DECL_EXTERNAL (decl1) = 0;
13606       DECL_INTERFACE_KNOWN (decl1) = 1;
13607       DECL_DEFER_OUTPUT (decl1) = 1;
13608     }
13609   else
13610     {
13611       /* This is a definition, not a reference.
13612          So clear DECL_EXTERNAL.  */
13613       DECL_EXTERNAL (decl1) = 0;
13614
13615       if ((DECL_DECLARED_INLINE_P (decl1) 
13616            || DECL_TEMPLATE_INSTANTIATION (decl1))
13617           && ! DECL_INTERFACE_KNOWN (decl1)
13618           /* Don't try to defer nested functions for now.  */
13619           && ! decl_function_context (decl1))
13620         DECL_DEFER_OUTPUT (decl1) = 1;
13621       else
13622         DECL_INTERFACE_KNOWN (decl1) = 1;
13623     }
13624
13625   pushlevel (0);
13626   current_binding_level->parm_flag = 1;
13627
13628   ++function_depth;
13629
13630   if (DECL_DESTRUCTOR_P (decl1))
13631     {
13632       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13633       DECL_CONTEXT (dtor_label) = current_function_decl;
13634     }
13635
13636   start_fname_decls ();
13637   
13638   store_parm_decls (current_function_parms);
13639
13640   return 1;
13641 }
13642 \f
13643 /* Store the parameter declarations into the current function declaration.
13644    This is called after parsing the parameter declarations, before
13645    digesting the body of the function.
13646
13647    Also install to binding contour return value identifier, if any.  */
13648
13649 static void
13650 store_parm_decls (tree current_function_parms)
13651 {
13652   register tree fndecl = current_function_decl;
13653   register tree parm;
13654
13655   /* This is a chain of any other decls that came in among the parm
13656      declarations.  If a parm is declared with  enum {foo, bar} x;
13657      then CONST_DECLs for foo and bar are put here.  */
13658   tree nonparms = NULL_TREE;
13659
13660   if (current_function_parms)
13661     {
13662       /* This case is when the function was defined with an ANSI prototype.
13663          The parms already have decls, so we need not do anything here
13664          except record them as in effect
13665          and complain if any redundant old-style parm decls were written.  */
13666
13667       tree specparms = current_function_parms;
13668       tree next;
13669
13670       /* Must clear this because it might contain TYPE_DECLs declared
13671              at class level.  */
13672       storedecls (NULL_TREE);
13673
13674       /* If we're doing semantic analysis, then we'll call pushdecl
13675              for each of these.  We must do them in reverse order so that
13676              they end in the correct forward order.  */
13677       specparms = nreverse (specparms);
13678
13679       for (parm = specparms; parm; parm = next)
13680         {
13681           next = TREE_CHAIN (parm);
13682           if (TREE_CODE (parm) == PARM_DECL)
13683             {
13684               if (DECL_NAME (parm) == NULL_TREE
13685                   || TREE_CODE (parm) != VOID_TYPE)
13686                 pushdecl (parm);
13687               else
13688                 error ("parameter `%D' declared void", parm);
13689             }
13690           else
13691             {
13692               /* If we find an enum constant or a type tag,
13693                  put it aside for the moment.  */
13694               TREE_CHAIN (parm) = NULL_TREE;
13695               nonparms = chainon (nonparms, parm);
13696             }
13697         }
13698
13699       /* Get the decls in their original chain order and record in the
13700          function.  This is all and only the PARM_DECLs that were
13701          pushed into scope by the loop above.  */
13702       DECL_ARGUMENTS (fndecl) = getdecls ();
13703       storetags (gettags ());
13704     }
13705   else
13706     DECL_ARGUMENTS (fndecl) = NULL_TREE;
13707
13708   /* Now store the final chain of decls for the arguments
13709      as the decl-chain of the current lexical scope.
13710      Put the enumerators in as well, at the front so that
13711      DECL_ARGUMENTS is not modified.  */
13712   storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13713
13714   /* Do the starting of the exception specifications, if we have any.  */
13715   if (flag_exceptions && !processing_template_decl
13716       && flag_enforce_eh_specs
13717       && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13718     current_eh_spec_block = begin_eh_spec_block ();
13719 }
13720
13721 \f
13722 /* We have finished doing semantic analysis on DECL, but have not yet
13723    generated RTL for its body.  Save away our current state, so that
13724    when we want to generate RTL later we know what to do.  */
13725
13726 static void
13727 save_function_data (tree decl)
13728 {
13729   struct language_function *f;
13730
13731   /* Save the language-specific per-function data so that we can
13732      get it back when we really expand this function.  */
13733   my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13734                       19990908);
13735
13736   /* Make a copy.  */
13737   f = ((struct language_function *)
13738        ggc_alloc (sizeof (struct language_function)));
13739   memcpy (f, cp_function_chain, sizeof (struct language_function));
13740   DECL_SAVED_FUNCTION_DATA (decl) = f;
13741
13742   /* Clear out the bits we don't need.  */
13743   f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13744   f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13745   f->x_named_label_uses = NULL;
13746   f->bindings = NULL;
13747   f->x_local_names = NULL;
13748
13749   /* When we get back here again, we will be expanding.  */
13750   f->x_expanding_p = 1;
13751
13752   /* If we've already decided that we cannot inline this function, we
13753      must remember that fact when we actually go to expand the
13754      function.  */
13755   if (current_function_cannot_inline)
13756     {
13757       f->cannot_inline = current_function_cannot_inline;
13758       DECL_INLINE (decl) = 0;
13759     }
13760 }
13761
13762 /* Add a note to mark the beginning of the main body of the constructor.
13763    This is used to set up the data structures for the cleanup regions for
13764    fully-constructed bases and members.  */
13765
13766 static void
13767 begin_constructor_body (void)
13768 {
13769 }
13770
13771 /* Add a note to mark the end of the main body of the constructor.  This is
13772    used to end the cleanup regions for fully-constructed bases and
13773    members.  */
13774
13775 static void
13776 finish_constructor_body (void)
13777 {
13778 }
13779
13780 /* Do all the processing for the beginning of a destructor; set up the
13781    vtable pointers and cleanups for bases and members.  */
13782
13783 static void
13784 begin_destructor_body (void)
13785 {
13786   tree if_stmt;
13787   tree compound_stmt;
13788
13789   /* If the dtor is empty, and we know there is not any possible
13790      way we could use any vtable entries, before they are possibly
13791      set by a base class dtor, we don't have to setup the vtables,
13792      as we know that any base class dtor will set up any vtables
13793      it needs.  We avoid MI, because one base class dtor can do a
13794      virtual dispatch to an overridden function that would need to
13795      have a non-related vtable set up, we cannot avoid setting up
13796      vtables in that case.  We could change this to see if there
13797      is just one vtable.
13798
13799      ??? In the destructor for a class, the vtables are set
13800      appropriately for that class.  There will be no non-related
13801      vtables.  jason 2001-12-11.  */
13802   if_stmt = begin_if_stmt ();
13803
13804   /* If it is not safe to avoid setting up the vtables, then
13805      someone will change the condition to be boolean_true_node.  
13806      (Actually, for now, we do not have code to set the condition
13807      appropriately, so we just assume that we always need to
13808      initialize the vtables.)  */
13809   finish_if_stmt_cond (boolean_true_node, if_stmt);
13810
13811   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13812
13813   /* Make all virtual function table pointers in non-virtual base
13814      classes point to CURRENT_CLASS_TYPE's virtual function
13815      tables.  */
13816   initialize_vtbl_ptrs (current_class_ptr);
13817
13818   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13819   finish_then_clause (if_stmt);
13820   finish_if_stmt ();
13821
13822   /* And insert cleanups for our bases and members so that they
13823      will be properly destroyed if we throw.  */
13824   push_base_cleanups ();
13825 }
13826
13827 /* At the end of every destructor we generate code to delete the object if
13828    necessary.  Do that now.  */
13829
13830 static void
13831 finish_destructor_body (void)
13832 {
13833   tree exprstmt;
13834
13835   /* Any return from a destructor will end up here; that way all base
13836      and member cleanups will be run when the function returns.  */
13837   add_stmt (build_stmt (LABEL_STMT, dtor_label));
13838
13839   /* In a virtual destructor, we must call delete.  */
13840   if (DECL_VIRTUAL_P (current_function_decl))
13841     {
13842       tree if_stmt;
13843       tree virtual_size = cxx_sizeof (current_class_type);
13844
13845       /* [class.dtor]
13846
13847       At the point of definition of a virtual destructor (including
13848       an implicit definition), non-placement operator delete shall
13849       be looked up in the scope of the destructor's class and if
13850       found shall be accessible and unambiguous.  */
13851       exprstmt = build_op_delete_call
13852         (DELETE_EXPR, current_class_ptr, virtual_size,
13853          LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13854
13855       if_stmt = begin_if_stmt ();
13856       finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13857                                   current_in_charge_parm,
13858                                   integer_one_node),
13859                            if_stmt);
13860       finish_expr_stmt (exprstmt);
13861       finish_then_clause (if_stmt);
13862       finish_if_stmt ();
13863     }
13864 }
13865
13866 /* Do the necessary processing for the beginning of a function body, which
13867    in this case includes member-initializers, but not the catch clauses of
13868    a function-try-block.  Currently, this means opening a binding level
13869    for the member-initializers (in a ctor) and member cleanups (in a dtor).
13870    In other functions, this isn't necessary, but it doesn't hurt.  */
13871
13872 tree
13873 begin_function_body (void)
13874 {
13875   tree stmt;
13876
13877   if (processing_template_decl)
13878     /* Do nothing now.  */;
13879   else
13880     /* Always keep the BLOCK node associated with the outermost pair of
13881        curly braces of a function.  These are needed for correct
13882        operation of dwarfout.c.  */
13883     keep_next_level (1);
13884
13885   stmt = begin_compound_stmt (0);
13886   COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13887
13888   if (processing_template_decl)
13889     /* Do nothing now.  */;
13890   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13891     begin_constructor_body ();
13892   else if (DECL_DESTRUCTOR_P (current_function_decl))
13893     begin_destructor_body ();
13894
13895   return stmt;
13896 }
13897
13898 /* Do the processing for the end of a function body.  Currently, this means
13899    closing out the cleanups for fully-constructed bases and members, and in
13900    the case of the destructor, deleting the object if desired.  Again, this
13901    is only meaningful for [cd]tors, since they are the only functions where
13902    there is a significant distinction between the main body and any
13903    function catch clauses.  Handling, say, main() return semantics here
13904    would be wrong, as flowing off the end of a function catch clause for
13905    main() would also need to return 0.  */
13906
13907 void
13908 finish_function_body (tree compstmt)
13909 {
13910   /* Close the block.  */
13911   finish_compound_stmt (0, compstmt);
13912
13913   if (processing_template_decl)
13914     /* Do nothing now.  */;
13915   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13916     finish_constructor_body ();
13917   else if (DECL_DESTRUCTOR_P (current_function_decl))
13918     finish_destructor_body ();
13919 }  
13920
13921 /* Finish up a function declaration and compile that function
13922    all the way to assembler language output.  The free the storage
13923    for the function definition.
13924
13925    FLAGS is a bitwise or of the following values:
13926      2 - INCLASS_INLINE
13927        We just finished processing the body of an in-class inline
13928        function definition.  (This processing will have taken place
13929        after the class definition is complete.)  */
13930
13931 tree
13932 finish_function (int flags)
13933 {
13934   register tree fndecl = current_function_decl;
13935   tree fntype, ctype = NULL_TREE;
13936   int inclass_inline = (flags & 2) != 0;
13937   int nested;
13938
13939   /* When we get some parse errors, we can end up without a
13940      current_function_decl, so cope.  */
13941   if (fndecl == NULL_TREE)
13942     return error_mark_node;
13943
13944   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13945       && DECL_VIRTUAL_P (fndecl)
13946       && !processing_template_decl)
13947     {
13948       tree fnclass = DECL_CONTEXT (fndecl);
13949       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13950         keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13951     }
13952
13953   nested = function_depth > 1;
13954   fntype = TREE_TYPE (fndecl);
13955
13956   /*  TREE_READONLY (fndecl) = 1;
13957       This caused &foo to be of type ptr-to-const-function
13958       which then got a warning when stored in a ptr-to-function variable.  */
13959
13960   my_friendly_assert (building_stmt_tree (), 20000911);
13961
13962   finish_fname_decls ();
13963   
13964   /* For a cloned function, we've already got all the code we need;
13965      there's no need to add any extra bits.  */
13966   if (!DECL_CLONED_FUNCTION_P (fndecl))
13967     {
13968       if (DECL_MAIN_P (current_function_decl))
13969         {
13970           /* Make it so that `main' always returns 0 by default.  */
13971 #if VMS_TARGET
13972           finish_return_stmt (integer_one_node);
13973 #else
13974           finish_return_stmt (integer_zero_node);
13975 #endif
13976         }
13977
13978       /* Finish dealing with exception specifiers.  */
13979       if (flag_exceptions && !processing_template_decl
13980           && flag_enforce_eh_specs
13981           && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13982         finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13983                               (TREE_TYPE (current_function_decl)),
13984                               current_eh_spec_block);
13985     }
13986
13987   /* If we're saving up tree structure, tie off the function now.  */
13988   finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
13989
13990   /* If this function can't throw any exceptions, remember that.  */
13991   if (!processing_template_decl
13992       && !cp_function_chain->can_throw
13993       && !flag_non_call_exceptions)
13994     TREE_NOTHROW (fndecl) = 1;
13995
13996   /* This must come after expand_function_end because cleanups might
13997      have declarations (from inline functions) that need to go into
13998      this function's blocks.  */
13999   
14000   /* If the current binding level isn't the outermost binding level
14001      for this function, either there is a bug, or we have experienced
14002      syntax errors and the statement tree is malformed.  */
14003   if (current_binding_level->parm_flag != 1)
14004     {
14005       /* Make sure we have already experienced errors.  */
14006       if (errorcount == 0)
14007         abort ();
14008
14009       /* Throw away the broken statement tree and extra binding
14010          levels.  */
14011       DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14012
14013       while (current_binding_level->parm_flag != 1)
14014         {
14015           if (current_binding_level->parm_flag == 2)
14016             pop_nested_class ();
14017           else
14018             poplevel (0, 0, 0);
14019         }
14020     }
14021   poplevel (1, 0, 1);
14022
14023   /* Set up the named return value optimization, if we can.  Here, we
14024      eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14025      for the nrv.  genrtl_start_function and declare_return_variable
14026      handle making the nrv and RESULT_DECL share space.  */
14027   if (current_function_return_value)
14028     {
14029       tree r = current_function_return_value;
14030       tree outer;
14031
14032       if (r != error_mark_node
14033           /* This is only worth doing for fns that return in memory--and
14034              simpler, since we don't have to worry about promoted modes.  */
14035           && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
14036           /* Only allow this for variables declared in the outer scope of
14037              the function so we know that their lifetime always ends with a
14038              return; see g++.dg/opt/nrv6.C.  We could be more flexible if
14039              we were to do this optimization in tree-ssa.  */
14040           /* Skip the artificial function body block.  */
14041           && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
14042               chain_member (r, BLOCK_VARS (outer))))
14043         {
14044           
14045           DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14046           walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14047                                         nullify_returns_r, r);
14048         }
14049       else
14050         /* Clear it so genrtl_start_function and declare_return_variable
14051            know we're not optimizing.  */
14052         current_function_return_value = NULL_TREE;
14053     }
14054
14055   /* Remember that we were in class scope.  */
14056   if (current_class_name)
14057     ctype = current_class_type;
14058
14059   /* Must mark the RESULT_DECL as being in this function.  */
14060   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14061
14062   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14063      to the FUNCTION_DECL node itself.  */
14064   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14065
14066   /* Save away current state, if appropriate.  */
14067   if (!processing_template_decl)
14068     save_function_data (fndecl);
14069
14070   /* If this function calls `setjmp' it cannot be inlined.  When
14071      `longjmp' is called it is not guaranteed to restore the value of
14072      local variables that have been modified since the call to
14073      `setjmp'.  So, if were to inline this function into some caller
14074      `c', then when we `longjmp', we might not restore all variables
14075      in `c'.  (It might seem, at first blush, that there's no way for
14076      this function to modify local variables in `c', but their
14077      addresses may have been stored somewhere accessible to this
14078      function.)  */
14079   if (!processing_template_decl && calls_setjmp_p (fndecl))
14080     DECL_UNINLINABLE (fndecl) = 1;
14081
14082   /* Complain if there's just no return statement.  */
14083   if (warn_return_type
14084       && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14085       && !current_function_returns_value && !current_function_returns_null
14086       /* Don't complain if we abort or throw.  */
14087       && !current_function_returns_abnormally
14088       && !DECL_NAME (DECL_RESULT (fndecl))
14089       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
14090          inline function, as we might never be compiled separately.  */
14091       && (DECL_INLINE (fndecl) || processing_template_decl))
14092     warning ("no return statement in function returning non-void");
14093     
14094   /* Clear out memory we no longer need.  */
14095   free_after_parsing (cfun);
14096   /* Since we never call rest_of_compilation, we never clear
14097      CFUN.  Do so explicitly.  */
14098   free_after_compilation (cfun);
14099   cfun = NULL;
14100
14101   /* If this is an in-class inline definition, we may have to pop the
14102      bindings for the template parameters that we added in
14103      maybe_begin_member_template_processing when start_function was
14104      called.  */
14105   if (inclass_inline)
14106     maybe_end_member_template_processing ();
14107
14108   /* Leave the scope of the class.  */
14109   if (ctype)
14110     pop_nested_class ();
14111
14112   --function_depth;
14113
14114   /* Clean up.  */
14115   if (! nested)
14116     /* Let the error reporting routines know that we're outside a
14117        function.  For a nested function, this value is used in
14118        cxx_pop_function_context and then reset via pop_function_context.  */
14119     current_function_decl = NULL_TREE;
14120
14121   return fndecl;
14122 }
14123 \f
14124 /* Create the FUNCTION_DECL for a function definition.
14125    DECLSPECS and DECLARATOR are the parts of the declaration;
14126    they describe the return type and the name of the function,
14127    but twisted together in a fashion that parallels the syntax of C.
14128
14129    This function creates a binding context for the function body
14130    as well as setting up the FUNCTION_DECL in current_function_decl.
14131
14132    Returns a FUNCTION_DECL on success.
14133
14134    If the DECLARATOR is not suitable for a function (it defines a datum
14135    instead), we return 0, which tells yyparse to report a parse error.
14136
14137    May return void_type_node indicating that this method is actually
14138    a friend.  See grokfield for more details.
14139
14140    Came here with a `.pushlevel' .
14141
14142    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14143    CHANGES TO CODE IN `grokfield'.  */
14144
14145 tree
14146 start_method (tree declspecs, tree declarator, tree attrlist)
14147 {
14148   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14149                                 &attrlist);
14150
14151   if (fndecl == error_mark_node)
14152     return error_mark_node;
14153
14154   if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14155     {
14156       error ("invalid member function declaration");
14157       return error_mark_node;
14158     }
14159
14160   if (attrlist)
14161     cplus_decl_attributes (&fndecl, attrlist, 0);
14162
14163   /* Pass friends other than inline friend functions back.  */
14164   if (fndecl == void_type_node)
14165     return fndecl;
14166
14167   if (DECL_IN_AGGR_P (fndecl))
14168     {
14169       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14170         {
14171           if (DECL_CONTEXT (fndecl)
14172               && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14173             error ("`%D' is already defined in class `%T'", fndecl,
14174                       DECL_CONTEXT (fndecl));
14175         }
14176       return void_type_node;
14177     }
14178
14179   check_template_shadow (fndecl);
14180
14181   DECL_DECLARED_INLINE_P (fndecl) = 1;
14182
14183   DID_INLINE_FUNC (fndecl) = 0;
14184   if (flag_default_inline)
14185     DECL_INLINE (fndecl) = 1;
14186
14187   /* We process method specializations in finish_struct_1.  */
14188   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14189     fndecl = push_template_decl (fndecl);
14190
14191   if (! DECL_FRIEND_P (fndecl))
14192     {
14193       if (TREE_CHAIN (fndecl))
14194         {
14195           fndecl = copy_node (fndecl);
14196           TREE_CHAIN (fndecl) = NULL_TREE;
14197         }
14198       grok_special_member_properties (fndecl);
14199     }
14200
14201   cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14202
14203   /* Make a place for the parms */
14204   pushlevel (0);
14205   current_binding_level->parm_flag = 1;
14206
14207   DECL_IN_AGGR_P (fndecl) = 1;
14208   return fndecl;
14209 }
14210
14211 /* Go through the motions of finishing a function definition.
14212    We don't compile this method until after the whole class has
14213    been processed.
14214
14215    FINISH_METHOD must return something that looks as though it
14216    came from GROKFIELD (since we are defining a method, after all).
14217
14218    This is called after parsing the body of the function definition.
14219    STMTS is the chain of statements that makes up the function body.
14220
14221    DECL is the ..._DECL that `start_method' provided.  */
14222
14223 tree
14224 finish_method (tree decl)
14225 {
14226   register tree fndecl = decl;
14227   tree old_initial;
14228
14229   register tree link;
14230
14231   if (decl == void_type_node)
14232     return decl;
14233
14234   old_initial = DECL_INITIAL (fndecl);
14235
14236   /* Undo the level for the parms (from start_method).
14237      This is like poplevel, but it causes nothing to be
14238      saved.  Saving information here confuses symbol-table
14239      output routines.  Besides, this information will
14240      be correctly output when this method is actually
14241      compiled.  */
14242
14243   /* Clear out the meanings of the local variables of this level;
14244      also record in each decl which block it belongs to.  */
14245
14246   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14247     {
14248       if (DECL_NAME (link) != NULL_TREE)
14249         pop_binding (DECL_NAME (link), link);
14250       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14251       DECL_CONTEXT (link) = NULL_TREE;
14252     }
14253
14254   poplevel (0, 0, 0);
14255
14256   DECL_INITIAL (fndecl) = old_initial;
14257
14258   /* We used to check if the context of FNDECL was different from
14259      current_class_type as another way to get inside here.  This didn't work
14260      for String.cc in libg++.  */
14261   if (DECL_FRIEND_P (fndecl))
14262     {
14263       CLASSTYPE_INLINE_FRIENDS (current_class_type)
14264         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14265       decl = void_type_node;
14266     }
14267
14268   return decl;
14269 }
14270 \f
14271
14272 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
14273    we can lay it out later, when and if its type becomes complete.  */
14274
14275 void
14276 maybe_register_incomplete_var (tree var)
14277 {
14278   my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14279
14280   /* Keep track of variables with incomplete types.  */
14281   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 
14282       && DECL_EXTERNAL (var))
14283     {
14284       tree inner_type = TREE_TYPE (var);
14285       
14286       while (TREE_CODE (inner_type) == ARRAY_TYPE)
14287         inner_type = TREE_TYPE (inner_type);
14288       inner_type = TYPE_MAIN_VARIANT (inner_type);
14289       
14290       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14291           /* RTTI TD entries are created while defining the type_info.  */
14292           || (TYPE_LANG_SPECIFIC (inner_type)
14293               && TYPE_BEING_DEFINED (inner_type)))
14294         incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14295     }
14296 }
14297
14298 /* Called when a class type (given by TYPE) is defined.  If there are
14299    any existing VAR_DECLs whose type hsa been completed by this
14300    declaration, update them now.  */
14301
14302 void
14303 complete_vars (tree type)
14304 {
14305   tree *list = &incomplete_vars;
14306
14307   my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14308   while (*list) 
14309     {
14310       if (same_type_p (type, TREE_PURPOSE (*list)))
14311         {
14312           tree var = TREE_VALUE (*list);
14313           /* Complete the type of the variable.  The VAR_DECL itself
14314              will be laid out in expand_expr.  */
14315           complete_type (TREE_TYPE (var));
14316           /* Remove this entry from the list.  */
14317           *list = TREE_CHAIN (*list);
14318         }
14319       else
14320         list = &TREE_CHAIN (*list);
14321     }
14322 }
14323
14324 /* If DECL is of a type which needs a cleanup, build that cleanup
14325    here.  */
14326
14327 tree
14328 cxx_maybe_build_cleanup (tree decl)
14329 {
14330   tree type = TREE_TYPE (decl);
14331
14332   if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14333     {
14334       int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14335       tree rval;
14336
14337       if (TREE_CODE (type) == ARRAY_TYPE)
14338         rval = decl;
14339       else
14340         {
14341           cxx_mark_addressable (decl);
14342           rval = build_unary_op (ADDR_EXPR, decl, 0);
14343         }
14344
14345       /* Optimize for space over speed here.  */
14346       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14347           || flag_expensive_optimizations)
14348         flags |= LOOKUP_NONVIRTUAL;
14349
14350       rval = build_delete (TREE_TYPE (rval), rval,
14351                            sfk_complete_destructor, flags, 0);
14352
14353       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14354           && ! TYPE_HAS_DESTRUCTOR (type))
14355         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14356                                                build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14357
14358       return rval;
14359     }
14360   return NULL_TREE;
14361 }
14362 \f
14363 /* When a stmt has been parsed, this function is called.  */
14364
14365 void
14366 finish_stmt (void)
14367 {
14368   /* Always assume this statement was not an expression statement.  If
14369      it actually was an expression statement, its our callers
14370      responsibility to fix this up.  */
14371   last_expr_type = NULL_TREE;
14372 }
14373
14374 /* DECL was originally constructed as a non-static member function,
14375    but turned out to be static.  Update it accordingly.  */
14376
14377 void
14378 revert_static_member_fn (tree decl)
14379 {
14380   tree tmp;
14381   tree function = TREE_TYPE (decl);
14382   tree args = TYPE_ARG_TYPES (function);
14383
14384   if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14385       != TYPE_UNQUALIFIED)
14386     error ("static member function `%#D' declared with type qualifiers",
14387               decl);
14388
14389   args = TREE_CHAIN (args);
14390   tmp = build_function_type (TREE_TYPE (function), args);
14391   tmp = build_qualified_type (tmp, cp_type_quals (function));
14392   tmp = build_exception_variant (tmp,
14393                                  TYPE_RAISES_EXCEPTIONS (function));
14394   TREE_TYPE (decl) = tmp;
14395   if (DECL_ARGUMENTS (decl))
14396     DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14397   DECL_STATIC_FUNCTION_P (decl) = 1;
14398 }
14399
14400 /* Initialize the variables used during compilation of a C++
14401    function.  */
14402
14403 void
14404 cxx_push_function_context (struct function * f)
14405 {
14406   struct language_function *p
14407     = ((struct language_function *)
14408        ggc_alloc_cleared (sizeof (struct language_function)));
14409   f->language = p;
14410
14411   /* It takes an explicit call to expand_body to generate RTL for a
14412      function.  */
14413   expanding_p = 0;
14414
14415   /* Whenever we start a new function, we destroy temporaries in the
14416      usual way.  */
14417   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14418 }
14419
14420 /* Free the language-specific parts of F, now that we've finished
14421    compiling the function.  */
14422
14423 void
14424 cxx_pop_function_context (struct function * f)
14425 {
14426   f->language = 0;
14427 }
14428
14429 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14430    one of the language-independent trees.  */
14431
14432 enum cp_tree_node_structure_enum
14433 cp_tree_node_structure (union lang_tree_node * t)
14434 {
14435   switch (TREE_CODE (&t->generic))
14436     {
14437     case DEFAULT_ARG:           return TS_CP_DEFAULT_ARG;
14438     case IDENTIFIER_NODE:       return TS_CP_IDENTIFIER;
14439     case OVERLOAD:              return TS_CP_OVERLOAD;
14440     case TEMPLATE_PARM_INDEX:   return TS_CP_TPI;
14441     case PTRMEM_CST:            return TS_CP_PTRMEM;
14442     case BASELINK:              return TS_CP_BASELINK;
14443     case WRAPPER:               return TS_CP_WRAPPER;
14444     default:                    return TS_CP_GENERIC;
14445     }
14446 }
14447
14448 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14449    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
14450
14451 tree
14452 identifier_global_value (tree t)
14453 {
14454   return IDENTIFIER_GLOBAL_VALUE (t);
14455 }
14456
14457 /* Build the void_list_node (void_type_node having been created).  */
14458 tree
14459 build_void_list_node (void)
14460 {
14461   tree t = build_tree_list (NULL_TREE, void_type_node);
14462   TREE_PARMLIST (t) = 1;
14463   return t;
14464 }
14465
14466 static int
14467 cp_missing_noreturn_ok_p (tree decl)
14468 {
14469   /* A missing noreturn is ok for the `main' function.  */
14470   return DECL_MAIN_P (decl);
14471 }
14472
14473 #include "gt-cp-decl.h"
14474 #include "gtype-cp.h"