OSDN Git Service

PR c++/9167, c++/9358
[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
54 static tree grokparms (tree);
55 static const char *redeclaration_error_message (tree, tree);
56
57 static void push_binding_level (struct cp_binding_level *, int,
58                                 int);
59 static void pop_binding_level (void);
60 static void suspend_binding_level (void);
61 static void resume_binding_level (struct cp_binding_level *);
62 static struct cp_binding_level *make_binding_level (void);
63 static void declare_namespace_level (void);
64 static int decl_jump_unsafe (tree);
65 static void storedecls (tree);
66 static void require_complete_types_for_parms (tree);
67 static int ambi_op_p (enum tree_code);
68 static int unary_op_p (enum tree_code);
69 static tree store_bindings (tree, tree);
70 static tree lookup_tag_reverse (tree, tree);
71 static void push_local_name (tree);
72 static void warn_extern_redeclared_static (tree, tree);
73 static tree grok_reference_init (tree, tree, tree);
74 static tree grokfndecl (tree, tree, tree, tree, int,
75                         enum overload_flags, tree,
76                         tree, int, int, int, int, int, int, tree);
77 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
78 static tree follow_tag_typedef (tree);
79 static tree lookup_tag (enum tree_code, tree,
80                         struct cp_binding_level *, int);
81 static void set_identifier_type_value_with_scope
82         (tree, tree, struct cp_binding_level *);
83 static void record_unknown_type (tree, const char *);
84 static tree builtin_function_1 (const char *, tree, tree, int,
85                                 enum built_in_class, const char *,
86                                 tree);
87 static tree build_library_fn_1 (tree, enum tree_code, tree);
88 static int member_function_or_else (tree, tree, enum overload_flags);
89 static void bad_specifiers (tree, const char *, int, int, int, int,
90                             int);
91 static tree maybe_process_template_type_declaration 
92         (tree, int, struct cp_binding_level*);
93 static void check_for_uninitialized_const_var (tree);
94 static hashval_t typename_hash (const void *);
95 static int typename_compare (const void *, const void *);
96 static void push_binding (tree, tree, struct cp_binding_level*);
97 static int add_binding (tree, tree);
98 static void pop_binding (tree, tree);
99 static tree local_variable_p_walkfn (tree *, int *, void *);
100 static tree find_binding (tree, tree);
101 static tree select_decl (tree, 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 void warn_about_implicit_typename_lookup (tree, tree);
109 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
110 static int walk_globals_r (tree, void*);
111 static int walk_vtables_r (tree, void*);
112 static void add_decl_to_level (tree, struct cp_binding_level *);
113 static tree make_label_decl (tree, int);
114 static void use_label (tree);
115 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
116                                    const char *, int);
117 static void check_previous_goto (struct named_label_use_list *);
118 static void check_switch_goto (struct cp_binding_level *);
119 static void check_previous_gotos (tree);
120 static void pop_label (tree, tree);
121 static void pop_labels (tree);
122 static void maybe_deduce_size_from_array_init (tree, tree);
123 static void layout_var_decl (tree);
124 static void maybe_commonize_var (tree);
125 static tree check_initializer (tree, tree, int);
126 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
127 static void save_function_data (tree);
128 static void check_function_type (tree, tree);
129 static void begin_constructor_body (void);
130 static void finish_constructor_body (void);
131 static void begin_destructor_body (void);
132 static void finish_destructor_body (void);
133 static tree create_array_type_for_decl (tree, tree, tree);
134 static tree get_atexit_node (void);
135 static tree get_dso_handle_node (void);
136 static tree start_cleanup_fn (void);
137 static void end_cleanup_fn (void);
138 static tree cp_make_fname_decl (tree, int);
139 static void initialize_predefined_identifiers (void);
140 static tree check_special_function_return_type 
141         (special_function_kind, tree, tree);
142 static tree push_cp_library_fn (enum tree_code, tree);
143 static tree build_cp_library_fn (tree, enum tree_code, tree);
144 static void store_parm_decls (tree);
145 static int cp_missing_noreturn_ok_p (tree);
146 static void initialize_local_var (tree, tree);
147 static void expand_static_init (tree, tree);
148 static tree next_initializable_field (tree);
149 static tree reshape_init (tree, tree *);
150 static tree build_typename_type (tree, tree, tree);
151
152 #if defined (DEBUG_BINDING_LEVELS)
153 static void indent (void);
154 #endif
155
156 /* Erroneous argument lists can use this *IFF* they do not modify it.  */
157 tree error_mark_list;
158
159 /* The following symbols are subsumed in the cp_global_trees array, and
160    listed here individually for documentation purposes.
161
162    C++ extensions
163         tree wchar_decl_node;
164
165         tree vtable_entry_type;
166         tree delta_type_node;
167         tree __t_desc_type_node;
168         tree ti_desc_type_node;
169         tree bltn_desc_type_node, ptr_desc_type_node;
170         tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
171         tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
172         tree ptm_desc_type_node;
173         tree base_desc_type_node;
174
175         tree class_type_node, record_type_node, union_type_node, enum_type_node;
176         tree unknown_type_node;
177
178    Array type `vtable_entry_type[]'
179
180         tree vtbl_type_node;
181         tree vtbl_ptr_type_node;
182
183    Namespaces,
184
185         tree std_node;
186         tree abi_node;
187
188    A FUNCTION_DECL which can call `abort'.  Not necessarily the
189    one that the user will declare, but sufficient to be called
190    by routines that want to abort the program.
191
192         tree abort_fndecl;
193
194    The FUNCTION_DECL for the default `::operator delete'.
195
196         tree global_delete_fndecl;
197
198    Used by RTTI
199         tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
200         tree tinfo_var_id;
201
202 */
203
204 tree cp_global_trees[CPTI_MAX];
205
206 /* Indicates that there is a type value in some namespace, although
207    that is not necessarily in scope at the moment.  */
208
209 static GTY(()) tree global_type_node;
210
211 /* Used only for jumps to as-yet undefined labels, since jumps to
212    defined labels can have their validity checked immediately.  */
213
214 struct named_label_use_list GTY(())
215 {
216   struct cp_binding_level *binding_level;
217   tree names_in_scope;
218   tree label_decl;
219   const char *filename_o_goto;
220   int lineno_o_goto;
221   struct named_label_use_list *next;
222 };
223
224 #define named_label_uses cp_function_chain->x_named_label_uses
225
226 #define local_names cp_function_chain->x_local_names
227
228 /* A list of objects which have constructors or destructors
229    which reside in the global scope.  The decl is stored in
230    the TREE_VALUE slot and the initializer is stored
231    in the TREE_PURPOSE slot.  */
232 tree static_aggregates;
233
234 /* -- end of C++ */
235
236 /* A node for the integer constants 2, and 3.  */
237
238 tree integer_two_node, integer_three_node;
239
240 /* Similar, for last_function_parm_tags.  */
241 tree last_function_parms;
242
243 /* A list of all LABEL_DECLs in the function that have names.  Here so
244    we can clear out their names' definitions at the end of the
245    function, and so we can check the validity of jumps to these labels.  */
246
247 struct named_label_list GTY(())
248 {
249   struct cp_binding_level *binding_level;
250   tree names_in_scope;
251   tree old_value;
252   tree label_decl;
253   tree bad_decls;
254   struct named_label_list *next;
255   unsigned int in_try_scope : 1;
256   unsigned int in_catch_scope : 1;
257 };
258
259 #define named_labels cp_function_chain->x_named_labels
260 \f
261 /* The name of the anonymous namespace, throughout this translation
262    unit.  */
263 tree anonymous_namespace_name;
264
265 /* The number of function bodies which we are currently processing.
266    (Zero if we are at namespace scope, one inside the body of a
267    function, two inside the body of a function in a local class, etc.)  */
268 int function_depth;
269
270 /* States indicating how grokdeclarator() should handle declspecs marked
271    with __attribute__((deprecated)).  An object declared as
272    __attribute__((deprecated)) suppresses warnings of uses of other
273    deprecated items.  */
274    
275 enum deprecated_states {
276   DEPRECATED_NORMAL,
277   DEPRECATED_SUPPRESS
278 };
279
280 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
281
282 /* Set by add_implicitly_declared_members() to keep those members from
283    being flagged as deprecated or reported as using deprecated
284    types.  */
285 int adding_implicit_members = 0;
286
287 /* True if a declaration with an `extern' linkage specifier is being
288    processed.  */
289 bool have_extern_spec;
290
291 \f
292 /* For each binding contour we allocate a binding_level structure
293    which records the names defined in that contour.
294    Contours include:
295     0) the global one
296     1) one for each function definition,
297        where internal declarations of the parameters appear.
298     2) one for each compound statement,
299        to record its declarations.
300
301    The current meaning of a name can be found by searching the levels
302    from the current one out to the global one.
303
304    Off to the side, may be the class_binding_level.  This exists only
305    to catch class-local declarations.  It is otherwise nonexistent.
306
307    Also there may be binding levels that catch cleanups that must be
308    run when exceptions occur.  Thus, to see whether a name is bound in
309    the current scope, it is not enough to look in the
310    CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
311    instead.  */
312
313 /* Note that the information in the `names' component of the global contour
314    is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
315
316 struct cp_binding_level GTY(())
317   {
318     /* A chain of _DECL nodes for all variables, constants, functions,
319        and typedef types.  These are in the reverse of the order
320        supplied.  There may be OVERLOADs on this list, too, but they
321        are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
322     tree names;
323
324     /* Count of elements in names chain.  */
325     size_t names_size;
326
327     /* A chain of NAMESPACE_DECL nodes.  */
328     tree namespaces;
329
330     /* A chain of VTABLE_DECL nodes.  */
331     tree vtables; 
332
333     /* A list of structure, union and enum definitions, for looking up
334        tag names.
335        It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
336        or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
337        or ENUMERAL_TYPE node.
338
339        C++: the TREE_VALUE nodes can be simple types for
340        component_bindings.  */
341     tree tags;
342
343     /* A list of USING_DECL nodes.  */
344     tree usings;
345
346     /* A list of used namespaces. PURPOSE is the namespace,
347        VALUE the common ancestor with this binding_level's namespace.  */
348     tree using_directives;
349
350     /* If this binding level is the binding level for a class, then
351        class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
352        is the name of an entity bound in the class.  The TREE_TYPE is
353        the DECL bound by this name in the class.  */
354     tree class_shadowed;
355
356     /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
357        is used for all binding levels. In addition the TREE_VALUE is the
358        IDENTIFIER_TYPE_VALUE before we entered the class.  */
359     tree type_shadowed;
360
361     /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
362        label in this scope.  The TREE_PURPOSE is the previous value of
363        the IDENTIFIER_LABEL VALUE.  */
364     tree shadowed_labels;
365
366     /* For each level (except not the global one),
367        a chain of BLOCK nodes for all the levels
368        that were entered and exited one level down.  */
369     tree blocks;
370
371     /* The _TYPE node for this level, if parm_flag == 2.  */
372     tree this_class;
373
374     /* The binding level which this one is contained in (inherits from).  */
375     struct cp_binding_level *level_chain;
376
377     /* List of VAR_DECLS saved from a previous for statement.
378        These would be dead in ISO-conforming code, but might
379        be referenced in ARM-era code.  These are stored in a
380        TREE_LIST; the TREE_VALUE is the actual declaration.  */
381     tree dead_vars_from_for;
382
383     /* 1 for the level that holds the parameters of a function.
384        2 for the level that holds a class declaration.  */
385     unsigned parm_flag : 2;
386
387     /* 1 means make a BLOCK for this level regardless of all else.
388        2 for temporary binding contours created by the compiler.  */
389     unsigned keep : 2;
390
391     /* Nonzero if this level "doesn't exist" for tags.  */
392     unsigned tag_transparent : 1;
393
394     /* Nonzero if this level can safely have additional
395        cleanup-needing variables added to it.  */
396     unsigned more_cleanups_ok : 1;
397     unsigned have_cleanups : 1;
398
399     /* Nonzero if this scope is for storing the decls for template
400        parameters and generic decls; these decls will be discarded and
401        replaced with a TEMPLATE_DECL.  */
402     unsigned template_parms_p : 1;
403
404     /* Nonzero if this scope corresponds to the `<>' in a
405        `template <>' clause.  Whenever this flag is set,
406        TEMPLATE_PARMS_P will be set as well.  */
407     unsigned template_spec_p : 1;
408
409     /* This is set for a namespace binding level.  */
410     unsigned namespace_p : 1;
411
412     /* True if this level is that of a for-statement where we need to
413        worry about ambiguous (ARM or ISO) scope rules.  */
414     unsigned is_for_scope : 1;
415
416     /* True if this level corresponds to a TRY block.  Currently this
417        information is only available while building the tree structure.  */
418     unsigned is_try_scope : 1;
419
420     /* True if this level corresponds to a CATCH block.  Currently this
421        information is only available while building the tree structure.  */
422     unsigned is_catch_scope : 1;
423
424     /* Three bits left for this word.  */
425
426     /* Binding depth at which this level began.  */
427     unsigned binding_depth;
428   };
429
430 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
431
432 /* The binding level currently in effect.  */
433
434 #define current_binding_level                   \
435   (cfun && cp_function_chain->bindings          \
436    ? cp_function_chain->bindings                \
437    : scope_chain->bindings)
438
439 /* The binding level of the current class, if any.  */
440
441 #define class_binding_level scope_chain->class_bindings
442
443 /* A chain of binding_level structures awaiting reuse.  */
444
445 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
446
447 /* The outermost binding level, for names of file scope.
448    This is created when the compiler is started and exists
449    through the entire run.  */
450
451 static GTY(()) struct cp_binding_level *global_binding_level;
452
453 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
454
455 static int keep_next_level_flag;
456
457 /* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
458    UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
459    time the VAR_DECL was declared, the type was incomplete.  */
460
461 static GTY(()) tree incomplete_vars;
462
463 #if defined(DEBUG_BINDING_LEVELS)
464 static int binding_depth = 0;
465 static int is_class_level = 0;
466
467 static void
468 indent (void)
469 {
470   register unsigned i;
471
472   for (i = 0; i < binding_depth*2; i++)
473     putc (' ', stderr);
474 }
475 #endif /* defined(DEBUG_BINDING_LEVELS) */
476
477 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
478
479 static void
480 push_binding_level (struct cp_binding_level *newlevel, 
481                     int tag_transparent, 
482                     int keep)
483 {
484   /* Add this level to the front of the chain (stack) of levels that
485      are active.  */
486   memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
487   newlevel->level_chain = current_binding_level;
488   current_binding_level = newlevel;
489   newlevel->tag_transparent = tag_transparent;
490   newlevel->more_cleanups_ok = 1;
491
492   newlevel->keep = keep;
493 #if defined(DEBUG_BINDING_LEVELS)
494   newlevel->binding_depth = binding_depth;
495   indent ();
496   fprintf (stderr, "push %s level 0x%08x line %d\n",
497            (is_class_level) ? "class" : "block", newlevel, lineno);
498   is_class_level = 0;
499   binding_depth++;
500 #endif /* defined(DEBUG_BINDING_LEVELS) */
501 }
502
503 /* Find the innermost enclosing class scope, and reset
504    CLASS_BINDING_LEVEL appropriately.  */
505
506 static void
507 find_class_binding_level (void)
508 {
509   struct cp_binding_level *level = current_binding_level;
510
511   while (level && level->parm_flag != 2)
512     level = level->level_chain;
513   if (level && level->parm_flag == 2)
514     class_binding_level = level;
515   else
516     class_binding_level = 0;
517 }
518
519 static void
520 pop_binding_level (void)
521 {
522   if (global_binding_level)
523     {
524       /* Cannot pop a level, if there are none left to pop.  */
525       if (current_binding_level == global_binding_level)
526         abort ();
527     }
528   /* Pop the current level, and free the structure for reuse.  */
529 #if defined(DEBUG_BINDING_LEVELS)
530   binding_depth--;
531   indent ();
532   fprintf (stderr, "pop  %s level 0x%08x line %d\n",
533           (is_class_level) ? "class" : "block",
534           current_binding_level, lineno);
535   if (is_class_level != (current_binding_level == class_binding_level))
536     {
537       indent ();
538       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
539     }
540   is_class_level = 0;
541 #endif /* defined(DEBUG_BINDING_LEVELS) */
542   {
543     register struct cp_binding_level *level = current_binding_level;
544     current_binding_level = current_binding_level->level_chain;
545     level->level_chain = free_binding_level;
546 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
547     if (level->binding_depth != binding_depth)
548       abort ();
549 #endif /* defined(DEBUG_BINDING_LEVELS) */
550     free_binding_level = level;
551     find_class_binding_level ();
552   }
553 }
554
555 static void
556 suspend_binding_level (void)
557 {
558   if (class_binding_level)
559     current_binding_level = class_binding_level;
560
561   if (global_binding_level)
562     {
563       /* Cannot suspend a level, if there are none left to suspend.  */
564       if (current_binding_level == global_binding_level)
565         abort ();
566     }
567   /* Suspend the current level.  */
568 #if defined(DEBUG_BINDING_LEVELS)
569   binding_depth--;
570   indent ();
571   fprintf (stderr, "suspend  %s level 0x%08x line %d\n",
572           (is_class_level) ? "class" : "block",
573           current_binding_level, lineno);
574   if (is_class_level != (current_binding_level == class_binding_level))
575     {
576       indent ();
577       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
578     }
579   is_class_level = 0;
580 #endif /* defined(DEBUG_BINDING_LEVELS) */
581   current_binding_level = current_binding_level->level_chain;
582   find_class_binding_level ();
583 }
584
585 static void
586 resume_binding_level (struct cp_binding_level* b)
587 {
588   /* Resuming binding levels is meant only for namespaces,
589      and those cannot nest into classes.  */
590   my_friendly_assert(!class_binding_level, 386);
591   /* Also, resuming a non-directly nested namespace is a no-no.  */
592   my_friendly_assert(b->level_chain == current_binding_level, 386);
593   current_binding_level = b;
594 #if defined(DEBUG_BINDING_LEVELS)
595   b->binding_depth = binding_depth;
596   indent ();
597   fprintf (stderr, "resume %s level 0x%08x line %d\n",
598            (is_class_level) ? "class" : "block", b, lineno);
599   is_class_level = 0;
600   binding_depth++;
601 #endif /* defined(DEBUG_BINDING_LEVELS) */
602 }
603 \f
604 /* Create a new `struct cp_binding_level'.  */
605
606 static
607 struct cp_binding_level *
608 make_binding_level (void)
609 {
610   /* NOSTRICT */
611   return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
612 }
613
614 /* Nonzero if we are currently in the global binding level.  */
615
616 int
617 global_bindings_p (void)
618 {
619   return current_binding_level == global_binding_level;
620 }
621
622 /* Return the innermost binding level that is not for a class scope.  */
623
624 static struct cp_binding_level *
625 innermost_nonclass_level (void)
626 {
627   struct cp_binding_level *b;
628
629   b = current_binding_level;
630   while (b->parm_flag == 2)
631     b = b->level_chain;
632
633   return b;
634 }
635
636 /* Nonzero if we are currently in a toplevel binding level.  This
637    means either the global binding level or a namespace in a toplevel
638    binding level.  Since there are no non-toplevel namespace levels,
639    this really means any namespace or template parameter level.  We
640    also include a class whose context is toplevel.  */
641
642 int
643 toplevel_bindings_p (void)
644 {
645   struct cp_binding_level *b = innermost_nonclass_level ();
646
647   return b->namespace_p || b->template_parms_p;
648 }
649
650 /* Nonzero if this is a namespace scope, or if we are defining a class
651    which is itself at namespace scope, or whose enclosing class is
652    such a class, etc.  */
653
654 int
655 namespace_bindings_p (void)
656 {
657   struct cp_binding_level *b = innermost_nonclass_level ();
658
659   return b->namespace_p;
660 }
661
662 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
663    unconditionally.  Otherwise, use the normal logic to decide whether
664    or not to create a BLOCK.  */
665
666 void
667 keep_next_level (int keep)
668 {
669   keep_next_level_flag = keep;
670 }
671
672 /* Nonzero if the current level needs to have a BLOCK made.  */
673
674 int
675 kept_level_p (void)
676 {
677   return (current_binding_level->blocks != NULL_TREE
678           || current_binding_level->keep
679           || current_binding_level->names != NULL_TREE
680           || (current_binding_level->tags != NULL_TREE
681               && !current_binding_level->tag_transparent));
682 }
683
684 static void
685 declare_namespace_level (void)
686 {
687   current_binding_level->namespace_p = 1;
688 }
689
690 /* Returns nonzero if this scope was created to store template
691    parameters.  */
692
693 int
694 template_parm_scope_p (void)
695 {
696   return current_binding_level->template_parms_p;
697 }
698
699 /* Returns the kind of template specialization we are currently
700    processing, given that it's declaration contained N_CLASS_SCOPES
701    explicit scope qualifications.  */
702
703 tmpl_spec_kind
704 current_tmpl_spec_kind (int n_class_scopes)
705 {
706   int n_template_parm_scopes = 0;
707   int seen_specialization_p = 0;
708   int innermost_specialization_p = 0;
709   struct cp_binding_level *b;
710
711   /* Scan through the template parameter scopes.  */
712   for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
713     {
714       /* If we see a specialization scope inside a parameter scope,
715          then something is wrong.  That corresponds to a declaration
716          like:
717
718             template <class T> template <> ...
719
720          which is always invalid since [temp.expl.spec] forbids the
721          specialization of a class member template if the enclosing
722          class templates are not explicitly specialized as well.  */
723       if (b->template_spec_p)
724         {
725           if (n_template_parm_scopes == 0)
726             innermost_specialization_p = 1;
727           else
728             seen_specialization_p = 1;
729         }
730       else if (seen_specialization_p == 1)
731         return tsk_invalid_member_spec;
732
733       ++n_template_parm_scopes;
734     }
735
736   /* Handle explicit instantiations.  */
737   if (processing_explicit_instantiation)
738     {
739       if (n_template_parm_scopes != 0)
740         /* We've seen a template parameter list during an explicit
741            instantiation.  For example:
742
743              template <class T> template void f(int);
744
745            This is erroneous.  */
746         return tsk_invalid_expl_inst;
747       else
748         return tsk_expl_inst;
749     }
750
751   if (n_template_parm_scopes < n_class_scopes)
752     /* We've not seen enough template headers to match all the
753        specialized classes present.  For example:
754
755          template <class T> void R<T>::S<T>::f(int);
756
757        This is invalid; there needs to be one set of template
758        parameters for each class.  */
759     return tsk_insufficient_parms;
760   else if (n_template_parm_scopes == n_class_scopes)
761     /* We're processing a non-template declaration (even though it may
762        be a member of a template class.)  For example:
763
764          template <class T> void S<T>::f(int);
765
766        The `class T' maches the `S<T>', leaving no template headers
767        corresponding to the `f'.  */
768     return tsk_none;
769   else if (n_template_parm_scopes > n_class_scopes + 1)
770     /* We've got too many template headers.  For example:
771
772          template <> template <class T> void f (T);
773
774        There need to be more enclosing classes.  */
775     return tsk_excessive_parms;
776   else
777     /* This must be a template.  It's of the form:
778
779          template <class T> template <class U> void S<T>::f(U);
780
781        This is a specialization if the innermost level was a
782        specialization; otherwise it's just a definition of the
783        template.  */
784     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
785 }
786
787 void
788 set_class_shadows (tree shadows)
789 {
790   class_binding_level->class_shadowed = shadows;
791 }
792
793 /* Enter a new binding level.
794    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
795    not for that of tags.  */
796
797 void
798 pushlevel (int tag_transparent)
799 {
800   struct cp_binding_level *newlevel;
801
802   if (cfun && !doing_semantic_analysis_p ())
803     return;
804
805   /* Reuse or create a struct for this binding level.  */
806 #if defined(DEBUG_BINDING_LEVELS)
807   if (0)
808 #else /* !defined(DEBUG_BINDING_LEVELS) */
809   if (free_binding_level)
810 #endif /* !defined(DEBUG_BINDING_LEVELS) */
811     {
812       newlevel = free_binding_level;
813       free_binding_level = free_binding_level->level_chain;
814     }
815   else
816     newlevel = make_binding_level ();
817
818   push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
819   keep_next_level_flag = 0;
820 }
821
822 /* We're defining an object of type TYPE.  If it needs a cleanup, but
823    we're not allowed to add any more objects with cleanups to the current
824    scope, create a new binding level.  */
825
826 void
827 maybe_push_cleanup_level (tree type)
828 {
829   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
830       && current_binding_level->more_cleanups_ok == 0)
831     {
832       keep_next_level (2);
833       pushlevel (1);
834       clear_last_expr ();
835       add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
836     }
837 }
838   
839 /* Enter a new scope.  The KIND indicates what kind of scope is being
840    created.  */
841
842 void
843 begin_scope (scope_kind sk)
844 {
845   pushlevel (0);
846
847   switch (sk)
848     {
849     case sk_block:
850       break;
851
852     case sk_try:
853       current_binding_level->is_try_scope = 1;
854       break;
855
856     case sk_catch:
857       current_binding_level->is_catch_scope = 1;
858       break;
859
860     case sk_for:
861       current_binding_level->is_for_scope = 1;
862       break;
863
864     case sk_template_spec:
865       current_binding_level->template_spec_p = 1;
866       /* Fall through.  */
867
868     case sk_template_parms:
869       current_binding_level->template_parms_p = 1;
870       break;
871
872     default:
873       abort ();
874     }
875 }
876
877 /* Exit the current scope.  */
878
879 void
880 finish_scope (void)
881 {
882   poplevel (0, 0, 0);
883 }
884
885 /* For a binding between a name and an entity at a block scope,
886    this is the `struct cp_binding_level' for the block.  */
887 #define BINDING_LEVEL(NODE) \
888   (((struct tree_binding*)(NODE))->scope.level)
889
890 /* A free list of CPLUS_BINDING nodes, connected by their
891    TREE_CHAINs.  */
892
893 static GTY((deletable (""))) tree free_bindings;
894
895 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
896    level at which this declaration is being bound.  */
897
898 static void
899 push_binding (tree id, 
900               tree decl,
901               struct cp_binding_level* level)
902 {
903   tree binding;
904
905   if (free_bindings)
906     {
907       binding = free_bindings;
908       free_bindings = TREE_CHAIN (binding);
909     }
910   else
911     binding = make_node (CPLUS_BINDING);
912
913   /* Now, fill in the binding information.  */
914   BINDING_VALUE (binding) = decl;
915   BINDING_TYPE (binding) = NULL_TREE;
916   BINDING_LEVEL (binding) = level;
917   INHERITED_VALUE_BINDING_P (binding) = 0;
918   LOCAL_BINDING_P (binding) = (level != class_binding_level);
919   BINDING_HAS_LEVEL_P (binding) = 1;
920
921   /* And put it on the front of the list of bindings for ID.  */
922   TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
923   IDENTIFIER_BINDING (id) = binding;
924 }
925
926 /* ID is already bound in the current scope.  But, DECL is an
927    additional binding for ID in the same scope.  This is the `struct
928    stat' hack whereby a non-typedef class-name or enum-name can be
929    bound at the same level as some other kind of entity.  It's the
930    responsibility of the caller to check that inserting this name is
931    valid here.  Returns nonzero if the new binding was successful.  */
932 static int
933 add_binding (tree id, tree decl)
934 {
935   tree binding = IDENTIFIER_BINDING (id);
936   int ok = 1;
937
938   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
939     /* The new name is the type name.  */
940     BINDING_TYPE (binding) = decl;
941   else if (!BINDING_VALUE (binding))
942     /* This situation arises when push_class_level_binding moves an
943        inherited type-binding out of the way to make room for a new
944        value binding.  */
945     BINDING_VALUE (binding) = decl;
946   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
947            && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
948     {
949       /* The old binding was a type name.  It was placed in
950          BINDING_VALUE because it was thought, at the point it was
951          declared, to be the only entity with such a name.  Move the
952          type name into the type slot; it is now hidden by the new
953          binding.  */
954       BINDING_TYPE (binding) = BINDING_VALUE (binding);
955       BINDING_VALUE (binding) = decl;
956       INHERITED_VALUE_BINDING_P (binding) = 0;
957     }
958   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
959            && TREE_CODE (decl) == TYPE_DECL
960            && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
961            && (same_type_p (TREE_TYPE (decl),
962                             TREE_TYPE (BINDING_VALUE (binding)))
963                /* If either type involves template parameters, we must
964                   wait until instantiation.  */
965                || uses_template_parms (TREE_TYPE (decl))
966                || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
967     /* We have two typedef-names, both naming the same type to have
968        the same name.  This is OK because of:
969
970          [dcl.typedef]
971
972          In a given scope, a typedef specifier can be used to redefine
973          the name of any type declared in that scope to refer to the
974          type to which it already refers.  */
975     ok = 0;
976   /* There can be two block-scope declarations of the same variable,
977      so long as they are `extern' declarations.  */
978   else if (TREE_CODE (decl) == VAR_DECL
979            && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
980            && DECL_EXTERNAL (decl)
981            && DECL_EXTERNAL (BINDING_VALUE (binding)))
982     {
983       duplicate_decls (decl, BINDING_VALUE (binding));
984       ok = 0;
985     }
986   else
987     {
988       error ("declaration of `%#D'", decl);
989       cp_error_at ("conflicts with previous declaration `%#D'",
990                    BINDING_VALUE (binding));
991       ok = 0;
992     }
993
994   return ok;
995 }
996
997 /* Add DECL to the list of things declared in B.  */
998
999 static void
1000 add_decl_to_level (tree decl, 
1001                    struct cp_binding_level* b)
1002 {
1003   if (TREE_CODE (decl) == NAMESPACE_DECL 
1004       && !DECL_NAMESPACE_ALIAS (decl))
1005     {
1006       TREE_CHAIN (decl) = b->namespaces;
1007       b->namespaces = decl;
1008     }
1009   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1010     {
1011       TREE_CHAIN (decl) = b->vtables;
1012       b->vtables = decl;
1013     }
1014   else       
1015     {
1016       /* We build up the list in reverse order, and reverse it later if
1017          necessary.  */
1018       TREE_CHAIN (decl) = b->names;
1019       b->names = decl;
1020       b->names_size++;
1021     }
1022 }
1023
1024 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1025    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1026    doesn't really belong to this binding level, that it got here
1027    through a using-declaration.  */
1028
1029 void
1030 push_local_binding (tree id, tree decl, int flags)
1031 {
1032   struct cp_binding_level *b;
1033
1034   /* Skip over any local classes.  This makes sense if we call
1035      push_local_binding with a friend decl of a local class.  */
1036   b = current_binding_level;
1037   while (b->parm_flag == 2)
1038     b = b->level_chain;
1039
1040   if (lookup_name_current_level (id))
1041     {
1042       /* Supplement the existing binding.  */
1043       if (!add_binding (id, decl))
1044         /* It didn't work.  Something else must be bound at this
1045            level.  Do not add DECL to the list of things to pop
1046            later.  */
1047         return;
1048     }
1049   else
1050     /* Create a new binding.  */
1051     push_binding (id, decl, b);
1052
1053   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1054     /* We must put the OVERLOAD into a TREE_LIST since the
1055        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1056        decls that got here through a using-declaration.  */
1057     decl = build_tree_list (NULL_TREE, decl);
1058
1059   /* And put DECL on the list of things declared by the current
1060      binding level.  */
1061   add_decl_to_level (decl, b);
1062 }
1063
1064 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1065    binding was successful.  */
1066
1067 int
1068 push_class_binding (tree id, tree decl)
1069 {
1070   int result = 1;
1071   tree binding = IDENTIFIER_BINDING (id);
1072   tree context;
1073
1074   /* Note that we declared this value so that we can issue an error if
1075      this is an invalid redeclaration of a name already used for some
1076      other purpose.  */
1077   note_name_declared_in_class (id, decl);
1078
1079   if (binding && BINDING_LEVEL (binding) == class_binding_level)
1080     /* Supplement the existing binding.  */
1081     result = add_binding (id, decl);
1082   else
1083     /* Create a new binding.  */
1084     push_binding (id, decl, class_binding_level);
1085
1086   /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1087      class-level declaration.  Note that we do not use DECL here
1088      because of the possibility of the `struct stat' hack; if DECL is
1089      a class-name or enum-name we might prefer a field-name, or some
1090      such.  */
1091   IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1092
1093   /* If this is a binding from a base class, mark it as such.  */
1094   binding = IDENTIFIER_BINDING (id);
1095   if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1096     {
1097       if (TREE_CODE (decl) == OVERLOAD)
1098         context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1099       else
1100         {
1101           my_friendly_assert (DECL_P (decl), 0);
1102           context = context_for_name_lookup (decl);
1103         }
1104
1105       if (is_properly_derived_from (current_class_type, context))
1106         INHERITED_VALUE_BINDING_P (binding) = 1;
1107       else
1108         INHERITED_VALUE_BINDING_P (binding) = 0;
1109     }
1110   else if (BINDING_VALUE (binding) == decl)
1111     /* We only encounter a TREE_LIST when push_class_decls detects an
1112        ambiguity.  Such an ambiguity can be overridden by a definition
1113        in this class.  */
1114     INHERITED_VALUE_BINDING_P (binding) = 1;
1115
1116   return result;
1117 }
1118
1119 /* Remove the binding for DECL which should be the innermost binding
1120    for ID.  */
1121
1122 static void
1123 pop_binding (tree id, tree decl)
1124 {
1125   tree binding;
1126
1127   if (id == NULL_TREE)
1128     /* It's easiest to write the loops that call this function without
1129        checking whether or not the entities involved have names.  We
1130        get here for such an entity.  */
1131     return;
1132
1133   /* Get the innermost binding for ID.  */
1134   binding = IDENTIFIER_BINDING (id);
1135
1136   /* The name should be bound.  */
1137   my_friendly_assert (binding != NULL_TREE, 0);
1138
1139   /* The DECL will be either the ordinary binding or the type
1140      binding for this identifier.  Remove that binding.  */
1141   if (BINDING_VALUE (binding) == decl)
1142     BINDING_VALUE (binding) = NULL_TREE;
1143   else if (BINDING_TYPE (binding) == decl)
1144     BINDING_TYPE (binding) = NULL_TREE;
1145   else
1146     abort ();
1147
1148   if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1149     {
1150       /* We're completely done with the innermost binding for this
1151          identifier.  Unhook it from the list of bindings.  */
1152       IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1153
1154       /* Add it to the free list.  */
1155       TREE_CHAIN (binding) = free_bindings;
1156       free_bindings = binding;
1157
1158       /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1159          it.  */
1160       BINDING_LEVEL (binding) = NULL;
1161     }
1162 }
1163
1164 /* When a label goes out of scope, check to see if that label was used
1165    in a valid manner, and issue any appropriate warnings or errors.  */
1166
1167 static void
1168 pop_label (tree label, tree old_value)
1169 {
1170   if (!processing_template_decl && doing_semantic_analysis_p ())
1171     {
1172       if (DECL_INITIAL (label) == NULL_TREE)
1173         {
1174           cp_error_at ("label `%D' used but not defined", label);
1175           /* Avoid crashing later.  */
1176           define_label (input_filename, 1, DECL_NAME (label));
1177         }
1178       else if (warn_unused_label && !TREE_USED (label))
1179         cp_warning_at ("label `%D' defined but not used", label);
1180     }
1181
1182   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1183 }
1184
1185 /* At the end of a function, all labels declared within the function
1186    go out of scope.  BLOCK is the top-level block for the
1187    function.  */
1188
1189 static void
1190 pop_labels (tree block)
1191 {
1192   struct named_label_list *link;
1193
1194   /* Clear out the definitions of all label names, since their scopes
1195      end here.  */
1196   for (link = named_labels; link; link = link->next)
1197     {
1198       pop_label (link->label_decl, link->old_value);
1199       /* Put the labels into the "variables" of the top-level block,
1200          so debugger can see them.  */
1201       TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1202       BLOCK_VARS (block) = link->label_decl;
1203     }
1204
1205   named_labels = NULL;
1206 }
1207
1208 /* Exit a binding level.
1209    Pop the level off, and restore the state of the identifier-decl mappings
1210    that were in effect when this level was entered.
1211
1212    If KEEP == 1, this level had explicit declarations, so
1213    and create a "block" (a BLOCK node) for the level
1214    to record its declarations and subblocks for symbol table output.
1215
1216    If FUNCTIONBODY is nonzero, this level is the body of a function,
1217    so create a block as if KEEP were set and also clear out all
1218    label names.
1219
1220    If REVERSE is nonzero, reverse the order of decls before putting
1221    them into the BLOCK.  */
1222
1223 tree
1224 poplevel (int keep, int reverse, int functionbody)
1225 {
1226   register tree link;
1227   /* The chain of decls was accumulated in reverse order.
1228      Put it into forward order, just for cleanliness.  */
1229   tree decls;
1230   int tmp = functionbody;
1231   int real_functionbody;
1232   tree tags;
1233   tree subblocks;
1234   tree block = NULL_TREE;
1235   tree decl;
1236   int leaving_for_scope;
1237
1238   if (cfun && !doing_semantic_analysis_p ())
1239     return NULL_TREE;
1240
1241   my_friendly_assert (current_binding_level->parm_flag != 2,
1242                       19990916);
1243
1244   real_functionbody = (current_binding_level->keep == 2
1245                        ? ((functionbody = 0), tmp) : functionbody);
1246   tags = functionbody >= 0 ? current_binding_level->tags : 0;
1247   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1248
1249   my_friendly_assert (!current_binding_level->class_shadowed,
1250                       19990414);
1251
1252   /* We used to use KEEP == 2 to indicate that the new block should go
1253      at the beginning of the list of blocks at this binding level,
1254      rather than the end.  This hack is no longer used.  */
1255   my_friendly_assert (keep == 0 || keep == 1, 0);
1256
1257   if (current_binding_level->keep == 1)
1258     keep = 1;
1259
1260   /* Any uses of undefined labels, and any defined labels, now operate
1261      under constraints of next binding contour.  */
1262   if (cfun && !functionbody)
1263     {
1264       struct cp_binding_level *level_chain;
1265       level_chain = current_binding_level->level_chain;
1266       if (level_chain)
1267         {
1268           struct named_label_use_list *uses;
1269           struct named_label_list *labels;
1270           for (labels = named_labels; labels; labels = labels->next)
1271             if (labels->binding_level == current_binding_level)
1272               {
1273                 tree decl;
1274                 if (current_binding_level->is_try_scope)
1275                   labels->in_try_scope = 1;
1276                 if (current_binding_level->is_catch_scope)
1277                   labels->in_catch_scope = 1;
1278                 for (decl = labels->names_in_scope; decl;
1279                      decl = TREE_CHAIN (decl))
1280                   if (decl_jump_unsafe (decl))
1281                     labels->bad_decls = tree_cons (NULL_TREE, decl,
1282                                                    labels->bad_decls);
1283                 labels->binding_level = level_chain;
1284                 labels->names_in_scope = level_chain->names;
1285               }
1286
1287           for (uses = named_label_uses; uses; uses = uses->next)
1288             if (uses->binding_level == current_binding_level)
1289               {
1290                 uses->binding_level = level_chain;
1291                 uses->names_in_scope = level_chain->names;
1292               }
1293         }
1294     }
1295
1296   /* Get the decls in the order they were written.
1297      Usually current_binding_level->names is in reverse order.
1298      But parameter decls were previously put in forward order.  */
1299
1300   if (reverse)
1301     current_binding_level->names
1302       = decls = nreverse (current_binding_level->names);
1303   else
1304     decls = current_binding_level->names;
1305
1306   /* Output any nested inline functions within this block
1307      if they weren't already output.  */
1308   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1309     if (TREE_CODE (decl) == FUNCTION_DECL
1310         && ! TREE_ASM_WRITTEN (decl)
1311         && DECL_INITIAL (decl) != NULL_TREE
1312         && TREE_ADDRESSABLE (decl)
1313         && decl_function_context (decl) == current_function_decl)
1314       {
1315         /* If this decl was copied from a file-scope decl
1316            on account of a block-scope extern decl,
1317            propagate TREE_ADDRESSABLE to the file-scope decl.  */
1318         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1319           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1320         else
1321           {
1322             push_function_context ();
1323             output_inline_function (decl);
1324             pop_function_context ();
1325           }
1326       }
1327
1328   /* When not in function-at-a-time mode, expand_end_bindings will
1329      warn about unused variables.  But, in function-at-a-time mode
1330      expand_end_bindings is not passed the list of variables in the
1331      current scope, and therefore no warning is emitted.  So, we
1332      explicitly warn here.  */
1333   if (!processing_template_decl)
1334     warn_about_unused_variables (getdecls ());
1335
1336   /* If there were any declarations or structure tags in that level,
1337      or if this level is a function body,
1338      create a BLOCK to record them for the life of this function.  */
1339   block = NULL_TREE;
1340   if (keep == 1 || functionbody)
1341     block = make_node (BLOCK);
1342   if (block != NULL_TREE)
1343     {
1344       BLOCK_VARS (block) = decls;
1345       BLOCK_SUBBLOCKS (block) = subblocks;
1346     }
1347
1348   /* In each subblock, record that this is its superior.  */
1349   if (keep >= 0)
1350     for (link = subblocks; link; link = TREE_CHAIN (link))
1351       BLOCK_SUPERCONTEXT (link) = block;
1352
1353   /* We still support the old for-scope rules, whereby the variables
1354      in a for-init statement were in scope after the for-statement
1355      ended.  We only use the new rules if flag_new_for_scope is
1356      nonzero.  */
1357   leaving_for_scope
1358     = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1359
1360   /* Remove declarations for all the DECLs in this level.  */
1361   for (link = decls; link; link = TREE_CHAIN (link))
1362     {
1363       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1364           && DECL_NAME (link))
1365         {
1366           tree outer_binding
1367             = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1368           tree ns_binding;
1369
1370           if (!outer_binding)
1371             ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1372           else
1373             ns_binding = NULL_TREE;
1374
1375           if (outer_binding
1376               && (BINDING_LEVEL (outer_binding)
1377                   == current_binding_level->level_chain))
1378             /* We have something like:
1379
1380                  int i;
1381                  for (int i; ;);
1382
1383                and we are leaving the `for' scope.  There's no reason to
1384                keep the binding of the inner `i' in this case.  */
1385             pop_binding (DECL_NAME (link), link);
1386           else if ((outer_binding
1387                     && (TREE_CODE (BINDING_VALUE (outer_binding))
1388                         == TYPE_DECL))
1389                    || (ns_binding
1390                        && TREE_CODE (ns_binding) == TYPE_DECL))
1391             /* Here, we have something like:
1392
1393                  typedef int I;
1394
1395                  void f () {
1396                    for (int I; ;);
1397                  }
1398
1399                We must pop the for-scope binding so we know what's a
1400                type and what isn't.  */
1401             pop_binding (DECL_NAME (link), link);
1402           else
1403             {
1404               /* Mark this VAR_DECL as dead so that we can tell we left it
1405                  there only for backward compatibility.  */
1406               DECL_DEAD_FOR_LOCAL (link) = 1;
1407
1408               /* Keep track of what should of have happenned when we
1409                  popped the binding.  */
1410               if (outer_binding && BINDING_VALUE (outer_binding))
1411                 DECL_SHADOWED_FOR_VAR (link)
1412                   = BINDING_VALUE (outer_binding);
1413
1414               /* Add it to the list of dead variables in the next
1415                  outermost binding to that we can remove these when we
1416                  leave that binding.  */
1417               current_binding_level->level_chain->dead_vars_from_for
1418                 = tree_cons (NULL_TREE, link,
1419                              current_binding_level->level_chain->
1420                              dead_vars_from_for);
1421
1422               /* Although we don't pop the CPLUS_BINDING, we do clear
1423                  its BINDING_LEVEL since the level is going away now.  */
1424               BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1425                 = 0;
1426             }
1427         }
1428       else
1429         {
1430           /* Remove the binding.  */
1431           decl = link;
1432           if (TREE_CODE (decl) == TREE_LIST)
1433             decl = TREE_VALUE (decl);
1434           if (DECL_P (decl))
1435             pop_binding (DECL_NAME (decl), decl);
1436           else if (TREE_CODE (decl) == OVERLOAD)
1437             pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1438           else
1439             abort ();
1440         }
1441     }
1442
1443   /* Remove declarations for any `for' variables from inner scopes
1444      that we kept around.  */
1445   for (link = current_binding_level->dead_vars_from_for;
1446        link; link = TREE_CHAIN (link))
1447     pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1448
1449   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1450   for (link = current_binding_level->type_shadowed;
1451        link; link = TREE_CHAIN (link))
1452     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1453
1454   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1455   for (link = current_binding_level->shadowed_labels;
1456        link;
1457        link = TREE_CHAIN (link))
1458     pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1459
1460   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1461      list if a `using' declaration put them there.  The debugging
1462      back-ends won't understand OVERLOAD, so we remove them here.
1463      Because the BLOCK_VARS are (temporarily) shared with
1464      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1465      popped all the bindings.  */
1466   if (block)
1467     {
1468       tree* d;
1469
1470       for (d = &BLOCK_VARS (block); *d; )
1471         {
1472           if (TREE_CODE (*d) == TREE_LIST)
1473             *d = TREE_CHAIN (*d);
1474           else
1475             d = &TREE_CHAIN (*d);
1476         }
1477     }
1478
1479   /* If the level being exited is the top level of a function,
1480      check over all the labels.  */
1481   if (functionbody)
1482     {
1483       /* Since this is the top level block of a function, the vars are
1484          the function's parameters.  Don't leave them in the BLOCK
1485          because they are found in the FUNCTION_DECL instead.  */
1486       BLOCK_VARS (block) = 0;
1487       pop_labels (block);
1488     }
1489
1490   tmp = current_binding_level->keep;
1491
1492   pop_binding_level ();
1493   if (functionbody)
1494     DECL_INITIAL (current_function_decl) = block;
1495   else if (block)
1496     current_binding_level->blocks
1497       = chainon (current_binding_level->blocks, block);
1498
1499   /* If we did not make a block for the level just exited,
1500      any blocks made for inner levels
1501      (since they cannot be recorded as subblocks in that level)
1502      must be carried forward so they will later become subblocks
1503      of something else.  */
1504   else if (subblocks)
1505     current_binding_level->blocks
1506       = chainon (current_binding_level->blocks, subblocks);
1507
1508   /* Each and every BLOCK node created here in `poplevel' is important
1509      (e.g. for proper debugging information) so if we created one
1510      earlier, mark it as "used".  */
1511   if (block)
1512     TREE_USED (block) = 1;
1513
1514   /* Take care of compiler's internal binding structures.  */
1515   if (tmp == 2)
1516     {
1517       tree scope_stmts;
1518
1519       scope_stmts
1520         = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1521       if (block)
1522         {
1523           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1524           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1525         }
1526
1527       block = poplevel (keep, reverse, functionbody);
1528     }
1529
1530   return block;
1531 }
1532
1533 /* Delete the node BLOCK from the current binding level.
1534    This is used for the block inside a stmt expr ({...})
1535    so that the block can be reinserted where appropriate.  */
1536
1537 void
1538 delete_block (tree block)
1539 {
1540   tree t;
1541   if (current_binding_level->blocks == block)
1542     current_binding_level->blocks = TREE_CHAIN (block);
1543   for (t = current_binding_level->blocks; t;)
1544     {
1545       if (TREE_CHAIN (t) == block)
1546         TREE_CHAIN (t) = TREE_CHAIN (block);
1547       else
1548         t = TREE_CHAIN (t);
1549     }
1550   TREE_CHAIN (block) = NULL_TREE;
1551   /* Clear TREE_USED which is always set by poplevel.
1552      The flag is set again if insert_block is called.  */
1553   TREE_USED (block) = 0;
1554 }
1555
1556 /* Insert BLOCK at the end of the list of subblocks of the
1557    current binding level.  This is used when a BIND_EXPR is expanded,
1558    to handle the BLOCK node inside the BIND_EXPR.  */
1559
1560 void
1561 insert_block (tree block)
1562 {
1563   TREE_USED (block) = 1;
1564   current_binding_level->blocks
1565     = chainon (current_binding_level->blocks, block);
1566 }
1567
1568 /* Set the BLOCK node for the innermost scope
1569    (the one we are currently in).  */
1570
1571 void
1572 set_block (tree block ATTRIBUTE_UNUSED )
1573 {
1574   /* The RTL expansion machinery requires us to provide this callback,
1575      but it is not applicable in function-at-a-time mode.  */
1576   my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1577 }
1578
1579 /* Do a pushlevel for class declarations.  */
1580
1581 void
1582 pushlevel_class (void)
1583 {
1584   register struct cp_binding_level *newlevel;
1585
1586   /* Reuse or create a struct for this binding level.  */
1587 #if defined(DEBUG_BINDING_LEVELS)
1588   if (0)
1589 #else /* !defined(DEBUG_BINDING_LEVELS) */
1590   if (free_binding_level)
1591 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1592     {
1593       newlevel = free_binding_level;
1594       free_binding_level = free_binding_level->level_chain;
1595     }
1596   else
1597     newlevel = make_binding_level ();
1598
1599 #if defined(DEBUG_BINDING_LEVELS)
1600   is_class_level = 1;
1601 #endif /* defined(DEBUG_BINDING_LEVELS) */
1602
1603   push_binding_level (newlevel, 0, 0);
1604
1605   class_binding_level = current_binding_level;
1606   class_binding_level->parm_flag = 2;
1607   class_binding_level->this_class = current_class_type;
1608 }
1609
1610 /* ...and a poplevel for class declarations.  */
1611
1612 void
1613 poplevel_class (void)
1614 {
1615   register struct cp_binding_level *level = class_binding_level;
1616   tree shadowed;
1617
1618   my_friendly_assert (level != 0, 354);
1619
1620   /* If we're leaving a toplevel class, don't bother to do the setting
1621      of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1622      shouldn't even be used when current_class_type isn't set, and second,
1623      if we don't touch it here, we're able to use the cache effect if the
1624      next time we're entering a class scope, it is the same class.  */
1625   if (current_class_depth != 1)
1626     {
1627       struct cp_binding_level* b;
1628
1629       /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1630       for (shadowed = level->class_shadowed;
1631            shadowed;
1632            shadowed = TREE_CHAIN (shadowed))
1633         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1634
1635       /* Find the next enclosing class, and recreate
1636          IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1637       b = level->level_chain;
1638       while (b && b->parm_flag != 2)
1639         b = b->level_chain;
1640
1641       if (b)
1642         for (shadowed = b->class_shadowed;
1643              shadowed;
1644              shadowed = TREE_CHAIN (shadowed))
1645           {
1646             tree t;
1647
1648             t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1649             while (t && BINDING_LEVEL (t) != b)
1650               t = TREE_CHAIN (t);
1651
1652             if (t)
1653               IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1654                 = BINDING_VALUE (t);
1655           }
1656     }
1657   else
1658     /* Remember to save what IDENTIFIER's were bound in this scope so we
1659        can recover from cache misses.  */
1660     {
1661       previous_class_type = current_class_type;
1662       previous_class_values = class_binding_level->class_shadowed;
1663     }
1664   for (shadowed = level->type_shadowed;
1665        shadowed;
1666        shadowed = TREE_CHAIN (shadowed))
1667     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1668
1669   /* Remove the bindings for all of the class-level declarations.  */
1670   for (shadowed = level->class_shadowed;
1671        shadowed;
1672        shadowed = TREE_CHAIN (shadowed))
1673     pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1674
1675   /* Now, pop out of the binding level which we created up in the
1676      `pushlevel_class' routine.  */
1677 #if defined(DEBUG_BINDING_LEVELS)
1678   is_class_level = 1;
1679 #endif /* defined(DEBUG_BINDING_LEVELS) */
1680
1681   pop_binding_level ();
1682 }
1683
1684 /* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1685    for any names in enclosing classes.  */
1686
1687 void
1688 clear_identifier_class_values (void)
1689 {
1690   tree t;
1691
1692   if (!class_binding_level)
1693     return;
1694
1695   for (t = class_binding_level->class_shadowed;
1696        t;
1697        t = TREE_CHAIN (t))
1698     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1699 }
1700
1701 /* Returns nonzero if T is a virtual function table.  */
1702
1703 int
1704 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1705 {
1706   return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1707 }
1708
1709 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1710    functions.  */
1711
1712 int
1713 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1714 {
1715   return (TREE_CODE (t) == TYPE_DECL
1716           && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1717           && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1718 }
1719
1720 /* Return the declarations that are members of the namespace NS.  */
1721
1722 tree
1723 cp_namespace_decls (tree ns)
1724 {
1725   return NAMESPACE_LEVEL (ns)->names;
1726 }
1727
1728 struct walk_globals_data {
1729   walk_globals_pred p;
1730   walk_globals_fn f;
1731   void *data;
1732 };
1733
1734 /* Walk the vtable declarations in NAMESPACE.  Whenever one is found
1735    for which P returns nonzero, call F with its address.  If any call
1736    to F returns a nonzero value, return a nonzero value.  */
1737
1738 static int
1739 walk_vtables_r (tree namespace, void* data)
1740 {
1741   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1742   walk_globals_fn f = wgd->f;
1743   void *d = wgd->data;
1744   tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1745   int result = 0;
1746
1747   for (; decl ; decl = TREE_CHAIN (decl))
1748     result |= (*f) (&decl, d);
1749
1750   return result;
1751 }
1752
1753 /* Walk the vtable declarations.  Whenever one is found for which P
1754    returns nonzero, call F with its address.  If any call to F
1755    returns a nonzero value, return a nonzero value.  */
1756 bool
1757 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1758 {    
1759   struct walk_globals_data wgd;
1760   wgd.p = p;    
1761   wgd.f = f;
1762   wgd.data = data;
1763
1764   return walk_namespaces (walk_vtables_r, &wgd);
1765 }
1766
1767 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1768    itself, calling F for each.  The DATA is passed to F as well.  */
1769
1770 static int
1771 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1772 {
1773   int result = 0;
1774   tree current = NAMESPACE_LEVEL (namespace)->namespaces;     
1775
1776   result |= (*f) (namespace, data);
1777
1778   for (; current; current = TREE_CHAIN (current))
1779     result |= walk_namespaces_r (current, f, data);
1780
1781   return result;
1782 }
1783
1784 /* Walk all the namespaces, calling F for each.  The DATA is passed to
1785    F as well.  */
1786
1787 int
1788 walk_namespaces (walk_namespaces_fn f, void* data)
1789 {
1790   return walk_namespaces_r (global_namespace, f, data);
1791 }
1792
1793 /* Walk the global declarations in NAMESPACE.  Whenever one is found
1794    for which P returns nonzero, call F with its address.  If any call
1795    to F returns a nonzero value, return a nonzero value.  */
1796
1797 static int
1798 walk_globals_r (tree namespace, void* data)
1799 {
1800   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1801   walk_globals_pred p = wgd->p;
1802   walk_globals_fn f = wgd->f;
1803   void *d = wgd->data;
1804   tree *t;
1805   int result = 0;
1806
1807   t = &NAMESPACE_LEVEL (namespace)->names;
1808
1809   while (*t)
1810     {
1811       tree glbl = *t;
1812
1813       if ((*p) (glbl, d))
1814         result |= (*f) (t, d);
1815
1816       /* If F changed *T, then *T still points at the next item to
1817          examine.  */
1818       if (*t == glbl)
1819         t = &TREE_CHAIN (*t);
1820     }
1821
1822   return result;
1823 }
1824
1825 /* Walk the global declarations.  Whenever one is found for which P
1826    returns true, call F with its address.  If any call to F
1827    returns true, return true.  */
1828
1829 bool
1830 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1831 {
1832   struct walk_globals_data wgd;
1833   wgd.p = p;
1834   wgd.f = f;
1835   wgd.data = data;
1836
1837   return walk_namespaces (walk_globals_r, &wgd);
1838 }
1839
1840 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1841    DATA is non-NULL, this is the last time we will call
1842    wrapup_global_declarations for this NAMESPACE.  */
1843
1844 int
1845 wrapup_globals_for_namespace (tree namespace, void* data)
1846 {
1847   tree globals = cp_namespace_decls (namespace);
1848   int len = NAMESPACE_LEVEL (namespace)->names_size;
1849   tree *vec = (tree *) alloca (sizeof (tree) * len);
1850   int i;
1851   int result;
1852   tree decl;
1853   int last_time = (data != 0);
1854
1855   if (last_time && namespace == global_namespace)
1856     /* Let compile_file handle the global namespace.  */
1857     return 0;
1858
1859   /* Process the decls in reverse order--earliest first.
1860      Put them into VEC from back to front, then take out from front.  */       
1861   for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1862     vec[len - i - 1] = decl;
1863
1864   if (last_time)
1865     {
1866       check_global_declarations (vec, len);
1867       return 0;
1868     }
1869
1870   /* Write out any globals that need to be output.  */
1871   result = wrapup_global_declarations (vec, len);
1872
1873   return result;
1874 }
1875
1876 \f
1877 /* For debugging.  */
1878 static int no_print_functions = 0;
1879 static int no_print_builtins = 0;
1880
1881 void
1882 print_binding_level (struct cp_binding_level* lvl)
1883 {
1884   tree t;
1885   int i = 0, len;
1886   fprintf (stderr, " blocks=");
1887   fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1888   if (lvl->tag_transparent)
1889     fprintf (stderr, " tag-transparent");
1890   if (lvl->more_cleanups_ok)
1891     fprintf (stderr, " more-cleanups-ok");
1892   if (lvl->have_cleanups)
1893     fprintf (stderr, " have-cleanups");
1894   fprintf (stderr, "\n");
1895   if (lvl->names)
1896     {
1897       fprintf (stderr, " names:\t");
1898       /* We can probably fit 3 names to a line?  */
1899       for (t = lvl->names; t; t = TREE_CHAIN (t))
1900         {
1901           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1902             continue;
1903           if (no_print_builtins
1904               && (TREE_CODE (t) == TYPE_DECL)
1905               && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1906             continue;
1907
1908           /* Function decls tend to have longer names.  */
1909           if (TREE_CODE (t) == FUNCTION_DECL)
1910             len = 3;
1911           else
1912             len = 2;
1913           i += len;
1914           if (i > 6)
1915             {
1916               fprintf (stderr, "\n\t");
1917               i = len;
1918             }
1919           print_node_brief (stderr, "", t, 0);
1920           if (t == error_mark_node)
1921             break;
1922         }
1923       if (i)
1924         fprintf (stderr, "\n");
1925     }
1926   if (lvl->tags)
1927     {
1928       fprintf (stderr, " tags:\t");
1929       i = 0;
1930       for (t = lvl->tags; t; t = TREE_CHAIN (t))
1931         {
1932           if (TREE_PURPOSE (t) == NULL_TREE)
1933             len = 3;
1934           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1935             len = 2;
1936           else
1937             len = 4;
1938           i += len;
1939           if (i > 5)
1940             {
1941               fprintf (stderr, "\n\t");
1942               i = len;
1943             }
1944           if (TREE_PURPOSE (t) == NULL_TREE)
1945             {
1946               print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1947               fprintf (stderr, ">");
1948             }
1949           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1950             print_node_brief (stderr, "", TREE_VALUE (t), 0);
1951           else
1952             {
1953               print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1954               print_node_brief (stderr, "", TREE_VALUE (t), 0);
1955               fprintf (stderr, ">");
1956             }
1957         }
1958       if (i)
1959         fprintf (stderr, "\n");
1960     }
1961   if (lvl->class_shadowed)
1962     {
1963       fprintf (stderr, " class-shadowed:");
1964       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1965         {
1966           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1967         }
1968       fprintf (stderr, "\n");
1969     }
1970   if (lvl->type_shadowed)
1971     {
1972       fprintf (stderr, " type-shadowed:");
1973       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1974         {
1975           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1976         }
1977       fprintf (stderr, "\n");
1978     }
1979 }
1980
1981 void
1982 print_other_binding_stack (struct cp_binding_level *stack)
1983 {
1984   struct cp_binding_level *level;
1985   for (level = stack; level != global_binding_level; level = level->level_chain)
1986     {
1987       fprintf (stderr, "binding level ");
1988       fprintf (stderr, HOST_PTR_PRINTF, level);
1989       fprintf (stderr, "\n");
1990       print_binding_level (level);
1991     }
1992 }
1993
1994 void
1995 print_binding_stack (void)
1996 {
1997   struct cp_binding_level *b;
1998   fprintf (stderr, "current_binding_level=");
1999   fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2000   fprintf (stderr, "\nclass_binding_level=");
2001   fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2002   fprintf (stderr, "\nglobal_binding_level=");
2003   fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2004   fprintf (stderr, "\n");
2005   if (class_binding_level)
2006     {
2007       for (b = class_binding_level; b; b = b->level_chain)
2008         if (b == current_binding_level)
2009           break;
2010       if (b)
2011         b = class_binding_level;
2012       else
2013         b = current_binding_level;
2014     }
2015   else
2016     b = current_binding_level;
2017   print_other_binding_stack (b);
2018   fprintf (stderr, "global:\n");
2019   print_binding_level (global_binding_level);
2020 }
2021
2022 /* Namespace binding access routines: The namespace_bindings field of
2023    the identifier is polymorphic, with three possible values:
2024    NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2025    indicating the BINDING_VALUE of global_namespace.  */
2026
2027 /* Check whether the a binding for the name to scope is known.
2028    Assumes that the bindings of the name are already a list
2029    of bindings. Returns the binding found, or NULL_TREE.  */
2030
2031 static tree
2032 find_binding (tree name, tree scope)
2033 {
2034   tree iter, prev = NULL_TREE;
2035
2036   scope = ORIGINAL_NAMESPACE (scope);
2037
2038   for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2039        iter = TREE_CHAIN (iter))
2040     {
2041       my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2042       if (BINDING_SCOPE (iter) == scope)
2043         {
2044           /* Move binding found to the front of the list, so
2045              subsequent lookups will find it faster.  */
2046           if (prev)
2047             {
2048               TREE_CHAIN (prev) = TREE_CHAIN (iter);
2049               TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2050               IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2051             }
2052           return iter;
2053         }
2054       prev = iter;
2055     }
2056   return NULL_TREE;
2057 }
2058
2059 /* Always returns a binding for name in scope. If the
2060    namespace_bindings is not a list, convert it to one first.
2061    If no binding is found, make a new one.  */
2062
2063 tree
2064 binding_for_name (tree name, tree scope)
2065 {
2066   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2067   tree result;
2068
2069   scope = ORIGINAL_NAMESPACE (scope);
2070
2071   if (b && TREE_CODE (b) != CPLUS_BINDING)
2072     {
2073       /* Get rid of optimization for global scope.  */
2074       IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2075       BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2076       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2077     }
2078   if (b && (result = find_binding (name, scope)))
2079     return result;
2080   /* Not found, make a new one.  */
2081   result = make_node (CPLUS_BINDING);
2082   TREE_CHAIN (result) = b;
2083   IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2084   BINDING_SCOPE (result) = scope;
2085   BINDING_TYPE (result) = NULL_TREE;
2086   BINDING_VALUE (result) = NULL_TREE;
2087   return result;
2088 }
2089
2090 /* Return the binding value for name in scope, considering that
2091    namespace_binding may or may not be a list of CPLUS_BINDINGS.  */
2092
2093 tree
2094 namespace_binding (tree name, tree scope)
2095 {
2096   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2097   if (b == NULL_TREE)
2098     return NULL_TREE;
2099   if (scope == NULL_TREE)
2100     scope = global_namespace;
2101   if (TREE_CODE (b) != CPLUS_BINDING)
2102     return (scope == global_namespace) ? b : NULL_TREE;
2103   name = find_binding (name,scope);
2104   if (name == NULL_TREE)
2105     return name;
2106   return BINDING_VALUE (name);
2107 }
2108
2109 /* Set the binding value for name in scope. If modifying the binding
2110    of global_namespace is attempted, try to optimize it.  */
2111
2112 void
2113 set_namespace_binding (tree name, tree scope, tree val)
2114 {
2115   tree b;
2116
2117   if (scope == NULL_TREE)
2118     scope = global_namespace;
2119
2120   if (scope == global_namespace)
2121     {
2122       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2123       if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2124         {
2125           IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2126           return;
2127         }
2128     }
2129   b = binding_for_name (name, scope);
2130   BINDING_VALUE (b) = val;
2131 }
2132
2133 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2134    select a name that is unique to this compilation unit.  */
2135
2136 void
2137 push_namespace (tree name)
2138 {
2139   tree d = NULL_TREE;
2140   int need_new = 1;
2141   int implicit_use = 0;
2142   int global = 0;
2143   if (!global_namespace)
2144     {
2145       /* This must be ::.  */
2146       my_friendly_assert (name == get_identifier ("::"), 377);
2147       global = 1;
2148     }
2149   else if (!name)
2150     {
2151       /* The name of anonymous namespace is unique for the translation
2152          unit.  */
2153       if (!anonymous_namespace_name)
2154         anonymous_namespace_name = get_file_function_name ('N');
2155       name = anonymous_namespace_name;
2156       d = IDENTIFIER_NAMESPACE_VALUE (name);
2157       if (d)
2158         /* Reopening anonymous namespace.  */
2159         need_new = 0;
2160       implicit_use = 1;
2161     }
2162   else
2163     {
2164       /* Check whether this is an extended namespace definition.  */
2165       d = IDENTIFIER_NAMESPACE_VALUE (name);
2166       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2167         {
2168           need_new = 0;
2169           if (DECL_NAMESPACE_ALIAS (d))
2170             {
2171               error ("namespace alias `%D' not allowed here, assuming `%D'",
2172                         d, DECL_NAMESPACE_ALIAS (d));
2173               d = DECL_NAMESPACE_ALIAS (d);
2174             }
2175         }
2176     }
2177
2178   if (need_new)
2179     {
2180       /* Make a new namespace, binding the name to it.  */
2181       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2182       /* The global namespace is not pushed, and the global binding
2183          level is set elsewhere.  */
2184       if (!global)
2185         {
2186           DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2187           d = pushdecl (d);
2188           pushlevel (0);
2189           declare_namespace_level ();
2190           NAMESPACE_LEVEL (d) = current_binding_level;
2191         }
2192     }
2193   else
2194     resume_binding_level (NAMESPACE_LEVEL (d));
2195
2196   if (implicit_use)
2197     do_using_directive (d);
2198   /* Enter the name space.  */
2199   current_namespace = d;
2200 }
2201
2202 /* Pop from the scope of the current namespace.  */
2203
2204 void
2205 pop_namespace (void)
2206 {
2207   my_friendly_assert (current_namespace != global_namespace, 20010801);
2208   current_namespace = CP_DECL_CONTEXT (current_namespace);
2209   /* The binding level is not popped, as it might be re-opened later.  */
2210   suspend_binding_level ();
2211 }
2212
2213 /* Push into the scope of the namespace NS, even if it is deeply
2214    nested within another namespace.  */
2215
2216 void
2217 push_nested_namespace (tree ns)
2218 {
2219   if (ns == global_namespace)
2220     push_to_top_level ();
2221   else
2222     {
2223       push_nested_namespace (CP_DECL_CONTEXT (ns));
2224       push_namespace (DECL_NAME (ns));
2225     }
2226 }
2227
2228 /* Pop back from the scope of the namespace NS, which was previously
2229    entered with push_nested_namespace.  */
2230
2231 void
2232 pop_nested_namespace (tree ns)
2233 {
2234   while (ns != global_namespace)
2235     {
2236       pop_namespace ();
2237       ns = CP_DECL_CONTEXT (ns);
2238     }
2239
2240   pop_from_top_level ();
2241 }
2242
2243 \f
2244 /* Subroutines for reverting temporarily to top-level for instantiation
2245    of templates and such.  We actually need to clear out the class- and
2246    local-value slots of all identifiers, so that only the global values
2247    are at all visible.  Simply setting current_binding_level to the global
2248    scope isn't enough, because more binding levels may be pushed.  */
2249 struct saved_scope *scope_chain;
2250
2251 static tree
2252 store_bindings (tree names, tree old_bindings)
2253 {
2254   tree t;
2255   tree search_bindings = old_bindings;
2256
2257   for (t = names; t; t = TREE_CHAIN (t))
2258     {
2259       tree binding, t1, id;
2260
2261       if (TREE_CODE (t) == TREE_LIST)
2262         id = TREE_PURPOSE (t);
2263       else
2264         id = DECL_NAME (t);
2265
2266       if (!id
2267           /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2268              we have no IDENTIFIER_BINDING if we have left the class
2269              scope, but cached the class-level declarations.  */
2270           || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2271         continue;
2272
2273       for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1))
2274         if (TREE_VEC_ELT (t1, 0) == id)
2275           goto skip_it;
2276
2277       my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2278       binding = make_tree_vec (4);
2279       TREE_VEC_ELT (binding, 0) = id;
2280       TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2281       TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2282       TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2283       IDENTIFIER_BINDING (id) = NULL_TREE;
2284       IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2285       TREE_CHAIN (binding) = old_bindings;
2286       old_bindings = binding;
2287     skip_it:
2288       ;
2289     }
2290   return old_bindings;
2291 }
2292
2293 void
2294 maybe_push_to_top_level (int pseudo)
2295 {
2296   struct saved_scope *s;
2297   struct cp_binding_level *b;
2298   tree old_bindings;
2299   int need_pop;
2300
2301   s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2302
2303   b = scope_chain ? current_binding_level : 0;
2304
2305   /* If we're in the middle of some function, save our state.  */
2306   if (cfun)
2307     {
2308       need_pop = 1;
2309       push_function_context_to (NULL_TREE);
2310     }
2311   else
2312     need_pop = 0;
2313
2314   old_bindings = NULL_TREE;
2315   if (scope_chain && previous_class_type)
2316     old_bindings = store_bindings (previous_class_values, old_bindings);
2317
2318   /* Have to include global_binding_level, because class-level decls
2319      aren't listed anywhere useful.  */
2320   for (; b; b = b->level_chain)
2321     {
2322       tree t;
2323
2324       /* Template IDs are inserted into the global level. If they were
2325          inserted into namespace level, finish_file wouldn't find them
2326          when doing pending instantiations. Therefore, don't stop at
2327          namespace level, but continue until :: .  */
2328       if (b == global_binding_level || (pseudo && b->template_parms_p))
2329         break;
2330
2331       old_bindings = store_bindings (b->names, old_bindings);
2332       /* We also need to check class_shadowed to save class-level type
2333          bindings, since pushclass doesn't fill in b->names.  */
2334       if (b->parm_flag == 2)
2335         old_bindings = store_bindings (b->class_shadowed, old_bindings);
2336
2337       /* Unwind type-value slots back to top level.  */
2338       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2339         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2340     }
2341   s->prev = scope_chain;
2342   s->old_bindings = old_bindings;
2343   s->bindings = b;
2344   s->need_pop_function_context = need_pop;
2345   s->function_decl = current_function_decl;
2346   s->last_parms = last_function_parms;
2347   s->check_access = flag_access_control;
2348
2349   scope_chain = s;
2350   current_function_decl = NULL_TREE;
2351   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2352   current_lang_name = lang_name_cplusplus;
2353   current_namespace = global_namespace;
2354 }
2355
2356 void
2357 push_to_top_level (void)
2358 {
2359   maybe_push_to_top_level (0);
2360 }
2361
2362 void
2363 pop_from_top_level (void)
2364 {
2365   struct saved_scope *s = scope_chain;
2366   tree t;
2367
2368   /* Clear out class-level bindings cache.  */
2369   if (previous_class_type)
2370     invalidate_class_lookup_cache ();
2371
2372   current_lang_base = 0;
2373
2374   scope_chain = s->prev;
2375   for (t = s->old_bindings; t; t = TREE_CHAIN (t))
2376     {
2377       tree id = TREE_VEC_ELT (t, 0);
2378
2379       SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2380       IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2381       IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2382     }
2383
2384   /* If we were in the middle of compiling a function, restore our
2385      state.  */
2386   if (s->need_pop_function_context)
2387     pop_function_context_from (NULL_TREE);
2388   current_function_decl = s->function_decl;
2389   last_function_parms = s->last_parms;
2390 }
2391 \f
2392 /* Push a definition of struct, union or enum tag "name".
2393    into binding_level "b".   "type" should be the type node,
2394    We assume that the tag "name" is not already defined.
2395
2396    Note that the definition may really be just a forward reference.
2397    In that case, the TYPE_SIZE will be a NULL_TREE.
2398
2399    C++ gratuitously puts all these tags in the name space.  */
2400
2401 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2402    record the shadowed value for this binding contour.  TYPE is
2403    the type that ID maps to.  */
2404
2405 static void
2406 set_identifier_type_value_with_scope (tree id, 
2407                                       tree type, 
2408                                       struct cp_binding_level* b)
2409 {
2410   if (!b->namespace_p)
2411     {
2412       /* Shadow the marker, not the real thing, so that the marker
2413          gets restored later.  */
2414       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2415       b->type_shadowed
2416         = tree_cons (id, old_type_value, b->type_shadowed);
2417     }
2418   else
2419     {
2420       tree binding = binding_for_name (id, current_namespace);
2421       BINDING_TYPE (binding) = type;
2422       /* Store marker instead of real type.  */
2423       type = global_type_node;
2424     }
2425   SET_IDENTIFIER_TYPE_VALUE (id, type);
2426 }
2427
2428 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2429
2430 void
2431 set_identifier_type_value (tree id, tree type)
2432 {
2433   set_identifier_type_value_with_scope (id, type, current_binding_level);
2434 }
2435
2436 /* Return the type associated with id.  */
2437
2438 tree
2439 identifier_type_value (tree id)
2440 {
2441   /* There is no type with that name, anywhere.  */
2442   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2443     return NULL_TREE;
2444   /* This is not the type marker, but the real thing.  */
2445   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2446     return REAL_IDENTIFIER_TYPE_VALUE (id);
2447   /* Have to search for it. It must be on the global level, now.
2448      Ask lookup_name not to return non-types. */
2449   id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2450   if (id)
2451     return TREE_TYPE (id);
2452   return NULL_TREE;
2453 }
2454
2455 /* Pop off extraneous binding levels left over due to syntax errors.
2456
2457    We don't pop past namespaces, as they might be valid.  */
2458
2459 void
2460 pop_everything (void)
2461 {
2462 #ifdef DEBUG_BINDING_LEVELS
2463   fprintf (stderr, "XXX entering pop_everything ()\n");
2464 #endif
2465   while (!toplevel_bindings_p ())
2466     {
2467       if (current_binding_level->parm_flag == 2)
2468         pop_nested_class ();
2469       else
2470         poplevel (0, 0, 0);
2471     }
2472 #ifdef DEBUG_BINDING_LEVELS
2473   fprintf (stderr, "XXX leaving pop_everything ()\n");
2474 #endif
2475 }
2476
2477 /* The type TYPE is being declared.  If it is a class template, or a
2478    specialization of a class template, do any processing required and
2479    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2480    being declared a friend.  B is the binding level at which this TYPE
2481    should be bound.
2482
2483    Returns the TYPE_DECL for TYPE, which may have been altered by this
2484    processing.  */
2485
2486 static tree
2487 maybe_process_template_type_declaration (tree type, 
2488                                          int globalize, 
2489                                          struct cp_binding_level* b)
2490 {
2491   tree decl = TYPE_NAME (type);
2492
2493   if (processing_template_parmlist)
2494     /* You can't declare a new template type in a template parameter
2495        list.  But, you can declare a non-template type:
2496
2497          template <class A*> struct S;
2498
2499        is a forward-declaration of `A'.  */
2500     ;
2501   else
2502     {
2503       maybe_check_template_type (type);
2504
2505       my_friendly_assert (IS_AGGR_TYPE (type)
2506                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2507
2508
2509       if (processing_template_decl)
2510         {
2511           /* This may change after the call to
2512              push_template_decl_real, but we want the original value.  */
2513           tree name = DECL_NAME (decl);
2514
2515           decl = push_template_decl_real (decl, globalize);
2516           /* If the current binding level is the binding level for the
2517              template parameters (see the comment in
2518              begin_template_parm_list) and the enclosing level is a class
2519              scope, and we're not looking at a friend, push the
2520              declaration of the member class into the class scope.  In the
2521              friend case, push_template_decl will already have put the
2522              friend into global scope, if appropriate.  */
2523           if (TREE_CODE (type) != ENUMERAL_TYPE
2524               && !globalize && b->template_parms_p
2525               && b->level_chain->parm_flag == 2)
2526             {
2527               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2528               /* Put this tag on the list of tags for the class, since
2529                  that won't happen below because B is not the class
2530                  binding level, but is instead the pseudo-global level.  */
2531               b->level_chain->tags =
2532                 tree_cons (name, type, b->level_chain->tags);
2533               if (!COMPLETE_TYPE_P (current_class_type))
2534                 {
2535                   maybe_add_class_template_decl_list (current_class_type,
2536                                                       type, /*friend_p=*/0);
2537                   CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2538                 }
2539             }
2540         }
2541     }
2542
2543   return decl;
2544 }
2545
2546 /* In C++, you don't have to write `struct S' to refer to `S'; you
2547    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2548    if the user had written `typedef struct S S'.  Create and return
2549    the TYPE_DECL for TYPE.  */
2550
2551 tree
2552 create_implicit_typedef (tree name, tree type)
2553 {
2554   tree decl;
2555
2556   decl = build_decl (TYPE_DECL, name, type);
2557   DECL_ARTIFICIAL (decl) = 1;
2558   /* There are other implicit type declarations, like the one *within*
2559      a class that allows you to write `S::S'.  We must distinguish
2560      amongst these.  */
2561   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2562   TYPE_NAME (type) = decl;
2563
2564   return decl;
2565 }
2566
2567 /* Remember a local name for name-mangling purposes.  */
2568
2569 static void
2570 push_local_name (tree decl)
2571 {
2572   size_t i, nelts;
2573   tree t, name;
2574
2575   if (!local_names)
2576     VARRAY_TREE_INIT (local_names, 8, "local_names");
2577
2578   name = DECL_NAME (decl);
2579
2580   nelts = VARRAY_ACTIVE_SIZE (local_names);
2581   for (i = 0; i < nelts; i++)
2582     {
2583       t = VARRAY_TREE (local_names, i);
2584       if (DECL_NAME (t) == name)
2585         {
2586           if (!DECL_LANG_SPECIFIC (decl))
2587             retrofit_lang_decl (decl);
2588           DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2589           if (DECL_LANG_SPECIFIC (t))
2590             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2591           else
2592             DECL_DISCRIMINATOR (decl) = 1;
2593
2594           VARRAY_TREE (local_names, i) = decl;
2595           return;
2596         }
2597     }
2598
2599   VARRAY_PUSH_TREE (local_names, decl);
2600 }
2601
2602 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2603    Normally put it into the inner-most non-tag-transparent scope,
2604    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2605    The latter is needed for implicit declarations.  */
2606
2607 void
2608 pushtag (tree name, tree type, int globalize)
2609 {
2610   register struct cp_binding_level *b;
2611
2612   b = current_binding_level;
2613   while (b->tag_transparent
2614          || (b->parm_flag == 2
2615              && (globalize
2616                  /* We may be defining a new type in the initializer
2617                     of a static member variable. We allow this when
2618                     not pedantic, and it is particularly useful for
2619                     type punning via an anonymous union.  */
2620                  || COMPLETE_TYPE_P (b->this_class))))
2621     b = b->level_chain;
2622
2623   b->tags = tree_cons (name, type, b->tags);
2624
2625   if (name)
2626     {
2627       /* Do C++ gratuitous typedefing.  */
2628       if (IDENTIFIER_TYPE_VALUE (name) != type)
2629         {
2630           register tree d = NULL_TREE;
2631           int in_class = 0;
2632           tree context = TYPE_CONTEXT (type);
2633
2634           if (! context)
2635             {
2636               tree cs = current_scope ();
2637
2638               if (! globalize)
2639                 context = cs;
2640               else if (cs != NULL_TREE && TYPE_P (cs))
2641                 /* When declaring a friend class of a local class, we want
2642                    to inject the newly named class into the scope
2643                    containing the local class, not the namespace scope.  */
2644                 context = decl_function_context (get_type_decl (cs));
2645             }
2646           if (!context)
2647             context = current_namespace;
2648
2649           if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2650               || b->parm_flag == 2)
2651             in_class = 1;
2652
2653           if (current_lang_name == lang_name_java)
2654             TYPE_FOR_JAVA (type) = 1;
2655
2656           d = create_implicit_typedef (name, type);
2657           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2658           if (! in_class)
2659             set_identifier_type_value_with_scope (name, type, b);
2660
2661           d = maybe_process_template_type_declaration (type,
2662                                                        globalize, b);
2663
2664           if (b->parm_flag == 2)
2665             {
2666               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2667                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2668                    class.  But if it's a member template class, we
2669                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2670                    is done later.  */
2671                 finish_member_declaration (d);
2672               else
2673                 pushdecl_class_level (d);
2674             }
2675           else
2676             d = pushdecl_with_scope (d, b);
2677
2678           /* FIXME what if it gets a name from typedef?  */
2679           if (ANON_AGGRNAME_P (name))
2680             DECL_IGNORED_P (d) = 1;
2681
2682           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2683
2684           /* If this is a local class, keep track of it.  We need this
2685              information for name-mangling, and so that it is possible to find
2686              all function definitions in a translation unit in a convenient
2687              way.  (It's otherwise tricky to find a member function definition
2688              it's only pointed to from within a local class.)  */
2689           if (TYPE_CONTEXT (type)
2690               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2691               && !processing_template_decl)
2692             VARRAY_PUSH_TREE (local_classes, type);
2693         }
2694       if (b->parm_flag == 2)
2695         {
2696           if (!COMPLETE_TYPE_P (current_class_type))
2697             {
2698               maybe_add_class_template_decl_list (current_class_type,
2699                                                   type, /*friend_p=*/0);
2700               CLASSTYPE_TAGS (current_class_type) = b->tags;
2701             }
2702         }
2703     }
2704
2705   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2706     /* Use the canonical TYPE_DECL for this node.  */
2707     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2708   else
2709     {
2710       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2711          will be the tagged type we just added to the current
2712          binding level.  This fake NULL-named TYPE_DECL node helps
2713          dwarfout.c to know when it needs to output a
2714          representation of a tagged type, and it also gives us a
2715          convenient place to record the "scope start" address for
2716          the tagged type.  */
2717
2718       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2719       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2720     }
2721 }
2722
2723 /* Counter used to create anonymous type names.  */
2724
2725 static GTY(()) int anon_cnt;
2726
2727 /* Return an IDENTIFIER which can be used as a name for
2728    anonymous structs and unions.  */
2729
2730 tree
2731 make_anon_name (void)
2732 {
2733   char buf[32];
2734
2735   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2736   return get_identifier (buf);
2737 }
2738
2739 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2740    This keeps dbxout from getting confused.  */
2741
2742 void
2743 clear_anon_tags (void)
2744 {
2745   register struct cp_binding_level *b;
2746   register tree tags;
2747   static int last_cnt = 0;
2748
2749   /* Fast out if no new anon names were declared.  */
2750   if (last_cnt == anon_cnt)
2751     return;
2752
2753   b = current_binding_level;
2754   while (b->tag_transparent)
2755     b = b->level_chain;
2756   tags = b->tags;
2757   while (tags)
2758     {
2759       /* A NULL purpose means we have already processed all tags
2760          from here to the end of the list.  */
2761       if (TREE_PURPOSE (tags) == NULL_TREE)
2762         break;
2763       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2764         TREE_PURPOSE (tags) = NULL_TREE;
2765       tags = TREE_CHAIN (tags);
2766     }
2767   last_cnt = anon_cnt;
2768 }
2769 \f
2770 /* Subroutine of duplicate_decls: return truthvalue of whether
2771    or not types of these decls match.
2772
2773    For C++, we must compare the parameter list so that `int' can match
2774    `int&' in a parameter position, but `int&' is not confused with
2775    `const int&'.  */
2776
2777 int
2778 decls_match (tree newdecl, tree olddecl)
2779 {
2780   int types_match;
2781
2782   if (newdecl == olddecl)
2783     return 1;
2784
2785   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2786     /* If the two DECLs are not even the same kind of thing, we're not
2787        interested in their types.  */
2788     return 0;
2789
2790   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2791     {
2792       tree f1 = TREE_TYPE (newdecl);
2793       tree f2 = TREE_TYPE (olddecl);
2794       tree p1 = TYPE_ARG_TYPES (f1);
2795       tree p2 = TYPE_ARG_TYPES (f2);
2796
2797       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2798           && ! (DECL_EXTERN_C_P (newdecl)
2799                 && DECL_EXTERN_C_P (olddecl)))
2800         return 0;
2801
2802       if (TREE_CODE (f1) != TREE_CODE (f2))
2803         return 0;
2804
2805       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2806         {
2807           if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2808               && (DECL_BUILT_IN (olddecl)
2809 #ifndef NO_IMPLICIT_EXTERN_C
2810                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2811                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2812 #endif
2813               ))
2814             {
2815               types_match = self_promoting_args_p (p1);
2816               if (p1 == void_list_node)
2817                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2818             }
2819 #ifndef NO_IMPLICIT_EXTERN_C
2820           else if (p1 == NULL_TREE
2821                    && (DECL_EXTERN_C_P (olddecl)
2822                        && DECL_IN_SYSTEM_HEADER (olddecl)
2823                        && !DECL_CLASS_SCOPE_P (olddecl))
2824                    && (DECL_EXTERN_C_P (newdecl)
2825                        && DECL_IN_SYSTEM_HEADER (newdecl)
2826                        && !DECL_CLASS_SCOPE_P (newdecl)))
2827             {
2828               types_match = self_promoting_args_p (p2);
2829               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2830             }
2831 #endif
2832           else
2833             types_match = compparms (p1, p2);
2834         }
2835       else
2836         types_match = 0;
2837     }
2838   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2839     {
2840       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2841                                 DECL_TEMPLATE_PARMS (olddecl)))
2842         return 0;
2843
2844       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2845           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2846         return 0;
2847
2848       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2849         types_match = 1;
2850       else
2851         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2852                                    DECL_TEMPLATE_RESULT (newdecl));
2853     }
2854   else
2855     {
2856       if (TREE_TYPE (newdecl) == error_mark_node)
2857         types_match = TREE_TYPE (olddecl) == error_mark_node;
2858       else if (TREE_TYPE (olddecl) == NULL_TREE)
2859         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2860       else if (TREE_TYPE (newdecl) == NULL_TREE)
2861         types_match = 0;
2862       else
2863         types_match = comptypes (TREE_TYPE (newdecl),
2864                                  TREE_TYPE (olddecl),
2865                                  COMPARE_REDECLARATION);
2866     }
2867
2868   return types_match;
2869 }
2870
2871 /* If NEWDECL is `static' and an `extern' was seen previously,
2872    warn about it.  OLDDECL is the previous declaration.
2873
2874    Note that this does not apply to the C++ case of declaring
2875    a variable `extern const' and then later `const'.
2876
2877    Don't complain about built-in functions, since they are beyond
2878    the user's control.  */
2879
2880 static void
2881 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2882 {
2883   static const char *const explicit_extern_static_warning
2884     = "`%D' was declared `extern' and later `static'";
2885   static const char *const implicit_extern_static_warning
2886     = "`%D' was declared implicitly `extern' and later `static'";
2887
2888   tree name;
2889
2890   if (TREE_CODE (newdecl) == TYPE_DECL
2891       || TREE_CODE (newdecl) == TEMPLATE_DECL
2892       || TREE_CODE (newdecl) == CONST_DECL)
2893     return;
2894
2895   /* Don't get confused by static member functions; that's a different
2896      use of `static'.  */
2897   if (TREE_CODE (newdecl) == FUNCTION_DECL
2898       && DECL_STATIC_FUNCTION_P (newdecl))
2899     return;
2900
2901   /* If the old declaration was `static', or the new one isn't, then
2902      then everything is OK.  */
2903   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2904     return;
2905
2906   /* It's OK to declare a builtin function as `static'.  */
2907   if (TREE_CODE (olddecl) == FUNCTION_DECL
2908       && DECL_ARTIFICIAL (olddecl))
2909     return;
2910
2911   name = DECL_ASSEMBLER_NAME (newdecl);
2912   pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2913               ? implicit_extern_static_warning
2914               : explicit_extern_static_warning, newdecl);
2915   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2916 }
2917
2918 /* Handle when a new declaration NEWDECL has the same name as an old
2919    one OLDDECL in the same binding contour.  Prints an error message
2920    if appropriate.
2921
2922    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2923    Otherwise, return 0.  */
2924
2925 int
2926 duplicate_decls (tree newdecl, tree olddecl)
2927 {
2928   unsigned olddecl_uid = DECL_UID (olddecl);
2929   int olddecl_friend = 0, types_match = 0;
2930   int new_defines_function = 0;
2931
2932   if (newdecl == olddecl)
2933     return 1;
2934
2935   types_match = decls_match (newdecl, olddecl);
2936
2937   /* If either the type of the new decl or the type of the old decl is an
2938      error_mark_node, then that implies that we have already issued an
2939      error (earlier) for some bogus type specification, and in that case,
2940      it is rather pointless to harass the user with yet more error message
2941      about the same declaration, so just pretend the types match here.  */
2942   if (TREE_TYPE (newdecl) == error_mark_node
2943       || TREE_TYPE (olddecl) == error_mark_node)
2944     types_match = 1;
2945
2946   if (DECL_P (olddecl)
2947       && TREE_CODE (newdecl) == FUNCTION_DECL
2948       && TREE_CODE (olddecl) == FUNCTION_DECL
2949       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2950     {
2951       if (DECL_DECLARED_INLINE_P (newdecl)
2952           && DECL_UNINLINABLE (newdecl)
2953           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2954         /* Already warned elsewhere.  */;
2955       else if (DECL_DECLARED_INLINE_P (olddecl)
2956                && DECL_UNINLINABLE (olddecl)
2957                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2958         /* Already warned.  */;
2959       else if (DECL_DECLARED_INLINE_P (newdecl)
2960                && DECL_UNINLINABLE (olddecl)
2961                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2962         {
2963           warning_with_decl (newdecl,
2964                              "function `%s' redeclared as inline");
2965           warning_with_decl (olddecl,
2966                              "previous declaration of function `%s' with attribute noinline");
2967         }
2968       else if (DECL_DECLARED_INLINE_P (olddecl)
2969                && DECL_UNINLINABLE (newdecl)
2970                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2971         {
2972           warning_with_decl (newdecl,
2973                              "function `%s' redeclared with attribute noinline");
2974           warning_with_decl (olddecl,
2975                              "previous declaration of function `%s' was inline");
2976         }
2977     }
2978
2979   /* Check for redeclaration and other discrepancies.  */
2980   if (TREE_CODE (olddecl) == FUNCTION_DECL
2981       && DECL_ARTIFICIAL (olddecl))
2982     {
2983       if (TREE_CODE (newdecl) != FUNCTION_DECL)
2984         {
2985           /* Avoid warnings redeclaring anticipated built-ins.  */
2986           if (DECL_ANTICIPATED (olddecl))
2987             return 0;
2988
2989           /* If you declare a built-in or predefined function name as static,
2990              the old definition is overridden, but optionally warn this was a
2991              bad choice of name.  */
2992           if (! TREE_PUBLIC (newdecl))
2993             {
2994               if (warn_shadow)
2995                 warning ("shadowing %s function `%#D'",
2996                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2997                             olddecl);
2998               /* Discard the old built-in function.  */
2999               return 0;
3000             }
3001           /* If the built-in is not ansi, then programs can override
3002              it even globally without an error.  */
3003           else if (! DECL_BUILT_IN (olddecl))
3004             warning ("library function `%#D' redeclared as non-function `%#D'",
3005                         olddecl, newdecl);
3006           else
3007             {
3008               error ("declaration of `%#D'", newdecl);
3009               error ("conflicts with built-in declaration `%#D'",
3010                         olddecl);
3011             }
3012           return 0;
3013         }
3014       else if (!types_match)
3015         {
3016           /* Avoid warnings redeclaring anticipated built-ins.  */
3017           if (DECL_ANTICIPATED (olddecl))
3018             ;  /* Do nothing yet.  */
3019           else if ((DECL_EXTERN_C_P (newdecl)
3020                && DECL_EXTERN_C_P (olddecl))
3021               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3022                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3023             {
3024               /* A near match; override the builtin.  */
3025
3026               if (TREE_PUBLIC (newdecl))
3027                 {
3028                   warning ("new declaration `%#D'", newdecl);
3029                   warning ("ambiguates built-in declaration `%#D'",
3030                               olddecl);
3031                 }
3032               else if (warn_shadow)
3033                 warning ("shadowing %s function `%#D'",
3034                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3035                             olddecl);
3036             }
3037           else
3038             /* Discard the old built-in function.  */
3039             return 0;
3040
3041           /* Replace the old RTL to avoid problems with inlining.  */
3042           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3043         }
3044
3045       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3046         {
3047           /* If a builtin function is redeclared as `static', merge
3048              the declarations, but make the original one static.  */
3049           DECL_THIS_STATIC (olddecl) = 1;
3050           TREE_PUBLIC (olddecl) = 0;
3051
3052           /* Make the old declaration consistent with the new one so
3053              that all remnants of the builtin-ness of this function
3054              will be banished.  */
3055           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3056           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3057         }
3058     }
3059   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3060     {
3061       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3062            && TREE_CODE (newdecl) != TYPE_DECL
3063            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3064                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3065           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3066               && TREE_CODE (olddecl) != TYPE_DECL
3067               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3068                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3069                         == TYPE_DECL))))
3070         {
3071           /* We do nothing special here, because C++ does such nasty
3072              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
3073              get shadowed, and know that if we need to find a TYPE_DECL
3074              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3075              slot of the identifier.  */
3076           return 0;
3077         }
3078
3079       if ((TREE_CODE (newdecl) == FUNCTION_DECL
3080            && DECL_FUNCTION_TEMPLATE_P (olddecl))
3081           || (TREE_CODE (olddecl) == FUNCTION_DECL
3082               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3083         return 0;
3084
3085       error ("`%#D' redeclared as different kind of symbol", newdecl);
3086       if (TREE_CODE (olddecl) == TREE_LIST)
3087         olddecl = TREE_VALUE (olddecl);
3088       cp_error_at ("previous declaration of `%#D'", olddecl);
3089
3090       /* New decl is completely inconsistent with the old one =>
3091          tell caller to replace the old one.  */
3092
3093       return 0;
3094     }
3095   else if (!types_match)
3096     {
3097       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3098         /* These are certainly not duplicate declarations; they're
3099            from different scopes.  */
3100         return 0;
3101
3102       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3103         {
3104           /* The name of a class template may not be declared to refer to
3105              any other template, class, function, object, namespace, value,
3106              or type in the same scope.  */
3107           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3108               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3109             {
3110               error ("declaration of template `%#D'", newdecl);
3111               cp_error_at ("conflicts with previous declaration `%#D'",
3112                            olddecl);
3113             }
3114           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3115                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3116                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3117                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3118                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3119                                            DECL_TEMPLATE_PARMS (olddecl))
3120                    /* Template functions can be disambiguated by
3121                       return type.  */
3122                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3123                                    TREE_TYPE (TREE_TYPE (olddecl))))
3124             {
3125               error ("new declaration `%#D'", newdecl);
3126               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3127             }
3128           return 0;
3129         }
3130       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3131         {
3132           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3133             {
3134               error ("declaration of C function `%#D' conflicts with",
3135                         newdecl);
3136               cp_error_at ("previous declaration `%#D' here", olddecl);
3137             }
3138           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3139                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3140             {
3141               error ("new declaration `%#D'", newdecl);
3142               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3143             }
3144           else
3145             return 0;
3146         }
3147
3148       /* Already complained about this, so don't do so again.  */
3149       else if (current_class_type == NULL_TREE
3150           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3151         {
3152           error ("conflicting types for `%#D'", newdecl);
3153           cp_error_at ("previous declaration as `%#D'", olddecl);
3154         }
3155     }
3156   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3157             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3158                  && (!DECL_TEMPLATE_INFO (newdecl)
3159                      || (DECL_TI_TEMPLATE (newdecl)
3160                          != DECL_TI_TEMPLATE (olddecl))))
3161                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3162                     && (!DECL_TEMPLATE_INFO (olddecl)
3163                         || (DECL_TI_TEMPLATE (olddecl)
3164                             != DECL_TI_TEMPLATE (newdecl))))))
3165     /* It's OK to have a template specialization and a non-template
3166        with the same type, or to have specializations of two
3167        different templates with the same type.  Note that if one is a
3168        specialization, and the other is an instantiation of the same
3169        template, that we do not exit at this point.  That situation
3170        can occur if we instantiate a template class, and then
3171        specialize one of its methods.  This situation is valid, but
3172        the declarations must be merged in the usual way.  */
3173     return 0;
3174   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3175            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3176                 && !DECL_USE_TEMPLATE (newdecl))
3177                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3178                    && !DECL_USE_TEMPLATE (olddecl))))
3179     /* One of the declarations is a template instantiation, and the
3180        other is not a template at all.  That's OK.  */
3181     return 0;
3182   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3183            && DECL_NAMESPACE_ALIAS (newdecl)
3184            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3185     /* Redeclaration of namespace alias, ignore it.  */
3186     return 1;
3187   else
3188     {
3189       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3190       if (errmsg)
3191         {
3192           error (errmsg, newdecl);
3193           if (DECL_NAME (olddecl) != NULL_TREE)
3194             cp_error_at ((DECL_INITIAL (olddecl)
3195                           && namespace_bindings_p ())
3196                          ? "`%#D' previously defined here"
3197                          : "`%#D' previously declared here", olddecl);
3198           return 0;
3199         }
3200       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3201                && DECL_INITIAL (olddecl) != NULL_TREE
3202                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3203                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3204         {
3205           /* Prototype decl follows defn w/o prototype.  */
3206           cp_warning_at ("prototype for `%#D'", newdecl);
3207           cp_warning_at ("follows non-prototype definition here", olddecl);
3208         }
3209       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3210                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3211         {
3212           /* extern "C" int foo ();
3213              int foo () { bar (); }
3214              is OK.  */
3215           if (current_lang_depth () == 0)
3216             SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3217           else
3218             {
3219               cp_error_at ("previous declaration of `%#D' with %L linkage",
3220                            olddecl, DECL_LANGUAGE (olddecl));
3221               error ("conflicts with new declaration with %L linkage",
3222                         DECL_LANGUAGE (newdecl));
3223             }
3224         }
3225
3226       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3227         ;
3228       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3229         {
3230           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3231           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3232           int i = 1;
3233
3234           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3235             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3236
3237           for (; t1 && t1 != void_list_node;
3238                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3239             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3240               {
3241                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3242                                            TREE_PURPOSE (t2)))
3243                   {
3244                     pedwarn ("default argument given for parameter %d of `%#D'",
3245                              i, newdecl);
3246                     cp_pedwarn_at ("after previous specification in `%#D'",
3247                                    olddecl);
3248                   }
3249                 else
3250                   {
3251                     error ("default argument given for parameter %d of `%#D'",
3252                               i, newdecl);
3253                     cp_error_at ("after previous specification in `%#D'",
3254                                  olddecl);
3255                   }
3256               }
3257
3258           if (DECL_DECLARED_INLINE_P (newdecl) 
3259               && ! DECL_DECLARED_INLINE_P (olddecl)
3260               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3261             {
3262               warning ("`%#D' was used before it was declared inline",
3263                           newdecl);
3264               cp_warning_at ("previous non-inline declaration here",
3265                              olddecl);
3266             }
3267         }
3268     }
3269
3270   /* Do not merge an implicit typedef with an explicit one.  In:
3271
3272        class A;
3273        ...
3274        typedef class A A __attribute__ ((foo));
3275
3276      the attribute should apply only to the typedef.  */
3277   if (TREE_CODE (olddecl) == TYPE_DECL
3278       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3279           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3280     return 0;
3281
3282   /* If new decl is `static' and an `extern' was seen previously,
3283      warn about it.  */
3284   warn_extern_redeclared_static (newdecl, olddecl);
3285
3286   /* We have committed to returning 1 at this point.  */
3287   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3288     {
3289       /* Now that functions must hold information normally held
3290          by field decls, there is extra work to do so that
3291          declaration information does not get destroyed during
3292          definition.  */
3293       if (DECL_VINDEX (olddecl))
3294         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3295       if (DECL_CONTEXT (olddecl))
3296         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3297       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3298       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3299       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3300       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3301       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3302       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3303       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3304         SET_OVERLOADED_OPERATOR_CODE
3305           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3306       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3307
3308       /* Optionally warn about more than one declaration for the same
3309          name, but don't warn about a function declaration followed by a
3310          definition.  */
3311       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3312           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3313           /* Don't warn about extern decl followed by definition.  */
3314           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3315           /* Don't warn about friends, let add_friend take care of it.  */
3316           && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3317         {
3318           warning ("redundant redeclaration of `%D' in same scope", newdecl);
3319           cp_warning_at ("previous declaration of `%D'", olddecl);
3320         }
3321     }
3322
3323   /* Deal with C++: must preserve virtual function table size.  */
3324   if (TREE_CODE (olddecl) == TYPE_DECL)
3325     {
3326       register tree newtype = TREE_TYPE (newdecl);
3327       register tree oldtype = TREE_TYPE (olddecl);
3328
3329       if (newtype != error_mark_node && oldtype != error_mark_node
3330           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3331         CLASSTYPE_FRIEND_CLASSES (newtype)
3332           = CLASSTYPE_FRIEND_CLASSES (oldtype);
3333 \
3334       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3335     }
3336
3337   /* Copy all the DECL_... slots specified in the new decl
3338      except for any that we copy here from the old type.  */
3339   DECL_ATTRIBUTES (newdecl)
3340     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3341
3342   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3343     {
3344       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3345       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3346         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3347                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3348
3349       /* If the new declaration is a definition, update the file and
3350          line information on the declaration.  */
3351       if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3352           && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3353         {
3354           DECL_SOURCE_LOCATION (olddecl) 
3355             = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3356             = DECL_SOURCE_LOCATION (newdecl);
3357         }
3358
3359       return 1;
3360     }
3361
3362   if (types_match)
3363     {
3364       /* Automatically handles default parameters.  */
3365       tree oldtype = TREE_TYPE (olddecl);
3366       tree newtype;
3367
3368       /* Merge the data types specified in the two decls.  */
3369       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3370
3371       /* If merge_types produces a non-typedef type, just use the old type.  */
3372       if (TREE_CODE (newdecl) == TYPE_DECL
3373           && newtype == DECL_ORIGINAL_TYPE (newdecl))
3374         newtype = oldtype;
3375
3376       if (TREE_CODE (newdecl) == VAR_DECL)
3377         {
3378           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3379           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3380         }
3381
3382       /* Do this after calling `merge_types' so that default
3383          parameters don't confuse us.  */
3384       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3385           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3386               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3387         {
3388           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3389                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3390           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3391                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3392
3393           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3394               && DECL_SOURCE_LINE (olddecl) != 0
3395               && flag_exceptions
3396               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3397                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3398             {
3399               error ("declaration of `%F' throws different exceptions",
3400                         newdecl);
3401               cp_error_at ("than previous declaration `%F'", olddecl);
3402             }
3403         }
3404       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3405
3406       /* Lay the type out, unless already done.  */
3407       if (! same_type_p (newtype, oldtype)
3408           && TREE_TYPE (newdecl) != error_mark_node
3409           && !(processing_template_decl && uses_template_parms (newdecl)))
3410         layout_type (TREE_TYPE (newdecl));
3411
3412       if ((TREE_CODE (newdecl) == VAR_DECL
3413            || TREE_CODE (newdecl) == PARM_DECL
3414            || TREE_CODE (newdecl) == RESULT_DECL
3415            || TREE_CODE (newdecl) == FIELD_DECL
3416            || TREE_CODE (newdecl) == TYPE_DECL)
3417           && !(processing_template_decl && uses_template_parms (newdecl)))
3418         layout_decl (newdecl, 0);
3419
3420       /* Merge the type qualifiers.  */
3421       if (TREE_READONLY (newdecl))
3422         TREE_READONLY (olddecl) = 1;
3423       if (TREE_THIS_VOLATILE (newdecl))
3424         TREE_THIS_VOLATILE (olddecl) = 1;
3425
3426       /* Merge the initialization information.  */
3427       if (DECL_INITIAL (newdecl) == NULL_TREE
3428           && DECL_INITIAL (olddecl) != NULL_TREE)
3429         {
3430           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3431           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3432           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3433               && DECL_LANG_SPECIFIC (newdecl)
3434               && DECL_LANG_SPECIFIC (olddecl))
3435             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3436         }
3437
3438       /* Merge the section attribute.
3439          We want to issue an error if the sections conflict but that must be
3440          done later in decl_attributes since we are called before attributes
3441          are assigned.  */
3442       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3443         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3444
3445       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3446         {
3447           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3448             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3449           DECL_NO_LIMIT_STACK (newdecl)
3450             |= DECL_NO_LIMIT_STACK (olddecl);
3451           /* Keep the old RTL.  */
3452           COPY_DECL_RTL (olddecl, newdecl);
3453         }
3454       else if (TREE_CODE (newdecl) == VAR_DECL 
3455                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3456         {
3457           /* Keep the old RTL.  We cannot keep the old RTL if the old
3458              declaration was for an incomplete object and the new
3459              declaration is not since many attributes of the RTL will
3460              change.  */
3461           COPY_DECL_RTL (olddecl, newdecl);
3462         }
3463     }
3464   /* If cannot merge, then use the new type and qualifiers,
3465      and don't preserve the old rtl.  */
3466   else
3467     {
3468       /* Clean out any memory we had of the old declaration.  */
3469       tree oldstatic = value_member (olddecl, static_aggregates);
3470       if (oldstatic)
3471         TREE_VALUE (oldstatic) = error_mark_node;
3472
3473       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3474       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3475       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3476       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3477     }
3478
3479   /* Merge the storage class information.  */
3480   merge_weak (newdecl, olddecl);
3481
3482   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3483   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3484   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3485   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3486   if (! DECL_EXTERNAL (olddecl))
3487     DECL_EXTERNAL (newdecl) = 0;
3488
3489   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3490     {
3491       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3492       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3493       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3494       DECL_TEMPLATE_INSTANTIATED (newdecl)
3495         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3496       /* Don't really know how much of the language-specific
3497          values we should copy from old to new.  */
3498       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3499       DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 
3500         DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3501       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3502       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3503       DECL_INITIALIZED_IN_CLASS_P (newdecl)
3504         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3505       olddecl_friend = DECL_FRIEND_P (olddecl);
3506
3507       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3508       if (TREE_CODE (newdecl) == FUNCTION_DECL
3509           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3510         {
3511           DECL_BEFRIENDING_CLASSES (newdecl)
3512             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3513                        DECL_BEFRIENDING_CLASSES (olddecl));
3514           DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3515         }
3516     }
3517
3518   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3519     {
3520       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3521           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3522         {
3523           /* If newdecl is not a specialization, then it is not a
3524              template-related function at all.  And that means that we
3525              shoud have exited above, returning 0.  */
3526           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3527                               0);
3528
3529           if (TREE_USED (olddecl))
3530             /* From [temp.expl.spec]:
3531
3532                If a template, a member template or the member of a class
3533                template is explicitly specialized then that
3534                specialization shall be declared before the first use of
3535                that specialization that would cause an implicit
3536                instantiation to take place, in every translation unit in
3537                which such a use occurs.  */
3538             error ("explicit specialization of %D after first use",
3539                       olddecl);
3540
3541           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3542
3543           /* [temp.expl.spec/14] We don't inline explicit specialization
3544              just because the primary template says so.  */
3545         }
3546       else
3547         {
3548           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3549             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3550
3551           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3552
3553           /* If either decl says `inline', this fn is inline, unless 
3554              its definition was passed already.  */
3555           if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3556             DECL_INLINE (olddecl) = 1;
3557           DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3558
3559           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3560             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3561         }
3562
3563       /* Preserve abstractness on cloned [cd]tors.  */
3564       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3565
3566       if (! types_match)
3567         {
3568           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3569           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3570           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3571         }
3572       if (! types_match || new_defines_function)
3573         {
3574           /* These need to be copied so that the names are available.
3575              Note that if the types do match, we'll preserve inline
3576              info and other bits, but if not, we won't.  */
3577           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3578           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3579         }
3580       if (new_defines_function)
3581         /* If defining a function declared with other language
3582            linkage, use the previously declared language linkage.  */
3583         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3584       else if (types_match)
3585         {
3586           /* If redeclaring a builtin function, and not a definition,
3587              it stays built in.  */
3588           if (DECL_BUILT_IN (olddecl))
3589             {
3590               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3591               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3592               /* If we're keeping the built-in definition, keep the rtl,
3593                  regardless of declaration matches.  */
3594               SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3595             }
3596           else
3597             DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3598
3599           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3600           /* Don't clear out the arguments if we're redefining a function.  */
3601           if (DECL_ARGUMENTS (olddecl))
3602             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3603         }
3604     }
3605   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3606     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3607
3608   /* Now preserve various other info from the definition.  */
3609   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3610   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3611   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3612   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3613
3614   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3615     {
3616       int function_size;
3617
3618       function_size = sizeof (struct tree_decl);
3619
3620       memcpy ((char *) olddecl + sizeof (struct tree_common),
3621               (char *) newdecl + sizeof (struct tree_common),
3622               function_size - sizeof (struct tree_common));
3623
3624       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3625         {
3626           /* If newdecl is a template instantiation, it is possible that
3627              the following sequence of events has occurred:
3628
3629              o A friend function was declared in a class template.  The
3630              class template was instantiated.
3631
3632              o The instantiation of the friend declaration was
3633              recorded on the instantiation list, and is newdecl.
3634
3635              o Later, however, instantiate_class_template called pushdecl
3636              on the newdecl to perform name injection.  But, pushdecl in
3637              turn called duplicate_decls when it discovered that another
3638              declaration of a global function with the same name already
3639              existed.
3640
3641              o Here, in duplicate_decls, we decided to clobber newdecl.
3642
3643              If we're going to do that, we'd better make sure that
3644              olddecl, and not newdecl, is on the list of
3645              instantiations so that if we try to do the instantiation
3646              again we won't get the clobbered declaration.  */
3647
3648           tree tmpl = DECL_TI_TEMPLATE (newdecl);
3649           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3650
3651           for (; decls; decls = TREE_CHAIN (decls))
3652             if (TREE_VALUE (decls) == newdecl)
3653               TREE_VALUE (decls) = olddecl;
3654         }
3655     }
3656   else
3657     {
3658       memcpy ((char *) olddecl + sizeof (struct tree_common),
3659               (char *) newdecl + sizeof (struct tree_common),
3660               sizeof (struct tree_decl) - sizeof (struct tree_common)
3661               + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3662     }
3663
3664   DECL_UID (olddecl) = olddecl_uid;
3665   if (olddecl_friend)
3666     DECL_FRIEND_P (olddecl) = 1;
3667
3668   /* NEWDECL contains the merged attribute lists.
3669      Update OLDDECL to be the same.  */
3670   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3671
3672   return 1;
3673 }
3674
3675 /* Record a decl-node X as belonging to the current lexical scope.
3676    Check for errors (such as an incompatible declaration for the same
3677    name already seen in the same scope).
3678
3679    Returns either X or an old decl for the same name.
3680    If an old decl is returned, it may have been smashed
3681    to agree with what X says.  */
3682
3683 tree
3684 pushdecl (tree x)
3685 {
3686   register tree t;
3687   register tree name;
3688   int need_new_binding;
3689
3690   /* We shouldn't be calling pushdecl when we're generating RTL for a
3691      function that we already did semantic analysis on previously.  */
3692   my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3693                       19990913);
3694
3695   need_new_binding = 1;
3696
3697   if (DECL_TEMPLATE_PARM_P (x))
3698     /* Template parameters have no context; they are not X::T even
3699        when declared within a class or namespace.  */
3700     ;
3701   else
3702     {
3703       if (current_function_decl && x != current_function_decl
3704           /* A local declaration for a function doesn't constitute
3705              nesting.  */
3706           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3707           /* A local declaration for an `extern' variable is in the
3708              scope of the current namespace, not the current
3709              function.  */
3710           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3711           && !DECL_CONTEXT (x))
3712         DECL_CONTEXT (x) = current_function_decl;
3713
3714       /* If this is the declaration for a namespace-scope function,
3715          but the declaration itself is in a local scope, mark the
3716          declaration.  */
3717       if (TREE_CODE (x) == FUNCTION_DECL
3718           && DECL_NAMESPACE_SCOPE_P (x)
3719           && current_function_decl
3720           && x != current_function_decl)
3721         DECL_LOCAL_FUNCTION_P (x) = 1;
3722     }
3723
3724   name = DECL_NAME (x);
3725   if (name)
3726     {
3727       int different_binding_level = 0;
3728
3729       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3730         name = TREE_OPERAND (name, 0);
3731
3732       /* In case this decl was explicitly namespace-qualified, look it
3733          up in its namespace context.  */
3734       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3735           && namespace_bindings_p ())
3736         t = namespace_binding (name, DECL_CONTEXT (x));
3737       else
3738         t = lookup_name_current_level (name);
3739
3740       /* [basic.link] If there is a visible declaration of an entity
3741          with linkage having the same name and type, ignoring entities
3742          declared outside the innermost enclosing namespace scope, the
3743          block scope declaration declares that same entity and
3744          receives the linkage of the previous declaration.  */
3745       if (! t && current_function_decl && x != current_function_decl
3746           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3747           && DECL_EXTERNAL (x))
3748         {
3749           /* Look in block scope.  */
3750           t = IDENTIFIER_VALUE (name);
3751           /* Or in the innermost namespace.  */
3752           if (! t)
3753             t = namespace_binding (name, DECL_CONTEXT (x));
3754           /* Does it have linkage?  Note that if this isn't a DECL, it's an
3755              OVERLOAD, which is OK.  */
3756           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3757             t = NULL_TREE;
3758           if (t)
3759             different_binding_level = 1;
3760         }
3761
3762       /* If we are declaring a function, and the result of name-lookup
3763          was an OVERLOAD, look for an overloaded instance that is
3764          actually the same as the function we are declaring.  (If
3765          there is one, we have to merge our declaration with the
3766          previous declaration.)  */
3767       if (t && TREE_CODE (t) == OVERLOAD)
3768         {
3769           tree match;
3770
3771           if (TREE_CODE (x) == FUNCTION_DECL)
3772             for (match = t; match; match = OVL_NEXT (match))
3773               {
3774                 if (decls_match (OVL_CURRENT (match), x))
3775                   break;
3776               }
3777           else
3778             /* Just choose one.  */
3779             match = t;
3780
3781           if (match)
3782             t = OVL_CURRENT (match);
3783           else
3784             t = NULL_TREE;
3785         }
3786
3787       if (t == error_mark_node)
3788         {
3789           /* error_mark_node is 0 for a while during initialization!  */
3790           t = NULL_TREE;
3791           cp_error_at ("`%#D' used prior to declaration", x);
3792         }
3793       else if (t != NULL_TREE)
3794         {
3795           if (different_binding_level)
3796             {
3797               if (decls_match (x, t))
3798                 /* The standard only says that the local extern
3799                    inherits linkage from the previous decl; in
3800                    particular, default args are not shared.  It would
3801                    be nice to propagate inlining info, though.  FIXME.  */
3802                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3803             }
3804           else if (TREE_CODE (t) == PARM_DECL)
3805             {
3806               if (DECL_CONTEXT (t) == NULL_TREE)
3807                 /* This is probaby caused by too many errors, but calling
3808                    abort will say that if errors have occurred.  */
3809                 abort ();
3810
3811               /* Check for duplicate params.  */
3812               if (duplicate_decls (x, t))
3813                 return t;
3814             }
3815           else if ((DECL_EXTERN_C_FUNCTION_P (x)
3816                     || DECL_FUNCTION_TEMPLATE_P (x))
3817                    && is_overloaded_fn (t))
3818             /* Don't do anything just yet.  */;
3819           else if (t == wchar_decl_node)
3820             {
3821               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3822                 pedwarn ("redeclaration of `wchar_t' as `%T'",
3823                             TREE_TYPE (x));
3824
3825               /* Throw away the redeclaration.  */
3826               return t;
3827             }
3828           else if (TREE_CODE (t) != TREE_CODE (x))
3829             {
3830               if (duplicate_decls (x, t))
3831                 return t;
3832             }
3833           else if (duplicate_decls (x, t))
3834             {
3835               if (TREE_CODE (t) == TYPE_DECL)
3836                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3837               else if (TREE_CODE (t) == FUNCTION_DECL)
3838                 check_default_args (t);
3839
3840               return t;
3841             }
3842           else if (DECL_MAIN_P (x))
3843             {
3844               /* A redeclaration of main, but not a duplicate of the
3845                  previous one.
3846
3847                  [basic.start.main]
3848
3849                  This function shall not be overloaded.  */
3850               cp_error_at ("invalid redeclaration of `%D'", t);
3851               error ("as `%D'", x);
3852               /* We don't try to push this declaration since that
3853                  causes a crash.  */
3854               return x;
3855             }
3856         }
3857
3858       check_template_shadow (x);
3859
3860       /* If this is a function conjured up by the backend, massage it
3861          so it looks friendly.  */
3862       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3863         {
3864           retrofit_lang_decl (x);
3865           SET_DECL_LANGUAGE (x, lang_c);
3866         }
3867
3868       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3869         {
3870           t = push_overloaded_decl (x, PUSH_LOCAL);
3871           if (t != x)
3872             return t;
3873           if (!namespace_bindings_p ())
3874             /* We do not need to create a binding for this name;
3875                push_overloaded_decl will have already done so if
3876                necessary.  */
3877             need_new_binding = 0;
3878         }
3879       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3880         {
3881           t = push_overloaded_decl (x, PUSH_GLOBAL);
3882           if (t == x)
3883             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3884           return t;
3885         }
3886
3887       /* If declaring a type as a typedef, copy the type (unless we're
3888          at line 0), and install this TYPE_DECL as the new type's typedef
3889          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
3890       if (TREE_CODE (x) == TYPE_DECL)
3891         {
3892           tree type = TREE_TYPE (x);
3893           if (DECL_SOURCE_LINE (x) == 0)
3894             {
3895               if (TYPE_NAME (type) == 0)
3896                 TYPE_NAME (type) = x;
3897             }
3898           else if (type != error_mark_node && TYPE_NAME (type) != x
3899                    /* We don't want to copy the type when all we're
3900                       doing is making a TYPE_DECL for the purposes of
3901                       inlining.  */
3902                    && (!TYPE_NAME (type)
3903                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3904             {
3905               DECL_ORIGINAL_TYPE (x) = type;
3906               type = build_type_copy (type);
3907               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3908               TYPE_NAME (type) = x;
3909               TREE_TYPE (x) = type;
3910             }
3911
3912           if (type != error_mark_node
3913               && TYPE_NAME (type)
3914               && TYPE_IDENTIFIER (type))
3915             set_identifier_type_value_with_scope (DECL_NAME (x), type,
3916                                                   current_binding_level);
3917
3918         }
3919
3920       /* Multiple external decls of the same identifier ought to match.
3921
3922          We get warnings about inline functions where they are defined.
3923          We get warnings about other functions from push_overloaded_decl.
3924
3925          Avoid duplicate warnings where they are used.  */
3926       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3927         {
3928           tree decl;
3929
3930           decl = IDENTIFIER_NAMESPACE_VALUE (name);
3931           if (decl && TREE_CODE (decl) == OVERLOAD)
3932             decl = OVL_FUNCTION (decl);
3933
3934           if (decl && decl != error_mark_node
3935               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3936               /* If different sort of thing, we already gave an error.  */
3937               && TREE_CODE (decl) == TREE_CODE (x)
3938               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3939             {
3940               pedwarn ("type mismatch with previous external decl", x);
3941               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3942             }
3943         }
3944
3945       /* This name is new in its binding level.
3946          Install the new declaration and return it.  */
3947       if (namespace_bindings_p ())
3948         {
3949           /* Install a global value.  */
3950
3951           /* If the first global decl has external linkage,
3952              warn if we later see static one.  */
3953           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3954             TREE_PUBLIC (name) = 1;
3955
3956           /* Bind the name for the entity.  */
3957           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3958                 && t != NULL_TREE)
3959               && (TREE_CODE (x) == TYPE_DECL
3960                   || TREE_CODE (x) == VAR_DECL
3961                   || TREE_CODE (x) == ALIAS_DECL
3962                   || TREE_CODE (x) == NAMESPACE_DECL
3963                   || TREE_CODE (x) == CONST_DECL
3964                   || TREE_CODE (x) == TEMPLATE_DECL))
3965             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3966
3967           /* Don't forget if the function was used via an implicit decl.  */
3968           if (IDENTIFIER_IMPLICIT_DECL (name)
3969               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3970             TREE_USED (x) = 1;
3971
3972           /* Don't forget if its address was taken in that way.  */
3973           if (IDENTIFIER_IMPLICIT_DECL (name)
3974               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3975             TREE_ADDRESSABLE (x) = 1;
3976
3977           /* Warn about mismatches against previous implicit decl.  */
3978           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3979               /* If this real decl matches the implicit, don't complain.  */
3980               && ! (TREE_CODE (x) == FUNCTION_DECL
3981                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3982             warning
3983               ("`%D' was previously implicitly declared to return `int'", x);
3984
3985           /* If new decl is `static' and an `extern' was seen previously,
3986              warn about it.  */
3987           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3988             warn_extern_redeclared_static (x, t);
3989         }
3990       else
3991         {
3992           /* Here to install a non-global value.  */
3993           tree oldlocal = IDENTIFIER_VALUE (name);
3994           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3995
3996           if (need_new_binding)
3997             {
3998               push_local_binding (name, x, 0);
3999               /* Because push_local_binding will hook X on to the
4000                  current_binding_level's name list, we don't want to
4001                  do that again below.  */
4002               need_new_binding = 0;
4003             }
4004
4005           /* If this is a TYPE_DECL, push it into the type value slot.  */
4006           if (TREE_CODE (x) == TYPE_DECL)
4007             set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4008                                                   current_binding_level);
4009
4010           /* Clear out any TYPE_DECL shadowed by a namespace so that
4011              we won't think this is a type.  The C struct hack doesn't
4012              go through namespaces.  */
4013           if (TREE_CODE (x) == NAMESPACE_DECL)
4014             set_identifier_type_value_with_scope (name, NULL_TREE,
4015                                                   current_binding_level);
4016
4017           if (oldlocal)
4018             {
4019               tree d = oldlocal;
4020
4021               while (oldlocal
4022                      && TREE_CODE (oldlocal) == VAR_DECL
4023                      && DECL_DEAD_FOR_LOCAL (oldlocal))
4024                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4025
4026               if (oldlocal == NULL_TREE)
4027                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4028             }
4029
4030           /* If this is an extern function declaration, see if we
4031              have a global definition or declaration for the function.  */
4032           if (oldlocal == NULL_TREE
4033               && DECL_EXTERNAL (x)
4034               && oldglobal != NULL_TREE
4035               && TREE_CODE (x) == FUNCTION_DECL
4036               && TREE_CODE (oldglobal) == FUNCTION_DECL)
4037             {
4038               /* We have one.  Their types must agree.  */
4039               if (decls_match (x, oldglobal))
4040                 /* OK */;
4041               else
4042                 {
4043                   warning ("extern declaration of `%#D' doesn't match", x);
4044                   cp_warning_at ("global declaration `%#D'", oldglobal);
4045                 }
4046             }
4047           /* If we have a local external declaration,
4048              and no file-scope declaration has yet been seen,
4049              then if we later have a file-scope decl it must not be static.  */
4050           if (oldlocal == NULL_TREE
4051               && oldglobal == NULL_TREE
4052               && DECL_EXTERNAL (x)
4053               && TREE_PUBLIC (x))
4054             TREE_PUBLIC (name) = 1;
4055
4056           /* Warn if shadowing an argument at the top level of the body.  */
4057           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4058               /* Inline decls shadow nothing.  */
4059               && !DECL_FROM_INLINE (x)
4060               && TREE_CODE (oldlocal) == PARM_DECL)
4061             {
4062               bool err = false;
4063
4064               /* Don't complain if it's from an enclosing function.  */
4065               if (DECL_CONTEXT (oldlocal) == current_function_decl
4066                   && TREE_CODE (x) != PARM_DECL)
4067                 {
4068                   /* Go to where the parms should be and see if we find
4069                      them there.  */
4070                   struct cp_binding_level *b = current_binding_level->level_chain;
4071
4072                   /* Skip the ctor/dtor cleanup level.  */
4073                   b = b->level_chain;
4074
4075                   /* ARM $8.3 */
4076                   if (b->parm_flag == 1)
4077                     {
4078                       error ("declaration of `%#D' shadows a parameter",
4079                                 name);
4080                       err = true;
4081                     }
4082                 }
4083
4084               if (warn_shadow && !err)
4085                 shadow_warning ("a parameter", name, oldlocal);
4086             }
4087
4088           /* Maybe warn if shadowing something else.  */
4089           else if (warn_shadow && !DECL_EXTERNAL (x)
4090               /* No shadow warnings for internally generated vars.  */
4091               && ! DECL_ARTIFICIAL (x)
4092               /* No shadow warnings for vars made for inlining.  */
4093               && ! DECL_FROM_INLINE (x))
4094             {
4095               if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4096                        && current_class_ptr
4097                        && !TREE_STATIC (name))
4098                 warning ("declaration of `%s' shadows a member of `this'",
4099                             IDENTIFIER_POINTER (name));
4100               else if (oldlocal != NULL_TREE
4101                        && TREE_CODE (oldlocal) == VAR_DECL)
4102                 shadow_warning ("a previous local", name, oldlocal);
4103               else if (oldglobal != NULL_TREE
4104                        && TREE_CODE (oldglobal) == VAR_DECL)
4105                 /* XXX shadow warnings in outer-more namespaces */
4106                 shadow_warning ("a global declaration", name, oldglobal);
4107             }
4108         }
4109
4110       if (TREE_CODE (x) == FUNCTION_DECL)
4111         check_default_args (x);
4112
4113       if (TREE_CODE (x) == VAR_DECL)
4114         maybe_register_incomplete_var (x);
4115     }
4116
4117   if (need_new_binding)
4118     add_decl_to_level (x,
4119                        DECL_NAMESPACE_SCOPE_P (x)
4120                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4121                        : current_binding_level);
4122
4123   return x;
4124 }
4125
4126 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4127    caller to set DECL_CONTEXT properly.  */
4128
4129 static tree
4130 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4131 {
4132   register struct cp_binding_level *b;
4133   tree function_decl = current_function_decl;
4134
4135   current_function_decl = NULL_TREE;
4136   if (level->parm_flag == 2)
4137     {
4138       b = class_binding_level;
4139       class_binding_level = level;
4140       pushdecl_class_level (x);
4141       class_binding_level = b;
4142     }
4143   else
4144     {
4145       b = current_binding_level;
4146       current_binding_level = level;
4147       x = pushdecl (x);
4148       current_binding_level = b;
4149     }
4150   current_function_decl = function_decl;
4151   return x;
4152 }
4153
4154 /* Like pushdecl, only it places X in the current namespace,
4155    if appropriate.  */
4156
4157 tree
4158 pushdecl_namespace_level (tree x)
4159 {
4160   register struct cp_binding_level *b = current_binding_level;
4161   register tree t;
4162
4163   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4164
4165   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4166      what we want.  */
4167   if (TREE_CODE (x) == TYPE_DECL)
4168     {
4169       tree name = DECL_NAME (x);
4170       tree newval;
4171       tree *ptr = (tree *)0;
4172       for (; b != global_binding_level; b = b->level_chain)
4173         {
4174           tree shadowed = b->type_shadowed;
4175           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4176             if (TREE_PURPOSE (shadowed) == name)
4177               {
4178                 ptr = &TREE_VALUE (shadowed);
4179                 /* Can't break out of the loop here because sometimes
4180                    a binding level will have duplicate bindings for
4181                    PT names.  It's gross, but I haven't time to fix it.  */
4182               }
4183         }
4184       newval = TREE_TYPE (x);
4185       if (ptr == (tree *)0)
4186         {
4187           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4188              up here if this is changed to an assertion.  --KR  */
4189           SET_IDENTIFIER_TYPE_VALUE (name, newval);
4190         }
4191       else
4192         {
4193           *ptr = newval;
4194         }
4195     }
4196   return t;
4197 }
4198
4199 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4200    if appropriate.  */
4201
4202 tree
4203 pushdecl_top_level (tree x)
4204 {
4205   push_to_top_level ();
4206   x = pushdecl_namespace_level (x);
4207   pop_from_top_level ();
4208   return x;
4209 }
4210
4211 /* Make the declaration of X appear in CLASS scope.  */
4212
4213 void
4214 pushdecl_class_level (tree x)
4215 {
4216   tree name;
4217
4218   /* Get the name of X.  */
4219   if (TREE_CODE (x) == OVERLOAD)
4220     name = DECL_NAME (get_first_fn (x));
4221   else
4222     name = DECL_NAME (x);
4223
4224   if (name)
4225     {
4226       push_class_level_binding (name, x);
4227       if (TREE_CODE (x) == TYPE_DECL)
4228         set_identifier_type_value (name, TREE_TYPE (x));
4229     }
4230   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4231     {
4232       /* If X is an anonymous aggregate, all of its members are
4233          treated as if they were members of the class containing the
4234          aggregate, for naming purposes.  */
4235       tree f;
4236
4237       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4238         pushdecl_class_level (f);
4239     }
4240 }
4241
4242 /* Enter DECL into the symbol table, if that's appropriate.  Returns
4243    DECL, or a modified version thereof.  */
4244
4245 tree
4246 maybe_push_decl (tree decl)
4247 {
4248   tree type = TREE_TYPE (decl);
4249
4250   /* Add this decl to the current binding level, but not if it comes
4251      from another scope, e.g. a static member variable.  TEM may equal
4252      DECL or it may be a previous decl of the same name.  */
4253   if (decl == error_mark_node
4254       || (TREE_CODE (decl) != PARM_DECL
4255           && DECL_CONTEXT (decl) != NULL_TREE
4256           /* Definitions of namespace members outside their namespace are
4257              possible.  */
4258           && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4259       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4260       || TREE_CODE (type) == UNKNOWN_TYPE
4261       /* The declaration of a template specialization does not affect
4262          the functions available for overload resolution, so we do not
4263          call pushdecl.  */
4264       || (TREE_CODE (decl) == FUNCTION_DECL
4265           && DECL_TEMPLATE_SPECIALIZATION (decl)))
4266     return decl;
4267   else
4268     return pushdecl (decl);
4269 }
4270
4271 /* Make the declaration(s) of X appear in CLASS scope
4272    under the name NAME.  */
4273
4274 void
4275 push_class_level_binding (tree name, tree x)
4276 {
4277   tree binding;
4278   /* The class_binding_level will be NULL if x is a template
4279      parameter name in a member template.  */
4280   if (!class_binding_level)
4281     return;
4282
4283   /* Make sure that this new member does not have the same name
4284      as a template parameter.  */
4285   if (TYPE_BEING_DEFINED (current_class_type))
4286     check_template_shadow (x);
4287
4288   /* If this declaration shadows a declaration from an enclosing
4289      class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4290      we leave this class.  Record the shadowed declaration here.  */
4291   binding = IDENTIFIER_BINDING (name);
4292   if (binding
4293       && ((TREE_CODE (x) == OVERLOAD
4294            && BINDING_VALUE (binding)
4295            && is_overloaded_fn (BINDING_VALUE (binding)))
4296           || INHERITED_VALUE_BINDING_P (binding)))
4297     {
4298       tree shadow;
4299       tree old_decl;
4300
4301       /* If the old binding was from a base class, and was for a tag
4302          name, slide it over to make room for the new binding.  The
4303          old binding is still visible if explicitly qualified with a
4304          class-key.  */
4305       if (INHERITED_VALUE_BINDING_P (binding)
4306           && BINDING_VALUE (binding)
4307           && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4308           && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4309           && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4310         {
4311           old_decl = BINDING_TYPE (binding);
4312           BINDING_TYPE (binding) = BINDING_VALUE (binding);
4313           BINDING_VALUE (binding) = NULL_TREE;
4314           INHERITED_VALUE_BINDING_P (binding) = 0;
4315         }
4316       else
4317         old_decl = BINDING_VALUE (binding);
4318
4319       /* Find the previous binding of name on the class-shadowed
4320          list, and update it.  */
4321       for (shadow = class_binding_level->class_shadowed;
4322            shadow;
4323            shadow = TREE_CHAIN (shadow))
4324         if (TREE_PURPOSE (shadow) == name
4325             && TREE_TYPE (shadow) == old_decl)
4326           {
4327             BINDING_VALUE (binding) = x;
4328             INHERITED_VALUE_BINDING_P (binding) = 0;
4329             TREE_TYPE (shadow) = x;
4330             IDENTIFIER_CLASS_VALUE (name) = x;
4331             return;
4332           }
4333     }
4334
4335   /* If we didn't replace an existing binding, put the binding on the
4336      stack of bindings for the identifier, and update the shadowed list.  */
4337   if (push_class_binding (name, x))
4338     {
4339       class_binding_level->class_shadowed
4340         = tree_cons (name, NULL,
4341                      class_binding_level->class_shadowed);
4342       /* Record the value we are binding NAME to so that we can know
4343          what to pop later.  */
4344       TREE_TYPE (class_binding_level->class_shadowed) = x;
4345     }
4346 }
4347
4348 /* Insert another USING_DECL into the current binding level, returning
4349    this declaration. If this is a redeclaration, do nothing, and
4350    return NULL_TREE if this not in namespace scope (in namespace
4351    scope, a using decl might extend any previous bindings).  */
4352
4353 tree
4354 push_using_decl (tree scope, tree name)
4355 {
4356   tree decl;
4357
4358   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4359   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4360   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4361     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4362       break;
4363   if (decl)
4364     return namespace_bindings_p () ? decl : NULL_TREE;
4365   decl = build_lang_decl (USING_DECL, name, void_type_node);
4366   DECL_INITIAL (decl) = scope;
4367   TREE_CHAIN (decl) = current_binding_level->usings;
4368   current_binding_level->usings = decl;
4369   return decl;
4370 }
4371
4372 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4373    changed (i.e. there was already a directive), or the fresh
4374    TREE_LIST otherwise.  */
4375
4376 tree
4377 push_using_directive (tree used)
4378 {
4379   tree ud = current_binding_level->using_directives;
4380   tree iter, ancestor;
4381
4382   /* Check if we already have this.  */
4383   if (purpose_member (used, ud) != NULL_TREE)
4384     return NULL_TREE;
4385
4386   /* Recursively add all namespaces used.  */
4387   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4388     push_using_directive (TREE_PURPOSE (iter));
4389
4390   ancestor = namespace_ancestor (current_decl_namespace (), used);
4391   ud = current_binding_level->using_directives;
4392   ud = tree_cons (used, ancestor, ud);
4393   current_binding_level->using_directives = ud;
4394   return ud;
4395 }
4396
4397 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4398    other definitions already in place.  We get around this by making
4399    the value of the identifier point to a list of all the things that
4400    want to be referenced by that name.  It is then up to the users of
4401    that name to decide what to do with that list.
4402
4403    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4404    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4405
4406    FLAGS is a bitwise-or of the following values:
4407      PUSH_LOCAL: Bind DECL in the current scope, rather than at
4408                  namespace scope.
4409      PUSH_USING: DECL is being pushed as the result of a using
4410                  declaration.
4411
4412    The value returned may be a previous declaration if we guessed wrong
4413    about what language DECL should belong to (C or C++).  Otherwise,
4414    it's always DECL (and never something that's not a _DECL).  */
4415
4416 tree
4417 push_overloaded_decl (tree decl, int flags)
4418 {
4419   tree name = DECL_NAME (decl);
4420   tree old;
4421   tree new_binding;
4422   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4423
4424   if (doing_global)
4425     old = namespace_binding (name, DECL_CONTEXT (decl));
4426   else
4427     old = lookup_name_current_level (name);
4428
4429   if (old)
4430     {
4431       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4432         {
4433           tree t = TREE_TYPE (old);
4434           if (IS_AGGR_TYPE (t) && warn_shadow
4435               && (! DECL_IN_SYSTEM_HEADER (decl)
4436                   || ! DECL_IN_SYSTEM_HEADER (old)))
4437             warning ("`%#D' hides constructor for `%#T'", decl, t);
4438           old = NULL_TREE;
4439         }
4440       else if (is_overloaded_fn (old))
4441         {
4442           tree tmp;
4443
4444           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4445             {
4446               tree fn = OVL_CURRENT (tmp);
4447
4448               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4449                   && !(flags & PUSH_USING)
4450                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4451                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4452                 error ("`%#D' conflicts with previous using declaration `%#D'",
4453                           decl, fn);
4454
4455               if (duplicate_decls (decl, fn))
4456                 return fn;
4457             }
4458         }
4459       else if (old == error_mark_node)
4460         /* Ignore the undefined symbol marker.  */
4461         old = NULL_TREE;
4462       else
4463         {
4464           cp_error_at ("previous non-function declaration `%#D'", old);
4465           error ("conflicts with function declaration `%#D'", decl);
4466           return decl;
4467         }
4468     }
4469
4470   if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4471     {
4472       if (old && TREE_CODE (old) != OVERLOAD)
4473         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4474       else
4475         new_binding = ovl_cons (decl, old);
4476       if (flags & PUSH_USING)
4477         OVL_USED (new_binding) = 1;
4478     }
4479   else
4480     /* NAME is not ambiguous.  */
4481     new_binding = decl;
4482
4483   if (doing_global)
4484     set_namespace_binding (name, current_namespace, new_binding);
4485   else
4486     {
4487       /* We only create an OVERLOAD if there was a previous binding at
4488          this level, or if decl is a template. In the former case, we
4489          need to remove the old binding and replace it with the new
4490          binding.  We must also run through the NAMES on the binding
4491          level where the name was bound to update the chain.  */
4492
4493       if (TREE_CODE (new_binding) == OVERLOAD && old)
4494         {
4495           tree *d;
4496
4497           for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4498                *d;
4499                d = &TREE_CHAIN (*d))
4500             if (*d == old
4501                 || (TREE_CODE (*d) == TREE_LIST
4502                     && TREE_VALUE (*d) == old))
4503               {
4504                 if (TREE_CODE (*d) == TREE_LIST)
4505                   /* Just replace the old binding with the new.  */
4506                   TREE_VALUE (*d) = new_binding;
4507                 else
4508                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
4509                   *d = tree_cons (NULL_TREE, new_binding,
4510                                   TREE_CHAIN (*d));
4511
4512                 /* And update the CPLUS_BINDING node.  */
4513                 BINDING_VALUE (IDENTIFIER_BINDING (name))
4514                   = new_binding;
4515                 return decl;
4516               }
4517
4518           /* We should always find a previous binding in this case.  */
4519           abort ();
4520         }
4521
4522       /* Install the new binding.  */
4523       push_local_binding (name, new_binding, flags);
4524     }
4525
4526   return decl;
4527 }
4528 \f
4529 /* Generate an implicit declaration for identifier FUNCTIONID
4530    as a function of type int ().  Print a warning if appropriate.  */
4531
4532 tree
4533 implicitly_declare (tree functionid)
4534 {
4535   register tree decl;
4536
4537   /* We used to reuse an old implicit decl here,
4538      but this loses with inline functions because it can clobber
4539      the saved decl chains.  */
4540   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4541
4542   DECL_EXTERNAL (decl) = 1;
4543   TREE_PUBLIC (decl) = 1;
4544
4545   /* ISO standard says implicit declarations are in the innermost block.
4546      So we record the decl in the standard fashion.  */
4547   pushdecl (decl);
4548   rest_of_decl_compilation (decl, NULL, 0, 0);
4549
4550   if (warn_implicit
4551       /* Only one warning per identifier.  */
4552       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4553     {
4554       pedwarn ("implicit declaration of function `%#D'", decl);
4555     }
4556
4557   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4558
4559   return decl;
4560 }
4561
4562 /* Return zero if the declaration NEWDECL is valid
4563    when the declaration OLDDECL (assumed to be for the same name)
4564    has already been seen.
4565    Otherwise return an error message format string with a %s
4566    where the identifier should go.  */
4567
4568 static const char *
4569 redeclaration_error_message (tree newdecl, tree olddecl)
4570 {
4571   if (TREE_CODE (newdecl) == TYPE_DECL)
4572     {
4573       /* Because C++ can put things into name space for free,
4574          constructs like "typedef struct foo { ... } foo"
4575          would look like an erroneous redeclaration.  */
4576       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4577         return 0;
4578       else
4579         return "redefinition of `%#D'";
4580     }
4581   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4582     {
4583       /* If this is a pure function, its olddecl will actually be
4584          the original initialization to `0' (which we force to call
4585          abort()).  Don't complain about redefinition in this case.  */
4586       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4587         return 0;
4588
4589       /* If both functions come from different namespaces, this is not
4590          a redeclaration - this is a conflict with a used function.  */
4591       if (DECL_NAMESPACE_SCOPE_P (olddecl)
4592           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4593         return "`%D' conflicts with used function";
4594
4595       /* We'll complain about linkage mismatches in
4596          warn_extern_redeclared_static.  */
4597
4598       /* Defining the same name twice is no good.  */
4599       if (DECL_INITIAL (olddecl) != NULL_TREE
4600           && DECL_INITIAL (newdecl) != NULL_TREE)
4601         {
4602           if (DECL_NAME (olddecl) == NULL_TREE)
4603             return "`%#D' not declared in class";
4604           else
4605             return "redefinition of `%#D'";
4606         }
4607       return 0;
4608     }
4609   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4610     {
4611       if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4612            && (DECL_TEMPLATE_RESULT (newdecl)
4613                != DECL_TEMPLATE_RESULT (olddecl))
4614            && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4615            && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4616           || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4617               && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4618               && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4619         return "redefinition of `%#D'";
4620       return 0;
4621     }
4622   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4623     {
4624       /* Objects declared at top level:  */
4625       /* If at least one is a reference, it's ok.  */
4626       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4627         return 0;
4628       /* Reject two definitions.  */
4629       return "redefinition of `%#D'";
4630     }
4631   else
4632     {
4633       /* Objects declared with block scope:  */
4634       /* Reject two definitions, and reject a definition
4635          together with an external reference.  */
4636       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4637         return "redeclaration of `%#D'";
4638       return 0;
4639     }
4640 }
4641 \f
4642 /* Create a new label, named ID.  */
4643
4644 static tree
4645 make_label_decl (tree id, int local_p)
4646 {
4647   tree decl;
4648
4649   decl = build_decl (LABEL_DECL, id, void_type_node);
4650   if (expanding_p)
4651     /* Make sure every label has an rtx.  */
4652     label_rtx (decl);
4653
4654   DECL_CONTEXT (decl) = current_function_decl;
4655   DECL_MODE (decl) = VOIDmode;
4656   C_DECLARED_LABEL_FLAG (decl) = local_p;
4657
4658   /* Say where one reference is to the label, for the sake of the
4659      error if it is not defined.  */
4660   DECL_SOURCE_LINE (decl) = lineno;
4661   DECL_SOURCE_FILE (decl) = input_filename;
4662
4663   /* Record the fact that this identifier is bound to this label.  */
4664   SET_IDENTIFIER_LABEL_VALUE (id, decl);
4665
4666   return decl;
4667 }
4668
4669 /* Record this label on the list of used labels so that we can check
4670    at the end of the function to see whether or not the label was
4671    actually defined, and so we can check when the label is defined whether
4672    this use is valid.  */
4673
4674 static void
4675 use_label (tree decl)
4676 {
4677   if (named_label_uses == NULL
4678       || named_label_uses->names_in_scope != current_binding_level->names
4679       || named_label_uses->label_decl != decl)
4680     {
4681       struct named_label_use_list *new_ent;
4682       new_ent = ((struct named_label_use_list *)
4683                  ggc_alloc (sizeof (struct named_label_use_list)));
4684       new_ent->label_decl = decl;
4685       new_ent->names_in_scope = current_binding_level->names;
4686       new_ent->binding_level = current_binding_level;
4687       new_ent->lineno_o_goto = lineno;
4688       new_ent->filename_o_goto = input_filename;
4689       new_ent->next = named_label_uses;
4690       named_label_uses = new_ent;
4691     }
4692 }
4693
4694 /* Look for a label named ID in the current function.  If one cannot
4695    be found, create one.  (We keep track of used, but undefined,
4696    labels, and complain about them at the end of a function.)  */
4697
4698 tree
4699 lookup_label (tree id)
4700 {
4701   tree decl;
4702   struct named_label_list *ent;
4703
4704   /* You can't use labels at global scope.  */
4705   if (current_function_decl == NULL_TREE)
4706     {
4707       error ("label `%s' referenced outside of any function",
4708              IDENTIFIER_POINTER (id));
4709       return NULL_TREE;
4710     }
4711
4712   /* See if we've already got this label.  */
4713   decl = IDENTIFIER_LABEL_VALUE (id);
4714   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4715     return decl;
4716
4717   /* Record this label on the list of labels used in this function.
4718      We do this before calling make_label_decl so that we get the
4719      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
4720   ent = ((struct named_label_list *)
4721          ggc_alloc_cleared (sizeof (struct named_label_list)));
4722   ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4723   ent->next = named_labels;
4724   named_labels = ent;
4725
4726   /* We need a new label.  */
4727   decl = make_label_decl (id, /*local_p=*/0);
4728
4729   /* Now fill in the information we didn't have before.  */
4730   ent->label_decl = decl;
4731
4732   return decl;
4733 }
4734
4735 /* Declare a local label named ID.  */
4736
4737 tree
4738 declare_local_label (tree id)
4739 {
4740   tree decl;
4741
4742   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4743      this scope we can restore the old value of
4744      IDENTIFIER_TYPE_VALUE.  */
4745   current_binding_level->shadowed_labels
4746     = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4747                  current_binding_level->shadowed_labels);
4748   /* Look for the label.  */
4749   decl = make_label_decl (id, /*local_p=*/1);
4750   /* Now fill in the information we didn't have before.  */
4751   TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4752
4753   return decl;
4754 }
4755
4756 /* Returns nonzero if it is ill-formed to jump past the declaration of
4757    DECL.  Returns 2 if it's also a real problem.  */
4758
4759 static int
4760 decl_jump_unsafe (tree decl)
4761 {
4762   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4763     return 0;
4764
4765   if (DECL_INITIAL (decl) == NULL_TREE
4766       && pod_type_p (TREE_TYPE (decl)))
4767     return 0;
4768
4769   /* This is really only important if we're crossing an initialization.
4770      The POD stuff is just pedantry; why should it matter if the class
4771      contains a field of pointer to member type?  */
4772   if (DECL_INITIAL (decl)
4773       || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4774     return 2;
4775   return 1;
4776 }
4777
4778 /* Check that a single previously seen jump to a newly defined label
4779    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4780    the jump context; NAMES are the names in scope in LEVEL at the jump
4781    context; FILE and LINE are the source position of the jump or 0.  */
4782
4783 static void
4784 check_previous_goto_1 (tree decl,
4785                        struct cp_binding_level* level,
4786                        tree names,
4787                        const char* file,
4788                        int line)
4789 {
4790   int identified = 0;
4791   int saw_eh = 0;
4792   struct cp_binding_level *b = current_binding_level;
4793   for (; b; b = b->level_chain)
4794     {
4795       tree new_decls = b->names;
4796       tree old_decls = (b == level ? names : NULL_TREE);
4797       for (; new_decls != old_decls;
4798            new_decls = TREE_CHAIN (new_decls))
4799         {
4800           int problem = decl_jump_unsafe (new_decls);
4801           if (! problem)
4802             continue;
4803
4804           if (! identified)
4805             {
4806               if (decl)
4807                 pedwarn ("jump to label `%D'", decl);
4808               else
4809                 pedwarn ("jump to case label");
4810
4811               if (file)
4812                 pedwarn_with_file_and_line (file, line, "  from here");
4813               identified = 1;
4814             }
4815
4816           if (problem > 1)
4817             cp_error_at ("  crosses initialization of `%#D'",
4818                          new_decls);
4819           else
4820             cp_pedwarn_at ("  enters scope of non-POD `%#D'",
4821                            new_decls);
4822         }
4823
4824       if (b == level)
4825         break;
4826       if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4827         {
4828           if (! identified)
4829             {
4830               if (decl)
4831                 pedwarn ("jump to label `%D'", decl);
4832               else
4833                 pedwarn ("jump to case label");
4834
4835               if (file)
4836                 pedwarn_with_file_and_line (file, line, "  from here");
4837               identified = 1;
4838             }
4839           if (b->is_try_scope)
4840             error ("  enters try block");
4841           else
4842             error ("  enters catch block");
4843           saw_eh = 1;
4844         }
4845     }
4846 }
4847
4848 static void
4849 check_previous_goto (struct named_label_use_list* use)
4850 {
4851   check_previous_goto_1 (use->label_decl, use->binding_level,
4852                          use->names_in_scope, use->filename_o_goto,
4853                          use->lineno_o_goto);
4854 }
4855
4856 static void
4857 check_switch_goto (struct cp_binding_level* level)
4858 {
4859   check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
4860 }
4861
4862 /* Check that any previously seen jumps to a newly defined label DECL
4863    are OK.  Called by define_label.  */
4864
4865 static void
4866 check_previous_gotos (tree decl)
4867 {
4868   struct named_label_use_list **usep;
4869
4870   if (! TREE_USED (decl))
4871     return;
4872
4873   for (usep = &named_label_uses; *usep; )
4874     {
4875       struct named_label_use_list *use = *usep;
4876       if (use->label_decl == decl)
4877         {
4878           check_previous_goto (use);
4879           *usep = use->next;
4880         }
4881       else
4882         usep = &(use->next);
4883     }
4884 }
4885
4886 /* Check that a new jump to a label DECL is OK.  Called by
4887    finish_goto_stmt.  */
4888
4889 void
4890 check_goto (tree decl)
4891 {
4892   int identified = 0;
4893   tree bad;
4894   struct named_label_list *lab;
4895
4896   /* We can't know where a computed goto is jumping.  So we assume
4897      that it's OK.  */
4898   if (! DECL_P (decl))
4899     return;
4900
4901   /* If the label hasn't been defined yet, defer checking.  */
4902   if (! DECL_INITIAL (decl))
4903     {
4904       use_label (decl);
4905       return;
4906     }
4907
4908   for (lab = named_labels; lab; lab = lab->next)
4909     if (decl == lab->label_decl)
4910       break;
4911
4912   /* If the label is not on named_labels it's a gcc local label, so
4913      it must be in an outer scope, so jumping to it is always OK.  */
4914   if (lab == 0)
4915     return;
4916
4917   if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4918       && !identified)
4919     {
4920       cp_pedwarn_at ("jump to label `%D'", decl);
4921       pedwarn ("  from here");
4922       identified = 1;
4923     }
4924
4925   for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4926     {
4927       tree b = TREE_VALUE (bad);
4928       int u = decl_jump_unsafe (b);
4929
4930       if (u > 1 && DECL_ARTIFICIAL (b))
4931         /* Can't skip init of __exception_info.  */
4932         cp_error_at ("  enters catch block", b);
4933       else if (u > 1)
4934         cp_error_at ("  skips initialization of `%#D'", b);
4935       else
4936         cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
4937     }
4938
4939   if (lab->in_try_scope)
4940     error ("  enters try block");
4941   else if (lab->in_catch_scope)
4942     error ("  enters catch block");
4943 }
4944
4945 /* Define a label, specifying the location in the source file.
4946    Return the LABEL_DECL node for the label, if the definition is valid.
4947    Otherwise return 0.  */
4948
4949 tree
4950 define_label (const char* filename, int line, tree name)
4951 {
4952   tree decl = lookup_label (name);
4953   struct named_label_list *ent;
4954   register struct cp_binding_level *p;
4955
4956   for (ent = named_labels; ent; ent = ent->next)
4957     if (ent->label_decl == decl)
4958       break;
4959
4960   /* After labels, make any new cleanups in the function go into their
4961      own new (temporary) binding contour.  */
4962   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4963     p->more_cleanups_ok = 0;
4964
4965   if (name == get_identifier ("wchar_t"))
4966     pedwarn ("label named wchar_t");
4967
4968   if (DECL_INITIAL (decl) != NULL_TREE)
4969     {
4970       error ("duplicate label `%D'", decl);
4971       return 0;
4972     }
4973   else
4974     {
4975       /* Mark label as having been defined.  */
4976       DECL_INITIAL (decl) = error_mark_node;
4977       /* Say where in the source.  */
4978       DECL_SOURCE_FILE (decl) = filename;
4979       DECL_SOURCE_LINE (decl) = line;
4980       if (ent)
4981         {
4982           ent->names_in_scope = current_binding_level->names;
4983           ent->binding_level = current_binding_level;
4984         }
4985       check_previous_gotos (decl);
4986       return decl;
4987     }
4988 }
4989
4990 struct cp_switch
4991 {
4992   struct cp_binding_level *level;
4993   struct cp_switch *next;
4994   /* The SWITCH_STMT being built.  */
4995   tree switch_stmt;
4996   /* A splay-tree mapping the low element of a case range to the high
4997      element, or NULL_TREE if there is no high element.  Used to
4998      determine whether or not a new case label duplicates an old case
4999      label.  We need a tree, rather than simply a hash table, because
5000      of the GNU case range extension.  */
5001   splay_tree cases;
5002 };
5003
5004 /* A stack of the currently active switch statements.  The innermost
5005    switch statement is on the top of the stack.  There is no need to
5006    mark the stack for garbage collection because it is only active
5007    during the processing of the body of a function, and we never
5008    collect at that point.  */
5009
5010 static struct cp_switch *switch_stack;
5011
5012 /* Called right after a switch-statement condition is parsed.
5013    SWITCH_STMT is the switch statement being parsed.  */
5014
5015 void
5016 push_switch (tree switch_stmt)
5017 {
5018   struct cp_switch *p
5019     = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5020   p->level = current_binding_level;
5021   p->next = switch_stack;
5022   p->switch_stmt = switch_stmt;
5023   p->cases = splay_tree_new (case_compare, NULL, NULL);
5024   switch_stack = p;
5025 }
5026
5027 void
5028 pop_switch (void)
5029 {
5030   struct cp_switch *cs;
5031
5032   cs = switch_stack;
5033   splay_tree_delete (cs->cases);
5034   switch_stack = switch_stack->next;
5035   free (cs);
5036 }
5037
5038 /* Note that we've seen a definition of a case label, and complain if this
5039    is a bad place for one.  */
5040
5041 tree
5042 finish_case_label (tree low_value, tree high_value)
5043 {
5044   tree cond, r;
5045   register struct cp_binding_level *p;
5046
5047   if (! switch_stack)
5048     {
5049       if (high_value)
5050         error ("case label not within a switch statement");
5051       else if (low_value)
5052         error ("case label `%E' not within a switch statement",
5053                   low_value);
5054       else
5055         error ("`default' label not within a switch statement");
5056       return NULL_TREE;
5057     }
5058
5059   if (processing_template_decl)
5060     {
5061       tree label;
5062
5063       /* For templates, just add the case label; we'll do semantic
5064          analysis at instantiation-time.  */
5065       label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5066       return add_stmt (build_case_label (low_value, high_value, label));
5067     }
5068
5069   /* Find the condition on which this switch statement depends.  */
5070   cond = SWITCH_COND (switch_stack->switch_stmt);
5071   if (cond && TREE_CODE (cond) == TREE_LIST)
5072     cond = TREE_VALUE (cond);
5073
5074   r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5075   if (r == error_mark_node)
5076     r = NULL_TREE;
5077
5078   check_switch_goto (switch_stack->level);
5079
5080   /* After labels, make any new cleanups in the function go into their
5081      own new (temporary) binding contour.  */
5082   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5083     p->more_cleanups_ok = 0;
5084
5085   return r;
5086 }
5087 \f
5088 /* Return the list of declarations of the current level.
5089    Note that this list is in reverse order unless/until
5090    you nreverse it; and when you do nreverse it, you must
5091    store the result back using `storedecls' or you will lose.  */
5092
5093 tree
5094 getdecls (void)
5095 {
5096   return current_binding_level->names;
5097 }
5098
5099 /* Return the list of type-tags (for structs, etc) of the current level.  */
5100
5101 tree
5102 gettags (void)
5103 {
5104   return current_binding_level->tags;
5105 }
5106
5107 /* Store the list of declarations of the current level.
5108    This is done for the parameter declarations of a function being defined,
5109    after they are modified in the light of any missing parameters.  */
5110
5111 static void
5112 storedecls (tree decls)
5113 {
5114   current_binding_level->names = decls;
5115 }
5116
5117 /* Similarly, store the list of tags of the current level.  */
5118
5119 void
5120 storetags (tree tags)
5121 {
5122   current_binding_level->tags = tags;
5123 }
5124 \f
5125 /* Return the type that should be used when TYPE's name is preceded
5126    by a tag such as 'struct' or 'union', or null if the name cannot
5127    be used in this way.
5128
5129    For example, when processing the third line of:
5130
5131         struct A;
5132         typedef struct A A;
5133         struct A;
5134
5135    lookup of A will find the typedef.  Given A's typedef, this function
5136    will return the type associated with "struct A".  For the tag to be
5137    anything other than TYPE, TYPE must be a typedef whose original type
5138    has the same name and context as TYPE itself.
5139
5140    It is not valid for a typedef of an anonymous type to be used with
5141    an explicit tag:
5142
5143        typedef struct { ... } B;
5144        struct B;
5145
5146    Return null for this case.  */
5147
5148 static tree
5149 follow_tag_typedef (tree type)
5150 {
5151   tree original;
5152
5153   original = original_type (type);
5154   if (! TYPE_NAME (original))
5155     return NULL_TREE;
5156   if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5157       && (CP_DECL_CONTEXT (TYPE_NAME (original))
5158           == CP_DECL_CONTEXT (TYPE_NAME (type)))
5159       && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5160     return original;
5161   else
5162     return NULL_TREE;
5163 }
5164
5165 /* Given NAME, an IDENTIFIER_NODE,
5166    return the structure (or union or enum) definition for that name.
5167    Searches binding levels from BINDING_LEVEL up to the global level.
5168    If THISLEVEL_ONLY is nonzero, searches only the specified context
5169    (but skips any tag-transparent contexts to find one that is
5170    meaningful for tags).
5171    FORM says which kind of type the caller wants;
5172    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5173    If the wrong kind of type is found, and it's not a template, an error is
5174    reported.  */
5175
5176 static tree
5177 lookup_tag (enum tree_code form,
5178             tree name,
5179             struct cp_binding_level* binding_level,
5180             int thislevel_only)
5181 {
5182   register struct cp_binding_level *level;
5183   /* Nonzero if, we should look past a template parameter level, even
5184      if THISLEVEL_ONLY.  */
5185   int allow_template_parms_p = 1;
5186
5187   for (level = binding_level; level; level = level->level_chain)
5188     {
5189       register tree tail;
5190       if (ANON_AGGRNAME_P (name))
5191         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5192           {
5193             /* There's no need for error checking here, because
5194                anon names are unique throughout the compilation.  */
5195             if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5196               return TREE_VALUE (tail);
5197           }
5198       else if (level->namespace_p)
5199         /* Do namespace lookup.  */
5200         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5201           {
5202             tree old = binding_for_name (name, tail);
5203
5204             /* If we just skipped past a template parameter level,
5205                even though THISLEVEL_ONLY, and we find a template
5206                class declaration, then we use the _TYPE node for the
5207                template.  See the example below.  */
5208             if (thislevel_only && !allow_template_parms_p
5209                 && old && BINDING_VALUE (old)
5210                 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5211               old = TREE_TYPE (BINDING_VALUE (old));
5212             else
5213               old = BINDING_TYPE (old);
5214
5215             if (old)
5216               {
5217                 /* We've found something at this binding level.  If it is
5218                    a typedef, extract the tag it refers to.  Lookup fails
5219                    if the typedef doesn't refer to a taggable type.  */
5220                 old = follow_tag_typedef (old);
5221                 if (!old)
5222                   return NULL_TREE;
5223                 if (TREE_CODE (old) != form
5224                     && (form == ENUMERAL_TYPE
5225                         || TREE_CODE (old) == ENUMERAL_TYPE))
5226                   {
5227                     error ("`%#D' redeclared as %C", old, form);
5228                     return NULL_TREE;
5229                   }
5230                 return old;
5231               }
5232             if (thislevel_only || tail == global_namespace)
5233               return NULL_TREE;
5234           }
5235       else
5236         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5237           {
5238             if (TREE_PURPOSE (tail) == name)
5239               {
5240                 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5241                 
5242                 if (code != form
5243                     && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5244                   {
5245                     /* Definition isn't the kind we were looking for.  */
5246                     error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5247                     return NULL_TREE;
5248                   }
5249                 return TREE_VALUE (tail);
5250               }
5251           }
5252       if (thislevel_only && ! level->tag_transparent)
5253         {
5254           if (level->template_parms_p && allow_template_parms_p)
5255             {
5256               /* We must deal with cases like this:
5257
5258                    template <class T> struct S;
5259                    template <class T> struct S {};
5260
5261                  When looking up `S', for the second declaration, we
5262                  would like to find the first declaration.  But, we
5263                  are in the pseudo-global level created for the
5264                  template parameters, rather than the (surrounding)
5265                  namespace level.  Thus, we keep going one more level,
5266                  even though THISLEVEL_ONLY is nonzero.  */
5267               allow_template_parms_p = 0;
5268               continue;
5269             }
5270           else
5271             return NULL_TREE;
5272         }
5273     }
5274   return NULL_TREE;
5275 }
5276
5277 /* Given a type, find the tag that was defined for it and return the tag name.
5278    Otherwise return 0.  However, the value can never be 0
5279    in the cases in which this is used.
5280
5281    C++: If NAME is nonzero, this is the new name to install.  This is
5282    done when replacing anonymous tags with real tag names.  */
5283
5284 static tree
5285 lookup_tag_reverse (tree type, tree name)
5286 {
5287   register struct cp_binding_level *level;
5288
5289   for (level = current_binding_level; level; level = level->level_chain)
5290     {
5291       register tree tail;
5292       for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5293         {
5294           if (TREE_VALUE (tail) == type)
5295             {
5296               if (name)
5297                 TREE_PURPOSE (tail) = name;
5298               return TREE_PURPOSE (tail);
5299             }
5300         }
5301     }
5302   return NULL_TREE;
5303 }
5304 \f
5305 /* Look up NAME in the NAMESPACE.  */
5306
5307 tree
5308 lookup_namespace_name (tree namespace, tree name)
5309 {
5310   tree val;
5311   tree template_id = NULL_TREE;
5312
5313   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5314
5315   if (TREE_CODE (name) == NAMESPACE_DECL)
5316     /* This happens for A::B<int> when B is a namespace.  */
5317     return name;
5318   else if (TREE_CODE (name) == TEMPLATE_DECL)
5319     {
5320       /* This happens for A::B where B is a template, and there are no
5321          template arguments.  */
5322       error ("invalid use of `%D'", name);
5323       return error_mark_node;
5324     }
5325
5326   namespace = ORIGINAL_NAMESPACE (namespace);
5327
5328   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5329     {
5330       template_id = name;
5331       name = TREE_OPERAND (name, 0);
5332       if (TREE_CODE (name) == OVERLOAD)
5333         name = DECL_NAME (OVL_CURRENT (name));
5334       else if (DECL_P (name))
5335         name = DECL_NAME (name);
5336     }
5337
5338   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5339
5340   val = make_node (CPLUS_BINDING);
5341   if (!qualified_lookup_using_namespace (name, namespace, val, 0))
5342     return error_mark_node;
5343
5344   if (BINDING_VALUE (val))
5345     {
5346       val = BINDING_VALUE (val);
5347
5348       if (template_id)
5349         {
5350           if (DECL_CLASS_TEMPLATE_P (val))
5351             val = lookup_template_class (val,
5352                                          TREE_OPERAND (template_id, 1),
5353                                          /*in_decl=*/NULL_TREE,
5354                                          /*context=*/NULL_TREE,
5355                                          /*entering_scope=*/0,
5356                                          tf_error | tf_warning);
5357           else if (DECL_FUNCTION_TEMPLATE_P (val)
5358                    || TREE_CODE (val) == OVERLOAD)
5359             val = lookup_template_function (val,
5360                                             TREE_OPERAND (template_id, 1));
5361           else
5362             {
5363               error ("`%D::%D' is not a template",
5364                         namespace, name);
5365               return error_mark_node;
5366             }
5367         }
5368
5369       /* If we have a single function from a using decl, pull it out.  */
5370       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5371         val = OVL_FUNCTION (val);
5372
5373       /* Ignore built-in functions that haven't been prototyped yet.  */
5374       if (!val || !DECL_P(val)
5375           || !DECL_LANG_SPECIFIC(val)
5376           || !DECL_ANTICIPATED (val))
5377         return val;
5378     }
5379
5380   error ("`%D' undeclared in namespace `%D'", name, namespace);
5381   return error_mark_node;
5382 }
5383
5384 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5385
5386 static hashval_t
5387 typename_hash (const void* k)
5388 {
5389   hashval_t hash;
5390   tree t = (tree) k;
5391
5392   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5393           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5394
5395   return hash;
5396 }
5397
5398 /* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5399
5400 static int
5401 typename_compare (const void * k1, const void * k2)
5402 {
5403   tree t1;
5404   tree t2;
5405   tree d1;
5406   tree d2;
5407
5408   t1 = (tree) k1;
5409   t2 = (tree) k2;
5410   d1 = TYPE_NAME (t1);
5411   d2 = TYPE_NAME (t2);
5412
5413   return (DECL_NAME (d1) == DECL_NAME (d2)
5414           && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5415           && ((TREE_TYPE (t1) != NULL_TREE)
5416               == (TREE_TYPE (t2) != NULL_TREE))
5417           && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5418           && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5419 }
5420
5421 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5422    the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5423    is non-NULL, this type is being created by the implicit typename
5424    extension, and BASE_TYPE is a type named `t' in some base class of
5425    `T' which depends on template parameters.
5426
5427    Returns the new TYPENAME_TYPE.  */
5428
5429 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5430
5431 tree
5432 build_typename_type (tree context, tree name, tree fullname)
5433 {
5434   tree t;
5435   tree d;
5436   PTR *e;
5437
5438   if (typename_htab == NULL)
5439     {
5440       typename_htab = htab_create_ggc (61, &typename_hash, 
5441                                        &typename_compare, NULL);
5442     }
5443
5444   /* Build the TYPENAME_TYPE.  */
5445   t = make_aggr_type (TYPENAME_TYPE);
5446   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5447   TYPENAME_TYPE_FULLNAME (t) = fullname;
5448
5449   /* Build the corresponding TYPE_DECL.  */
5450   d = build_decl (TYPE_DECL, name, t);
5451   TYPE_NAME (TREE_TYPE (d)) = d;
5452   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5453   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5454   DECL_ARTIFICIAL (d) = 1;
5455
5456   /* See if we already have this type.  */
5457   e = htab_find_slot (typename_htab, t, INSERT);
5458   if (*e)
5459     t = (tree) *e;
5460   else
5461     *e = t;
5462
5463   return t;
5464 }
5465
5466 /* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5467    unless an error occurs, in which case error_mark_node is returned.
5468    If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5469    set, we return that, rather than the _TYPE it corresponds to, in
5470    other cases we look through the type decl.  If TF_ERROR is set,
5471    complain about errors, otherwise be quiet.  */
5472
5473 tree
5474 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5475 {
5476   tree fullname;
5477
5478   if (TYPE_P (name))
5479     {
5480       if (!(TYPE_LANG_SPECIFIC (name)
5481             && (CLASSTYPE_IS_TEMPLATE (name)
5482                 || CLASSTYPE_USE_TEMPLATE (name))))
5483         name = TYPE_IDENTIFIER (name);
5484       else
5485         /* Create a TEMPLATE_ID_EXPR for the type.  */
5486         name = build_nt (TEMPLATE_ID_EXPR,
5487                          CLASSTYPE_TI_TEMPLATE (name),
5488                          CLASSTYPE_TI_ARGS (name));
5489     }
5490   else if (TREE_CODE (name) == TYPE_DECL)
5491     name = DECL_NAME (name);
5492
5493   fullname = name;
5494
5495   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5496     {
5497       name = TREE_OPERAND (name, 0);
5498       if (TREE_CODE (name) == TEMPLATE_DECL)
5499         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5500     }
5501   if (TREE_CODE (name) == TEMPLATE_DECL)
5502     {
5503       error ("`%D' used without template parameters", name);
5504       return error_mark_node;
5505     }
5506   if (TREE_CODE (name) != IDENTIFIER_NODE)
5507     abort ();
5508
5509   if (TREE_CODE (context) == NAMESPACE_DECL)
5510     {
5511       /* We can get here from typename_sub0 in the explicit_template_type
5512          expansion.  Just fail.  */
5513       if (complain & tf_error)
5514         error ("no class template named `%#T' in `%#T'",
5515                   name, context);
5516       return error_mark_node;
5517     }
5518
5519   if (! uses_template_parms (context)
5520       || currently_open_class (context))
5521     {
5522       if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5523         {
5524           tree tmpl = NULL_TREE;
5525           if (IS_AGGR_TYPE (context))
5526             tmpl = lookup_field (context, name, 0, 0);
5527           if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5528             {
5529               if (complain & tf_error)
5530                 error ("no class template named `%#T' in `%#T'",
5531                           name, context);
5532               return error_mark_node;
5533             }
5534
5535           if (complain & tf_error)
5536             {
5537               if (complain & tf_parsing)
5538                 perform_or_defer_access_check (context, tmpl);
5539               else
5540                 enforce_access (context, tmpl);
5541             }
5542
5543           return lookup_template_class (tmpl,
5544                                         TREE_OPERAND (fullname, 1),
5545                                         NULL_TREE, context,
5546                                         /*entering_scope=*/0,
5547                                         tf_error | tf_warning);
5548         }
5549       else
5550         {
5551           tree t;
5552
5553           if (!IS_AGGR_TYPE (context))
5554             {
5555               if (complain & tf_error)
5556                 error ("no type named `%#T' in `%#T'", name, context);
5557               return error_mark_node;
5558             }
5559
5560           t = lookup_field (context, name, 0, 1);
5561           if (t)
5562             {
5563               if (TREE_CODE (t) != TYPE_DECL)
5564                 {
5565                   if (complain & tf_error)
5566                     error ("no type named `%#T' in `%#T'", name, context);
5567                   return error_mark_node;
5568                 }
5569
5570               if (complain & tf_error)
5571                 {
5572                   if (complain & tf_parsing)
5573                     perform_or_defer_access_check (context, t);
5574                   else
5575                     enforce_access (context, t);
5576                 }
5577
5578               if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5579                 t = TREE_TYPE (t);
5580               
5581               return t;
5582             }
5583         }
5584     }
5585
5586   /* If the CONTEXT is not a template type, then either the field is
5587      there now or its never going to be.  */
5588   if (!uses_template_parms (context))
5589     {
5590       if (complain & tf_error)
5591         error ("no type named `%#T' in `%#T'", name, context);
5592       return error_mark_node;
5593     }
5594
5595   return build_typename_type (context, name, fullname);
5596 }
5597
5598 /* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
5599    unless an error occurs, in which case error_mark_node is returned.
5600    If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5601    corresponds to.  If COMPLAIN zero, don't complain about any errors
5602    that occur.  */
5603
5604 tree
5605 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5606 {
5607   tree t;
5608   tree d;
5609
5610   if (TYPE_P (name))
5611     name = TYPE_IDENTIFIER (name);
5612   else if (DECL_P (name))
5613     name = DECL_NAME (name);
5614   if (TREE_CODE (name) != IDENTIFIER_NODE)
5615     abort ();
5616
5617   if (!uses_template_parms (context)
5618       || currently_open_class (context))
5619     {
5620       tree tmpl = NULL_TREE;
5621
5622       if (IS_AGGR_TYPE (context))
5623         tmpl = lookup_field (context, name, 0, 0);
5624
5625       if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5626         {
5627           if (complain & tf_error)
5628             error ("no class template named `%#T' in `%#T'", name, context);
5629           return error_mark_node;
5630         }
5631       
5632       if (complain & tf_error)
5633         {
5634           if (complain & tf_parsing)
5635             perform_or_defer_access_check (context, tmpl);
5636           else
5637             enforce_access (context, tmpl);
5638         }
5639
5640       return tmpl;
5641     }
5642
5643   /* Build the UNBOUND_CLASS_TEMPLATE.  */
5644   t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5645   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5646   TREE_TYPE (t) = NULL_TREE;
5647
5648   /* Build the corresponding TEMPLATE_DECL.  */
5649   d = build_decl (TEMPLATE_DECL, name, t);
5650   TYPE_NAME (TREE_TYPE (d)) = d;
5651   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5652   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5653   DECL_ARTIFICIAL (d) = 1;
5654
5655   return t;
5656 }
5657
5658 /* Select the right _DECL from multiple choices.  */
5659
5660 static tree
5661 select_decl (tree binding, int flags)
5662 {
5663   tree val;
5664   val = BINDING_VALUE (binding);
5665
5666   if (LOOKUP_NAMESPACES_ONLY (flags))
5667     {
5668       /* We are not interested in types.  */
5669       if (val && TREE_CODE (val) == NAMESPACE_DECL)
5670         return val;
5671       return NULL_TREE;
5672     }
5673
5674   /* If we could have a type and
5675      we have nothing or we need a type and have none.  */
5676   if (BINDING_TYPE (binding)
5677       && (!val || ((flags & LOOKUP_PREFER_TYPES)
5678                    && TREE_CODE (val) != TYPE_DECL)))
5679     val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5680   /* Don't return non-types if we really prefer types.  */
5681   else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5682            && (TREE_CODE (val) != TEMPLATE_DECL
5683                || !DECL_CLASS_TEMPLATE_P (val)))
5684     val = NULL_TREE;
5685
5686   return val;
5687 }
5688
5689 /* Unscoped lookup of a global: iterate over current namespaces,
5690    considering using-directives.  If SPACESP is non-NULL, store a list
5691    of the namespaces we've considered in it.  */
5692
5693 tree
5694 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5695 {
5696   tree b = make_node (CPLUS_BINDING);
5697   tree initial = current_decl_namespace ();
5698   tree scope = initial;
5699   tree siter;
5700   struct cp_binding_level *level;
5701   tree val = NULL_TREE;
5702
5703   if (spacesp)
5704     *spacesp = NULL_TREE;
5705
5706   for (; !val; scope = CP_DECL_CONTEXT (scope))
5707     {
5708       if (spacesp)
5709         *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5710       val = binding_for_name (name, scope);
5711
5712       /* Ignore anticipated built-in functions.  */
5713       if (val && BINDING_VALUE (val)
5714           && DECL_P (BINDING_VALUE (val))
5715           && DECL_LANG_SPECIFIC (BINDING_VALUE (val))
5716           && DECL_ANTICIPATED (BINDING_VALUE (val)))
5717         {
5718           BINDING_VALUE (b) = NULL_TREE;
5719           BINDING_TYPE (b) = NULL_TREE;
5720         }
5721       else
5722         {
5723           /* Initialize binding for this context.  */
5724           BINDING_VALUE (b) = BINDING_VALUE (val);
5725           BINDING_TYPE (b) = BINDING_TYPE (val);
5726         }
5727
5728       /* Add all _DECLs seen through local using-directives.  */
5729       for (level = current_binding_level;
5730            !level->namespace_p;
5731            level = level->level_chain)
5732         if (!lookup_using_namespace (name, b, level->using_directives,
5733                                      scope, flags, spacesp))
5734           /* Give up because of error.  */
5735           return error_mark_node;
5736
5737       /* Add all _DECLs seen through global using-directives.  */
5738       /* XXX local and global using lists should work equally.  */
5739       siter = initial;
5740       while (1)
5741         {
5742           if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
5743                                        scope, flags, spacesp))
5744             /* Give up because of error.  */
5745             return error_mark_node;
5746           if (siter == scope) break;
5747           siter = CP_DECL_CONTEXT (siter);
5748         }
5749
5750       val = select_decl (b, flags);
5751       if (scope == global_namespace)
5752         break;
5753     }
5754   return val;
5755 }
5756
5757 /* Combine prefer_type and namespaces_only into flags.  */
5758
5759 static int
5760 lookup_flags (int prefer_type, int namespaces_only)
5761 {
5762   if (namespaces_only)
5763     return LOOKUP_PREFER_NAMESPACES;
5764   if (prefer_type > 1)
5765     return LOOKUP_PREFER_TYPES;
5766   if (prefer_type > 0)
5767     return LOOKUP_PREFER_BOTH;
5768   return 0;
5769 }
5770
5771 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5772    ignore it or not.  Subroutine of lookup_name_real.  */
5773
5774 static tree
5775 qualify_lookup (tree val, int flags)
5776 {
5777   if (val == NULL_TREE)
5778     return val;
5779   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5780     return val;
5781   if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5782     return val;
5783   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5784     return NULL_TREE;
5785   return val;
5786 }
5787
5788 /* Any other BINDING overrides an implicit TYPENAME.  Warn about
5789    that.  */
5790
5791 static void
5792 warn_about_implicit_typename_lookup (tree typename, tree binding)
5793 {
5794   tree subtype = TREE_TYPE (TREE_TYPE (typename));
5795   tree name = DECL_NAME (typename);
5796
5797   if (! (TREE_CODE (binding) == TEMPLATE_DECL
5798          && CLASSTYPE_TEMPLATE_INFO (subtype)
5799          && CLASSTYPE_TI_TEMPLATE (subtype) == binding)
5800       && ! (TREE_CODE (binding) == TYPE_DECL
5801             && same_type_p (TREE_TYPE (binding), subtype)))
5802     {
5803       warning ("lookup of `%D' finds `%#D'",
5804                   name, binding);
5805       warning ("  instead of `%D' from dependent base class",
5806                   typename);
5807       warning ("  (use `typename %T::%D' if that's what you meant)",
5808                   constructor_name (current_class_type), name);
5809     }
5810 }
5811
5812 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5813    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
5814    bindings.  
5815
5816    Returns a DECL (or OVERLOAD, or BASELINK) representing the
5817    declaration found.  */
5818
5819 tree
5820 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5821 {
5822   if (TREE_CODE (scope) == NAMESPACE_DECL)
5823     {
5824       tree val;
5825
5826       val = make_node (CPLUS_BINDING);
5827       flags |= LOOKUP_COMPLAIN;
5828       if (is_type_p)
5829         flags |= LOOKUP_PREFER_TYPES;
5830       if (!qualified_lookup_using_namespace (name, scope, val, flags))
5831         return NULL_TREE;
5832       return select_decl (val, flags);
5833     }
5834   else
5835     return lookup_member (scope, name, 0, is_type_p);
5836 }
5837
5838 /* Check to see whether or not DECL is a variable that would have been
5839    in scope under the ARM, but is not in scope under the ANSI/ISO
5840    standard.  If so, issue an error message.  If name lookup would
5841    work in both cases, but return a different result, this function
5842    returns the result of ANSI/ISO lookup.  Otherwise, it returns
5843    DECL.  */
5844
5845 tree
5846 check_for_out_of_scope_variable (tree decl)
5847 {
5848   tree shadowed;
5849
5850   /* We only care about out of scope variables.  */
5851   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5852     return decl;
5853
5854   shadowed = DECL_SHADOWED_FOR_VAR (decl);
5855   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5856          && DECL_DEAD_FOR_LOCAL (shadowed))
5857     shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5858   if (!shadowed)
5859     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5860   if (shadowed)
5861     {
5862       if (!DECL_ERROR_REPORTED (decl))
5863         {
5864           warning ("name lookup of `%D' changed",
5865                       DECL_NAME (decl));
5866           cp_warning_at ("  matches this `%D' under ISO standard rules",
5867                          shadowed);
5868           cp_warning_at ("  matches this `%D' under old rules", decl);
5869           DECL_ERROR_REPORTED (decl) = 1;
5870         }
5871       return shadowed;
5872     }
5873
5874   /* If we have already complained about this declaration, there's no
5875      need to do it again.  */
5876   if (DECL_ERROR_REPORTED (decl))
5877     return decl;
5878
5879   DECL_ERROR_REPORTED (decl) = 1;
5880   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5881     {
5882       error ("name lookup of `%D' changed for new ISO `for' scoping",
5883              DECL_NAME (decl));
5884       cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
5885       return error_mark_node;
5886     }
5887   else
5888     {
5889       pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5890                DECL_NAME (decl));
5891       cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
5892     }
5893
5894   return decl;
5895 }
5896
5897 /* Look up NAME in the current binding level and its superiors in the
5898    namespace of variables, functions and typedefs.  Return a ..._DECL
5899    node of some kind representing its definition if there is only one
5900    such declaration, or return a TREE_LIST with all the overloaded
5901    definitions if there are many, or return 0 if it is undefined.
5902
5903    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5904    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5905    Otherwise we prefer non-TYPE_DECLs.
5906
5907    If NONCLASS is nonzero, we don't look for the NAME in class scope,
5908    using IDENTIFIER_CLASS_VALUE.  */
5909
5910 tree
5911 lookup_name_real (tree name, 
5912                   int prefer_type, 
5913                   int nonclass, 
5914                   int namespaces_only,
5915                   int flags)
5916 {
5917   tree t;
5918   tree val = NULL_TREE;
5919
5920   /* Conversion operators are handled specially because ordinary
5921      unqualified name lookup will not find template conversion
5922      operators.  */
5923   if (IDENTIFIER_TYPENAME_P (name)) 
5924     {
5925       struct cp_binding_level *level;
5926
5927       for (level = current_binding_level; 
5928            level && !level->namespace_p; 
5929            level = level->level_chain)
5930         {
5931           tree class_type;
5932           tree operators;
5933           
5934           /* A conversion operator can only be declared in a class 
5935              scope.  */
5936           if (level->parm_flag != 2)
5937             continue;
5938           
5939           /* Lookup the conversion operator in the class.  */
5940           class_type = level->this_class;
5941           operators = lookup_fnfields (class_type, name, /*protect=*/0);
5942           if (operators)
5943             return operators;
5944         }
5945
5946       return NULL_TREE;
5947     }
5948
5949   flags |= lookup_flags (prefer_type, namespaces_only);
5950
5951   /* First, look in non-namespace scopes.  */
5952
5953   if (current_class_type == NULL_TREE)
5954     nonclass = 1;
5955
5956   for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
5957     {
5958       tree binding;
5959
5960       if (!LOCAL_BINDING_P (t) && nonclass)
5961         /* We're not looking for class-scoped bindings, so keep going.  */
5962         continue;
5963
5964       /* If this is the kind of thing we're looking for, we're done.  */
5965       if (qualify_lookup (BINDING_VALUE (t), flags))
5966         binding = BINDING_VALUE (t);
5967       else if ((flags & LOOKUP_PREFER_TYPES)
5968                && qualify_lookup (BINDING_TYPE (t), flags))
5969         binding = BINDING_TYPE (t);
5970       else
5971         binding = NULL_TREE;
5972
5973       if (binding)
5974         {
5975           val = binding;
5976           break;
5977         }
5978     }
5979
5980   /* Now lookup in namespace scopes.  */
5981   if (!val)
5982     {
5983       t = unqualified_namespace_lookup (name, flags, 0);
5984       if (t)
5985         val = t;
5986     }
5987
5988   if (val)
5989     {
5990       /* If we have a single function from a using decl, pull it out.  */
5991       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5992         val = OVL_FUNCTION (val);
5993     }
5994
5995   return val;
5996 }
5997
5998 tree
5999 lookup_name_nonclass (tree name)
6000 {
6001   return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
6002 }
6003
6004 tree
6005 lookup_function_nonclass (tree name, tree args)
6006 {
6007   return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6008 }
6009
6010 tree
6011 lookup_name (tree name, int prefer_type)
6012 {
6013   return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
6014 }
6015
6016 /* Similar to `lookup_name' but look only in the innermost non-class
6017    binding level.  */
6018
6019 tree
6020 lookup_name_current_level (tree name)
6021 {
6022   struct cp_binding_level *b;
6023   tree t = NULL_TREE;
6024
6025   b = current_binding_level;
6026   while (b->parm_flag == 2)
6027     b = b->level_chain;
6028
6029   if (b->namespace_p)
6030     {
6031       t = IDENTIFIER_NAMESPACE_VALUE (name);
6032
6033       /* extern "C" function() */
6034       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6035         t = TREE_VALUE (t);
6036     }
6037   else if (IDENTIFIER_BINDING (name)
6038            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6039     {
6040       while (1)
6041         {
6042           if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6043             return IDENTIFIER_VALUE (name);
6044
6045           if (b->keep == 2)
6046             b = b->level_chain;
6047           else
6048             break;
6049         }
6050     }
6051
6052   return t;
6053 }
6054
6055 /* Like lookup_name_current_level, but for types.  */
6056
6057 tree
6058 lookup_type_current_level (tree name)
6059 {
6060   register tree t = NULL_TREE;
6061
6062   my_friendly_assert (! current_binding_level->namespace_p, 980716);
6063
6064   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6065       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6066     {
6067       struct cp_binding_level *b = current_binding_level;
6068       while (1)
6069         {
6070           if (purpose_member (name, b->type_shadowed))
6071             return REAL_IDENTIFIER_TYPE_VALUE (name);
6072           if (b->keep == 2)
6073             b = b->level_chain;
6074           else
6075             break;
6076         }
6077     }
6078
6079   return t;
6080 }
6081
6082 \f
6083 /* Push the declarations of builtin types into the namespace.
6084    RID_INDEX is the index of the builtin type
6085    in the array RID_POINTERS.  NAME is the name used when looking
6086    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6087
6088 void
6089 record_builtin_type (enum rid rid_index, 
6090                      const char* name, 
6091                      tree type)
6092 {
6093   tree rname = NULL_TREE, tname = NULL_TREE;
6094   tree tdecl = NULL_TREE;
6095
6096   if ((int) rid_index < (int) RID_MAX)
6097     rname = ridpointers[(int) rid_index];
6098   if (name)
6099     tname = get_identifier (name);
6100
6101   TYPE_BUILT_IN (type) = 1;
6102
6103   if (tname)
6104     {
6105       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6106       set_identifier_type_value (tname, NULL_TREE);
6107       if ((int) rid_index < (int) RID_MAX)
6108         /* Built-in types live in the global namespace.  */
6109         SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6110     }
6111   if (rname != NULL_TREE)
6112     {
6113       if (tname != NULL_TREE)
6114         {
6115           set_identifier_type_value (rname, NULL_TREE);
6116           SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6117         }
6118       else
6119         {
6120           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6121           set_identifier_type_value (rname, NULL_TREE);
6122         }
6123     }
6124 }
6125
6126 /* Record one of the standard Java types.
6127  * Declare it as having the given NAME.
6128  * If SIZE > 0, it is the size of one of the integral types;
6129  * otherwise it is the negative of the size of one of the other types.  */
6130
6131 static tree
6132 record_builtin_java_type (const char* name, int size)
6133 {
6134   tree type, decl;
6135   if (size > 0)
6136     type = make_signed_type (size);
6137   else if (size > -32)
6138     { /* "__java_char" or ""__java_boolean".  */
6139       type = make_unsigned_type (-size);
6140       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6141     }
6142   else
6143     { /* "__java_float" or ""__java_double".  */
6144       type = make_node (REAL_TYPE);
6145       TYPE_PRECISION (type) = - size;
6146       layout_type (type);
6147     }
6148   record_builtin_type (RID_MAX, name, type);
6149   decl = TYPE_NAME (type);
6150
6151   /* Suppress generate debug symbol entries for these types,
6152      since for normal C++ they are just clutter.
6153      However, push_lang_context undoes this if extern "Java" is seen.  */
6154   DECL_IGNORED_P (decl) = 1;
6155
6156   TYPE_FOR_JAVA (type) = 1;
6157   return type;
6158 }
6159
6160 /* Push a type into the namespace so that the back-ends ignore it.  */
6161
6162 static void
6163 record_unknown_type (tree type, const char* name)
6164 {
6165   tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6166   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6167   DECL_IGNORED_P (decl) = 1;
6168   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6169   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6170   TYPE_ALIGN (type) = 1;
6171   TYPE_USER_ALIGN (type) = 0;
6172   TYPE_MODE (type) = TYPE_MODE (void_type_node);
6173 }
6174
6175 /* An string for which we should create an IDENTIFIER_NODE at
6176    startup.  */
6177
6178 typedef struct predefined_identifier
6179 {
6180   /* The name of the identifier.  */
6181   const char *const name;
6182   /* The place where the IDENTIFIER_NODE should be stored.  */
6183   tree *const node;
6184   /* Nonzero if this is the name of a constructor or destructor.  */
6185   const int ctor_or_dtor_p;
6186 } predefined_identifier;
6187
6188 /* Create all the predefined identifiers.  */
6189
6190 static void
6191 initialize_predefined_identifiers (void)
6192 {
6193   const predefined_identifier *pid;
6194
6195   /* A table of identifiers to create at startup.  */
6196   static const predefined_identifier predefined_identifiers[] = {
6197     { "C++", &lang_name_cplusplus, 0 },
6198     { "C", &lang_name_c, 0 },
6199     { "Java", &lang_name_java, 0 },
6200     { CTOR_NAME, &ctor_identifier, 1 },
6201     { "__base_ctor", &base_ctor_identifier, 1 },
6202     { "__comp_ctor", &complete_ctor_identifier, 1 },
6203     { DTOR_NAME, &dtor_identifier, 1 },
6204     { "__comp_dtor", &complete_dtor_identifier, 1 },
6205     { "__base_dtor", &base_dtor_identifier, 1 },
6206     { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6207     { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6208     { "nelts", &nelts_identifier, 0 },
6209     { THIS_NAME, &this_identifier, 0 },
6210     { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6211     { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6212     { "_vptr", &vptr_identifier, 0 },
6213     { "__vtt_parm", &vtt_parm_identifier, 0 },
6214     { "std", &std_identifier, 0 },
6215     { NULL, NULL, 0 }
6216   };
6217
6218   for (pid = predefined_identifiers; pid->name; ++pid)
6219     {
6220       *pid->node = get_identifier (pid->name);
6221       if (pid->ctor_or_dtor_p)
6222         IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6223     }
6224 }
6225
6226 /* Create the predefined scalar types of C,
6227    and some nodes representing standard constants (0, 1, (void *)0).
6228    Initialize the global binding level.
6229    Make definitions for built-in primitive functions.  */
6230
6231 void
6232 cxx_init_decl_processing (void)
6233 {
6234   tree void_ftype;
6235   tree void_ftype_ptr;
6236
6237   /* Create all the identifiers we need.  */
6238   initialize_predefined_identifiers ();
6239
6240   /* Fill in back-end hooks.  */
6241   lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6242
6243   /* Create the global variables.  */
6244   push_to_top_level ();
6245
6246   /* Enter the global namespace.  */
6247   my_friendly_assert (global_namespace == NULL_TREE, 375);
6248   push_namespace (get_identifier ("::"));
6249   global_namespace = current_namespace;
6250   current_lang_name = NULL_TREE;
6251
6252   /* Adjust various flags based on command-line settings.  */
6253   if (! flag_permissive && ! pedantic)
6254     flag_pedantic_errors = 1;
6255   if (!flag_no_inline)
6256     {
6257       flag_inline_trees = 1;
6258       flag_no_inline = 1;
6259     }
6260   if (flag_inline_functions)
6261     {
6262       flag_inline_trees = 2;
6263       flag_inline_functions = 0;
6264     }
6265
6266   /* Force minimum function alignment if using the least significant
6267      bit of function pointers to store the virtual bit.  */
6268   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6269       && force_align_functions_log < 1)
6270     force_align_functions_log = 1;
6271
6272   /* Initially, C.  */
6273   current_lang_name = lang_name_c;
6274
6275   current_function_decl = NULL_TREE;
6276   current_binding_level = NULL_BINDING_LEVEL;
6277   free_binding_level = NULL_BINDING_LEVEL;
6278
6279   build_common_tree_nodes (flag_signed_char);
6280
6281   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6282   TREE_TYPE (error_mark_list) = error_mark_node;
6283
6284   /* Make the binding_level structure for global names.  */
6285   pushlevel (0);
6286   global_binding_level = current_binding_level;
6287   /* The global level is the namespace level of ::.  */
6288   NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6289   declare_namespace_level ();
6290
6291   /* Create the `std' namespace.  */
6292   push_namespace (std_identifier);
6293   std_node = current_namespace;
6294   pop_namespace ();
6295
6296   c_common_nodes_and_builtins ();
6297
6298   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6299   java_short_type_node = record_builtin_java_type ("__java_short", 16);
6300   java_int_type_node = record_builtin_java_type ("__java_int", 32);
6301   java_long_type_node = record_builtin_java_type ("__java_long", 64);
6302   java_float_type_node = record_builtin_java_type ("__java_float", -32);
6303   java_double_type_node = record_builtin_java_type ("__java_double", -64);
6304   java_char_type_node = record_builtin_java_type ("__java_char", -16);
6305   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6306
6307   integer_two_node = build_int_2 (2, 0);
6308   TREE_TYPE (integer_two_node) = integer_type_node;
6309   integer_three_node = build_int_2 (3, 0);
6310   TREE_TYPE (integer_three_node) = integer_type_node;
6311
6312   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6313   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6314   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6315   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6316   TYPE_PRECISION (boolean_type_node) = 1;
6317   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6318   boolean_false_node = build_int_2 (0, 0);
6319   TREE_TYPE (boolean_false_node) = boolean_type_node;
6320   boolean_true_node = build_int_2 (1, 0);
6321   TREE_TYPE (boolean_true_node) = boolean_type_node;
6322
6323   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6324
6325 #if 0
6326   record_builtin_type (RID_MAX, NULL, string_type_node);
6327 #endif
6328
6329   delta_type_node = ptrdiff_type_node;
6330   vtable_index_type = ptrdiff_type_node;
6331
6332   vtt_parm_type = build_pointer_type (const_ptr_type_node);
6333   void_ftype = build_function_type (void_type_node, void_list_node);
6334   void_ftype_ptr = build_function_type (void_type_node,
6335                                         tree_cons (NULL_TREE,
6336                                                    ptr_type_node, 
6337                                                    void_list_node));
6338   void_ftype_ptr
6339     = build_exception_variant (void_ftype_ptr, empty_except_spec);
6340
6341   /* C++ extensions */
6342
6343   unknown_type_node = make_node (UNKNOWN_TYPE);
6344   record_unknown_type (unknown_type_node, "unknown type");
6345
6346   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6347   TREE_TYPE (unknown_type_node) = unknown_type_node;
6348
6349   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6350      result.  */
6351   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6352   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6353
6354   {
6355     /* Make sure we get a unique function type, so we can give
6356        its pointer type a name.  (This wins for gdb.) */
6357     tree vfunc_type = make_node (FUNCTION_TYPE);
6358     TREE_TYPE (vfunc_type) = integer_type_node;
6359     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6360     layout_type (vfunc_type);
6361
6362     vtable_entry_type = build_pointer_type (vfunc_type);
6363   }
6364   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6365
6366   vtbl_type_node
6367     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6368   layout_type (vtbl_type_node);
6369   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6370   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6371   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6372   layout_type (vtbl_ptr_type_node);
6373   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6374
6375   push_namespace (get_identifier ("__cxxabiv1"));
6376   abi_node = current_namespace;
6377   pop_namespace ();
6378
6379   global_type_node = make_node (LANG_TYPE);
6380   record_unknown_type (global_type_node, "global type");
6381
6382   /* Now, C++.  */
6383   current_lang_name = lang_name_cplusplus;
6384
6385   {
6386     tree bad_alloc_type_node, newtype, deltype;
6387     tree ptr_ftype_sizetype;
6388
6389     push_namespace (std_identifier);
6390     bad_alloc_type_node 
6391       = xref_tag (class_type, get_identifier ("bad_alloc"), 
6392                   /*attributes=*/NULL_TREE, 1);
6393     pop_namespace ();
6394     ptr_ftype_sizetype 
6395       = build_function_type (ptr_type_node,
6396                              tree_cons (NULL_TREE,
6397                                         size_type_node,
6398                                         void_list_node));
6399     newtype = build_exception_variant
6400       (ptr_ftype_sizetype, add_exception_specifier
6401        (NULL_TREE, bad_alloc_type_node, -1));
6402     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6403     push_cp_library_fn (NEW_EXPR, newtype);
6404     push_cp_library_fn (VEC_NEW_EXPR, newtype);
6405     global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6406     push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6407   }
6408
6409   abort_fndecl
6410     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6411
6412   /* Perform other language dependent initializations.  */
6413   init_class_processing ();
6414   init_search_processing ();
6415   init_rtti_processing ();
6416
6417   if (flag_exceptions)
6418     init_exception_processing ();
6419
6420   if (! supports_one_only ())
6421     flag_weak = 0;
6422
6423   make_fname_decl = cp_make_fname_decl;
6424   start_fname_decls ();
6425
6426   /* Show we use EH for cleanups.  */
6427   using_eh_for_cleanups ();
6428
6429   /* Maintain consistency.  Perhaps we should just complain if they
6430      say -fwritable-strings?  */
6431   if (flag_writable_strings)
6432     flag_const_strings = 0;
6433 }
6434
6435 /* Generate an initializer for a function naming variable from
6436    NAME. NAME may be NULL, in which case we generate a special
6437    ERROR_MARK node which should be replaced later.  */
6438
6439 tree
6440 cp_fname_init (const char* name)
6441 {
6442   tree domain = NULL_TREE;
6443   tree type;
6444   tree init = NULL_TREE;
6445   size_t length = 0;
6446
6447   if (name)
6448     {
6449       length = strlen (name);
6450       domain = build_index_type (size_int (length));
6451       init = build_string (length + 1, name);
6452     }
6453   
6454   type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6455   type = build_cplus_array_type (type, domain);
6456
6457   if (init)
6458     TREE_TYPE (init) = type;
6459   else
6460     /* We don't know the value until instantiation time. Make
6461        something which will be digested now, but replaced later.  */
6462     init = build (ERROR_MARK, type);
6463   
6464   return init;
6465 }
6466
6467 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6468    decl, NAME is the initialization string and TYPE_DEP indicates whether
6469    NAME depended on the type of the function. We make use of that to detect
6470    __PRETTY_FUNCTION__ inside a template fn. This is being done
6471    lazily at the point of first use, so we musn't push the decl now.  */
6472
6473 static tree
6474 cp_make_fname_decl (tree id, int type_dep)
6475 {
6476   const char *const name = (type_dep && processing_template_decl
6477                       ? NULL : fname_as_string (type_dep));
6478   tree init = cp_fname_init (name);
6479   tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6480
6481   /* As we're using pushdecl_with_scope, we must set the context.  */
6482   DECL_CONTEXT (decl) = current_function_decl;
6483   DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6484       
6485   TREE_STATIC (decl) = 1;
6486   TREE_READONLY (decl) = 1;
6487   DECL_ARTIFICIAL (decl) = 1;
6488   DECL_INITIAL (decl) = init;
6489   
6490   TREE_USED (decl) = 1;
6491
6492   if (current_function_decl)
6493     {
6494       struct cp_binding_level *b = current_binding_level;
6495       while (b->level_chain->parm_flag == 0)
6496         b = b->level_chain;
6497       pushdecl_with_scope (decl, b);
6498     }   
6499
6500   cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6501       
6502   return decl;
6503 }
6504
6505 /* Make a definition for a builtin function named NAME in the current
6506    namespace, whose data type is TYPE and whose context is CONTEXT.
6507    TYPE should be a function type with argument types.
6508
6509    CLASS and CODE tell later passes how to compile calls to this function.
6510    See tree.h for possible values.
6511
6512    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6513    the name to be called if we can't opencode the function.
6514    If ATTRS is nonzero, use that for the function's attribute
6515    list.  */
6516
6517 static tree
6518 builtin_function_1 (const char* name,
6519                     tree type,
6520                     tree context,
6521                     int code,
6522                     enum built_in_class class,
6523                     const char* libname,
6524                     tree attrs)
6525 {
6526   tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6527   DECL_BUILT_IN_CLASS (decl) = class;
6528   DECL_FUNCTION_CODE (decl) = code;
6529   DECL_CONTEXT (decl) = context;
6530
6531   pushdecl (decl);
6532
6533   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6534      we cannot change DECL_ASSEMBLER_NAME until we have installed this
6535      function in the namespace.  */
6536   if (libname)
6537     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6538   make_decl_rtl (decl, NULL);
6539
6540   /* Warn if a function in the namespace for users
6541      is used without an occasion to consider it declared.  */
6542   if (name[0] != '_' || name[1] != '_')
6543     DECL_ANTICIPATED (decl) = 1;
6544
6545   /* Possibly apply some default attributes to this built-in function.  */
6546   if (attrs)
6547     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6548   else
6549     decl_attributes (&decl, NULL_TREE, 0);
6550
6551   return decl;
6552 }
6553
6554 /* Entry point for the benefit of c_common_nodes_and_builtins.
6555
6556    Make a defintion for a builtin function named NAME and whose data type
6557    is TYPE.  TYPE should be a function type with argument types.  This
6558    function places the anticipated declaration in the global namespace
6559    and additionally in the std namespace if appropriate.
6560
6561    CLASS and CODE tell later passes how to compile calls to this function.
6562    See tree.h for possible values.
6563
6564    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6565    the name to be called if we can't opencode the function.
6566
6567    If ATTRS is nonzero, use that for the function's attribute
6568    list.  */
6569
6570 tree
6571 builtin_function (const char* name,
6572                   tree type,
6573                   int code,
6574                   enum built_in_class class,
6575                   const char* libname,
6576                   tree attrs)
6577 {
6578   /* All builtins that don't begin with an '_' should additionally
6579      go in the 'std' namespace.  */
6580   if (name[0] != '_')
6581     {
6582       push_namespace (std_identifier);
6583       builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6584       pop_namespace ();
6585     }
6586
6587   return builtin_function_1 (name, type, NULL_TREE, code,
6588                              class, libname, attrs);
6589 }
6590
6591 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6592    function.  Not called directly.  */
6593
6594 static tree
6595 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6596 {
6597   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6598   DECL_EXTERNAL (fn) = 1;
6599   TREE_PUBLIC (fn) = 1;
6600   DECL_ARTIFICIAL (fn) = 1;
6601   TREE_NOTHROW (fn) = 1;
6602   SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6603   SET_DECL_LANGUAGE (fn, lang_c);
6604   return fn;
6605 }
6606
6607 /* Returns the _DECL for a library function with C linkage.
6608    We assume that such functions never throw; if this is incorrect,
6609    callers should unset TREE_NOTHROW.  */
6610
6611 tree
6612 build_library_fn (tree name, tree type)
6613 {
6614   return build_library_fn_1 (name, ERROR_MARK, type);
6615 }
6616
6617 /* Returns the _DECL for a library function with C++ linkage.  */
6618
6619 static tree
6620 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6621 {
6622   tree fn = build_library_fn_1 (name, operator_code, type);
6623   TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6624   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6625   SET_DECL_LANGUAGE (fn, lang_cplusplus);
6626   set_mangled_name_for_decl (fn);
6627   return fn;
6628 }
6629
6630 /* Like build_library_fn, but takes a C string instead of an
6631    IDENTIFIER_NODE.  */
6632
6633 tree
6634 build_library_fn_ptr (const char* name, tree type)
6635 {
6636   return build_library_fn (get_identifier (name), type);
6637 }
6638
6639 /* Like build_cp_library_fn, but takes a C string instead of an
6640    IDENTIFIER_NODE.  */
6641
6642 tree
6643 build_cp_library_fn_ptr (const char* name, tree type)
6644 {
6645   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6646 }
6647
6648 /* Like build_library_fn, but also pushes the function so that we will
6649    be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
6650
6651 tree
6652 push_library_fn (tree name, tree type)
6653 {
6654   tree fn = build_library_fn (name, type);
6655   pushdecl_top_level (fn);
6656   return fn;
6657 }
6658
6659 /* Like build_cp_library_fn, but also pushes the function so that it
6660    will be found by normal lookup.  */
6661
6662 static tree
6663 push_cp_library_fn (enum tree_code operator_code, tree type)
6664 {
6665   tree fn = build_cp_library_fn (ansi_opname (operator_code),
6666                                  operator_code,
6667                                  type);
6668   pushdecl (fn);
6669   return fn;
6670 }
6671
6672 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6673    a FUNCTION_TYPE.  */
6674
6675 tree
6676 push_void_library_fn (tree name, tree parmtypes)
6677 {
6678   tree type = build_function_type (void_type_node, parmtypes);
6679   return push_library_fn (name, type);
6680 }
6681
6682 /* Like push_library_fn, but also note that this function throws
6683    and does not return.  Used for __throw_foo and the like.  */
6684
6685 tree
6686 push_throw_library_fn (tree name, tree type)
6687 {
6688   tree fn = push_library_fn (name, type);
6689   TREE_THIS_VOLATILE (fn) = 1;
6690   TREE_NOTHROW (fn) = 0;
6691   return fn;
6692 }
6693
6694 /* Apply default attributes to a function, if a system function with default
6695    attributes.  */
6696
6697 void
6698 cxx_insert_default_attributes (tree decl)
6699 {
6700   if (!DECL_EXTERN_C_FUNCTION_P (decl))
6701     return;
6702   if (!TREE_PUBLIC (decl))
6703     return;
6704   c_common_insert_default_attributes (decl);
6705 }
6706 \f
6707 /* When we call finish_struct for an anonymous union, we create
6708    default copy constructors and such.  But, an anonymous union
6709    shouldn't have such things; this function undoes the damage to the
6710    anonymous union type T.
6711
6712    (The reason that we create the synthesized methods is that we don't
6713    distinguish `union { int i; }' from `typedef union { int i; } U'.
6714    The first is an anonymous union; the second is just an ordinary
6715    union type.)  */
6716
6717 void
6718 fixup_anonymous_aggr (tree t)
6719 {
6720   tree *q;
6721
6722   /* Wipe out memory of synthesized methods */
6723   TYPE_HAS_CONSTRUCTOR (t) = 0;
6724   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6725   TYPE_HAS_INIT_REF (t) = 0;
6726   TYPE_HAS_CONST_INIT_REF (t) = 0;
6727   TYPE_HAS_ASSIGN_REF (t) = 0;
6728   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6729
6730   /* Splice the implicitly generated functions out of the TYPE_METHODS
6731      list.  */
6732   q = &TYPE_METHODS (t);
6733   while (*q)
6734     {
6735       if (DECL_ARTIFICIAL (*q))
6736         *q = TREE_CHAIN (*q);
6737       else
6738         q = &TREE_CHAIN (*q);
6739     }
6740
6741   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
6742   if (TYPE_METHODS (t))
6743     cp_error_at ("an anonymous union cannot have function members", t);
6744
6745   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6746      assignment operators (because they cannot have these methods themselves).
6747      For anonymous unions this is already checked because they are not allowed
6748      in any union, otherwise we have to check it.  */
6749   if (TREE_CODE (t) != UNION_TYPE)
6750     {
6751       tree field, type;
6752
6753       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6754         if (TREE_CODE (field) == FIELD_DECL)
6755           {
6756             type = TREE_TYPE (field);
6757             if (CLASS_TYPE_P (type))
6758               {
6759                 if (TYPE_NEEDS_CONSTRUCTING (type))
6760                   cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6761                                field);
6762                 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6763                   cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6764                                field);
6765                 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6766                   cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6767                                field);
6768               }
6769           }
6770     }
6771 }
6772
6773 /* Make sure that a declaration with no declarator is well-formed, i.e.
6774    just declares a tagged type or anonymous union.
6775
6776    Returns the type declared; or NULL_TREE if none.  */
6777
6778 tree
6779 check_tag_decl (tree declspecs)
6780 {
6781   int found_type = 0;
6782   int saw_friend = 0;
6783   int saw_typedef = 0;
6784   tree ob_modifier = NULL_TREE;
6785   register tree link;
6786   /* If a class, struct, or enum type is declared by the DECLSPECS
6787      (i.e, if a class-specifier, enum-specifier, or non-typename
6788      elaborated-type-specifier appears in the DECLSPECS),
6789      DECLARED_TYPE is set to the corresponding type.  */
6790   tree declared_type = NULL_TREE;
6791   bool error_p = false;
6792
6793   for (link = declspecs; link; link = TREE_CHAIN (link))
6794     {
6795       tree value = TREE_VALUE (link);
6796
6797       if (TYPE_P (value)
6798           || TREE_CODE (value) == TYPE_DECL
6799           || (TREE_CODE (value) == IDENTIFIER_NODE
6800               && IDENTIFIER_GLOBAL_VALUE (value)
6801               && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6802         {
6803           ++found_type;
6804
6805           if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6806             {
6807               if (! in_system_header)
6808                 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6809               return NULL_TREE;
6810             }
6811
6812           if (TYPE_P (value)
6813               && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6814                   || TREE_CODE (value) == ENUMERAL_TYPE))
6815             {
6816               my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6817               declared_type = value;
6818             }
6819         }
6820       else if (value == ridpointers[(int) RID_TYPEDEF])
6821         saw_typedef = 1;
6822       else if (value == ridpointers[(int) RID_FRIEND])
6823         {
6824           if (current_class_type == NULL_TREE
6825               || current_scope () != current_class_type)
6826             ob_modifier = value;
6827           else
6828             saw_friend = 1;
6829         }
6830       else if (value == ridpointers[(int) RID_STATIC]
6831                || value == ridpointers[(int) RID_EXTERN]
6832                || value == ridpointers[(int) RID_AUTO]
6833                || value == ridpointers[(int) RID_REGISTER]
6834                || value == ridpointers[(int) RID_INLINE]
6835                || value == ridpointers[(int) RID_VIRTUAL]
6836                || value == ridpointers[(int) RID_CONST]
6837                || value == ridpointers[(int) RID_VOLATILE]
6838                || value == ridpointers[(int) RID_EXPLICIT]
6839                || value == ridpointers[(int) RID_THREAD])
6840         ob_modifier = value;
6841       else if (value == error_mark_node)
6842         error_p = true;
6843     }
6844
6845   if (found_type > 1)
6846     error ("multiple types in one declaration");
6847
6848   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6849     pedwarn ("declaration does not declare anything");
6850   /* Check for an anonymous union.  */
6851   else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6852            && TYPE_ANONYMOUS_P (declared_type))
6853     {
6854       /* 7/3 In a simple-declaration, the optional init-declarator-list
6855          can be omitted only when declaring a class (clause 9) or
6856          enumeration (7.2), that is, when the decl-specifier-seq contains
6857          either a class-specifier, an elaborated-type-specifier with
6858          a class-key (9.1), or an enum-specifier.  In these cases and
6859          whenever a class-specifier or enum-specifier is present in the
6860          decl-specifier-seq, the identifiers in these specifiers are among
6861          the names being declared by the declaration (as class-name,
6862          enum-names, or enumerators, depending on the syntax).  In such
6863          cases, and except for the declaration of an unnamed bit-field (9.6),
6864          the decl-specifier-seq shall introduce one or more names into the
6865          program, or shall redeclare a name introduced by a previous
6866          declaration.  [Example:
6867              enum { };            // ill-formed
6868              typedef class { };   // ill-formed
6869          --end example]  */
6870       if (saw_typedef)
6871         {
6872           error ("missing type-name in typedef-declaration");
6873           return NULL_TREE;
6874         }
6875       /* Anonymous unions are objects, so they can have specifiers.  */;
6876       SET_ANON_AGGR_TYPE_P (declared_type);
6877
6878       if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 
6879           && !in_system_header)
6880         pedwarn ("ISO C++ prohibits anonymous structs");
6881     }
6882
6883   else if (ob_modifier)
6884     {
6885       if (ob_modifier == ridpointers[(int) RID_INLINE]
6886           || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6887         error ("`%D' can only be specified for functions", ob_modifier);
6888       else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6889         error ("`%D' can only be specified inside a class", ob_modifier);
6890       else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6891         error ("`%D' can only be specified for constructors",
6892                   ob_modifier);
6893       else
6894         error ("`%D' can only be specified for objects and functions",
6895                   ob_modifier);
6896     }
6897
6898   return declared_type;
6899 }
6900
6901 /* Called when a declaration is seen that contains no names to declare.
6902    If its type is a reference to a structure, union or enum inherited
6903    from a containing scope, shadow that tag name for the current scope
6904    with a forward reference.
6905    If its type defines a new named structure or union
6906    or defines an enum, it is valid but we need not do anything here.
6907    Otherwise, it is an error.
6908
6909    C++: may have to grok the declspecs to learn about static,
6910    complain for anonymous unions.  
6911
6912    Returns the TYPE declared -- or NULL_TREE if none.  */
6913
6914 tree
6915 shadow_tag (tree declspecs)
6916 {
6917   tree t = check_tag_decl (declspecs);
6918
6919   if (!t)
6920     return NULL_TREE;
6921
6922   maybe_process_partial_specialization (t);
6923
6924   /* This is where the variables in an anonymous union are
6925      declared.  An anonymous union declaration looks like:
6926      union { ... } ;
6927      because there is no declarator after the union, the parser
6928      sends that declaration here.  */
6929   if (ANON_AGGR_TYPE_P (t))
6930     {
6931       fixup_anonymous_aggr (t);
6932
6933       if (TYPE_FIELDS (t))
6934         {
6935           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6936                                       NULL);
6937           finish_anon_union (decl);
6938         }
6939     }
6940
6941   return t;
6942 }
6943 \f
6944 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
6945
6946 tree
6947 groktypename (tree typename)
6948 {
6949   tree specs, attrs;
6950   tree type;
6951   if (TREE_CODE (typename) != TREE_LIST)
6952     return typename;
6953   split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6954   type = grokdeclarator (TREE_VALUE (typename), specs,
6955                          TYPENAME, 0, &attrs);
6956   if (attrs)
6957     cplus_decl_attributes (&type, attrs, 0);
6958   return type;
6959 }
6960
6961 /* Decode a declarator in an ordinary declaration or data definition.
6962    This is called as soon as the type information and variable name
6963    have been parsed, before parsing the initializer if any.
6964    Here we create the ..._DECL node, fill in its type,
6965    and put it on the list of decls for the current context.
6966    The ..._DECL node is returned as the value.
6967
6968    Exception: for arrays where the length is not specified,
6969    the type is left null, to be filled in by `cp_finish_decl'.
6970
6971    Function definitions do not come here; they go to start_function
6972    instead.  However, external and forward declarations of functions
6973    do go through here.  Structure field declarations are done by
6974    grokfield and not through here.  */
6975
6976 tree
6977 start_decl (tree declarator, 
6978             tree declspecs, 
6979             int initialized, 
6980             tree attributes, 
6981             tree prefix_attributes)
6982 {
6983   tree decl;
6984   register tree type, tem;
6985   tree context;
6986
6987   /* This should only be done once on the top most decl.  */
6988   if (have_extern_spec)
6989     {
6990       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6991                              declspecs);
6992       have_extern_spec = false;
6993     }
6994
6995   /* An object declared as __attribute__((deprecated)) suppresses
6996      warnings of uses of other deprecated items.  */
6997   if (lookup_attribute ("deprecated", attributes))
6998     deprecated_state = DEPRECATED_SUPPRESS;
6999
7000   attributes = chainon (attributes, prefix_attributes);
7001
7002   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7003                          &attributes);
7004
7005   deprecated_state = DEPRECATED_NORMAL;
7006
7007   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7008     return NULL_TREE;
7009
7010   type = TREE_TYPE (decl);
7011
7012   if (type == error_mark_node)
7013     return NULL_TREE;
7014
7015   context = DECL_CONTEXT (decl);
7016
7017   if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7018       && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7019     {
7020       /* When parsing the initializer, lookup should use the object's
7021          namespace.  */
7022       push_decl_namespace (context);
7023     }
7024
7025   /* We are only interested in class contexts, later.  */
7026   if (context && TREE_CODE (context) == NAMESPACE_DECL)
7027     context = NULL_TREE;
7028
7029   if (initialized)
7030     /* Is it valid for this decl to have an initializer at all?
7031        If not, set INITIALIZED to zero, which will indirectly
7032        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7033     switch (TREE_CODE (decl))
7034       {
7035       case TYPE_DECL:
7036         error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7037         initialized = 0;
7038         break;
7039
7040       case FUNCTION_DECL:
7041         error ("function `%#D' is initialized like a variable", decl);
7042         initialized = 0;
7043         break;
7044
7045       default:
7046         break;
7047       }
7048
7049   if (initialized)
7050     {
7051       if (! toplevel_bindings_p ()
7052           && DECL_EXTERNAL (decl))
7053         warning ("declaration of `%#D' has `extern' and is initialized",
7054                     decl);
7055       DECL_EXTERNAL (decl) = 0;
7056       if (toplevel_bindings_p ())
7057         TREE_STATIC (decl) = 1;
7058
7059       /* Tell `pushdecl' this is an initialized decl
7060          even though we don't yet have the initializer expression.
7061          Also tell `cp_finish_decl' it may store the real initializer.  */
7062       DECL_INITIAL (decl) = error_mark_node;
7063     }
7064
7065   /* Set attributes here so if duplicate decl, will have proper attributes.  */
7066   cplus_decl_attributes (&decl, attributes, 0);
7067
7068   /* If #pragma weak was used, mark the decl weak now.  */
7069   if (current_binding_level == global_binding_level)
7070     maybe_apply_pragma_weak (decl);
7071
7072   if (TREE_CODE (decl) == FUNCTION_DECL
7073       && DECL_DECLARED_INLINE_P (decl)
7074       && DECL_UNINLINABLE (decl)
7075       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7076     warning_with_decl (decl,
7077                        "inline function `%s' given attribute noinline");
7078
7079   if (context && COMPLETE_TYPE_P (complete_type (context)))
7080     {
7081       push_nested_class (context, 2);
7082
7083       if (TREE_CODE (decl) == VAR_DECL)
7084         {
7085           tree field = lookup_field (context, DECL_NAME (decl), 0, 0);
7086           if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7087             error ("`%#D' is not a static member of `%#T'", decl, context);
7088           else
7089             {
7090               if (DECL_CONTEXT (field) != context)
7091                 {
7092                   if (!same_type_p (DECL_CONTEXT (field), context))
7093                     pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7094                              DECL_CONTEXT (field), DECL_NAME (decl),
7095                              context, DECL_NAME (decl));
7096                   DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7097                 }
7098               /* Static data member are tricky; an in-class initialization
7099                  still doesn't provide a definition, so the in-class
7100                  declaration will have DECL_EXTERNAL set, but will have an
7101                  initialization.  Thus, duplicate_decls won't warn
7102                  about this situation, and so we check here.  */
7103               if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7104                 error ("duplicate initialization of %D", decl);
7105               if (duplicate_decls (decl, field))
7106                 decl = field;
7107             }
7108         }
7109       else
7110         {
7111           tree field = check_classfn (context, decl);
7112           if (field && duplicate_decls (decl, field))
7113             decl = field;
7114         }
7115
7116       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7117       DECL_IN_AGGR_P (decl) = 0;
7118       if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7119           || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7120         {
7121           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7122           /* [temp.expl.spec] An explicit specialization of a static data
7123              member of a template is a definition if the declaration
7124              includes an initializer; otherwise, it is a declaration.
7125
7126              We check for processing_specialization so this only applies
7127              to the new specialization syntax.  */
7128           if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7129             DECL_EXTERNAL (decl) = 1;
7130         }
7131
7132       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7133         pedwarn ("declaration of `%#D' outside of class is not definition",
7134                     decl);
7135     }
7136
7137   /* Enter this declaration into the symbol table.  */
7138   tem = maybe_push_decl (decl);
7139
7140   if (processing_template_decl)
7141     tem = push_template_decl (tem);
7142
7143 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7144   /* Tell the back-end to use or not use .common as appropriate.  If we say
7145      -fconserve-space, we want this to save .data space, at the expense of
7146      wrong semantics.  If we say -fno-conserve-space, we want this to
7147      produce errors about redefs; to do this we force variables into the
7148      data segment.  */
7149   DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7150                         || !DECL_THREAD_LOCAL (tem))
7151                        && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7152 #endif
7153
7154   if (! processing_template_decl)
7155     start_decl_1 (tem);
7156
7157   return tem;
7158 }
7159
7160 void
7161 start_decl_1 (tree decl)
7162 {
7163   tree type = TREE_TYPE (decl);
7164   int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7165
7166   if (type == error_mark_node)
7167     return;
7168
7169   maybe_push_cleanup_level (type);
7170
7171   if (initialized)
7172     /* Is it valid for this decl to have an initializer at all?
7173        If not, set INITIALIZED to zero, which will indirectly
7174        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7175     {
7176       /* Don't allow initializations for incomplete types except for
7177          arrays which might be completed by the initialization.  */
7178       if (COMPLETE_TYPE_P (complete_type (type)))
7179         ;                       /* A complete type is ok.  */
7180       else if (TREE_CODE (type) != ARRAY_TYPE)
7181         {
7182           error ("variable `%#D' has initializer but incomplete type",
7183                     decl);
7184           initialized = 0;
7185           type = TREE_TYPE (decl) = error_mark_node;
7186         }
7187       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7188         {
7189           if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7190             error ("elements of array `%#D' have incomplete type", decl);
7191           /* else we already gave an error in start_decl.  */
7192           initialized = 0;
7193         }
7194     }
7195
7196   if (!initialized
7197       && TREE_CODE (decl) != TYPE_DECL
7198       && TREE_CODE (decl) != TEMPLATE_DECL
7199       && type != error_mark_node
7200       && IS_AGGR_TYPE (type)
7201       && ! DECL_EXTERNAL (decl))
7202     {
7203       if ((! processing_template_decl || ! uses_template_parms (type))
7204           && !COMPLETE_TYPE_P (complete_type (type)))
7205         {
7206           error ("aggregate `%#D' has incomplete type and cannot be defined",
7207                  decl);
7208           /* Change the type so that assemble_variable will give
7209              DECL an rtl we can live with: (mem (const_int 0)).  */
7210           type = TREE_TYPE (decl) = error_mark_node;
7211         }
7212       else
7213         {
7214           /* If any base type in the hierarchy of TYPE needs a constructor,
7215              then we set initialized to 1.  This way any nodes which are
7216              created for the purposes of initializing this aggregate
7217              will live as long as it does.  This is necessary for global
7218              aggregates which do not have their initializers processed until
7219              the end of the file.  */
7220           initialized = TYPE_NEEDS_CONSTRUCTING (type);
7221         }
7222     }
7223
7224   if (! initialized)
7225     DECL_INITIAL (decl) = NULL_TREE;
7226 }
7227
7228 /* Handle initialization of references.
7229    These three arguments are from `cp_finish_decl', and have the
7230    same meaning here that they do there.
7231
7232    Quotes on semantics can be found in ARM 8.4.3.  */
7233
7234 static tree
7235 grok_reference_init (tree decl, tree type, tree init)
7236 {
7237   tree tmp;
7238
7239   if (init == NULL_TREE)
7240     {
7241       if ((DECL_LANG_SPECIFIC (decl) == 0
7242            || DECL_IN_AGGR_P (decl) == 0)
7243           && ! DECL_THIS_EXTERN (decl))
7244         error ("`%D' declared as reference but not initialized", decl);
7245       return NULL_TREE;
7246     }
7247
7248   if (TREE_CODE (init) == CONSTRUCTOR)
7249     {
7250       error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7251       return NULL_TREE;
7252     }
7253
7254   if (TREE_CODE (init) == TREE_LIST)
7255     init = build_compound_expr (init);
7256
7257   if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7258     init = convert_from_reference (init);
7259
7260   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7261       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7262     {
7263       /* Note: default conversion is only called in very special cases.  */
7264       init = default_conversion (init);
7265     }
7266
7267   /* Convert INIT to the reference type TYPE.  This may involve the
7268      creation of a temporary, whose lifetime must be the same as that
7269      of the reference.  If so, a DECL_STMT for the temporary will be
7270      added just after the DECL_STMT for DECL.  That's why we don't set
7271      DECL_INITIAL for local references (instead assigning to them
7272      explicitly); we need to allow the temporary to be initialized
7273      first.  */
7274   tmp = convert_to_reference
7275     (type, init, CONV_IMPLICIT,
7276      LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND,
7277      decl);
7278
7279   if (tmp == error_mark_node)
7280     return NULL_TREE;
7281   else if (tmp == NULL_TREE)
7282     {
7283       error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7284       return NULL_TREE;
7285     }
7286
7287   if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7288     return tmp;
7289
7290   DECL_INITIAL (decl) = tmp;
7291
7292   return NULL_TREE;
7293 }
7294
7295 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7296    array until we finish parsing the initializer.  If that's the
7297    situation we're in, update DECL accordingly.  */
7298
7299 static void
7300 maybe_deduce_size_from_array_init (tree decl, tree init)
7301 {
7302   tree type = TREE_TYPE (decl);
7303
7304   if (TREE_CODE (type) == ARRAY_TYPE
7305       && TYPE_DOMAIN (type) == NULL_TREE
7306       && TREE_CODE (decl) != TYPE_DECL)
7307     {
7308       /* do_default is really a C-ism to deal with tentative definitions.
7309          But let's leave it here to ease the eventual merge.  */
7310       int do_default = !DECL_EXTERNAL (decl);
7311       tree initializer = init ? init : DECL_INITIAL (decl);
7312       int failure = complete_array_type (type, initializer, do_default);
7313
7314       if (failure == 1)
7315         error ("initializer fails to determine size of `%D'", decl);
7316
7317       if (failure == 2)
7318         {
7319           if (do_default)
7320             error ("array size missing in `%D'", decl);
7321           /* If a `static' var's size isn't known, make it extern as
7322              well as static, so it does not get allocated.  If it's not
7323              `static', then don't mark it extern; finish_incomplete_decl
7324              will give it a default size and it will get allocated.  */
7325           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7326             DECL_EXTERNAL (decl) = 1;
7327         }
7328
7329       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7330           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7331                               integer_zero_node))
7332         error ("zero-size array `%D'", decl);
7333
7334       layout_decl (decl, 0);
7335     }
7336 }
7337
7338 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7339    any appropriate error messages regarding the layout.  */
7340
7341 static void
7342 layout_var_decl (tree decl)
7343 {
7344   tree type = TREE_TYPE (decl);
7345 #if 0
7346   tree ttype = target_type (type);
7347 #endif
7348
7349   /* If we haven't already layed out this declaration, do so now.
7350      Note that we must not call complete type for an external object
7351      because it's type might involve templates that we are not
7352      supposed to isntantiate yet.  (And it's perfectly valid to say
7353      `extern X x' for some incomplete type `X'.)  */
7354   if (!DECL_EXTERNAL (decl))
7355     complete_type (type);
7356   if (!DECL_SIZE (decl) 
7357       && TREE_TYPE (decl) != error_mark_node
7358       && (COMPLETE_TYPE_P (type)
7359           || (TREE_CODE (type) == ARRAY_TYPE 
7360               && !TYPE_DOMAIN (type)
7361               && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7362     layout_decl (decl, 0);
7363
7364   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7365     {
7366       /* An automatic variable with an incomplete type: that is an error.
7367          Don't talk about array types here, since we took care of that
7368          message in grokdeclarator.  */
7369       error ("storage size of `%D' isn't known", decl);
7370       TREE_TYPE (decl) = error_mark_node;
7371     }
7372 #if 0
7373   /* Keep this code around in case we later want to control debug info
7374      based on whether a type is "used".  (jason 1999-11-11) */
7375
7376   else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7377     /* Let debugger know it should output info for this type.  */
7378     note_debug_info_needed (ttype);
7379
7380   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7381     note_debug_info_needed (DECL_CONTEXT (decl));
7382 #endif
7383
7384   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7385       && DECL_SIZE (decl) != NULL_TREE
7386       && ! TREE_CONSTANT (DECL_SIZE (decl)))
7387     {
7388       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7389         constant_expression_warning (DECL_SIZE (decl));
7390       else
7391         error ("storage size of `%D' isn't constant", decl);
7392     }
7393
7394   if (TREE_STATIC (decl)
7395       && !DECL_ARTIFICIAL (decl)
7396       && current_function_decl
7397       && DECL_CONTEXT (decl) == current_function_decl)
7398     push_local_name (decl);
7399 }
7400
7401 /* If a local static variable is declared in an inline function, or if
7402    we have a weak definition, we must endeavor to create only one
7403    instance of the variable at link-time.  */
7404
7405 static void
7406 maybe_commonize_var (tree decl)
7407 {
7408   /* Static data in a function with comdat linkage also has comdat
7409      linkage.  */
7410   if (TREE_STATIC (decl)
7411       /* Don't mess with __FUNCTION__.  */
7412       && ! DECL_ARTIFICIAL (decl)
7413       && current_function_decl
7414       && DECL_CONTEXT (decl) == current_function_decl
7415       && (DECL_DECLARED_INLINE_P (current_function_decl)
7416           || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7417       && TREE_PUBLIC (current_function_decl))
7418     {
7419       /* If flag_weak, we don't need to mess with this, as we can just
7420          make the function weak, and let it refer to its unique local
7421          copy.  This works because we don't allow the function to be
7422          inlined.  */
7423       if (! flag_weak)
7424         {
7425           if (DECL_INTERFACE_KNOWN (current_function_decl))
7426             {
7427               TREE_PUBLIC (decl) = 1;
7428               DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7429             }
7430           else if (DECL_INITIAL (decl) == NULL_TREE
7431                    || DECL_INITIAL (decl) == error_mark_node)
7432             {
7433               TREE_PUBLIC (decl) = 1;
7434               DECL_COMMON (decl) = 1;
7435             }
7436           /* else we lose. We can only do this if we can use common,
7437              which we can't if it has been initialized.  */
7438
7439           if (!TREE_PUBLIC (decl))
7440             {
7441               cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7442               cp_warning_at ("  you can work around this by removing the initializer", decl);
7443             }
7444         }
7445       else
7446         comdat_linkage (decl);
7447     }
7448   else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7449     /* Set it up again; we might have set DECL_INITIAL since the last
7450        time.  */
7451     comdat_linkage (decl);
7452 }
7453
7454 /* Issue an error message if DECL is an uninitialized const variable.  */
7455
7456 static void
7457 check_for_uninitialized_const_var (tree decl)
7458 {
7459   tree type = TREE_TYPE (decl);
7460
7461   /* ``Unless explicitly declared extern, a const object does not have
7462      external linkage and must be initialized. ($8.4; $12.1)'' ARM
7463      7.1.6 */
7464   if (TREE_CODE (decl) == VAR_DECL
7465       && TREE_CODE (type) != REFERENCE_TYPE
7466       && CP_TYPE_CONST_P (type)
7467       && !TYPE_NEEDS_CONSTRUCTING (type)
7468       && !DECL_INITIAL (decl))
7469     error ("uninitialized const `%D'", decl);
7470 }
7471
7472 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
7473    returned is the next FIELD_DECL (possibly FIELD itself) that can be
7474    initialized.  If there are no more such fields, the return value
7475    will be NULL.  */
7476
7477 static tree
7478 next_initializable_field (tree field)
7479 {
7480   while (field
7481          && (TREE_CODE (field) != FIELD_DECL
7482              || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7483              || DECL_ARTIFICIAL (field)))
7484     field = TREE_CHAIN (field);
7485
7486   return field;
7487 }
7488
7489 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7490    brace-enclosed aggregate initializer.
7491
7492    *INITP is one of a list of initializers describing a brace-enclosed
7493    initializer for an entity of the indicated aggregate TYPE.  It may
7494    not presently match the shape of the TYPE; for example:
7495    
7496      struct S { int a; int b; };
7497      struct S a[] = { 1, 2, 3, 4 };
7498
7499    Here *INITP will point to TREE_LIST of four elements, rather than a
7500    list of two elements, each itself a list of two elements.  This
7501    routine transforms INIT from the former form into the latter.  The
7502    revised initializer is returned.  */
7503
7504 static tree
7505 reshape_init (tree type, tree *initp)
7506 {
7507   tree inits;
7508   tree old_init;
7509   tree old_init_value;
7510   tree new_init;
7511   bool brace_enclosed_p;
7512
7513   old_init = *initp;
7514   old_init_value = (TREE_CODE (*initp) == TREE_LIST
7515                     ? TREE_VALUE (*initp) : old_init);
7516
7517   /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
7518   if (!old_init_value)
7519     {
7520       my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7521       TREE_VALUE (old_init) = error_mark_node;
7522       return old_init;
7523     }
7524
7525   /* If the initializer is brace-enclosed, pull initializers from the
7526      enclosed elements.  Advance past the brace-enclosed initializer
7527      now.  */
7528   if (TREE_CODE (old_init_value) == CONSTRUCTOR 
7529       && TREE_HAS_CONSTRUCTOR (old_init_value))
7530     {
7531       *initp = TREE_CHAIN (old_init);
7532       TREE_CHAIN (old_init) = NULL_TREE;
7533       inits = CONSTRUCTOR_ELTS (old_init_value);
7534       initp = &inits;
7535       brace_enclosed_p = true;
7536     }
7537   else
7538     {
7539       inits = NULL_TREE;
7540       brace_enclosed_p = false;
7541     }
7542
7543   /* A non-aggregate type is always initialized with a single
7544      initializer.  */
7545   if (!CP_AGGREGATE_TYPE_P (type))
7546       {
7547         *initp = TREE_CHAIN (old_init);
7548         TREE_CHAIN (old_init) = NULL_TREE;
7549         /* It is invalid to initialize a non-aggregate type with a
7550            brace-enclosed initializer.  */
7551         if (brace_enclosed_p)
7552           {
7553             error ("brace-enclosed initializer used to initialize `%T'",
7554                    type);
7555             if (TREE_CODE (old_init) == TREE_LIST)
7556               TREE_VALUE (old_init) = error_mark_node;
7557             else
7558               old_init = error_mark_node;
7559           }
7560         
7561         return old_init;
7562       }
7563
7564   /* [dcl.init.aggr]
7565
7566      All implicit type conversions (clause _conv_) are considered when
7567      initializing the aggregate member with an initializer from an
7568      initializer-list.  If the initializer can initialize a member,
7569      the member is initialized.  Otherwise, if the member is itself a
7570      non-empty subaggregate, brace elision is assumed and the
7571      initializer is considered for the initialization of the first
7572      member of the subaggregate.  */
7573   if (CLASS_TYPE_P (type) 
7574       && !brace_enclosed_p
7575       && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7576     {
7577       *initp = TREE_CHAIN (old_init);
7578       TREE_CHAIN (old_init) = NULL_TREE;
7579       return old_init;
7580     }
7581
7582   if (TREE_CODE (old_init_value) == STRING_CST
7583       && TREE_CODE (type) == ARRAY_TYPE
7584       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7585     {
7586       /* [dcl.init.string]
7587
7588          A char array (whether plain char, signed char, or unsigned char)
7589          can be initialized by a string-literal (optionally enclosed in
7590          braces); a wchar_t array can be initialized by a wide
7591          string-literal (optionally enclosed in braces).  */
7592       new_init = old_init;
7593       /* Move past the initializer.  */
7594       *initp = TREE_CHAIN (old_init);
7595       TREE_CHAIN (old_init) = NULL_TREE;
7596     }
7597   else
7598     {
7599       /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */  
7600       new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
7601       TREE_HAS_CONSTRUCTOR (new_init) = 1;
7602
7603       if (CLASS_TYPE_P (type))
7604         {
7605           tree field;
7606
7607           field = next_initializable_field (TYPE_FIELDS (type));
7608
7609           if (!field)
7610             {
7611               /* [dcl.init.aggr]
7612               
7613                  An initializer for an aggregate member that is an
7614                  empty class shall have the form of an empty
7615                  initializer-list {}.  */
7616               if (!brace_enclosed_p)
7617                 error ("initializer for `%T' must be brace-enclosed",
7618                        type);
7619             }
7620           else
7621             {
7622               /* Loop through the initializable fields, gathering
7623                  initializers.  */
7624               while (*initp && field)
7625                 {
7626                   tree field_init;
7627
7628                   field_init = reshape_init (TREE_TYPE (field), initp);
7629                   TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7630                   CONSTRUCTOR_ELTS (new_init) = field_init;
7631                   /* [dcl.init.aggr] 
7632
7633                      When a union  is  initialized with a brace-enclosed
7634                      initializer, the braces shall only contain an
7635                      initializer for the first member of the union.  */
7636                   if (TREE_CODE (type) == UNION_TYPE)
7637                     break;
7638                   if (TREE_PURPOSE (field_init))
7639                     field = TREE_PURPOSE (field_init);
7640                   field = next_initializable_field (TREE_CHAIN (field));
7641                 }
7642             }
7643         }
7644       else if (TREE_CODE (type) == ARRAY_TYPE)
7645         {
7646           tree index;
7647           tree max_index;
7648
7649           /* If the bound of the array is known, take no more initializers
7650              than are allowed.  */
7651           max_index = (TYPE_DOMAIN (type) 
7652                        ? array_type_nelts (type) : NULL_TREE);
7653           /* Loop through the array elements, gathering initializers.  */
7654           for (index = size_zero_node;
7655                *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7656                index = size_binop (PLUS_EXPR, index, size_one_node))
7657             {
7658               tree element_init;
7659
7660               element_init = reshape_init (TREE_TYPE (type), initp);
7661               TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7662               CONSTRUCTOR_ELTS (new_init) = element_init;
7663               if (TREE_PURPOSE (element_init))
7664                 index = TREE_PURPOSE (element_init);
7665             }
7666         }
7667       else
7668         abort ();
7669
7670       /* The initializers were placed in reverse order in the
7671          CONSTRUCTOR.  */
7672       CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7673
7674       if (TREE_CODE (old_init) == TREE_LIST)
7675         new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7676     }
7677
7678   /* If this was a brace-enclosed initializer and all of the
7679      initializers were not used up, there is a problem.  */
7680   if (brace_enclosed_p && *initp)
7681     error ("too many initializers for `%T'", type);
7682
7683   return new_init;
7684 }
7685
7686 /* Verify INIT (the initializer for DECL), and record the
7687    initialization in DECL_INITIAL, if appropriate.  
7688
7689    If the return value is non-NULL, it is an expression that must be
7690    evaluated dynamically to initialize DECL.  */
7691
7692 static tree
7693 check_initializer (tree decl, tree init, int flags)
7694 {
7695   tree type = TREE_TYPE (decl);
7696
7697   /* If `start_decl' didn't like having an initialization, ignore it now.  */
7698   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7699     init = NULL_TREE;
7700
7701   /* If an initializer is present, DECL_INITIAL has been
7702      error_mark_node, to indicate that an as-of-yet unevaluated
7703      initialization will occur.  From now on, DECL_INITIAL reflects
7704      the static initialization -- if any -- of DECL.  */
7705   DECL_INITIAL (decl) = NULL_TREE;
7706
7707   /* Things that are going to be initialized need to have complete
7708      type.  */
7709   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7710
7711   if (type == error_mark_node)
7712     /* We will have already complained.  */
7713     init = NULL_TREE;
7714   else if (init && COMPLETE_TYPE_P (type) 
7715            && !TREE_CONSTANT (TYPE_SIZE (type)))
7716     {
7717       error ("variable-sized object `%D' may not be initialized", decl);
7718       init = NULL_TREE;
7719     }
7720   else if (TREE_CODE (type) == ARRAY_TYPE
7721            && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7722     {
7723       error ("elements of array `%#D' have incomplete type", decl);
7724       init = NULL_TREE;
7725     }
7726   else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7727     {
7728       error ("`%D' has incomplete type", decl);
7729       TREE_TYPE (decl) = error_mark_node;
7730       init = NULL_TREE;
7731     }
7732
7733   if (TREE_CODE (decl) == CONST_DECL)
7734     {
7735       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7736
7737       DECL_INITIAL (decl) = init;
7738
7739       my_friendly_assert (init != NULL_TREE, 149);
7740       init = NULL_TREE;
7741     }
7742   else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7743     init = grok_reference_init (decl, type, init);
7744   else if (init)
7745     {
7746       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7747         {
7748           /* [dcl.init] paragraph 13,
7749              If T is a scalar type, then a declaration of the form
7750              T x = { a };
7751              is equivalent to
7752              T x = a;
7753              
7754              reshape_init will complain about the extra braces,
7755              and doesn't do anything useful in the case where TYPE is
7756              scalar, so just don't call it.  */
7757           if (CP_AGGREGATE_TYPE_P (type))
7758             init = reshape_init (type, &init);
7759         }
7760
7761       /* If DECL has an array type without a specific bound, deduce the
7762          array size from the initializer.  */
7763       maybe_deduce_size_from_array_init (decl, init);
7764       type = TREE_TYPE (decl);
7765       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7766         TREE_TYPE (init) = type;
7767
7768       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7769         {
7770           if (TREE_CODE (type) == ARRAY_TYPE)
7771             goto initialize_aggr;
7772           else if (TREE_CODE (init) == CONSTRUCTOR
7773                    && TREE_HAS_CONSTRUCTOR (init))
7774             {
7775               if (TYPE_NON_AGGREGATE_CLASS (type))
7776                 {
7777                   error ("`%D' must be initialized by constructor, not by `{...}'",
7778                          decl);
7779                   init = error_mark_node;
7780                 }
7781               else
7782                 goto dont_use_constructor;
7783             }
7784           else
7785             {
7786               int saved_stmts_are_full_exprs_p;
7787
7788             initialize_aggr:
7789               saved_stmts_are_full_exprs_p = 0;
7790               if (building_stmt_tree ())
7791                 {
7792                   saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7793                   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7794                 }
7795               init = build_aggr_init (decl, init, flags);
7796               if (building_stmt_tree ())
7797                 current_stmt_tree ()->stmts_are_full_exprs_p =
7798                   saved_stmts_are_full_exprs_p;
7799               return init;
7800             }
7801         }
7802       else
7803         {
7804         dont_use_constructor:
7805           if (TREE_CODE (init) != TREE_VEC)
7806             init = store_init_value (decl, init);
7807         }
7808     }
7809   else if (DECL_EXTERNAL (decl))
7810     ;
7811   else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7812     goto initialize_aggr;
7813   else if (IS_AGGR_TYPE (type))
7814     {
7815       tree core_type = strip_array_types (type);
7816
7817       if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7818         error ("structure `%D' with uninitialized const members", decl);
7819       if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7820         error ("structure `%D' with uninitialized reference members",
7821                decl);
7822
7823       check_for_uninitialized_const_var (decl);
7824     }
7825   else
7826     check_for_uninitialized_const_var (decl);
7827
7828   if (init && init != error_mark_node)
7829     init = build (INIT_EXPR, type, decl, init);
7830
7831   return init;
7832 }
7833
7834 /* If DECL is not a local variable, give it RTL.  */
7835
7836 static void
7837 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7838 {
7839   int toplev = toplevel_bindings_p ();
7840   int defer_p;
7841
7842   /* Handle non-variables up front.  */
7843   if (TREE_CODE (decl) != VAR_DECL)
7844     {
7845       rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7846       return;
7847     }
7848
7849   /* If we see a class member here, it should be a static data
7850      member.  */
7851   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7852     {
7853       my_friendly_assert (TREE_STATIC (decl), 19990828);
7854       /* An in-class declaration of a static data member should be
7855          external; it is only a declaration, and not a definition.  */
7856       if (init == NULL_TREE)
7857         my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7858     }
7859
7860   /* Set the DECL_ASSEMBLER_NAME for the variable.  */
7861   if (asmspec)
7862     {
7863       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7864       /* The `register' keyword, when used together with an
7865          asm-specification, indicates that the variable should be
7866          placed in a particular register.  */
7867       if (DECL_REGISTER (decl))
7868         DECL_C_HARD_REGISTER (decl) = 1;
7869     }
7870
7871   /* We don't create any RTL for local variables.  */
7872   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7873     return;
7874
7875   /* We defer emission of local statics until the corresponding
7876      DECL_STMT is expanded.  */
7877   defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7878
7879   /* We try to defer namespace-scope static constants so that they are
7880      not emitted into the object file unnecessarily.  */
7881   if (!DECL_VIRTUAL_P (decl)
7882       && TREE_READONLY (decl)
7883       && DECL_INITIAL (decl) != NULL_TREE
7884       && DECL_INITIAL (decl) != error_mark_node
7885       && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7886       && toplev
7887       && !TREE_PUBLIC (decl))
7888     {
7889       /* Fool with the linkage of static consts according to #pragma
7890          interface.  */
7891       if (!interface_unknown && !TREE_PUBLIC (decl))
7892         {
7893           TREE_PUBLIC (decl) = 1;
7894           DECL_EXTERNAL (decl) = interface_only;
7895         }
7896
7897       defer_p = 1;
7898     }
7899   /* Likewise for template instantiations.  */
7900   else if (DECL_COMDAT (decl))
7901     defer_p = 1;
7902
7903   /* If we're deferring the variable, we only need to make RTL if
7904      there's an ASMSPEC.  Otherwise, we'll lazily create it later when
7905      we need it.  (There's no way to lazily create RTL for things that
7906      have assembly specs because the information about the specifier
7907      isn't stored in the tree, yet)  */
7908   if (defer_p && asmspec)
7909     make_decl_rtl (decl, asmspec);
7910   /* If we're not deferring, go ahead and assemble the variable.  */
7911   else if (!defer_p)
7912     rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7913 }
7914
7915 /* The old ARM scoping rules injected variables declared in the
7916    initialization statement of a for-statement into the surrounding
7917    scope.  We support this usage, in order to be backward-compatible.
7918    DECL is a just-declared VAR_DECL; if necessary inject its
7919    declaration into the surrounding scope.  */
7920
7921 void
7922 maybe_inject_for_scope_var (tree decl)
7923 {
7924   if (!DECL_NAME (decl))
7925     return;
7926   
7927   /* Declarations of __FUNCTION__ and its ilk appear magically when
7928      the variable is first used.  If that happens to be inside a
7929      for-loop, we don't want to do anything special.  */
7930   if (DECL_PRETTY_FUNCTION_P (decl))
7931     return;
7932
7933   if (current_binding_level->is_for_scope)
7934     {
7935       struct cp_binding_level *outer
7936         = current_binding_level->level_chain;
7937
7938       /* Check to see if the same name is already bound at the outer
7939          level, either because it was directly declared, or because a
7940          dead for-decl got preserved.  In either case, the code would
7941          not have been valid under the ARM scope rules, so clear
7942          is_for_scope for the current_binding_level.
7943
7944          Otherwise, we need to preserve the temp slot for decl to last
7945          into the outer binding level.  */
7946
7947       tree outer_binding
7948         = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7949
7950       if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7951           && (TREE_CODE (BINDING_VALUE (outer_binding))
7952               == VAR_DECL)
7953           && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7954         {
7955           BINDING_VALUE (outer_binding)
7956             = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7957           current_binding_level->is_for_scope = 0;
7958         }
7959     }
7960 }
7961
7962 /* Generate code to initialize DECL (a local variable).  */
7963
7964 static void
7965 initialize_local_var (tree decl, tree init)
7966 {
7967   tree type = TREE_TYPE (decl);
7968
7969   my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7970                       || TREE_CODE (decl) == RESULT_DECL, 
7971                       20021010);
7972   my_friendly_assert (!TREE_STATIC (decl), 20021010);
7973
7974   if (DECL_SIZE (decl) == NULL_TREE)
7975     {
7976       /* If we used it already as memory, it must stay in memory.  */
7977       DECL_INITIAL (decl) = NULL_TREE;
7978       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7979     }
7980
7981   if (DECL_SIZE (decl) && type != error_mark_node)
7982     {
7983       int already_used;
7984
7985       /* Compute and store the initial value.  */
7986       already_used = TREE_USED (decl) || TREE_USED (type);
7987
7988       /* Perform the initialization.  */
7989       if (init)
7990         {
7991           int saved_stmts_are_full_exprs_p;
7992
7993           my_friendly_assert (building_stmt_tree (), 20000906);
7994           saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7995           current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7996           finish_expr_stmt (init);
7997           current_stmt_tree ()->stmts_are_full_exprs_p =
7998             saved_stmts_are_full_exprs_p;
7999         }
8000
8001       /* Set this to 0 so we can tell whether an aggregate which was
8002          initialized was ever used.  Don't do this if it has a
8003          destructor, so we don't complain about the 'resource
8004          allocation is initialization' idiom.  Now set
8005          attribute((unused)) on types so decls of that type will be
8006          marked used. (see TREE_USED, above.)  */
8007       if (TYPE_NEEDS_CONSTRUCTING (type)
8008           && ! already_used
8009           && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8010           && DECL_NAME (decl))
8011         TREE_USED (decl) = 0;
8012       else if (already_used)
8013         TREE_USED (decl) = 1;
8014     }
8015
8016   /* Generate a cleanup, if necessary.  */
8017   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8018     {
8019       tree cleanup;
8020
8021       /* Compute the cleanup.  */
8022       cleanup = cxx_maybe_build_cleanup (decl);
8023       
8024       /* Record the cleanup required for this declaration.  */
8025       if (DECL_SIZE (decl) && cleanup)
8026         finish_decl_cleanup (decl, cleanup);
8027     }
8028 }
8029
8030 /* Finish processing of a declaration;
8031    install its line number and initial value.
8032    If the length of an array type is not known before,
8033    it must be determined now, from the initial value, or it is an error.
8034
8035    INIT holds the value of an initializer that should be allowed to escape
8036    the normal rules.
8037
8038    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8039    if the (init) syntax was used.  */
8040
8041 void
8042 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8043 {
8044   register tree type;
8045   tree ttype = NULL_TREE;
8046   const char *asmspec = NULL;
8047   int was_readonly = 0;
8048
8049   if (! decl)
8050     {
8051       if (init)
8052         error ("assignment (not initialization) in declaration");
8053       return;
8054     }
8055
8056   /* If a name was specified, get the string.  */
8057   if (current_binding_level == global_binding_level)
8058     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8059   if (asmspec_tree)
8060     asmspec = TREE_STRING_POINTER (asmspec_tree);
8061
8062   if (init && TREE_CODE (init) == NAMESPACE_DECL)
8063     {
8064       error ("cannot initialize `%D' to namespace `%D'",
8065                 decl, init);
8066       init = NULL_TREE;
8067     }
8068
8069   if (current_class_type
8070       && CP_DECL_CONTEXT (decl) == current_class_type
8071       && TYPE_BEING_DEFINED (current_class_type)
8072       && (DECL_INITIAL (decl) || init))
8073     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8074
8075   if (TREE_CODE (decl) == VAR_DECL
8076       && DECL_CONTEXT (decl)
8077       && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8078       && DECL_CONTEXT (decl) != current_namespace
8079       && init)
8080     {
8081       /* Leave the namespace of the object.  */
8082       pop_decl_namespace ();
8083     }
8084
8085   type = TREE_TYPE (decl);
8086
8087   if (type == error_mark_node)
8088     return;
8089
8090   if (TYPE_HAS_MUTABLE_P (type))
8091     TREE_READONLY (decl) = 0;
8092
8093   if (processing_template_decl)
8094     {
8095       /* Add this declaration to the statement-tree.  */
8096       if (at_function_scope_p ()
8097           && TREE_CODE (decl) != RESULT_DECL)
8098         add_decl_stmt (decl);
8099
8100       if (init && DECL_INITIAL (decl))
8101         DECL_INITIAL (decl) = init;
8102       goto finish_end0;
8103     }
8104
8105   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8106   my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8107
8108   /* Take care of TYPE_DECLs up front.  */
8109   if (TREE_CODE (decl) == TYPE_DECL)
8110     {
8111       if (type != error_mark_node
8112           && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8113         {
8114           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8115             warning ("shadowing previous type declaration of `%#D'", decl);
8116           set_identifier_type_value (DECL_NAME (decl), type);
8117           CLASSTYPE_GOT_SEMICOLON (type) = 1;
8118         }
8119
8120       /* If we have installed this as the canonical typedef for this
8121          type, and that type has not been defined yet, delay emitting
8122          the debug information for it, as we will emit it later.  */
8123       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8124           && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8125         TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8126
8127       rest_of_decl_compilation (decl, NULL,
8128                                 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8129       goto finish_end;
8130     }
8131
8132   if (TREE_CODE (decl) != FUNCTION_DECL)
8133     ttype = target_type (type);
8134
8135   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8136       && TYPE_NEEDS_CONSTRUCTING (type))
8137     {
8138       /* Currently, GNU C++ puts constants in text space, making them
8139          impossible to initialize.  In the future, one would hope for
8140          an operating system which understood the difference between
8141          initialization and the running of a program.  */
8142       was_readonly = 1;
8143       TREE_READONLY (decl) = 0;
8144     }
8145
8146   if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8147     {
8148       /* This must override the asm specifier which was placed by
8149          grokclassfn.  Lay this out fresh.  */
8150       SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8151       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8152       make_decl_rtl (decl, asmspec);
8153     }
8154   else if (TREE_CODE (decl) == RESULT_DECL)
8155     init = check_initializer (decl, init, flags);
8156   else if (TREE_CODE (decl) == VAR_DECL)
8157     {
8158       /* Only PODs can have thread-local storage.  Other types may require
8159          various kinds of non-trivial initialization.  */
8160       if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8161         error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8162                decl, TREE_TYPE (decl));
8163       /* Convert the initializer to the type of DECL, if we have not
8164          already initialized DECL.  */
8165       if (!DECL_INITIALIZED_P (decl)
8166           /* If !DECL_EXTERNAL then DECL is being defined.  In the
8167              case of a static data member initialized inside the
8168              class-specifier, there can be an initializer even if DECL
8169              is *not* defined.  */
8170           && (!DECL_EXTERNAL (decl) || init))
8171         {
8172           init = check_initializer (decl, init, flags);
8173           /* Thread-local storage cannot be dynamically initialized.  */
8174           if (DECL_THREAD_LOCAL (decl) && init)
8175             {
8176               error ("`%D' is thread-local and so cannot be dynamically "
8177                      "initialized", decl);
8178               init = NULL_TREE;
8179             }
8180           /* Handle:
8181              
8182              [dcl.init]
8183              
8184              The memory occupied by any object of static storage
8185              duration is zero-initialized at program startup before
8186              any other initialization takes place.
8187              
8188              We cannot create an appropriate initializer until after
8189              the type of DECL is finalized.  If DECL_INITIAL is set,
8190              then the DECL is statically initialized, and any
8191              necessary zero-initialization has already been performed.  */
8192           if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8193             DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8194                                                    /*static_storage_p=*/true);
8195           /* Remember that the initialization for this variable has
8196              taken place.  */
8197           DECL_INITIALIZED_P (decl) = 1;
8198         }
8199       /* If the variable has an array type, lay out the type, even if
8200          there is no initializer.  It is valid to index through the
8201          array, and we must get TYPE_ALIGN set correctly on the array
8202          type.  */
8203       else if (TREE_CODE (type) == ARRAY_TYPE)
8204         layout_type (type);
8205     }
8206
8207   /* Add this declaration to the statement-tree.  This needs to happen
8208      after the call to check_initializer so that the DECL_STMT for a
8209      reference temp is added before the DECL_STMT for the reference itself.  */
8210   if (building_stmt_tree ()
8211       && at_function_scope_p ()
8212       && TREE_CODE (decl) != RESULT_DECL)
8213     add_decl_stmt (decl);
8214
8215   if (TREE_CODE (decl) == VAR_DECL)
8216     layout_var_decl (decl);
8217
8218   /* Output the assembler code and/or RTL code for variables and functions,
8219      unless the type is an undefined structure or union.
8220      If not, it will get done when the type is completed.  */
8221   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8222       || TREE_CODE (decl) == RESULT_DECL)
8223     {
8224       if (TREE_CODE (decl) == VAR_DECL)
8225         maybe_commonize_var (decl);
8226
8227       make_rtl_for_nonlocal_decl (decl, init, asmspec);
8228
8229       if (TREE_CODE (type) == FUNCTION_TYPE
8230           || TREE_CODE (type) == METHOD_TYPE)
8231         abstract_virtuals_error (decl,
8232                                  strip_array_types (TREE_TYPE (type)));
8233       else
8234         abstract_virtuals_error (decl, strip_array_types (type));
8235
8236       if (TREE_CODE (decl) == FUNCTION_DECL 
8237           || TREE_TYPE (decl) == error_mark_node)
8238         /* No initialization required.  */
8239         ;
8240       else if (DECL_EXTERNAL (decl)
8241                && ! (DECL_LANG_SPECIFIC (decl)
8242                      && DECL_NOT_REALLY_EXTERN (decl)))
8243         {
8244           if (init)
8245             DECL_INITIAL (decl) = init;
8246         }
8247       else
8248         {
8249           /* A variable definition.  */
8250           if (DECL_FUNCTION_SCOPE_P (decl))
8251             {
8252               /* This is a local declaration.  */
8253               if (doing_semantic_analysis_p ())
8254                 maybe_inject_for_scope_var (decl);
8255               /* Initialize the local variable.  */
8256               if (processing_template_decl)
8257                 {
8258                   if (init || DECL_INITIAL (decl) == error_mark_node)
8259                     DECL_INITIAL (decl) = init;
8260                 }
8261               else if (!TREE_STATIC (decl))
8262                 initialize_local_var (decl, init);
8263             }
8264
8265           if (TREE_STATIC (decl))
8266             expand_static_init (decl, init);
8267         }
8268     finish_end0:
8269
8270       /* Undo call to `pushclass' that was done in `start_decl'
8271          due to initialization of qualified member variable.
8272          I.e., Foo::x = 10;  */
8273       {
8274         tree context = CP_DECL_CONTEXT (decl);
8275         if (context
8276             && TYPE_P (context)
8277             && (TREE_CODE (decl) == VAR_DECL
8278                 /* We also have a pushclass done that we need to undo here
8279                    if we're at top level and declare a method.  */
8280                 || TREE_CODE (decl) == FUNCTION_DECL)
8281             /* If size hasn't been set, we're still defining it,
8282                and therefore inside the class body; don't pop
8283                the binding level..  */
8284             && COMPLETE_TYPE_P (context)
8285             && context == current_class_type)
8286           pop_nested_class ();
8287       }
8288     }
8289
8290  finish_end:
8291
8292   if (was_readonly)
8293     TREE_READONLY (decl) = 1;
8294 }
8295
8296 /* This is here for a midend callback from c-common.c */
8297
8298 void
8299 finish_decl (tree decl, tree init, tree asmspec_tree)
8300 {
8301   cp_finish_decl (decl, init, asmspec_tree, 0);
8302 }
8303
8304 /* Returns a declaration for a VAR_DECL as if:
8305
8306      extern "C" TYPE NAME;
8307
8308    had been seen.  Used to create compiler-generated global
8309    variables.  */
8310
8311 tree
8312 declare_global_var (tree name, tree type)
8313 {
8314   tree decl;
8315
8316   push_to_top_level ();
8317   decl = build_decl (VAR_DECL, name, type);
8318   TREE_PUBLIC (decl) = 1;
8319   DECL_EXTERNAL (decl) = 1;
8320   DECL_ARTIFICIAL (decl) = 1;
8321   pushdecl (decl);
8322   cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8323   pop_from_top_level ();
8324
8325   return decl;
8326 }
8327
8328 /* Returns a pointer to the `atexit' function.  Note that if
8329    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8330    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
8331
8332 static tree
8333 get_atexit_node (void)
8334 {
8335   tree atexit_fndecl;
8336   tree arg_types;
8337   tree fn_type;
8338   tree fn_ptr_type;
8339   const char *name;
8340
8341   if (atexit_node)
8342     return atexit_node;
8343
8344   if (flag_use_cxa_atexit)
8345     {
8346       /* The declaration for `__cxa_atexit' is:
8347
8348            int __cxa_atexit (void (*)(void *), void *, void *)
8349
8350          We build up the argument types and then then function type
8351          itself.  */
8352
8353       /* First, build the pointer-to-function type for the first
8354          argument.  */
8355       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8356       fn_type = build_function_type (void_type_node, arg_types);
8357       fn_ptr_type = build_pointer_type (fn_type);
8358       /* Then, build the rest of the argument types.  */
8359       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8360       arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8361       arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8362       /* And the final __cxa_atexit type.  */
8363       fn_type = build_function_type (integer_type_node, arg_types);
8364       fn_ptr_type = build_pointer_type (fn_type);
8365       name = "__cxa_atexit";
8366     }
8367   else
8368     {
8369       /* The declaration for `atexit' is:
8370
8371            int atexit (void (*)());
8372
8373          We build up the argument types and then then function type
8374          itself.  */
8375       fn_type = build_function_type (void_type_node, void_list_node);
8376       fn_ptr_type = build_pointer_type (fn_type);
8377       arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8378       /* Build the final atexit type.  */
8379       fn_type = build_function_type (integer_type_node, arg_types);
8380       name = "atexit";
8381     }
8382
8383   /* Now, build the function declaration.  */
8384   push_lang_context (lang_name_c);
8385   atexit_fndecl = build_library_fn_ptr (name, fn_type);
8386   mark_used (atexit_fndecl);
8387   pop_lang_context ();
8388   atexit_node = default_conversion (atexit_fndecl);
8389
8390   return atexit_node;
8391 }
8392
8393 /* Returns the __dso_handle VAR_DECL.  */
8394
8395 static tree
8396 get_dso_handle_node (void)
8397 {
8398   if (dso_handle_node)
8399     return dso_handle_node;
8400
8401   /* Declare the variable.  */
8402   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8403                                         ptr_type_node);
8404
8405   return dso_handle_node;
8406 }
8407
8408 /* Begin a new function with internal linkage whose job will be simply
8409    to destroy some particular variable.  */
8410
8411 static tree
8412 start_cleanup_fn (void)
8413 {
8414   static int counter = 0;
8415   int old_interface_only = interface_only;
8416   int old_interface_unknown = interface_unknown;
8417   char name[32];
8418   tree parmtypes;
8419   tree fntype;
8420   tree fndecl;
8421
8422   push_to_top_level ();
8423
8424   /* No need to mangle this.  */
8425   push_lang_context (lang_name_c);
8426
8427   interface_only = 0;
8428   interface_unknown = 1;
8429
8430   /* Build the parameter-types.  */
8431   parmtypes = void_list_node;
8432   /* Functions passed to __cxa_atexit take an additional parameter.
8433      We'll just ignore it.  After we implement the new calling
8434      convention for destructors, we can eliminate the use of
8435      additional cleanup functions entirely in the -fnew-abi case.  */
8436   if (flag_use_cxa_atexit)
8437     parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8438   /* Build the function type itself.  */
8439   fntype = build_function_type (void_type_node, parmtypes);
8440   /* Build the name of the function.  */
8441   sprintf (name, "__tcf_%d", counter++);
8442   /* Build the function declaration.  */
8443   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8444   /* It's a function with internal linkage, generated by the
8445      compiler.  */
8446   TREE_PUBLIC (fndecl) = 0;
8447   DECL_ARTIFICIAL (fndecl) = 1;
8448   /* Make the function `inline' so that it is only emitted if it is
8449      actually needed.  It is unlikely that it will be inlined, since
8450      it is only called via a function pointer, but we avoid unnecessary
8451      emissions this way.  */
8452   DECL_INLINE (fndecl) = 1;
8453   /* Build the parameter.  */
8454   if (flag_use_cxa_atexit)
8455     {
8456       tree parmdecl;
8457
8458       parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8459       DECL_CONTEXT (parmdecl) = fndecl;
8460       TREE_USED (parmdecl) = 1;
8461       DECL_ARGUMENTS (fndecl) = parmdecl;
8462     }
8463
8464   pushdecl (fndecl);
8465   start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8466
8467   interface_unknown = old_interface_unknown;
8468   interface_only = old_interface_only;
8469
8470   pop_lang_context ();
8471
8472   return current_function_decl;
8473 }
8474
8475 /* Finish the cleanup function begun by start_cleanup_fn.  */
8476
8477 static void
8478 end_cleanup_fn (void)
8479 {
8480   expand_body (finish_function (0));
8481
8482   pop_from_top_level ();
8483 }
8484
8485 /* Generate code to handle the destruction of DECL, an object with
8486    static storage duration.  */
8487
8488 void
8489 register_dtor_fn (tree decl)
8490 {
8491   tree cleanup;
8492   tree compound_stmt;
8493   tree args;
8494   tree fcall;
8495   int saved_flag_access_control;
8496
8497   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8498     return;
8499
8500   /* Call build_cleanup before we enter the anonymous function so that
8501      any access checks will be done relative to the current scope,
8502      rather than the scope of the anonymous function.  */
8503   build_cleanup (decl);
8504
8505   /* Now start the function.  */
8506   cleanup = start_cleanup_fn ();
8507
8508   /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8509      to the original function, rather than the anonymous one.  That
8510      will make the back-end think that nested functions are in use,
8511      which causes confusion.  */
8512   saved_flag_access_control = flag_access_control;
8513   scope_chain->check_access = flag_access_control = 0;
8514   fcall = build_cleanup (decl);
8515   scope_chain->check_access = flag_access_control = saved_flag_access_control;
8516
8517   /* Create the body of the anonymous function.  */
8518   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8519   finish_expr_stmt (fcall);
8520   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8521   end_cleanup_fn ();
8522
8523   /* Call atexit with the cleanup function.  */
8524   cxx_mark_addressable (cleanup);
8525   cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8526   if (flag_use_cxa_atexit)
8527     {
8528       args = tree_cons (NULL_TREE, 
8529                         build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8530                         NULL_TREE);
8531       args = tree_cons (NULL_TREE, null_pointer_node, args);
8532       args = tree_cons (NULL_TREE, cleanup, args);
8533     }
8534   else
8535     args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8536   finish_expr_stmt (build_function_call (get_atexit_node (), args));
8537 }
8538
8539 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8540    is its initializer.  Generate code to handle the construction
8541    and destruction of DECL.  */
8542
8543 static void
8544 expand_static_init (tree decl, tree init)
8545 {
8546   tree oldstatic;
8547
8548   my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8549   my_friendly_assert (TREE_STATIC (decl), 20021010);
8550
8551   /* Some variables require no initialization.  */
8552   if (!init 
8553       && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8554       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8555     return;
8556
8557   oldstatic = value_member (decl, static_aggregates);
8558
8559   if (oldstatic)
8560     {
8561       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8562         error ("multiple initializations given for `%D'", decl);
8563     }
8564   else if (! toplevel_bindings_p ())
8565     {
8566       /* Emit code to perform this initialization but once.  */
8567       tree if_stmt;
8568       tree then_clause;
8569       tree assignment;
8570       tree guard;
8571       tree guard_init;
8572
8573       /* Emit code to perform this initialization but once.  This code
8574          looks like:
8575
8576            static int guard = 0;
8577            if (!guard) {
8578              // Do initialization.
8579              guard = 1;
8580              // Register variable for destruction at end of program.
8581            }
8582
8583          Note that the `temp' variable is only set to 1 *after* the
8584          initialization is complete.  This ensures that an exception,
8585          thrown during the construction, will cause the variable to
8586          reinitialized when we pass through this code again, as per:
8587
8588            [stmt.dcl]
8589
8590            If the initialization exits by throwing an exception, the
8591            initialization is not complete, so it will be tried again
8592            the next time control enters the declaration.
8593
8594          In theory, this process should be thread-safe, too; multiple
8595          threads should not be able to initialize the variable more
8596          than once.  We don't yet attempt to ensure thread-safety.  */
8597
8598       /* Create the guard variable.  */
8599       guard = get_guard (decl);
8600
8601       /* Begin the conditional initialization.  */
8602       if_stmt = begin_if_stmt ();
8603       finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8604       then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8605
8606       /* Do the initialization itself.  */
8607       assignment = init ? init : NULL_TREE;
8608
8609       /* Once the assignment is complete, set TEMP to 1.  Since the
8610          construction of the static object is complete at this point,
8611          we want to make sure TEMP is set to 1 even if a temporary
8612          constructed during the initialization throws an exception
8613          when it is destroyed.  So, we combine the initialization and
8614          the assignment to TEMP into a single expression, ensuring
8615          that when we call finish_expr_stmt the cleanups will not be
8616          run until after TEMP is set to 1.  */
8617       guard_init = set_guard (guard);
8618       if (assignment)
8619         {
8620           assignment = tree_cons (NULL_TREE, assignment,
8621                                   build_tree_list (NULL_TREE,
8622                                                    guard_init));
8623           assignment = build_compound_expr (assignment);
8624         }
8625       else
8626         assignment = guard_init;
8627       finish_expr_stmt (assignment);
8628
8629       /* Use atexit to register a function for destroying this static
8630          variable.  */
8631       register_dtor_fn (decl);
8632
8633       finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8634       finish_then_clause (if_stmt);
8635       finish_if_stmt ();
8636     }
8637   else
8638     static_aggregates = tree_cons (init, decl, static_aggregates);
8639 }
8640
8641 /* Finish the declaration of a catch-parameter.  */
8642
8643 tree
8644 start_handler_parms (tree declspecs, tree declarator)
8645 {
8646   tree decl;
8647   if (declspecs)
8648     {
8649       decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8650                              1, NULL);
8651       if (decl == NULL_TREE)
8652         error ("invalid catch parameter");
8653     }
8654   else
8655     decl = NULL_TREE;
8656
8657   return decl;
8658 }
8659
8660 \f
8661 /* Make TYPE a complete type based on INITIAL_VALUE.
8662    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8663    2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8664
8665 int
8666 complete_array_type (tree type, tree initial_value, int do_default)
8667 {
8668   register tree maxindex = NULL_TREE;
8669   int value = 0;
8670
8671   if (initial_value)
8672     {
8673       /* An array of character type can be initialized from a
8674          brace-enclosed string constant.  */
8675       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8676           && TREE_CODE (initial_value) == CONSTRUCTOR
8677           && CONSTRUCTOR_ELTS (initial_value)
8678           && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8679               == STRING_CST)
8680           && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8681         initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8682
8683       /* Note MAXINDEX is really the maximum index, one less than the
8684          size.  */
8685       if (TREE_CODE (initial_value) == STRING_CST)
8686         {
8687           int eltsize
8688             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8689           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8690                                    / eltsize) - 1, 0);
8691         }
8692       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8693         {
8694           tree elts = CONSTRUCTOR_ELTS (initial_value);
8695
8696           maxindex = ssize_int (-1);
8697           for (; elts; elts = TREE_CHAIN (elts))
8698             {
8699               if (TREE_PURPOSE (elts))
8700                 maxindex = TREE_PURPOSE (elts);
8701               else
8702                 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8703             }
8704           maxindex = copy_node (maxindex);
8705         }
8706       else
8707         {
8708           /* Make an error message unless that happened already.  */
8709           if (initial_value != error_mark_node)
8710             value = 1;
8711           else
8712             initial_value = NULL_TREE;
8713
8714           /* Prevent further error messages.  */
8715           maxindex = build_int_2 (0, 0);
8716         }
8717     }
8718
8719   if (!maxindex)
8720     {
8721       if (do_default)
8722         maxindex = build_int_2 (0, 0);
8723       value = 2;
8724     }
8725
8726   if (maxindex)
8727     {
8728       tree itype;
8729       tree domain;
8730
8731       domain = build_index_type (maxindex);
8732       TYPE_DOMAIN (type) = domain;
8733
8734       if (! TREE_TYPE (maxindex))
8735         TREE_TYPE (maxindex) = domain;
8736       if (initial_value)
8737         itype = TREE_TYPE (initial_value);
8738       else
8739         itype = NULL;
8740       if (itype && !TYPE_DOMAIN (itype))
8741         TYPE_DOMAIN (itype) = domain;
8742       /* The type of the main variant should never be used for arrays
8743          of different sizes.  It should only ever be completed with the
8744          size of the array.  */
8745       if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8746         TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8747     }
8748
8749   /* Lay out the type now that we can get the real answer.  */
8750
8751   layout_type (type);
8752
8753   return value;
8754 }
8755 \f
8756 /* Return zero if something is declared to be a member of type
8757    CTYPE when in the context of CUR_TYPE.  STRING is the error
8758    message to print in that case.  Otherwise, quietly return 1.  */
8759
8760 static int
8761 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8762 {
8763   if (ctype && ctype != cur_type)
8764     {
8765       if (flags == DTOR_FLAG)
8766         error ("destructor for alien class `%T' cannot be a member",
8767                   ctype);
8768       else
8769         error ("constructor for alien class `%T' cannot be a member",
8770                   ctype);
8771       return 0;
8772     }
8773   return 1;
8774 }
8775 \f
8776 /* Subroutine of `grokdeclarator'.  */
8777
8778 /* Generate errors possibly applicable for a given set of specifiers.
8779    This is for ARM $7.1.2.  */
8780
8781 static void
8782 bad_specifiers (tree object,
8783                 const char* type,
8784                 int virtualp,
8785                 int quals,
8786                 int inlinep,
8787                 int friendp,
8788                 int raises)
8789 {
8790   if (virtualp)
8791     error ("`%D' declared as a `virtual' %s", object, type);
8792   if (inlinep)
8793     error ("`%D' declared as an `inline' %s", object, type);
8794   if (quals)
8795     error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8796               object, type);
8797   if (friendp)
8798     cp_error_at ("`%D' declared as a friend", object);
8799   if (raises
8800       && (TREE_CODE (object) == TYPE_DECL
8801           || (!TYPE_PTRFN_P (TREE_TYPE (object))
8802               && !TYPE_REFFN_P (TREE_TYPE (object))
8803               && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8804     cp_error_at ("`%D' declared with an exception specification", object);
8805 }
8806
8807 /* CTYPE is class type, or null if non-class.
8808    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8809    or METHOD_TYPE.
8810    DECLARATOR is the function's name.
8811    VIRTUALP is truthvalue of whether the function is virtual or not.
8812    FLAGS are to be passed through to `grokclassfn'.
8813    QUALS are qualifiers indicating whether the function is `const'
8814    or `volatile'.
8815    RAISES is a list of exceptions that this function can raise.
8816    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8817    not look, and -1 if we should not call `grokclassfn' at all.
8818
8819    Returns `NULL_TREE' if something goes wrong, after issuing
8820    applicable error messages.  */
8821
8822 static tree
8823 grokfndecl (tree ctype, 
8824             tree type,
8825             tree declarator,
8826             tree orig_declarator,
8827             int virtualp,
8828             enum overload_flags flags,
8829             tree quals, 
8830             tree raises,
8831             int check, 
8832             int friendp, 
8833             int publicp, 
8834             int inlinep, 
8835             int funcdef_flag, 
8836             int template_count,
8837             tree in_namespace)
8838 {
8839   tree decl;
8840   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8841   int has_default_arg = 0;
8842   tree t;
8843
8844   if (raises)
8845     type = build_exception_variant (type, raises);
8846
8847   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8848   /* Propagate volatile out from type to decl.  */
8849   if (TYPE_VOLATILE (type))
8850     TREE_THIS_VOLATILE (decl) = 1;
8851
8852   /* If this decl has namespace scope, set that up.  */
8853   if (in_namespace)
8854     set_decl_namespace (decl, in_namespace, friendp);
8855   else if (!ctype)
8856     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8857
8858   /* `main' and builtins have implicit 'C' linkage.  */
8859   if ((MAIN_NAME_P (declarator)
8860        || (IDENTIFIER_LENGTH (declarator) > 10
8861            && IDENTIFIER_POINTER (declarator)[0] == '_'
8862            && IDENTIFIER_POINTER (declarator)[1] == '_'
8863            && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8864       && current_lang_name == lang_name_cplusplus
8865       && ctype == NULL_TREE
8866       /* NULL_TREE means global namespace.  */
8867       && DECL_CONTEXT (decl) == NULL_TREE)
8868     SET_DECL_LANGUAGE (decl, lang_c);
8869
8870   /* Should probably propagate const out from type to decl I bet (mrs).  */
8871   if (staticp)
8872     {
8873       DECL_STATIC_FUNCTION_P (decl) = 1;
8874       DECL_CONTEXT (decl) = ctype;
8875     }
8876
8877   if (ctype)
8878     DECL_CONTEXT (decl) = ctype;
8879
8880   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8881     {
8882       if (processing_template_decl)
8883         error ("cannot declare `::main' to be a template");
8884       if (inlinep)
8885         error ("cannot declare `::main' to be inline");
8886       if (!publicp)
8887         error ("cannot declare `::main' to be static");
8888       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8889                         integer_type_node))
8890         error ("`main' must return `int'");
8891       inlinep = 0;
8892       publicp = 1;
8893     }
8894
8895   /* Members of anonymous types and local classes have no linkage; make
8896      them internal.  */
8897   /* FIXME what if it gets a name from typedef?  */
8898   if (ctype && (TYPE_ANONYMOUS_P (ctype)
8899                 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8900     publicp = 0;
8901
8902   if (publicp)
8903     {
8904       /* [basic.link]: A name with no linkage (notably, the name of a class
8905          or enumeration declared in a local scope) shall not be used to
8906          declare an entity with linkage.
8907
8908          Only check this for public decls for now.  */
8909       t = no_linkage_check (TREE_TYPE (decl));
8910       if (t)
8911         {
8912           if (TYPE_ANONYMOUS_P (t))
8913             {
8914               if (DECL_EXTERN_C_P (decl))
8915                 /* Allow this; it's pretty common in C.  */;
8916               else
8917                 {
8918                   pedwarn ("non-local function `%#D' uses anonymous type",
8919                               decl);
8920                   if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8921                     cp_pedwarn_at ("\
8922 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8923                                 TYPE_NAME (t));
8924                 }
8925             }
8926           else
8927             pedwarn ("non-local function `%#D' uses local type `%T'",
8928                         decl, t);
8929         }
8930     }
8931
8932   TREE_PUBLIC (decl) = publicp;
8933   if (! publicp)
8934     {
8935       DECL_INTERFACE_KNOWN (decl) = 1;
8936       DECL_NOT_REALLY_EXTERN (decl) = 1;
8937     }
8938
8939   /* If the declaration was declared inline, mark it as such.  */
8940   if (inlinep)
8941     DECL_DECLARED_INLINE_P (decl) = 1;
8942   /* We inline functions that are explicitly declared inline, or, when
8943      the user explicitly asks us to, all functions.  */
8944   if (DECL_DECLARED_INLINE_P (decl) || flag_inline_trees == 2)
8945     DECL_INLINE (decl) = 1;
8946
8947   DECL_EXTERNAL (decl) = 1;
8948   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8949     {
8950       error ("%smember function `%D' cannot have `%T' method qualifier",
8951                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8952       quals = NULL_TREE;
8953     }
8954
8955   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8956     grok_op_properties (decl, friendp);
8957
8958   if (ctype && decl_function_context (decl))
8959     DECL_NO_STATIC_CHAIN (decl) = 1;
8960
8961   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8962     if (TREE_PURPOSE (t)
8963         && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8964       {
8965         has_default_arg = 1;
8966         break;
8967       }
8968
8969   if (friendp
8970       && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8971     {
8972       if (funcdef_flag)
8973         error
8974           ("defining explicit specialization `%D' in friend declaration",
8975            orig_declarator);
8976       else
8977         {
8978           tree fns = TREE_OPERAND (orig_declarator, 0);
8979           tree args = TREE_OPERAND (orig_declarator, 1);
8980
8981           if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8982             {
8983               /* Something like `template <class T> friend void f<T>()'.  */
8984               error ("invalid use of template-id `%D' in declaration of primary template",
8985                         orig_declarator);
8986               return NULL_TREE;
8987             }
8988
8989
8990           /* A friend declaration of the form friend void f<>().  Record
8991              the information in the TEMPLATE_ID_EXPR.  */
8992           SET_DECL_IMPLICIT_INSTANTIATION (decl);
8993
8994           if (TREE_CODE (fns) == COMPONENT_REF)
8995             {
8996               /* Due to bison parser ickiness, we will have already looked
8997                  up an operator_name or PFUNCNAME within the current class
8998                  (see template_id in parse.y). If the current class contains
8999                  such a name, we'll get a COMPONENT_REF here. Undo that.  */
9000
9001               my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
9002                                   == current_class_type, 20001120);
9003               fns = TREE_OPERAND (fns, 1);
9004             }
9005           my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
9006                               || TREE_CODE (fns) == LOOKUP_EXPR
9007                               || TREE_CODE (fns) == OVERLOAD, 20001120);
9008           DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
9009
9010           if (has_default_arg)
9011             {
9012               error ("default arguments are not allowed in declaration of friend template specialization `%D'",
9013                         decl);
9014               return NULL_TREE;
9015             }
9016
9017           if (inlinep)
9018             {
9019               error ("`inline' is not allowed in declaration of friend template specialization `%D'",
9020                         decl);
9021               return NULL_TREE;
9022             }
9023         }
9024     }
9025
9026   if (funcdef_flag)
9027     /* Make the init_value nonzero so pushdecl knows this is not
9028        tentative.  error_mark_node is replaced later with the BLOCK.  */
9029     DECL_INITIAL (decl) = error_mark_node;
9030
9031   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9032     TREE_NOTHROW (decl) = 1;
9033
9034   /* Caller will do the rest of this.  */
9035   if (check < 0)
9036     return decl;
9037
9038   if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9039     DECL_CONSTRUCTOR_P (decl) = 1;
9040
9041   /* Function gets the ugly name, field gets the nice one.  This call
9042      may change the type of the function (because of default
9043      parameters)!  */
9044   if (ctype != NULL_TREE)
9045     grokclassfn (ctype, decl, flags, quals);
9046
9047   decl = check_explicit_specialization (orig_declarator, decl,
9048                                         template_count,
9049                                         2 * (funcdef_flag != 0) +
9050                                         4 * (friendp != 0));
9051   if (decl == error_mark_node)
9052     return NULL_TREE;
9053
9054   if (ctype != NULL_TREE
9055       && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9056       && check)
9057     {
9058       tree old_decl;
9059
9060       old_decl = check_classfn (ctype, decl);
9061
9062       if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9063         /* Because grokfndecl is always supposed to return a
9064            FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9065            here.  We depend on our callers to figure out that its
9066            really a template that's being returned.  */
9067         old_decl = DECL_TEMPLATE_RESULT (old_decl);
9068
9069       if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9070           && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9071         {
9072           /* Remove the `this' parm added by grokclassfn.
9073              XXX Isn't this done in start_function, too?  */
9074           revert_static_member_fn (decl);
9075           last_function_parms = TREE_CHAIN (last_function_parms);
9076         }
9077       if (old_decl && DECL_ARTIFICIAL (old_decl))
9078         error ("definition of implicitly-declared `%D'", old_decl);
9079
9080       if (old_decl)
9081         {
9082           /* Since we've smashed OLD_DECL to its
9083              DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9084           if (TREE_CODE (decl) == TEMPLATE_DECL)
9085             decl = DECL_TEMPLATE_RESULT (decl);
9086
9087           /* Attempt to merge the declarations.  This can fail, in
9088              the case of some invalid specialization declarations.  */
9089           if (!duplicate_decls (decl, old_decl))
9090             error ("no `%#D' member function declared in class `%T'",
9091                       decl, ctype);
9092           return old_decl;
9093         }
9094     }
9095
9096   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9097     return NULL_TREE;
9098
9099   if (ctype == NULL_TREE || check)
9100     return decl;
9101
9102   if (virtualp)
9103     DECL_VIRTUAL_P (decl) = 1;
9104
9105   return decl;
9106 }
9107
9108 /* Create a VAR_DECL named NAME with the indicated TYPE.  
9109
9110    If SCOPE is non-NULL, it is the class type or namespace containing
9111    the variable.  If SCOPE is NULL, the variable should is created in
9112    the innermost enclosings scope.  */
9113
9114 static tree
9115 grokvardecl (tree type,
9116              tree name,
9117              RID_BIT_TYPE * specbits_in,
9118              int initialized,
9119              int constp,
9120              tree scope)
9121 {
9122   tree decl;
9123   RID_BIT_TYPE specbits;
9124
9125   my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, 
9126                       20020808);
9127
9128   specbits = *specbits_in;
9129
9130   /* Compute the scope in which to place the variable.  */
9131   if (!scope)
9132     {
9133       /* An explicit "extern" specifier indicates a namespace-scope
9134          variable.  */
9135       if (RIDBIT_SETP (RID_EXTERN, specbits))
9136         scope = current_namespace;
9137       else if (!at_function_scope_p ())
9138         {
9139           scope = current_scope ();
9140           if (!scope)
9141             scope = current_namespace;
9142         }
9143     }
9144
9145   if (scope
9146       && (/* If the variable is a namespace-scope variable declared in a
9147              template, we need DECL_LANG_SPECIFIC.  */
9148           (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9149           /* Similarly for namespace-scope variables with language linkage
9150              other than C++.  */
9151           || (TREE_CODE (scope) == NAMESPACE_DECL 
9152               && current_lang_name != lang_name_cplusplus)
9153           /* Similarly for static data members.  */
9154           || TYPE_P (scope)))
9155     decl = build_lang_decl (VAR_DECL, name, type);
9156   else
9157     decl = build_decl (VAR_DECL, name, type);
9158
9159   if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9160     set_decl_namespace (decl, scope, 0);
9161   else
9162     DECL_CONTEXT (decl) = scope;
9163
9164   if (name && scope && current_lang_name != lang_name_c)
9165     /* We can't mangle lazily here because we don't have any
9166        way to recover whether or not a variable was `extern
9167        "C"' later.  */
9168     mangle_decl (decl);
9169
9170   if (RIDBIT_SETP (RID_EXTERN, specbits))
9171     {
9172       DECL_THIS_EXTERN (decl) = 1;
9173       DECL_EXTERNAL (decl) = !initialized;
9174     }
9175
9176   /* In class context, static means one per class,
9177      public access, and static storage.  */
9178   if (DECL_CLASS_SCOPE_P (decl))
9179     {
9180       TREE_PUBLIC (decl) = 1;
9181       TREE_STATIC (decl) = 1;
9182       DECL_EXTERNAL (decl) = 0;
9183     }
9184   /* At top level, either `static' or no s.c. makes a definition
9185      (perhaps tentative), and absence of `static' makes it public.  */
9186   else if (toplevel_bindings_p ())
9187     {
9188       TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9189                             && (DECL_THIS_EXTERN (decl) || ! constp));
9190       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9191     }
9192   /* Not at top level, only `static' makes a static definition.  */
9193   else
9194     {
9195       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9196       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9197     }
9198
9199   if (RIDBIT_SETP (RID_THREAD, specbits))
9200     {
9201       if (targetm.have_tls)
9202         DECL_THREAD_LOCAL (decl) = 1;
9203       else
9204         /* A mere warning is sure to result in improper semantics
9205            at runtime.  Don't bother to allow this to compile.  */
9206         error ("thread-local storage not supported for this target");
9207     }
9208
9209   if (TREE_PUBLIC (decl))
9210     {
9211       /* [basic.link]: A name with no linkage (notably, the name of a class
9212          or enumeration declared in a local scope) shall not be used to
9213          declare an entity with linkage.
9214
9215          Only check this for public decls for now.  */
9216       tree t = no_linkage_check (TREE_TYPE (decl));
9217       if (t)
9218         {
9219           if (TYPE_ANONYMOUS_P (t))
9220             /* Ignore for now; `enum { foo } e' is pretty common.  */;
9221           else
9222             pedwarn ("non-local variable `%#D' uses local type `%T'",
9223                         decl, t);
9224         }
9225     }
9226
9227   return decl;
9228 }
9229
9230 /* Create and return a canonical pointer to member function type, for
9231    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9232
9233 tree
9234 build_ptrmemfunc_type (tree type)
9235 {
9236   tree field, fields;
9237   tree t;
9238   tree unqualified_variant = NULL_TREE;
9239
9240   if (type == error_mark_node)
9241     return type;
9242
9243   /* If a canonical type already exists for this type, use it.  We use
9244      this method instead of type_hash_canon, because it only does a
9245      simple equality check on the list of field members.  */
9246
9247   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9248     return t;
9249
9250   /* Make sure that we always have the unqualified pointer-to-member
9251      type first.  */
9252   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9253     unqualified_variant
9254       = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9255
9256   t = make_aggr_type (RECORD_TYPE);
9257   /* Let the front-end know this is a pointer to member function...  */
9258   TYPE_PTRMEMFUNC_FLAG (t) = 1;
9259   /* ... and not really an aggregate.  */
9260   SET_IS_AGGR_TYPE (t, 0);
9261
9262   field = build_decl (FIELD_DECL, pfn_identifier, type);
9263   fields = field;
9264   
9265   field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9266   TREE_CHAIN (field) = fields;
9267   fields = field;
9268   
9269   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9270
9271   /* Zap out the name so that the back-end will give us the debugging
9272      information for this anonymous RECORD_TYPE.  */
9273   TYPE_NAME (t) = NULL_TREE;
9274
9275   /* If this is not the unqualified form of this pointer-to-member
9276      type, set the TYPE_MAIN_VARIANT for this type to be the
9277      unqualified type.  Since they are actually RECORD_TYPEs that are
9278      not variants of each other, we must do this manually.  */
9279   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9280     {
9281       t = build_qualified_type (t, cp_type_quals (type));
9282       TYPE_MAIN_VARIANT (t) = unqualified_variant;
9283       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9284       TYPE_NEXT_VARIANT (unqualified_variant) = t;
9285     }
9286
9287   /* Cache this pointer-to-member type so that we can find it again
9288      later.  */
9289   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9290
9291   /* Seems to be wanted.  */
9292   CLASSTYPE_GOT_SEMICOLON (t) = 1;
9293
9294   return t;
9295 }
9296
9297 /* Create and return a pointer to data member type.  */
9298
9299 tree
9300 build_ptrmem_type (tree class_type, tree member_type)
9301 {
9302   return build_pointer_type (build_offset_type (class_type, member_type));
9303 }
9304
9305 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9306    Check to see that the definition is valid.  Issue appropriate error
9307    messages.  Return 1 if the definition is particularly bad, or 0
9308    otherwise.  */
9309
9310 int
9311 check_static_variable_definition (tree decl, tree type)
9312 {
9313   /* Motion 10 at San Diego: If a static const integral data member is
9314      initialized with an integral constant expression, the initializer
9315      may appear either in the declaration (within the class), or in
9316      the definition, but not both.  If it appears in the class, the
9317      member is a member constant.  The file-scope definition is always
9318      required.  */
9319   if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9320     {
9321       error ("invalid in-class initialization of static data member of non-integral type `%T'",
9322              type);
9323       /* If we just return the declaration, crashes will sometimes
9324          occur.  We therefore return void_type_node, as if this was a
9325          friend declaration, to cause callers to completely ignore
9326          this declaration.  */
9327       return 1;
9328     }
9329   else if (!CP_TYPE_CONST_P (type))
9330     error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9331               decl);
9332   else if (pedantic && !INTEGRAL_TYPE_P (type))
9333     pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9334
9335   return 0;
9336 }
9337
9338 /* Given the SIZE (i.e., number of elements) in an array, compute an
9339    appropriate index type for the array.  If non-NULL, NAME is the
9340    name of the thing being declared.  */
9341
9342 tree
9343 compute_array_index_type (tree name, tree size)
9344 {
9345   tree itype;
9346
9347   /* If this involves a template parameter, it will be a constant at
9348      instantiation time, but we don't know what the value is yet.
9349      Even if no template parameters are involved, we may an expression
9350      that is not a constant; we don't even simplify `1 + 2' when
9351      processing a template.  */
9352   if (processing_template_decl)
9353     {
9354       /* Resolve a qualified reference to an enumerator or static
9355          const data member of ours.  */
9356       if (TREE_CODE (size) == SCOPE_REF
9357           && TREE_OPERAND (size, 0) == current_class_type)
9358         {
9359           tree t = lookup_field (current_class_type,
9360                                  TREE_OPERAND (size, 1), 0, 0);
9361           if (t)
9362             size = t;
9363         }
9364
9365       return build_index_type (build_min (MINUS_EXPR, sizetype,
9366                                           size, integer_one_node));
9367     }
9368
9369   /* The size might be the result of a cast.  */
9370   STRIP_TYPE_NOPS (size);
9371
9372   /* It might be a const variable or enumeration constant.  */
9373   size = decl_constant_value (size);
9374
9375   /* The array bound must be an integer type.  */
9376   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9377       && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9378       && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9379     {
9380       if (name)
9381         error ("size of array `%D' has non-integer type", name);
9382       else
9383         error ("size of array has non-integer type");
9384       size = integer_one_node;
9385     }
9386
9387   /* Normally, the array-bound will be a constant.  */
9388   if (TREE_CODE (size) == INTEGER_CST)
9389     {
9390       /* Check to see if the array bound overflowed.  Make that an
9391          error, no matter how generous we're being.  */
9392       int old_flag_pedantic_errors = flag_pedantic_errors;
9393       int old_pedantic = pedantic;
9394       pedantic = flag_pedantic_errors = 1;
9395       constant_expression_warning (size);
9396       pedantic = old_pedantic;
9397       flag_pedantic_errors = old_flag_pedantic_errors;
9398
9399       /* An array must have a positive number of elements.  */
9400       if (INT_CST_LT (size, integer_zero_node))
9401         {
9402           if (name)
9403             error ("size of array `%D' is negative", name);
9404           else
9405             error ("size of array is negative");
9406           size = integer_one_node;
9407         }
9408       /* Except that an extension we allow zero-sized arrays.  We
9409          always allow them in system headers because glibc uses
9410          them.  */
9411       else if (integer_zerop (size) && pedantic && !in_system_header)
9412         {
9413           if (name)
9414             pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9415           else
9416             pedwarn ("ISO C++ forbids zero-size array");
9417         }
9418     }
9419   else if (TREE_CONSTANT (size))
9420     {
9421       /* `(int) &fn' is not a valid array bound.  */
9422       if (name)
9423         error ("size of array `%D' is not an integral constant-expression",
9424                   name);
9425       else
9426         error ("size of array is not an integral constant-expression");
9427     }
9428
9429   /* Compute the index of the largest element in the array.  It is
9430      one less than the number of elements in the array.  */
9431   itype
9432     = fold (cp_build_binary_op (MINUS_EXPR,
9433                                 cp_convert (ssizetype, size),
9434                                 cp_convert (ssizetype,
9435                                             integer_one_node)));
9436
9437   /* Check for variable-sized arrays.  We allow such things as an
9438      extension, even though they are not allowed in ANSI/ISO C++.  */
9439   if (!TREE_CONSTANT (itype))
9440     {
9441       if (pedantic)
9442         {
9443           if (name)
9444             pedwarn ("ISO C++ forbids variable-size array `%D'",
9445                         name);
9446           else
9447             pedwarn ("ISO C++ forbids variable-size array");
9448         }
9449
9450       /* Create a variable-sized array index type.  */
9451       itype = variable_size (itype);
9452     }
9453   /* Make sure that there was no overflow when creating to a signed
9454      index type.  (For example, on a 32-bit machine, an array with
9455      size 2^32 - 1 is too big.)  */
9456   else if (TREE_OVERFLOW (itype))
9457     {
9458       error ("overflow in array dimension");
9459       TREE_OVERFLOW (itype) = 0;
9460     }
9461
9462   /* Create and return the appropriate index type.  */
9463   return build_index_type (itype);
9464 }
9465
9466 /* Returns the scope (if any) in which the entity declared by
9467    DECLARATOR will be located.  If the entity was declared with an
9468    unqualified name, NULL_TREE is returned.  */
9469
9470 tree
9471 get_scope_of_declarator (tree declarator)
9472 {
9473   if (!declarator)
9474     return NULL_TREE;
9475   
9476   switch (TREE_CODE (declarator))
9477     {
9478     case CALL_EXPR:
9479     case ARRAY_REF:
9480     case INDIRECT_REF:
9481     case ADDR_EXPR:
9482       /* For any of these, the main declarator is the first operand.  */
9483       return get_scope_of_declarator (TREE_OPERAND
9484                                       (declarator, 0));
9485
9486     case SCOPE_REF:
9487       /* For a pointer-to-member, continue descending.  */
9488       if (TREE_CODE (TREE_OPERAND (declarator, 1))
9489           == INDIRECT_REF)
9490         return get_scope_of_declarator (TREE_OPERAND
9491                                         (declarator, 1));
9492       /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9493          which the declaration occurs is the first operand.  */
9494       return TREE_OPERAND (declarator, 0);
9495
9496     case TREE_LIST:
9497       /* Attributes to be applied. The declarator is TREE_VALUE.  */
9498       return get_scope_of_declarator (TREE_VALUE (declarator));
9499       
9500     default:
9501       /* Otherwise, we have a declarator-id which is not a qualified
9502          name; the entity will be declared in the current scope.  */
9503       return NULL_TREE;
9504     }
9505 }
9506
9507 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9508    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
9509    with this type.  */
9510
9511 static tree
9512 create_array_type_for_decl (tree name, tree type, tree size)
9513 {
9514   tree itype = NULL_TREE;
9515   const char* error_msg;
9516
9517   /* If things have already gone awry, bail now.  */
9518   if (type == error_mark_node || size == error_mark_node)
9519     return error_mark_node;
9520
9521   /* Assume that everything will go OK.  */
9522   error_msg = NULL;
9523
9524   /* There are some types which cannot be array elements.  */
9525   switch (TREE_CODE (type))
9526     {
9527     case VOID_TYPE:
9528       error_msg = "array of void";
9529       break;
9530
9531     case FUNCTION_TYPE:
9532       error_msg = "array of functions";
9533       break;
9534
9535     case REFERENCE_TYPE:
9536       error_msg = "array of references";
9537       break;
9538
9539     case OFFSET_TYPE:
9540       error_msg = "array of data members";
9541       break;
9542
9543     case METHOD_TYPE:
9544       error_msg = "array of function members";
9545       break;
9546
9547     default:
9548       break;
9549     }
9550
9551   /* If something went wrong, issue an error-message and return.  */
9552   if (error_msg)
9553     {
9554       if (name)
9555         error ("declaration of `%D' as %s", name, error_msg);
9556       else
9557         error ("creating %s", error_msg);
9558
9559       return error_mark_node;
9560     }
9561
9562   /* [dcl.array]
9563
9564      The constant expressions that specify the bounds of the arrays
9565      can be omitted only for the first member of the sequence.  */
9566   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9567     {
9568       if (name)
9569         error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9570                   name);
9571       else
9572         error ("multidimensional array must have bounds for all dimensions except the first");
9573
9574       return error_mark_node;
9575     }
9576
9577   /* Figure out the index type for the array.  */
9578   if (size)
9579     itype = compute_array_index_type (name, size);
9580
9581   return build_cplus_array_type (type, itype);
9582 }
9583
9584 /* Check that it's OK to declare a function with the indicated TYPE.
9585    SFK indicates the kind of special function (if any) that this
9586    function is.  OPTYPE is the type given in a conversion operator
9587    declaration.  Returns the actual return type of the function; that
9588    may be different than TYPE if an error occurs, or for certain
9589    special functions.  */
9590
9591 static tree
9592 check_special_function_return_type (special_function_kind sfk,
9593                                     tree type,
9594                                     tree optype)
9595 {
9596   switch (sfk)
9597     {
9598     case sfk_constructor:
9599       if (type)
9600         error ("return type specification for constructor invalid");
9601
9602       type = void_type_node;
9603       break;
9604
9605     case sfk_destructor:
9606       if (type)
9607         error ("return type specification for destructor invalid");
9608       type = void_type_node;
9609       break;
9610
9611     case sfk_conversion:
9612       if (type && !same_type_p (type, optype))
9613         error ("operator `%T' declared to return `%T'", optype, type);
9614       else if (type)
9615         pedwarn ("return type specified for `operator %T'",  optype);
9616       type = optype;
9617       break;
9618
9619     default:
9620       abort ();
9621       break;
9622     }
9623
9624   return type;
9625 }
9626
9627 /* Given declspecs and a declarator (abstract or otherwise), determine
9628    the name and type of the object declared and construct a DECL node
9629    for it.
9630
9631    DECLSPECS is a chain of tree_list nodes whose value fields
9632     are the storage classes and type specifiers.
9633
9634    DECL_CONTEXT says which syntactic context this declaration is in:
9635      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9636      FUNCDEF for a function definition.  Like NORMAL but a few different
9637       error messages in each case.  Return value may be zero meaning
9638       this definition is too screwy to try to parse.
9639      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
9640       handle member functions (which have FIELD context).
9641       Return value may be zero meaning this definition is too screwy to
9642       try to parse.
9643      PARM for a parameter declaration (either within a function prototype
9644       or before a function body).  Make a PARM_DECL, or return void_type_node.
9645      CATCHPARM for a parameter declaration before a catch clause.
9646      TYPENAME if for a typename (in a cast or sizeof).
9647       Don't make a DECL node; just return the ..._TYPE node.
9648      FIELD for a struct or union field; make a FIELD_DECL.
9649      BITFIELD for a field with specified width.
9650    INITIALIZED is 1 if the decl has an initializer.
9651
9652    ATTRLIST is a pointer to the list of attributes, which may be NULL
9653    if there are none; *ATTRLIST may be modified if attributes from inside
9654    the declarator should be applied to the declaration.
9655
9656    When this function is called, scoping variables (such as
9657    CURRENT_CLASS_TYPE) should reflect the scope in which the
9658    declaration occurs, not the scope in which the new declaration will
9659    be placed.  For example, on:
9660
9661      void S::f() { ... }
9662
9663    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9664    should not be `S'.  */
9665
9666 tree
9667 grokdeclarator (tree declarator,
9668                 tree declspecs,
9669                 enum decl_context decl_context,
9670                 int initialized,
9671                 tree* attrlist)
9672 {
9673   RID_BIT_TYPE specbits;
9674   int nclasses = 0;
9675   tree spec;
9676   tree type = NULL_TREE;
9677   int longlong = 0;
9678   int type_quals;
9679   int virtualp, explicitp, friendp, inlinep, staticp;
9680   int explicit_int = 0;
9681   int explicit_char = 0;
9682   int defaulted_int = 0;
9683   int extern_langp = 0;
9684   tree dependant_name = NULL_TREE;
9685   
9686   tree typedef_decl = NULL_TREE;
9687   const char *name;
9688   tree typedef_type = NULL_TREE;
9689   int funcdef_flag = 0;
9690   enum tree_code innermost_code = ERROR_MARK;
9691   int bitfield = 0;
9692 #if 0
9693   /* See the code below that used this.  */
9694   tree decl_attr = NULL_TREE;
9695 #endif
9696
9697   /* Keep track of what sort of function is being processed
9698      so that we can warn about default return values, or explicit
9699      return values which do not match prescribed defaults.  */
9700   special_function_kind sfk = sfk_none;
9701
9702   tree dname = NULL_TREE;
9703   tree ctype = current_class_type;
9704   tree ctor_return_type = NULL_TREE;
9705   enum overload_flags flags = NO_SPECIAL;
9706   tree quals = NULL_TREE;
9707   tree raises = NULL_TREE;
9708   int template_count = 0;
9709   tree in_namespace = NULL_TREE;
9710   tree returned_attrs = NULL_TREE;
9711   tree scope = NULL_TREE;
9712
9713   RIDBIT_RESET_ALL (specbits);
9714   if (decl_context == FUNCDEF)
9715     funcdef_flag = 1, decl_context = NORMAL;
9716   else if (decl_context == MEMFUNCDEF)
9717     funcdef_flag = -1, decl_context = FIELD;
9718   else if (decl_context == BITFIELD)
9719     bitfield = 1, decl_context = FIELD;
9720
9721   /* Look inside a declarator for the name being declared
9722      and get it as a string, for an error message.  */
9723   {
9724     tree *next = &declarator;
9725     register tree decl;
9726     name = NULL;
9727
9728     while (next && *next)
9729       {
9730         decl = *next;
9731         switch (TREE_CODE (decl))
9732           {
9733           case TREE_LIST:
9734             /* For attributes.  */
9735             next = &TREE_VALUE (decl);
9736             break;
9737
9738           case COND_EXPR:
9739             ctype = NULL_TREE;
9740             next = &TREE_OPERAND (decl, 0);
9741             break;
9742
9743           case BIT_NOT_EXPR:    /* For C++ destructors!  */
9744             {
9745               tree name = TREE_OPERAND (decl, 0);
9746               tree rename = NULL_TREE;
9747
9748               my_friendly_assert (flags == NO_SPECIAL, 152);
9749               flags = DTOR_FLAG;
9750               sfk = sfk_destructor;
9751               if (TYPE_P (name))
9752                 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9753               my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9754               if (ctype == NULL_TREE)
9755                 {
9756                   if (current_class_type == NULL_TREE)
9757                     {
9758                       error ("destructors must be member functions");
9759                       flags = NO_SPECIAL;
9760                     }
9761                   else
9762                     {
9763                       tree t = constructor_name (current_class_type);
9764                       if (t != name)
9765                         rename = t;
9766                     }
9767                 }
9768               else
9769                 {
9770                   tree t = constructor_name (ctype);
9771                   if (t != name)
9772                     rename = t;
9773                 }
9774
9775               if (rename)
9776                 {
9777                   error ("destructor `%T' must match class name `%T'",
9778                             name, rename);
9779                   TREE_OPERAND (decl, 0) = rename;
9780                 }
9781               next = &name;
9782             }
9783             break;
9784
9785           case ADDR_EXPR:       /* C++ reference declaration */
9786             /* Fall through.  */
9787           case ARRAY_REF:
9788           case INDIRECT_REF:
9789             ctype = NULL_TREE;
9790             innermost_code = TREE_CODE (decl);
9791             next = &TREE_OPERAND (decl, 0);
9792             break;
9793
9794           case CALL_EXPR:
9795             innermost_code = TREE_CODE (decl);
9796             if (decl_context == FIELD && ctype == NULL_TREE)
9797               ctype = current_class_type;
9798             if (ctype
9799                 && TREE_OPERAND (decl, 0)
9800                 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9801                     && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9802                                            ctype)))
9803               TREE_OPERAND (decl, 0) = constructor_name (ctype);
9804             next = &TREE_OPERAND (decl, 0);
9805             decl = *next;
9806             if (ctype != NULL_TREE
9807                 && decl != NULL_TREE && flags != DTOR_FLAG
9808                 && constructor_name_p (decl, ctype))
9809               {
9810                 sfk = sfk_constructor;
9811                 ctor_return_type = ctype;
9812               }
9813             ctype = NULL_TREE;
9814             break;
9815
9816           case TEMPLATE_ID_EXPR:
9817               {
9818                 tree fns = TREE_OPERAND (decl, 0);
9819
9820                 if (TREE_CODE (fns) == LOOKUP_EXPR)
9821                   fns = TREE_OPERAND (fns, 0);
9822
9823                 dname = fns;
9824                 if (TREE_CODE (dname) == COMPONENT_REF)
9825                   dname = TREE_OPERAND (dname, 1);
9826                 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9827                   {
9828                     my_friendly_assert (is_overloaded_fn (dname),
9829                                         19990331);
9830                     dname = DECL_NAME (get_first_fn (dname));
9831                   }
9832               }
9833           /* Fall through.  */
9834
9835           case IDENTIFIER_NODE:
9836             if (TREE_CODE (decl) == IDENTIFIER_NODE)
9837               dname = decl;
9838
9839             next = 0;
9840
9841             if (C_IS_RESERVED_WORD (dname))
9842               {
9843                 error ("declarator-id missing; using reserved word `%D'",
9844                           dname);
9845                 name = IDENTIFIER_POINTER (dname);
9846               }
9847             else if (!IDENTIFIER_TYPENAME_P (dname))
9848               name = IDENTIFIER_POINTER (dname);
9849             else
9850               {
9851                 my_friendly_assert (flags == NO_SPECIAL, 154);
9852                 flags = TYPENAME_FLAG;
9853                 ctor_return_type = TREE_TYPE (dname);
9854                 sfk = sfk_conversion;
9855                 if (IDENTIFIER_GLOBAL_VALUE (dname)
9856                     && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9857                         == TYPE_DECL))
9858                   name = IDENTIFIER_POINTER (dname);
9859                 else
9860                   name = "<invalid operator>";
9861               }
9862             break;
9863
9864             /* C++ extension */
9865           case SCOPE_REF:
9866             {
9867               /* Perform error checking, and decide on a ctype.  */
9868               tree cname = TREE_OPERAND (decl, 0);
9869               if (cname == NULL_TREE)
9870                 ctype = NULL_TREE;
9871               else if (TREE_CODE (cname) == NAMESPACE_DECL)
9872                 {
9873                   ctype = NULL_TREE;
9874                   in_namespace = TREE_OPERAND (decl, 0);
9875                 }
9876               else if (! is_aggr_type (cname, 1))
9877                 ctype = NULL_TREE;
9878               /* Must test TREE_OPERAND (decl, 1), in case user gives
9879                  us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9880               else if (TREE_OPERAND (decl, 1)
9881                        && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9882                 ctype = cname;
9883               else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9884                        || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9885                 {
9886                   /* This might be declaring a member of a template
9887                      parm to be a friend.  */
9888                   ctype = cname;
9889                   dependant_name = TREE_OPERAND (decl, 1);
9890                 }
9891               else if (ctype == NULL_TREE)
9892                 ctype = cname;
9893               else if (TREE_COMPLEXITY (decl) == current_class_depth)
9894                 ;
9895               else
9896                 {
9897                   if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9898                     {
9899                       error ("type `%T' is not derived from type `%T'",
9900                                 cname, ctype);
9901                       ctype = NULL_TREE;
9902                     }
9903                   else
9904                     ctype = cname;
9905                 }
9906
9907               /* It is valid to write:
9908
9909                    class C { void f(); };
9910                    typedef C D;
9911                    void D::f();
9912
9913                  The standard is not clear about whether `typedef const C D' is
9914                  legal; as of 2002-09-15 the committee is considering
9915                  that question.  EDG 3.0 allows that syntax.
9916                  Therefore, we do as well.  */
9917               if (ctype)
9918                 ctype = TYPE_MAIN_VARIANT (ctype);
9919               /* Update the declarator so that when we process it
9920                  again the correct type is present.  */
9921               TREE_OPERAND (decl, 0) = ctype;
9922
9923               if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9924                   && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9925                                          ctype))
9926                 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9927               next = &TREE_OPERAND (decl, 1);
9928               decl = *next;
9929               if (ctype)
9930                 {
9931                   if (TREE_CODE (decl) == IDENTIFIER_NODE
9932                       && constructor_name_p (decl, ctype))
9933                     {
9934                       sfk = sfk_constructor;
9935                       ctor_return_type = ctype;
9936                     }
9937                   else if (TREE_CODE (decl) == BIT_NOT_EXPR
9938                            && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9939                            && constructor_name_p (TREE_OPERAND (decl, 0),
9940                                                   ctype))
9941                     {
9942                       sfk = sfk_destructor;
9943                       ctor_return_type = ctype;
9944                       flags = DTOR_FLAG;
9945                       TREE_OPERAND (decl, 0) = constructor_name (ctype);
9946                       next = &TREE_OPERAND (decl, 0);
9947                     }
9948                 }
9949             }
9950             break;
9951
9952           case ERROR_MARK:
9953             next = 0;
9954             break;
9955
9956           case TYPE_DECL:
9957             /* Parse error puts this typespec where
9958                a declarator should go.  */
9959             error ("`%T' specified as declarator-id", DECL_NAME (decl));
9960             if (TREE_TYPE (decl) == current_class_type)
9961               error ("  perhaps you want `%T' for a constructor",
9962                         current_class_name);
9963             dname = DECL_NAME (decl);
9964             name = IDENTIFIER_POINTER (dname);
9965
9966             /* Avoid giving two errors for this.  */
9967             IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9968
9969             declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9970             *next = dname;
9971             next = 0;
9972             break;
9973
9974           case BASELINK:
9975             next = &BASELINK_FUNCTIONS (decl);
9976             break;
9977
9978           case TEMPLATE_DECL:
9979             /* Sometimes, we see a template-name used as part of a 
9980                decl-specifier like in 
9981                   std::allocator alloc;
9982                Handle that gracefully.  */
9983             error ("invalid use of template-name '%E' in a declarator", decl);
9984             return error_mark_node;
9985             break;
9986             
9987           default:
9988             my_friendly_assert (0, 20020917);
9989           }
9990       }
9991   }
9992
9993   /* A function definition's declarator must have the form of
9994      a function declarator.  */
9995
9996   if (funcdef_flag && innermost_code != CALL_EXPR)
9997     return 0;
9998
9999   if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
10000       && innermost_code != CALL_EXPR
10001       && ! (ctype && declspecs == NULL_TREE))
10002     {
10003       error ("declaration of `%D' as non-function", dname);
10004       return void_type_node;
10005     }
10006
10007   /* Anything declared one level down from the top level
10008      must be one of the parameters of a function
10009      (because the body is at least two levels down).  */
10010
10011   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10012      by not allowing C++ class definitions to specify their parameters
10013      with xdecls (must be spec.d in the parmlist).
10014
10015      Since we now wait to push a class scope until we are sure that
10016      we are in a legitimate method context, we must set oldcname
10017      explicitly (since current_class_name is not yet alive).
10018
10019      We also want to avoid calling this a PARM if it is in a namespace.  */
10020
10021   if (decl_context == NORMAL && !toplevel_bindings_p ())
10022     {
10023       struct cp_binding_level *b = current_binding_level;
10024       current_binding_level = b->level_chain;
10025       if (current_binding_level != 0 && toplevel_bindings_p ())
10026         decl_context = PARM;
10027       current_binding_level = b;
10028     }
10029
10030   if (name == NULL)
10031     name = decl_context == PARM ? "parameter" : "type name";
10032
10033   /* Look through the decl specs and record which ones appear.
10034      Some typespecs are defined as built-in typenames.
10035      Others, the ones that are modifiers of other types,
10036      are represented by bits in SPECBITS: set the bits for
10037      the modifiers that appear.  Storage class keywords are also in SPECBITS.
10038
10039      If there is a typedef name or a type, store the type in TYPE.
10040      This includes builtin typedefs such as `int'.
10041
10042      Set EXPLICIT_INT if the type is `int' or `char' and did not
10043      come from a user typedef.
10044
10045      Set LONGLONG if `long' is mentioned twice.
10046
10047      For C++, constructors and destructors have their own fast treatment.  */
10048
10049   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10050     {
10051       register int i;
10052       register tree id;
10053
10054       /* Certain parse errors slip through.  For example,
10055          `int class;' is not caught by the parser. Try
10056          weakly to recover here.  */
10057       if (TREE_CODE (spec) != TREE_LIST)
10058         return 0;
10059
10060       id = TREE_VALUE (spec);
10061
10062       /* If the entire declaration is itself tagged as deprecated then
10063          suppress reports of deprecated items.  */
10064       if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10065         {
10066           if (deprecated_state != DEPRECATED_SUPPRESS)
10067             warn_deprecated_use (id);
10068         }
10069
10070       if (TREE_CODE (id) == IDENTIFIER_NODE)
10071         {
10072           if (id == ridpointers[(int) RID_INT]
10073               || id == ridpointers[(int) RID_CHAR]
10074               || id == ridpointers[(int) RID_BOOL]
10075               || id == ridpointers[(int) RID_WCHAR])
10076             {
10077               if (type)
10078                 {
10079                   if (id == ridpointers[(int) RID_BOOL])
10080                     error ("`bool' is now a keyword");
10081                   else
10082                     error ("extraneous `%T' ignored", id);
10083                 }
10084               else
10085                 {
10086                   if (id == ridpointers[(int) RID_INT])
10087                     explicit_int = 1;
10088                   else if (id == ridpointers[(int) RID_CHAR])
10089                     explicit_char = 1;
10090                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10091                 }
10092               goto found;
10093             }
10094           /* C++ aggregate types.  */
10095           if (IDENTIFIER_HAS_TYPE_VALUE (id))
10096             {
10097               if (type)
10098                 error ("multiple declarations `%T' and `%T'", type, id);
10099               else
10100                 type = IDENTIFIER_TYPE_VALUE (id);
10101               goto found;
10102             }
10103
10104           for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10105             {
10106               if (ridpointers[i] == id)
10107                 {
10108                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10109                     {
10110                       if (pedantic && ! in_system_header && warn_long_long)
10111                         pedwarn ("ISO C++ does not support `long long'");
10112                       if (longlong)
10113                         error ("`long long long' is too long for GCC");
10114                       else
10115                         longlong = 1;
10116                     }
10117                   else if (RIDBIT_SETP (i, specbits))
10118                     pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10119
10120                   /* Diagnose "__thread extern" or "__thread static".  */
10121                   if (RIDBIT_SETP (RID_THREAD, specbits))
10122                     {
10123                       if (i == (int)RID_EXTERN)
10124                         error ("`__thread' before `extern'");
10125                       else if (i == (int)RID_STATIC)
10126                         error ("`__thread' before `static'");
10127                     }
10128
10129                   if (i == (int)RID_EXTERN
10130                       && TREE_PURPOSE (spec) == error_mark_node)
10131                     /* This extern was part of a language linkage.  */
10132                     extern_langp = 1;
10133
10134                   RIDBIT_SET (i, specbits);
10135                   goto found;
10136                 }
10137             }
10138         }
10139       else if (TREE_CODE (id) == TYPE_DECL)
10140         {
10141           if (type)
10142             error ("multiple declarations `%T' and `%T'", type,
10143                       TREE_TYPE (id));
10144           else
10145             {
10146               type = TREE_TYPE (id);
10147               TREE_VALUE (spec) = type;
10148               typedef_decl = id;
10149             }
10150           goto found;
10151         }
10152       if (type)
10153         error ("two or more data types in declaration of `%s'", name);
10154       else if (TREE_CODE (id) == IDENTIFIER_NODE)
10155         {
10156           register tree t = lookup_name (id, 1);
10157           if (!t || TREE_CODE (t) != TYPE_DECL)
10158             error ("`%s' fails to be a typedef or built in type",
10159                    IDENTIFIER_POINTER (id));
10160           else
10161             {
10162               type = TREE_TYPE (t);
10163               typedef_decl = t;
10164             }
10165         }
10166       else if (id != error_mark_node)
10167         /* Can't change CLASS nodes into RECORD nodes here!  */
10168         type = id;
10169
10170     found: ;
10171     }
10172
10173 #if 0
10174   /* See the code below that used this.  */
10175   if (typedef_decl)
10176     decl_attr = DECL_ATTRIBUTES (typedef_decl);
10177 #endif
10178   typedef_type = type;
10179
10180   /* No type at all: default to `int', and set DEFAULTED_INT
10181      because it was not a user-defined typedef.  */
10182
10183   if (type == NULL_TREE
10184       && (RIDBIT_SETP (RID_SIGNED, specbits)
10185           || RIDBIT_SETP (RID_UNSIGNED, specbits)
10186           || RIDBIT_SETP (RID_LONG, specbits)
10187           || RIDBIT_SETP (RID_SHORT, specbits)))
10188     {
10189       /* These imply 'int'.  */
10190       type = integer_type_node;
10191       defaulted_int = 1;
10192     }
10193
10194   if (sfk != sfk_none)
10195     type = check_special_function_return_type (sfk, type,
10196                                                ctor_return_type);
10197   else if (type == NULL_TREE)
10198     {
10199       int is_main;
10200
10201       explicit_int = -1;
10202
10203       /* We handle `main' specially here, because 'main () { }' is so
10204          common.  With no options, it is allowed.  With -Wreturn-type,
10205          it is a warning.  It is only an error with -pedantic-errors.  */
10206       is_main = (funcdef_flag
10207                  && dname && MAIN_NAME_P (dname)
10208                  && ctype == NULL_TREE
10209                  && in_namespace == NULL_TREE
10210                  && current_namespace == global_namespace);
10211
10212       if (in_system_header || flag_ms_extensions)
10213         /* Allow it, sigh.  */;
10214       else if (pedantic || ! is_main)
10215         pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10216                     name);
10217       else if (warn_return_type)
10218         warning ("ISO C++ forbids declaration of `%s' with no type",
10219                     name);
10220
10221       type = integer_type_node;
10222     }
10223   
10224   ctype = NULL_TREE;
10225
10226   /* Now process the modifiers that were specified
10227      and check for invalid combinations.  */
10228
10229   /* Long double is a special combination.  */
10230
10231   if (RIDBIT_SETP (RID_LONG, specbits)
10232       && TYPE_MAIN_VARIANT (type) == double_type_node)
10233     {
10234       RIDBIT_RESET (RID_LONG, specbits);
10235       type = build_qualified_type (long_double_type_node,
10236                                    cp_type_quals (type));
10237     }
10238
10239   /* Check all other uses of type modifiers.  */
10240
10241   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10242       || RIDBIT_SETP (RID_SIGNED, specbits)
10243       || RIDBIT_SETP (RID_LONG, specbits)
10244       || RIDBIT_SETP (RID_SHORT, specbits))
10245     {
10246       int ok = 0;
10247
10248       if (TREE_CODE (type) == REAL_TYPE)
10249         error ("short, signed or unsigned invalid for `%s'", name);
10250       else if (TREE_CODE (type) != INTEGER_TYPE)
10251         error ("long, short, signed or unsigned invalid for `%s'", name);
10252       else if (RIDBIT_SETP (RID_LONG, specbits)
10253                && RIDBIT_SETP (RID_SHORT, specbits))
10254         error ("long and short specified together for `%s'", name);
10255       else if ((RIDBIT_SETP (RID_LONG, specbits)
10256                 || RIDBIT_SETP (RID_SHORT, specbits))
10257                && explicit_char)
10258         error ("long or short specified with char for `%s'", name);
10259       else if ((RIDBIT_SETP (RID_LONG, specbits)
10260                 || RIDBIT_SETP (RID_SHORT, specbits))
10261                && TREE_CODE (type) == REAL_TYPE)
10262         error ("long or short specified with floating type for `%s'", name);
10263       else if (RIDBIT_SETP (RID_SIGNED, specbits)
10264                && RIDBIT_SETP (RID_UNSIGNED, specbits))
10265         error ("signed and unsigned given together for `%s'", name);
10266       else
10267         {
10268           ok = 1;
10269           if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10270             {
10271               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10272                        name);
10273               if (flag_pedantic_errors)
10274                 ok = 0;
10275             }
10276         }
10277
10278       /* Discard the type modifiers if they are invalid.  */
10279       if (! ok)
10280         {
10281           RIDBIT_RESET (RID_UNSIGNED, specbits);
10282           RIDBIT_RESET (RID_SIGNED, specbits);
10283           RIDBIT_RESET (RID_LONG, specbits);
10284           RIDBIT_RESET (RID_SHORT, specbits);
10285           longlong = 0;
10286         }
10287     }
10288
10289   if (RIDBIT_SETP (RID_COMPLEX, specbits)
10290       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10291     {
10292       error ("complex invalid for `%s'", name);
10293       RIDBIT_RESET (RID_COMPLEX, specbits);
10294     }
10295
10296   /* Decide whether an integer type is signed or not.
10297      Optionally treat bitfields as signed by default.  */
10298   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10299       /* [class.bit]
10300
10301          It is implementation-defined whether a plain (neither
10302          explicitly signed or unsigned) char, short, int, or long
10303          bit-field is signed or unsigned.
10304
10305          Naturally, we extend this to long long as well.  Note that
10306          this does not include wchar_t.  */
10307       || (bitfield && !flag_signed_bitfields
10308           && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10309           /* A typedef for plain `int' without `signed' can be
10310              controlled just like plain `int', but a typedef for
10311              `signed int' cannot be so controlled.  */
10312           && !(typedef_decl
10313                && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10314           && (TREE_CODE (type) == INTEGER_TYPE
10315               || TREE_CODE (type) == CHAR_TYPE)
10316           && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10317     {
10318       if (longlong)
10319         type = long_long_unsigned_type_node;
10320       else if (RIDBIT_SETP (RID_LONG, specbits))
10321         type = long_unsigned_type_node;
10322       else if (RIDBIT_SETP (RID_SHORT, specbits))
10323         type = short_unsigned_type_node;
10324       else if (type == char_type_node)
10325         type = unsigned_char_type_node;
10326       else if (typedef_decl)
10327         type = c_common_unsigned_type (type);
10328       else
10329         type = unsigned_type_node;
10330     }
10331   else if (RIDBIT_SETP (RID_SIGNED, specbits)
10332            && type == char_type_node)
10333     type = signed_char_type_node;
10334   else if (longlong)
10335     type = long_long_integer_type_node;
10336   else if (RIDBIT_SETP (RID_LONG, specbits))
10337     type = long_integer_type_node;
10338   else if (RIDBIT_SETP (RID_SHORT, specbits))
10339     type = short_integer_type_node;
10340
10341   if (RIDBIT_SETP (RID_COMPLEX, specbits))
10342     {
10343       /* If we just have "complex", it is equivalent to
10344          "complex double", but if any modifiers at all are specified it is
10345          the complex form of TYPE.  E.g, "complex short" is
10346          "complex short int".  */
10347
10348       if (defaulted_int && ! longlong
10349           && ! (RIDBIT_SETP (RID_LONG, specbits)
10350                 || RIDBIT_SETP (RID_SHORT, specbits)
10351                 || RIDBIT_SETP (RID_SIGNED, specbits)
10352                 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10353         type = complex_double_type_node;
10354       else if (type == integer_type_node)
10355         type = complex_integer_type_node;
10356       else if (type == float_type_node)
10357         type = complex_float_type_node;
10358       else if (type == double_type_node)
10359         type = complex_double_type_node;
10360       else if (type == long_double_type_node)
10361         type = complex_long_double_type_node;
10362       else
10363         type = build_complex_type (type);
10364     }
10365
10366   type_quals = TYPE_UNQUALIFIED;
10367   if (RIDBIT_SETP (RID_CONST, specbits))
10368     type_quals |= TYPE_QUAL_CONST;
10369   if (RIDBIT_SETP (RID_VOLATILE, specbits))
10370     type_quals |= TYPE_QUAL_VOLATILE;
10371   if (RIDBIT_SETP (RID_RESTRICT, specbits))
10372     type_quals |= TYPE_QUAL_RESTRICT;
10373   if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10374     error ("qualifiers are not allowed on declaration of `operator %T'",
10375               ctor_return_type);
10376
10377   type_quals |= cp_type_quals (type);
10378   type = cp_build_qualified_type_real
10379     (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10380                          ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10381   /* We might have ignored or rejected some of the qualifiers.  */
10382   type_quals = cp_type_quals (type);
10383   
10384   staticp = 0;
10385   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10386   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10387   RIDBIT_RESET (RID_VIRTUAL, specbits);
10388   explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10389   RIDBIT_RESET (RID_EXPLICIT, specbits);
10390
10391   if (RIDBIT_SETP (RID_STATIC, specbits))
10392     staticp = 1 + (decl_context == FIELD);
10393
10394   if (virtualp && staticp == 2)
10395     {
10396       error ("member `%D' cannot be declared both virtual and static",
10397                 dname);
10398       staticp = 0;
10399     }
10400   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10401   RIDBIT_RESET (RID_FRIEND, specbits);
10402
10403   if (dependant_name && !friendp)
10404     {
10405       error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10406       return void_type_node;
10407     }
10408   
10409   /* Warn if two storage classes are given. Default to `auto'.  */
10410
10411   if (RIDBIT_ANY_SET (specbits))
10412     {
10413       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10414       if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10415       if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10416       if (decl_context == PARM && nclasses > 0)
10417         error ("storage class specifiers invalid in parameter declarations");
10418       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10419         {
10420           if (decl_context == PARM)
10421             error ("typedef declaration invalid in parameter declaration");
10422           nclasses++;
10423         }
10424       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10425       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10426       if (!nclasses && !friendp && extern_langp)
10427         nclasses++;
10428     }
10429
10430   /* Give error if `virtual' is used outside of class declaration.  */
10431   if (virtualp
10432       && (current_class_name == NULL_TREE || decl_context != FIELD))
10433     {
10434       error ("virtual outside class declaration");
10435       virtualp = 0;
10436     }
10437
10438   /* Static anonymous unions are dealt with here.  */
10439   if (staticp && decl_context == TYPENAME
10440       && TREE_CODE (declspecs) == TREE_LIST
10441       && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10442     decl_context = FIELD;
10443
10444   /* Warn about storage classes that are invalid for certain
10445      kinds of declarations (parameters, typenames, etc.).  */
10446
10447   /* "static __thread" and "extern __thread" are allowed.  */
10448   if (nclasses == 2
10449       && RIDBIT_SETP (RID_THREAD, specbits)
10450       && (RIDBIT_SETP (RID_EXTERN, specbits)
10451           || RIDBIT_SETP (RID_STATIC, specbits)))
10452     nclasses = 1;
10453     
10454   if (nclasses > 1)
10455     error ("multiple storage classes in declaration of `%s'", name);
10456   else if (decl_context != NORMAL && nclasses > 0)
10457     {
10458       if ((decl_context == PARM || decl_context == CATCHPARM)
10459           && (RIDBIT_SETP (RID_REGISTER, specbits)
10460               || RIDBIT_SETP (RID_AUTO, specbits)))
10461         ;
10462       else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10463         ;
10464       else if (decl_context == FIELD
10465                /* C++ allows static class elements  */
10466                && RIDBIT_SETP (RID_STATIC, specbits))
10467         /* C++ also allows inlines and signed and unsigned elements,
10468            but in those cases we don't come in here.  */
10469         ;
10470       else
10471         {
10472           if (decl_context == FIELD)
10473             {
10474               tree tmp = NULL_TREE;
10475               register int op = 0;
10476
10477               if (declarator)
10478                 {
10479                   /* Avoid trying to get an operand off an identifier node.  */
10480                   if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10481                     tmp = declarator;
10482                   else
10483                     tmp = TREE_OPERAND (declarator, 0);
10484                   op = IDENTIFIER_OPNAME_P (tmp);
10485                   if (IDENTIFIER_TYPENAME_P (tmp))
10486                     {
10487                       if (IDENTIFIER_GLOBAL_VALUE (tmp)
10488                           && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10489                               == TYPE_DECL))
10490                         name = IDENTIFIER_POINTER (tmp);
10491                       else
10492                         name = "<invalid operator>";
10493                     }
10494                 }
10495               error ("storage class specified for %s `%s'",
10496                      op ? "member operator" : "field",
10497                      name);
10498             }
10499           else
10500             {
10501               if (decl_context == PARM || decl_context == CATCHPARM)
10502                 error ("storage class specified for parameter `%s'", name);
10503               else
10504                 error ("storage class specified for typename");
10505             }
10506           RIDBIT_RESET (RID_REGISTER, specbits);
10507           RIDBIT_RESET (RID_AUTO, specbits);
10508           RIDBIT_RESET (RID_EXTERN, specbits);
10509           RIDBIT_RESET (RID_THREAD, specbits);
10510         }
10511     }
10512   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10513     {
10514       if (toplevel_bindings_p ())
10515         {
10516           /* It's common practice (and completely valid) to have a const
10517              be initialized and declared extern.  */
10518           if (!(type_quals & TYPE_QUAL_CONST))
10519             warning ("`%s' initialized and declared `extern'", name);
10520         }
10521       else
10522         error ("`%s' has both `extern' and initializer", name);
10523     }
10524   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10525            && ! toplevel_bindings_p ())
10526     error ("nested function `%s' declared `extern'", name);
10527   else if (toplevel_bindings_p ())
10528     {
10529       if (RIDBIT_SETP (RID_AUTO, specbits))
10530         error ("top-level declaration of `%s' specifies `auto'", name);
10531     }
10532   else if (RIDBIT_SETP (RID_THREAD, specbits)
10533            && !RIDBIT_SETP (RID_EXTERN, specbits)
10534            && !RIDBIT_SETP (RID_STATIC, specbits))
10535     {
10536       error ("function-scope `%s' implicitly auto and declared `__thread'",
10537              name);
10538       RIDBIT_RESET (RID_THREAD, specbits);
10539     }
10540
10541   if (nclasses > 0 && friendp)
10542     error ("storage class specifiers invalid in friend function declarations");
10543
10544   scope = get_scope_of_declarator (declarator);
10545
10546   /* Now figure out the structure of the declarator proper.
10547      Descend through it, creating more complex types, until we reach
10548      the declared identifier (or NULL_TREE, in an abstract declarator).  */
10549
10550   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10551          && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10552     {
10553       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10554          an INDIRECT_REF (for *...),
10555          a CALL_EXPR (for ...(...)),
10556          an identifier (for the name being declared)
10557          or a null pointer (for the place in an absolute declarator
10558          where the name was omitted).
10559          For the last two cases, we have just exited the loop.
10560
10561          For C++ it could also be
10562          a SCOPE_REF (for class :: ...).  In this case, we have converted
10563          sensible names to types, and those are the values we use to
10564          qualify the member name.
10565          an ADDR_EXPR (for &...),
10566          a BIT_NOT_EXPR (for destructors)
10567
10568          At this point, TYPE is the type of elements of an array,
10569          or for a function to return, or for a pointer to point to.
10570          After this sequence of ifs, TYPE is the type of the
10571          array or function or pointer, and DECLARATOR has had its
10572          outermost layer removed.  */
10573
10574       if (type == error_mark_node)
10575         {
10576           if (TREE_CODE (declarator) == SCOPE_REF)
10577             declarator = TREE_OPERAND (declarator, 1);
10578           else
10579             declarator = TREE_OPERAND (declarator, 0);
10580           continue;
10581         }
10582       if (quals != NULL_TREE
10583           && (declarator == NULL_TREE
10584               || TREE_CODE (declarator) != SCOPE_REF))
10585         {
10586           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10587             ctype = TYPE_METHOD_BASETYPE (type);
10588           if (ctype != NULL_TREE)
10589             {
10590               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10591               grok_method_quals (ctype, dummy, quals);
10592               type = TREE_TYPE (dummy);
10593               quals = NULL_TREE;
10594             }
10595         }
10596
10597       switch (TREE_CODE (declarator))
10598         {
10599         case TREE_LIST:
10600           {
10601             /* We encode a declarator with embedded attributes using
10602                a TREE_LIST.  */
10603             tree attrs = TREE_PURPOSE (declarator);
10604             tree inner_decl;
10605             int attr_flags;
10606
10607             declarator = TREE_VALUE (declarator);
10608             inner_decl = declarator;
10609             while (inner_decl != NULL_TREE
10610                    && TREE_CODE (inner_decl) == TREE_LIST)
10611               inner_decl = TREE_VALUE (inner_decl);
10612             attr_flags = 0;
10613             if (inner_decl == NULL_TREE
10614                 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10615               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10616             if (TREE_CODE (inner_decl) == CALL_EXPR)
10617               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10618             if (TREE_CODE (inner_decl) == ARRAY_REF)
10619               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10620             returned_attrs = decl_attributes (&type,
10621                                               chainon (returned_attrs, attrs),
10622                                               attr_flags);
10623           }
10624           break;
10625
10626         case ARRAY_REF:
10627           {
10628             register tree size;
10629
10630             size = TREE_OPERAND (declarator, 1);
10631
10632             /* VC++ spells a zero-sized array with [].  */
10633             if (size == NULL_TREE && decl_context == FIELD && ! staticp
10634                 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10635               size = integer_zero_node;
10636
10637             declarator = TREE_OPERAND (declarator, 0);
10638
10639             type = create_array_type_for_decl (dname, type, size);
10640
10641             ctype = NULL_TREE;
10642           }
10643           break;
10644
10645         case CALL_EXPR:
10646           {
10647             tree arg_types;
10648             int funcdecl_p;
10649             tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10650             tree inner_decl = TREE_OPERAND (declarator, 0);
10651
10652             /* Declaring a function type.
10653                Make sure we have a valid type for the function to return.  */
10654
10655             /* We now know that the TYPE_QUALS don't apply to the
10656                decl, but to its return type.  */
10657             type_quals = TYPE_UNQUALIFIED;
10658
10659             /* Warn about some types functions can't return.  */
10660
10661             if (TREE_CODE (type) == FUNCTION_TYPE)
10662               {
10663                 error ("`%s' declared as function returning a function", name);
10664                 type = integer_type_node;
10665               }
10666             if (TREE_CODE (type) == ARRAY_TYPE)
10667               {
10668                 error ("`%s' declared as function returning an array", name);
10669                 type = integer_type_node;
10670               }
10671
10672             if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10673               inner_decl = TREE_OPERAND (inner_decl, 1);
10674
10675             if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10676               inner_decl = dname;
10677
10678             /* Pick up type qualifiers which should be applied to `this'.  */
10679             quals = CALL_DECLARATOR_QUALS (declarator);
10680
10681             /* Pick up the exception specifications.  */
10682             raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10683
10684             /* Say it's a definition only for the CALL_EXPR
10685                closest to the identifier.  */
10686             funcdecl_p
10687               = inner_decl
10688               && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10689                   || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10690                   || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10691
10692             if (ctype == NULL_TREE
10693                 && decl_context == FIELD
10694                 && funcdecl_p
10695                 && (friendp == 0 || dname == current_class_name))
10696               ctype = current_class_type;
10697
10698             if (ctype && sfk == sfk_conversion)
10699               TYPE_HAS_CONVERSION (ctype) = 1;
10700             if (ctype && constructor_name_p (dname, ctype))
10701               {
10702                 /* We are within a class's scope. If our declarator name
10703                    is the same as the class name, and we are defining
10704                    a function, then it is a constructor/destructor, and
10705                    therefore returns a void type.  */
10706
10707                 if (flags == DTOR_FLAG)
10708                   {
10709                     /* ISO C++ 12.4/2.  A destructor may not be
10710                        declared const or volatile.  A destructor may
10711                        not be static.  */
10712                     if (staticp == 2)
10713                       error ("destructor cannot be static member function");
10714                     if (quals)
10715                       {
10716                         error ("destructors may not be `%s'",
10717                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10718                         quals = NULL_TREE;
10719                       }
10720                     if (decl_context == FIELD)
10721                       {
10722                         if (! member_function_or_else (ctype,
10723                                                        current_class_type,
10724                                                        flags))
10725                           return void_type_node;
10726                       }
10727                   }
10728                 else            /* It's a constructor.  */
10729                   {
10730                     if (explicitp == 1)
10731                       explicitp = 2;
10732                     /* ISO C++ 12.1.  A constructor may not be
10733                        declared const or volatile.  A constructor may
10734                        not be virtual.  A constructor may not be
10735                        static.  */
10736                     if (staticp == 2)
10737                       error ("constructor cannot be static member function");
10738                     if (virtualp)
10739                       {
10740                         pedwarn ("constructors cannot be declared virtual");
10741                         virtualp = 0;
10742                       }
10743                     if (quals)
10744                       {
10745                         error ("constructors may not be `%s'",
10746                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10747                         quals = NULL_TREE;
10748                       }
10749                     {
10750                       RID_BIT_TYPE tmp_bits;
10751                       memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10752                       RIDBIT_RESET (RID_INLINE, tmp_bits);
10753                       RIDBIT_RESET (RID_STATIC, tmp_bits);
10754                       if (RIDBIT_ANY_SET (tmp_bits))
10755                         error ("return value type specifier for constructor ignored");
10756                     }
10757                     if (decl_context == FIELD)
10758                       {
10759                         if (! member_function_or_else (ctype,
10760                                                        current_class_type,
10761                                                        flags))
10762                           return void_type_node;
10763                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10764                         if (sfk != sfk_constructor)
10765                           return NULL_TREE;
10766                       }
10767                   }
10768                 if (decl_context == FIELD)
10769                   staticp = 0;
10770               }
10771             else if (friendp)
10772               {
10773                 if (initialized)
10774                   error ("can't initialize friend function `%s'", name);
10775                 if (virtualp)
10776                   {
10777                     /* Cannot be both friend and virtual.  */
10778                     error ("virtual functions cannot be friends");
10779                     RIDBIT_RESET (RID_FRIEND, specbits);
10780                     friendp = 0;
10781                   }
10782                 if (decl_context == NORMAL)
10783                   error ("friend declaration not in class definition");
10784                 if (current_function_decl && funcdef_flag)
10785                   error ("can't define friend function `%s' in a local class definition",
10786                             name);
10787               }
10788
10789             /* Construct the function type and go to the next
10790                inner layer of declarator.  */
10791
10792             declarator = TREE_OPERAND (declarator, 0);
10793
10794             /* FIXME: This is where default args should be fully
10795                processed.  */
10796
10797             arg_types = grokparms (inner_parms);
10798
10799             if (declarator && flags == DTOR_FLAG)
10800               {
10801                 /* A destructor declared in the body of a class will
10802                    be represented as a BIT_NOT_EXPR.  But, we just
10803                    want the underlying IDENTIFIER.  */
10804                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10805                   declarator = TREE_OPERAND (declarator, 0);
10806
10807                 if (arg_types != void_list_node)
10808                   {
10809                     error ("destructors may not have parameters");
10810                     arg_types = void_list_node;
10811                     last_function_parms = NULL_TREE;
10812                   }
10813               }
10814
10815             /* ANSI says that `const int foo ();'
10816                does not make the function foo const.  */
10817             type = build_function_type (type, arg_types);
10818           }
10819           break;
10820
10821         case ADDR_EXPR:
10822         case INDIRECT_REF:
10823           /* Filter out pointers-to-references and references-to-references.
10824              We can get these if a TYPE_DECL is used.  */
10825
10826           if (TREE_CODE (type) == REFERENCE_TYPE)
10827             {
10828               error (TREE_CODE (declarator) == ADDR_EXPR
10829                      ? "cannot declare reference to `%#T'"
10830                      : "cannot declare pointer to `%#T'", type);
10831               type = TREE_TYPE (type);
10832             }
10833           else if (VOID_TYPE_P (type)
10834                    && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10835             error (ctype ? "cannot declare pointer to `%#T' member"
10836                      : "cannot declare reference to `%#T'", type);
10837
10838           /* Merge any constancy or volatility into the target type
10839              for the pointer.  */
10840
10841           /* We now know that the TYPE_QUALS don't apply to the decl,
10842              but to the target of the pointer.  */
10843           type_quals = TYPE_UNQUALIFIED;
10844
10845           if (TREE_CODE (declarator) == ADDR_EXPR)
10846             {
10847               if (!VOID_TYPE_P (type))
10848                 type = build_reference_type (type);
10849             }
10850           else if (TREE_CODE (type) == METHOD_TYPE)
10851             type = build_ptrmemfunc_type (build_pointer_type (type));
10852           else if (ctype)
10853             type = build_ptrmem_type (ctype, type);
10854           else
10855             type = build_pointer_type (type);
10856
10857           /* Process a list of type modifier keywords (such as
10858              const or volatile) that were given inside the `*' or `&'.  */
10859
10860           if (TREE_TYPE (declarator))
10861             {
10862               register tree typemodlist;
10863               int erred = 0;
10864               int constp = 0;
10865               int volatilep = 0;
10866               int restrictp = 0;
10867               
10868               for (typemodlist = TREE_TYPE (declarator); typemodlist;
10869                    typemodlist = TREE_CHAIN (typemodlist))
10870                 {
10871                   tree qualifier = TREE_VALUE (typemodlist);
10872
10873                   if (qualifier == ridpointers[(int) RID_CONST])
10874                     {
10875                       constp++;
10876                       type_quals |= TYPE_QUAL_CONST;
10877                     }
10878                   else if (qualifier == ridpointers[(int) RID_VOLATILE])
10879                     {
10880                       volatilep++;
10881                       type_quals |= TYPE_QUAL_VOLATILE;
10882                     }
10883                   else if (qualifier == ridpointers[(int) RID_RESTRICT])
10884                     {
10885                       restrictp++;
10886                       type_quals |= TYPE_QUAL_RESTRICT;
10887                     }
10888                   else if (!erred)
10889                     {
10890                       erred = 1;
10891                       error ("invalid type modifier within pointer declarator");
10892                     }
10893                 }
10894               if (constp > 1)
10895                 pedwarn ("duplicate `const'");
10896               if (volatilep > 1)
10897                 pedwarn ("duplicate `volatile'");
10898               if (restrictp > 1)
10899                 pedwarn ("duplicate `restrict'");
10900               type = cp_build_qualified_type (type, type_quals);
10901               type_quals = cp_type_quals (type);
10902             }
10903           declarator = TREE_OPERAND (declarator, 0);
10904           ctype = NULL_TREE;
10905           break;
10906
10907         case SCOPE_REF:
10908           {
10909             /* We have converted type names to NULL_TREE if the
10910                name was bogus, or to a _TYPE node, if not.
10911
10912                The variable CTYPE holds the type we will ultimately
10913                resolve to.  The code here just needs to build
10914                up appropriate member types.  */
10915             tree sname = TREE_OPERAND (declarator, 1);
10916             tree t;
10917
10918             /* Destructors can have their visibilities changed as well.  */
10919             if (TREE_CODE (sname) == BIT_NOT_EXPR)
10920               sname = TREE_OPERAND (sname, 0);
10921
10922             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10923               {
10924                 /* We had a reference to a global decl, or
10925                    perhaps we were given a non-aggregate typedef,
10926                    in which case we cleared this out, and should just
10927                    keep going as though it wasn't there.  */
10928                 declarator = sname;
10929                 continue;
10930               }
10931             ctype = TREE_OPERAND (declarator, 0);
10932
10933             t = ctype;
10934             while (t != NULL_TREE && CLASS_TYPE_P (t))
10935               {
10936                 /* You're supposed to have one `template <...>'
10937                    for every template class, but you don't need one
10938                    for a full specialization.  For example:
10939
10940                      template <class T> struct S{};
10941                      template <> struct S<int> { void f(); };
10942                      void S<int>::f () {}
10943
10944                    is correct; there shouldn't be a `template <>' for
10945                    the definition of `S<int>::f'.  */
10946                 if (CLASSTYPE_TEMPLATE_INFO (t)
10947                     && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10948                         || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10949                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10950                   template_count += 1;
10951
10952                 t = TYPE_MAIN_DECL (t);
10953                 t = DECL_CONTEXT (t);
10954               }
10955
10956             if (sname == NULL_TREE)
10957               goto done_scoping;
10958
10959             if (TREE_CODE (sname) == IDENTIFIER_NODE)
10960               {
10961                 /* This is the `standard' use of the scoping operator:
10962                    basetype :: member .  */
10963
10964                 if (ctype == current_class_type)
10965                   {
10966                     /* class A {
10967                          void A::f ();
10968                        };
10969
10970                        Is this ill-formed?  */
10971
10972                     if (pedantic)
10973                       pedwarn ("extra qualification `%T::' on member `%s' ignored",
10974                                   ctype, name);
10975                   }
10976                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10977                   {
10978                     if (current_class_type == NULL_TREE || friendp)
10979                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
10980                                                       TYPE_ARG_TYPES (type));
10981                     else
10982                       {
10983                         error ("cannot declare member function `%T::%s' within `%T'",
10984                                   ctype, name, current_class_type);
10985                         return error_mark_node;
10986                       }
10987                   }
10988                 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10989                          || COMPLETE_TYPE_P (complete_type (ctype)))
10990                   {
10991                     /* Have to move this code elsewhere in this function.
10992                        this code is used for i.e., typedef int A::M; M *pm;
10993
10994                        It is?  How? jason 10/2/94 */
10995
10996                     if (current_class_type)
10997                       {
10998                         error ("cannot declare member `%T::%s' within `%T'",
10999                                   ctype, name, current_class_type);
11000                         return void_type_node;
11001                       }
11002                   }
11003                 else
11004                   {
11005                     cxx_incomplete_type_error (NULL_TREE, ctype);
11006                     return error_mark_node;
11007                   }
11008
11009                 declarator = sname;
11010               }
11011             else if (TREE_CODE (sname) == SCOPE_REF)
11012               abort ();
11013             else
11014               {
11015               done_scoping:
11016                 declarator = TREE_OPERAND (declarator, 1);
11017                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
11018                   /* In this case, we will deal with it later.  */
11019                   ;
11020                 else if (TREE_CODE (type) == FUNCTION_TYPE)
11021                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11022                                                   TYPE_ARG_TYPES (type));
11023               }
11024           }
11025           break;
11026
11027         case BIT_NOT_EXPR:
11028           declarator = TREE_OPERAND (declarator, 0);
11029           break;
11030
11031         case BASELINK:
11032           declarator = BASELINK_FUNCTIONS (declarator);
11033           break;
11034
11035         case RECORD_TYPE:
11036         case UNION_TYPE:
11037         case ENUMERAL_TYPE:
11038           declarator = NULL_TREE;
11039           break;
11040
11041         case ERROR_MARK:
11042           declarator = NULL_TREE;
11043           break;
11044
11045         default:
11046           abort ();
11047         }
11048     }
11049
11050   if (returned_attrs)
11051     {
11052       if (attrlist)
11053         *attrlist = chainon (returned_attrs, *attrlist);
11054       else
11055         attrlist = &returned_attrs;
11056     }
11057
11058   /* Resolve any TYPENAME_TYPEs from the decl-specifier-seq that refer
11059      to ctype.  They couldn't be resolved earlier because we hadn't
11060      pushed into the class yet.  
11061
11062      For example, consider:
11063
11064        template <typename T>
11065        struct S {
11066          typedef T X;
11067          X f();
11068        };
11069
11070        template <typename T>
11071        typename S<T>::X f() {}
11072
11073        When parsing the decl-specifier-seq for the definition of `f',
11074        we construct a TYPENAME_TYPE for `S<T>::X'.  By substituting
11075        here, we resolve it to the correct type.  */
11076   if (scope && CLASS_TYPE_P (scope)
11077       && current_template_parms
11078       && uses_template_parms (scope))
11079     {
11080       tree args = current_template_args ();
11081       push_scope (scope);
11082       type = tsubst (type, args, tf_error | tf_warning,
11083                      NULL_TREE);
11084       pop_scope (scope);
11085     }
11086
11087   /* Now TYPE has the actual type.  */
11088
11089   /* Did array size calculations overflow?  */
11090
11091   if (TREE_CODE (type) == ARRAY_TYPE
11092       && COMPLETE_TYPE_P (type)
11093       && TREE_OVERFLOW (TYPE_SIZE (type)))
11094     {
11095       error ("size of array `%s' is too large", name);
11096       /* If we proceed with the array type as it is, we'll eventually
11097          crash in tree_low_cst().  */
11098       type = error_mark_node;
11099     }
11100
11101   if (decl_context == FIELD 
11102       && !processing_template_decl 
11103       && variably_modified_type_p (type))
11104     {
11105       error ("data member may not have variably modified type `%T'", type);
11106       type = error_mark_node;
11107     }
11108
11109   if (explicitp == 1 || (explicitp && friendp))
11110     {
11111       /* [dcl.fct.spec] The explicit specifier shall only be used in
11112          declarations of constructors within a class definition.  */
11113       error ("only declarations of constructors can be `explicit'");
11114       explicitp = 0;
11115     }
11116
11117   if (RIDBIT_SETP (RID_MUTABLE, specbits))
11118     {
11119       if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
11120         {
11121           error ("non-member `%s' cannot be declared `mutable'", name);
11122           RIDBIT_RESET (RID_MUTABLE, specbits);
11123         }
11124       else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11125         {
11126           error ("non-object member `%s' cannot be declared `mutable'", name);
11127           RIDBIT_RESET (RID_MUTABLE, specbits);
11128         }
11129       else if (TREE_CODE (type) == FUNCTION_TYPE
11130                || TREE_CODE (type) == METHOD_TYPE)
11131         {
11132           error ("function `%s' cannot be declared `mutable'", name);
11133           RIDBIT_RESET (RID_MUTABLE, specbits);
11134         }
11135       else if (staticp)
11136         {
11137           error ("static `%s' cannot be declared `mutable'", name);
11138           RIDBIT_RESET (RID_MUTABLE, specbits);
11139         }
11140       else if (type_quals & TYPE_QUAL_CONST)
11141         {
11142           error ("const `%s' cannot be declared `mutable'", name);
11143           RIDBIT_RESET (RID_MUTABLE, specbits);
11144         }
11145     }
11146
11147   if (declarator == NULL_TREE
11148       || TREE_CODE (declarator) == IDENTIFIER_NODE
11149       || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11150           && (TREE_CODE (type) == FUNCTION_TYPE
11151               || TREE_CODE (type) == METHOD_TYPE)))
11152     /* OK */;
11153   else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11154     {
11155       error ("template-id `%D' used as a declarator", declarator);
11156       declarator = dname;
11157     }
11158   else
11159     /* Unexpected declarator format.  */
11160     abort ();
11161
11162   /* If this is declaring a typedef name, return a TYPE_DECL.  */
11163
11164   if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11165     {
11166       tree decl;
11167
11168       /* Note that the grammar rejects storage classes
11169          in typenames, fields or parameters.  */
11170       if (current_lang_name == lang_name_java)
11171         TYPE_FOR_JAVA (type) = 1;
11172
11173       if (decl_context == FIELD)
11174         {
11175           if (constructor_name_p (declarator, current_class_type))
11176             pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11177                         declarator);
11178           decl = build_lang_decl (TYPE_DECL, declarator, type);
11179         }
11180       else
11181         {
11182           decl = build_decl (TYPE_DECL, declarator, type);
11183           if (in_namespace || ctype)
11184             cp_error_at ("typedef name may not be a nested-name-specifier",
11185                          decl);
11186           if (!current_function_decl)
11187             DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11188         }
11189       
11190       /* If the user declares "typedef struct {...} foo" then the
11191          struct will have an anonymous name.  Fill that name in now.
11192          Nothing can refer to it, so nothing needs know about the name
11193          change.  */
11194       if (type != error_mark_node
11195           && declarator
11196           && TYPE_NAME (type)
11197           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11198           && TYPE_ANONYMOUS_P (type)
11199           /* Don't do this if there are attributes.  */
11200           && (!attrlist || !*attrlist)
11201           && cp_type_quals (type) == TYPE_UNQUALIFIED)
11202         {
11203           tree oldname = TYPE_NAME (type);
11204           tree t;
11205
11206           /* Replace the anonymous name with the real name everywhere.  */
11207           lookup_tag_reverse (type, declarator);
11208           for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11209             if (TYPE_NAME (t) == oldname)
11210               TYPE_NAME (t) = decl;
11211
11212           if (TYPE_LANG_SPECIFIC (type))
11213             TYPE_WAS_ANONYMOUS (type) = 1;
11214
11215           /* If this is a typedef within a template class, the nested
11216              type is a (non-primary) template.  The name for the
11217              template needs updating as well.  */
11218           if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11219             DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11220               = TYPE_IDENTIFIER (type);
11221
11222           /* FIXME remangle member functions; member functions of a
11223              type with external linkage have external linkage.  */
11224         }
11225
11226       if (quals)
11227         {
11228           if (ctype == NULL_TREE)
11229             {
11230               if (TREE_CODE (type) != METHOD_TYPE)
11231                 cp_error_at ("invalid type qualifier for non-member function type", decl);
11232               else
11233                 ctype = TYPE_METHOD_BASETYPE (type);
11234             }
11235           if (ctype != NULL_TREE)
11236             grok_method_quals (ctype, decl, quals);
11237         }
11238
11239       if (RIDBIT_SETP (RID_SIGNED, specbits)
11240           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11241         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11242
11243       bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11244                       inlinep, friendp, raises != NULL_TREE);
11245
11246       return decl;
11247     }
11248
11249   /* Detect the case of an array type of unspecified size
11250      which came, as such, direct from a typedef name.
11251      We must copy the type, so that the array's domain can be
11252      individually set by the object's initializer.  */
11253
11254   if (type && typedef_type
11255       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11256       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11257     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11258
11259   /* Detect where we're using a typedef of function type to declare a
11260      function. last_function_parms will not be set, so we must create
11261      it now.  */
11262   
11263   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11264     {
11265       tree decls = NULL_TREE;
11266       tree args;
11267
11268       for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11269         {
11270           tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11271
11272           TREE_CHAIN (decl) = decls;
11273           decls = decl;
11274         }
11275       
11276       last_function_parms = nreverse (decls);
11277     }
11278
11279   /* If this is a type name (such as, in a cast or sizeof),
11280      compute the type and return it now.  */
11281
11282   if (decl_context == TYPENAME)
11283     {
11284       /* Note that the grammar rejects storage classes
11285          in typenames, fields or parameters.  */
11286       if (type_quals != TYPE_UNQUALIFIED)
11287         type_quals = TYPE_UNQUALIFIED;
11288
11289       /* Special case: "friend class foo" looks like a TYPENAME context.  */
11290       if (friendp)
11291         {
11292           if (type_quals != TYPE_UNQUALIFIED)
11293             {
11294               error ("type qualifiers specified for friend class declaration");
11295               type_quals = TYPE_UNQUALIFIED;
11296             }
11297           if (inlinep)
11298             {
11299               error ("`inline' specified for friend class declaration");
11300               inlinep = 0;
11301             }
11302
11303           if (!current_aggr)
11304             {
11305               /* Don't allow friend declaration without a class-key.  */
11306               if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11307                 pedwarn ("template parameters cannot be friends");
11308               else if (TREE_CODE (type) == TYPENAME_TYPE)
11309                 pedwarn ("friend declaration requires class-key, "
11310                          "i.e. `friend class %T::%D'",
11311                          TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11312               else
11313                 pedwarn ("friend declaration requires class-key, "
11314                          "i.e. `friend %#T'",
11315                          type);
11316             }
11317
11318           /* Only try to do this stuff if we didn't already give up.  */
11319           if (type != integer_type_node)
11320             {
11321               /* A friendly class?  */
11322               if (current_class_type)
11323                 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11324               else
11325                 error ("trying to make class `%T' a friend of global scope",
11326                           type);
11327
11328               type = void_type_node;
11329             }
11330         }
11331       else if (quals)
11332         {
11333           if (ctype == NULL_TREE)
11334             {
11335               if (TREE_CODE (type) != METHOD_TYPE)
11336                 error ("invalid qualifiers on non-member function type");
11337               else
11338                 ctype = TYPE_METHOD_BASETYPE (type);
11339             }
11340           if (ctype)
11341             {
11342               tree dummy = build_decl (TYPE_DECL, declarator, type);
11343               grok_method_quals (ctype, dummy, quals);
11344               type = TREE_TYPE (dummy);
11345             }
11346         }
11347
11348       return type;
11349     }
11350   else if (declarator == NULL_TREE && decl_context != PARM
11351            && decl_context != CATCHPARM
11352            && TREE_CODE (type) != UNION_TYPE
11353            && ! bitfield)
11354     {
11355       error ("abstract declarator `%T' used as declaration", type);
11356       declarator = make_anon_name ();
11357     }
11358
11359   /* `void' at top level (not within pointer)
11360      is allowed only in typedefs or type names.
11361      We don't complain about parms either, but that is because
11362      a better error message can be made later.  */
11363
11364   if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11365     {
11366       if (! declarator)
11367         error ("unnamed variable or field declared void");
11368       else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11369         {
11370           if (IDENTIFIER_OPNAME_P (declarator))
11371             abort ();
11372           else
11373             error ("variable or field `%s' declared void", name);
11374         }
11375       else
11376         error ("variable or field declared void");
11377       type = integer_type_node;
11378     }
11379
11380   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11381      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
11382
11383   if (decl_context == PARM || decl_context == CATCHPARM)
11384     {
11385       if (ctype || in_namespace)
11386         error ("cannot use `::' in parameter declaration");
11387
11388       /* A parameter declared as an array of T is really a pointer to T.
11389          One declared as a function is really a pointer to a function.
11390          One declared as a member is really a pointer to member.  */
11391
11392       if (TREE_CODE (type) == ARRAY_TYPE)
11393         {
11394           /* Transfer const-ness of array into that of type pointed to.  */
11395           type = build_pointer_type (TREE_TYPE (type));
11396           type_quals = TYPE_UNQUALIFIED;
11397         }
11398       else if (TREE_CODE (type) == FUNCTION_TYPE)
11399         type = build_pointer_type (type);
11400       else if (TREE_CODE (type) == OFFSET_TYPE)
11401         type = build_pointer_type (type);
11402     }
11403
11404   {
11405     register tree decl;
11406
11407     if (decl_context == PARM)
11408       {
11409         decl = cp_build_parm_decl (declarator, type);
11410
11411         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11412                         inlinep, friendp, raises != NULL_TREE);
11413       }
11414     else if (decl_context == FIELD)
11415       {
11416         if (type == error_mark_node)
11417           {
11418             /* Happens when declaring arrays of sizes which
11419                are error_mark_node, for example.  */
11420             decl = NULL_TREE;
11421           }
11422         else if (in_namespace && !friendp)
11423           {
11424             /* Something like struct S { int N::j; };  */
11425             error ("invalid use of `::'");
11426             decl = NULL_TREE;
11427           }
11428         else if (TREE_CODE (type) == FUNCTION_TYPE)
11429           {
11430             int publicp = 0;
11431             tree function_context;
11432
11433             /* We catch the others as conflicts with the builtin
11434                typedefs.  */
11435             if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11436               {
11437                 error ("function `%D' cannot be declared friend",
11438                           declarator);
11439                 friendp = 0;
11440               }
11441
11442             if (friendp == 0)
11443               {
11444                 if (ctype == NULL_TREE)
11445                   ctype = current_class_type;
11446
11447                 if (ctype == NULL_TREE)
11448                   {
11449                     error ("can't make `%D' into a method -- not in a class",
11450                               declarator);
11451                     return void_type_node;
11452                   }
11453
11454                 /* ``A union may [ ... ] not [ have ] virtual functions.''
11455                    ARM 9.5 */
11456                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11457                   {
11458                     error ("function `%D' declared virtual inside a union",
11459                               declarator);
11460                     return void_type_node;
11461                   }
11462
11463                 if (declarator == ansi_opname (NEW_EXPR)
11464                     || declarator == ansi_opname (VEC_NEW_EXPR)
11465                     || declarator == ansi_opname (DELETE_EXPR)
11466                     || declarator == ansi_opname (VEC_DELETE_EXPR))
11467                   {
11468                     if (virtualp)
11469                       {
11470                         error ("`%D' cannot be declared virtual, since it is always static",
11471                                   declarator);
11472                         virtualp = 0;
11473                       }
11474                   }
11475                 else if (staticp < 2)
11476                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11477                                                   TYPE_ARG_TYPES (type));
11478               }
11479
11480             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
11481             function_context = (ctype != NULL_TREE) ?
11482               decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11483             publicp = (! friendp || ! staticp)
11484               && function_context == NULL_TREE;
11485             decl = grokfndecl (ctype, type,
11486                                TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11487                                ? declarator : dname,
11488                                declarator,
11489                                virtualp, flags, quals, raises,
11490                                friendp ? -1 : 0, friendp, publicp, inlinep,
11491                                funcdef_flag, template_count, in_namespace);
11492             if (decl == NULL_TREE)
11493               return decl;
11494 #if 0
11495             /* This clobbers the attrs stored in `decl' from `attrlist'.  */
11496             /* The decl and setting of decl_attr is also turned off.  */
11497             decl = build_decl_attribute_variant (decl, decl_attr);
11498 #endif
11499
11500             /* [class.conv.ctor]
11501
11502                A constructor declared without the function-specifier
11503                explicit that can be called with a single parameter
11504                specifies a conversion from the type of its first
11505                parameter to the type of its class.  Such a constructor
11506                is called a converting constructor.  */
11507             if (explicitp == 2)
11508               DECL_NONCONVERTING_P (decl) = 1;
11509             else if (DECL_CONSTRUCTOR_P (decl))
11510               {
11511                 /* The constructor can be called with exactly one
11512                    parameter if there is at least one parameter, and
11513                    any subsequent parameters have default arguments.
11514                    Ignore any compiler-added parms.  */
11515                 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11516
11517                 if (arg_types == void_list_node
11518                     || (arg_types
11519                         && TREE_CHAIN (arg_types)
11520                         && TREE_CHAIN (arg_types) != void_list_node
11521                         && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11522                   DECL_NONCONVERTING_P (decl) = 1;
11523               }
11524           }
11525         else if (TREE_CODE (type) == METHOD_TYPE)
11526           {
11527             /* We only get here for friend declarations of
11528                members of other classes.  */
11529             /* All method decls are public, so tell grokfndecl to set
11530                TREE_PUBLIC, also.  */
11531             decl = grokfndecl (ctype, type, declarator, declarator,
11532                                virtualp, flags, quals, raises,
11533                                friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11534                                template_count, in_namespace);
11535             if (decl == NULL_TREE)
11536               return NULL_TREE;
11537           }
11538         else if (!staticp && ! processing_template_decl
11539                  && !COMPLETE_TYPE_P (complete_type (type))
11540                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11541           {
11542             if (declarator)
11543               error ("field `%D' has incomplete type", declarator);
11544             else
11545               error ("name `%T' has incomplete type", type);
11546
11547             /* If we're instantiating a template, tell them which
11548                instantiation made the field's type be incomplete.  */
11549             if (current_class_type
11550                 && TYPE_NAME (current_class_type)
11551                 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11552                 && declspecs && TREE_VALUE (declspecs)
11553                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11554               error ("  in instantiation of template `%T'",
11555                         current_class_type);
11556
11557             type = error_mark_node;
11558             decl = NULL_TREE;
11559           }
11560         else
11561           {
11562             if (friendp)
11563               {
11564                 error ("`%s' is neither function nor member function; cannot be declared friend",
11565                        IDENTIFIER_POINTER (declarator));
11566                 friendp = 0;
11567               }
11568             decl = NULL_TREE;
11569           }
11570
11571         if (friendp)
11572           {
11573             /* Friends are treated specially.  */
11574             if (ctype == current_class_type)
11575               warning ("member functions are implicitly friends of their class");
11576             else
11577               {
11578                 tree t = NULL_TREE;
11579                 if (decl && DECL_NAME (decl))
11580                   {
11581                     if (template_class_depth (current_class_type) == 0)
11582                       {
11583                         decl
11584                           = check_explicit_specialization
11585                           (declarator, decl,
11586                            template_count, 2 * (funcdef_flag != 0) + 4);
11587                         if (decl == error_mark_node)
11588                           return error_mark_node;
11589                       }
11590                     
11591                     t = do_friend (ctype, declarator, decl,
11592                                    last_function_parms, *attrlist,
11593                                    flags, quals, funcdef_flag);
11594                   }
11595                 if (t && funcdef_flag)
11596                   return t;
11597   
11598                 return void_type_node;
11599               }
11600           }
11601
11602         /* Structure field.  It may not be a function, except for C++ */
11603
11604         if (decl == NULL_TREE)
11605           {
11606             if (initialized)
11607               {
11608                 if (!staticp)
11609                   {
11610                     /* An attempt is being made to initialize a non-static
11611                        member.  But, from [class.mem]:
11612
11613                        4 A member-declarator can contain a
11614                        constant-initializer only if it declares a static
11615                        member (_class.static_) of integral or enumeration
11616                        type, see _class.static.data_.
11617
11618                        This used to be relatively common practice, but
11619                        the rest of the compiler does not correctly
11620                        handle the initialization unless the member is
11621                        static so we make it static below.  */
11622                     pedwarn ("ISO C++ forbids initialization of member `%D'",
11623                                 declarator);
11624                     pedwarn ("making `%D' static", declarator);
11625                     staticp = 1;
11626                   }
11627
11628                 if (uses_template_parms (type))
11629                   /* We'll check at instantiation time.  */
11630                   ;
11631                 else if (check_static_variable_definition (declarator,
11632                                                            type))
11633                   /* If we just return the declaration, crashes
11634                      will sometimes occur.  We therefore return
11635                      void_type_node, as if this was a friend
11636                      declaration, to cause callers to completely
11637                      ignore this declaration.  */
11638                   return void_type_node;
11639               }
11640
11641             /* 9.2p13 [class.mem] */
11642             if (constructor_name_p (declarator, current_class_type)
11643                 /* The standard does not allow non-static data members
11644                    here either, but we agreed at the 10/99 meeting
11645                    to change that in TC 1 so that they are allowed in
11646                    classes with no user-defined constructors.  */
11647                 && staticp)
11648               pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11649                           declarator);
11650
11651             if (staticp)
11652               {
11653                 /* C++ allows static class members.  All other work
11654                    for this is done by grokfield.  */
11655                 decl = build_lang_decl (VAR_DECL, declarator, type);
11656                 TREE_STATIC (decl) = 1;
11657                 /* In class context, 'static' means public access.  */
11658                 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11659               }
11660             else
11661               {
11662                 decl = build_decl (FIELD_DECL, declarator, type);
11663                 DECL_NONADDRESSABLE_P (decl) = bitfield;
11664                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11665                   {
11666                     DECL_MUTABLE_P (decl) = 1;
11667                     RIDBIT_RESET (RID_MUTABLE, specbits);
11668                   }
11669               }
11670
11671             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11672                             inlinep, friendp, raises != NULL_TREE);
11673           }
11674       }
11675     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11676       {
11677         tree original_name;
11678         int publicp = 0;
11679
11680         if (! declarator)
11681           return NULL_TREE;
11682
11683         if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11684           original_name = dname;
11685         else
11686           original_name = declarator;
11687
11688         if (RIDBIT_SETP (RID_AUTO, specbits))
11689           error ("storage class `auto' invalid for function `%s'", name);
11690         else if (RIDBIT_SETP (RID_REGISTER, specbits))
11691           error ("storage class `register' invalid for function `%s'", name);
11692         else if (RIDBIT_SETP (RID_THREAD, specbits))
11693           error ("storage class `__thread' invalid for function `%s'", name);
11694
11695         /* Function declaration not at top level.
11696            Storage classes other than `extern' are not allowed
11697            and `extern' makes no difference.  */
11698         if (! toplevel_bindings_p ()
11699             && (RIDBIT_SETP (RID_STATIC, specbits)
11700                 || RIDBIT_SETP (RID_INLINE, specbits))
11701             && pedantic)
11702           {
11703             if (RIDBIT_SETP (RID_STATIC, specbits))
11704               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11705             else
11706               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11707           }
11708
11709         if (ctype == NULL_TREE)
11710           {
11711             if (virtualp)
11712               {
11713                 error ("virtual non-class function `%s'", name);
11714                 virtualp = 0;
11715               }
11716           }
11717         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11718           type = build_cplus_method_type (ctype, TREE_TYPE (type),
11719                                           TYPE_ARG_TYPES (type));
11720
11721         /* Record presence of `static'.  */
11722         publicp = (ctype != NULL_TREE
11723                    || RIDBIT_SETP (RID_EXTERN, specbits)
11724                    || !RIDBIT_SETP (RID_STATIC, specbits));
11725
11726         decl = grokfndecl (ctype, type, original_name, declarator,
11727                            virtualp, flags, quals, raises,
11728                            1, friendp,
11729                            publicp, inlinep, funcdef_flag,
11730                            template_count, in_namespace);
11731         if (decl == NULL_TREE)
11732           return NULL_TREE;
11733
11734         if (staticp == 1)
11735           {
11736             int invalid_static = 0;
11737
11738             /* Don't allow a static member function in a class, and forbid
11739                declaring main to be static.  */
11740             if (TREE_CODE (type) == METHOD_TYPE)
11741               {
11742                 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11743                 invalid_static = 1;
11744               }
11745             else if (current_function_decl)
11746               {
11747                 /* FIXME need arm citation */
11748                 error ("cannot declare static function inside another function");
11749                 invalid_static = 1;
11750               }
11751
11752             if (invalid_static)
11753               {
11754                 staticp = 0;
11755                 RIDBIT_RESET (RID_STATIC, specbits);
11756               }
11757           }
11758       }
11759     else
11760       {
11761         /* It's a variable.  */
11762
11763         /* An uninitialized decl with `extern' is a reference.  */
11764         decl = grokvardecl (type, declarator, &specbits,
11765                             initialized,
11766                             (type_quals & TYPE_QUAL_CONST) != 0,
11767                             ctype ? ctype : in_namespace);
11768         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11769                         inlinep, friendp, raises != NULL_TREE);
11770
11771         if (ctype)
11772           {
11773             DECL_CONTEXT (decl) = ctype;
11774             if (staticp == 1)
11775               {
11776                 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11777                 staticp = 0;
11778                 RIDBIT_RESET (RID_STATIC, specbits);
11779               }
11780             if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11781               {
11782                 error ("static member `%D' declared `register'", decl);
11783                 RIDBIT_RESET (RID_REGISTER, specbits);
11784               }
11785             if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11786               {
11787                 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11788                             decl);
11789                 RIDBIT_RESET (RID_EXTERN, specbits);
11790               }
11791           }
11792       }
11793
11794     my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11795
11796     /* Record `register' declaration for warnings on &
11797        and in case doing stupid register allocation.  */
11798
11799     if (RIDBIT_SETP (RID_REGISTER, specbits))
11800       DECL_REGISTER (decl) = 1;
11801
11802     if (RIDBIT_SETP (RID_EXTERN, specbits))
11803       DECL_THIS_EXTERN (decl) = 1;
11804
11805     if (RIDBIT_SETP (RID_STATIC, specbits))
11806       DECL_THIS_STATIC (decl) = 1;
11807
11808     /* Record constancy and volatility.  There's no need to do this
11809        when processing a template; we'll do this for the instantiated
11810        declaration based on the type of DECL.  */
11811     if (!processing_template_decl)
11812       c_apply_type_quals_to_decl (type_quals, decl);
11813
11814     return decl;
11815   }
11816 }
11817 \f
11818 /* Subroutine of start_function.  Ensure that each of the parameter
11819    types (as listed in PARMS) is complete, as is required for a
11820    function definition.  */
11821
11822 static void
11823 require_complete_types_for_parms (tree parms)
11824 {
11825   for (; parms; parms = TREE_CHAIN (parms))
11826     {
11827       if (VOID_TYPE_P (TREE_TYPE (parms)))
11828         /* grokparms will have already issued an error */
11829         TREE_TYPE (parms) = error_mark_node;
11830       else if (complete_type_or_else (TREE_TYPE (parms), parms))
11831         {
11832           layout_decl (parms, 0);
11833           DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11834         }
11835       else
11836         TREE_TYPE (parms) = error_mark_node;
11837     }
11838 }
11839
11840 /* Returns nonzero if T is a local variable.  */
11841
11842 int
11843 local_variable_p (tree t)
11844 {
11845   if ((TREE_CODE (t) == VAR_DECL
11846        /* A VAR_DECL with a context that is a _TYPE is a static data
11847           member.  */
11848        && !TYPE_P (CP_DECL_CONTEXT (t))
11849        /* Any other non-local variable must be at namespace scope.  */
11850        && !DECL_NAMESPACE_SCOPE_P (t))
11851       || (TREE_CODE (t) == PARM_DECL))
11852     return 1;
11853
11854   return 0;
11855 }
11856
11857 /* Returns nonzero if T is an automatic local variable or a label.
11858    (These are the declarations that need to be remapped when the code
11859    containing them is duplicated.)  */
11860
11861 int
11862 nonstatic_local_decl_p (tree t)
11863 {
11864   return ((local_variable_p (t) && !TREE_STATIC (t))
11865           || TREE_CODE (t) == LABEL_DECL
11866           || TREE_CODE (t) == RESULT_DECL);
11867 }
11868
11869 /* Like local_variable_p, but suitable for use as a tree-walking
11870    function.  */
11871
11872 static tree
11873 local_variable_p_walkfn (tree* tp,
11874                          int* walk_subtrees ATTRIBUTE_UNUSED ,
11875                          void* data ATTRIBUTE_UNUSED )
11876 {
11877   return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11878           ? *tp : NULL_TREE);
11879 }
11880
11881 /* Check that ARG, which is a default-argument expression for a
11882    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
11883    something goes wrong.  DECL may also be a _TYPE node, rather than a
11884    DECL, if there is no DECL available.  */
11885
11886 tree
11887 check_default_argument (tree decl, tree arg)
11888 {
11889   tree var;
11890   tree decl_type;
11891
11892   if (TREE_CODE (arg) == DEFAULT_ARG)
11893     /* We get a DEFAULT_ARG when looking at an in-class declaration
11894        with a default argument.  Ignore the argument for now; we'll
11895        deal with it after the class is complete.  */
11896     return arg;
11897
11898   if (processing_template_decl || uses_template_parms (arg))
11899     /* We don't do anything checking until instantiation-time.  Note
11900        that there may be uninstantiated arguments even for an
11901        instantiated function, since default arguments are not
11902        instantiated until they are needed.  */
11903     return arg;
11904
11905   if (TYPE_P (decl))
11906     {
11907       decl_type = decl;
11908       decl = NULL_TREE;
11909     }
11910   else
11911     decl_type = TREE_TYPE (decl);
11912
11913   if (arg == error_mark_node
11914       || decl == error_mark_node
11915       || TREE_TYPE (arg) == error_mark_node
11916       || decl_type == error_mark_node)
11917     /* Something already went wrong.  There's no need to check
11918        further.  */
11919     return error_mark_node;
11920
11921   /* [dcl.fct.default]
11922
11923      A default argument expression is implicitly converted to the
11924      parameter type.  */
11925   if (!TREE_TYPE (arg)
11926       || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11927     {
11928       if (decl)
11929         error ("default argument for `%#D' has type `%T'",
11930                   decl, TREE_TYPE (arg));
11931       else
11932         error ("default argument for parameter of type `%T' has type `%T'",
11933                   decl_type, TREE_TYPE (arg));
11934
11935       return error_mark_node;
11936     }
11937
11938   /* [dcl.fct.default]
11939
11940      Local variables shall not be used in default argument
11941      expressions.
11942
11943      The keyword `this' shall not be used in a default argument of a
11944      member function.  */
11945   var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11946                                       NULL);
11947   if (var)
11948     {
11949       error ("default argument `%E' uses local variable `%D'",
11950                 arg, var);
11951       return error_mark_node;
11952     }
11953
11954   /* All is well.  */
11955   return arg;
11956 }
11957
11958 /* Decode the list of parameter types for a function type.
11959    Given the list of things declared inside the parens,
11960    return a list of types.
11961
11962    We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11963    flag. If unset, we append void_list_node. A parmlist declared
11964    as `(void)' is accepted as the empty parmlist.
11965
11966    Also set last_function_parms to the chain of PARM_DECLs.  */
11967
11968 static tree
11969 grokparms (tree first_parm)
11970 {
11971   tree result = NULL_TREE;
11972   tree decls = NULL_TREE;
11973   int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11974   tree parm, chain;
11975   int any_error = 0;
11976
11977   my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11978
11979   for (parm = first_parm; parm != NULL_TREE; parm = chain)
11980     {
11981       tree type = NULL_TREE;
11982       tree decl = TREE_VALUE (parm);
11983       tree init = TREE_PURPOSE (parm);
11984       tree specs, attrs;
11985
11986       chain = TREE_CHAIN (parm);
11987       /* @@ weak defense against parse errors.  */
11988       if (TREE_CODE (decl) != VOID_TYPE
11989           && TREE_CODE (decl) != TREE_LIST)
11990         {
11991           /* Give various messages as the need arises.  */
11992           if (TREE_CODE (decl) == STRING_CST)
11993             error ("invalid string constant `%E'", decl);
11994           else if (TREE_CODE (decl) == INTEGER_CST)
11995             error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11996           continue;
11997         }
11998
11999       if (parm == void_list_node)
12000         break;
12001
12002       split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
12003       decl = grokdeclarator (TREE_VALUE (decl), specs,
12004                              PARM, init != NULL_TREE, &attrs);
12005       if (! decl || TREE_TYPE (decl) == error_mark_node)
12006         continue;
12007
12008       if (attrs)
12009         cplus_decl_attributes (&decl, attrs, 0);
12010
12011       type = TREE_TYPE (decl);
12012       if (VOID_TYPE_P (type))
12013         {
12014           if (same_type_p (type, void_type_node)
12015               && !DECL_NAME (decl) && !result && !chain && !ellipsis)
12016             /* this is a parmlist of `(void)', which is ok.  */
12017             break;
12018           cxx_incomplete_type_error (decl, type);
12019           /* It's not a good idea to actually create parameters of
12020              type `void'; other parts of the compiler assume that a
12021              void type terminates the parameter list.  */
12022           type = error_mark_node;
12023           TREE_TYPE (decl) = error_mark_node;
12024         }
12025
12026       if (type != error_mark_node)
12027         {
12028           /* Top-level qualifiers on the parameters are
12029              ignored for function types.  */
12030           type = TYPE_MAIN_VARIANT (type);
12031           if (TREE_CODE (type) == METHOD_TYPE)
12032             {
12033               error ("parameter `%D' invalidly declared method type", decl);
12034               type = build_pointer_type (type);
12035               TREE_TYPE (decl) = type;
12036             }
12037           else if (TREE_CODE (type) == OFFSET_TYPE)
12038             {
12039               error ("parameter `%D' invalidly declared offset type", decl);
12040               type = build_pointer_type (type);
12041               TREE_TYPE (decl) = type;
12042             }
12043           else if (abstract_virtuals_error (decl, type))
12044             any_error = 1;  /* Seems like a good idea.  */
12045           else if (POINTER_TYPE_P (type))
12046             {
12047               /* [dcl.fct]/6, parameter types cannot contain pointers
12048                  (references) to arrays of unknown bound.  */
12049               tree t = TREE_TYPE (type);
12050               int ptr = TYPE_PTR_P (type);
12051
12052               while (1)
12053                 {
12054                   if (TYPE_PTR_P (t))
12055                     ptr = 1;
12056                   else if (TREE_CODE (t) != ARRAY_TYPE)
12057                     break;
12058                   else if (!TYPE_DOMAIN (t))
12059                     break;
12060                   t = TREE_TYPE (t);
12061                 }
12062               if (TREE_CODE (t) == ARRAY_TYPE)
12063                 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12064                           decl, ptr ? "pointer" : "reference", t);
12065             }
12066
12067           if (!any_error && init)
12068             init = check_default_argument (decl, init);
12069           else
12070             init = NULL_TREE;
12071         }
12072
12073       TREE_CHAIN (decl) = decls;
12074       decls = decl;
12075       result = tree_cons (init, type, result);
12076     }
12077   decls = nreverse (decls);
12078   result = nreverse (result);
12079   if (!ellipsis)
12080     result = chainon (result, void_list_node);
12081   last_function_parms = decls;
12082
12083   return result;
12084 }
12085
12086 \f
12087 /* D is a constructor or overloaded `operator='.
12088
12089    Let T be the class in which D is declared. Then, this function
12090    returns:
12091
12092    -1 if D's is an ill-formed constructor or copy assignment operator
12093       whose first parameter is of type `T'.
12094    0  if D is not a copy constructor or copy assignment
12095       operator.
12096    1  if D is a copy constructor or copy assignment operator whose
12097       first parameter is a reference to const qualified T.
12098    2  if D is a copy constructor or copy assignment operator whose
12099       first parameter is a reference to non-const qualified T.
12100
12101    This function can be used as a predicate. Positive values indicate
12102    a copy constructor and nonzero values indicate a copy assignment
12103    operator.  */
12104
12105 int
12106 copy_fn_p (tree d)
12107 {
12108   tree args;
12109   tree arg_type;
12110   int result = 1;
12111   
12112   my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12113
12114   if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12115     /* Instantiations of template member functions are never copy
12116        functions.  Note that member functions of templated classes are
12117        represented as template functions internally, and we must
12118        accept those as copy functions.  */
12119     return 0;
12120     
12121   args = FUNCTION_FIRST_USER_PARMTYPE (d);
12122   if (!args)
12123     return 0;
12124
12125   arg_type = TREE_VALUE (args);
12126
12127   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12128     {
12129       /* Pass by value copy assignment operator.  */
12130       result = -1;
12131     }
12132   else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12133            && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12134     {
12135       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12136         result = 2;
12137     }
12138   else
12139     return 0;
12140   
12141   args = TREE_CHAIN (args);
12142
12143   if (args && args != void_list_node && !TREE_PURPOSE (args))
12144     /* There are more non-optional args.  */
12145     return 0;
12146
12147   return result;
12148 }
12149
12150 /* Remember any special properties of member function DECL.  */
12151
12152 void grok_special_member_properties (tree decl)
12153 {
12154   if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12155     ; /* Not special.  */
12156   else if (DECL_CONSTRUCTOR_P (decl))
12157     {
12158       int ctor = copy_fn_p (decl);
12159       
12160       if (ctor > 0)
12161         {
12162           /* [class.copy]
12163               
12164              A non-template constructor for class X is a copy
12165              constructor if its first parameter is of type X&, const
12166              X&, volatile X& or const volatile X&, and either there
12167              are no other parameters or else all other parameters have
12168              default arguments.  */
12169           TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12170           if (ctor > 1)
12171             TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12172         }
12173       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12174         TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12175     }
12176   else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12177     {
12178       /* [class.copy]
12179           
12180          A non-template assignment operator for class X is a copy
12181          assignment operator if its parameter is of type X, X&, const
12182          X&, volatile X& or const volatile X&.  */
12183       
12184       int assop = copy_fn_p (decl);
12185       
12186       if (assop)
12187         {
12188           TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12189           if (assop != 1)
12190             TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12191           if (DECL_PURE_VIRTUAL_P (decl))
12192             TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12193         }
12194     }
12195 }
12196
12197 /* Check a constructor DECL has the correct form.  Complains
12198    if the class has a constructor of the form X(X).  */
12199
12200 int
12201 grok_ctor_properties (tree ctype, tree decl)
12202 {
12203   int ctor_parm = copy_fn_p (decl);
12204
12205   if (ctor_parm < 0)
12206     {
12207       /* [class.copy]
12208           
12209          A declaration of a constructor for a class X is ill-formed if
12210          its first parameter is of type (optionally cv-qualified) X
12211          and either there are no other parameters or else all other
12212          parameters have default arguments.
12213           
12214          We *don't* complain about member template instantiations that
12215          have this form, though; they can occur as we try to decide
12216          what constructor to use during overload resolution.  Since
12217          overload resolution will never prefer such a constructor to
12218          the non-template copy constructor (which is either explicitly
12219          or implicitly defined), there's no need to worry about their
12220          existence.  Theoretically, they should never even be
12221          instantiated, but that's hard to forestall.  */
12222       error ("invalid constructor; you probably meant `%T (const %T&)'",
12223                 ctype, ctype);
12224       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12225       return 0;
12226     }
12227   
12228   return 1;
12229 }
12230
12231 /* An operator with this code is unary, but can also be binary.  */
12232
12233 static int
12234 ambi_op_p (enum tree_code code)
12235 {
12236   return (code == INDIRECT_REF
12237           || code == ADDR_EXPR
12238           || code == CONVERT_EXPR
12239           || code == NEGATE_EXPR
12240           || code == PREINCREMENT_EXPR
12241           || code == PREDECREMENT_EXPR);
12242 }
12243
12244 /* An operator with this name can only be unary.  */
12245
12246 static int
12247 unary_op_p (enum tree_code code)
12248 {
12249   return (code == TRUTH_NOT_EXPR
12250           || code == BIT_NOT_EXPR
12251           || code == COMPONENT_REF
12252           || code == TYPE_EXPR);
12253 }
12254
12255 /* Do a little sanity-checking on how they declared their operator.  */
12256
12257 void
12258 grok_op_properties (tree decl, int friendp)
12259 {
12260   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12261   tree argtype;
12262   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12263   tree name = DECL_NAME (decl);
12264   enum tree_code operator_code;
12265   int arity;
12266
12267   /* Count the number of arguments.  */
12268   for (argtype = argtypes, arity = 0;
12269        argtype && argtype != void_list_node;
12270        argtype = TREE_CHAIN (argtype))
12271     ++arity;
12272
12273   if (current_class_type == NULL_TREE)
12274     friendp = 1;
12275
12276   if (DECL_CONV_FN_P (decl))
12277     operator_code = TYPE_EXPR;
12278   else
12279     do
12280       {
12281 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)       \
12282         if (ansi_opname (CODE) == name)                         \
12283           {                                                     \
12284             operator_code = (CODE);                             \
12285             break;                                              \
12286           }                                                     \
12287         else if (ansi_assopname (CODE) == name)                 \
12288           {                                                     \
12289             operator_code = (CODE);                             \
12290             DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;              \
12291             break;                                              \
12292           }
12293
12294 #include "operators.def"
12295 #undef DEF_OPERATOR
12296
12297         abort ();
12298       }
12299     while (0);
12300   my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12301   SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12302
12303   if (! friendp)
12304     {
12305       switch (operator_code)
12306         {
12307         case CALL_EXPR:
12308           TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12309           break;
12310
12311         case ARRAY_REF:
12312           TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12313           break;
12314
12315         case COMPONENT_REF:
12316         case MEMBER_REF:
12317           TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12318           break;
12319
12320         case NEW_EXPR:
12321           TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12322           break;
12323
12324         case DELETE_EXPR:
12325           TYPE_GETS_DELETE (current_class_type) |= 1;
12326           break;
12327
12328         case VEC_NEW_EXPR:
12329           TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12330           break;
12331
12332         case VEC_DELETE_EXPR:
12333           TYPE_GETS_DELETE (current_class_type) |= 2;
12334           break;
12335
12336         default:
12337           break;
12338         }
12339     }
12340
12341   if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12342     {
12343       /* When the compiler encounters the definition of A::operator new, it
12344          doesn't look at the class declaration to find out if it's static.  */
12345       if (methodp)
12346         revert_static_member_fn (decl);
12347
12348       TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12349     }
12350   else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12351     {
12352       if (methodp)
12353         revert_static_member_fn (decl);
12354
12355       TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12356     }
12357   else
12358     {
12359       /* An operator function must either be a non-static member function
12360          or have at least one parameter of a class, a reference to a class,
12361          an enumeration, or a reference to an enumeration.  13.4.0.6 */
12362       if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12363         {
12364           if (operator_code == TYPE_EXPR
12365               || operator_code == CALL_EXPR
12366               || operator_code == COMPONENT_REF
12367               || operator_code == ARRAY_REF
12368               || operator_code == NOP_EXPR)
12369             error ("`%D' must be a nonstatic member function", decl);
12370           else
12371             {
12372               tree p = argtypes;
12373
12374               if (DECL_STATIC_FUNCTION_P (decl))
12375                 error ("`%D' must be either a non-static member function or a non-member function", decl);
12376
12377               if (p)
12378                 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12379                   {
12380                     tree arg = TREE_VALUE (p);
12381                     if (TREE_CODE (arg) == REFERENCE_TYPE)
12382                       arg = TREE_TYPE (arg);
12383
12384                     /* This lets bad template code slip through.  */
12385                     if (IS_AGGR_TYPE (arg)
12386                         || TREE_CODE (arg) == ENUMERAL_TYPE
12387                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12388                         || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12389                       goto foundaggr;
12390                   }
12391               error
12392                 ("`%D' must have an argument of class or enumerated type",
12393                  decl);
12394             foundaggr:
12395               ;
12396             }
12397         }
12398
12399       if (operator_code == CALL_EXPR)
12400         return;                 /* No restrictions on args.  */
12401
12402       if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12403         {
12404           tree t = TREE_TYPE (name);
12405           if (! friendp)
12406             {
12407               int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12408               const char *what = 0;
12409
12410               if (ref)
12411                 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12412
12413               if (TREE_CODE (t) == VOID_TYPE)
12414                 what = "void";
12415               else if (t == current_class_type)
12416                 what = "the same type";
12417               /* Don't force t to be complete here.  */
12418               else if (IS_AGGR_TYPE (t)
12419                        && COMPLETE_TYPE_P (t)
12420                        && DERIVED_FROM_P (t, current_class_type))
12421                 what = "a base class";
12422
12423               if (what)
12424                 warning ("conversion to %s%s will never use a type conversion operator",
12425                          ref ? "a reference to " : "", what);
12426             }
12427         }
12428       if (operator_code == COND_EXPR)
12429         {
12430           /* 13.4.0.3 */
12431           error ("ISO C++ prohibits overloading operator ?:");
12432         }
12433       else if (ambi_op_p (operator_code))
12434         {
12435           if (arity == 1)
12436             /* We pick the one-argument operator codes by default, so
12437                we don't have to change anything.  */
12438             ;
12439           else if (arity == 2)
12440             {
12441               /* If we thought this was a unary operator, we now know
12442                  it to be a binary operator.  */
12443               switch (operator_code)
12444                 {
12445                 case INDIRECT_REF:
12446                   operator_code = MULT_EXPR;
12447                   break;
12448
12449                 case ADDR_EXPR:
12450                   operator_code = BIT_AND_EXPR;
12451                   break;
12452
12453                 case CONVERT_EXPR:
12454                   operator_code = PLUS_EXPR;
12455                   break;
12456
12457                 case NEGATE_EXPR:
12458                   operator_code = MINUS_EXPR;
12459                   break;
12460
12461                 case PREINCREMENT_EXPR:
12462                   operator_code = POSTINCREMENT_EXPR;
12463                   break;
12464
12465                 case PREDECREMENT_EXPR:
12466                   operator_code = POSTDECREMENT_EXPR;
12467                   break;
12468
12469                 default:
12470                   abort ();
12471                 }
12472
12473               SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12474
12475               if ((operator_code == POSTINCREMENT_EXPR
12476                    || operator_code == POSTDECREMENT_EXPR)
12477                   && ! processing_template_decl
12478                   && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12479                 {
12480                   if (methodp)
12481                     error ("postfix `%D' must take `int' as its argument",
12482                               decl);
12483                   else
12484                     error
12485                       ("postfix `%D' must take `int' as its second argument",
12486                        decl);
12487                 }
12488             }
12489           else
12490             {
12491               if (methodp)
12492                 error ("`%D' must take either zero or one argument", decl);
12493               else
12494                 error ("`%D' must take either one or two arguments", decl);
12495             }
12496
12497           /* More Effective C++ rule 6.  */
12498           if (warn_ecpp
12499               && (operator_code == POSTINCREMENT_EXPR
12500                   || operator_code == POSTDECREMENT_EXPR
12501                   || operator_code == PREINCREMENT_EXPR
12502                   || operator_code == PREDECREMENT_EXPR))
12503             {
12504               tree arg = TREE_VALUE (argtypes);
12505               tree ret = TREE_TYPE (TREE_TYPE (decl));
12506               if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12507                 arg = TREE_TYPE (arg);
12508               arg = TYPE_MAIN_VARIANT (arg);
12509               if (operator_code == PREINCREMENT_EXPR
12510                   || operator_code == PREDECREMENT_EXPR)
12511                 {
12512                   if (TREE_CODE (ret) != REFERENCE_TYPE
12513                       || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12514                                        arg))
12515                     warning ("prefix `%D' should return `%T'", decl,
12516                                 build_reference_type (arg));
12517                 }
12518               else
12519                 {
12520                   if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12521                     warning ("postfix `%D' should return `%T'", decl, arg);
12522                 }
12523             }
12524         }
12525       else if (unary_op_p (operator_code))
12526         {
12527           if (arity != 1)
12528             {
12529               if (methodp)
12530                 error ("`%D' must take `void'", decl);
12531               else
12532                 error ("`%D' must take exactly one argument", decl);
12533             }
12534         }
12535       else /* if (binary_op_p (operator_code)) */
12536         {
12537           if (arity != 2)
12538             {
12539               if (methodp)
12540                 error ("`%D' must take exactly one argument", decl);
12541               else
12542                 error ("`%D' must take exactly two arguments", decl);
12543             }
12544
12545           /* More Effective C++ rule 7.  */
12546           if (warn_ecpp
12547               && (operator_code == TRUTH_ANDIF_EXPR
12548                   || operator_code == TRUTH_ORIF_EXPR
12549                   || operator_code == COMPOUND_EXPR))
12550             warning ("user-defined `%D' always evaluates both arguments",
12551                         decl);
12552         }
12553
12554       /* Effective C++ rule 23.  */
12555       if (warn_ecpp
12556           && arity == 2
12557           && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12558           && (operator_code == PLUS_EXPR
12559               || operator_code == MINUS_EXPR
12560               || operator_code == TRUNC_DIV_EXPR
12561               || operator_code == MULT_EXPR
12562               || operator_code == TRUNC_MOD_EXPR)
12563           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12564         warning ("`%D' should return by value", decl);
12565
12566       /* [over.oper]/8 */
12567       for (; argtypes && argtypes != void_list_node;
12568           argtypes = TREE_CHAIN (argtypes))
12569         if (TREE_PURPOSE (argtypes))
12570           {
12571             TREE_PURPOSE (argtypes) = NULL_TREE;
12572             if (operator_code == POSTINCREMENT_EXPR
12573                 || operator_code == POSTDECREMENT_EXPR)
12574               {
12575                 if (pedantic)
12576                   pedwarn ("`%D' cannot have default arguments", decl);
12577               }
12578             else
12579               error ("`%D' cannot have default arguments", decl);
12580           }
12581
12582     }
12583 }
12584 \f
12585 static const char *
12586 tag_name (enum tag_types code)
12587 {
12588   switch (code)
12589     {
12590     case record_type:
12591       return "struct";
12592     case class_type:
12593       return "class";
12594     case union_type:
12595       return "union ";
12596     case enum_type:
12597       return "enum";
12598     default:
12599       abort ();
12600     }
12601 }
12602
12603 /* Get the struct, enum or union (CODE says which) with tag NAME.
12604    Define the tag as a forward-reference if it is not defined.
12605
12606    C++: If a class derivation is given, process it here, and report
12607    an error if multiple derivation declarations are not identical.
12608
12609    If this is a definition, come in through xref_tag and only look in
12610    the current frame for the name (since C++ allows new names in any
12611    scope.)  */
12612
12613 tree
12614 xref_tag (enum tag_types tag_code, tree name, tree attributes, 
12615           bool globalize)
12616 {
12617   enum tree_code code;
12618   register tree ref, t;
12619   struct cp_binding_level *b = current_binding_level;
12620   tree context = NULL_TREE;
12621
12622   switch (tag_code)
12623     {
12624     case record_type:
12625     case class_type:
12626       code = RECORD_TYPE;
12627       break;
12628     case union_type:
12629       code = UNION_TYPE;
12630       break;
12631     case enum_type:
12632       code = ENUMERAL_TYPE;
12633       break;
12634     default:
12635       abort ();
12636     }
12637
12638   /* If a cross reference is requested, look up the type
12639      already defined for this tag and return it.  */
12640   if (TYPE_P (name))
12641     {
12642       t = name;
12643       name = TYPE_IDENTIFIER (t);
12644     }
12645   else
12646     t = IDENTIFIER_TYPE_VALUE (name);
12647
12648   /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
12649   if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12650     {
12651       static int explained;
12652       tree shadowed;
12653
12654       warning ("`%s %T' declares a new type at namespace scope",
12655                   tag_name (tag_code), name);
12656       if (!explained++)
12657         warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12658                     tag_name (tag_code),
12659                     constructor_name (current_class_type),
12660                     TYPE_IDENTIFIER (t));
12661
12662       /* We need to remove the class scope binding for the
12663          TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
12664       for (shadowed = b->class_shadowed;
12665            shadowed;
12666            shadowed = TREE_CHAIN (shadowed))
12667         if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12668           {
12669             TREE_PURPOSE (shadowed) = NULL_TREE;
12670             break;
12671           }
12672     }
12673
12674   if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12675       && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12676     t = NULL_TREE;
12677
12678   if (! globalize)
12679     {
12680       /* If we know we are defining this tag, only look it up in
12681          this scope and don't try to find it as a type.  */
12682       ref = lookup_tag (code, name, b, 1);
12683     }
12684   else
12685     {
12686       if (t)
12687         {
12688           ref = follow_tag_typedef (t);
12689
12690           /* [dcl.type.elab] If the identifier resolves to a
12691              typedef-name or a template type-parameter, the
12692              elaborated-type-specifier is ill-formed.  */
12693           if (!ref)
12694             {
12695               pedwarn ("using typedef-name `%D' after `%s'",
12696                        TYPE_NAME (t), tag_name (tag_code));
12697               ref = t;
12698             }
12699           else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12700             error ("using template type parameter `%T' after `%s'",
12701                    t, tag_name (tag_code));
12702         }
12703       else
12704         ref = lookup_tag (code, name, b, 0);
12705
12706       if (! ref)
12707         {
12708           /* Try finding it as a type declaration.  If that wins,
12709              use it.  */
12710           ref = lookup_name (name, 1);
12711
12712           if (ref != NULL_TREE
12713               && processing_template_decl
12714               && DECL_CLASS_TEMPLATE_P (ref)
12715               && template_class_depth (current_class_type) == 0)
12716             /* Since GLOBALIZE is true, we're declaring a global
12717                template, so we want this type.  */
12718             ref = DECL_TEMPLATE_RESULT (ref);
12719
12720           if (ref && TREE_CODE (ref) == TYPE_DECL
12721               && TREE_CODE (TREE_TYPE (ref)) == code)
12722             ref = TREE_TYPE (ref);
12723           else
12724             ref = NULL_TREE;
12725         }
12726
12727       if (ref && current_class_type
12728           && template_class_depth (current_class_type)
12729           && PROCESSING_REAL_TEMPLATE_DECL_P ())
12730         {
12731           /* Since GLOBALIZE is nonzero, we are not looking at a
12732              definition of this tag.  Since, in addition, we are currently
12733              processing a (member) template declaration of a template
12734              class, we must be very careful; consider:
12735
12736                template <class X>
12737                struct S1
12738
12739                template <class U>
12740                struct S2
12741                { template <class V>
12742                friend struct S1; };
12743
12744              Here, the S2::S1 declaration should not be confused with the
12745              outer declaration.  In particular, the inner version should
12746              have a template parameter of level 2, not level 1.  This
12747              would be particularly important if the member declaration
12748              were instead:
12749
12750                template <class V = U> friend struct S1;
12751
12752              say, when we should tsubst into `U' when instantiating
12753              S2.  On the other hand, when presented with:
12754
12755                  template <class T>
12756                  struct S1 {
12757                    template <class U>
12758                    struct S2 {};
12759                    template <class U>
12760                    friend struct S2;
12761                  };
12762
12763               we must find the inner binding eventually.  We
12764               accomplish this by making sure that the new type we
12765               create to represent this declaration has the right
12766               TYPE_CONTEXT.  */
12767           context = TYPE_CONTEXT (ref);
12768           ref = NULL_TREE;
12769         }
12770     }
12771
12772   if (! ref)
12773     {
12774       /* If no such tag is yet defined, create a forward-reference node
12775          and record it as the "definition".
12776          When a real declaration of this type is found,
12777          the forward-reference will be altered into a real type.  */
12778       if (code == ENUMERAL_TYPE)
12779         {
12780           error ("use of enum `%#D' without previous declaration", name);
12781
12782           ref = make_node (ENUMERAL_TYPE);
12783
12784           /* Give the type a default layout like unsigned int
12785              to avoid crashing if it does not get defined.  */
12786           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12787           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12788           TYPE_USER_ALIGN (ref) = 0;
12789           TREE_UNSIGNED (ref) = 1;
12790           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12791           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12792           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12793
12794           /* Enable us to recognize when a type is created in class context.
12795              To do nested classes correctly, this should probably be cleared
12796              out when we leave this classes scope.  Currently this in only
12797              done in `start_enum'.  */
12798
12799           pushtag (name, ref, globalize);
12800         }
12801       else
12802         {
12803           struct cp_binding_level *old_b = class_binding_level;
12804
12805           ref = make_aggr_type (code);
12806           TYPE_CONTEXT (ref) = context;
12807
12808 #ifdef NONNESTED_CLASSES
12809           /* Class types don't nest the way enums do.  */
12810           class_binding_level = (struct cp_binding_level *)0;
12811 #endif
12812           pushtag (name, ref, globalize);
12813           class_binding_level = old_b;
12814         }
12815     }
12816   else
12817     {
12818       if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12819         redeclare_class_template (ref, current_template_parms);
12820     }
12821
12822   TYPE_ATTRIBUTES (ref) = attributes;
12823
12824   return ref;
12825 }
12826
12827 tree
12828 xref_tag_from_type (tree old, tree id, int globalize)
12829 {
12830   enum tag_types tag_kind;
12831
12832   if (TREE_CODE (old) == RECORD_TYPE)
12833     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12834   else
12835     tag_kind  = union_type;
12836
12837   if (id == NULL_TREE)
12838     id = TYPE_IDENTIFIER (old);
12839
12840   return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12841 }
12842
12843 /* REF is a type (named NAME), for which we have just seen some
12844    baseclasses.  BINFO is a list of those baseclasses; the
12845    TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12846    the base-class.  CODE_TYPE_NODE indicates whether REF is a class,
12847    struct, or union.  */
12848
12849 void
12850 xref_basetypes (tree ref, tree binfo)
12851 {
12852   /* In the declaration `A : X, Y, ... Z' we mark all the types
12853      (A, X, Y, ..., Z) so we can check for duplicates.  */
12854   tree binfos;
12855   tree *basep;
12856
12857   int i;
12858   enum tag_types tag_code;
12859
12860   if (TREE_CODE (ref) == UNION_TYPE)
12861     {
12862       error ("derived union `%T' invalid", ref);
12863       return;
12864     }
12865
12866   tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12867
12868   /* First, make sure that any templates in base-classes are
12869      instantiated.  This ensures that if we call ourselves recursively
12870      we do not get confused about which classes are marked and which
12871      are not.  */
12872   basep = &binfo; 
12873   while (*basep) 
12874     {
12875       tree basetype = TREE_VALUE (*basep);
12876       if (!(processing_template_decl && uses_template_parms (basetype))
12877           && !complete_type_or_else (basetype, NULL))
12878         /* An incomplete type.  Remove it form the list.  */
12879         *basep = TREE_CHAIN (*basep);
12880       else
12881         basep = &TREE_CHAIN (*basep);
12882     }
12883
12884   SET_CLASSTYPE_MARKED (ref);
12885   BINFO_BASETYPES (TYPE_BINFO (ref)) = binfos 
12886     = make_tree_vec (list_length (binfo));
12887
12888   for (i = 0; binfo; binfo = TREE_CHAIN (binfo))
12889     {
12890       /* The base of a derived struct is public by default.  */
12891       int via_public
12892         = (TREE_PURPOSE (binfo) == access_public_node
12893            || TREE_PURPOSE (binfo) == access_public_virtual_node
12894            || (tag_code != class_type
12895                && (TREE_PURPOSE (binfo) == access_default_node
12896                    || TREE_PURPOSE (binfo) == access_default_virtual_node)));
12897       int via_protected
12898         = (TREE_PURPOSE (binfo) == access_protected_node
12899            || TREE_PURPOSE (binfo) == access_protected_virtual_node);
12900       int via_virtual
12901         = (TREE_PURPOSE (binfo) == access_private_virtual_node
12902            || TREE_PURPOSE (binfo) == access_protected_virtual_node
12903            || TREE_PURPOSE (binfo) == access_public_virtual_node
12904            || TREE_PURPOSE (binfo) == access_default_virtual_node);
12905       tree basetype = TREE_VALUE (binfo);
12906       tree base_binfo;
12907
12908       if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12909         basetype = TREE_TYPE (basetype);
12910       if (!basetype
12911           || (TREE_CODE (basetype) != RECORD_TYPE
12912               && TREE_CODE (basetype) != TYPENAME_TYPE
12913               && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12914               && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12915         {
12916           error ("base type `%T' fails to be a struct or class type",
12917                     TREE_VALUE (binfo));
12918           continue;
12919         }
12920
12921       if (CLASSTYPE_MARKED (basetype))
12922         {
12923           if (basetype == ref)
12924             error ("recursive type `%T' undefined", basetype);
12925           else
12926             error ("duplicate base type `%T' invalid", basetype);
12927           continue;
12928         }
12929
12930       if (TYPE_FOR_JAVA (basetype)
12931           && (current_lang_depth () == 0))
12932         TYPE_FOR_JAVA (ref) = 1;
12933
12934       /* Note that the BINFO records which describe individual
12935          inheritances are *not* shared in the lattice!  They
12936          cannot be shared because a given baseclass may be
12937          inherited with different `accessibility' by different
12938          derived classes.  (Each BINFO record describing an
12939          individual inheritance contains flags which say what
12940          the `accessibility' of that particular inheritance is.)  */
12941
12942       base_binfo
12943         = make_binfo (size_zero_node, basetype,
12944                       CLASS_TYPE_P (basetype)
12945                       ? TYPE_BINFO_VTABLE (basetype) : NULL_TREE,
12946                       CLASS_TYPE_P (basetype)
12947                       ? TYPE_BINFO_VIRTUALS (basetype) : NULL_TREE);
12948
12949       TREE_VEC_ELT (binfos, i) = base_binfo;
12950       TREE_VIA_PUBLIC (base_binfo) = via_public;
12951       TREE_VIA_PROTECTED (base_binfo) = via_protected;
12952       TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12953       BINFO_INHERITANCE_CHAIN (base_binfo) = TYPE_BINFO (ref);
12954
12955       /* We need to unshare the binfos now so that lookups during class
12956          definition work.  */
12957       unshare_base_binfos (base_binfo);
12958
12959       SET_CLASSTYPE_MARKED (basetype);
12960
12961       /* We are free to modify these bits because they are meaningless
12962          at top level, and BASETYPE is a top-level type.  */
12963       if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12964         {
12965           TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12966           /* Converting to a virtual base class requires looking
12967              up the offset of the virtual base.  */
12968           TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12969         }
12970
12971       if (CLASS_TYPE_P (basetype))
12972         {
12973           TYPE_HAS_NEW_OPERATOR (ref)
12974             |= TYPE_HAS_NEW_OPERATOR (basetype);
12975           TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12976             |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12977           TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12978           /* If the base-class uses multiple inheritance, so do we.  */
12979           TYPE_USES_MULTIPLE_INHERITANCE (ref)
12980             |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12981           /* Likewise, if converting to a base of the base may require
12982              code, then we may need to generate code to convert to a
12983              base as well.  */
12984           TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12985             |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12986         }
12987
12988       i += 1;
12989     }
12990   if (i)
12991     TREE_VEC_LENGTH (binfos) = i;
12992   else
12993     BINFO_BASETYPES (TYPE_BINFO (ref)) = NULL_TREE;
12994
12995   if (i > 1)
12996     {
12997       TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12998       /* If there is more than one non-empty they cannot be at the same
12999          address.  */
13000       TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13001     }
13002
13003   /* Unmark all the types.  */
13004   while (--i >= 0)
13005     CLEAR_CLASSTYPE_MARKED (BINFO_TYPE (TREE_VEC_ELT (binfos, i)));
13006   CLEAR_CLASSTYPE_MARKED (ref);
13007
13008   /* Now that we know all the base-classes, set up the list of virtual
13009      bases.  */
13010   get_vbase_types (ref);
13011 }
13012
13013 \f
13014 /* Begin compiling the definition of an enumeration type.
13015    NAME is its name (or null if anonymous).
13016    Returns the type object, as yet incomplete.
13017    Also records info about it so that build_enumerator
13018    may be used to declare the individual values as they are read.  */
13019
13020 tree
13021 start_enum (tree name)
13022 {
13023   register tree enumtype = NULL_TREE;
13024   struct cp_binding_level *b = current_binding_level;
13025
13026   /* If this is the real definition for a previous forward reference,
13027      fill in the contents in the same object that used to be the
13028      forward reference.  */
13029
13030   if (name != NULL_TREE)
13031     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13032
13033   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13034     {
13035       error ("multiple definition of `%#T'", enumtype);
13036       cp_error_at ("previous definition here", enumtype);
13037       /* Clear out TYPE_VALUES, and start again.  */
13038       TYPE_VALUES (enumtype) = NULL_TREE;
13039     }
13040   else
13041     {
13042       enumtype = make_node (ENUMERAL_TYPE);
13043       pushtag (name, enumtype, 0);
13044     }
13045
13046   return enumtype;
13047 }
13048
13049 /* After processing and defining all the values of an enumeration type,
13050    install their decls in the enumeration type and finish it off.
13051    ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
13052
13053 void
13054 finish_enum (tree enumtype)
13055 {
13056   tree pair;
13057   tree minnode;
13058   tree maxnode;
13059   tree t;
13060   bool unsignedp;
13061   int lowprec;
13062   int highprec; 
13063   int precision;
13064
13065   /* We built up the VALUES in reverse order.  */
13066   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13067
13068   /* [dcl.enum]
13069
13070      Following the closing brace of an enum-specifier, each
13071      enumerator has the type of its enumeration.  Prior to the
13072      closing brace, the type of each enumerator is the type of
13073      its initializing value.  */
13074   for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13075     TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13076   
13077   /* For an enum defined in a template, all further processing is
13078      postponed until the template is instantiated.  */
13079   if (processing_template_decl)
13080     {
13081       if (at_function_scope_p ())
13082         add_stmt (build_min (TAG_DEFN, enumtype));
13083       return;
13084     }
13085
13086   /* Figure out what the minimum and maximum values of the enumerators
13087      are.  */
13088   if (TYPE_VALUES (enumtype))
13089     {
13090       minnode = maxnode = NULL_TREE;
13091
13092       for (pair = TYPE_VALUES (enumtype);
13093            pair;
13094            pair = TREE_CHAIN (pair))
13095         {
13096           tree value;
13097
13098           value = DECL_INITIAL (TREE_VALUE (pair));
13099
13100           if (!minnode)
13101             minnode = maxnode = value;
13102           else if (tree_int_cst_lt (maxnode, value))
13103             maxnode = value;
13104           else if (tree_int_cst_lt (value, minnode))
13105             minnode = value;
13106         }
13107     }
13108   else
13109     minnode = maxnode = integer_zero_node;
13110
13111   /* Compute the number of bits require to represent all values of the
13112      enumeration.  We must do this before the type of MINNODE and
13113      MAXNODE are transformed, since min_precision relies on the
13114      TREE_TYPE of the value it is passed.  */
13115   unsignedp = tree_int_cst_sgn (minnode) >= 0;
13116   lowprec = min_precision (minnode, unsignedp);
13117   highprec = min_precision (maxnode, unsignedp);
13118   precision = MAX (lowprec, highprec);
13119
13120   /* Set the TREE_TYPE for the values as well.  That's so that when we
13121      call decl_constant_value we get an entity of the right type (but
13122      with the constant value).  In addition, transform the TYPE_VALUES
13123      list to contain the values, rather than the CONST_DECLs for them.  */
13124   for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13125     {
13126       tree value = DECL_INITIAL (TREE_VALUE (pair));
13127
13128       TREE_TYPE (value) = enumtype;
13129       TREE_VALUE (pair) = value;
13130     }
13131
13132   /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
13133   TYPE_SIZE (enumtype) = NULL_TREE;
13134   TYPE_PRECISION (enumtype) = precision;
13135   if (unsignedp)
13136     fixup_unsigned_type (enumtype);
13137   else
13138     fixup_signed_type (enumtype);
13139
13140   if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13141     /* Use the width of the narrowest normal C type which is wide
13142        enough.  */
13143     TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13144                                                 (precision, 1));
13145   else
13146     TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13147
13148   TYPE_SIZE (enumtype) = NULL_TREE;
13149   layout_type (enumtype);
13150
13151   /* Fix up all variant types of this enum type.  */
13152   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13153     {
13154       TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13155       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13156       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13157       TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13158       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13159       TYPE_MODE (t) = TYPE_MODE (enumtype);
13160       TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13161       TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13162       TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13163       TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13164     }
13165
13166   /* Finish debugging output for this type.  */
13167   rest_of_type_compilation (enumtype, namespace_bindings_p ());
13168 }
13169
13170 /* Build and install a CONST_DECL for an enumeration constant of the
13171    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13172    Assignment of sequential values by default is handled here.  */
13173
13174 void
13175 build_enumerator (tree name, tree value, tree enumtype)
13176 {
13177   tree decl;
13178   tree context;
13179   tree type;
13180   tree values;
13181
13182   /* Remove no-op casts from the value.  */
13183   if (value)
13184     STRIP_TYPE_NOPS (value);
13185
13186   if (! processing_template_decl)
13187     {
13188       /* Validate and default VALUE.  */
13189       if (value != NULL_TREE)
13190         {
13191           value = decl_constant_value (value);
13192
13193           if (TREE_CODE (value) == INTEGER_CST)
13194             {
13195               value = default_conversion (value);
13196               constant_expression_warning (value);
13197             }
13198           else
13199             {
13200               error ("enumerator value for `%D' not integer constant", name);
13201               value = NULL_TREE;
13202             }
13203         }
13204
13205       /* Default based on previous value.  */
13206       if (value == NULL_TREE && ! processing_template_decl)
13207         {
13208           tree prev_value;
13209
13210           if (TYPE_VALUES (enumtype))
13211             {
13212               /* The next value is the previous value ...  */
13213               prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13214               /* ... plus one.  */
13215               value = cp_build_binary_op (PLUS_EXPR,
13216                                           prev_value,
13217                                           integer_one_node);
13218
13219               if (tree_int_cst_lt (value, prev_value))
13220                 error ("overflow in enumeration values at `%D'", name);
13221             }
13222           else
13223             value = integer_zero_node;
13224         }
13225
13226       /* Remove no-op casts from the value.  */
13227       if (value)
13228         STRIP_TYPE_NOPS (value);
13229 #if 0
13230       /* To fix MAX_VAL enum consts. (bkoz)  */
13231       TREE_TYPE (value) = integer_type_node;
13232 #endif
13233     }
13234
13235   /* We always have to copy here; not all INTEGER_CSTs are unshared.
13236      Even in other cases, we will later (in finish_enum) be setting
13237      the type of VALUE.  But, we don't need to make a copy if this
13238      VALUE is one of the enumeration constants for this same
13239      enumeration type.  */
13240   for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
13241     if (TREE_VALUE (values) == value)
13242       break;
13243   /* If we didn't break out of the loop, then we do need a copy.  */
13244   if (!values && value)
13245     value = copy_node (value);
13246
13247   /* C++ associates enums with global, function, or class declarations.  */
13248   context = current_scope ();
13249
13250   /* Build the actual enumeration constant.  Note that the enumeration
13251     constants have the type of their initializers until the
13252     enumeration is complete:
13253
13254       [ dcl.enum ]
13255
13256       Following the closing brace of an enum-specifier, each enumer-
13257       ator has the type of its enumeration.  Prior to the closing
13258       brace, the type of each enumerator is the type of its
13259       initializing value.
13260
13261     In finish_enum we will reset the type.  Of course, if we're
13262     processing a template, there may be no value.  */
13263   type = value ? TREE_TYPE (value) : NULL_TREE;
13264
13265   if (context && context == current_class_type)
13266     /* This enum declaration is local to the class.  We need the full
13267        lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
13268     decl = build_lang_decl (CONST_DECL, name, type);
13269   else
13270     /* It's a global enum, or it's local to a function.  (Note local to
13271       a function could mean local to a class method.  */
13272     decl = build_decl (CONST_DECL, name, type);
13273
13274   DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13275   DECL_INITIAL (decl) = value;
13276   TREE_READONLY (decl) = 1;
13277
13278   if (context && context == current_class_type)
13279     /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13280       on the TYPE_FIELDS list for `S'.  (That's so that you can say
13281       things like `S::i' later.)  */
13282     finish_member_declaration (decl);
13283   else
13284     pushdecl (decl);
13285
13286   /* Add this enumeration constant to the list for this type.  */
13287   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13288 }
13289
13290 \f
13291 /* We're defining DECL.  Make sure that it's type is OK.  */
13292
13293 static void
13294 check_function_type (tree decl, tree current_function_parms)
13295 {
13296   tree fntype = TREE_TYPE (decl);
13297   tree return_type = complete_type (TREE_TYPE (fntype));
13298
13299   /* In a function definition, arg types must be complete.  */
13300   require_complete_types_for_parms (current_function_parms);
13301
13302   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13303     {
13304       error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13305
13306       /* Make it return void instead, but don't change the
13307          type of the DECL_RESULT, in case we have a named return value.  */
13308       if (TREE_CODE (fntype) == METHOD_TYPE)
13309         {
13310           tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13311           TREE_TYPE (decl)
13312             = build_cplus_method_type (ctype,
13313                                        void_type_node,
13314                                        FUNCTION_ARG_CHAIN (decl));
13315         }
13316       else
13317         TREE_TYPE (decl)
13318           = build_function_type (void_type_node,
13319                                  TYPE_ARG_TYPES (TREE_TYPE (decl)));
13320       TREE_TYPE (decl)
13321         = build_exception_variant (fntype,
13322                                    TYPE_RAISES_EXCEPTIONS (fntype));
13323     }
13324   else
13325     abstract_virtuals_error (decl, TREE_TYPE (fntype));
13326 }
13327
13328 /* Create the FUNCTION_DECL for a function definition.
13329    DECLSPECS and DECLARATOR are the parts of the declaration;
13330    they describe the function's name and the type it returns,
13331    but twisted together in a fashion that parallels the syntax of C.
13332
13333    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13334    DECLARATOR is really the DECL for the function we are about to
13335    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13336    indicating that the function is an inline defined in-class.
13337
13338    This function creates a binding context for the function body
13339    as well as setting up the FUNCTION_DECL in current_function_decl.
13340
13341    Returns 1 on success.  If the DECLARATOR is not suitable for a function
13342    (it defines a datum instead), we return 0, which tells
13343    yyparse to report a parse error.
13344
13345    For C++, we must first check whether that datum makes any sense.
13346    For example, "class A local_a(1,2);" means that variable local_a
13347    is an aggregate of type A, which should have a constructor
13348    applied to it with the argument list [1, 2].  */
13349
13350 int
13351 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13352 {
13353   tree decl1;
13354   tree ctype = NULL_TREE;
13355   tree fntype;
13356   tree restype;
13357   int doing_friend = 0;
13358   struct cp_binding_level *bl;
13359   tree current_function_parms;
13360
13361   /* Sanity check.  */
13362   my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13363   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13364
13365   /* This should only be done once on the top most decl.  */
13366   if (have_extern_spec)
13367     {
13368       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13369       have_extern_spec = false;
13370     }
13371
13372   if (flags & SF_PRE_PARSED)
13373     {
13374       decl1 = declarator;
13375
13376       fntype = TREE_TYPE (decl1);
13377       if (TREE_CODE (fntype) == METHOD_TYPE)
13378         ctype = TYPE_METHOD_BASETYPE (fntype);
13379
13380       /* ISO C++ 11.4/5.  A friend function defined in a class is in
13381          the (lexical) scope of the class in which it is defined.  */
13382       if (!ctype && DECL_FRIEND_P (decl1))
13383         {
13384           ctype = DECL_FRIEND_CONTEXT (decl1);
13385
13386           /* CTYPE could be null here if we're dealing with a template;
13387              for example, `inline friend float foo()' inside a template
13388              will have no CTYPE set.  */
13389           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13390             ctype = NULL_TREE;
13391           else
13392             doing_friend = 1;
13393         }
13394
13395       last_function_parms = DECL_ARGUMENTS (decl1);
13396     }
13397   else
13398     {
13399       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13400       /* If the declarator is not suitable for a function definition,
13401          cause a syntax error.  */
13402       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13403         return 0;
13404
13405       cplus_decl_attributes (&decl1, attrs, 0);
13406
13407       /* If #pragma weak was used, mark the decl weak now.  */
13408       if (current_binding_level == global_binding_level)
13409         maybe_apply_pragma_weak (decl1);
13410
13411       fntype = TREE_TYPE (decl1);
13412
13413       restype = TREE_TYPE (fntype);
13414       if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13415         {
13416           error ("semicolon missing after declaration of `%#T'", restype);
13417           shadow_tag (build_tree_list (NULL_TREE, restype));
13418           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13419           if (TREE_CODE (fntype) == FUNCTION_TYPE)
13420             fntype = build_function_type (integer_type_node,
13421                                           TYPE_ARG_TYPES (fntype));
13422           else
13423             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13424                                               integer_type_node,
13425                                               TYPE_ARG_TYPES (fntype));
13426           TREE_TYPE (decl1) = fntype;
13427         }
13428
13429       if (TREE_CODE (fntype) == METHOD_TYPE)
13430         ctype = TYPE_METHOD_BASETYPE (fntype);
13431       else if (DECL_MAIN_P (decl1))
13432         {
13433           /* If this doesn't return integer_type, complain.  */
13434           if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13435             {
13436               if (pedantic || warn_return_type)
13437                 pedwarn ("return type for `main' changed to `int'");
13438               TREE_TYPE (decl1) = fntype = default_function_type;
13439             }
13440         }
13441     }
13442
13443   if (DECL_DECLARED_INLINE_P (decl1)
13444       && lookup_attribute ("noinline", attrs))
13445     warning_with_decl (decl1,
13446                        "inline function `%s' given attribute noinline");
13447
13448   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13449     /* This is a constructor, we must ensure that any default args
13450        introduced by this definition are propagated to the clones
13451        now. The clones are used directly in overload resolution.  */
13452     adjust_clone_args (decl1);
13453
13454   /* Sometimes we don't notice that a function is a static member, and
13455      build a METHOD_TYPE for it.  Fix that up now.  */
13456   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13457       && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13458     {
13459       revert_static_member_fn (decl1);
13460       last_function_parms = TREE_CHAIN (last_function_parms);
13461       ctype = NULL_TREE;
13462     }
13463
13464   /* Warn if function was previously implicitly declared
13465      (but not if we warned then).  */
13466   if (! warn_implicit
13467       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13468     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13469
13470   /* Set up current_class_type, and enter the scope of the class, if
13471      appropriate.  */
13472   if (ctype)
13473     push_nested_class (ctype, 1);
13474   else if (DECL_STATIC_FUNCTION_P (decl1))
13475     push_nested_class (DECL_CONTEXT (decl1), 2);
13476
13477   /* Now that we have entered the scope of the class, we must restore
13478      the bindings for any template parameters surrounding DECL1, if it
13479      is an inline member template.  (Order is important; consider the
13480      case where a template parameter has the same name as a field of
13481      the class.)  It is not until after this point that
13482      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
13483   if (flags & SF_INCLASS_INLINE)
13484     maybe_begin_member_template_processing (decl1);
13485
13486   /* Effective C++ rule 15.  */
13487   if (warn_ecpp
13488       && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13489       && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13490     warning ("`operator=' should return a reference to `*this'");
13491
13492   /* Make the init_value nonzero so pushdecl knows this is not tentative.
13493      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
13494   if (!DECL_INITIAL (decl1))
13495     DECL_INITIAL (decl1) = error_mark_node;
13496
13497   /* This function exists in static storage.
13498      (This does not mean `static' in the C sense!)  */
13499   TREE_STATIC (decl1) = 1;
13500
13501   /* We must call push_template_decl after current_class_type is set
13502      up.  (If we are processing inline definitions after exiting a
13503      class scope, current_class_type will be NULL_TREE until set above
13504      by push_nested_class.)  */
13505   if (processing_template_decl)
13506     decl1 = push_template_decl (decl1);
13507
13508   /* We are now in the scope of the function being defined.  */
13509   current_function_decl = decl1;
13510
13511   /* Save the parm names or decls from this function's declarator
13512      where store_parm_decls will find them.  */
13513   current_function_parms = last_function_parms;
13514
13515   /* Make sure the parameter and return types are reasonable.  When
13516      you declare a function, these types can be incomplete, but they
13517      must be complete when you define the function.  */
13518   if (! processing_template_decl)
13519     check_function_type (decl1, current_function_parms);
13520
13521   /* Build the return declaration for the function.  */
13522   restype = TREE_TYPE (fntype);
13523   /* Promote the value to int before returning it.  */
13524   if (c_promoting_integer_type_p (restype))
13525     restype = type_promotes_to (restype);
13526   if (DECL_RESULT (decl1) == NULL_TREE)
13527     {
13528       DECL_RESULT (decl1)
13529         = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13530       c_apply_type_quals_to_decl (cp_type_quals (restype),
13531                                   DECL_RESULT (decl1));
13532     }
13533
13534   /* Initialize RTL machinery.  We cannot do this until
13535      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
13536      even when processing a template; this is how we get
13537      CFUN set up, and our per-function variables initialized.
13538      FIXME factor out the non-RTL stuff.  */
13539   bl = current_binding_level;
13540   init_function_start (decl1, input_filename, lineno);
13541   current_binding_level = bl;
13542
13543   /* Even though we're inside a function body, we still don't want to
13544      call expand_expr to calculate the size of a variable-sized array.
13545      We haven't necessarily assigned RTL to all variables yet, so it's
13546      not safe to try to expand expressions involving them.  */
13547   immediate_size_expand = 0;
13548   cfun->x_dont_save_pending_sizes_p = 1;
13549
13550   /* Start the statement-tree, start the tree now.  */
13551   begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13552
13553   /* Let the user know we're compiling this function.  */
13554   announce_function (decl1);
13555
13556   /* Record the decl so that the function name is defined.
13557      If we already have a decl for this name, and it is a FUNCTION_DECL,
13558      use the old decl.  */
13559   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13560     {
13561       /* A specialization is not used to guide overload resolution.  */
13562       if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13563           && ! DECL_FUNCTION_MEMBER_P (decl1))
13564         decl1 = pushdecl (decl1);
13565       else
13566         {
13567           /* We need to set the DECL_CONTEXT.  */
13568           if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13569             DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13570           /* And make sure we have enough default args.  */
13571           check_default_args (decl1);
13572         }
13573       fntype = TREE_TYPE (decl1);
13574     }
13575
13576   /* Reset these in case the call to pushdecl changed them.  */
13577   current_function_decl = decl1;
13578   cfun->decl = decl1;
13579
13580   /* If we are (erroneously) defining a function that we have already
13581      defined before, wipe out what we knew before.  */
13582   if (!DECL_PENDING_INLINE_P (decl1))
13583     DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13584
13585   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13586     {
13587       /* We know that this was set up by `grokclassfn'.  We do not
13588          wait until `store_parm_decls', since evil parse errors may
13589          never get us to that point.  Here we keep the consistency
13590          between `current_class_type' and `current_class_ptr'.  */
13591       tree t = DECL_ARGUMENTS (decl1);
13592
13593       my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13594                           162);
13595       my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13596                           19990811);
13597
13598       cp_function_chain->x_current_class_ref
13599         = build_indirect_ref (t, NULL);
13600       cp_function_chain->x_current_class_ptr = t;
13601
13602       /* Constructors and destructors need to know whether they're "in
13603          charge" of initializing virtual base classes.  */
13604       t = TREE_CHAIN (t);
13605       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13606         {
13607           current_in_charge_parm = t;
13608           t = TREE_CHAIN (t);
13609         }
13610       if (DECL_HAS_VTT_PARM_P (decl1))
13611         {
13612           if (DECL_NAME (t) != vtt_parm_identifier)
13613             abort ();
13614           current_vtt_parm = t;
13615         }
13616     }
13617
13618   if (DECL_INTERFACE_KNOWN (decl1))
13619     {
13620       tree ctx = decl_function_context (decl1);
13621
13622       if (DECL_NOT_REALLY_EXTERN (decl1))
13623         DECL_EXTERNAL (decl1) = 0;
13624
13625       if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13626           && TREE_PUBLIC (ctx))
13627         /* This is a function in a local class in an extern inline
13628            function.  */
13629         comdat_linkage (decl1);
13630     }
13631   /* If this function belongs to an interface, it is public.
13632      If it belongs to someone else's interface, it is also external.
13633      This only affects inlines and template instantiations.  */
13634   else if (interface_unknown == 0
13635            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13636                || flag_alt_external_templates))
13637     {
13638       if (DECL_DECLARED_INLINE_P (decl1) 
13639           || DECL_TEMPLATE_INSTANTIATION (decl1)
13640           || processing_template_decl)
13641         {
13642           DECL_EXTERNAL (decl1)
13643             = (interface_only
13644                || (DECL_DECLARED_INLINE_P (decl1) 
13645                    && ! flag_implement_inlines
13646                    && !DECL_VINDEX (decl1)));
13647
13648           /* For WIN32 we also want to put these in linkonce sections.  */
13649           maybe_make_one_only (decl1);
13650         }
13651       else
13652         DECL_EXTERNAL (decl1) = 0;
13653       DECL_NOT_REALLY_EXTERN (decl1) = 0;
13654       DECL_INTERFACE_KNOWN (decl1) = 1;
13655     }
13656   else if (interface_unknown && interface_only
13657            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13658                || flag_alt_external_templates))
13659     {
13660       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13661          interface, we will have interface_only set but not
13662          interface_known.  In that case, we don't want to use the normal
13663          heuristics because someone will supply a #pragma implementation
13664          elsewhere, and deducing it here would produce a conflict.  */
13665       comdat_linkage (decl1);
13666       DECL_EXTERNAL (decl1) = 0;
13667       DECL_INTERFACE_KNOWN (decl1) = 1;
13668       DECL_DEFER_OUTPUT (decl1) = 1;
13669     }
13670   else
13671     {
13672       /* This is a definition, not a reference.
13673          So clear DECL_EXTERNAL.  */
13674       DECL_EXTERNAL (decl1) = 0;
13675
13676       if ((DECL_DECLARED_INLINE_P (decl1) 
13677            || DECL_TEMPLATE_INSTANTIATION (decl1))
13678           && ! DECL_INTERFACE_KNOWN (decl1)
13679           /* Don't try to defer nested functions for now.  */
13680           && ! decl_function_context (decl1))
13681         DECL_DEFER_OUTPUT (decl1) = 1;
13682       else
13683         DECL_INTERFACE_KNOWN (decl1) = 1;
13684     }
13685
13686   pushlevel (0);
13687   current_binding_level->parm_flag = 1;
13688
13689   ++function_depth;
13690
13691   if (DECL_DESTRUCTOR_P (decl1))
13692     {
13693       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13694       DECL_CONTEXT (dtor_label) = current_function_decl;
13695     }
13696
13697   start_fname_decls ();
13698   
13699   store_parm_decls (current_function_parms);
13700
13701   return 1;
13702 }
13703 \f
13704 /* Store the parameter declarations into the current function declaration.
13705    This is called after parsing the parameter declarations, before
13706    digesting the body of the function.
13707
13708    Also install to binding contour return value identifier, if any.  */
13709
13710 static void
13711 store_parm_decls (tree current_function_parms)
13712 {
13713   register tree fndecl = current_function_decl;
13714   register tree parm;
13715
13716   /* This is a chain of any other decls that came in among the parm
13717      declarations.  If a parm is declared with  enum {foo, bar} x;
13718      then CONST_DECLs for foo and bar are put here.  */
13719   tree nonparms = NULL_TREE;
13720
13721   if (current_function_parms)
13722     {
13723       /* This case is when the function was defined with an ANSI prototype.
13724          The parms already have decls, so we need not do anything here
13725          except record them as in effect
13726          and complain if any redundant old-style parm decls were written.  */
13727
13728       tree specparms = current_function_parms;
13729       tree next;
13730
13731       /* Must clear this because it might contain TYPE_DECLs declared
13732              at class level.  */
13733       storedecls (NULL_TREE);
13734
13735       /* If we're doing semantic analysis, then we'll call pushdecl
13736              for each of these.  We must do them in reverse order so that
13737              they end in the correct forward order.  */
13738       specparms = nreverse (specparms);
13739
13740       for (parm = specparms; parm; parm = next)
13741         {
13742           next = TREE_CHAIN (parm);
13743           if (TREE_CODE (parm) == PARM_DECL)
13744             {
13745               if (DECL_NAME (parm) == NULL_TREE
13746                   || TREE_CODE (parm) != VOID_TYPE)
13747                 pushdecl (parm);
13748               else
13749                 error ("parameter `%D' declared void", parm);
13750             }
13751           else
13752             {
13753               /* If we find an enum constant or a type tag,
13754                  put it aside for the moment.  */
13755               TREE_CHAIN (parm) = NULL_TREE;
13756               nonparms = chainon (nonparms, parm);
13757             }
13758         }
13759
13760       /* Get the decls in their original chain order and record in the
13761          function.  This is all and only the PARM_DECLs that were
13762          pushed into scope by the loop above.  */
13763       DECL_ARGUMENTS (fndecl) = getdecls ();
13764       storetags (gettags ());
13765     }
13766   else
13767     DECL_ARGUMENTS (fndecl) = NULL_TREE;
13768
13769   /* Now store the final chain of decls for the arguments
13770      as the decl-chain of the current lexical scope.
13771      Put the enumerators in as well, at the front so that
13772      DECL_ARGUMENTS is not modified.  */
13773   storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13774
13775   /* Do the starting of the exception specifications, if we have any.  */
13776   if (flag_exceptions && !processing_template_decl
13777       && flag_enforce_eh_specs
13778       && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13779     current_eh_spec_block = begin_eh_spec_block ();
13780 }
13781
13782 \f
13783 /* We have finished doing semantic analysis on DECL, but have not yet
13784    generated RTL for its body.  Save away our current state, so that
13785    when we want to generate RTL later we know what to do.  */
13786
13787 static void
13788 save_function_data (tree decl)
13789 {
13790   struct language_function *f;
13791
13792   /* Save the language-specific per-function data so that we can
13793      get it back when we really expand this function.  */
13794   my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13795                       19990908);
13796
13797   /* Make a copy.  */
13798   f = ((struct language_function *)
13799        ggc_alloc (sizeof (struct language_function)));
13800   memcpy (f, cp_function_chain, sizeof (struct language_function));
13801   DECL_SAVED_FUNCTION_DATA (decl) = f;
13802
13803   /* Clear out the bits we don't need.  */
13804   f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13805   f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13806   f->x_named_label_uses = NULL;
13807   f->bindings = NULL;
13808   f->x_local_names = NULL;
13809
13810   /* When we get back here again, we will be expanding.  */
13811   f->x_expanding_p = 1;
13812
13813   /* If we've already decided that we cannot inline this function, we
13814      must remember that fact when we actually go to expand the
13815      function.  */
13816   if (current_function_cannot_inline)
13817     {
13818       f->cannot_inline = current_function_cannot_inline;
13819       DECL_INLINE (decl) = 0;
13820     }
13821 }
13822
13823 /* Add a note to mark the beginning of the main body of the constructor.
13824    This is used to set up the data structures for the cleanup regions for
13825    fully-constructed bases and members.  */
13826
13827 static void
13828 begin_constructor_body (void)
13829 {
13830 }
13831
13832 /* Add a note to mark the end of the main body of the constructor.  This is
13833    used to end the cleanup regions for fully-constructed bases and
13834    members.  */
13835
13836 static void
13837 finish_constructor_body (void)
13838 {
13839 }
13840
13841 /* Do all the processing for the beginning of a destructor; set up the
13842    vtable pointers and cleanups for bases and members.  */
13843
13844 static void
13845 begin_destructor_body (void)
13846 {
13847   tree if_stmt;
13848   tree compound_stmt;
13849
13850   /* If the dtor is empty, and we know there is not any possible
13851      way we could use any vtable entries, before they are possibly
13852      set by a base class dtor, we don't have to setup the vtables,
13853      as we know that any base class dtor will set up any vtables
13854      it needs.  We avoid MI, because one base class dtor can do a
13855      virtual dispatch to an overridden function that would need to
13856      have a non-related vtable set up, we cannot avoid setting up
13857      vtables in that case.  We could change this to see if there
13858      is just one vtable.
13859
13860      ??? In the destructor for a class, the vtables are set
13861      appropriately for that class.  There will be no non-related
13862      vtables.  jason 2001-12-11.  */
13863   if_stmt = begin_if_stmt ();
13864
13865   /* If it is not safe to avoid setting up the vtables, then
13866      someone will change the condition to be boolean_true_node.  
13867      (Actually, for now, we do not have code to set the condition
13868      appropriately, so we just assume that we always need to
13869      initialize the vtables.)  */
13870   finish_if_stmt_cond (boolean_true_node, if_stmt);
13871
13872   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13873
13874   /* Make all virtual function table pointers in non-virtual base
13875      classes point to CURRENT_CLASS_TYPE's virtual function
13876      tables.  */
13877   initialize_vtbl_ptrs (current_class_ptr);
13878
13879   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13880   finish_then_clause (if_stmt);
13881   finish_if_stmt ();
13882
13883   /* And insert cleanups for our bases and members so that they
13884      will be properly destroyed if we throw.  */
13885   push_base_cleanups ();
13886 }
13887
13888 /* At the end of every destructor we generate code to delete the object if
13889    necessary.  Do that now.  */
13890
13891 static void
13892 finish_destructor_body (void)
13893 {
13894   tree exprstmt;
13895
13896   /* Any return from a destructor will end up here; that way all base
13897      and member cleanups will be run when the function returns.  */
13898   add_stmt (build_stmt (LABEL_STMT, dtor_label));
13899
13900   /* In a virtual destructor, we must call delete.  */
13901   if (DECL_VIRTUAL_P (current_function_decl))
13902     {
13903       tree if_stmt;
13904       tree virtual_size = cxx_sizeof (current_class_type);
13905
13906       /* [class.dtor]
13907
13908       At the point of definition of a virtual destructor (including
13909       an implicit definition), non-placement operator delete shall
13910       be looked up in the scope of the destructor's class and if
13911       found shall be accessible and unambiguous.  */
13912       exprstmt = build_op_delete_call
13913         (DELETE_EXPR, current_class_ptr, virtual_size,
13914          LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13915
13916       if_stmt = begin_if_stmt ();
13917       finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13918                                   current_in_charge_parm,
13919                                   integer_one_node),
13920                            if_stmt);
13921       finish_expr_stmt (exprstmt);
13922       finish_then_clause (if_stmt);
13923       finish_if_stmt ();
13924     }
13925 }
13926
13927 /* Do the necessary processing for the beginning of a function body, which
13928    in this case includes member-initializers, but not the catch clauses of
13929    a function-try-block.  Currently, this means opening a binding level
13930    for the member-initializers (in a ctor) and member cleanups (in a dtor).
13931    In other functions, this isn't necessary, but it doesn't hurt.  */
13932
13933 tree
13934 begin_function_body (void)
13935 {
13936   tree stmt;
13937
13938   if (processing_template_decl)
13939     /* Do nothing now.  */;
13940   else
13941     /* Always keep the BLOCK node associated with the outermost pair of
13942        curly braces of a function.  These are needed for correct
13943        operation of dwarfout.c.  */
13944     keep_next_level (1);
13945
13946   stmt = begin_compound_stmt (0);
13947   COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13948
13949   if (processing_template_decl)
13950     /* Do nothing now.  */;
13951   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13952     begin_constructor_body ();
13953   else if (DECL_DESTRUCTOR_P (current_function_decl))
13954     begin_destructor_body ();
13955
13956   return stmt;
13957 }
13958
13959 /* Do the processing for the end of a function body.  Currently, this means
13960    closing out the cleanups for fully-constructed bases and members, and in
13961    the case of the destructor, deleting the object if desired.  Again, this
13962    is only meaningful for [cd]tors, since they are the only functions where
13963    there is a significant distinction between the main body and any
13964    function catch clauses.  Handling, say, main() return semantics here
13965    would be wrong, as flowing off the end of a function catch clause for
13966    main() would also need to return 0.  */
13967
13968 void
13969 finish_function_body (tree compstmt)
13970 {
13971   /* Close the block.  */
13972   finish_compound_stmt (0, compstmt);
13973
13974   if (processing_template_decl)
13975     /* Do nothing now.  */;
13976   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13977     finish_constructor_body ();
13978   else if (DECL_DESTRUCTOR_P (current_function_decl))
13979     finish_destructor_body ();
13980 }  
13981
13982 /* Finish up a function declaration and compile that function
13983    all the way to assembler language output.  The free the storage
13984    for the function definition.
13985
13986    FLAGS is a bitwise or of the following values:
13987      2 - INCLASS_INLINE
13988        We just finished processing the body of an in-class inline
13989        function definition.  (This processing will have taken place
13990        after the class definition is complete.)  */
13991
13992 tree
13993 finish_function (int flags)
13994 {
13995   register tree fndecl = current_function_decl;
13996   tree fntype, ctype = NULL_TREE;
13997   int inclass_inline = (flags & 2) != 0;
13998   int nested;
13999
14000   /* When we get some parse errors, we can end up without a
14001      current_function_decl, so cope.  */
14002   if (fndecl == NULL_TREE)
14003     return error_mark_node;
14004
14005   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14006       && DECL_VIRTUAL_P (fndecl)
14007       && !processing_template_decl)
14008     {
14009       tree fnclass = DECL_CONTEXT (fndecl);
14010       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14011         keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14012     }
14013
14014   nested = function_depth > 1;
14015   fntype = TREE_TYPE (fndecl);
14016
14017   /*  TREE_READONLY (fndecl) = 1;
14018       This caused &foo to be of type ptr-to-const-function
14019       which then got a warning when stored in a ptr-to-function variable.  */
14020
14021   my_friendly_assert (building_stmt_tree (), 20000911);
14022
14023   finish_fname_decls ();
14024   
14025   /* For a cloned function, we've already got all the code we need;
14026      there's no need to add any extra bits.  */
14027   if (!DECL_CLONED_FUNCTION_P (fndecl))
14028     {
14029       if (DECL_MAIN_P (current_function_decl))
14030         {
14031           /* Make it so that `main' always returns 0 by default.  */
14032 #if VMS_TARGET
14033           finish_return_stmt (integer_one_node);
14034 #else
14035           finish_return_stmt (integer_zero_node);
14036 #endif
14037         }
14038
14039       /* Finish dealing with exception specifiers.  */
14040       if (flag_exceptions && !processing_template_decl
14041           && flag_enforce_eh_specs
14042           && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14043         finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14044                               (TREE_TYPE (current_function_decl)),
14045                               current_eh_spec_block);
14046     }
14047
14048   /* If we're saving up tree structure, tie off the function now.  */
14049   finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14050
14051   /* This must come after expand_function_end because cleanups might
14052      have declarations (from inline functions) that need to go into
14053      this function's blocks.  */
14054   
14055   /* If the current binding level isn't the outermost binding level
14056      for this function, either there is a bug, or we have experienced
14057      syntax errors and the statement tree is malformed.  */
14058   if (current_binding_level->parm_flag != 1)
14059     {
14060       /* Make sure we have already experienced errors.  */
14061       if (errorcount == 0)
14062         abort ();
14063
14064       /* Throw away the broken statement tree and extra binding
14065          levels.  */
14066       DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14067
14068       while (current_binding_level->parm_flag != 1)
14069         {
14070           if (current_binding_level->parm_flag == 2)
14071             pop_nested_class ();
14072           else
14073             poplevel (0, 0, 0);
14074         }
14075     }
14076   poplevel (1, 0, 1);
14077
14078   /* Set up the named return value optimization, if we can.  Here, we
14079      eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14080      for the nrv.  genrtl_start_function and declare_return_variable
14081      handle making the nrv and RESULT_DECL share space.  */
14082   if (current_function_return_value)
14083     {
14084       tree r = current_function_return_value;
14085       /* This is only worth doing for fns that return in memory--and
14086          simpler, since we don't have to worry about promoted modes.  */
14087       if (r != error_mark_node
14088           && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl))))
14089         {
14090           DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14091           walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14092                                         nullify_returns_r, r);
14093         }
14094       else
14095         /* Clear it so genrtl_start_function and declare_return_variable
14096            know we're not optimizing.  */
14097         current_function_return_value = NULL_TREE;
14098     }
14099
14100   /* Remember that we were in class scope.  */
14101   if (current_class_name)
14102     ctype = current_class_type;
14103
14104   /* Must mark the RESULT_DECL as being in this function.  */
14105   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14106
14107   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14108      to the FUNCTION_DECL node itself.  */
14109   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14110
14111   /* Save away current state, if appropriate.  */
14112   if (!processing_template_decl)
14113     save_function_data (fndecl);
14114
14115   /* If this function calls `setjmp' it cannot be inlined.  When
14116      `longjmp' is called it is not guaranteed to restore the value of
14117      local variables that have been modified since the call to
14118      `setjmp'.  So, if were to inline this function into some caller
14119      `c', then when we `longjmp', we might not restore all variables
14120      in `c'.  (It might seem, at first blush, that there's no way for
14121      this function to modify local variables in `c', but their
14122      addresses may have been stored somewhere accessible to this
14123      function.)  */
14124   if (!processing_template_decl && calls_setjmp_p (fndecl))
14125     DECL_UNINLINABLE (fndecl) = 1;
14126
14127   /* Complain if there's just no return statement.  */
14128   if (warn_return_type
14129       && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14130       && !current_function_returns_value && !current_function_returns_null
14131       /* Don't complain if we abort or throw.  */
14132       && !current_function_returns_abnormally
14133       && !DECL_NAME (DECL_RESULT (fndecl))
14134       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
14135          inline function, as we might never be compiled separately.  */
14136       && (DECL_INLINE (fndecl) || processing_template_decl))
14137     warning ("no return statement in function returning non-void");
14138     
14139   /* Clear out memory we no longer need.  */
14140   free_after_parsing (cfun);
14141   /* Since we never call rest_of_compilation, we never clear
14142      CFUN.  Do so explicitly.  */
14143   free_after_compilation (cfun);
14144   cfun = NULL;
14145
14146   /* If this is an in-class inline definition, we may have to pop the
14147      bindings for the template parameters that we added in
14148      maybe_begin_member_template_processing when start_function was
14149      called.  */
14150   if (inclass_inline)
14151     maybe_end_member_template_processing ();
14152
14153   /* Leave the scope of the class.  */
14154   if (ctype)
14155     pop_nested_class ();
14156
14157   --function_depth;
14158
14159   /* Clean up.  */
14160   if (! nested)
14161     /* Let the error reporting routines know that we're outside a
14162        function.  For a nested function, this value is used in
14163        cxx_pop_function_context and then reset via pop_function_context.  */
14164     current_function_decl = NULL_TREE;
14165
14166   return fndecl;
14167 }
14168 \f
14169 /* Create the FUNCTION_DECL for a function definition.
14170    DECLSPECS and DECLARATOR are the parts of the declaration;
14171    they describe the return type and the name of the function,
14172    but twisted together in a fashion that parallels the syntax of C.
14173
14174    This function creates a binding context for the function body
14175    as well as setting up the FUNCTION_DECL in current_function_decl.
14176
14177    Returns a FUNCTION_DECL on success.
14178
14179    If the DECLARATOR is not suitable for a function (it defines a datum
14180    instead), we return 0, which tells yyparse to report a parse error.
14181
14182    May return void_type_node indicating that this method is actually
14183    a friend.  See grokfield for more details.
14184
14185    Came here with a `.pushlevel' .
14186
14187    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14188    CHANGES TO CODE IN `grokfield'.  */
14189
14190 tree
14191 start_method (tree declspecs, tree declarator, tree attrlist)
14192 {
14193   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14194                                 &attrlist);
14195
14196   if (fndecl == error_mark_node)
14197     return error_mark_node;
14198
14199   if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14200     {
14201       error ("invalid member function declaration");
14202       return error_mark_node;
14203     }
14204
14205   if (attrlist)
14206     cplus_decl_attributes (&fndecl, attrlist, 0);
14207
14208   /* Pass friends other than inline friend functions back.  */
14209   if (fndecl == void_type_node)
14210     return fndecl;
14211
14212   if (DECL_IN_AGGR_P (fndecl))
14213     {
14214       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14215         {
14216           if (DECL_CONTEXT (fndecl)
14217               && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14218             error ("`%D' is already defined in class `%T'", fndecl,
14219                       DECL_CONTEXT (fndecl));
14220         }
14221       return void_type_node;
14222     }
14223
14224   check_template_shadow (fndecl);
14225
14226   DECL_DECLARED_INLINE_P (fndecl) = 1;
14227
14228   if (flag_default_inline)
14229     DECL_INLINE (fndecl) = 1;
14230
14231   /* We process method specializations in finish_struct_1.  */
14232   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14233     fndecl = push_template_decl (fndecl);
14234
14235   if (! DECL_FRIEND_P (fndecl))
14236     {
14237       if (TREE_CHAIN (fndecl))
14238         {
14239           fndecl = copy_node (fndecl);
14240           TREE_CHAIN (fndecl) = NULL_TREE;
14241         }
14242       grok_special_member_properties (fndecl);
14243     }
14244
14245   cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14246
14247   /* Make a place for the parms */
14248   pushlevel (0);
14249   current_binding_level->parm_flag = 1;
14250
14251   DECL_IN_AGGR_P (fndecl) = 1;
14252   return fndecl;
14253 }
14254
14255 /* Go through the motions of finishing a function definition.
14256    We don't compile this method until after the whole class has
14257    been processed.
14258
14259    FINISH_METHOD must return something that looks as though it
14260    came from GROKFIELD (since we are defining a method, after all).
14261
14262    This is called after parsing the body of the function definition.
14263    STMTS is the chain of statements that makes up the function body.
14264
14265    DECL is the ..._DECL that `start_method' provided.  */
14266
14267 tree
14268 finish_method (tree decl)
14269 {
14270   register tree fndecl = decl;
14271   tree old_initial;
14272
14273   register tree link;
14274
14275   if (decl == void_type_node)
14276     return decl;
14277
14278   old_initial = DECL_INITIAL (fndecl);
14279
14280   /* Undo the level for the parms (from start_method).
14281      This is like poplevel, but it causes nothing to be
14282      saved.  Saving information here confuses symbol-table
14283      output routines.  Besides, this information will
14284      be correctly output when this method is actually
14285      compiled.  */
14286
14287   /* Clear out the meanings of the local variables of this level;
14288      also record in each decl which block it belongs to.  */
14289
14290   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14291     {
14292       if (DECL_NAME (link) != NULL_TREE)
14293         pop_binding (DECL_NAME (link), link);
14294       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14295       DECL_CONTEXT (link) = NULL_TREE;
14296     }
14297
14298   poplevel (0, 0, 0);
14299
14300   DECL_INITIAL (fndecl) = old_initial;
14301
14302   /* We used to check if the context of FNDECL was different from
14303      current_class_type as another way to get inside here.  This didn't work
14304      for String.cc in libg++.  */
14305   if (DECL_FRIEND_P (fndecl))
14306     {
14307       CLASSTYPE_INLINE_FRIENDS (current_class_type)
14308         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14309       decl = void_type_node;
14310     }
14311
14312   return decl;
14313 }
14314 \f
14315
14316 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
14317    we can lay it out later, when and if its type becomes complete.  */
14318
14319 void
14320 maybe_register_incomplete_var (tree var)
14321 {
14322   my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14323
14324   /* Keep track of variables with incomplete types.  */
14325   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 
14326       && DECL_EXTERNAL (var))
14327     {
14328       tree inner_type = TREE_TYPE (var);
14329       
14330       while (TREE_CODE (inner_type) == ARRAY_TYPE)
14331         inner_type = TREE_TYPE (inner_type);
14332       inner_type = TYPE_MAIN_VARIANT (inner_type);
14333       
14334       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14335           /* RTTI TD entries are created while defining the type_info.  */
14336           || (TYPE_LANG_SPECIFIC (inner_type)
14337               && TYPE_BEING_DEFINED (inner_type)))
14338         incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14339     }
14340 }
14341
14342 /* Called when a class type (given by TYPE) is defined.  If there are
14343    any existing VAR_DECLs whose type hsa been completed by this
14344    declaration, update them now.  */
14345
14346 void
14347 complete_vars (tree type)
14348 {
14349   tree *list = &incomplete_vars;
14350
14351   my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14352   while (*list) 
14353     {
14354       if (same_type_p (type, TREE_PURPOSE (*list)))
14355         {
14356           tree var = TREE_VALUE (*list);
14357           /* Complete the type of the variable.  The VAR_DECL itself
14358              will be laid out in expand_expr.  */
14359           complete_type (TREE_TYPE (var));
14360           /* Remove this entry from the list.  */
14361           *list = TREE_CHAIN (*list);
14362         }
14363       else
14364         list = &TREE_CHAIN (*list);
14365     }
14366 }
14367
14368 /* If DECL is of a type which needs a cleanup, build that cleanup
14369    here.  */
14370
14371 tree
14372 cxx_maybe_build_cleanup (tree decl)
14373 {
14374   tree type = TREE_TYPE (decl);
14375
14376   if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14377     {
14378       int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14379       tree rval;
14380
14381       if (TREE_CODE (type) == ARRAY_TYPE)
14382         rval = decl;
14383       else
14384         {
14385           cxx_mark_addressable (decl);
14386           rval = build_unary_op (ADDR_EXPR, decl, 0);
14387         }
14388
14389       /* Optimize for space over speed here.  */
14390       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14391           || flag_expensive_optimizations)
14392         flags |= LOOKUP_NONVIRTUAL;
14393
14394       rval = build_delete (TREE_TYPE (rval), rval,
14395                            sfk_complete_destructor, flags, 0);
14396
14397       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14398           && ! TYPE_HAS_DESTRUCTOR (type))
14399         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14400                                                build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14401
14402       return rval;
14403     }
14404   return NULL_TREE;
14405 }
14406 \f
14407 /* When a stmt has been parsed, this function is called.  */
14408
14409 void
14410 finish_stmt (void)
14411 {
14412   /* Always assume this statement was not an expression statement.  If
14413      it actually was an expression statement, its our callers
14414      responsibility to fix this up.  */
14415   last_expr_type = NULL_TREE;
14416 }
14417
14418 /* DECL was originally constructed as a non-static member function,
14419    but turned out to be static.  Update it accordingly.  */
14420
14421 void
14422 revert_static_member_fn (tree decl)
14423 {
14424   tree tmp;
14425   tree function = TREE_TYPE (decl);
14426   tree args = TYPE_ARG_TYPES (function);
14427
14428   if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14429       != TYPE_UNQUALIFIED)
14430     error ("static member function `%#D' declared with type qualifiers",
14431               decl);
14432
14433   args = TREE_CHAIN (args);
14434   tmp = build_function_type (TREE_TYPE (function), args);
14435   tmp = build_qualified_type (tmp, cp_type_quals (function));
14436   tmp = build_exception_variant (tmp,
14437                                  TYPE_RAISES_EXCEPTIONS (function));
14438   TREE_TYPE (decl) = tmp;
14439   if (DECL_ARGUMENTS (decl))
14440     DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14441   DECL_STATIC_FUNCTION_P (decl) = 1;
14442 }
14443
14444 /* Initialize the variables used during compilation of a C++
14445    function.  */
14446
14447 void
14448 cxx_push_function_context (struct function * f)
14449 {
14450   struct language_function *p
14451     = ((struct language_function *)
14452        ggc_alloc_cleared (sizeof (struct language_function)));
14453   f->language = p;
14454
14455   /* It takes an explicit call to expand_body to generate RTL for a
14456      function.  */
14457   expanding_p = 0;
14458
14459   /* Whenever we start a new function, we destroy temporaries in the
14460      usual way.  */
14461   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14462 }
14463
14464 /* Free the language-specific parts of F, now that we've finished
14465    compiling the function.  */
14466
14467 void
14468 cxx_pop_function_context (struct function * f)
14469 {
14470   f->language = 0;
14471 }
14472
14473 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14474    one of the language-independent trees.  */
14475
14476 enum cp_tree_node_structure_enum
14477 cp_tree_node_structure (union lang_tree_node * t)
14478 {
14479   switch (TREE_CODE (&t->generic))
14480     {
14481     case DEFAULT_ARG:           return TS_CP_DEFAULT_ARG;
14482     case IDENTIFIER_NODE:       return TS_CP_IDENTIFIER;
14483     case CPLUS_BINDING:         return TS_CP_BINDING;
14484     case OVERLOAD:              return TS_CP_OVERLOAD;
14485     case TEMPLATE_PARM_INDEX:   return TS_CP_TPI;
14486     case PTRMEM_CST:            return TS_CP_PTRMEM;
14487     case BASELINK:              return TS_CP_BASELINK;
14488     case WRAPPER:               return TS_CP_WRAPPER;
14489     case SRCLOC:                return TS_CP_SRCLOC;
14490     default:                    return TS_CP_GENERIC;
14491     }
14492 }
14493
14494 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14495    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
14496
14497 tree
14498 identifier_global_value (tree t)
14499 {
14500   return IDENTIFIER_GLOBAL_VALUE (t);
14501 }
14502
14503 /* Build the void_list_node (void_type_node having been created).  */
14504 tree
14505 build_void_list_node (void)
14506 {
14507   tree t = build_tree_list (NULL_TREE, void_type_node);
14508   TREE_PARMLIST (t) = 1;
14509   return t;
14510 }
14511
14512 static int
14513 cp_missing_noreturn_ok_p (tree decl)
14514 {
14515   /* A missing noreturn is ok for the `main' function.  */
14516   return DECL_MAIN_P (decl);
14517 }
14518
14519 #include "gt-cp-decl.h"
14520 #include "gtype-cp.h"