OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C++ compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003  Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* Process declarations and symbol lookup for C++ front end.
25    Also constructs types; the standard scalar types at initialization,
26    and structure, union, array and enum types when they are declared.  */
27
28 /* ??? not all decl nodes are given the most useful possible
29    line numbers.  For example, the CONST_DECLs for enum values.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "lex.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "diagnostic.h"
52 #include "debug.h"
53 #include "timevar.h"
54
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
57
58 static void push_binding_level (cxx_scope *);
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 int decl_jump_unsafe (tree);
63 static void storedecls (tree);
64 static void require_complete_types_for_parms (tree);
65 static int ambi_op_p (enum tree_code);
66 static int unary_op_p (enum tree_code);
67 static cxx_saved_binding *store_bindings (tree, cxx_saved_binding *);
68 static tree lookup_tag_reverse (tree, tree);
69 static void push_local_name (tree);
70 static void warn_extern_redeclared_static (tree, tree);
71 static tree grok_reference_init (tree, tree, tree);
72 static tree grokfndecl (tree, tree, tree, tree, int,
73                         enum overload_flags, tree,
74                         tree, int, int, int, int, int, int, tree);
75 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
76 static tree follow_tag_typedef (tree);
77 static tree lookup_tag (enum tree_code, tree,
78                         struct cp_binding_level *, int);
79 static void set_identifier_type_value_with_scope
80         (tree, tree, struct cp_binding_level *);
81 static void record_unknown_type (tree, const char *);
82 static tree builtin_function_1 (const char *, tree, tree, int,
83                                 enum built_in_class, const char *,
84                                 tree);
85 static tree build_library_fn_1 (tree, enum tree_code, tree);
86 static int member_function_or_else (tree, tree, enum overload_flags);
87 static void bad_specifiers (tree, const char *, int, int, int, int,
88                             int);
89 static tree maybe_process_template_type_declaration 
90         (tree, int, struct cp_binding_level*);
91 static void check_for_uninitialized_const_var (tree);
92 static hashval_t typename_hash (const void *);
93 static int typename_compare (const void *, const void *);
94 static void push_binding (tree, tree, struct cp_binding_level*);
95 static int add_binding (tree, tree);
96 static void pop_binding (tree, tree);
97 static tree local_variable_p_walkfn (tree *, int *, void *);
98 static tree select_decl (cxx_binding *, int);
99 static int lookup_flags (int, int);
100 static tree qualify_lookup (tree, int);
101 static tree record_builtin_java_type (const char *, int);
102 static const char *tag_name (enum tag_types code);
103 static void find_class_binding_level (void);
104 static struct cp_binding_level *innermost_nonclass_level (void);
105 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
106 static int walk_globals_r (tree, void*);
107 static int walk_vtables_r (tree, void*);
108 static void add_decl_to_level (tree, struct cp_binding_level *);
109 static tree make_label_decl (tree, int);
110 static void use_label (tree);
111 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
112                                    const location_t *);
113 static void check_previous_goto (struct named_label_use_list *);
114 static void check_switch_goto (struct cp_binding_level *);
115 static void check_previous_gotos (tree);
116 static void pop_label (tree, tree);
117 static void pop_labels (tree);
118 static void maybe_deduce_size_from_array_init (tree, tree);
119 static void layout_var_decl (tree);
120 static void maybe_commonize_var (tree);
121 static tree check_initializer (tree, tree, int);
122 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
123 static void save_function_data (tree);
124 static void check_function_type (tree, tree);
125 static void begin_constructor_body (void);
126 static void finish_constructor_body (void);
127 static void begin_destructor_body (void);
128 static void finish_destructor_body (void);
129 static tree create_array_type_for_decl (tree, tree, tree);
130 static tree get_atexit_node (void);
131 static tree get_dso_handle_node (void);
132 static tree start_cleanup_fn (void);
133 static void end_cleanup_fn (void);
134 static tree cp_make_fname_decl (tree, int);
135 static void initialize_predefined_identifiers (void);
136 static tree check_special_function_return_type 
137         (special_function_kind, tree, tree);
138 static tree push_cp_library_fn (enum tree_code, tree);
139 static tree build_cp_library_fn (tree, enum tree_code, tree);
140 static void store_parm_decls (tree);
141 static int cp_missing_noreturn_ok_p (tree);
142 static void initialize_local_var (tree, tree);
143 static void expand_static_init (tree, tree);
144 static tree next_initializable_field (tree);
145 static tree reshape_init (tree, tree *);
146 static tree build_typename_type (tree, tree, tree);
147
148 /* Erroneous argument lists can use this *IFF* they do not modify it.  */
149 tree error_mark_list;
150
151 /* The following symbols are subsumed in the cp_global_trees array, and
152    listed here individually for documentation purposes.
153
154    C++ extensions
155         tree wchar_decl_node;
156
157         tree vtable_entry_type;
158         tree delta_type_node;
159         tree __t_desc_type_node;
160         tree ti_desc_type_node;
161         tree bltn_desc_type_node, ptr_desc_type_node;
162         tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
163         tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
164         tree ptm_desc_type_node;
165         tree base_desc_type_node;
166
167         tree class_type_node, record_type_node, union_type_node, enum_type_node;
168         tree unknown_type_node;
169
170    Array type `vtable_entry_type[]'
171
172         tree vtbl_type_node;
173         tree vtbl_ptr_type_node;
174
175    Namespaces,
176
177         tree std_node;
178         tree abi_node;
179
180    A FUNCTION_DECL which can call `abort'.  Not necessarily the
181    one that the user will declare, but sufficient to be called
182    by routines that want to abort the program.
183
184         tree abort_fndecl;
185
186    The FUNCTION_DECL for the default `::operator delete'.
187
188         tree global_delete_fndecl;
189
190    Used by RTTI
191         tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
192         tree tinfo_var_id;
193
194 */
195
196 tree cp_global_trees[CPTI_MAX];
197
198 /* Indicates that there is a type value in some namespace, although
199    that is not necessarily in scope at the moment.  */
200
201 static GTY(()) tree global_type_node;
202
203 /* The node that holds the "name" of the global scope.  */
204 static GTY(()) tree global_scope_name;
205
206 /* Used only for jumps to as-yet undefined labels, since jumps to
207    defined labels can have their validity checked immediately.  */
208
209 struct named_label_use_list GTY(())
210 {
211   struct cp_binding_level *binding_level;
212   tree names_in_scope;
213   tree label_decl;
214   location_t o_goto_locus;
215   struct named_label_use_list *next;
216 };
217
218 #define named_label_uses cp_function_chain->x_named_label_uses
219
220 #define local_names cp_function_chain->x_local_names
221
222 /* A list of objects which have constructors or destructors
223    which reside in the global scope.  The decl is stored in
224    the TREE_VALUE slot and the initializer is stored
225    in the TREE_PURPOSE slot.  */
226 tree static_aggregates;
227
228 /* -- end of C++ */
229
230 /* A node for the integer constants 2, and 3.  */
231
232 tree integer_two_node, integer_three_node;
233
234 /* Similar, for last_function_parm_tags.  */
235 tree last_function_parms;
236
237 /* A list of all LABEL_DECLs in the function that have names.  Here so
238    we can clear out their names' definitions at the end of the
239    function, and so we can check the validity of jumps to these labels.  */
240
241 struct named_label_list GTY(())
242 {
243   struct cp_binding_level *binding_level;
244   tree names_in_scope;
245   tree old_value;
246   tree label_decl;
247   tree bad_decls;
248   struct named_label_list *next;
249   unsigned int in_try_scope : 1;
250   unsigned int in_catch_scope : 1;
251 };
252
253 #define named_labels cp_function_chain->x_named_labels
254 \f
255 /* The name of the anonymous namespace, throughout this translation
256    unit.  */
257 tree anonymous_namespace_name;
258
259 /* The number of function bodies which we are currently processing.
260    (Zero if we are at namespace scope, one inside the body of a
261    function, two inside the body of a function in a local class, etc.)  */
262 int function_depth;
263
264 /* States indicating how grokdeclarator() should handle declspecs marked
265    with __attribute__((deprecated)).  An object declared as
266    __attribute__((deprecated)) suppresses warnings of uses of other
267    deprecated items.  */
268    
269 enum deprecated_states {
270   DEPRECATED_NORMAL,
271   DEPRECATED_SUPPRESS
272 };
273
274 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
275
276 /* Set by add_implicitly_declared_members() to keep those members from
277    being flagged as deprecated or reported as using deprecated
278    types.  */
279 int adding_implicit_members = 0;
280
281 /* True if a declaration with an `extern' linkage specifier is being
282    processed.  */
283 bool have_extern_spec;
284
285 \f
286 /* For each binding contour we allocate a binding_level structure
287    which records the names defined in that contour.
288    Contours include:
289     0) the global one
290     1) one for each function definition,
291        where internal declarations of the parameters appear.
292     2) one for each compound statement,
293        to record its declarations.
294
295    The current meaning of a name can be found by searching the levels
296    from the current one out to the global one.
297
298    Off to the side, may be the class_binding_level.  This exists only
299    to catch class-local declarations.  It is otherwise nonexistent.
300
301    Also there may be binding levels that catch cleanups that must be
302    run when exceptions occur.  Thus, to see whether a name is bound in
303    the current scope, it is not enough to look in the
304    CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
305    instead.  */
306
307 /* Note that the information in the `names' component of the global contour
308    is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
309
310 struct cp_binding_level GTY(())
311   {
312     /* A chain of _DECL nodes for all variables, constants, functions,
313        and typedef types.  These are in the reverse of the order
314        supplied.  There may be OVERLOADs on this list, too, but they
315        are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
316     tree names;
317
318     /* Count of elements in names chain.  */
319     size_t names_size;
320
321     /* A chain of NAMESPACE_DECL nodes.  */
322     tree namespaces;
323
324     /* An array of static functions and variables (for namespaces only) */
325     varray_type static_decls;
326
327     /* A chain of VTABLE_DECL nodes.  */
328     tree vtables; 
329
330     /* A dictionary for looking up user-defined-types.  */
331     binding_table type_decls;
332
333     /* A list of USING_DECL nodes.  */
334     tree usings;
335
336     /* A list of used namespaces. PURPOSE is the namespace,
337        VALUE the common ancestor with this binding_level's namespace.  */
338     tree using_directives;
339
340     /* If this binding level is the binding level for a class, then
341        class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
342        is the name of an entity bound in the class.  The TREE_TYPE is
343        the DECL bound by this name in the class.  */
344     tree class_shadowed;
345
346     /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
347        is used for all binding levels. In addition the TREE_VALUE is the
348        IDENTIFIER_TYPE_VALUE before we entered the class.  */
349     tree type_shadowed;
350
351     /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
352        label in this scope.  The TREE_PURPOSE is the previous value of
353        the IDENTIFIER_LABEL VALUE.  */
354     tree shadowed_labels;
355
356     /* For each level (except not the global one),
357        a chain of BLOCK nodes for all the levels
358        that were entered and exited one level down.  */
359     tree blocks;
360
361     /* The entity (namespace, class, function) the scope of which this
362        binding contour corresponds to.  Otherwise NULL.  */
363     tree this_entity;
364
365     /* The binding level which this one is contained in (inherits from).  */
366     struct cp_binding_level *level_chain;
367
368     /* List of VAR_DECLS saved from a previous for statement.
369        These would be dead in ISO-conforming code, but might
370        be referenced in ARM-era code.  These are stored in a
371        TREE_LIST; the TREE_VALUE is the actual declaration.  */
372     tree dead_vars_from_for;
373
374     /* 1 for the level that holds the parameters of a function.
375        2 for the level that holds a class declaration.  */
376     unsigned parm_flag : 2;
377
378     /* 1 means make a BLOCK for this level regardless of all else.
379        2 for temporary binding contours created by the compiler.  */
380     unsigned keep : 2;
381
382     /* Nonzero if this level "doesn't exist" for tags.  */
383     unsigned tag_transparent : 1;
384
385     /* Nonzero if this level can safely have additional
386        cleanup-needing variables added to it.  */
387     unsigned more_cleanups_ok : 1;
388     unsigned have_cleanups : 1;
389
390     /* Nonzero if this scope is for storing the decls for template
391        parameters and generic decls; these decls will be discarded and
392        replaced with a TEMPLATE_DECL.  */
393     unsigned template_parms_p : 1;
394
395     /* Nonzero if this scope corresponds to the `<>' in a
396        `template <>' clause.  Whenever this flag is set,
397        TEMPLATE_PARMS_P will be set as well.  */
398     unsigned template_spec_p : 1;
399
400     /* This is set for a namespace binding level.  */
401     unsigned namespace_p : 1;
402
403     /* True if this level is that of a for-statement where we need to
404        worry about ambiguous (ARM or ISO) scope rules.  */
405     unsigned is_for_scope : 1;
406
407     /* True if this level corresponds to a TRY block.  Currently this
408        information is only available while building the tree structure.  */
409     unsigned is_try_scope : 1;
410
411     /* True if this level corresponds to a CATCH block.  Currently this
412        information is only available while building the tree structure.  */
413     unsigned is_catch_scope : 1;
414
415     /* Three bits left for this word.  */
416
417     /* Binding depth at which this level began.  */
418     unsigned binding_depth;
419   };
420
421 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
422
423 /* The binding level currently in effect.  */
424
425 #define current_binding_level                   \
426   (*(cfun && cp_function_chain->bindings        \
427    ? &cp_function_chain->bindings               \
428    : &scope_chain->bindings))
429
430 /* The binding level of the current class, if any.  */
431
432 #define class_binding_level scope_chain->class_bindings
433
434 /* A chain of binding_level structures awaiting reuse.  */
435
436 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
437
438 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
439
440 static int keep_next_level_flag;
441
442 /* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
443    UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
444    time the VAR_DECL was declared, the type was incomplete.  */
445
446 static GTY(()) tree incomplete_vars;
447
448 #ifndef ENABLE_SCOPE_CHECKING
449 #  define ENABLE_SCOPE_CHECKING 0
450 #else
451 #  define ENABLE_SCOPE_CHECKING 1
452 #endif
453
454 static int binding_depth = 0;
455 static int is_class_level = 0;
456
457 static void
458 indent (int depth)
459 {
460   int i;
461
462   for (i = 0; i < depth * 2; i++)
463     putc (' ', stderr);
464 }
465
466 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
467
468 /* Return a string describing the kind of SCOPE we have.  */
469 static const char *
470 cxx_scope_descriptor (cxx_scope *scope)
471 {
472   const char *desc;
473
474   if (scope->namespace_p)
475     desc = "namespace-scope";
476   else if (scope->parm_flag == 1)
477     desc = "function-prototype-scope";
478   else if (scope->parm_flag == 2)
479     desc = "class-scope";
480   else if (scope->is_for_scope)
481     desc = "for-scope";
482   else if (scope->is_try_scope)
483     desc = "try-scope";
484   else if (scope->is_catch_scope)
485     desc = "catch-scope";
486   else if (scope->template_spec_p)
487     desc = "template-explicit-spec-scope";
488   else if (scope->template_parms_p)
489     desc = "template-prototype-scope";
490   else
491     desc = "block-scope";
492
493   return desc;
494 }
495
496 /* Output a debugging information about SCOPE when performning
497    ACTION at LINE.  */
498 static void
499 cxx_scope_debug (cxx_scope *scope, int line, const char *action)
500 {
501   const char *desc = cxx_scope_descriptor (scope);
502   if (scope->this_entity)
503     verbatim ("%s %s(%E) %p %d\n", action, desc,
504               scope->this_entity, (void *) scope, line);
505   else
506     verbatim ("%s %s %p %d\n", action, desc, (void *) scope, line);
507 }
508
509 /* Construct a scope that may be TAG-TRANSPARENT, the sub-blocks of
510    which may be KEPT.  */
511 static inline cxx_scope *
512 make_cxx_scope (bool tag_transparent, int keep)
513 {
514   cxx_scope *scope;
515   
516   /* Reuse or create a struct for this binding level.  */
517   if (!ENABLE_SCOPE_CHECKING && free_binding_level)
518     {
519       scope = free_binding_level;
520       free_binding_level = scope->level_chain;
521     }
522   else
523     scope = ggc_alloc (sizeof (cxx_scope));
524
525   memset (scope, 0, sizeof (cxx_scope));
526   scope->tag_transparent = tag_transparent;
527   scope->keep = keep;
528   scope->more_cleanups_ok = true;
529
530   return scope;
531 }
532
533 static void
534 push_binding_level (cxx_scope *newlevel)
535 {
536   /* Add this level to the front of the chain (stack) of levels that
537      are active.  */
538   newlevel->level_chain = current_binding_level;
539   current_binding_level = newlevel;
540
541   if (ENABLE_SCOPE_CHECKING)
542     {
543       newlevel->binding_depth = binding_depth;
544       indent (binding_depth);
545       cxx_scope_debug (newlevel, input_location.line, "push");
546       is_class_level = 0;
547       binding_depth++;
548     }
549 }
550
551 /* Find the innermost enclosing class scope, and reset
552    CLASS_BINDING_LEVEL appropriately.  */
553
554 static void
555 find_class_binding_level (void)
556 {
557   struct cp_binding_level *level = current_binding_level;
558
559   while (level && level->parm_flag != 2)
560     level = level->level_chain;
561   if (level && level->parm_flag == 2)
562     class_binding_level = level;
563   else
564     class_binding_level = 0;
565 }
566
567 static void
568 pop_binding_level (void)
569 {
570   if (NAMESPACE_LEVEL (global_namespace))
571     /* Cannot pop a level, if there are none left to pop.  */
572     my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
573   /* Pop the current level, and free the structure for reuse.  */
574   if (ENABLE_SCOPE_CHECKING)
575     {
576       indent (--binding_depth);
577       cxx_scope_debug (current_binding_level, input_location.line, "pop");
578       if (is_class_level != (current_binding_level == class_binding_level))
579         {
580           indent (binding_depth);
581           verbatim ("XXX is_class_level != (current_binding_level "
582                     "== class_binding_level)\n");
583         }
584       is_class_level = 0;
585     }
586   {
587     register struct cp_binding_level *level = current_binding_level;
588     current_binding_level = current_binding_level->level_chain;
589     level->level_chain = free_binding_level;
590     if (level->parm_flag == 2)
591       level->type_decls = NULL;
592     else
593       binding_table_free (level->type_decls);
594     my_friendly_assert (!ENABLE_SCOPE_CHECKING
595                         || level->binding_depth == binding_depth,
596                         20030529);
597     free_binding_level = level;
598     find_class_binding_level ();
599   }
600 }
601
602 static void
603 suspend_binding_level (void)
604 {
605   if (class_binding_level)
606     current_binding_level = class_binding_level;
607
608   if (NAMESPACE_LEVEL (global_namespace))
609     /* Cannot suspend a level, if there are none left to suspend.  */
610     my_friendly_assert (!global_scope_p (current_binding_level), 20030527);
611   /* Suspend the current level.  */
612   if (ENABLE_SCOPE_CHECKING)
613     {
614       indent (--binding_depth);
615       cxx_scope_debug (current_binding_level, input_location.line, "suspend");
616       if (is_class_level != (current_binding_level == class_binding_level))
617         {
618           indent (binding_depth);
619           verbatim ("XXX is_class_level != (current_binding_level "
620                     "== class_binding_level)\n");
621         }
622       is_class_level = 0;
623     }
624   current_binding_level = current_binding_level->level_chain;
625   find_class_binding_level ();
626 }
627
628 static void
629 resume_binding_level (struct cp_binding_level* b)
630 {
631   /* Resuming binding levels is meant only for namespaces,
632      and those cannot nest into classes.  */
633   my_friendly_assert(!class_binding_level, 386);
634   /* Also, resuming a non-directly nested namespace is a no-no.  */
635   my_friendly_assert(b->level_chain == current_binding_level, 386);
636   current_binding_level = b;
637   if (ENABLE_SCOPE_CHECKING)
638     {
639       b->binding_depth = binding_depth;
640       indent (binding_depth);
641       cxx_scope_debug (b, input_location.line, "resume");
642       is_class_level = 0;
643       binding_depth++;
644     }
645 }
646 \f
647 /* Nonzero if we are currently in the global binding level.  */
648
649 int
650 global_bindings_p (void)
651 {
652   return global_scope_p (current_binding_level);
653 }
654
655 /* Return the innermost binding level that is not for a class scope.  */
656
657 static struct cp_binding_level *
658 innermost_nonclass_level (void)
659 {
660   struct cp_binding_level *b;
661
662   b = current_binding_level;
663   while (b->parm_flag == 2)
664     b = b->level_chain;
665
666   return b;
667 }
668
669 /* Nonzero if we are currently in a toplevel binding level.  This
670    means either the global binding level or a namespace in a toplevel
671    binding level.  Since there are no non-toplevel namespace levels,
672    this really means any namespace or template parameter level.  We
673    also include a class whose context is toplevel.  */
674
675 int
676 toplevel_bindings_p (void)
677 {
678   struct cp_binding_level *b = innermost_nonclass_level ();
679
680   return b->namespace_p || b->template_parms_p;
681 }
682
683 /* Nonzero if this is a namespace scope, or if we are defining a class
684    which is itself at namespace scope, or whose enclosing class is
685    such a class, etc.  */
686
687 int
688 namespace_bindings_p (void)
689 {
690   struct cp_binding_level *b = innermost_nonclass_level ();
691
692   return b->namespace_p;
693 }
694
695 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
696    unconditionally.  Otherwise, use the normal logic to decide whether
697    or not to create a BLOCK.  */
698
699 void
700 keep_next_level (int keep)
701 {
702   keep_next_level_flag = keep;
703 }
704
705 /* Nonzero if the current level needs to have a BLOCK made.  */
706
707 int
708 kept_level_p (void)
709 {
710   return (current_binding_level->blocks != NULL_TREE
711           || current_binding_level->keep
712           || current_binding_level->names != NULL_TREE
713           || (current_binding_level->type_decls != NULL
714               && !current_binding_level->tag_transparent));
715 }
716
717 /* Returns nonzero if this scope was created to store template
718    parameters.  */
719
720 int
721 template_parm_scope_p (void)
722 {
723   return current_binding_level->template_parms_p;
724 }
725
726 /* Returns the kind of template specialization we are currently
727    processing, given that it's declaration contained N_CLASS_SCOPES
728    explicit scope qualifications.  */
729
730 tmpl_spec_kind
731 current_tmpl_spec_kind (int n_class_scopes)
732 {
733   int n_template_parm_scopes = 0;
734   int seen_specialization_p = 0;
735   int innermost_specialization_p = 0;
736   struct cp_binding_level *b;
737
738   /* Scan through the template parameter scopes.  */
739   for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
740     {
741       /* If we see a specialization scope inside a parameter scope,
742          then something is wrong.  That corresponds to a declaration
743          like:
744
745             template <class T> template <> ...
746
747          which is always invalid since [temp.expl.spec] forbids the
748          specialization of a class member template if the enclosing
749          class templates are not explicitly specialized as well.  */
750       if (b->template_spec_p)
751         {
752           if (n_template_parm_scopes == 0)
753             innermost_specialization_p = 1;
754           else
755             seen_specialization_p = 1;
756         }
757       else if (seen_specialization_p == 1)
758         return tsk_invalid_member_spec;
759
760       ++n_template_parm_scopes;
761     }
762
763   /* Handle explicit instantiations.  */
764   if (processing_explicit_instantiation)
765     {
766       if (n_template_parm_scopes != 0)
767         /* We've seen a template parameter list during an explicit
768            instantiation.  For example:
769
770              template <class T> template void f(int);
771
772            This is erroneous.  */
773         return tsk_invalid_expl_inst;
774       else
775         return tsk_expl_inst;
776     }
777
778   if (n_template_parm_scopes < n_class_scopes)
779     /* We've not seen enough template headers to match all the
780        specialized classes present.  For example:
781
782          template <class T> void R<T>::S<T>::f(int);
783
784        This is invalid; there needs to be one set of template
785        parameters for each class.  */
786     return tsk_insufficient_parms;
787   else if (n_template_parm_scopes == n_class_scopes)
788     /* We're processing a non-template declaration (even though it may
789        be a member of a template class.)  For example:
790
791          template <class T> void S<T>::f(int);
792
793        The `class T' maches the `S<T>', leaving no template headers
794        corresponding to the `f'.  */
795     return tsk_none;
796   else if (n_template_parm_scopes > n_class_scopes + 1)
797     /* We've got too many template headers.  For example:
798
799          template <> template <class T> void f (T);
800
801        There need to be more enclosing classes.  */
802     return tsk_excessive_parms;
803   else
804     /* This must be a template.  It's of the form:
805
806          template <class T> template <class U> void S<T>::f(U);
807
808        This is a specialization if the innermost level was a
809        specialization; otherwise it's just a definition of the
810        template.  */
811     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
812 }
813
814 void
815 set_class_shadows (tree shadows)
816 {
817   class_binding_level->class_shadowed = shadows;
818 }
819
820 /* Enter a new binding level.
821    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
822    not for that of tags.  */
823
824 void
825 pushlevel (int tag_transparent)
826 {
827   if (cfun && !doing_semantic_analysis_p ())
828     return;
829
830   push_binding_level (make_cxx_scope (tag_transparent, keep_next_level_flag));
831   keep_next_level_flag = 0;
832 }
833
834 /* We're defining an object of type TYPE.  If it needs a cleanup, but
835    we're not allowed to add any more objects with cleanups to the current
836    scope, create a new binding level.  */
837
838 void
839 maybe_push_cleanup_level (tree type)
840 {
841   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
842       && current_binding_level->more_cleanups_ok == 0)
843     {
844       keep_next_level (2);
845       pushlevel (1);
846       clear_last_expr ();
847       add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
848     }
849 }
850   
851 /* Enter a new scope.  The KIND indicates what kind of scope is being
852    created.  */
853
854 void
855 begin_scope (scope_kind sk)
856 {
857   pushlevel (0);
858
859   switch (sk)
860     {
861     case sk_block:
862       break;
863
864     case sk_try:
865       current_binding_level->is_try_scope = 1;
866       break;
867
868     case sk_catch:
869       current_binding_level->is_catch_scope = 1;
870       break;
871
872     case sk_for:
873       current_binding_level->is_for_scope = 1;
874       break;
875
876     case sk_template_spec:
877       current_binding_level->template_spec_p = 1;
878       /* Fall through.  */
879
880     case sk_template_parms:
881       current_binding_level->template_parms_p = 1;
882       break;
883
884     default:
885       abort ();
886     }
887 }
888
889 /* Exit the current scope.  */
890
891 void
892 finish_scope (void)
893 {
894   poplevel (0, 0, 0);
895 }
896
897 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
898    level at which this declaration is being bound.  */
899
900 static void
901 push_binding (tree id, tree decl, cxx_scope* level)
902 {
903    cxx_binding *binding = cxx_binding_make (decl, NULL);
904
905   /* Now, fill in the binding information.  */
906   binding->previous = IDENTIFIER_BINDING (id);
907   BINDING_SCOPE (binding) = level;
908   INHERITED_VALUE_BINDING_P (binding) = 0;
909   LOCAL_BINDING_P (binding) = (level != class_binding_level);
910
911   /* And put it on the front of the list of bindings for ID.  */
912   IDENTIFIER_BINDING (id) = binding;
913 }
914
915 /* ID is already bound in the current scope.  But, DECL is an
916    additional binding for ID in the same scope.  This is the `struct
917    stat' hack whereby a non-typedef class-name or enum-name can be
918    bound at the same level as some other kind of entity.  It's the
919    responsibility of the caller to check that inserting this name is
920    valid here.  Returns nonzero if the new binding was successful.  */
921 static int
922 add_binding (tree id, tree decl)
923 {
924   cxx_binding *binding = IDENTIFIER_BINDING (id);
925   int ok = 1;
926
927   timevar_push (TV_NAME_LOOKUP);
928   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
929     /* The new name is the type name.  */
930     BINDING_TYPE (binding) = decl;
931   else if (!BINDING_VALUE (binding))
932     /* This situation arises when push_class_level_binding moves an
933        inherited type-binding out of the way to make room for a new
934        value binding.  */
935     BINDING_VALUE (binding) = decl;
936   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
937            && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
938     {
939       /* The old binding was a type name.  It was placed in
940          BINDING_VALUE because it was thought, at the point it was
941          declared, to be the only entity with such a name.  Move the
942          type name into the type slot; it is now hidden by the new
943          binding.  */
944       BINDING_TYPE (binding) = BINDING_VALUE (binding);
945       BINDING_VALUE (binding) = decl;
946       INHERITED_VALUE_BINDING_P (binding) = 0;
947     }
948   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
949            && TREE_CODE (decl) == TYPE_DECL
950            && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
951            && (same_type_p (TREE_TYPE (decl),
952                             TREE_TYPE (BINDING_VALUE (binding)))
953                /* If either type involves template parameters, we must
954                   wait until instantiation.  */
955                || uses_template_parms (TREE_TYPE (decl))
956                || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
957     /* We have two typedef-names, both naming the same type to have
958        the same name.  This is OK because of:
959
960          [dcl.typedef]
961
962          In a given scope, a typedef specifier can be used to redefine
963          the name of any type declared in that scope to refer to the
964          type to which it already refers.  */
965     ok = 0;
966   /* There can be two block-scope declarations of the same variable,
967      so long as they are `extern' declarations.  */
968   else if (TREE_CODE (decl) == VAR_DECL
969            && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
970            && DECL_EXTERNAL (decl)
971            && DECL_EXTERNAL (BINDING_VALUE (binding)))
972     {
973       duplicate_decls (decl, BINDING_VALUE (binding));
974       ok = 0;
975     }
976   else
977     {
978       error ("declaration of `%#D'", decl);
979       cp_error_at ("conflicts with previous declaration `%#D'",
980                    BINDING_VALUE (binding));
981       ok = 0;
982     }
983
984   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
985 }
986
987 /* Add DECL to the list of things declared in B.  */
988
989 static void
990 add_decl_to_level (tree decl, 
991                    struct cp_binding_level* b)
992 {
993   if (TREE_CODE (decl) == NAMESPACE_DECL 
994       && !DECL_NAMESPACE_ALIAS (decl))
995     {
996       TREE_CHAIN (decl) = b->namespaces;
997       b->namespaces = decl;
998     }
999   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1000     {
1001       TREE_CHAIN (decl) = b->vtables;
1002       b->vtables = decl;
1003     }
1004   else       
1005     {
1006       /* We build up the list in reverse order, and reverse it later if
1007          necessary.  */
1008       TREE_CHAIN (decl) = b->names;
1009       b->names = decl;
1010       b->names_size++;
1011
1012       /* If appropriate, add decl to separate list of statics */
1013       if (b->namespace_p)
1014         if ((TREE_CODE (decl) == VAR_DECL && TREE_STATIC (decl))
1015             || (TREE_CODE (decl) == FUNCTION_DECL
1016                 && (!TREE_PUBLIC (decl) || DECL_DECLARED_INLINE_P (decl))))
1017           VARRAY_PUSH_TREE (b->static_decls, decl);
1018     }
1019 }
1020
1021 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1022    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1023    doesn't really belong to this binding level, that it got here
1024    through a using-declaration.  */
1025
1026 void
1027 push_local_binding (tree id, tree decl, int flags)
1028 {
1029   struct cp_binding_level *b;
1030
1031   /* Skip over any local classes.  This makes sense if we call
1032      push_local_binding with a friend decl of a local class.  */
1033   b = current_binding_level;
1034   while (b->parm_flag == 2)
1035     b = b->level_chain;
1036
1037   if (lookup_name_current_level (id))
1038     {
1039       /* Supplement the existing binding.  */
1040       if (!add_binding (id, decl))
1041         /* It didn't work.  Something else must be bound at this
1042            level.  Do not add DECL to the list of things to pop
1043            later.  */
1044         return;
1045     }
1046   else
1047     /* Create a new binding.  */
1048     push_binding (id, decl, b);
1049
1050   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1051     /* We must put the OVERLOAD into a TREE_LIST since the
1052        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1053        decls that got here through a using-declaration.  */
1054     decl = build_tree_list (NULL_TREE, decl);
1055
1056   /* And put DECL on the list of things declared by the current
1057      binding level.  */
1058   add_decl_to_level (decl, b);
1059 }
1060
1061 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1062    binding was successful.  */
1063
1064 int
1065 push_class_binding (tree id, tree decl)
1066 {
1067   int result = 1;
1068   cxx_binding *binding = IDENTIFIER_BINDING (id);
1069   tree context;
1070
1071   timevar_push (TV_NAME_LOOKUP);
1072   /* Note that we declared this value so that we can issue an error if
1073      this is an invalid redeclaration of a name already used for some
1074      other purpose.  */
1075   note_name_declared_in_class (id, decl);
1076
1077   if (binding && BINDING_SCOPE (binding) == class_binding_level)
1078     /* Supplement the existing binding.  */
1079     result = add_binding (id, decl);
1080   else
1081     /* Create a new binding.  */
1082     push_binding (id, decl, class_binding_level);
1083
1084   /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1085      class-level declaration.  Note that we do not use DECL here
1086      because of the possibility of the `struct stat' hack; if DECL is
1087      a class-name or enum-name we might prefer a field-name, or some
1088      such.  */
1089   IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1090
1091   /* If this is a binding from a base class, mark it as such.  */
1092   binding = IDENTIFIER_BINDING (id);
1093   if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1094     {
1095       if (TREE_CODE (decl) == OVERLOAD)
1096         context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1097       else
1098         {
1099           my_friendly_assert (DECL_P (decl), 0);
1100           context = context_for_name_lookup (decl);
1101         }
1102
1103       if (is_properly_derived_from (current_class_type, context))
1104         INHERITED_VALUE_BINDING_P (binding) = 1;
1105       else
1106         INHERITED_VALUE_BINDING_P (binding) = 0;
1107     }
1108   else if (BINDING_VALUE (binding) == decl)
1109     /* We only encounter a TREE_LIST when push_class_decls detects an
1110        ambiguity.  Such an ambiguity can be overridden by a definition
1111        in this class.  */
1112     INHERITED_VALUE_BINDING_P (binding) = 1;
1113
1114   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1115 }
1116
1117 /* Remove the binding for DECL which should be the innermost binding
1118    for ID.  */
1119
1120 static void
1121 pop_binding (tree id, tree decl)
1122 {
1123   cxx_binding *binding;
1124
1125   if (id == NULL_TREE)
1126     /* It's easiest to write the loops that call this function without
1127        checking whether or not the entities involved have names.  We
1128        get here for such an entity.  */
1129     return;
1130
1131   /* Get the innermost binding for ID.  */
1132   binding = IDENTIFIER_BINDING (id);
1133
1134   /* The name should be bound.  */
1135   my_friendly_assert (binding != NULL, 0);
1136
1137   /* The DECL will be either the ordinary binding or the type
1138      binding for this identifier.  Remove that binding.  */
1139   if (BINDING_VALUE (binding) == decl)
1140     BINDING_VALUE (binding) = NULL_TREE;
1141   else if (BINDING_TYPE (binding) == decl)
1142     BINDING_TYPE (binding) = NULL_TREE;
1143   else
1144     abort ();
1145
1146   if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1147     {
1148       /* We're completely done with the innermost binding for this
1149          identifier.  Unhook it from the list of bindings.  */
1150       IDENTIFIER_BINDING (id) = binding->previous;
1151
1152       /* Add it to the free list.  */
1153       cxx_binding_free (binding);
1154
1155       /* Clear the BINDING_SCOPE so the garbage collector doesn't walk
1156          it.  */
1157       BINDING_SCOPE (binding) = NULL;
1158     }
1159 }
1160
1161 /* When a label goes out of scope, check to see if that label was used
1162    in a valid manner, and issue any appropriate warnings or errors.  */
1163
1164 static void
1165 pop_label (tree label, tree old_value)
1166 {
1167   if (!processing_template_decl && doing_semantic_analysis_p ())
1168     {
1169       if (DECL_INITIAL (label) == NULL_TREE)
1170         {
1171           cp_error_at ("label `%D' used but not defined", label);
1172           /* Avoid crashing later.  */
1173           define_label (input_filename, 1, DECL_NAME (label));
1174         }
1175       else if (warn_unused_label && !TREE_USED (label))
1176         cp_warning_at ("label `%D' defined but not used", label);
1177     }
1178
1179   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1180 }
1181
1182 /* At the end of a function, all labels declared within the function
1183    go out of scope.  BLOCK is the top-level block for the
1184    function.  */
1185
1186 static void
1187 pop_labels (tree block)
1188 {
1189   struct named_label_list *link;
1190
1191   /* Clear out the definitions of all label names, since their scopes
1192      end here.  */
1193   for (link = named_labels; link; link = link->next)
1194     {
1195       pop_label (link->label_decl, link->old_value);
1196       /* Put the labels into the "variables" of the top-level block,
1197          so debugger can see them.  */
1198       TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1199       BLOCK_VARS (block) = link->label_decl;
1200     }
1201
1202   named_labels = NULL;
1203 }
1204
1205 /* Exit a binding level.
1206    Pop the level off, and restore the state of the identifier-decl mappings
1207    that were in effect when this level was entered.
1208
1209    If KEEP == 1, this level had explicit declarations, so
1210    and create a "block" (a BLOCK node) for the level
1211    to record its declarations and subblocks for symbol table output.
1212
1213    If FUNCTIONBODY is nonzero, this level is the body of a function,
1214    so create a block as if KEEP were set and also clear out all
1215    label names.
1216
1217    If REVERSE is nonzero, reverse the order of decls before putting
1218    them into the BLOCK.  */
1219
1220 tree
1221 poplevel (int keep, int reverse, int functionbody)
1222 {
1223   register tree link;
1224   /* The chain of decls was accumulated in reverse order.
1225      Put it into forward order, just for cleanliness.  */
1226   tree decls;
1227   int tmp = functionbody;
1228   int real_functionbody;
1229   tree subblocks;
1230   tree block = NULL_TREE;
1231   tree decl;
1232   int leaving_for_scope;
1233
1234   timevar_push (TV_NAME_LOOKUP);
1235   if (cfun && !doing_semantic_analysis_p ())
1236     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1237
1238   my_friendly_assert (current_binding_level->parm_flag != 2,
1239                       19990916);
1240
1241   real_functionbody = (current_binding_level->keep == 2
1242                        ? ((functionbody = 0), tmp) : functionbody);
1243   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1244
1245   my_friendly_assert (!current_binding_level->class_shadowed,
1246                       19990414);
1247
1248   /* We used to use KEEP == 2 to indicate that the new block should go
1249      at the beginning of the list of blocks at this binding level,
1250      rather than the end.  This hack is no longer used.  */
1251   my_friendly_assert (keep == 0 || keep == 1, 0);
1252
1253   if (current_binding_level->keep == 1)
1254     keep = 1;
1255
1256   /* Any uses of undefined labels, and any defined labels, now operate
1257      under constraints of next binding contour.  */
1258   if (cfun && !functionbody)
1259     {
1260       struct cp_binding_level *level_chain;
1261       level_chain = current_binding_level->level_chain;
1262       if (level_chain)
1263         {
1264           struct named_label_use_list *uses;
1265           struct named_label_list *labels;
1266           for (labels = named_labels; labels; labels = labels->next)
1267             if (labels->binding_level == current_binding_level)
1268               {
1269                 tree decl;
1270                 if (current_binding_level->is_try_scope)
1271                   labels->in_try_scope = 1;
1272                 if (current_binding_level->is_catch_scope)
1273                   labels->in_catch_scope = 1;
1274                 for (decl = labels->names_in_scope; decl;
1275                      decl = TREE_CHAIN (decl))
1276                   if (decl_jump_unsafe (decl))
1277                     labels->bad_decls = tree_cons (NULL_TREE, decl,
1278                                                    labels->bad_decls);
1279                 labels->binding_level = level_chain;
1280                 labels->names_in_scope = level_chain->names;
1281               }
1282
1283           for (uses = named_label_uses; uses; uses = uses->next)
1284             if (uses->binding_level == current_binding_level)
1285               {
1286                 uses->binding_level = level_chain;
1287                 uses->names_in_scope = level_chain->names;
1288               }
1289         }
1290     }
1291
1292   /* Get the decls in the order they were written.
1293      Usually current_binding_level->names is in reverse order.
1294      But parameter decls were previously put in forward order.  */
1295
1296   if (reverse)
1297     current_binding_level->names
1298       = decls = nreverse (current_binding_level->names);
1299   else
1300     decls = current_binding_level->names;
1301
1302   /* Output any nested inline functions within this block
1303      if they weren't already output.  */
1304   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1305     if (TREE_CODE (decl) == FUNCTION_DECL
1306         && ! TREE_ASM_WRITTEN (decl)
1307         && DECL_INITIAL (decl) != NULL_TREE
1308         && TREE_ADDRESSABLE (decl)
1309         && decl_function_context (decl) == current_function_decl)
1310       {
1311         /* If this decl was copied from a file-scope decl
1312            on account of a block-scope extern decl,
1313            propagate TREE_ADDRESSABLE to the file-scope decl.  */
1314         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1315           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1316         else
1317           {
1318             push_function_context ();
1319             output_inline_function (decl);
1320             pop_function_context ();
1321           }
1322       }
1323
1324   /* When not in function-at-a-time mode, expand_end_bindings will
1325      warn about unused variables.  But, in function-at-a-time mode
1326      expand_end_bindings is not passed the list of variables in the
1327      current scope, and therefore no warning is emitted.  So, we
1328      explicitly warn here.  */
1329   if (!processing_template_decl)
1330     warn_about_unused_variables (getdecls ());
1331
1332   /* If there were any declarations or structure tags in that level,
1333      or if this level is a function body,
1334      create a BLOCK to record them for the life of this function.  */
1335   block = NULL_TREE;
1336   if (keep == 1 || functionbody)
1337     block = make_node (BLOCK);
1338   if (block != NULL_TREE)
1339     {
1340       BLOCK_VARS (block) = decls;
1341       BLOCK_SUBBLOCKS (block) = subblocks;
1342     }
1343
1344   /* In each subblock, record that this is its superior.  */
1345   if (keep >= 0)
1346     for (link = subblocks; link; link = TREE_CHAIN (link))
1347       BLOCK_SUPERCONTEXT (link) = block;
1348
1349   /* We still support the old for-scope rules, whereby the variables
1350      in a for-init statement were in scope after the for-statement
1351      ended.  We only use the new rules if flag_new_for_scope is
1352      nonzero.  */
1353   leaving_for_scope
1354     = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1355
1356   /* Remove declarations for all the DECLs in this level.  */
1357   for (link = decls; link; link = TREE_CHAIN (link))
1358     {
1359       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1360           && DECL_NAME (link))
1361         {
1362           cxx_binding *outer_binding
1363             = IDENTIFIER_BINDING (DECL_NAME (link))->previous;
1364           tree ns_binding;
1365
1366           if (!outer_binding)
1367             ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1368           else
1369             ns_binding = NULL_TREE;
1370
1371           if (outer_binding
1372               && (BINDING_SCOPE (outer_binding)
1373                   == current_binding_level->level_chain))
1374             /* We have something like:
1375
1376                  int i;
1377                  for (int i; ;);
1378
1379                and we are leaving the `for' scope.  There's no reason to
1380                keep the binding of the inner `i' in this case.  */
1381             pop_binding (DECL_NAME (link), link);
1382           else if ((outer_binding
1383                     && (TREE_CODE (BINDING_VALUE (outer_binding))
1384                         == TYPE_DECL))
1385                    || (ns_binding
1386                        && TREE_CODE (ns_binding) == TYPE_DECL))
1387             /* Here, we have something like:
1388
1389                  typedef int I;
1390
1391                  void f () {
1392                    for (int I; ;);
1393                  }
1394
1395                We must pop the for-scope binding so we know what's a
1396                type and what isn't.  */
1397             pop_binding (DECL_NAME (link), link);
1398           else
1399             {
1400               /* Mark this VAR_DECL as dead so that we can tell we left it
1401                  there only for backward compatibility.  */
1402               DECL_DEAD_FOR_LOCAL (link) = 1;
1403
1404               /* Keep track of what should have happened when we
1405                  popped the binding.  */
1406               if (outer_binding && BINDING_VALUE (outer_binding))
1407                 DECL_SHADOWED_FOR_VAR (link)
1408                   = BINDING_VALUE (outer_binding);
1409
1410               /* Add it to the list of dead variables in the next
1411                  outermost binding to that we can remove these when we
1412                  leave that binding.  */
1413               current_binding_level->level_chain->dead_vars_from_for
1414                 = tree_cons (NULL_TREE, link,
1415                              current_binding_level->level_chain->
1416                              dead_vars_from_for);
1417
1418               /* Although we don't pop the cxx_binding, we do clear
1419                  its BINDING_SCOPE since the level is going away now.  */
1420               BINDING_SCOPE (IDENTIFIER_BINDING (DECL_NAME (link))) = 0;
1421             }
1422         }
1423       else
1424         {
1425           /* Remove the binding.  */
1426           decl = link;
1427           if (TREE_CODE (decl) == TREE_LIST)
1428             decl = TREE_VALUE (decl);
1429           if (DECL_P (decl))
1430             pop_binding (DECL_NAME (decl), decl);
1431           else if (TREE_CODE (decl) == OVERLOAD)
1432             pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1433           else
1434             abort ();
1435         }
1436     }
1437
1438   /* Remove declarations for any `for' variables from inner scopes
1439      that we kept around.  */
1440   for (link = current_binding_level->dead_vars_from_for;
1441        link; link = TREE_CHAIN (link))
1442     pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1443
1444   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1445   for (link = current_binding_level->type_shadowed;
1446        link; link = TREE_CHAIN (link))
1447     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1448
1449   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1450   for (link = current_binding_level->shadowed_labels;
1451        link;
1452        link = TREE_CHAIN (link))
1453     pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1454
1455   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1456      list if a `using' declaration put them there.  The debugging
1457      back-ends won't understand OVERLOAD, so we remove them here.
1458      Because the BLOCK_VARS are (temporarily) shared with
1459      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1460      popped all the bindings.  */
1461   if (block)
1462     {
1463       tree* d;
1464
1465       for (d = &BLOCK_VARS (block); *d; )
1466         {
1467           if (TREE_CODE (*d) == TREE_LIST)
1468             *d = TREE_CHAIN (*d);
1469           else
1470             d = &TREE_CHAIN (*d);
1471         }
1472     }
1473
1474   /* If the level being exited is the top level of a function,
1475      check over all the labels.  */
1476   if (functionbody)
1477     {
1478       /* Since this is the top level block of a function, the vars are
1479          the function's parameters.  Don't leave them in the BLOCK
1480          because they are found in the FUNCTION_DECL instead.  */
1481       BLOCK_VARS (block) = 0;
1482       pop_labels (block);
1483     }
1484
1485   tmp = current_binding_level->keep;
1486
1487   pop_binding_level ();
1488   if (functionbody)
1489     DECL_INITIAL (current_function_decl) = block;
1490   else if (block)
1491     current_binding_level->blocks
1492       = chainon (current_binding_level->blocks, block);
1493
1494   /* If we did not make a block for the level just exited,
1495      any blocks made for inner levels
1496      (since they cannot be recorded as subblocks in that level)
1497      must be carried forward so they will later become subblocks
1498      of something else.  */
1499   else if (subblocks)
1500     current_binding_level->blocks
1501       = chainon (current_binding_level->blocks, subblocks);
1502
1503   /* Each and every BLOCK node created here in `poplevel' is important
1504      (e.g. for proper debugging information) so if we created one
1505      earlier, mark it as "used".  */
1506   if (block)
1507     TREE_USED (block) = 1;
1508
1509   /* Take care of compiler's internal binding structures.  */
1510   if (tmp == 2)
1511     {
1512       tree scope_stmts;
1513
1514       scope_stmts
1515         = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1516       if (block)
1517         {
1518           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1519           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1520         }
1521
1522       block = poplevel (keep, reverse, functionbody);
1523     }
1524
1525   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1526 }
1527
1528 /* Delete the node BLOCK from the current binding level.
1529    This is used for the block inside a stmt expr ({...})
1530    so that the block can be reinserted where appropriate.  */
1531
1532 void
1533 delete_block (tree block)
1534 {
1535   tree t;
1536   if (current_binding_level->blocks == block)
1537     current_binding_level->blocks = TREE_CHAIN (block);
1538   for (t = current_binding_level->blocks; t;)
1539     {
1540       if (TREE_CHAIN (t) == block)
1541         TREE_CHAIN (t) = TREE_CHAIN (block);
1542       else
1543         t = TREE_CHAIN (t);
1544     }
1545   TREE_CHAIN (block) = NULL_TREE;
1546   /* Clear TREE_USED which is always set by poplevel.
1547      The flag is set again if insert_block is called.  */
1548   TREE_USED (block) = 0;
1549 }
1550
1551 /* Insert BLOCK at the end of the list of subblocks of the
1552    current binding level.  This is used when a BIND_EXPR is expanded,
1553    to handle the BLOCK node inside the BIND_EXPR.  */
1554
1555 void
1556 insert_block (tree block)
1557 {
1558   TREE_USED (block) = 1;
1559   current_binding_level->blocks
1560     = chainon (current_binding_level->blocks, block);
1561 }
1562
1563 /* Set the BLOCK node for the innermost scope
1564    (the one we are currently in).  */
1565
1566 void
1567 set_block (tree block ATTRIBUTE_UNUSED )
1568 {
1569   /* The RTL expansion machinery requires us to provide this callback,
1570      but it is not applicable in function-at-a-time mode.  */
1571   my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1572 }
1573
1574 /* Do a pushlevel for class declarations.  */
1575
1576 void
1577 pushlevel_class (void)
1578 {
1579   if (ENABLE_SCOPE_CHECKING)
1580     is_class_level = 1;
1581
1582   push_binding_level (make_cxx_scope (false, 0));
1583
1584   class_binding_level = current_binding_level;
1585   class_binding_level->parm_flag = 2;
1586   class_binding_level->this_entity = current_class_type;
1587 }
1588
1589 /* ...and a poplevel for class declarations.  */
1590
1591 void
1592 poplevel_class (void)
1593 {
1594   register struct cp_binding_level *level = class_binding_level;
1595   tree shadowed;
1596
1597   timevar_push (TV_NAME_LOOKUP);
1598   my_friendly_assert (level != 0, 354);
1599
1600   /* If we're leaving a toplevel class, don't bother to do the setting
1601      of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1602      shouldn't even be used when current_class_type isn't set, and second,
1603      if we don't touch it here, we're able to use the cache effect if the
1604      next time we're entering a class scope, it is the same class.  */
1605   if (current_class_depth != 1)
1606     {
1607       struct cp_binding_level* b;
1608
1609       /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1610       for (shadowed = level->class_shadowed;
1611            shadowed;
1612            shadowed = TREE_CHAIN (shadowed))
1613         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1614
1615       /* Find the next enclosing class, and recreate
1616          IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1617       b = level->level_chain;
1618       while (b && b->parm_flag != 2)
1619         b = b->level_chain;
1620
1621       if (b)
1622         for (shadowed = b->class_shadowed;
1623              shadowed;
1624              shadowed = TREE_CHAIN (shadowed))
1625           {
1626             cxx_binding *binding;
1627             
1628             binding = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1629             while (binding && BINDING_SCOPE (binding) != b)
1630               binding = binding->previous;
1631
1632             if (binding)
1633               IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1634                 = BINDING_VALUE (binding);
1635           }
1636     }
1637   else
1638     /* Remember to save what IDENTIFIER's were bound in this scope so we
1639        can recover from cache misses.  */
1640     {
1641       previous_class_type = current_class_type;
1642       previous_class_values = class_binding_level->class_shadowed;
1643     }
1644   for (shadowed = level->type_shadowed;
1645        shadowed;
1646        shadowed = TREE_CHAIN (shadowed))
1647     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1648
1649   /* Remove the bindings for all of the class-level declarations.  */
1650   for (shadowed = level->class_shadowed;
1651        shadowed;
1652        shadowed = TREE_CHAIN (shadowed))
1653     pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1654
1655   /* Now, pop out of the binding level which we created up in the
1656      `pushlevel_class' routine.  */
1657   if (ENABLE_SCOPE_CHECKING)
1658     is_class_level = 1;
1659
1660   pop_binding_level ();
1661   timevar_pop (TV_NAME_LOOKUP);
1662 }
1663
1664 /* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1665    for any names in enclosing classes.  */
1666
1667 void
1668 clear_identifier_class_values (void)
1669 {
1670   tree t;
1671
1672   if (!class_binding_level)
1673     return;
1674
1675   for (t = class_binding_level->class_shadowed;
1676        t;
1677        t = TREE_CHAIN (t))
1678     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1679 }
1680
1681 /* Returns nonzero if T is a virtual function table.  */
1682
1683 int
1684 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1685 {
1686   return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1687 }
1688
1689 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1690    functions.  */
1691
1692 int
1693 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1694 {
1695   return (TREE_CODE (t) == TYPE_DECL
1696           && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1697           && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1698 }
1699
1700 /* Return the declarations that are members of the namespace NS.  */
1701
1702 tree
1703 cp_namespace_decls (tree ns)
1704 {
1705   return NAMESPACE_LEVEL (ns)->names;
1706 }
1707
1708 struct walk_globals_data {
1709   walk_globals_pred p;
1710   walk_globals_fn f;
1711   void *data;
1712 };
1713
1714 /* Walk the vtable declarations in NAMESPACE.  Whenever one is found
1715    for which P returns nonzero, call F with its address.  If any call
1716    to F returns a nonzero value, return a nonzero value.  */
1717
1718 static int
1719 walk_vtables_r (tree namespace, void* data)
1720 {
1721   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1722   walk_globals_fn f = wgd->f;
1723   void *d = wgd->data;
1724   tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1725   int result = 0;
1726
1727   for (; decl ; decl = TREE_CHAIN (decl))
1728     result |= (*f) (&decl, d);
1729
1730   return result;
1731 }
1732
1733 /* Walk the vtable declarations.  Whenever one is found for which P
1734    returns nonzero, call F with its address.  If any call to F
1735    returns a nonzero value, return a nonzero value.  */
1736 bool
1737 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1738 {    
1739   struct walk_globals_data wgd;
1740   wgd.p = p;    
1741   wgd.f = f;
1742   wgd.data = data;
1743
1744   return walk_namespaces (walk_vtables_r, &wgd);
1745 }
1746
1747 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1748    itself, calling F for each.  The DATA is passed to F as well.  */
1749
1750 static int
1751 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1752 {
1753   int result = 0;
1754   tree current = NAMESPACE_LEVEL (namespace)->namespaces;     
1755
1756   result |= (*f) (namespace, data);
1757
1758   for (; current; current = TREE_CHAIN (current))
1759     result |= walk_namespaces_r (current, f, data);
1760
1761   return result;
1762 }
1763
1764 /* Walk all the namespaces, calling F for each.  The DATA is passed to
1765    F as well.  */
1766
1767 int
1768 walk_namespaces (walk_namespaces_fn f, void* data)
1769 {
1770   return walk_namespaces_r (global_namespace, f, data);
1771 }
1772
1773 /* Walk the global declarations in NAMESPACE.  Whenever one is found
1774    for which P returns nonzero, call F with its address.  If any call
1775    to F returns a nonzero value, return a nonzero value.  */
1776
1777 static int
1778 walk_globals_r (tree namespace, void* data)
1779 {
1780   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1781   walk_globals_pred p = wgd->p;
1782   walk_globals_fn f = wgd->f;
1783   void *d = wgd->data;
1784   tree *t;
1785   int result = 0;
1786
1787   t = &NAMESPACE_LEVEL (namespace)->names;
1788
1789   while (*t)
1790     {
1791       tree glbl = *t;
1792
1793       if ((*p) (glbl, d))
1794         result |= (*f) (t, d);
1795
1796       /* If F changed *T, then *T still points at the next item to
1797          examine.  */
1798       if (*t == glbl)
1799         t = &TREE_CHAIN (*t);
1800     }
1801
1802   return result;
1803 }
1804
1805 /* Walk the global declarations.  Whenever one is found for which P
1806    returns true, call F with its address.  If any call to F
1807    returns true, return true.  */
1808
1809 bool
1810 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1811 {
1812   struct walk_globals_data wgd;
1813   wgd.p = p;
1814   wgd.f = f;
1815   wgd.data = data;
1816
1817   return walk_namespaces (walk_globals_r, &wgd);
1818 }
1819
1820 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1821    DATA is non-NULL, this is the last time we will call
1822    wrapup_global_declarations for this NAMESPACE.  */
1823
1824 int
1825 wrapup_globals_for_namespace (tree namespace, void* data)
1826 {
1827   struct cp_binding_level *level = NAMESPACE_LEVEL (namespace);
1828   varray_type statics = level->static_decls;
1829   tree *vec = &VARRAY_TREE (statics, 0);
1830   int len = VARRAY_ACTIVE_SIZE (statics);
1831   int last_time = (data != 0);
1832
1833   if (last_time)
1834     {
1835       check_global_declarations (vec, len);
1836       return 0;
1837     }
1838
1839   /* Write out any globals that need to be output.  */
1840   return wrapup_global_declarations (vec, len);
1841 }
1842
1843 \f
1844 /* For debugging.  */
1845 static int no_print_functions = 0;
1846 static int no_print_builtins = 0;
1847
1848 /* Called from print_binding_level through binding_table_foreach to
1849    print the content of binding ENTRY.  DATA is a pointer to line offset
1850    marker.  */
1851 static void
1852 bt_print_entry (binding_entry entry, void *data)
1853 {
1854   int *p = (int *) data;
1855   int len;
1856
1857   if (entry->name == NULL)
1858     len = 3;
1859   else if (entry->name == TYPE_IDENTIFIER (entry->type))
1860     len = 2;
1861   else
1862     len = 4;
1863     len = 4;
1864
1865   *p += len;
1866
1867   if (*p > 5)
1868     {
1869       fprintf (stderr, "\n\t");
1870       *p = len;
1871     }
1872   if (entry->name == NULL)
1873     {
1874       print_node_brief (stderr, "<unnamed-typedef", entry->type, 0);
1875       fprintf (stderr, ">");
1876     }
1877   else if (entry->name == TYPE_IDENTIFIER (entry->type))
1878     print_node_brief (stderr, "", entry->type, 0);
1879   else
1880     {
1881       print_node_brief (stderr, "<typedef", entry->name, 0);
1882       print_node_brief (stderr, "", entry->type, 0);
1883       fprintf (stderr, ">");
1884     }
1885 }
1886
1887 void
1888 print_binding_level (struct cp_binding_level* lvl)
1889 {
1890   tree t;
1891   int i = 0, len;
1892   fprintf (stderr, " blocks=");
1893   fprintf (stderr, HOST_PTR_PRINTF, (void *) lvl->blocks);
1894   if (lvl->tag_transparent)
1895     fprintf (stderr, " tag-transparent");
1896   if (lvl->more_cleanups_ok)
1897     fprintf (stderr, " more-cleanups-ok");
1898   if (lvl->have_cleanups)
1899     fprintf (stderr, " have-cleanups");
1900   fprintf (stderr, "\n");
1901   if (lvl->names)
1902     {
1903       fprintf (stderr, " names:\t");
1904       /* We can probably fit 3 names to a line?  */
1905       for (t = lvl->names; t; t = TREE_CHAIN (t))
1906         {
1907           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1908             continue;
1909           if (no_print_builtins
1910               && (TREE_CODE (t) == TYPE_DECL)
1911               && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1912             continue;
1913
1914           /* Function decls tend to have longer names.  */
1915           if (TREE_CODE (t) == FUNCTION_DECL)
1916             len = 3;
1917           else
1918             len = 2;
1919           i += len;
1920           if (i > 6)
1921             {
1922               fprintf (stderr, "\n\t");
1923               i = len;
1924             }
1925           print_node_brief (stderr, "", t, 0);
1926           if (t == error_mark_node)
1927             break;
1928         }
1929       if (i)
1930         fprintf (stderr, "\n");
1931     }
1932   if (lvl->type_decls)
1933     {
1934       fprintf (stderr, " tags:\t");
1935       i = 0;
1936       binding_table_foreach (lvl->type_decls, bt_print_entry, &i);
1937       if (i)
1938         fprintf (stderr, "\n");
1939     }
1940   if (lvl->class_shadowed)
1941     {
1942       fprintf (stderr, " class-shadowed:");
1943       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1944         {
1945           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1946         }
1947       fprintf (stderr, "\n");
1948     }
1949   if (lvl->type_shadowed)
1950     {
1951       fprintf (stderr, " type-shadowed:");
1952       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1953         {
1954           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1955         }
1956       fprintf (stderr, "\n");
1957     }
1958 }
1959
1960 void
1961 print_other_binding_stack (struct cp_binding_level *stack)
1962 {
1963   struct cp_binding_level *level;
1964   for (level = stack; !global_scope_p (level); level = level->level_chain)
1965     {
1966       fprintf (stderr, "binding level ");
1967       fprintf (stderr, HOST_PTR_PRINTF, (void *) level);
1968       fprintf (stderr, "\n");
1969       print_binding_level (level);
1970     }
1971 }
1972
1973 void
1974 print_binding_stack (void)
1975 {
1976   struct cp_binding_level *b;
1977   fprintf (stderr, "current_binding_level=");
1978   fprintf (stderr, HOST_PTR_PRINTF, (void *) current_binding_level);
1979   fprintf (stderr, "\nclass_binding_level=");
1980   fprintf (stderr, HOST_PTR_PRINTF, (void *) class_binding_level);
1981   fprintf (stderr, "\nNAMESPACE_LEVEL (global_namespace)=");
1982   fprintf (stderr, HOST_PTR_PRINTF,
1983            (void *) NAMESPACE_LEVEL (global_namespace));
1984   fprintf (stderr, "\n");
1985   if (class_binding_level)
1986     {
1987       for (b = class_binding_level; b; b = b->level_chain)
1988         if (b == current_binding_level)
1989           break;
1990       if (b)
1991         b = class_binding_level;
1992       else
1993         b = current_binding_level;
1994     }
1995   else
1996     b = current_binding_level;
1997   print_other_binding_stack (b);
1998   fprintf (stderr, "global:\n");
1999   print_binding_level (NAMESPACE_LEVEL (global_namespace));
2000 }
2001
2002 /* Namespace binding access routines: The namespace_bindings field of
2003    the identifier is polymorphic, with three possible values:
2004    NULL_TREE, a list of "cxx_binding"s.  */
2005
2006
2007 /* Push the initial binding contour of NAMESPACE-scope.  Any subsequent
2008    push of NS is actually a resume.  */
2009 static void
2010 initial_push_namespace_scope (tree ns)
2011 {
2012   tree name = DECL_NAME (ns);
2013   cxx_scope *scope;
2014
2015   pushlevel (0);
2016   scope = current_binding_level;
2017   scope->namespace_p = true;
2018   scope->type_decls = binding_table_new (name == std_identifier
2019                                          ? NAMESPACE_STD_HT_SIZE
2020                                          : (name == global_scope_name
2021                                             ? GLOBAL_SCOPE_HT_SIZE
2022                                             : NAMESPACE_ORDINARY_HT_SIZE));
2023   VARRAY_TREE_INIT (scope->static_decls,
2024                     name == std_identifier || name == global_scope_name
2025                     ? 200 : 10,
2026                     "Static declarations");
2027   scope->this_entity = ns;
2028   NAMESPACE_LEVEL (ns) = scope;
2029 }
2030
2031 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2032    select a name that is unique to this compilation unit.  */
2033
2034 void
2035 push_namespace (tree name)
2036 {
2037   tree d = NULL_TREE;
2038   int need_new = 1;
2039   int implicit_use = 0;
2040
2041   timevar_push (TV_NAME_LOOKUP);
2042   
2043   /* We should not get here if the global_namespace is not yet constructed
2044      nor if NAME designates the global namespace:  The global scope is
2045      constructed elsewhere.  */
2046   my_friendly_assert (global_namespace != NULL && name != global_scope_name,
2047                       20030531);
2048
2049   if (!name)
2050     {
2051       /* The name of anonymous namespace is unique for the translation
2052          unit.  */
2053       if (!anonymous_namespace_name)
2054         anonymous_namespace_name = get_file_function_name ('N');
2055       name = anonymous_namespace_name;
2056       d = IDENTIFIER_NAMESPACE_VALUE (name);
2057       if (d)
2058         /* Reopening anonymous namespace.  */
2059         need_new = 0;
2060       implicit_use = 1;
2061     }
2062   else
2063     {
2064       /* Check whether this is an extended namespace definition.  */
2065       d = IDENTIFIER_NAMESPACE_VALUE (name);
2066       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2067         {
2068           need_new = 0;
2069           if (DECL_NAMESPACE_ALIAS (d))
2070             {
2071               error ("namespace alias `%D' not allowed here, assuming `%D'",
2072                         d, DECL_NAMESPACE_ALIAS (d));
2073               d = DECL_NAMESPACE_ALIAS (d);
2074             }
2075         }
2076     }
2077
2078   if (need_new)
2079     {
2080       /* Make a new namespace, binding the name to it.  */
2081       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2082       DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2083       d = pushdecl (d);
2084       initial_push_namespace_scope (d);
2085     }
2086   else
2087     resume_binding_level (NAMESPACE_LEVEL (d));
2088
2089   if (implicit_use)
2090     do_using_directive (d);
2091   /* Enter the name space.  */
2092   current_namespace = d;
2093
2094   timevar_pop (TV_NAME_LOOKUP);
2095 }
2096
2097 /* Pop from the scope of the current namespace.  */
2098
2099 void
2100 pop_namespace (void)
2101 {
2102   my_friendly_assert (current_namespace != global_namespace, 20010801);
2103   current_namespace = CP_DECL_CONTEXT (current_namespace);
2104   /* The binding level is not popped, as it might be re-opened later.  */
2105   suspend_binding_level ();
2106 }
2107
2108 /* Push into the scope of the namespace NS, even if it is deeply
2109    nested within another namespace.  */
2110
2111 void
2112 push_nested_namespace (tree ns)
2113 {
2114   if (ns == global_namespace)
2115     push_to_top_level ();
2116   else
2117     {
2118       push_nested_namespace (CP_DECL_CONTEXT (ns));
2119       push_namespace (DECL_NAME (ns));
2120     }
2121 }
2122
2123 /* Pop back from the scope of the namespace NS, which was previously
2124    entered with push_nested_namespace.  */
2125
2126 void
2127 pop_nested_namespace (tree ns)
2128 {
2129   timevar_push (TV_NAME_LOOKUP);
2130   while (ns != global_namespace)
2131     {
2132       pop_namespace ();
2133       ns = CP_DECL_CONTEXT (ns);
2134     }
2135
2136   pop_from_top_level ();
2137   timevar_pop (TV_NAME_LOOKUP);
2138 }
2139
2140 \f
2141 /* Allocate storage for saving a C++ binding.  */
2142 #define cxx_saved_binding_make() \
2143   (ggc_alloc (sizeof (cxx_saved_binding)))
2144
2145 struct cxx_saved_binding GTY(())
2146 {
2147   /* Link that chains saved C++ bindings for a given name into a stack.  */
2148   cxx_saved_binding *previous;
2149   /* The name of the current binding.  */
2150   tree identifier;
2151   /* The binding we're saving.  */
2152   cxx_binding *binding;
2153   tree class_value;
2154   tree real_type_value;
2155 };
2156
2157 /* Subroutines for reverting temporarily to top-level for instantiation
2158    of templates and such.  We actually need to clear out the class- and
2159    local-value slots of all identifiers, so that only the global values
2160    are at all visible.  Simply setting current_binding_level to the global
2161    scope isn't enough, because more binding levels may be pushed.  */
2162 struct saved_scope *scope_chain;
2163
2164 static cxx_saved_binding *
2165 store_bindings (tree names, cxx_saved_binding *old_bindings)
2166 {
2167   tree t;
2168   cxx_saved_binding *search_bindings = old_bindings;
2169
2170   timevar_push (TV_NAME_LOOKUP);
2171   for (t = names; t; t = TREE_CHAIN (t))
2172     {
2173       tree id;
2174       cxx_saved_binding *saved;
2175       cxx_saved_binding *t1;
2176
2177       if (TREE_CODE (t) == TREE_LIST)
2178         id = TREE_PURPOSE (t);
2179       else
2180         id = DECL_NAME (t);
2181
2182       if (!id
2183           /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2184              we have no IDENTIFIER_BINDING if we have left the class
2185              scope, but cached the class-level declarations.  */
2186           || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2187         continue;
2188
2189       for (t1 = search_bindings; t1; t1 = t1->previous)
2190         if (t1->identifier == id)
2191           goto skip_it;
2192
2193       my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2194       saved = cxx_saved_binding_make ();
2195       saved->previous = old_bindings;
2196       saved->identifier = id;
2197       saved->binding = IDENTIFIER_BINDING (id);
2198       saved->class_value = IDENTIFIER_CLASS_VALUE (id);;
2199       saved->real_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2200       IDENTIFIER_BINDING (id) = NULL;
2201       IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2202       old_bindings = saved;
2203     skip_it:
2204       ;
2205     }
2206   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2207 }
2208
2209 void
2210 maybe_push_to_top_level (int pseudo)
2211 {
2212   struct saved_scope *s;
2213   struct cp_binding_level *b;
2214   cxx_saved_binding *old_bindings;
2215   int need_pop;
2216
2217   timevar_push (TV_NAME_LOOKUP);
2218   s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2219
2220   b = scope_chain ? current_binding_level : 0;
2221
2222   /* If we're in the middle of some function, save our state.  */
2223   if (cfun)
2224     {
2225       need_pop = 1;
2226       push_function_context_to (NULL_TREE);
2227     }
2228   else
2229     need_pop = 0;
2230
2231   old_bindings = NULL;
2232   if (scope_chain && previous_class_type)
2233     old_bindings = store_bindings (previous_class_values, old_bindings);
2234
2235   /* Have to include the global scope, because class-scope decls
2236      aren't listed anywhere useful.  */
2237   for (; b; b = b->level_chain)
2238     {
2239       tree t;
2240
2241       /* Template IDs are inserted into the global level. If they were
2242          inserted into namespace level, finish_file wouldn't find them
2243          when doing pending instantiations. Therefore, don't stop at
2244          namespace level, but continue until :: .  */
2245       if (global_scope_p (b) || (pseudo && b->template_parms_p))
2246         break;
2247
2248       old_bindings = store_bindings (b->names, old_bindings);
2249       /* We also need to check class_shadowed to save class-level type
2250          bindings, since pushclass doesn't fill in b->names.  */
2251       if (b->parm_flag == 2)
2252         old_bindings = store_bindings (b->class_shadowed, old_bindings);
2253
2254       /* Unwind type-value slots back to top level.  */
2255       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2256         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2257     }
2258   s->prev = scope_chain;
2259   s->old_bindings = old_bindings;
2260   s->bindings = b;
2261   s->need_pop_function_context = need_pop;
2262   s->function_decl = current_function_decl;
2263   s->last_parms = last_function_parms;
2264
2265   scope_chain = s;
2266   current_function_decl = NULL_TREE;
2267   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2268   current_lang_name = lang_name_cplusplus;
2269   current_namespace = global_namespace;
2270   timevar_pop (TV_NAME_LOOKUP);
2271 }
2272
2273 void
2274 push_to_top_level (void)
2275 {
2276   maybe_push_to_top_level (0);
2277 }
2278
2279 void
2280 pop_from_top_level (void)
2281 {
2282   struct saved_scope *s = scope_chain;
2283   cxx_saved_binding *saved;
2284
2285   timevar_push (TV_NAME_LOOKUP); 
2286   /* Clear out class-level bindings cache.  */
2287   if (previous_class_type)
2288     invalidate_class_lookup_cache ();
2289
2290   current_lang_base = 0;
2291
2292   scope_chain = s->prev;
2293   for (saved = s->old_bindings; saved; saved = saved->previous)
2294     {
2295       tree id = saved->identifier;
2296
2297       IDENTIFIER_BINDING (id) = saved->binding;
2298       IDENTIFIER_CLASS_VALUE (id) = saved->class_value;
2299       SET_IDENTIFIER_TYPE_VALUE (id, saved->real_type_value);
2300     }
2301
2302   /* If we were in the middle of compiling a function, restore our
2303      state.  */
2304   if (s->need_pop_function_context)
2305     pop_function_context_from (NULL_TREE);
2306   current_function_decl = s->function_decl;
2307   last_function_parms = s->last_parms;
2308   timevar_pop (TV_NAME_LOOKUP);
2309 }
2310 \f
2311 /* Push a definition of struct, union or enum tag "name".
2312    into binding_level "b".   "type" should be the type node,
2313    We assume that the tag "name" is not already defined.
2314
2315    Note that the definition may really be just a forward reference.
2316    In that case, the TYPE_SIZE will be a NULL_TREE.
2317
2318    C++ gratuitously puts all these tags in the name space.  */
2319
2320 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2321    record the shadowed value for this binding contour.  TYPE is
2322    the type that ID maps to.  */
2323
2324 static void
2325 set_identifier_type_value_with_scope (tree id, 
2326                                       tree type, 
2327                                       struct cp_binding_level* b)
2328 {
2329   if (!b->namespace_p)
2330     {
2331       /* Shadow the marker, not the real thing, so that the marker
2332          gets restored later.  */
2333       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2334       b->type_shadowed
2335         = tree_cons (id, old_type_value, b->type_shadowed);
2336     }
2337   else
2338     {
2339       cxx_binding *binding =
2340          binding_for_name (NAMESPACE_LEVEL (current_namespace), id);
2341       BINDING_TYPE (binding) = type;
2342       /* Store marker instead of real type.  */
2343       type = global_type_node;
2344     }
2345   SET_IDENTIFIER_TYPE_VALUE (id, type);
2346 }
2347
2348 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2349
2350 void
2351 set_identifier_type_value (tree id, tree type)
2352 {
2353   set_identifier_type_value_with_scope (id, type, current_binding_level);
2354 }
2355
2356 /* Return the type associated with id.  */
2357
2358 tree
2359 identifier_type_value (tree id)
2360 {
2361   timevar_push (TV_NAME_LOOKUP);
2362   /* There is no type with that name, anywhere.  */
2363   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2364     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2365   /* This is not the type marker, but the real thing.  */
2366   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2367     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2368   /* Have to search for it. It must be on the global level, now.
2369      Ask lookup_name not to return non-types. */
2370   id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2371   if (id)
2372     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2373   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2374 }
2375
2376 /* Pop off extraneous binding levels left over due to syntax errors.
2377
2378    We don't pop past namespaces, as they might be valid.  */
2379
2380 void
2381 pop_everything (void)
2382 {
2383   if (ENABLE_SCOPE_CHECKING)
2384     verbatim ("XXX entering pop_everything ()\n");
2385   while (!toplevel_bindings_p ())
2386     {
2387       if (current_binding_level->parm_flag == 2)
2388         pop_nested_class ();
2389       else
2390         poplevel (0, 0, 0);
2391     }
2392   if (ENABLE_SCOPE_CHECKING)
2393     verbatim ("XXX leaving pop_everything ()\n");
2394 }
2395
2396 /* The type TYPE is being declared.  If it is a class template, or a
2397    specialization of a class template, do any processing required and
2398    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2399    being declared a friend.  B is the binding level at which this TYPE
2400    should be bound.
2401
2402    Returns the TYPE_DECL for TYPE, which may have been altered by this
2403    processing.  */
2404
2405 static tree
2406 maybe_process_template_type_declaration (tree type, 
2407                                          int globalize, 
2408                                          struct cp_binding_level* b)
2409 {
2410   tree decl = TYPE_NAME (type);
2411
2412   if (processing_template_parmlist)
2413     /* You can't declare a new template type in a template parameter
2414        list.  But, you can declare a non-template type:
2415
2416          template <class A*> struct S;
2417
2418        is a forward-declaration of `A'.  */
2419     ;
2420   else
2421     {
2422       maybe_check_template_type (type);
2423
2424       my_friendly_assert (IS_AGGR_TYPE (type)
2425                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2426
2427
2428       if (processing_template_decl)
2429         {
2430           /* This may change after the call to
2431              push_template_decl_real, but we want the original value.  */
2432           tree name = DECL_NAME (decl);
2433
2434           decl = push_template_decl_real (decl, globalize);
2435           /* If the current binding level is the binding level for the
2436              template parameters (see the comment in
2437              begin_template_parm_list) and the enclosing level is a class
2438              scope, and we're not looking at a friend, push the
2439              declaration of the member class into the class scope.  In the
2440              friend case, push_template_decl will already have put the
2441              friend into global scope, if appropriate.  */
2442           if (TREE_CODE (type) != ENUMERAL_TYPE
2443               && !globalize && b->template_parms_p
2444               && b->level_chain->parm_flag == 2)
2445             {
2446               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2447               /* Put this UDT in the table of UDTs for the class, since
2448                  that won't happen below because B is not the class
2449                  binding level, but is instead the pseudo-global level.  */
2450               if (b->level_chain->type_decls == NULL)
2451                 b->level_chain->type_decls =
2452                   binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2453               binding_table_insert (b->level_chain->type_decls, name, type);
2454               if (!COMPLETE_TYPE_P (current_class_type))
2455                 {
2456                   maybe_add_class_template_decl_list (current_class_type,
2457                                                       type, /*friend_p=*/0);
2458                   CLASSTYPE_NESTED_UTDS (current_class_type) =
2459                     b->level_chain->type_decls;
2460                 }
2461             }
2462         }
2463     }
2464
2465   return decl;
2466 }
2467
2468 /* In C++, you don't have to write `struct S' to refer to `S'; you
2469    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2470    if the user had written `typedef struct S S'.  Create and return
2471    the TYPE_DECL for TYPE.  */
2472
2473 tree
2474 create_implicit_typedef (tree name, tree type)
2475 {
2476   tree decl;
2477
2478   decl = build_decl (TYPE_DECL, name, type);
2479   DECL_ARTIFICIAL (decl) = 1;
2480   /* There are other implicit type declarations, like the one *within*
2481      a class that allows you to write `S::S'.  We must distinguish
2482      amongst these.  */
2483   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2484   TYPE_NAME (type) = decl;
2485
2486   return decl;
2487 }
2488
2489 /* Remember a local name for name-mangling purposes.  */
2490
2491 static void
2492 push_local_name (tree decl)
2493 {
2494   size_t i, nelts;
2495   tree t, name;
2496
2497   timevar_push (TV_NAME_LOOKUP);
2498   if (!local_names)
2499     VARRAY_TREE_INIT (local_names, 8, "local_names");
2500
2501   name = DECL_NAME (decl);
2502
2503   nelts = VARRAY_ACTIVE_SIZE (local_names);
2504   for (i = 0; i < nelts; i++)
2505     {
2506       t = VARRAY_TREE (local_names, i);
2507       if (DECL_NAME (t) == name)
2508         {
2509           if (!DECL_LANG_SPECIFIC (decl))
2510             retrofit_lang_decl (decl);
2511           DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2512           if (DECL_LANG_SPECIFIC (t))
2513             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2514           else
2515             DECL_DISCRIMINATOR (decl) = 1;
2516
2517           VARRAY_TREE (local_names, i) = decl;
2518           timevar_pop (TV_NAME_LOOKUP);
2519           return;
2520         }
2521     }
2522
2523   VARRAY_PUSH_TREE (local_names, decl);
2524   timevar_pop (TV_NAME_LOOKUP);
2525 }
2526
2527 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2528    Normally put it into the inner-most non-tag-transparent scope,
2529    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2530    The latter is needed for implicit declarations.  */
2531
2532 void
2533 pushtag (tree name, tree type, int globalize)
2534 {
2535   register struct cp_binding_level *b;
2536
2537   timevar_push (TV_NAME_LOOKUP);
2538   b = current_binding_level;
2539   while (b->tag_transparent
2540          || (b->parm_flag == 2
2541              && (globalize
2542                  /* We may be defining a new type in the initializer
2543                     of a static member variable. We allow this when
2544                     not pedantic, and it is particularly useful for
2545                     type punning via an anonymous union.  */
2546                  || COMPLETE_TYPE_P (b->this_entity))))
2547     b = b->level_chain;
2548
2549   if (b->type_decls == NULL)
2550     b->type_decls = binding_table_new (SCOPE_DEFAULT_HT_SIZE);
2551   binding_table_insert (b->type_decls, name, type);
2552
2553   if (name)
2554     {
2555       /* Do C++ gratuitous typedefing.  */
2556       if (IDENTIFIER_TYPE_VALUE (name) != type)
2557         {
2558           register tree d = NULL_TREE;
2559           int in_class = 0;
2560           tree context = TYPE_CONTEXT (type);
2561
2562           if (! context)
2563             {
2564               tree cs = current_scope ();
2565
2566               if (! globalize)
2567                 context = cs;
2568               else if (cs != NULL_TREE && TYPE_P (cs))
2569                 /* When declaring a friend class of a local class, we want
2570                    to inject the newly named class into the scope
2571                    containing the local class, not the namespace scope.  */
2572                 context = decl_function_context (get_type_decl (cs));
2573             }
2574           if (!context)
2575             context = current_namespace;
2576
2577           if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2578               || b->parm_flag == 2)
2579             in_class = 1;
2580
2581           if (current_lang_name == lang_name_java)
2582             TYPE_FOR_JAVA (type) = 1;
2583
2584           d = create_implicit_typedef (name, type);
2585           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2586           if (! in_class)
2587             set_identifier_type_value_with_scope (name, type, b);
2588
2589           d = maybe_process_template_type_declaration (type,
2590                                                        globalize, b);
2591
2592           if (b->parm_flag == 2)
2593             {
2594               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2595                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2596                    class.  But if it's a member template class, we
2597                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2598                    is done later.  */
2599                 finish_member_declaration (d);
2600               else
2601                 pushdecl_class_level (d);
2602             }
2603           else
2604             d = pushdecl_with_scope (d, b);
2605
2606           /* FIXME what if it gets a name from typedef?  */
2607           if (ANON_AGGRNAME_P (name))
2608             DECL_IGNORED_P (d) = 1;
2609
2610           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2611
2612           /* If this is a local class, keep track of it.  We need this
2613              information for name-mangling, and so that it is possible to find
2614              all function definitions in a translation unit in a convenient
2615              way.  (It's otherwise tricky to find a member function definition
2616              it's only pointed to from within a local class.)  */
2617           if (TYPE_CONTEXT (type)
2618               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2619               && !processing_template_decl)
2620             VARRAY_PUSH_TREE (local_classes, type);
2621         }
2622       if (b->parm_flag == 2)
2623         {
2624           if (!COMPLETE_TYPE_P (current_class_type))
2625             {
2626               maybe_add_class_template_decl_list (current_class_type,
2627                                                   type, /*friend_p=*/0);
2628               CLASSTYPE_NESTED_UTDS (current_class_type) = b->type_decls;
2629             }
2630         }
2631     }
2632
2633   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2634     /* Use the canonical TYPE_DECL for this node.  */
2635     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2636   else
2637     {
2638       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2639          will be the tagged type we just added to the current
2640          binding level.  This fake NULL-named TYPE_DECL node helps
2641          dwarfout.c to know when it needs to output a
2642          representation of a tagged type, and it also gives us a
2643          convenient place to record the "scope start" address for
2644          the tagged type.  */
2645
2646       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2647       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2648     }
2649   timevar_pop (TV_NAME_LOOKUP);
2650 }
2651
2652 /* Counter used to create anonymous type names.  */
2653
2654 static GTY(()) int anon_cnt;
2655
2656 /* Return an IDENTIFIER which can be used as a name for
2657    anonymous structs and unions.  */
2658
2659 tree
2660 make_anon_name (void)
2661 {
2662   char buf[32];
2663
2664   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2665   return get_identifier (buf);
2666 }
2667
2668 /* Clear the TREE_PURPOSE slot of UTDs which have anonymous typenames.
2669    This keeps dbxout from getting confused.  */
2670
2671 void
2672 clear_anon_tags (void)
2673 {
2674   register struct cp_binding_level *b;
2675   static int last_cnt = 0;
2676
2677   /* Fast out if no new anon names were declared.  */
2678   if (last_cnt == anon_cnt)
2679     return;
2680
2681   b = current_binding_level;
2682   while (b->tag_transparent)
2683     b = b->level_chain;
2684   if (b->type_decls != NULL)
2685     binding_table_remove_anonymous_types (b->type_decls);
2686   last_cnt = anon_cnt;
2687 }
2688 \f
2689 /* Subroutine of duplicate_decls: return truthvalue of whether
2690    or not types of these decls match.
2691
2692    For C++, we must compare the parameter list so that `int' can match
2693    `int&' in a parameter position, but `int&' is not confused with
2694    `const int&'.  */
2695
2696 int
2697 decls_match (tree newdecl, tree olddecl)
2698 {
2699   int types_match;
2700
2701   if (newdecl == olddecl)
2702     return 1;
2703
2704   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2705     /* If the two DECLs are not even the same kind of thing, we're not
2706        interested in their types.  */
2707     return 0;
2708
2709   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2710     {
2711       tree f1 = TREE_TYPE (newdecl);
2712       tree f2 = TREE_TYPE (olddecl);
2713       tree p1 = TYPE_ARG_TYPES (f1);
2714       tree p2 = TYPE_ARG_TYPES (f2);
2715
2716       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2717           && ! (DECL_EXTERN_C_P (newdecl)
2718                 && DECL_EXTERN_C_P (olddecl)))
2719         return 0;
2720
2721       if (TREE_CODE (f1) != TREE_CODE (f2))
2722         return 0;
2723
2724       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2725         {
2726           if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2727               && (DECL_BUILT_IN (olddecl)
2728 #ifndef NO_IMPLICIT_EXTERN_C
2729                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2730                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2731 #endif
2732               ))
2733             {
2734               types_match = self_promoting_args_p (p1);
2735               if (p1 == void_list_node)
2736                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2737             }
2738 #ifndef NO_IMPLICIT_EXTERN_C
2739           else if (p1 == NULL_TREE
2740                    && (DECL_EXTERN_C_P (olddecl)
2741                        && DECL_IN_SYSTEM_HEADER (olddecl)
2742                        && !DECL_CLASS_SCOPE_P (olddecl))
2743                    && (DECL_EXTERN_C_P (newdecl)
2744                        && DECL_IN_SYSTEM_HEADER (newdecl)
2745                        && !DECL_CLASS_SCOPE_P (newdecl)))
2746             {
2747               types_match = self_promoting_args_p (p2);
2748               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2749             }
2750 #endif
2751           else
2752             types_match = compparms (p1, p2);
2753         }
2754       else
2755         types_match = 0;
2756     }
2757   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2758     {
2759       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2760                                 DECL_TEMPLATE_PARMS (olddecl)))
2761         return 0;
2762
2763       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2764           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2765         return 0;
2766
2767       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2768         types_match = 1;
2769       else
2770         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2771                                    DECL_TEMPLATE_RESULT (newdecl));
2772     }
2773   else
2774     {
2775       if (TREE_TYPE (newdecl) == error_mark_node)
2776         types_match = TREE_TYPE (olddecl) == error_mark_node;
2777       else if (TREE_TYPE (olddecl) == NULL_TREE)
2778         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2779       else if (TREE_TYPE (newdecl) == NULL_TREE)
2780         types_match = 0;
2781       else
2782         types_match = comptypes (TREE_TYPE (newdecl),
2783                                  TREE_TYPE (olddecl),
2784                                  COMPARE_REDECLARATION);
2785     }
2786
2787   return types_match;
2788 }
2789
2790 /* If NEWDECL is `static' and an `extern' was seen previously,
2791    warn about it.  OLDDECL is the previous declaration.
2792
2793    Note that this does not apply to the C++ case of declaring
2794    a variable `extern const' and then later `const'.
2795
2796    Don't complain about built-in functions, since they are beyond
2797    the user's control.  */
2798
2799 static void
2800 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2801 {
2802   static const char *const explicit_extern_static_warning
2803     = "`%D' was declared `extern' and later `static'";
2804   static const char *const implicit_extern_static_warning
2805     = "`%D' was declared implicitly `extern' and later `static'";
2806
2807   tree name;
2808
2809   if (TREE_CODE (newdecl) == TYPE_DECL
2810       || TREE_CODE (newdecl) == TEMPLATE_DECL
2811       || TREE_CODE (newdecl) == CONST_DECL)
2812     return;
2813
2814   /* Don't get confused by static member functions; that's a different
2815      use of `static'.  */
2816   if (TREE_CODE (newdecl) == FUNCTION_DECL
2817       && DECL_STATIC_FUNCTION_P (newdecl))
2818     return;
2819
2820   /* If the old declaration was `static', or the new one isn't, then
2821      then everything is OK.  */
2822   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2823     return;
2824
2825   /* It's OK to declare a builtin function as `static'.  */
2826   if (TREE_CODE (olddecl) == FUNCTION_DECL
2827       && DECL_ARTIFICIAL (olddecl))
2828     return;
2829
2830   name = DECL_ASSEMBLER_NAME (newdecl);
2831   pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2832               ? implicit_extern_static_warning
2833               : explicit_extern_static_warning, newdecl);
2834   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2835 }
2836
2837 /* Handle when a new declaration NEWDECL has the same name as an old
2838    one OLDDECL in the same binding contour.  Prints an error message
2839    if appropriate.
2840
2841    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2842    Otherwise, return 0.  */
2843
2844 int
2845 duplicate_decls (tree newdecl, tree olddecl)
2846 {
2847   unsigned olddecl_uid = DECL_UID (olddecl);
2848   int olddecl_friend = 0, types_match = 0;
2849   int new_defines_function = 0;
2850
2851   if (newdecl == olddecl)
2852     return 1;
2853
2854   types_match = decls_match (newdecl, olddecl);
2855
2856   /* If either the type of the new decl or the type of the old decl is an
2857      error_mark_node, then that implies that we have already issued an
2858      error (earlier) for some bogus type specification, and in that case,
2859      it is rather pointless to harass the user with yet more error message
2860      about the same declaration, so just pretend the types match here.  */
2861   if (TREE_TYPE (newdecl) == error_mark_node
2862       || TREE_TYPE (olddecl) == error_mark_node)
2863     types_match = 1;
2864
2865   if (DECL_P (olddecl)
2866       && TREE_CODE (newdecl) == FUNCTION_DECL
2867       && TREE_CODE (olddecl) == FUNCTION_DECL
2868       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2869     {
2870       if (DECL_DECLARED_INLINE_P (newdecl)
2871           && DECL_UNINLINABLE (newdecl)
2872           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2873         /* Already warned elsewhere.  */;
2874       else if (DECL_DECLARED_INLINE_P (olddecl)
2875                && DECL_UNINLINABLE (olddecl)
2876                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2877         /* Already warned.  */;
2878       else if (DECL_DECLARED_INLINE_P (newdecl)
2879                && DECL_UNINLINABLE (olddecl)
2880                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2881         {
2882           warning ("%Hfunction '%D' redeclared as inline",
2883                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2884           warning ("%Hprevious declaration of '%D' with attribute noinline",
2885                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2886         }
2887       else if (DECL_DECLARED_INLINE_P (olddecl)
2888                && DECL_UNINLINABLE (newdecl)
2889                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2890         {
2891           warning ("%Hfunction '%D' redeclared with attribute noinline",
2892                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2893           warning ("%Hprevious declaration of '%D' was inline",
2894                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2895         }
2896     }
2897
2898   /* Check for redeclaration and other discrepancies.  */
2899   if (TREE_CODE (olddecl) == FUNCTION_DECL
2900       && DECL_ARTIFICIAL (olddecl))
2901     {
2902       if (TREE_CODE (newdecl) != FUNCTION_DECL)
2903         {
2904           /* Avoid warnings redeclaring anticipated built-ins.  */
2905           if (DECL_ANTICIPATED (olddecl))
2906             return 0;
2907
2908           /* If you declare a built-in or predefined function name as static,
2909              the old definition is overridden, but optionally warn this was a
2910              bad choice of name.  */
2911           if (! TREE_PUBLIC (newdecl))
2912             {
2913               if (warn_shadow)
2914                 warning ("shadowing %s function `%#D'",
2915                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2916                             olddecl);
2917               /* Discard the old built-in function.  */
2918               return 0;
2919             }
2920           /* If the built-in is not ansi, then programs can override
2921              it even globally without an error.  */
2922           else if (! DECL_BUILT_IN (olddecl))
2923             warning ("library function `%#D' redeclared as non-function `%#D'",
2924                         olddecl, newdecl);
2925           else
2926             {
2927               error ("declaration of `%#D'", newdecl);
2928               error ("conflicts with built-in declaration `%#D'",
2929                         olddecl);
2930             }
2931           return 0;
2932         }
2933       else if (!types_match)
2934         {
2935           /* Avoid warnings redeclaring anticipated built-ins.  */
2936           if (DECL_ANTICIPATED (olddecl))
2937             ;  /* Do nothing yet.  */
2938           else if ((DECL_EXTERN_C_P (newdecl)
2939                && DECL_EXTERN_C_P (olddecl))
2940               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
2941                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
2942             {
2943               /* A near match; override the builtin.  */
2944
2945               if (TREE_PUBLIC (newdecl))
2946                 {
2947                   warning ("new declaration `%#D'", newdecl);
2948                   warning ("ambiguates built-in declaration `%#D'",
2949                               olddecl);
2950                 }
2951               else if (warn_shadow)
2952                 warning ("shadowing %s function `%#D'",
2953                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
2954                             olddecl);
2955             }
2956           else
2957             /* Discard the old built-in function.  */
2958             return 0;
2959
2960           /* Replace the old RTL to avoid problems with inlining.  */
2961           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2962         }
2963       /* Even if the types match, prefer the new declarations type
2964          for anticipated built-ins, for exception lists, etc...  */
2965       else if (DECL_ANTICIPATED (olddecl))
2966         TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
2967
2968       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
2969         {
2970           /* If a builtin function is redeclared as `static', merge
2971              the declarations, but make the original one static.  */
2972           DECL_THIS_STATIC (olddecl) = 1;
2973           TREE_PUBLIC (olddecl) = 0;
2974
2975           /* Make the old declaration consistent with the new one so
2976              that all remnants of the builtin-ness of this function
2977              will be banished.  */
2978           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
2979           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
2980         }
2981     }
2982   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
2983     {
2984       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
2985            && TREE_CODE (newdecl) != TYPE_DECL
2986            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
2987                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
2988           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
2989               && TREE_CODE (olddecl) != TYPE_DECL
2990               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
2991                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
2992                         == TYPE_DECL))))
2993         {
2994           /* We do nothing special here, because C++ does such nasty
2995              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
2996              get shadowed, and know that if we need to find a TYPE_DECL
2997              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
2998              slot of the identifier.  */
2999           return 0;
3000         }
3001
3002       if ((TREE_CODE (newdecl) == FUNCTION_DECL
3003            && DECL_FUNCTION_TEMPLATE_P (olddecl))
3004           || (TREE_CODE (olddecl) == FUNCTION_DECL
3005               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3006         return 0;
3007
3008       error ("`%#D' redeclared as different kind of symbol", newdecl);
3009       if (TREE_CODE (olddecl) == TREE_LIST)
3010         olddecl = TREE_VALUE (olddecl);
3011       cp_error_at ("previous declaration of `%#D'", olddecl);
3012
3013       /* New decl is completely inconsistent with the old one =>
3014          tell caller to replace the old one.  */
3015
3016       return 0;
3017     }
3018   else if (!types_match)
3019     {
3020       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3021         /* These are certainly not duplicate declarations; they're
3022            from different scopes.  */
3023         return 0;
3024
3025       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3026         {
3027           /* The name of a class template may not be declared to refer to
3028              any other template, class, function, object, namespace, value,
3029              or type in the same scope.  */
3030           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3031               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3032             {
3033               error ("declaration of template `%#D'", newdecl);
3034               cp_error_at ("conflicts with previous declaration `%#D'",
3035                            olddecl);
3036             }
3037           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3038                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3039                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3040                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3041                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3042                                            DECL_TEMPLATE_PARMS (olddecl))
3043                    /* Template functions can be disambiguated by
3044                       return type.  */
3045                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3046                                    TREE_TYPE (TREE_TYPE (olddecl))))
3047             {
3048               error ("new declaration `%#D'", newdecl);
3049               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3050             }
3051           return 0;
3052         }
3053       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3054         {
3055           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3056             {
3057               error ("declaration of C function `%#D' conflicts with",
3058                         newdecl);
3059               cp_error_at ("previous declaration `%#D' here", olddecl);
3060             }
3061           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3062                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3063             {
3064               error ("new declaration `%#D'", newdecl);
3065               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3066             }
3067           else
3068             return 0;
3069         }
3070
3071       /* Already complained about this, so don't do so again.  */
3072       else if (current_class_type == NULL_TREE
3073           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3074         {
3075           error ("conflicting types for `%#D'", newdecl);
3076           cp_error_at ("previous declaration as `%#D'", olddecl);
3077         }
3078     }
3079   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3080             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3081                  && (!DECL_TEMPLATE_INFO (newdecl)
3082                      || (DECL_TI_TEMPLATE (newdecl)
3083                          != DECL_TI_TEMPLATE (olddecl))))
3084                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3085                     && (!DECL_TEMPLATE_INFO (olddecl)
3086                         || (DECL_TI_TEMPLATE (olddecl)
3087                             != DECL_TI_TEMPLATE (newdecl))))))
3088     /* It's OK to have a template specialization and a non-template
3089        with the same type, or to have specializations of two
3090        different templates with the same type.  Note that if one is a
3091        specialization, and the other is an instantiation of the same
3092        template, that we do not exit at this point.  That situation
3093        can occur if we instantiate a template class, and then
3094        specialize one of its methods.  This situation is valid, but
3095        the declarations must be merged in the usual way.  */
3096     return 0;
3097   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3098            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3099                 && !DECL_USE_TEMPLATE (newdecl))
3100                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3101                    && !DECL_USE_TEMPLATE (olddecl))))
3102     /* One of the declarations is a template instantiation, and the
3103        other is not a template at all.  That's OK.  */
3104     return 0;
3105   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3106            && DECL_NAMESPACE_ALIAS (newdecl)
3107            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3108     /* Redeclaration of namespace alias, ignore it.  */
3109     return 1;
3110   else
3111     {
3112       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3113       if (errmsg)
3114         {
3115           error (errmsg, newdecl);
3116           if (DECL_NAME (olddecl) != NULL_TREE)
3117             cp_error_at ((DECL_INITIAL (olddecl)
3118                           && namespace_bindings_p ())
3119                          ? "`%#D' previously defined here"
3120                          : "`%#D' previously declared here", olddecl);
3121           return 0;
3122         }
3123       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3124                && DECL_INITIAL (olddecl) != NULL_TREE
3125                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3126                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3127         {
3128           /* Prototype decl follows defn w/o prototype.  */
3129           cp_warning_at ("prototype for `%#D'", newdecl);
3130           cp_warning_at ("follows non-prototype definition here", olddecl);
3131         }
3132       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3133                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3134         {
3135           /* extern "C" int foo ();
3136              int foo () { bar (); }
3137              is OK.  */
3138           if (current_lang_depth () == 0)
3139             SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3140           else
3141             {
3142               cp_error_at ("previous declaration of `%#D' with %L linkage",
3143                            olddecl, DECL_LANGUAGE (olddecl));
3144               error ("conflicts with new declaration with %L linkage",
3145                         DECL_LANGUAGE (newdecl));
3146             }
3147         }
3148
3149       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3150         ;
3151       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3152         {
3153           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3154           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3155           int i = 1;
3156
3157           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3158             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3159
3160           for (; t1 && t1 != void_list_node;
3161                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3162             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3163               {
3164                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3165                                            TREE_PURPOSE (t2)))
3166                   {
3167                     pedwarn ("default argument given for parameter %d of `%#D'",
3168                              i, newdecl);
3169                     cp_pedwarn_at ("after previous specification in `%#D'",
3170                                    olddecl);
3171                   }
3172                 else
3173                   {
3174                     error ("default argument given for parameter %d of `%#D'",
3175                               i, newdecl);
3176                     cp_error_at ("after previous specification in `%#D'",
3177                                  olddecl);
3178                   }
3179               }
3180
3181           if (DECL_DECLARED_INLINE_P (newdecl) 
3182               && ! DECL_DECLARED_INLINE_P (olddecl)
3183               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3184             {
3185               warning ("`%#D' was used before it was declared inline",
3186                           newdecl);
3187               cp_warning_at ("previous non-inline declaration here",
3188                              olddecl);
3189             }
3190         }
3191     }
3192
3193   /* Do not merge an implicit typedef with an explicit one.  In:
3194
3195        class A;
3196        ...
3197        typedef class A A __attribute__ ((foo));
3198
3199      the attribute should apply only to the typedef.  */
3200   if (TREE_CODE (olddecl) == TYPE_DECL
3201       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3202           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3203     return 0;
3204
3205   /* If new decl is `static' and an `extern' was seen previously,
3206      warn about it.  */
3207   warn_extern_redeclared_static (newdecl, olddecl);
3208
3209   /* We have committed to returning 1 at this point.  */
3210   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3211     {
3212       /* Now that functions must hold information normally held
3213          by field decls, there is extra work to do so that
3214          declaration information does not get destroyed during
3215          definition.  */
3216       if (DECL_VINDEX (olddecl))
3217         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3218       if (DECL_CONTEXT (olddecl))
3219         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3220       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3221       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3222       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3223       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3224       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3225       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3226       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3227         SET_OVERLOADED_OPERATOR_CODE
3228           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3229       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3230
3231       /* Optionally warn about more than one declaration for the same
3232          name, but don't warn about a function declaration followed by a
3233          definition.  */
3234       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3235           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3236           /* Don't warn about extern decl followed by definition.  */
3237           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3238           /* Don't warn about friends, let add_friend take care of it.  */
3239           && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3240         {
3241           warning ("redundant redeclaration of `%D' in same scope", newdecl);
3242           cp_warning_at ("previous declaration of `%D'", olddecl);
3243         }
3244     }
3245
3246   /* Deal with C++: must preserve virtual function table size.  */
3247   if (TREE_CODE (olddecl) == TYPE_DECL)
3248     {
3249       register tree newtype = TREE_TYPE (newdecl);
3250       register tree oldtype = TREE_TYPE (olddecl);
3251
3252       if (newtype != error_mark_node && oldtype != error_mark_node
3253           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3254         CLASSTYPE_FRIEND_CLASSES (newtype)
3255           = CLASSTYPE_FRIEND_CLASSES (oldtype);
3256
3257       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3258     }
3259
3260   /* Copy all the DECL_... slots specified in the new decl
3261      except for any that we copy here from the old type.  */
3262   DECL_ATTRIBUTES (newdecl)
3263     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3264
3265   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3266     {
3267       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3268       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3269         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3270                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3271
3272       /* If the new declaration is a definition, update the file and
3273          line information on the declaration.  */
3274       if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3275           && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3276         {
3277           DECL_SOURCE_LOCATION (olddecl) 
3278             = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3279             = DECL_SOURCE_LOCATION (newdecl);
3280         }
3281
3282       return 1;
3283     }
3284
3285   if (types_match)
3286     {
3287       /* Automatically handles default parameters.  */
3288       tree oldtype = TREE_TYPE (olddecl);
3289       tree newtype;
3290
3291       /* Merge the data types specified in the two decls.  */
3292       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3293
3294       /* If merge_types produces a non-typedef type, just use the old type.  */
3295       if (TREE_CODE (newdecl) == TYPE_DECL
3296           && newtype == DECL_ORIGINAL_TYPE (newdecl))
3297         newtype = oldtype;
3298
3299       if (TREE_CODE (newdecl) == VAR_DECL)
3300         {
3301           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3302           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3303         }
3304
3305       /* Do this after calling `merge_types' so that default
3306          parameters don't confuse us.  */
3307       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3308           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3309               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3310         {
3311           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3312                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3313           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3314                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3315
3316           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3317               && DECL_SOURCE_LINE (olddecl) != 0
3318               && flag_exceptions
3319               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3320                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3321             {
3322               error ("declaration of `%F' throws different exceptions",
3323                         newdecl);
3324               cp_error_at ("than previous declaration `%F'", olddecl);
3325             }
3326         }
3327       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3328
3329       /* Lay the type out, unless already done.  */
3330       if (! same_type_p (newtype, oldtype)
3331           && TREE_TYPE (newdecl) != error_mark_node
3332           && !(processing_template_decl && uses_template_parms (newdecl)))
3333         layout_type (TREE_TYPE (newdecl));
3334
3335       if ((TREE_CODE (newdecl) == VAR_DECL
3336            || TREE_CODE (newdecl) == PARM_DECL
3337            || TREE_CODE (newdecl) == RESULT_DECL
3338            || TREE_CODE (newdecl) == FIELD_DECL
3339            || TREE_CODE (newdecl) == TYPE_DECL)
3340           && !(processing_template_decl && uses_template_parms (newdecl)))
3341         layout_decl (newdecl, 0);
3342
3343       /* Merge the type qualifiers.  */
3344       if (TREE_READONLY (newdecl))
3345         TREE_READONLY (olddecl) = 1;
3346       if (TREE_THIS_VOLATILE (newdecl))
3347         TREE_THIS_VOLATILE (olddecl) = 1;
3348
3349       /* Merge the initialization information.  */
3350       if (DECL_INITIAL (newdecl) == NULL_TREE
3351           && DECL_INITIAL (olddecl) != NULL_TREE)
3352         {
3353           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3354           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3355           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3356               && DECL_LANG_SPECIFIC (newdecl)
3357               && DECL_LANG_SPECIFIC (olddecl))
3358             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3359         }
3360
3361       /* Merge the section attribute.
3362          We want to issue an error if the sections conflict but that must be
3363          done later in decl_attributes since we are called before attributes
3364          are assigned.  */
3365       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3366         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3367
3368       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3369         {
3370           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3371             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3372           DECL_NO_LIMIT_STACK (newdecl) |= DECL_NO_LIMIT_STACK (olddecl);
3373           TREE_THIS_VOLATILE (newdecl) |= TREE_THIS_VOLATILE (olddecl);
3374           TREE_READONLY (newdecl) |= TREE_READONLY (olddecl);
3375           TREE_NOTHROW (newdecl) |= TREE_NOTHROW (olddecl);
3376           DECL_IS_MALLOC (newdecl) |= DECL_IS_MALLOC (olddecl);
3377           DECL_IS_PURE (newdecl) |= DECL_IS_PURE (olddecl);
3378           /* Keep the old RTL.  */
3379           COPY_DECL_RTL (olddecl, newdecl);
3380         }
3381       else if (TREE_CODE (newdecl) == VAR_DECL 
3382                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3383         {
3384           /* Keep the old RTL.  We cannot keep the old RTL if the old
3385              declaration was for an incomplete object and the new
3386              declaration is not since many attributes of the RTL will
3387              change.  */
3388           COPY_DECL_RTL (olddecl, newdecl);
3389         }
3390     }
3391   /* If cannot merge, then use the new type and qualifiers,
3392      and don't preserve the old rtl.  */
3393   else
3394     {
3395       /* Clean out any memory we had of the old declaration.  */
3396       tree oldstatic = value_member (olddecl, static_aggregates);
3397       if (oldstatic)
3398         TREE_VALUE (oldstatic) = error_mark_node;
3399
3400       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3401       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3402       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3403       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3404     }
3405
3406   /* Merge the storage class information.  */
3407   merge_weak (newdecl, olddecl);
3408
3409   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3410   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3411   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3412   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3413   if (! DECL_EXTERNAL (olddecl))
3414     DECL_EXTERNAL (newdecl) = 0;
3415
3416   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3417     {
3418       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3419       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3420       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3421       DECL_TEMPLATE_INSTANTIATED (newdecl)
3422         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3423       /* Don't really know how much of the language-specific
3424          values we should copy from old to new.  */
3425       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3426       DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 
3427         DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3428       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3429       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3430       DECL_INITIALIZED_IN_CLASS_P (newdecl)
3431         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3432       olddecl_friend = DECL_FRIEND_P (olddecl);
3433
3434       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3435       if (TREE_CODE (newdecl) == FUNCTION_DECL
3436           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3437         {
3438           DECL_BEFRIENDING_CLASSES (newdecl)
3439             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3440                        DECL_BEFRIENDING_CLASSES (olddecl));
3441           /* DECL_THUNKS is only valid for virtual functions,
3442              otherwise it is a DECL_FRIEND_CONTEXT.  */
3443           if (DECL_VIRTUAL_P (newdecl))
3444             DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3445         }
3446     }
3447
3448   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3449     {
3450       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3451           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3452         {
3453           /* If newdecl is not a specialization, then it is not a
3454              template-related function at all.  And that means that we
3455              should have exited above, returning 0.  */
3456           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3457                               0);
3458
3459           if (TREE_USED (olddecl))
3460             /* From [temp.expl.spec]:
3461
3462                If a template, a member template or the member of a class
3463                template is explicitly specialized then that
3464                specialization shall be declared before the first use of
3465                that specialization that would cause an implicit
3466                instantiation to take place, in every translation unit in
3467                which such a use occurs.  */
3468             error ("explicit specialization of %D after first use",
3469                       olddecl);
3470
3471           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3472
3473           /* [temp.expl.spec/14] We don't inline explicit specialization
3474              just because the primary template says so.  */
3475         }
3476       else
3477         {
3478           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3479             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3480
3481           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3482
3483           /* If either decl says `inline', this fn is inline, unless 
3484              its definition was passed already.  */
3485           if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3486             DECL_INLINE (olddecl) = 1;
3487           DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3488
3489           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3490             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3491         }
3492
3493       /* Preserve abstractness on cloned [cd]tors.  */
3494       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3495
3496       if (! types_match)
3497         {
3498           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3499           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3500           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3501         }
3502       if (! types_match || new_defines_function)
3503         {
3504           /* These need to be copied so that the names are available.
3505              Note that if the types do match, we'll preserve inline
3506              info and other bits, but if not, we won't.  */
3507           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3508           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3509         }
3510       if (new_defines_function)
3511         /* If defining a function declared with other language
3512            linkage, use the previously declared language linkage.  */
3513         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3514       else if (types_match)
3515         {
3516           /* If redeclaring a builtin function, and not a definition,
3517              it stays built in.  */
3518           if (DECL_BUILT_IN (olddecl))
3519             {
3520               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3521               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3522               /* If we're keeping the built-in definition, keep the rtl,
3523                  regardless of declaration matches.  */
3524               SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3525             }
3526           else
3527             DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3528
3529           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3530           /* Don't clear out the arguments if we're redefining a function.  */
3531           if (DECL_ARGUMENTS (olddecl))
3532             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3533         }
3534     }
3535   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3536     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3537
3538   /* Now preserve various other info from the definition.  */
3539   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3540   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3541   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3542   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3543
3544   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3545     {
3546       int function_size;
3547
3548       function_size = sizeof (struct tree_decl);
3549
3550       memcpy ((char *) olddecl + sizeof (struct tree_common),
3551               (char *) newdecl + sizeof (struct tree_common),
3552               function_size - sizeof (struct tree_common));
3553
3554       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3555         {
3556           /* If newdecl is a template instantiation, it is possible that
3557              the following sequence of events has occurred:
3558
3559              o A friend function was declared in a class template.  The
3560              class template was instantiated.
3561
3562              o The instantiation of the friend declaration was
3563              recorded on the instantiation list, and is newdecl.
3564
3565              o Later, however, instantiate_class_template called pushdecl
3566              on the newdecl to perform name injection.  But, pushdecl in
3567              turn called duplicate_decls when it discovered that another
3568              declaration of a global function with the same name already
3569              existed.
3570
3571              o Here, in duplicate_decls, we decided to clobber newdecl.
3572
3573              If we're going to do that, we'd better make sure that
3574              olddecl, and not newdecl, is on the list of
3575              instantiations so that if we try to do the instantiation
3576              again we won't get the clobbered declaration.  */
3577
3578           tree tmpl = DECL_TI_TEMPLATE (newdecl);
3579           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3580
3581           for (; decls; decls = TREE_CHAIN (decls))
3582             if (TREE_VALUE (decls) == newdecl)
3583               TREE_VALUE (decls) = olddecl;
3584         }
3585     }
3586   else
3587     {
3588       memcpy ((char *) olddecl + sizeof (struct tree_common),
3589               (char *) newdecl + sizeof (struct tree_common),
3590               sizeof (struct tree_decl) - sizeof (struct tree_common)
3591               + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3592     }
3593
3594   DECL_UID (olddecl) = olddecl_uid;
3595   if (olddecl_friend)
3596     DECL_FRIEND_P (olddecl) = 1;
3597
3598   /* NEWDECL contains the merged attribute lists.
3599      Update OLDDECL to be the same.  */
3600   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3601
3602   /* If OLDDECL had its DECL_RTL instantiated, re-invoke make_decl_rtl
3603     so that encode_section_info has a chance to look at the new decl
3604     flags and attributes.  */
3605   if (DECL_RTL_SET_P (olddecl) 
3606       && (TREE_CODE (olddecl) == FUNCTION_DECL
3607           || (TREE_CODE (olddecl) == VAR_DECL
3608               && TREE_STATIC (olddecl))))
3609     make_decl_rtl (olddecl, NULL);
3610
3611   return 1;
3612 }
3613
3614 /* Record a decl-node X as belonging to the current lexical scope.
3615    Check for errors (such as an incompatible declaration for the same
3616    name already seen in the same scope).
3617
3618    Returns either X or an old decl for the same name.
3619    If an old decl is returned, it may have been smashed
3620    to agree with what X says.  */
3621
3622 tree
3623 pushdecl (tree x)
3624 {
3625   register tree t;
3626   register tree name;
3627   int need_new_binding;
3628
3629   timevar_push (TV_NAME_LOOKUP);
3630   /* We shouldn't be calling pushdecl when we're generating RTL for a
3631      function that we already did semantic analysis on previously.  */
3632   my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3633                       19990913);
3634
3635   need_new_binding = 1;
3636
3637   if (DECL_TEMPLATE_PARM_P (x))
3638     /* Template parameters have no context; they are not X::T even
3639        when declared within a class or namespace.  */
3640     ;
3641   else
3642     {
3643       if (current_function_decl && x != current_function_decl
3644           /* A local declaration for a function doesn't constitute
3645              nesting.  */
3646           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3647           /* A local declaration for an `extern' variable is in the
3648              scope of the current namespace, not the current
3649              function.  */
3650           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3651           && !DECL_CONTEXT (x))
3652         DECL_CONTEXT (x) = current_function_decl;
3653
3654       /* If this is the declaration for a namespace-scope function,
3655          but the declaration itself is in a local scope, mark the
3656          declaration.  */
3657       if (TREE_CODE (x) == FUNCTION_DECL
3658           && DECL_NAMESPACE_SCOPE_P (x)
3659           && current_function_decl
3660           && x != current_function_decl)
3661         DECL_LOCAL_FUNCTION_P (x) = 1;
3662     }
3663
3664   name = DECL_NAME (x);
3665   if (name)
3666     {
3667       int different_binding_level = 0;
3668
3669       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3670         name = TREE_OPERAND (name, 0);
3671
3672       /* In case this decl was explicitly namespace-qualified, look it
3673          up in its namespace context.  */
3674       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3675           && namespace_bindings_p ())
3676         t = namespace_binding (name, DECL_CONTEXT (x));
3677       else
3678         t = lookup_name_current_level (name);
3679
3680       /* [basic.link] If there is a visible declaration of an entity
3681          with linkage having the same name and type, ignoring entities
3682          declared outside the innermost enclosing namespace scope, the
3683          block scope declaration declares that same entity and
3684          receives the linkage of the previous declaration.  */
3685       if (! t && current_function_decl && x != current_function_decl
3686           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3687           && DECL_EXTERNAL (x))
3688         {
3689           /* Look in block scope.  */
3690           t = IDENTIFIER_VALUE (name);
3691           /* Or in the innermost namespace.  */
3692           if (! t)
3693             t = namespace_binding (name, DECL_CONTEXT (x));
3694           /* Does it have linkage?  Note that if this isn't a DECL, it's an
3695              OVERLOAD, which is OK.  */
3696           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3697             t = NULL_TREE;
3698           if (t)
3699             different_binding_level = 1;
3700         }
3701
3702       /* If we are declaring a function, and the result of name-lookup
3703          was an OVERLOAD, look for an overloaded instance that is
3704          actually the same as the function we are declaring.  (If
3705          there is one, we have to merge our declaration with the
3706          previous declaration.)  */
3707       if (t && TREE_CODE (t) == OVERLOAD)
3708         {
3709           tree match;
3710
3711           if (TREE_CODE (x) == FUNCTION_DECL)
3712             for (match = t; match; match = OVL_NEXT (match))
3713               {
3714                 if (decls_match (OVL_CURRENT (match), x))
3715                   break;
3716               }
3717           else
3718             /* Just choose one.  */
3719             match = t;
3720
3721           if (match)
3722             t = OVL_CURRENT (match);
3723           else
3724             t = NULL_TREE;
3725         }
3726
3727       if (t == error_mark_node)
3728         {
3729           /* error_mark_node is 0 for a while during initialization!  */
3730           t = NULL_TREE;
3731           cp_error_at ("`%#D' used prior to declaration", x);
3732         }
3733       else if (t != NULL_TREE)
3734         {
3735           if (different_binding_level)
3736             {
3737               if (decls_match (x, t))
3738                 /* The standard only says that the local extern
3739                    inherits linkage from the previous decl; in
3740                    particular, default args are not shared.  It would
3741                    be nice to propagate inlining info, though.  FIXME.  */
3742                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3743             }
3744           else if (TREE_CODE (t) == PARM_DECL)
3745             {
3746               if (DECL_CONTEXT (t) == NULL_TREE)
3747                 /* This is probaby caused by too many errors, but calling
3748                    abort will say that if errors have occurred.  */
3749                 abort ();
3750
3751               /* Check for duplicate params.  */
3752               if (duplicate_decls (x, t))
3753                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3754             }
3755           else if ((DECL_EXTERN_C_FUNCTION_P (x)
3756                     || DECL_FUNCTION_TEMPLATE_P (x))
3757                    && is_overloaded_fn (t))
3758             /* Don't do anything just yet.  */;
3759           else if (t == wchar_decl_node)
3760             {
3761               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3762                 pedwarn ("redeclaration of `wchar_t' as `%T'",
3763                             TREE_TYPE (x));
3764
3765               /* Throw away the redeclaration.  */
3766               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3767             }
3768           else if (TREE_CODE (t) != TREE_CODE (x))
3769             {
3770               if (duplicate_decls (x, t))
3771                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3772             }
3773           else if (duplicate_decls (x, t))
3774             {
3775               if (TREE_CODE (t) == TYPE_DECL)
3776                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3777               else if (TREE_CODE (t) == FUNCTION_DECL)
3778                 check_default_args (t);
3779
3780               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3781             }
3782           else if (DECL_MAIN_P (x))
3783             {
3784               /* A redeclaration of main, but not a duplicate of the
3785                  previous one.
3786
3787                  [basic.start.main]
3788
3789                  This function shall not be overloaded.  */
3790               cp_error_at ("invalid redeclaration of `%D'", t);
3791               error ("as `%D'", x);
3792               /* We don't try to push this declaration since that
3793                  causes a crash.  */
3794               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3795             }
3796         }
3797
3798       check_template_shadow (x);
3799
3800       /* If this is a function conjured up by the backend, massage it
3801          so it looks friendly.  */
3802       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3803         {
3804           retrofit_lang_decl (x);
3805           SET_DECL_LANGUAGE (x, lang_c);
3806         }
3807
3808       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3809         {
3810           t = push_overloaded_decl (x, PUSH_LOCAL);
3811           if (t != x)
3812             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3813           if (!namespace_bindings_p ())
3814             /* We do not need to create a binding for this name;
3815                push_overloaded_decl will have already done so if
3816                necessary.  */
3817             need_new_binding = 0;
3818         }
3819       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3820         {
3821           t = push_overloaded_decl (x, PUSH_GLOBAL);
3822           if (t == x)
3823             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3824           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3825         }
3826
3827       /* If declaring a type as a typedef, copy the type (unless we're
3828          at line 0), and install this TYPE_DECL as the new type's typedef
3829          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
3830       if (TREE_CODE (x) == TYPE_DECL)
3831         {
3832           tree type = TREE_TYPE (x);
3833           if (DECL_SOURCE_LINE (x) == 0)
3834             {
3835               if (TYPE_NAME (type) == 0)
3836                 TYPE_NAME (type) = x;
3837             }
3838           else if (type != error_mark_node && TYPE_NAME (type) != x
3839                    /* We don't want to copy the type when all we're
3840                       doing is making a TYPE_DECL for the purposes of
3841                       inlining.  */
3842                    && (!TYPE_NAME (type)
3843                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3844             {
3845               DECL_ORIGINAL_TYPE (x) = type;
3846               type = build_type_copy (type);
3847               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3848               TYPE_NAME (type) = x;
3849               TREE_TYPE (x) = type;
3850             }
3851
3852           if (type != error_mark_node
3853               && TYPE_NAME (type)
3854               && TYPE_IDENTIFIER (type))
3855             set_identifier_type_value_with_scope (DECL_NAME (x), type,
3856                                                   current_binding_level);
3857
3858         }
3859
3860       /* Multiple external decls of the same identifier ought to match.
3861
3862          We get warnings about inline functions where they are defined.
3863          We get warnings about other functions from push_overloaded_decl.
3864
3865          Avoid duplicate warnings where they are used.  */
3866       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3867         {
3868           tree decl;
3869
3870           decl = IDENTIFIER_NAMESPACE_VALUE (name);
3871           if (decl && TREE_CODE (decl) == OVERLOAD)
3872             decl = OVL_FUNCTION (decl);
3873
3874           if (decl && decl != error_mark_node
3875               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3876               /* If different sort of thing, we already gave an error.  */
3877               && TREE_CODE (decl) == TREE_CODE (x)
3878               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3879             {
3880               pedwarn ("type mismatch with previous external decl", x);
3881               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3882             }
3883         }
3884
3885       /* This name is new in its binding level.
3886          Install the new declaration and return it.  */
3887       if (namespace_bindings_p ())
3888         {
3889           /* Install a global value.  */
3890
3891           /* If the first global decl has external linkage,
3892              warn if we later see static one.  */
3893           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3894             TREE_PUBLIC (name) = 1;
3895
3896           /* Bind the name for the entity.  */
3897           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3898                 && t != NULL_TREE)
3899               && (TREE_CODE (x) == TYPE_DECL
3900                   || TREE_CODE (x) == VAR_DECL
3901                   || TREE_CODE (x) == ALIAS_DECL
3902                   || TREE_CODE (x) == NAMESPACE_DECL
3903                   || TREE_CODE (x) == CONST_DECL
3904                   || TREE_CODE (x) == TEMPLATE_DECL))
3905             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3906
3907           /* Don't forget if the function was used via an implicit decl.  */
3908           if (IDENTIFIER_IMPLICIT_DECL (name)
3909               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3910             TREE_USED (x) = 1;
3911
3912           /* Don't forget if its address was taken in that way.  */
3913           if (IDENTIFIER_IMPLICIT_DECL (name)
3914               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3915             TREE_ADDRESSABLE (x) = 1;
3916
3917           /* Warn about mismatches against previous implicit decl.  */
3918           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
3919               /* If this real decl matches the implicit, don't complain.  */
3920               && ! (TREE_CODE (x) == FUNCTION_DECL
3921                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
3922             warning
3923               ("`%D' was previously implicitly declared to return `int'", x);
3924
3925           /* If new decl is `static' and an `extern' was seen previously,
3926              warn about it.  */
3927           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
3928             warn_extern_redeclared_static (x, t);
3929         }
3930       else
3931         {
3932           /* Here to install a non-global value.  */
3933           tree oldlocal = IDENTIFIER_VALUE (name);
3934           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
3935
3936           if (need_new_binding)
3937             {
3938               push_local_binding (name, x, 0);
3939               /* Because push_local_binding will hook X on to the
3940                  current_binding_level's name list, we don't want to
3941                  do that again below.  */
3942               need_new_binding = 0;
3943             }
3944
3945           /* If this is a TYPE_DECL, push it into the type value slot.  */
3946           if (TREE_CODE (x) == TYPE_DECL)
3947             set_identifier_type_value_with_scope (name, TREE_TYPE (x),
3948                                                   current_binding_level);
3949
3950           /* Clear out any TYPE_DECL shadowed by a namespace so that
3951              we won't think this is a type.  The C struct hack doesn't
3952              go through namespaces.  */
3953           if (TREE_CODE (x) == NAMESPACE_DECL)
3954             set_identifier_type_value_with_scope (name, NULL_TREE,
3955                                                   current_binding_level);
3956
3957           if (oldlocal)
3958             {
3959               tree d = oldlocal;
3960
3961               while (oldlocal
3962                      && TREE_CODE (oldlocal) == VAR_DECL
3963                      && DECL_DEAD_FOR_LOCAL (oldlocal))
3964                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
3965
3966               if (oldlocal == NULL_TREE)
3967                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
3968             }
3969
3970           /* If this is an extern function declaration, see if we
3971              have a global definition or declaration for the function.  */
3972           if (oldlocal == NULL_TREE
3973               && DECL_EXTERNAL (x)
3974               && oldglobal != NULL_TREE
3975               && TREE_CODE (x) == FUNCTION_DECL
3976               && TREE_CODE (oldglobal) == FUNCTION_DECL)
3977             {
3978               /* We have one.  Their types must agree.  */
3979               if (decls_match (x, oldglobal))
3980                 /* OK */;
3981               else
3982                 {
3983                   warning ("extern declaration of `%#D' doesn't match", x);
3984                   cp_warning_at ("global declaration `%#D'", oldglobal);
3985                 }
3986             }
3987           /* If we have a local external declaration,
3988              and no file-scope declaration has yet been seen,
3989              then if we later have a file-scope decl it must not be static.  */
3990           if (oldlocal == NULL_TREE
3991               && oldglobal == NULL_TREE
3992               && DECL_EXTERNAL (x)
3993               && TREE_PUBLIC (x))
3994             TREE_PUBLIC (name) = 1;
3995
3996           /* Warn if shadowing an argument at the top level of the body.  */
3997           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
3998               /* Inline decls shadow nothing.  */
3999               && !DECL_FROM_INLINE (x)
4000               && TREE_CODE (oldlocal) == PARM_DECL
4001               /* Don't check the `this' parameter.  */
4002               && !DECL_ARTIFICIAL (oldlocal))
4003             {
4004               bool err = false;
4005
4006               /* Don't complain if it's from an enclosing function.  */
4007               if (DECL_CONTEXT (oldlocal) == current_function_decl
4008                   && TREE_CODE (x) != PARM_DECL)
4009                 {
4010                   /* Go to where the parms should be and see if we find
4011                      them there.  */
4012                   struct cp_binding_level *b = current_binding_level->level_chain;
4013
4014                   /* Skip the ctor/dtor cleanup level.  */
4015                   b = b->level_chain;
4016
4017                   /* ARM $8.3 */
4018                   if (b->parm_flag == 1)
4019                     {
4020                       error ("declaration of `%#D' shadows a parameter",
4021                                 name);
4022                       err = true;
4023                     }
4024                 }
4025
4026               if (warn_shadow && !err)
4027                 shadow_warning (SW_PARAM,
4028                                 IDENTIFIER_POINTER (name), oldlocal);
4029             }
4030
4031           /* Maybe warn if shadowing something else.  */
4032           else if (warn_shadow && !DECL_EXTERNAL (x)
4033               /* No shadow warnings for internally generated vars.  */
4034               && ! DECL_ARTIFICIAL (x)
4035               /* No shadow warnings for vars made for inlining.  */
4036               && ! DECL_FROM_INLINE (x))
4037             {
4038               if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4039                        && current_class_ptr
4040                        && !TREE_STATIC (name))
4041                 warning ("declaration of `%s' shadows a member of `this'",
4042                             IDENTIFIER_POINTER (name));
4043               else if (oldlocal != NULL_TREE
4044                        && TREE_CODE (oldlocal) == VAR_DECL)
4045                 shadow_warning (SW_LOCAL,
4046                                 IDENTIFIER_POINTER (name), oldlocal);
4047               else if (oldglobal != NULL_TREE
4048                        && TREE_CODE (oldglobal) == VAR_DECL)
4049                 /* XXX shadow warnings in outer-more namespaces */
4050                 shadow_warning (SW_GLOBAL,
4051                                 IDENTIFIER_POINTER (name), oldglobal);
4052             }
4053         }
4054
4055       if (TREE_CODE (x) == FUNCTION_DECL)
4056         check_default_args (x);
4057
4058       if (TREE_CODE (x) == VAR_DECL)
4059         maybe_register_incomplete_var (x);
4060     }
4061
4062   if (need_new_binding)
4063     add_decl_to_level (x,
4064                        DECL_NAMESPACE_SCOPE_P (x)
4065                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4066                        : current_binding_level);
4067
4068   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4069 }
4070
4071 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4072    caller to set DECL_CONTEXT properly.  */
4073
4074 static tree
4075 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4076 {
4077   register struct cp_binding_level *b;
4078   tree function_decl = current_function_decl;
4079
4080   timevar_push (TV_NAME_LOOKUP);
4081   current_function_decl = NULL_TREE;
4082   if (level->parm_flag == 2)
4083     {
4084       b = class_binding_level;
4085       class_binding_level = level;
4086       pushdecl_class_level (x);
4087       class_binding_level = b;
4088     }
4089   else
4090     {
4091       b = current_binding_level;
4092       current_binding_level = level;
4093       x = pushdecl (x);
4094       current_binding_level = b;
4095     }
4096   current_function_decl = function_decl;
4097   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4098 }
4099
4100 /* Like pushdecl, only it places X in the current namespace,
4101    if appropriate.  */
4102
4103 tree
4104 pushdecl_namespace_level (tree x)
4105 {
4106   register struct cp_binding_level *b = current_binding_level;
4107   register tree t;
4108
4109   timevar_push (TV_NAME_LOOKUP);
4110   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4111
4112   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4113      what we want.  */
4114   if (TREE_CODE (x) == TYPE_DECL)
4115     {
4116       tree name = DECL_NAME (x);
4117       tree newval;
4118       tree *ptr = (tree *)0;
4119       for (; !global_scope_p (b); b = b->level_chain)
4120         {
4121           tree shadowed = b->type_shadowed;
4122           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4123             if (TREE_PURPOSE (shadowed) == name)
4124               {
4125                 ptr = &TREE_VALUE (shadowed);
4126                 /* Can't break out of the loop here because sometimes
4127                    a binding level will have duplicate bindings for
4128                    PT names.  It's gross, but I haven't time to fix it.  */
4129               }
4130         }
4131       newval = TREE_TYPE (x);
4132       if (ptr == (tree *)0)
4133         {
4134           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4135              up here if this is changed to an assertion.  --KR  */
4136           SET_IDENTIFIER_TYPE_VALUE (name, newval);
4137         }
4138       else
4139         {
4140           *ptr = newval;
4141         }
4142     }
4143   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4144 }
4145
4146 /* Like pushdecl, only it places X in the global scope if appropriate.  */
4147
4148 tree
4149 pushdecl_top_level (tree x)
4150 {
4151   timevar_push (TV_NAME_LOOKUP);
4152   push_to_top_level ();
4153   x = pushdecl_namespace_level (x);
4154   pop_from_top_level ();
4155   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4156 }
4157
4158 /* Make the declaration of X appear in CLASS scope.  */
4159
4160 void
4161 pushdecl_class_level (tree x)
4162 {
4163   tree name;
4164
4165   timevar_push (TV_NAME_LOOKUP);
4166   /* Get the name of X.  */
4167   if (TREE_CODE (x) == OVERLOAD)
4168     name = DECL_NAME (get_first_fn (x));
4169   else
4170     name = DECL_NAME (x);
4171
4172   if (name)
4173     {
4174       push_class_level_binding (name, x);
4175       if (TREE_CODE (x) == TYPE_DECL)
4176         set_identifier_type_value (name, TREE_TYPE (x));
4177     }
4178   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4179     {
4180       /* If X is an anonymous aggregate, all of its members are
4181          treated as if they were members of the class containing the
4182          aggregate, for naming purposes.  */
4183       tree f;
4184
4185       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4186         pushdecl_class_level (f);
4187     }
4188   timevar_pop (TV_NAME_LOOKUP);
4189 }
4190
4191 /* Enter DECL into the symbol table, if that's appropriate.  Returns
4192    DECL, or a modified version thereof.  */
4193
4194 tree
4195 maybe_push_decl (tree decl)
4196 {
4197   tree type = TREE_TYPE (decl);
4198
4199   /* Add this decl to the current binding level, but not if it comes
4200      from another scope, e.g. a static member variable.  TEM may equal
4201      DECL or it may be a previous decl of the same name.  */
4202   if (decl == error_mark_node
4203       || (TREE_CODE (decl) != PARM_DECL
4204           && DECL_CONTEXT (decl) != NULL_TREE
4205           /* Definitions of namespace members outside their namespace are
4206              possible.  */
4207           && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4208       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4209       || TREE_CODE (type) == UNKNOWN_TYPE
4210       /* The declaration of a template specialization does not affect
4211          the functions available for overload resolution, so we do not
4212          call pushdecl.  */
4213       || (TREE_CODE (decl) == FUNCTION_DECL
4214           && DECL_TEMPLATE_SPECIALIZATION (decl)))
4215     return decl;
4216   else
4217     return pushdecl (decl);
4218 }
4219
4220 /* Make the declaration(s) of X appear in CLASS scope
4221    under the name NAME.  */
4222
4223 void
4224 push_class_level_binding (tree name, tree x)
4225 {
4226   cxx_binding *binding;
4227   timevar_push (TV_NAME_LOOKUP);
4228   /* The class_binding_level will be NULL if x is a template
4229      parameter name in a member template.  */
4230   if (!class_binding_level)
4231     {
4232       timevar_pop (TV_NAME_LOOKUP);
4233       return;
4234     }
4235
4236   /* Make sure that this new member does not have the same name
4237      as a template parameter.  */
4238   if (TYPE_BEING_DEFINED (current_class_type))
4239     check_template_shadow (x);
4240
4241   /* If this declaration shadows a declaration from an enclosing
4242      class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4243      we leave this class.  Record the shadowed declaration here.  */
4244   binding = IDENTIFIER_BINDING (name);
4245   if (binding
4246       && ((TREE_CODE (x) == OVERLOAD
4247            && BINDING_VALUE (binding)
4248            && is_overloaded_fn (BINDING_VALUE (binding)))
4249           || INHERITED_VALUE_BINDING_P (binding)))
4250     {
4251       tree shadow;
4252       tree old_decl;
4253
4254       /* If the old binding was from a base class, and was for a tag
4255          name, slide it over to make room for the new binding.  The
4256          old binding is still visible if explicitly qualified with a
4257          class-key.  */
4258       if (INHERITED_VALUE_BINDING_P (binding)
4259           && BINDING_VALUE (binding)
4260           && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4261           && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4262           && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4263         {
4264           old_decl = BINDING_TYPE (binding);
4265           BINDING_TYPE (binding) = BINDING_VALUE (binding);
4266           BINDING_VALUE (binding) = NULL_TREE;
4267           INHERITED_VALUE_BINDING_P (binding) = 0;
4268         }
4269       else
4270         old_decl = BINDING_VALUE (binding);
4271
4272       /* Find the previous binding of name on the class-shadowed
4273          list, and update it.  */
4274       for (shadow = class_binding_level->class_shadowed;
4275            shadow;
4276            shadow = TREE_CHAIN (shadow))
4277         if (TREE_PURPOSE (shadow) == name
4278             && TREE_TYPE (shadow) == old_decl)
4279           {
4280             BINDING_VALUE (binding) = x;
4281             INHERITED_VALUE_BINDING_P (binding) = 0;
4282             TREE_TYPE (shadow) = x;
4283             IDENTIFIER_CLASS_VALUE (name) = x;
4284             timevar_pop (TV_NAME_LOOKUP);
4285             return;
4286           }
4287     }
4288
4289   /* If we didn't replace an existing binding, put the binding on the
4290      stack of bindings for the identifier, and update the shadowed list.  */
4291   if (push_class_binding (name, x))
4292     {
4293       class_binding_level->class_shadowed
4294         = tree_cons (name, NULL,
4295                      class_binding_level->class_shadowed);
4296       /* Record the value we are binding NAME to so that we can know
4297          what to pop later.  */
4298       TREE_TYPE (class_binding_level->class_shadowed) = x;
4299     }
4300   timevar_pop (TV_NAME_LOOKUP);
4301 }
4302
4303 /* Insert another USING_DECL into the current binding level, returning
4304    this declaration. If this is a redeclaration, do nothing, and
4305    return NULL_TREE if this not in namespace scope (in namespace
4306    scope, a using decl might extend any previous bindings).  */
4307
4308 tree
4309 push_using_decl (tree scope, tree name)
4310 {
4311   tree decl;
4312
4313   timevar_push (TV_NAME_LOOKUP);
4314   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4315   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4316   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4317     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4318       break;
4319   if (decl)
4320     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4321                             namespace_bindings_p () ? decl : NULL_TREE);
4322   decl = build_lang_decl (USING_DECL, name, void_type_node);
4323   DECL_INITIAL (decl) = scope;
4324   TREE_CHAIN (decl) = current_binding_level->usings;
4325   current_binding_level->usings = decl;
4326   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4327 }
4328
4329 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4330    changed (i.e. there was already a directive), or the fresh
4331    TREE_LIST otherwise.  */
4332
4333 tree
4334 push_using_directive (tree used)
4335 {
4336   tree ud = current_binding_level->using_directives;
4337   tree iter, ancestor;
4338
4339   timevar_push (TV_NAME_LOOKUP);
4340   /* Check if we already have this.  */
4341   if (purpose_member (used, ud) != NULL_TREE)
4342     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4343
4344   ancestor = namespace_ancestor (current_decl_namespace (), used);
4345   ud = current_binding_level->using_directives;
4346   ud = tree_cons (used, ancestor, ud);
4347   current_binding_level->using_directives = ud;
4348
4349   /* Recursively add all namespaces used.  */
4350   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4351     push_using_directive (TREE_PURPOSE (iter));
4352
4353   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4354 }
4355
4356 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4357    other definitions already in place.  We get around this by making
4358    the value of the identifier point to a list of all the things that
4359    want to be referenced by that name.  It is then up to the users of
4360    that name to decide what to do with that list.
4361
4362    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4363    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4364
4365    FLAGS is a bitwise-or of the following values:
4366      PUSH_LOCAL: Bind DECL in the current scope, rather than at
4367                  namespace scope.
4368      PUSH_USING: DECL is being pushed as the result of a using
4369                  declaration.
4370
4371    The value returned may be a previous declaration if we guessed wrong
4372    about what language DECL should belong to (C or C++).  Otherwise,
4373    it's always DECL (and never something that's not a _DECL).  */
4374
4375 tree
4376 push_overloaded_decl (tree decl, int flags)
4377 {
4378   tree name = DECL_NAME (decl);
4379   tree old;
4380   tree new_binding;
4381   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4382
4383   timevar_push (TV_NAME_LOOKUP);
4384   if (doing_global)
4385     old = namespace_binding (name, DECL_CONTEXT (decl));
4386   else
4387     old = lookup_name_current_level (name);
4388
4389   if (old)
4390     {
4391       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4392         {
4393           tree t = TREE_TYPE (old);
4394           if (IS_AGGR_TYPE (t) && warn_shadow
4395               && (! DECL_IN_SYSTEM_HEADER (decl)
4396                   || ! DECL_IN_SYSTEM_HEADER (old)))
4397             warning ("`%#D' hides constructor for `%#T'", decl, t);
4398           old = NULL_TREE;
4399         }
4400       else if (is_overloaded_fn (old))
4401         {
4402           tree tmp;
4403
4404           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4405             {
4406               tree fn = OVL_CURRENT (tmp);
4407
4408               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4409                   && !(flags & PUSH_USING)
4410                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4411                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4412                 error ("`%#D' conflicts with previous using declaration `%#D'",
4413                           decl, fn);
4414
4415               if (duplicate_decls (decl, fn))
4416                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4417             }
4418         }
4419       else if (old == error_mark_node)
4420         /* Ignore the undefined symbol marker.  */
4421         old = NULL_TREE;
4422       else
4423         {
4424           cp_error_at ("previous non-function declaration `%#D'", old);
4425           error ("conflicts with function declaration `%#D'", decl);
4426           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4427         }
4428     }
4429
4430   if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4431     {
4432       if (old && TREE_CODE (old) != OVERLOAD)
4433         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4434       else
4435         new_binding = ovl_cons (decl, old);
4436       if (flags & PUSH_USING)
4437         OVL_USED (new_binding) = 1;
4438     }
4439   else
4440     /* NAME is not ambiguous.  */
4441     new_binding = decl;
4442
4443   if (doing_global)
4444     set_namespace_binding (name, current_namespace, new_binding);
4445   else
4446     {
4447       /* We only create an OVERLOAD if there was a previous binding at
4448          this level, or if decl is a template. In the former case, we
4449          need to remove the old binding and replace it with the new
4450          binding.  We must also run through the NAMES on the binding
4451          level where the name was bound to update the chain.  */
4452
4453       if (TREE_CODE (new_binding) == OVERLOAD && old)
4454         {
4455           tree *d;
4456
4457           for (d = &BINDING_SCOPE (IDENTIFIER_BINDING (name))->names;
4458                *d;
4459                d = &TREE_CHAIN (*d))
4460             if (*d == old
4461                 || (TREE_CODE (*d) == TREE_LIST
4462                     && TREE_VALUE (*d) == old))
4463               {
4464                 if (TREE_CODE (*d) == TREE_LIST)
4465                   /* Just replace the old binding with the new.  */
4466                   TREE_VALUE (*d) = new_binding;
4467                 else
4468                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
4469                   *d = tree_cons (NULL_TREE, new_binding,
4470                                   TREE_CHAIN (*d));
4471
4472                 /* And update the cxx_binding node.  */
4473                 BINDING_VALUE (IDENTIFIER_BINDING (name))
4474                   = new_binding;
4475                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4476               }
4477
4478           /* We should always find a previous binding in this case.  */
4479           abort ();
4480         }
4481
4482       /* Install the new binding.  */
4483       push_local_binding (name, new_binding, flags);
4484     }
4485
4486   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4487 }
4488 \f
4489 /* Generate an implicit declaration for identifier FUNCTIONID
4490    as a function of type int ().  Print a warning if appropriate.  */
4491
4492 tree
4493 implicitly_declare (tree functionid)
4494 {
4495   register tree decl;
4496
4497   /* We used to reuse an old implicit decl here,
4498      but this loses with inline functions because it can clobber
4499      the saved decl chains.  */
4500   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4501
4502   DECL_EXTERNAL (decl) = 1;
4503   TREE_PUBLIC (decl) = 1;
4504
4505   /* ISO standard says implicit declarations are in the innermost block.
4506      So we record the decl in the standard fashion.  */
4507   pushdecl (decl);
4508   rest_of_decl_compilation (decl, NULL, 0, 0);
4509
4510   if (warn_implicit
4511       /* Only one warning per identifier.  */
4512       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4513     {
4514       pedwarn ("implicit declaration of function `%#D'", decl);
4515     }
4516
4517   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4518
4519   return decl;
4520 }
4521
4522 /* Return zero if the declaration NEWDECL is valid
4523    when the declaration OLDDECL (assumed to be for the same name)
4524    has already been seen.
4525    Otherwise return an error message format string with a %s
4526    where the identifier should go.  */
4527
4528 static const char *
4529 redeclaration_error_message (tree newdecl, tree olddecl)
4530 {
4531   if (TREE_CODE (newdecl) == TYPE_DECL)
4532     {
4533       /* Because C++ can put things into name space for free,
4534          constructs like "typedef struct foo { ... } foo"
4535          would look like an erroneous redeclaration.  */
4536       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4537         return 0;
4538       else
4539         return "redefinition of `%#D'";
4540     }
4541   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4542     {
4543       /* If this is a pure function, its olddecl will actually be
4544          the original initialization to `0' (which we force to call
4545          abort()).  Don't complain about redefinition in this case.  */
4546       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4547         return 0;
4548
4549       /* If both functions come from different namespaces, this is not
4550          a redeclaration - this is a conflict with a used function.  */
4551       if (DECL_NAMESPACE_SCOPE_P (olddecl)
4552           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4553         return "`%D' conflicts with used function";
4554
4555       /* We'll complain about linkage mismatches in
4556          warn_extern_redeclared_static.  */
4557
4558       /* Defining the same name twice is no good.  */
4559       if (DECL_INITIAL (olddecl) != NULL_TREE
4560           && DECL_INITIAL (newdecl) != NULL_TREE)
4561         {
4562           if (DECL_NAME (olddecl) == NULL_TREE)
4563             return "`%#D' not declared in class";
4564           else
4565             return "redefinition of `%#D'";
4566         }
4567       return 0;
4568     }
4569   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4570     {
4571       if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4572            && (DECL_TEMPLATE_RESULT (newdecl)
4573                != DECL_TEMPLATE_RESULT (olddecl))
4574            && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4575            && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4576           || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4577               && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4578               && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4579         return "redefinition of `%#D'";
4580       return 0;
4581     }
4582   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4583     {
4584       /* Objects declared at top level:  */
4585       /* If at least one is a reference, it's ok.  */
4586       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4587         return 0;
4588       /* Reject two definitions.  */
4589       return "redefinition of `%#D'";
4590     }
4591   else
4592     {
4593       /* Objects declared with block scope:  */
4594       /* Reject two definitions, and reject a definition
4595          together with an external reference.  */
4596       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4597         return "redeclaration of `%#D'";
4598       return 0;
4599     }
4600 }
4601 \f
4602 /* Create a new label, named ID.  */
4603
4604 static tree
4605 make_label_decl (tree id, int local_p)
4606 {
4607   tree decl;
4608
4609   decl = build_decl (LABEL_DECL, id, void_type_node);
4610   if (expanding_p)
4611     /* Make sure every label has an rtx.  */
4612     label_rtx (decl);
4613
4614   DECL_CONTEXT (decl) = current_function_decl;
4615   DECL_MODE (decl) = VOIDmode;
4616   C_DECLARED_LABEL_FLAG (decl) = local_p;
4617
4618   /* Say where one reference is to the label, for the sake of the
4619      error if it is not defined.  */
4620   DECL_SOURCE_LOCATION (decl) = input_location;
4621
4622   /* Record the fact that this identifier is bound to this label.  */
4623   SET_IDENTIFIER_LABEL_VALUE (id, decl);
4624
4625   return decl;
4626 }
4627
4628 /* Record this label on the list of used labels so that we can check
4629    at the end of the function to see whether or not the label was
4630    actually defined, and so we can check when the label is defined whether
4631    this use is valid.  */
4632
4633 static void
4634 use_label (tree decl)
4635 {
4636   if (named_label_uses == NULL
4637       || named_label_uses->names_in_scope != current_binding_level->names
4638       || named_label_uses->label_decl != decl)
4639     {
4640       struct named_label_use_list *new_ent;
4641       new_ent = ((struct named_label_use_list *)
4642                  ggc_alloc (sizeof (struct named_label_use_list)));
4643       new_ent->label_decl = decl;
4644       new_ent->names_in_scope = current_binding_level->names;
4645       new_ent->binding_level = current_binding_level;
4646       new_ent->o_goto_locus = input_location;
4647       new_ent->next = named_label_uses;
4648       named_label_uses = new_ent;
4649     }
4650 }
4651
4652 /* Look for a label named ID in the current function.  If one cannot
4653    be found, create one.  (We keep track of used, but undefined,
4654    labels, and complain about them at the end of a function.)  */
4655
4656 tree
4657 lookup_label (tree id)
4658 {
4659   tree decl;
4660   struct named_label_list *ent;
4661
4662   timevar_push (TV_NAME_LOOKUP);
4663   /* You can't use labels at global scope.  */
4664   if (current_function_decl == NULL_TREE)
4665     {
4666       error ("label `%s' referenced outside of any function",
4667              IDENTIFIER_POINTER (id));
4668       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4669     }
4670
4671   /* See if we've already got this label.  */
4672   decl = IDENTIFIER_LABEL_VALUE (id);
4673   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4674     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4675
4676   /* Record this label on the list of labels used in this function.
4677      We do this before calling make_label_decl so that we get the
4678      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
4679   ent = ((struct named_label_list *)
4680          ggc_alloc_cleared (sizeof (struct named_label_list)));
4681   ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4682   ent->next = named_labels;
4683   named_labels = ent;
4684
4685   /* We need a new label.  */
4686   decl = make_label_decl (id, /*local_p=*/0);
4687
4688   /* Now fill in the information we didn't have before.  */
4689   ent->label_decl = decl;
4690
4691   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4692 }
4693
4694 /* Declare a local label named ID.  */
4695
4696 tree
4697 declare_local_label (tree id)
4698 {
4699   tree decl;
4700
4701   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4702      this scope we can restore the old value of
4703      IDENTIFIER_TYPE_VALUE.  */
4704   current_binding_level->shadowed_labels
4705     = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4706                  current_binding_level->shadowed_labels);
4707   /* Look for the label.  */
4708   decl = make_label_decl (id, /*local_p=*/1);
4709   /* Now fill in the information we didn't have before.  */
4710   TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4711
4712   return decl;
4713 }
4714
4715 /* Returns nonzero if it is ill-formed to jump past the declaration of
4716    DECL.  Returns 2 if it's also a real problem.  */
4717
4718 static int
4719 decl_jump_unsafe (tree decl)
4720 {
4721   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4722     return 0;
4723
4724   if (DECL_INITIAL (decl) == NULL_TREE
4725       && pod_type_p (TREE_TYPE (decl)))
4726     return 0;
4727
4728   /* This is really only important if we're crossing an initialization.
4729      The POD stuff is just pedantry; why should it matter if the class
4730      contains a field of pointer to member type?  */
4731   if (DECL_INITIAL (decl)
4732       || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4733     return 2;
4734   return 1;
4735 }
4736
4737 /* Check that a single previously seen jump to a newly defined label
4738    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4739    the jump context; NAMES are the names in scope in LEVEL at the jump
4740    context; FILE and LINE are the source position of the jump or 0.  */
4741
4742 static void
4743 check_previous_goto_1 (tree decl,
4744                        struct cp_binding_level* level,
4745                        tree names, const location_t *locus)
4746 {
4747   int identified = 0;
4748   int saw_eh = 0;
4749   struct cp_binding_level *b = current_binding_level;
4750   for (; b; b = b->level_chain)
4751     {
4752       tree new_decls = b->names;
4753       tree old_decls = (b == level ? names : NULL_TREE);
4754       for (; new_decls != old_decls;
4755            new_decls = TREE_CHAIN (new_decls))
4756         {
4757           int problem = decl_jump_unsafe (new_decls);
4758           if (! problem)
4759             continue;
4760
4761           if (! identified)
4762             {
4763               if (decl)
4764                 pedwarn ("jump to label `%D'", decl);
4765               else
4766                 pedwarn ("jump to case label");
4767
4768               if (locus)
4769                 pedwarn ("%H  from here", locus);
4770               identified = 1;
4771             }
4772
4773           if (problem > 1)
4774             cp_error_at ("  crosses initialization of `%#D'",
4775                          new_decls);
4776           else
4777             cp_pedwarn_at ("  enters scope of non-POD `%#D'",
4778                            new_decls);
4779         }
4780
4781       if (b == level)
4782         break;
4783       if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4784         {
4785           if (! identified)
4786             {
4787               if (decl)
4788                 pedwarn ("jump to label `%D'", decl);
4789               else
4790                 pedwarn ("jump to case label");
4791
4792               if (locus)
4793                 pedwarn ("%H  from here", locus);
4794               identified = 1;
4795             }
4796           if (b->is_try_scope)
4797             error ("  enters try block");
4798           else
4799             error ("  enters catch block");
4800           saw_eh = 1;
4801         }
4802     }
4803 }
4804
4805 static void
4806 check_previous_goto (struct named_label_use_list* use)
4807 {
4808   check_previous_goto_1 (use->label_decl, use->binding_level,
4809                          use->names_in_scope, &use->o_goto_locus);
4810 }
4811
4812 static void
4813 check_switch_goto (struct cp_binding_level* level)
4814 {
4815   check_previous_goto_1 (NULL_TREE, level, level->names, NULL);
4816 }
4817
4818 /* Check that any previously seen jumps to a newly defined label DECL
4819    are OK.  Called by define_label.  */
4820
4821 static void
4822 check_previous_gotos (tree decl)
4823 {
4824   struct named_label_use_list **usep;
4825
4826   if (! TREE_USED (decl))
4827     return;
4828
4829   for (usep = &named_label_uses; *usep; )
4830     {
4831       struct named_label_use_list *use = *usep;
4832       if (use->label_decl == decl)
4833         {
4834           check_previous_goto (use);
4835           *usep = use->next;
4836         }
4837       else
4838         usep = &(use->next);
4839     }
4840 }
4841
4842 /* Check that a new jump to a label DECL is OK.  Called by
4843    finish_goto_stmt.  */
4844
4845 void
4846 check_goto (tree decl)
4847 {
4848   int identified = 0;
4849   tree bad;
4850   struct named_label_list *lab;
4851
4852   /* We can't know where a computed goto is jumping.  So we assume
4853      that it's OK.  */
4854   if (! DECL_P (decl))
4855     return;
4856
4857   /* If the label hasn't been defined yet, defer checking.  */
4858   if (! DECL_INITIAL (decl))
4859     {
4860       use_label (decl);
4861       return;
4862     }
4863
4864   for (lab = named_labels; lab; lab = lab->next)
4865     if (decl == lab->label_decl)
4866       break;
4867
4868   /* If the label is not on named_labels it's a gcc local label, so
4869      it must be in an outer scope, so jumping to it is always OK.  */
4870   if (lab == 0)
4871     return;
4872
4873   if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4874       && !identified)
4875     {
4876       cp_pedwarn_at ("jump to label `%D'", decl);
4877       pedwarn ("  from here");
4878       identified = 1;
4879     }
4880
4881   for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4882     {
4883       tree b = TREE_VALUE (bad);
4884       int u = decl_jump_unsafe (b);
4885
4886       if (u > 1 && DECL_ARTIFICIAL (b))
4887         /* Can't skip init of __exception_info.  */
4888         cp_error_at ("  enters catch block", b);
4889       else if (u > 1)
4890         cp_error_at ("  skips initialization of `%#D'", b);
4891       else
4892         cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
4893     }
4894
4895   if (lab->in_try_scope)
4896     error ("  enters try block");
4897   else if (lab->in_catch_scope)
4898     error ("  enters catch block");
4899 }
4900
4901 /* Define a label, specifying the location in the source file.
4902    Return the LABEL_DECL node for the label, if the definition is valid.
4903    Otherwise return 0.  */
4904
4905 tree
4906 define_label (const char* filename, int line, tree name)
4907 {
4908   tree decl = lookup_label (name);
4909   struct named_label_list *ent;
4910   register struct cp_binding_level *p;
4911
4912   timevar_push (TV_NAME_LOOKUP);
4913   for (ent = named_labels; ent; ent = ent->next)
4914     if (ent->label_decl == decl)
4915       break;
4916
4917   /* After labels, make any new cleanups in the function go into their
4918      own new (temporary) binding contour.  */
4919   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4920     p->more_cleanups_ok = 0;
4921
4922   if (name == get_identifier ("wchar_t"))
4923     pedwarn ("label named wchar_t");
4924
4925   if (DECL_INITIAL (decl) != NULL_TREE)
4926     {
4927       error ("duplicate label `%D'", decl);
4928       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4929     }
4930   else
4931     {
4932       /* Mark label as having been defined.  */
4933       DECL_INITIAL (decl) = error_mark_node;
4934       /* Say where in the source.  */
4935       DECL_SOURCE_FILE (decl) = filename;
4936       DECL_SOURCE_LINE (decl) = line;
4937       if (ent)
4938         {
4939           ent->names_in_scope = current_binding_level->names;
4940           ent->binding_level = current_binding_level;
4941         }
4942       check_previous_gotos (decl);
4943       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4944     }
4945   timevar_pop (TV_NAME_LOOKUP);
4946 }
4947
4948 struct cp_switch
4949 {
4950   struct cp_binding_level *level;
4951   struct cp_switch *next;
4952   /* The SWITCH_STMT being built.  */
4953   tree switch_stmt;
4954   /* A splay-tree mapping the low element of a case range to the high
4955      element, or NULL_TREE if there is no high element.  Used to
4956      determine whether or not a new case label duplicates an old case
4957      label.  We need a tree, rather than simply a hash table, because
4958      of the GNU case range extension.  */
4959   splay_tree cases;
4960 };
4961
4962 /* A stack of the currently active switch statements.  The innermost
4963    switch statement is on the top of the stack.  There is no need to
4964    mark the stack for garbage collection because it is only active
4965    during the processing of the body of a function, and we never
4966    collect at that point.  */
4967
4968 static struct cp_switch *switch_stack;
4969
4970 /* Called right after a switch-statement condition is parsed.
4971    SWITCH_STMT is the switch statement being parsed.  */
4972
4973 void
4974 push_switch (tree switch_stmt)
4975 {
4976   struct cp_switch *p
4977     = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
4978   p->level = current_binding_level;
4979   p->next = switch_stack;
4980   p->switch_stmt = switch_stmt;
4981   p->cases = splay_tree_new (case_compare, NULL, NULL);
4982   switch_stack = p;
4983 }
4984
4985 void
4986 pop_switch (void)
4987 {
4988   struct cp_switch *cs;
4989
4990   cs = switch_stack;
4991   splay_tree_delete (cs->cases);
4992   switch_stack = switch_stack->next;
4993   free (cs);
4994 }
4995
4996 /* Note that we've seen a definition of a case label, and complain if this
4997    is a bad place for one.  */
4998
4999 tree
5000 finish_case_label (tree low_value, tree high_value)
5001 {
5002   tree cond, r;
5003   register struct cp_binding_level *p;
5004
5005   if (! switch_stack)
5006     {
5007       if (high_value)
5008         error ("case label not within a switch statement");
5009       else if (low_value)
5010         error ("case label `%E' not within a switch statement",
5011                   low_value);
5012       else
5013         error ("`default' label not within a switch statement");
5014       return NULL_TREE;
5015     }
5016
5017   if (processing_template_decl)
5018     {
5019       tree label;
5020
5021       /* For templates, just add the case label; we'll do semantic
5022          analysis at instantiation-time.  */
5023       label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5024       return add_stmt (build_case_label (low_value, high_value, label));
5025     }
5026
5027   /* Find the condition on which this switch statement depends.  */
5028   cond = SWITCH_COND (switch_stack->switch_stmt);
5029   if (cond && TREE_CODE (cond) == TREE_LIST)
5030     cond = TREE_VALUE (cond);
5031
5032   r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5033
5034   check_switch_goto (switch_stack->level);
5035
5036   /* After labels, make any new cleanups in the function go into their
5037      own new (temporary) binding contour.  */
5038   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5039     p->more_cleanups_ok = 0;
5040
5041   return r;
5042 }
5043 \f
5044 /* Return the list of declarations of the current level.
5045    Note that this list is in reverse order unless/until
5046    you nreverse it; and when you do nreverse it, you must
5047    store the result back using `storedecls' or you will lose.  */
5048
5049 tree
5050 getdecls (void)
5051 {
5052   return current_binding_level->names;
5053 }
5054
5055 /* Store the list of declarations of the current level.
5056    This is done for the parameter declarations of a function being defined,
5057    after they are modified in the light of any missing parameters.  */
5058
5059 static void
5060 storedecls (tree decls)
5061 {
5062   current_binding_level->names = decls;
5063 }
5064
5065 /* Set the current binding TABLE for type declarations..  This is a
5066    temporary workaround of the fact that the data structure classtypes
5067    does not currently carry its allocated cxx_scope structure.  */
5068 void
5069 cxx_remember_type_decls (binding_table table)
5070 {
5071   current_binding_level->type_decls = table;
5072 }
5073 \f
5074 /* Return the type that should be used when TYPE's name is preceded
5075    by a tag such as 'struct' or 'union', or null if the name cannot
5076    be used in this way.
5077
5078    For example, when processing the third line of:
5079
5080         struct A;
5081         typedef struct A A;
5082         struct A;
5083
5084    lookup of A will find the typedef.  Given A's typedef, this function
5085    will return the type associated with "struct A".  For the tag to be
5086    anything other than TYPE, TYPE must be a typedef whose original type
5087    has the same name and context as TYPE itself.
5088
5089    It is not valid for a typedef of an anonymous type to be used with
5090    an explicit tag:
5091
5092        typedef struct { ... } B;
5093        struct B;
5094
5095    Return null for this case.  */
5096
5097 static tree
5098 follow_tag_typedef (tree type)
5099 {
5100   tree original;
5101
5102   original = original_type (type);
5103   if (! TYPE_NAME (original))
5104     return NULL_TREE;
5105   if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5106       && (CP_DECL_CONTEXT (TYPE_NAME (original))
5107           == CP_DECL_CONTEXT (TYPE_NAME (type)))
5108       && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5109     return original;
5110   else
5111     return NULL_TREE;
5112 }
5113
5114 /* Given NAME, an IDENTIFIER_NODE,
5115    return the structure (or union or enum) definition for that name.
5116    Searches binding levels from BINDING_SCOPE up to the global level.
5117    If THISLEVEL_ONLY is nonzero, searches only the specified context
5118    (but skips any tag-transparent contexts to find one that is
5119    meaningful for tags).
5120    FORM says which kind of type the caller wants;
5121    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5122    If the wrong kind of type is found, and it's not a template, an error is
5123    reported.  */
5124
5125 static tree
5126 lookup_tag (enum tree_code form, tree name,
5127             struct cp_binding_level* binding_level, int thislevel_only)
5128 {
5129   register struct cp_binding_level *level;
5130   /* Nonzero if, we should look past a template parameter level, even
5131      if THISLEVEL_ONLY.  */
5132   int allow_template_parms_p = 1;
5133   bool type_is_anonymous = ANON_AGGRNAME_P (name);
5134
5135   timevar_push (TV_NAME_LOOKUP);
5136   for (level = binding_level; level; level = level->level_chain)
5137     {
5138       register tree tail;
5139       if (type_is_anonymous && level->type_decls != NULL)
5140         {
5141           tree type = binding_table_find_anon_type (level->type_decls, name);
5142           /* There is no need for error checking here, because
5143            anon names are unique throughout the compilation.  */
5144           if (type != NULL)
5145             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, type);
5146         }
5147       else if (level->namespace_p)
5148         /* Do namespace lookup.  */
5149         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5150           {
5151             cxx_binding *binding =
5152               cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (tail), name);
5153             tree old;
5154
5155             /* If we just skipped past a template parameter level,
5156                even though THISLEVEL_ONLY, and we find a template
5157                class declaration, then we use the _TYPE node for the
5158                template.  See the example below.  */
5159             if (thislevel_only && !allow_template_parms_p
5160                 && binding && BINDING_VALUE (binding)
5161                 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (binding)))
5162               old = TREE_TYPE (BINDING_VALUE (binding));
5163             else if (binding)
5164               old = BINDING_TYPE (binding);
5165             else
5166               old = NULL_TREE;
5167
5168             if (old)
5169               {
5170                 /* We've found something at this binding level.  If it is
5171                    a typedef, extract the tag it refers to.  Lookup fails
5172                    if the typedef doesn't refer to a taggable type.  */
5173                 old = follow_tag_typedef (old);
5174                 if (!old)
5175                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5176                 if (TREE_CODE (old) != form
5177                     && (form == ENUMERAL_TYPE
5178                         || TREE_CODE (old) == ENUMERAL_TYPE))
5179                   {
5180                     error ("`%#D' redeclared as %C", old, form);
5181                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5182                   }
5183                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5184               }
5185             if (thislevel_only || tail == global_namespace)
5186               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5187           }
5188       else if (level->type_decls != NULL)
5189         {
5190           binding_entry entry = binding_table_find (level->type_decls, name);
5191           if (entry != NULL)
5192             {
5193               enum tree_code code = TREE_CODE (entry->type);
5194                 
5195               if (code != form
5196                   && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5197                 {
5198                   /* Definition isn't the kind we were looking for.  */
5199                   error ("`%#D' redeclared as %C", entry->type, form);
5200                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5201                 }
5202               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->type);
5203             }
5204           }
5205       if (thislevel_only && ! level->tag_transparent)
5206         {
5207           if (level->template_parms_p && allow_template_parms_p)
5208             {
5209               /* We must deal with cases like this:
5210
5211                    template <class T> struct S;
5212                    template <class T> struct S {};
5213
5214                  When looking up `S', for the second declaration, we
5215                  would like to find the first declaration.  But, we
5216                  are in the pseudo-global level created for the
5217                  template parameters, rather than the (surrounding)
5218                  namespace level.  Thus, we keep going one more level,
5219                  even though THISLEVEL_ONLY is nonzero.  */
5220               allow_template_parms_p = 0;
5221               continue;
5222             }
5223           else
5224             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5225         }
5226     }
5227   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5228 }
5229
5230 /* Given a type, find the tag that was defined for it and return the tag name.
5231    Otherwise return 0.  However, the value can never be 0
5232    in the cases in which this is used.
5233
5234    C++: If NAME is nonzero, this is the new name to install.  This is
5235    done when replacing anonymous tags with real tag names.  */
5236
5237 static tree
5238 lookup_tag_reverse (tree type, tree name)
5239 {
5240   register struct cp_binding_level *level;
5241
5242   timevar_push (TV_NAME_LOOKUP);
5243   for (level = current_binding_level; level; level = level->level_chain)
5244     {
5245       binding_entry entry = level->type_decls == NULL
5246         ? NULL
5247         : binding_table_reverse_maybe_remap (level->type_decls, type, name);
5248       if (entry)
5249         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, entry->name);
5250     }
5251   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5252 }
5253 \f
5254 /* Look up NAME in the NAMESPACE.  */
5255
5256 tree
5257 lookup_namespace_name (tree namespace, tree name)
5258 {
5259   tree val;
5260   tree template_id = NULL_TREE;
5261   cxx_binding binding;
5262
5263   timevar_push (TV_NAME_LOOKUP);
5264   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5265
5266   if (TREE_CODE (name) == NAMESPACE_DECL)
5267     /* This happens for A::B<int> when B is a namespace.  */
5268     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5269   else if (TREE_CODE (name) == TEMPLATE_DECL)
5270     {
5271       /* This happens for A::B where B is a template, and there are no
5272          template arguments.  */
5273       error ("invalid use of `%D'", name);
5274       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5275     }
5276
5277   namespace = ORIGINAL_NAMESPACE (namespace);
5278
5279   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5280     {
5281       template_id = name;
5282       name = TREE_OPERAND (name, 0);
5283       if (TREE_CODE (name) == OVERLOAD)
5284         name = DECL_NAME (OVL_CURRENT (name));
5285       else if (DECL_P (name))
5286         name = DECL_NAME (name);
5287     }
5288
5289   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5290
5291   cxx_binding_clear (&binding);
5292   if (!qualified_lookup_using_namespace (name, namespace, &binding, 0))
5293     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5294
5295   if (binding.value)
5296     {
5297       val = binding.value;
5298
5299       if (template_id)
5300         {
5301           if (DECL_CLASS_TEMPLATE_P (val))
5302             val = lookup_template_class (val,
5303                                          TREE_OPERAND (template_id, 1),
5304                                          /*in_decl=*/NULL_TREE,
5305                                          /*context=*/NULL_TREE,
5306                                          /*entering_scope=*/0,
5307                                          tf_error | tf_warning);
5308           else if (DECL_FUNCTION_TEMPLATE_P (val)
5309                    || TREE_CODE (val) == OVERLOAD)
5310             val = lookup_template_function (val,
5311                                             TREE_OPERAND (template_id, 1));
5312           else
5313             {
5314               error ("`%D::%D' is not a template",
5315                         namespace, name);
5316               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5317             }
5318         }
5319
5320       /* If we have a single function from a using decl, pull it out.  */
5321       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5322         val = OVL_FUNCTION (val);
5323
5324       /* Ignore built-in functions that haven't been prototyped yet.  */
5325       if (!val || !DECL_P(val)
5326           || !DECL_LANG_SPECIFIC(val)
5327           || !DECL_ANTICIPATED (val))
5328         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5329     }
5330
5331   error ("`%D' undeclared in namespace `%D'", name, namespace);
5332   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5333 }
5334
5335 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5336
5337 static hashval_t
5338 typename_hash (const void* k)
5339 {
5340   hashval_t hash;
5341   tree t = (tree) k;
5342
5343   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5344           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5345
5346   return hash;
5347 }
5348
5349 /* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5350
5351 static int
5352 typename_compare (const void * k1, const void * k2)
5353 {
5354   tree t1;
5355   tree t2;
5356   tree d1;
5357   tree d2;
5358
5359   t1 = (tree) k1;
5360   t2 = (tree) k2;
5361   d1 = TYPE_NAME (t1);
5362   d2 = TYPE_NAME (t2);
5363
5364   return (DECL_NAME (d1) == DECL_NAME (d2)
5365           && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5366           && ((TREE_TYPE (t1) != NULL_TREE)
5367               == (TREE_TYPE (t2) != NULL_TREE))
5368           && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5369           && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5370 }
5371
5372 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5373    the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5374    is non-NULL, this type is being created by the implicit typename
5375    extension, and BASE_TYPE is a type named `t' in some base class of
5376    `T' which depends on template parameters.
5377
5378    Returns the new TYPENAME_TYPE.  */
5379
5380 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5381
5382 tree
5383 build_typename_type (tree context, tree name, tree fullname)
5384 {
5385   tree t;
5386   tree d;
5387   PTR *e;
5388
5389   if (typename_htab == NULL)
5390     {
5391       typename_htab = htab_create_ggc (61, &typename_hash, 
5392                                        &typename_compare, NULL);
5393     }
5394
5395   /* Build the TYPENAME_TYPE.  */
5396   t = make_aggr_type (TYPENAME_TYPE);
5397   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5398   TYPENAME_TYPE_FULLNAME (t) = fullname;
5399
5400   /* Build the corresponding TYPE_DECL.  */
5401   d = build_decl (TYPE_DECL, name, t);
5402   TYPE_NAME (TREE_TYPE (d)) = d;
5403   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5404   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5405   DECL_ARTIFICIAL (d) = 1;
5406
5407   /* See if we already have this type.  */
5408   e = htab_find_slot (typename_htab, t, INSERT);
5409   if (*e)
5410     t = (tree) *e;
5411   else
5412     *e = t;
5413
5414   return t;
5415 }
5416
5417 /* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5418    unless an error occurs, in which case error_mark_node is returned.
5419    If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5420    set, we return that, rather than the _TYPE it corresponds to, in
5421    other cases we look through the type decl.  If TF_ERROR is set,
5422    complain about errors, otherwise be quiet.  */
5423
5424 tree
5425 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5426 {
5427   tree fullname;
5428
5429   if (name == error_mark_node
5430       || context == NULL_TREE
5431       || context == error_mark_node)
5432     return error_mark_node;
5433
5434   if (TYPE_P (name))
5435     {
5436       if (!(TYPE_LANG_SPECIFIC (name)
5437             && (CLASSTYPE_IS_TEMPLATE (name)
5438                 || CLASSTYPE_USE_TEMPLATE (name))))
5439         name = TYPE_IDENTIFIER (name);
5440       else
5441         /* Create a TEMPLATE_ID_EXPR for the type.  */
5442         name = build_nt (TEMPLATE_ID_EXPR,
5443                          CLASSTYPE_TI_TEMPLATE (name),
5444                          CLASSTYPE_TI_ARGS (name));
5445     }
5446   else if (TREE_CODE (name) == TYPE_DECL)
5447     name = DECL_NAME (name);
5448
5449   fullname = name;
5450
5451   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5452     {
5453       name = TREE_OPERAND (name, 0);
5454       if (TREE_CODE (name) == TEMPLATE_DECL)
5455         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5456     }
5457   if (TREE_CODE (name) == TEMPLATE_DECL)
5458     {
5459       error ("`%D' used without template parameters", name);
5460       return error_mark_node;
5461     }
5462   if (TREE_CODE (name) != IDENTIFIER_NODE)
5463     abort ();
5464
5465   if (TREE_CODE (context) == NAMESPACE_DECL)
5466     {
5467       /* We can get here from typename_sub0 in the explicit_template_type
5468          expansion.  Just fail.  */
5469       if (complain & tf_error)
5470         error ("no class template named `%#T' in `%#T'",
5471                   name, context);
5472       return error_mark_node;
5473     }
5474
5475   if (! uses_template_parms (context)
5476       || currently_open_class (context))
5477     {
5478       if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5479         {
5480           tree tmpl = NULL_TREE;
5481           if (IS_AGGR_TYPE (context))
5482             tmpl = lookup_field (context, name, 0, false);
5483           if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5484             {
5485               if (complain & tf_error)
5486                 error ("no class template named `%#T' in `%#T'",
5487                           name, context);
5488               return error_mark_node;
5489             }
5490
5491           if (complain & tf_error)
5492             perform_or_defer_access_check (context, tmpl);
5493
5494           return lookup_template_class (tmpl,
5495                                         TREE_OPERAND (fullname, 1),
5496                                         NULL_TREE, context,
5497                                         /*entering_scope=*/0,
5498                                         tf_error | tf_warning);
5499         }
5500       else
5501         {
5502           tree t;
5503
5504           if (!IS_AGGR_TYPE (context))
5505             {
5506               if (complain & tf_error)
5507                 error ("no type named `%#T' in `%#T'", name, context);
5508               return error_mark_node;
5509             }
5510
5511           t = lookup_field (context, name, 0, true);
5512           if (t)
5513             {
5514               if (TREE_CODE (t) != TYPE_DECL)
5515                 {
5516                   if (complain & tf_error)
5517                     error ("no type named `%#T' in `%#T'", name, context);
5518                   return error_mark_node;
5519                 }
5520
5521               if (complain & tf_error)
5522                 perform_or_defer_access_check (context, t);
5523
5524               if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5525                 t = TREE_TYPE (t);
5526               
5527               return t;
5528             }
5529         }
5530     }
5531
5532   /* If the CONTEXT is not a template type, then either the field is
5533      there now or its never going to be.  */
5534   if (!uses_template_parms (context))
5535     {
5536       if (complain & tf_error)
5537         error ("no type named `%#T' in `%#T'", name, context);
5538       return error_mark_node;
5539     }
5540
5541   return build_typename_type (context, name, fullname);
5542 }
5543
5544 /* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
5545    unless an error occurs, in which case error_mark_node is returned.
5546    If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5547    corresponds to.  If COMPLAIN zero, don't complain about any errors
5548    that occur.  */
5549
5550 tree
5551 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5552 {
5553   tree t;
5554   tree d;
5555
5556   if (TYPE_P (name))
5557     name = TYPE_IDENTIFIER (name);
5558   else if (DECL_P (name))
5559     name = DECL_NAME (name);
5560   if (TREE_CODE (name) != IDENTIFIER_NODE)
5561     abort ();
5562
5563   if (!uses_template_parms (context)
5564       || currently_open_class (context))
5565     {
5566       tree tmpl = NULL_TREE;
5567
5568       if (IS_AGGR_TYPE (context))
5569         tmpl = lookup_field (context, name, 0, false);
5570
5571       if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5572         {
5573           if (complain & tf_error)
5574             error ("no class template named `%#T' in `%#T'", name, context);
5575           return error_mark_node;
5576         }
5577       
5578       if (complain & tf_error)
5579         perform_or_defer_access_check (context, tmpl);
5580
5581       return tmpl;
5582     }
5583
5584   /* Build the UNBOUND_CLASS_TEMPLATE.  */
5585   t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5586   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5587   TREE_TYPE (t) = NULL_TREE;
5588
5589   /* Build the corresponding TEMPLATE_DECL.  */
5590   d = build_decl (TEMPLATE_DECL, name, t);
5591   TYPE_NAME (TREE_TYPE (d)) = d;
5592   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5593   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5594   DECL_ARTIFICIAL (d) = 1;
5595
5596   return t;
5597 }
5598
5599 /* Select the right _DECL from multiple choices.  */
5600
5601 static tree
5602 select_decl (cxx_binding *binding, int flags)
5603 {
5604   tree val;
5605   val = BINDING_VALUE (binding);
5606
5607   timevar_push (TV_NAME_LOOKUP);
5608   if (LOOKUP_NAMESPACES_ONLY (flags))
5609     {
5610       /* We are not interested in types.  */
5611       if (val && TREE_CODE (val) == NAMESPACE_DECL)
5612         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5613       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5614     }
5615
5616   /* If we could have a type and
5617      we have nothing or we need a type and have none.  */
5618   if (BINDING_TYPE (binding)
5619       && (!val || ((flags & LOOKUP_PREFER_TYPES)
5620                    && TREE_CODE (val) != TYPE_DECL)))
5621     val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5622   /* Don't return non-types if we really prefer types.  */
5623   else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5624            && (TREE_CODE (val) != TEMPLATE_DECL
5625                || !DECL_CLASS_TEMPLATE_P (val)))
5626     val = NULL_TREE;
5627
5628   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5629 }
5630
5631 /* Unscoped lookup of a global: iterate over current namespaces,
5632    considering using-directives.  If SPACESP is non-NULL, store a list
5633    of the namespaces we've considered in it.  */
5634
5635 tree
5636 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5637 {
5638   tree initial = current_decl_namespace ();
5639   tree scope = initial;
5640   tree siter;
5641   struct cp_binding_level *level;
5642   tree val = NULL_TREE;
5643   cxx_binding binding;
5644
5645   timevar_push (TV_NAME_LOOKUP);
5646   cxx_binding_clear (&binding);
5647   if (spacesp)
5648     *spacesp = NULL_TREE;
5649
5650   for (; !val; scope = CP_DECL_CONTEXT (scope))
5651     {
5652       cxx_binding *b =
5653          cxx_scope_find_binding_for_name (NAMESPACE_LEVEL (scope), name);
5654       if (spacesp)
5655         *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5656
5657       /* Ignore anticipated built-in functions.  */
5658       if (b && BINDING_VALUE (b)
5659           && DECL_P (BINDING_VALUE (b))
5660           && DECL_LANG_SPECIFIC (BINDING_VALUE (b))
5661           && DECL_ANTICIPATED (BINDING_VALUE (b)))
5662         /* Keep binding cleared.  */;
5663       else if (b)
5664         {
5665           /* Initialize binding for this context.  */
5666           binding.value = BINDING_VALUE (b);
5667           binding.type = BINDING_TYPE (b);
5668         }
5669
5670       /* Add all _DECLs seen through local using-directives.  */
5671       for (level = current_binding_level;
5672            !level->namespace_p;
5673            level = level->level_chain)
5674         if (!lookup_using_namespace (name, &binding, level->using_directives,
5675                                      scope, flags, spacesp))
5676           /* Give up because of error.  */
5677           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5678
5679       /* Add all _DECLs seen through global using-directives.  */
5680       /* XXX local and global using lists should work equally.  */
5681       siter = initial;
5682       while (1)
5683         {
5684           if (!lookup_using_namespace (name, &binding,
5685                                        DECL_NAMESPACE_USING (siter),
5686                                        scope, flags, spacesp))
5687             /* Give up because of error.  */
5688             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5689           if (siter == scope) break;
5690           siter = CP_DECL_CONTEXT (siter);
5691         }
5692
5693       val = select_decl (&binding, flags);
5694       if (scope == global_namespace)
5695         break;
5696     }
5697   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5698 }
5699
5700 /* Combine prefer_type and namespaces_only into flags.  */
5701
5702 static int
5703 lookup_flags (int prefer_type, int namespaces_only)
5704 {
5705   if (namespaces_only)
5706     return LOOKUP_PREFER_NAMESPACES;
5707   if (prefer_type > 1)
5708     return LOOKUP_PREFER_TYPES;
5709   if (prefer_type > 0)
5710     return LOOKUP_PREFER_BOTH;
5711   return 0;
5712 }
5713
5714 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5715    ignore it or not.  Subroutine of lookup_name_real.  */
5716
5717 static tree
5718 qualify_lookup (tree val, int flags)
5719 {
5720   if (val == NULL_TREE)
5721     return val;
5722   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5723     return val;
5724   if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5725     return val;
5726   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5727     return NULL_TREE;
5728   return val;
5729 }
5730
5731 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5732    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
5733    bindings.  
5734
5735    Returns a DECL (or OVERLOAD, or BASELINK) representing the
5736    declaration found.  */
5737
5738 tree
5739 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5740 {
5741   if (TREE_CODE (scope) == NAMESPACE_DECL)
5742     {
5743       cxx_binding binding;
5744
5745       cxx_binding_clear (&binding);
5746       flags |= LOOKUP_COMPLAIN;
5747       if (is_type_p)
5748         flags |= LOOKUP_PREFER_TYPES;
5749       if (!qualified_lookup_using_namespace (name, scope, &binding, flags))
5750         return NULL_TREE;
5751       return select_decl (&binding, flags);
5752     }
5753   else
5754     return lookup_member (scope, name, 0, is_type_p);
5755 }
5756
5757 /* Check to see whether or not DECL is a variable that would have been
5758    in scope under the ARM, but is not in scope under the ANSI/ISO
5759    standard.  If so, issue an error message.  If name lookup would
5760    work in both cases, but return a different result, this function
5761    returns the result of ANSI/ISO lookup.  Otherwise, it returns
5762    DECL.  */
5763
5764 tree
5765 check_for_out_of_scope_variable (tree decl)
5766 {
5767   tree shadowed;
5768
5769   /* We only care about out of scope variables.  */
5770   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5771     return decl;
5772
5773   shadowed = DECL_SHADOWED_FOR_VAR (decl);
5774   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5775          && DECL_DEAD_FOR_LOCAL (shadowed))
5776     shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5777   if (!shadowed)
5778     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5779   if (shadowed)
5780     {
5781       if (!DECL_ERROR_REPORTED (decl))
5782         {
5783           warning ("name lookup of `%D' changed",
5784                       DECL_NAME (decl));
5785           cp_warning_at ("  matches this `%D' under ISO standard rules",
5786                          shadowed);
5787           cp_warning_at ("  matches this `%D' under old rules", decl);
5788           DECL_ERROR_REPORTED (decl) = 1;
5789         }
5790       return shadowed;
5791     }
5792
5793   /* If we have already complained about this declaration, there's no
5794      need to do it again.  */
5795   if (DECL_ERROR_REPORTED (decl))
5796     return decl;
5797
5798   DECL_ERROR_REPORTED (decl) = 1;
5799   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5800     {
5801       error ("name lookup of `%D' changed for new ISO `for' scoping",
5802              DECL_NAME (decl));
5803       cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
5804       return error_mark_node;
5805     }
5806   else
5807     {
5808       pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5809                DECL_NAME (decl));
5810       cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
5811     }
5812
5813   return decl;
5814 }
5815
5816 /* Look up NAME in the current binding level and its superiors in the
5817    namespace of variables, functions and typedefs.  Return a ..._DECL
5818    node of some kind representing its definition if there is only one
5819    such declaration, or return a TREE_LIST with all the overloaded
5820    definitions if there are many, or return 0 if it is undefined.
5821
5822    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5823    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5824    Otherwise we prefer non-TYPE_DECLs.
5825
5826    If NONCLASS is nonzero, we don't look for the NAME in class scope,
5827    using IDENTIFIER_CLASS_VALUE.  */
5828
5829 tree
5830 lookup_name_real (tree name, int prefer_type, int nonclass, 
5831                   int namespaces_only, int flags)
5832 {
5833   cxx_binding *iter;
5834   tree val = NULL_TREE;
5835
5836   timevar_push (TV_NAME_LOOKUP);
5837   /* Conversion operators are handled specially because ordinary
5838      unqualified name lookup will not find template conversion
5839      operators.  */
5840   if (IDENTIFIER_TYPENAME_P (name)) 
5841     {
5842       struct cp_binding_level *level;
5843
5844       for (level = current_binding_level; 
5845            level && !level->namespace_p; 
5846            level = level->level_chain)
5847         {
5848           tree class_type;
5849           tree operators;
5850           
5851           /* A conversion operator can only be declared in a class 
5852              scope.  */
5853           if (level->parm_flag != 2)
5854             continue;
5855           
5856           /* Lookup the conversion operator in the class.  */
5857           class_type = level->this_entity;
5858           operators = lookup_fnfields (class_type, name, /*protect=*/0);
5859           if (operators)
5860             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5861         }
5862
5863       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5864     }
5865
5866   flags |= lookup_flags (prefer_type, namespaces_only);
5867
5868   /* First, look in non-namespace scopes.  */
5869
5870   if (current_class_type == NULL_TREE)
5871     nonclass = 1;
5872
5873   for (iter = IDENTIFIER_BINDING (name); iter; iter = iter->previous)
5874     {
5875       tree binding;
5876
5877       if (!LOCAL_BINDING_P (iter) && nonclass)
5878         /* We're not looking for class-scoped bindings, so keep going.  */
5879         continue;
5880
5881       /* If this is the kind of thing we're looking for, we're done.  */
5882       if (qualify_lookup (BINDING_VALUE (iter), flags))
5883         binding = BINDING_VALUE (iter);
5884       else if ((flags & LOOKUP_PREFER_TYPES)
5885                && qualify_lookup (BINDING_TYPE (iter), flags))
5886         binding = BINDING_TYPE (iter);
5887       else
5888         binding = NULL_TREE;
5889
5890       if (binding)
5891         {
5892           val = binding;
5893           break;
5894         }
5895     }
5896
5897   /* Now lookup in namespace scopes.  */
5898   if (!val)
5899     {
5900       tree t = unqualified_namespace_lookup (name, flags, 0);
5901       if (t)
5902         val = t;
5903     }
5904
5905   if (val)
5906     {
5907       /* If we have a single function from a using decl, pull it out.  */
5908       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5909         val = OVL_FUNCTION (val);
5910     }
5911
5912   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5913 }
5914
5915 tree
5916 lookup_name_nonclass (tree name)
5917 {
5918   return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
5919 }
5920
5921 tree
5922 lookup_function_nonclass (tree name, tree args)
5923 {
5924   return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
5925 }
5926
5927 tree
5928 lookup_name (tree name, int prefer_type)
5929 {
5930   return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
5931 }
5932
5933 /* Similar to `lookup_name' but look only in the innermost non-class
5934    binding level.  */
5935
5936 tree
5937 lookup_name_current_level (tree name)
5938 {
5939   struct cp_binding_level *b;
5940   tree t = NULL_TREE;
5941
5942   timevar_push (TV_NAME_LOOKUP);
5943   b = current_binding_level;
5944   while (b->parm_flag == 2)
5945     b = b->level_chain;
5946
5947   if (b->namespace_p)
5948     {
5949       t = IDENTIFIER_NAMESPACE_VALUE (name);
5950
5951       /* extern "C" function() */
5952       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
5953         t = TREE_VALUE (t);
5954     }
5955   else if (IDENTIFIER_BINDING (name)
5956            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
5957     {
5958       while (1)
5959         {
5960           if (BINDING_SCOPE (IDENTIFIER_BINDING (name)) == b)
5961             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
5962
5963           if (b->keep == 2)
5964             b = b->level_chain;
5965           else
5966             break;
5967         }
5968     }
5969
5970   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
5971 }
5972
5973 /* Like lookup_name_current_level, but for types.  */
5974
5975 tree
5976 lookup_type_current_level (tree name)
5977 {
5978   register tree t = NULL_TREE;
5979
5980   timevar_push (TV_NAME_LOOKUP);
5981   my_friendly_assert (! current_binding_level->namespace_p, 980716);
5982
5983   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
5984       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
5985     {
5986       struct cp_binding_level *b = current_binding_level;
5987       while (1)
5988         {
5989           if (purpose_member (name, b->type_shadowed))
5990             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
5991                                     REAL_IDENTIFIER_TYPE_VALUE (name));
5992           if (b->keep == 2)
5993             b = b->level_chain;
5994           else
5995             break;
5996         }
5997     }
5998
5999   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6000 }
6001
6002 \f
6003 /* Push the declarations of builtin types into the namespace.
6004    RID_INDEX is the index of the builtin type
6005    in the array RID_POINTERS.  NAME is the name used when looking
6006    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6007
6008 void
6009 record_builtin_type (enum rid rid_index, 
6010                      const char* name, 
6011                      tree type)
6012 {
6013   tree rname = NULL_TREE, tname = NULL_TREE;
6014   tree tdecl = NULL_TREE;
6015
6016   if ((int) rid_index < (int) RID_MAX)
6017     rname = ridpointers[(int) rid_index];
6018   if (name)
6019     tname = get_identifier (name);
6020
6021   if (tname)
6022     {
6023       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6024       set_identifier_type_value (tname, NULL_TREE);
6025       if ((int) rid_index < (int) RID_MAX)
6026         /* Built-in types live in the global namespace.  */
6027         SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6028     }
6029   if (rname != NULL_TREE)
6030     {
6031       if (tname != NULL_TREE)
6032         {
6033           set_identifier_type_value (rname, NULL_TREE);
6034           SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6035         }
6036       else
6037         {
6038           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6039           set_identifier_type_value (rname, NULL_TREE);
6040         }
6041     }
6042 }
6043
6044 /* Record one of the standard Java types.
6045  * Declare it as having the given NAME.
6046  * If SIZE > 0, it is the size of one of the integral types;
6047  * otherwise it is the negative of the size of one of the other types.  */
6048
6049 static tree
6050 record_builtin_java_type (const char* name, int size)
6051 {
6052   tree type, decl;
6053   if (size > 0)
6054     type = make_signed_type (size);
6055   else if (size > -32)
6056     { /* "__java_char" or ""__java_boolean".  */
6057       type = make_unsigned_type (-size);
6058       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6059     }
6060   else
6061     { /* "__java_float" or ""__java_double".  */
6062       type = make_node (REAL_TYPE);
6063       TYPE_PRECISION (type) = - size;
6064       layout_type (type);
6065     }
6066   record_builtin_type (RID_MAX, name, type);
6067   decl = TYPE_NAME (type);
6068
6069   /* Suppress generate debug symbol entries for these types,
6070      since for normal C++ they are just clutter.
6071      However, push_lang_context undoes this if extern "Java" is seen.  */
6072   DECL_IGNORED_P (decl) = 1;
6073
6074   TYPE_FOR_JAVA (type) = 1;
6075   return type;
6076 }
6077
6078 /* Push a type into the namespace so that the back-ends ignore it.  */
6079
6080 static void
6081 record_unknown_type (tree type, const char* name)
6082 {
6083   tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6084   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6085   DECL_IGNORED_P (decl) = 1;
6086   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6087   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6088   TYPE_ALIGN (type) = 1;
6089   TYPE_USER_ALIGN (type) = 0;
6090   TYPE_MODE (type) = TYPE_MODE (void_type_node);
6091 }
6092
6093 /* An string for which we should create an IDENTIFIER_NODE at
6094    startup.  */
6095
6096 typedef struct predefined_identifier
6097 {
6098   /* The name of the identifier.  */
6099   const char *const name;
6100   /* The place where the IDENTIFIER_NODE should be stored.  */
6101   tree *const node;
6102   /* Nonzero if this is the name of a constructor or destructor.  */
6103   const int ctor_or_dtor_p;
6104 } predefined_identifier;
6105
6106 /* Create all the predefined identifiers.  */
6107
6108 static void
6109 initialize_predefined_identifiers (void)
6110 {
6111   const predefined_identifier *pid;
6112
6113   /* A table of identifiers to create at startup.  */
6114   static const predefined_identifier predefined_identifiers[] = {
6115     { "C++", &lang_name_cplusplus, 0 },
6116     { "C", &lang_name_c, 0 },
6117     { "Java", &lang_name_java, 0 },
6118     { CTOR_NAME, &ctor_identifier, 1 },
6119     { "__base_ctor", &base_ctor_identifier, 1 },
6120     { "__comp_ctor", &complete_ctor_identifier, 1 },
6121     { DTOR_NAME, &dtor_identifier, 1 },
6122     { "__comp_dtor", &complete_dtor_identifier, 1 },
6123     { "__base_dtor", &base_dtor_identifier, 1 },
6124     { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6125     { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6126     { "nelts", &nelts_identifier, 0 },
6127     { THIS_NAME, &this_identifier, 0 },
6128     { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6129     { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6130     { "_vptr", &vptr_identifier, 0 },
6131     { "__vtt_parm", &vtt_parm_identifier, 0 },
6132     { "::", &global_scope_name, 0 },
6133     { "std", &std_identifier, 0 },
6134     { NULL, NULL, 0 }
6135   };
6136
6137   for (pid = predefined_identifiers; pid->name; ++pid)
6138     {
6139       *pid->node = get_identifier (pid->name);
6140       if (pid->ctor_or_dtor_p)
6141         IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6142     }
6143 }
6144
6145 /* Create the predefined scalar types of C,
6146    and some nodes representing standard constants (0, 1, (void *)0).
6147    Initialize the global binding level.
6148    Make definitions for built-in primitive functions.  */
6149
6150 void
6151 cxx_init_decl_processing (void)
6152 {
6153   tree void_ftype;
6154   tree void_ftype_ptr;
6155
6156   /* Create all the identifiers we need.  */
6157   initialize_predefined_identifiers ();
6158
6159   /* Fill in back-end hooks.  */
6160   lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6161
6162   /* Create the global variables.  */
6163   push_to_top_level ();
6164
6165   current_function_decl = NULL_TREE;
6166   current_binding_level = NULL_BINDING_LEVEL;
6167   free_binding_level = NULL_BINDING_LEVEL;
6168   /* Enter the global namespace.  */
6169   my_friendly_assert (global_namespace == NULL_TREE, 375);
6170   global_namespace = build_lang_decl (NAMESPACE_DECL, global_scope_name,
6171                                       void_type_node);
6172   initial_push_namespace_scope (global_namespace);
6173
6174   current_lang_name = NULL_TREE;
6175
6176   /* Adjust various flags based on command-line settings.  */
6177   if (! flag_permissive && ! pedantic)
6178     flag_pedantic_errors = 1;
6179   if (!flag_no_inline)
6180     {
6181       flag_inline_trees = 1;
6182       flag_no_inline = 1;
6183     }
6184   if (flag_inline_functions)
6185     {
6186       flag_inline_trees = 2;
6187       flag_inline_functions = 0;
6188     }
6189
6190   /* Force minimum function alignment if using the least significant
6191      bit of function pointers to store the virtual bit.  */
6192   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6193       && force_align_functions_log < 1)
6194     force_align_functions_log = 1;
6195
6196   /* Initially, C.  */
6197   current_lang_name = lang_name_c;
6198
6199   build_common_tree_nodes (flag_signed_char);
6200
6201   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6202   TREE_TYPE (error_mark_list) = error_mark_node;
6203
6204   /* Create the `std' namespace.  */
6205   push_namespace (std_identifier);
6206   std_node = current_namespace;
6207   pop_namespace ();
6208
6209   c_common_nodes_and_builtins ();
6210
6211   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6212   java_short_type_node = record_builtin_java_type ("__java_short", 16);
6213   java_int_type_node = record_builtin_java_type ("__java_int", 32);
6214   java_long_type_node = record_builtin_java_type ("__java_long", 64);
6215   java_float_type_node = record_builtin_java_type ("__java_float", -32);
6216   java_double_type_node = record_builtin_java_type ("__java_double", -64);
6217   java_char_type_node = record_builtin_java_type ("__java_char", -16);
6218   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6219
6220   integer_two_node = build_int_2 (2, 0);
6221   TREE_TYPE (integer_two_node) = integer_type_node;
6222   integer_three_node = build_int_2 (3, 0);
6223   TREE_TYPE (integer_three_node) = integer_type_node;
6224
6225   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6226   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6227   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6228   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6229   TYPE_PRECISION (boolean_type_node) = 1;
6230   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6231   boolean_false_node = build_int_2 (0, 0);
6232   TREE_TYPE (boolean_false_node) = boolean_type_node;
6233   boolean_true_node = build_int_2 (1, 0);
6234   TREE_TYPE (boolean_true_node) = boolean_type_node;
6235
6236   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6237
6238 #if 0
6239   record_builtin_type (RID_MAX, NULL, string_type_node);
6240 #endif
6241
6242   delta_type_node = ptrdiff_type_node;
6243   vtable_index_type = ptrdiff_type_node;
6244
6245   vtt_parm_type = build_pointer_type (const_ptr_type_node);
6246   void_ftype = build_function_type (void_type_node, void_list_node);
6247   void_ftype_ptr = build_function_type (void_type_node,
6248                                         tree_cons (NULL_TREE,
6249                                                    ptr_type_node, 
6250                                                    void_list_node));
6251   void_ftype_ptr
6252     = build_exception_variant (void_ftype_ptr, empty_except_spec);
6253
6254   /* C++ extensions */
6255
6256   unknown_type_node = make_node (UNKNOWN_TYPE);
6257   record_unknown_type (unknown_type_node, "unknown type");
6258
6259   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6260   TREE_TYPE (unknown_type_node) = unknown_type_node;
6261
6262   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6263      result.  */
6264   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6265   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6266
6267   {
6268     /* Make sure we get a unique function type, so we can give
6269        its pointer type a name.  (This wins for gdb.) */
6270     tree vfunc_type = make_node (FUNCTION_TYPE);
6271     TREE_TYPE (vfunc_type) = integer_type_node;
6272     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6273     layout_type (vfunc_type);
6274
6275     vtable_entry_type = build_pointer_type (vfunc_type);
6276   }
6277   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6278
6279   vtbl_type_node
6280     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6281   layout_type (vtbl_type_node);
6282   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6283   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6284   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6285   layout_type (vtbl_ptr_type_node);
6286   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6287
6288   push_namespace (get_identifier ("__cxxabiv1"));
6289   abi_node = current_namespace;
6290   pop_namespace ();
6291
6292   global_type_node = make_node (LANG_TYPE);
6293   record_unknown_type (global_type_node, "global type");
6294
6295   /* Now, C++.  */
6296   current_lang_name = lang_name_cplusplus;
6297
6298   {
6299     tree bad_alloc_type_node, newtype, deltype;
6300     tree ptr_ftype_sizetype;
6301
6302     push_namespace (std_identifier);
6303     bad_alloc_type_node 
6304       = xref_tag (class_type, get_identifier ("bad_alloc"), 
6305                   /*attributes=*/NULL_TREE, 1);
6306     pop_namespace ();
6307     ptr_ftype_sizetype 
6308       = build_function_type (ptr_type_node,
6309                              tree_cons (NULL_TREE,
6310                                         size_type_node,
6311                                         void_list_node));
6312     newtype = build_exception_variant
6313       (ptr_ftype_sizetype, add_exception_specifier
6314        (NULL_TREE, bad_alloc_type_node, -1));
6315     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6316     push_cp_library_fn (NEW_EXPR, newtype);
6317     push_cp_library_fn (VEC_NEW_EXPR, newtype);
6318     global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6319     push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6320   }
6321
6322   abort_fndecl
6323     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6324
6325   /* Perform other language dependent initializations.  */
6326   init_class_processing ();
6327   init_search_processing ();
6328   init_rtti_processing ();
6329
6330   if (flag_exceptions)
6331     init_exception_processing ();
6332
6333   if (! supports_one_only ())
6334     flag_weak = 0;
6335
6336   make_fname_decl = cp_make_fname_decl;
6337   start_fname_decls ();
6338
6339   /* Show we use EH for cleanups.  */
6340   using_eh_for_cleanups ();
6341
6342   /* Maintain consistency.  Perhaps we should just complain if they
6343      say -fwritable-strings?  */
6344   if (flag_writable_strings)
6345     flag_const_strings = 0;
6346 }
6347
6348 /* Generate an initializer for a function naming variable from
6349    NAME. NAME may be NULL, in which case we generate a special
6350    ERROR_MARK node which should be replaced later.  */
6351
6352 tree
6353 cp_fname_init (const char* name)
6354 {
6355   tree domain = NULL_TREE;
6356   tree type;
6357   tree init = NULL_TREE;
6358   size_t length = 0;
6359
6360   if (name)
6361     {
6362       length = strlen (name);
6363       domain = build_index_type (size_int (length));
6364       init = build_string (length + 1, name);
6365     }
6366   
6367   type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6368   type = build_cplus_array_type (type, domain);
6369
6370   if (init)
6371     TREE_TYPE (init) = type;
6372   else
6373     /* We don't know the value until instantiation time. Make
6374        something which will be digested now, but replaced later.  */
6375     init = build (ERROR_MARK, type);
6376   
6377   return init;
6378 }
6379
6380 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6381    decl, NAME is the initialization string and TYPE_DEP indicates whether
6382    NAME depended on the type of the function. We make use of that to detect
6383    __PRETTY_FUNCTION__ inside a template fn. This is being done
6384    lazily at the point of first use, so we musn't push the decl now.  */
6385
6386 static tree
6387 cp_make_fname_decl (tree id, int type_dep)
6388 {
6389   const char *const name = (type_dep && processing_template_decl
6390                       ? NULL : fname_as_string (type_dep));
6391   tree init = cp_fname_init (name);
6392   tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6393
6394   /* As we're using pushdecl_with_scope, we must set the context.  */
6395   DECL_CONTEXT (decl) = current_function_decl;
6396   DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6397       
6398   TREE_STATIC (decl) = 1;
6399   TREE_READONLY (decl) = 1;
6400   DECL_ARTIFICIAL (decl) = 1;
6401   DECL_INITIAL (decl) = init;
6402   
6403   TREE_USED (decl) = 1;
6404
6405   if (current_function_decl)
6406     {
6407       struct cp_binding_level *b = current_binding_level;
6408       while (b->level_chain->parm_flag == 0)
6409         b = b->level_chain;
6410       pushdecl_with_scope (decl, b);
6411     }   
6412
6413   cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6414       
6415   return decl;
6416 }
6417
6418 /* Make a definition for a builtin function named NAME in the current
6419    namespace, whose data type is TYPE and whose context is CONTEXT.
6420    TYPE should be a function type with argument types.
6421
6422    CLASS and CODE tell later passes how to compile calls to this function.
6423    See tree.h for possible values.
6424
6425    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6426    the name to be called if we can't opencode the function.
6427    If ATTRS is nonzero, use that for the function's attribute
6428    list.  */
6429
6430 static tree
6431 builtin_function_1 (const char* name,
6432                     tree type,
6433                     tree context,
6434                     int code,
6435                     enum built_in_class class,
6436                     const char* libname,
6437                     tree attrs)
6438 {
6439   tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6440   DECL_BUILT_IN_CLASS (decl) = class;
6441   DECL_FUNCTION_CODE (decl) = code;
6442   DECL_CONTEXT (decl) = context;
6443
6444   pushdecl (decl);
6445
6446   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6447      we cannot change DECL_ASSEMBLER_NAME until we have installed this
6448      function in the namespace.  */
6449   if (libname)
6450     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6451   make_decl_rtl (decl, NULL);
6452
6453   /* Warn if a function in the namespace for users
6454      is used without an occasion to consider it declared.  */
6455   if (name[0] != '_' || name[1] != '_')
6456     DECL_ANTICIPATED (decl) = 1;
6457
6458   /* Possibly apply some default attributes to this built-in function.  */
6459   if (attrs)
6460     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6461   else
6462     decl_attributes (&decl, NULL_TREE, 0);
6463
6464   return decl;
6465 }
6466
6467 /* Entry point for the benefit of c_common_nodes_and_builtins.
6468
6469    Make a defintion for a builtin function named NAME and whose data type
6470    is TYPE.  TYPE should be a function type with argument types.  This
6471    function places the anticipated declaration in the global namespace
6472    and additionally in the std namespace if appropriate.
6473
6474    CLASS and CODE tell later passes how to compile calls to this function.
6475    See tree.h for possible values.
6476
6477    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6478    the name to be called if we can't opencode the function.
6479
6480    If ATTRS is nonzero, use that for the function's attribute
6481    list.  */
6482
6483 tree
6484 builtin_function (const char* name,
6485                   tree type,
6486                   int code,
6487                   enum built_in_class class,
6488                   const char* libname,
6489                   tree attrs)
6490 {
6491   /* All builtins that don't begin with an '_' should additionally
6492      go in the 'std' namespace.  */
6493   if (name[0] != '_')
6494     {
6495       push_namespace (std_identifier);
6496       builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6497       pop_namespace ();
6498     }
6499
6500   return builtin_function_1 (name, type, NULL_TREE, code,
6501                              class, libname, attrs);
6502 }
6503
6504 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6505    function.  Not called directly.  */
6506
6507 static tree
6508 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6509 {
6510   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6511   DECL_EXTERNAL (fn) = 1;
6512   TREE_PUBLIC (fn) = 1;
6513   DECL_ARTIFICIAL (fn) = 1;
6514   TREE_NOTHROW (fn) = 1;
6515   SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6516   SET_DECL_LANGUAGE (fn, lang_c);
6517   return fn;
6518 }
6519
6520 /* Returns the _DECL for a library function with C linkage.
6521    We assume that such functions never throw; if this is incorrect,
6522    callers should unset TREE_NOTHROW.  */
6523
6524 tree
6525 build_library_fn (tree name, tree type)
6526 {
6527   return build_library_fn_1 (name, ERROR_MARK, type);
6528 }
6529
6530 /* Returns the _DECL for a library function with C++ linkage.  */
6531
6532 static tree
6533 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6534 {
6535   tree fn = build_library_fn_1 (name, operator_code, type);
6536   TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6537   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6538   SET_DECL_LANGUAGE (fn, lang_cplusplus);
6539   set_mangled_name_for_decl (fn);
6540   return fn;
6541 }
6542
6543 /* Like build_library_fn, but takes a C string instead of an
6544    IDENTIFIER_NODE.  */
6545
6546 tree
6547 build_library_fn_ptr (const char* name, tree type)
6548 {
6549   return build_library_fn (get_identifier (name), type);
6550 }
6551
6552 /* Like build_cp_library_fn, but takes a C string instead of an
6553    IDENTIFIER_NODE.  */
6554
6555 tree
6556 build_cp_library_fn_ptr (const char* name, tree type)
6557 {
6558   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6559 }
6560
6561 /* Like build_library_fn, but also pushes the function so that we will
6562    be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
6563
6564 tree
6565 push_library_fn (tree name, tree type)
6566 {
6567   tree fn = build_library_fn (name, type);
6568   pushdecl_top_level (fn);
6569   return fn;
6570 }
6571
6572 /* Like build_cp_library_fn, but also pushes the function so that it
6573    will be found by normal lookup.  */
6574
6575 static tree
6576 push_cp_library_fn (enum tree_code operator_code, tree type)
6577 {
6578   tree fn = build_cp_library_fn (ansi_opname (operator_code),
6579                                  operator_code,
6580                                  type);
6581   pushdecl (fn);
6582   return fn;
6583 }
6584
6585 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6586    a FUNCTION_TYPE.  */
6587
6588 tree
6589 push_void_library_fn (tree name, tree parmtypes)
6590 {
6591   tree type = build_function_type (void_type_node, parmtypes);
6592   return push_library_fn (name, type);
6593 }
6594
6595 /* Like push_library_fn, but also note that this function throws
6596    and does not return.  Used for __throw_foo and the like.  */
6597
6598 tree
6599 push_throw_library_fn (tree name, tree type)
6600 {
6601   tree fn = push_library_fn (name, type);
6602   TREE_THIS_VOLATILE (fn) = 1;
6603   TREE_NOTHROW (fn) = 0;
6604   return fn;
6605 }
6606
6607 /* Apply default attributes to a function, if a system function with default
6608    attributes.  */
6609
6610 void
6611 cxx_insert_default_attributes (tree decl)
6612 {
6613   if (!DECL_EXTERN_C_FUNCTION_P (decl))
6614     return;
6615   if (!TREE_PUBLIC (decl))
6616     return;
6617   c_common_insert_default_attributes (decl);
6618 }
6619 \f
6620 /* When we call finish_struct for an anonymous union, we create
6621    default copy constructors and such.  But, an anonymous union
6622    shouldn't have such things; this function undoes the damage to the
6623    anonymous union type T.
6624
6625    (The reason that we create the synthesized methods is that we don't
6626    distinguish `union { int i; }' from `typedef union { int i; } U'.
6627    The first is an anonymous union; the second is just an ordinary
6628    union type.)  */
6629
6630 void
6631 fixup_anonymous_aggr (tree t)
6632 {
6633   tree *q;
6634
6635   /* Wipe out memory of synthesized methods */
6636   TYPE_HAS_CONSTRUCTOR (t) = 0;
6637   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6638   TYPE_HAS_INIT_REF (t) = 0;
6639   TYPE_HAS_CONST_INIT_REF (t) = 0;
6640   TYPE_HAS_ASSIGN_REF (t) = 0;
6641   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6642
6643   /* Splice the implicitly generated functions out of the TYPE_METHODS
6644      list.  */
6645   q = &TYPE_METHODS (t);
6646   while (*q)
6647     {
6648       if (DECL_ARTIFICIAL (*q))
6649         *q = TREE_CHAIN (*q);
6650       else
6651         q = &TREE_CHAIN (*q);
6652     }
6653
6654   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
6655   if (TYPE_METHODS (t))
6656     cp_error_at ("an anonymous union cannot have function members", t);
6657
6658   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6659      assignment operators (because they cannot have these methods themselves).
6660      For anonymous unions this is already checked because they are not allowed
6661      in any union, otherwise we have to check it.  */
6662   if (TREE_CODE (t) != UNION_TYPE)
6663     {
6664       tree field, type;
6665
6666       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6667         if (TREE_CODE (field) == FIELD_DECL)
6668           {
6669             type = TREE_TYPE (field);
6670             if (CLASS_TYPE_P (type))
6671               {
6672                 if (TYPE_NEEDS_CONSTRUCTING (type))
6673                   cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6674                                field);
6675                 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6676                   cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6677                                field);
6678                 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6679                   cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6680                                field);
6681               }
6682           }
6683     }
6684 }
6685
6686 /* Make sure that a declaration with no declarator is well-formed, i.e.
6687    just declares a tagged type or anonymous union.
6688
6689    Returns the type declared; or NULL_TREE if none.  */
6690
6691 tree
6692 check_tag_decl (tree declspecs)
6693 {
6694   int found_type = 0;
6695   int saw_friend = 0;
6696   int saw_typedef = 0;
6697   tree ob_modifier = NULL_TREE;
6698   register tree link;
6699   /* If a class, struct, or enum type is declared by the DECLSPECS
6700      (i.e, if a class-specifier, enum-specifier, or non-typename
6701      elaborated-type-specifier appears in the DECLSPECS),
6702      DECLARED_TYPE is set to the corresponding type.  */
6703   tree declared_type = NULL_TREE;
6704   bool error_p = false;
6705
6706   for (link = declspecs; link; link = TREE_CHAIN (link))
6707     {
6708       tree value = TREE_VALUE (link);
6709
6710       if (TYPE_P (value)
6711           || TREE_CODE (value) == TYPE_DECL
6712           || (TREE_CODE (value) == IDENTIFIER_NODE
6713               && IDENTIFIER_GLOBAL_VALUE (value)
6714               && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6715         {
6716           ++found_type;
6717
6718           if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6719             {
6720               if (! in_system_header)
6721                 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6722               return NULL_TREE;
6723             }
6724
6725           if (TYPE_P (value)
6726               && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6727                   || TREE_CODE (value) == ENUMERAL_TYPE))
6728             {
6729               my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6730               declared_type = value;
6731             }
6732         }
6733       else if (value == ridpointers[(int) RID_TYPEDEF])
6734         saw_typedef = 1;
6735       else if (value == ridpointers[(int) RID_FRIEND])
6736         {
6737           if (current_class_type == NULL_TREE
6738               || current_scope () != current_class_type)
6739             ob_modifier = value;
6740           else
6741             saw_friend = 1;
6742         }
6743       else if (value == ridpointers[(int) RID_STATIC]
6744                || value == ridpointers[(int) RID_EXTERN]
6745                || value == ridpointers[(int) RID_AUTO]
6746                || value == ridpointers[(int) RID_REGISTER]
6747                || value == ridpointers[(int) RID_INLINE]
6748                || value == ridpointers[(int) RID_VIRTUAL]
6749                || value == ridpointers[(int) RID_CONST]
6750                || value == ridpointers[(int) RID_VOLATILE]
6751                || value == ridpointers[(int) RID_EXPLICIT]
6752                || value == ridpointers[(int) RID_THREAD])
6753         ob_modifier = value;
6754       else if (value == error_mark_node)
6755         error_p = true;
6756     }
6757
6758   if (found_type > 1)
6759     error ("multiple types in one declaration");
6760
6761   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6762     pedwarn ("declaration does not declare anything");
6763   /* Check for an anonymous union.  */
6764   else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6765            && TYPE_ANONYMOUS_P (declared_type))
6766     {
6767       /* 7/3 In a simple-declaration, the optional init-declarator-list
6768          can be omitted only when declaring a class (clause 9) or
6769          enumeration (7.2), that is, when the decl-specifier-seq contains
6770          either a class-specifier, an elaborated-type-specifier with
6771          a class-key (9.1), or an enum-specifier.  In these cases and
6772          whenever a class-specifier or enum-specifier is present in the
6773          decl-specifier-seq, the identifiers in these specifiers are among
6774          the names being declared by the declaration (as class-name,
6775          enum-names, or enumerators, depending on the syntax).  In such
6776          cases, and except for the declaration of an unnamed bit-field (9.6),
6777          the decl-specifier-seq shall introduce one or more names into the
6778          program, or shall redeclare a name introduced by a previous
6779          declaration.  [Example:
6780              enum { };            // ill-formed
6781              typedef class { };   // ill-formed
6782          --end example]  */
6783       if (saw_typedef)
6784         {
6785           error ("missing type-name in typedef-declaration");
6786           return NULL_TREE;
6787         }
6788       /* Anonymous unions are objects, so they can have specifiers.  */;
6789       SET_ANON_AGGR_TYPE_P (declared_type);
6790
6791       if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 
6792           && !in_system_header)
6793         pedwarn ("ISO C++ prohibits anonymous structs");
6794     }
6795
6796   else if (ob_modifier)
6797     {
6798       if (ob_modifier == ridpointers[(int) RID_INLINE]
6799           || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6800         error ("`%D' can only be specified for functions", ob_modifier);
6801       else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6802         error ("`%D' can only be specified inside a class", ob_modifier);
6803       else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6804         error ("`%D' can only be specified for constructors",
6805                   ob_modifier);
6806       else
6807         error ("`%D' can only be specified for objects and functions",
6808                   ob_modifier);
6809     }
6810
6811   return declared_type;
6812 }
6813
6814 /* Called when a declaration is seen that contains no names to declare.
6815    If its type is a reference to a structure, union or enum inherited
6816    from a containing scope, shadow that tag name for the current scope
6817    with a forward reference.
6818    If its type defines a new named structure or union
6819    or defines an enum, it is valid but we need not do anything here.
6820    Otherwise, it is an error.
6821
6822    C++: may have to grok the declspecs to learn about static,
6823    complain for anonymous unions.  
6824
6825    Returns the TYPE declared -- or NULL_TREE if none.  */
6826
6827 tree
6828 shadow_tag (tree declspecs)
6829 {
6830   tree t = check_tag_decl (declspecs);
6831
6832   if (!t)
6833     return NULL_TREE;
6834
6835   maybe_process_partial_specialization (t);
6836
6837   /* This is where the variables in an anonymous union are
6838      declared.  An anonymous union declaration looks like:
6839      union { ... } ;
6840      because there is no declarator after the union, the parser
6841      sends that declaration here.  */
6842   if (ANON_AGGR_TYPE_P (t))
6843     {
6844       fixup_anonymous_aggr (t);
6845
6846       if (TYPE_FIELDS (t))
6847         {
6848           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6849                                       NULL);
6850           finish_anon_union (decl);
6851         }
6852     }
6853
6854   return t;
6855 }
6856 \f
6857 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
6858
6859 tree
6860 groktypename (tree typename)
6861 {
6862   tree specs, attrs;
6863   tree type;
6864   if (TREE_CODE (typename) != TREE_LIST)
6865     return typename;
6866   split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6867   type = grokdeclarator (TREE_VALUE (typename), specs,
6868                          TYPENAME, 0, &attrs);
6869   if (attrs)
6870     cplus_decl_attributes (&type, attrs, 0);
6871   return type;
6872 }
6873
6874 /* Decode a declarator in an ordinary declaration or data definition.
6875    This is called as soon as the type information and variable name
6876    have been parsed, before parsing the initializer if any.
6877    Here we create the ..._DECL node, fill in its type,
6878    and put it on the list of decls for the current context.
6879    The ..._DECL node is returned as the value.
6880
6881    Exception: for arrays where the length is not specified,
6882    the type is left null, to be filled in by `cp_finish_decl'.
6883
6884    Function definitions do not come here; they go to start_function
6885    instead.  However, external and forward declarations of functions
6886    do go through here.  Structure field declarations are done by
6887    grokfield and not through here.  */
6888
6889 tree
6890 start_decl (tree declarator, 
6891             tree declspecs, 
6892             int initialized, 
6893             tree attributes, 
6894             tree prefix_attributes)
6895 {
6896   tree decl;
6897   register tree type, tem;
6898   tree context;
6899
6900   /* This should only be done once on the top most decl.  */
6901   if (have_extern_spec)
6902     {
6903       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
6904                              declspecs);
6905       have_extern_spec = false;
6906     }
6907
6908   /* An object declared as __attribute__((deprecated)) suppresses
6909      warnings of uses of other deprecated items.  */
6910   if (lookup_attribute ("deprecated", attributes))
6911     deprecated_state = DEPRECATED_SUPPRESS;
6912
6913   attributes = chainon (attributes, prefix_attributes);
6914
6915   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
6916                          &attributes);
6917
6918   deprecated_state = DEPRECATED_NORMAL;
6919
6920   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
6921     return NULL_TREE;
6922
6923   type = TREE_TYPE (decl);
6924
6925   if (type == error_mark_node)
6926     return NULL_TREE;
6927
6928   context = DECL_CONTEXT (decl);
6929
6930   if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
6931       && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
6932     {
6933       /* When parsing the initializer, lookup should use the object's
6934          namespace.  */
6935       push_decl_namespace (context);
6936     }
6937
6938   /* We are only interested in class contexts, later.  */
6939   if (context && TREE_CODE (context) == NAMESPACE_DECL)
6940     context = NULL_TREE;
6941
6942   if (initialized)
6943     /* Is it valid for this decl to have an initializer at all?
6944        If not, set INITIALIZED to zero, which will indirectly
6945        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
6946     switch (TREE_CODE (decl))
6947       {
6948       case TYPE_DECL:
6949         error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
6950         initialized = 0;
6951         break;
6952
6953       case FUNCTION_DECL:
6954         error ("function `%#D' is initialized like a variable", decl);
6955         initialized = 0;
6956         break;
6957
6958       default:
6959         break;
6960       }
6961
6962   if (initialized)
6963     {
6964       if (! toplevel_bindings_p ()
6965           && DECL_EXTERNAL (decl))
6966         warning ("declaration of `%#D' has `extern' and is initialized",
6967                     decl);
6968       DECL_EXTERNAL (decl) = 0;
6969       if (toplevel_bindings_p ())
6970         TREE_STATIC (decl) = 1;
6971
6972       /* Tell `pushdecl' this is an initialized decl
6973          even though we don't yet have the initializer expression.
6974          Also tell `cp_finish_decl' it may store the real initializer.  */
6975       DECL_INITIAL (decl) = error_mark_node;
6976     }
6977
6978   /* Set attributes here so if duplicate decl, will have proper attributes.  */
6979   cplus_decl_attributes (&decl, attributes, 0);
6980
6981   /* If #pragma weak was used, mark the decl weak now.  */
6982   if (global_scope_p (current_binding_level))
6983     maybe_apply_pragma_weak (decl);
6984
6985   if (TREE_CODE (decl) == FUNCTION_DECL
6986       && DECL_DECLARED_INLINE_P (decl)
6987       && DECL_UNINLINABLE (decl)
6988       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
6989     warning ("%Hinline function '%D' given attribute noinline",
6990              &DECL_SOURCE_LOCATION (decl), decl);
6991
6992   if (context && COMPLETE_TYPE_P (complete_type (context)))
6993     {
6994       push_nested_class (context);
6995
6996       if (TREE_CODE (decl) == VAR_DECL)
6997         {
6998           tree field = lookup_field (context, DECL_NAME (decl), 0, false);
6999           if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7000             error ("`%#D' is not a static member of `%#T'", decl, context);
7001           else
7002             {
7003               if (DECL_CONTEXT (field) != context)
7004                 {
7005                   if (!same_type_p (DECL_CONTEXT (field), context))
7006                     pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7007                              DECL_CONTEXT (field), DECL_NAME (decl),
7008                              context, DECL_NAME (decl));
7009                   DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7010                 }
7011               /* Static data member are tricky; an in-class initialization
7012                  still doesn't provide a definition, so the in-class
7013                  declaration will have DECL_EXTERNAL set, but will have an
7014                  initialization.  Thus, duplicate_decls won't warn
7015                  about this situation, and so we check here.  */
7016               if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7017                 error ("duplicate initialization of %D", decl);
7018               if (duplicate_decls (decl, field))
7019                 decl = field;
7020             }
7021         }
7022       else
7023         {
7024           tree field = check_classfn (context, decl);
7025           if (field && duplicate_decls (decl, field))
7026             decl = field;
7027         }
7028
7029       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7030       DECL_IN_AGGR_P (decl) = 0;
7031       if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7032           || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7033         {
7034           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7035           /* [temp.expl.spec] An explicit specialization of a static data
7036              member of a template is a definition if the declaration
7037              includes an initializer; otherwise, it is a declaration.
7038
7039              We check for processing_specialization so this only applies
7040              to the new specialization syntax.  */
7041           if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7042             DECL_EXTERNAL (decl) = 1;
7043         }
7044
7045       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7046         pedwarn ("declaration of `%#D' outside of class is not definition",
7047                     decl);
7048     }
7049
7050   /* Enter this declaration into the symbol table.  */
7051   tem = maybe_push_decl (decl);
7052
7053   if (processing_template_decl)
7054     tem = push_template_decl (tem);
7055
7056 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7057   /* Tell the back-end to use or not use .common as appropriate.  If we say
7058      -fconserve-space, we want this to save .data space, at the expense of
7059      wrong semantics.  If we say -fno-conserve-space, we want this to
7060      produce errors about redefs; to do this we force variables into the
7061      data segment.  */
7062   DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7063                         || !DECL_THREAD_LOCAL (tem))
7064                        && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7065 #endif
7066
7067   if (! processing_template_decl)
7068     start_decl_1 (tem);
7069
7070   return tem;
7071 }
7072
7073 void
7074 start_decl_1 (tree decl)
7075 {
7076   tree type = TREE_TYPE (decl);
7077   int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7078
7079   if (type == error_mark_node)
7080     return;
7081
7082   maybe_push_cleanup_level (type);
7083
7084   if (initialized)
7085     /* Is it valid for this decl to have an initializer at all?
7086        If not, set INITIALIZED to zero, which will indirectly
7087        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7088     {
7089       /* Don't allow initializations for incomplete types except for
7090          arrays which might be completed by the initialization.  */
7091       if (COMPLETE_TYPE_P (complete_type (type)))
7092         ;                       /* A complete type is ok.  */
7093       else if (TREE_CODE (type) != ARRAY_TYPE)
7094         {
7095           error ("variable `%#D' has initializer but incomplete type",
7096                     decl);
7097           initialized = 0;
7098           type = TREE_TYPE (decl) = error_mark_node;
7099         }
7100       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7101         {
7102           if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7103             error ("elements of array `%#D' have incomplete type", decl);
7104           /* else we already gave an error in start_decl.  */
7105           initialized = 0;
7106         }
7107     }
7108
7109   if (!initialized
7110       && TREE_CODE (decl) != TYPE_DECL
7111       && TREE_CODE (decl) != TEMPLATE_DECL
7112       && type != error_mark_node
7113       && IS_AGGR_TYPE (type)
7114       && ! DECL_EXTERNAL (decl))
7115     {
7116       if ((! processing_template_decl || ! uses_template_parms (type))
7117           && !COMPLETE_TYPE_P (complete_type (type)))
7118         {
7119           error ("aggregate `%#D' has incomplete type and cannot be defined",
7120                  decl);
7121           /* Change the type so that assemble_variable will give
7122              DECL an rtl we can live with: (mem (const_int 0)).  */
7123           type = TREE_TYPE (decl) = error_mark_node;
7124         }
7125       else
7126         {
7127           /* If any base type in the hierarchy of TYPE needs a constructor,
7128              then we set initialized to 1.  This way any nodes which are
7129              created for the purposes of initializing this aggregate
7130              will live as long as it does.  This is necessary for global
7131              aggregates which do not have their initializers processed until
7132              the end of the file.  */
7133           initialized = TYPE_NEEDS_CONSTRUCTING (type);
7134         }
7135     }
7136
7137   if (! initialized)
7138     DECL_INITIAL (decl) = NULL_TREE;
7139 }
7140
7141 /* Handle initialization of references.
7142    These three arguments are from `cp_finish_decl', and have the
7143    same meaning here that they do there.
7144
7145    Quotes on semantics can be found in ARM 8.4.3.  */
7146
7147 static tree
7148 grok_reference_init (tree decl, tree type, tree init)
7149 {
7150   tree tmp;
7151
7152   if (init == NULL_TREE)
7153     {
7154       if ((DECL_LANG_SPECIFIC (decl) == 0
7155            || DECL_IN_AGGR_P (decl) == 0)
7156           && ! DECL_THIS_EXTERN (decl))
7157         error ("`%D' declared as reference but not initialized", decl);
7158       return NULL_TREE;
7159     }
7160
7161   if (TREE_CODE (init) == CONSTRUCTOR)
7162     {
7163       error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7164       return NULL_TREE;
7165     }
7166
7167   if (TREE_CODE (init) == TREE_LIST)
7168     init = build_compound_expr (init);
7169
7170   if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7171     init = convert_from_reference (init);
7172
7173   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7174       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7175     {
7176       /* Note: default conversion is only called in very special cases.  */
7177       init = default_conversion (init);
7178     }
7179
7180   /* Convert INIT to the reference type TYPE.  This may involve the
7181      creation of a temporary, whose lifetime must be the same as that
7182      of the reference.  If so, a DECL_STMT for the temporary will be
7183      added just after the DECL_STMT for DECL.  That's why we don't set
7184      DECL_INITIAL for local references (instead assigning to them
7185      explicitly); we need to allow the temporary to be initialized
7186      first.  */
7187   tmp = initialize_reference (type, init, decl);
7188
7189   if (tmp == error_mark_node)
7190     return NULL_TREE;
7191   else if (tmp == NULL_TREE)
7192     {
7193       error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7194       return NULL_TREE;
7195     }
7196
7197   if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7198     return tmp;
7199
7200   DECL_INITIAL (decl) = tmp;
7201
7202   return NULL_TREE;
7203 }
7204
7205 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7206    array until we finish parsing the initializer.  If that's the
7207    situation we're in, update DECL accordingly.  */
7208
7209 static void
7210 maybe_deduce_size_from_array_init (tree decl, tree init)
7211 {
7212   tree type = TREE_TYPE (decl);
7213
7214   if (TREE_CODE (type) == ARRAY_TYPE
7215       && TYPE_DOMAIN (type) == NULL_TREE
7216       && TREE_CODE (decl) != TYPE_DECL)
7217     {
7218       /* do_default is really a C-ism to deal with tentative definitions.
7219          But let's leave it here to ease the eventual merge.  */
7220       int do_default = !DECL_EXTERNAL (decl);
7221       tree initializer = init ? init : DECL_INITIAL (decl);
7222       int failure = complete_array_type (type, initializer, do_default);
7223
7224       if (failure == 1)
7225         error ("initializer fails to determine size of `%D'", decl);
7226
7227       if (failure == 2)
7228         {
7229           if (do_default)
7230             error ("array size missing in `%D'", decl);
7231           /* If a `static' var's size isn't known, make it extern as
7232              well as static, so it does not get allocated.  If it's not
7233              `static', then don't mark it extern; finish_incomplete_decl
7234              will give it a default size and it will get allocated.  */
7235           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7236             DECL_EXTERNAL (decl) = 1;
7237         }
7238
7239       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7240           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7241                               integer_zero_node))
7242         error ("zero-size array `%D'", decl);
7243
7244       layout_decl (decl, 0);
7245     }
7246 }
7247
7248 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7249    any appropriate error messages regarding the layout.  */
7250
7251 static void
7252 layout_var_decl (tree decl)
7253 {
7254   tree type = TREE_TYPE (decl);
7255 #if 0
7256   tree ttype = target_type (type);
7257 #endif
7258
7259   /* If we haven't already layed out this declaration, do so now.
7260      Note that we must not call complete type for an external object
7261      because it's type might involve templates that we are not
7262      supposed to isntantiate yet.  (And it's perfectly valid to say
7263      `extern X x' for some incomplete type `X'.)  */
7264   if (!DECL_EXTERNAL (decl))
7265     complete_type (type);
7266   if (!DECL_SIZE (decl) 
7267       && TREE_TYPE (decl) != error_mark_node
7268       && (COMPLETE_TYPE_P (type)
7269           || (TREE_CODE (type) == ARRAY_TYPE 
7270               && !TYPE_DOMAIN (type)
7271               && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7272     layout_decl (decl, 0);
7273
7274   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7275     {
7276       /* An automatic variable with an incomplete type: that is an error.
7277          Don't talk about array types here, since we took care of that
7278          message in grokdeclarator.  */
7279       error ("storage size of `%D' isn't known", decl);
7280       TREE_TYPE (decl) = error_mark_node;
7281     }
7282 #if 0
7283   /* Keep this code around in case we later want to control debug info
7284      based on whether a type is "used".  (jason 1999-11-11) */
7285
7286   else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7287     /* Let debugger know it should output info for this type.  */
7288     note_debug_info_needed (ttype);
7289
7290   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7291     note_debug_info_needed (DECL_CONTEXT (decl));
7292 #endif
7293
7294   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7295       && DECL_SIZE (decl) != NULL_TREE
7296       && ! TREE_CONSTANT (DECL_SIZE (decl)))
7297     {
7298       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7299         constant_expression_warning (DECL_SIZE (decl));
7300       else
7301         error ("storage size of `%D' isn't constant", decl);
7302     }
7303
7304   if (TREE_STATIC (decl)
7305       && !DECL_ARTIFICIAL (decl)
7306       && current_function_decl
7307       && DECL_CONTEXT (decl) == current_function_decl)
7308     push_local_name (decl);
7309 }
7310
7311 /* If a local static variable is declared in an inline function, or if
7312    we have a weak definition, we must endeavor to create only one
7313    instance of the variable at link-time.  */
7314
7315 static void
7316 maybe_commonize_var (tree decl)
7317 {
7318   /* Static data in a function with comdat linkage also has comdat
7319      linkage.  */
7320   if (TREE_STATIC (decl)
7321       /* Don't mess with __FUNCTION__.  */
7322       && ! DECL_ARTIFICIAL (decl)
7323       && DECL_FUNCTION_SCOPE_P (decl)
7324       /* Unfortunately, import_export_decl has not always been called
7325          before the function is processed, so we cannot simply check
7326          DECL_COMDAT.  */ 
7327       && (DECL_COMDAT (DECL_CONTEXT (decl))
7328           || ((DECL_DECLARED_INLINE_P (DECL_CONTEXT (decl))
7329                || DECL_TEMPLATE_INSTANTIATION (DECL_CONTEXT (decl)))
7330               && TREE_PUBLIC (DECL_CONTEXT (decl)))))
7331     {
7332       if (flag_weak)
7333         {
7334           /* With weak symbols, we simply make the variable COMDAT;
7335              that will cause copies in multiple translations units to
7336              be merged.  */
7337           comdat_linkage (decl);
7338         }
7339       else
7340         {
7341           if (DECL_INITIAL (decl) == NULL_TREE
7342               || DECL_INITIAL (decl) == error_mark_node)
7343             {
7344               /* Without weak symbols, we can use COMMON to merge
7345                  uninitialized variables.  */
7346               TREE_PUBLIC (decl) = 1;
7347               DECL_COMMON (decl) = 1;
7348             }
7349           else
7350             {
7351               /* While for initialized variables, we must use internal
7352                  linkage -- which means that multiple copies will not
7353                  be merged.  */
7354               TREE_PUBLIC (decl) = 0;
7355               DECL_COMMON (decl) = 0;
7356               cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7357               cp_warning_at ("  you can work around this by removing the initializer", decl);
7358             }
7359         }
7360     }
7361   else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7362     /* Set it up again; we might have set DECL_INITIAL since the last
7363        time.  */
7364     comdat_linkage (decl);
7365 }
7366
7367 /* Issue an error message if DECL is an uninitialized const variable.  */
7368
7369 static void
7370 check_for_uninitialized_const_var (tree decl)
7371 {
7372   tree type = TREE_TYPE (decl);
7373
7374   /* ``Unless explicitly declared extern, a const object does not have
7375      external linkage and must be initialized. ($8.4; $12.1)'' ARM
7376      7.1.6 */
7377   if (TREE_CODE (decl) == VAR_DECL
7378       && TREE_CODE (type) != REFERENCE_TYPE
7379       && CP_TYPE_CONST_P (type)
7380       && !TYPE_NEEDS_CONSTRUCTING (type)
7381       && !DECL_INITIAL (decl))
7382     error ("uninitialized const `%D'", decl);
7383 }
7384
7385 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
7386    returned is the next FIELD_DECL (possibly FIELD itself) that can be
7387    initialized.  If there are no more such fields, the return value
7388    will be NULL.  */
7389
7390 static tree
7391 next_initializable_field (tree field)
7392 {
7393   while (field
7394          && (TREE_CODE (field) != FIELD_DECL
7395              || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7396              || DECL_ARTIFICIAL (field)))
7397     field = TREE_CHAIN (field);
7398
7399   return field;
7400 }
7401
7402 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7403    brace-enclosed aggregate initializer.
7404
7405    *INITP is one of a list of initializers describing a brace-enclosed
7406    initializer for an entity of the indicated aggregate TYPE.  It may
7407    not presently match the shape of the TYPE; for example:
7408    
7409      struct S { int a; int b; };
7410      struct S a[] = { 1, 2, 3, 4 };
7411
7412    Here *INITP will point to TREE_LIST of four elements, rather than a
7413    list of two elements, each itself a list of two elements.  This
7414    routine transforms INIT from the former form into the latter.  The
7415    revised initializer is returned.  */
7416
7417 static tree
7418 reshape_init (tree type, tree *initp)
7419 {
7420   tree inits;
7421   tree old_init;
7422   tree old_init_value;
7423   tree new_init;
7424   bool brace_enclosed_p;
7425
7426   old_init = *initp;
7427   old_init_value = (TREE_CODE (*initp) == TREE_LIST
7428                     ? TREE_VALUE (*initp) : old_init);
7429
7430   /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
7431   if (!old_init_value)
7432     {
7433       my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7434       TREE_VALUE (old_init) = error_mark_node;
7435       return old_init;
7436     }
7437
7438   /* If the initializer is brace-enclosed, pull initializers from the
7439      enclosed elements.  Advance past the brace-enclosed initializer
7440      now.  */
7441   if (TREE_CODE (old_init_value) == CONSTRUCTOR
7442       && TREE_TYPE (old_init_value) == NULL_TREE
7443       && TREE_HAS_CONSTRUCTOR (old_init_value))
7444     {
7445       *initp = TREE_CHAIN (old_init);
7446       TREE_CHAIN (old_init) = NULL_TREE;
7447       inits = CONSTRUCTOR_ELTS (old_init_value);
7448       initp = &inits;
7449       brace_enclosed_p = true;
7450     }
7451   else
7452     {
7453       inits = NULL_TREE;
7454       brace_enclosed_p = false;
7455     }
7456
7457   /* A non-aggregate type is always initialized with a single
7458      initializer.  */
7459   if (!CP_AGGREGATE_TYPE_P (type))
7460       {
7461         *initp = TREE_CHAIN (old_init);
7462         TREE_CHAIN (old_init) = NULL_TREE;
7463         /* It is invalid to initialize a non-aggregate type with a
7464            brace-enclosed initializer.  */
7465         if (brace_enclosed_p)
7466           {
7467             error ("brace-enclosed initializer used to initialize `%T'",
7468                    type);
7469             if (TREE_CODE (old_init) == TREE_LIST)
7470               TREE_VALUE (old_init) = error_mark_node;
7471             else
7472               old_init = error_mark_node;
7473           }
7474         
7475         return old_init;
7476       }
7477
7478   /* [dcl.init.aggr]
7479
7480      All implicit type conversions (clause _conv_) are considered when
7481      initializing the aggregate member with an initializer from an
7482      initializer-list.  If the initializer can initialize a member,
7483      the member is initialized.  Otherwise, if the member is itself a
7484      non-empty subaggregate, brace elision is assumed and the
7485      initializer is considered for the initialization of the first
7486      member of the subaggregate.  */
7487   if (!brace_enclosed_p
7488       && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7489     {
7490       *initp = TREE_CHAIN (old_init);
7491       TREE_CHAIN (old_init) = NULL_TREE;
7492       return old_init;
7493     }
7494
7495   if (TREE_CODE (old_init_value) == STRING_CST
7496       && TREE_CODE (type) == ARRAY_TYPE
7497       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7498     {
7499       /* [dcl.init.string]
7500
7501          A char array (whether plain char, signed char, or unsigned char)
7502          can be initialized by a string-literal (optionally enclosed in
7503          braces); a wchar_t array can be initialized by a wide
7504          string-literal (optionally enclosed in braces).  */
7505       new_init = old_init;
7506       /* Move past the initializer.  */
7507       *initp = TREE_CHAIN (old_init);
7508       TREE_CHAIN (old_init) = NULL_TREE;
7509     }
7510   else
7511     {
7512       /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */  
7513       new_init = build_constructor (type, NULL_TREE);
7514       TREE_HAS_CONSTRUCTOR (new_init) = 1;
7515
7516       if (CLASS_TYPE_P (type))
7517         {
7518           tree field;
7519
7520           field = next_initializable_field (TYPE_FIELDS (type));
7521
7522           if (!field)
7523             {
7524               /* [dcl.init.aggr]
7525               
7526                  An initializer for an aggregate member that is an
7527                  empty class shall have the form of an empty
7528                  initializer-list {}.  */
7529               if (!brace_enclosed_p)
7530                 error ("initializer for `%T' must be brace-enclosed",
7531                        type);
7532             }
7533           else
7534             {
7535               /* Loop through the initializable fields, gathering
7536                  initializers.  */
7537               while (*initp)
7538                 {
7539                   tree field_init;
7540
7541                   /* Handle designated initializers, as an extension.  */
7542                   if (TREE_PURPOSE (*initp))
7543                     {
7544                       if (pedantic)
7545                         pedwarn ("ISO C++ does not allow designated initializers");
7546                       field = lookup_field_1 (type, TREE_PURPOSE (*initp),
7547                                               /*want_type=*/false);
7548                       if (!field || TREE_CODE (field) != FIELD_DECL)
7549                         error ("`%T' has no non-static data member named `%D'",
7550                                type, TREE_PURPOSE (*initp));
7551                     }
7552                   if (!field)
7553                     break;
7554
7555                   field_init = reshape_init (TREE_TYPE (field), initp);
7556                   TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7557                   CONSTRUCTOR_ELTS (new_init) = field_init;
7558                   /* [dcl.init.aggr] 
7559
7560                      When a union  is  initialized with a brace-enclosed
7561                      initializer, the braces shall only contain an
7562                      initializer for the first member of the union.  */
7563                   if (TREE_CODE (type) == UNION_TYPE)
7564                     break;
7565                   field = next_initializable_field (TREE_CHAIN (field));
7566                 }
7567             }
7568         }
7569       else if (TREE_CODE (type) == ARRAY_TYPE)
7570         {
7571           tree index;
7572           tree max_index;
7573
7574           /* If the bound of the array is known, take no more initializers
7575              than are allowed.  */
7576           max_index = (TYPE_DOMAIN (type) 
7577                        ? array_type_nelts (type) : NULL_TREE);
7578           /* Loop through the array elements, gathering initializers.  */
7579           for (index = size_zero_node;
7580                *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7581                index = size_binop (PLUS_EXPR, index, size_one_node))
7582             {
7583               tree element_init;
7584
7585               element_init = reshape_init (TREE_TYPE (type), initp);
7586               TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7587               CONSTRUCTOR_ELTS (new_init) = element_init;
7588               if (TREE_PURPOSE (element_init))
7589                 index = TREE_PURPOSE (element_init);
7590             }
7591         }
7592       else
7593         abort ();
7594
7595       /* The initializers were placed in reverse order in the
7596          CONSTRUCTOR.  */
7597       CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7598
7599       if (TREE_CODE (old_init) == TREE_LIST)
7600         new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7601     }
7602
7603   /* If this was a brace-enclosed initializer and all of the
7604      initializers were not used up, there is a problem.  */
7605   if (brace_enclosed_p && *initp)
7606     error ("too many initializers for `%T'", type);
7607
7608   return new_init;
7609 }
7610
7611 /* Verify INIT (the initializer for DECL), and record the
7612    initialization in DECL_INITIAL, if appropriate.  
7613
7614    If the return value is non-NULL, it is an expression that must be
7615    evaluated dynamically to initialize DECL.  */
7616
7617 static tree
7618 check_initializer (tree decl, tree init, int flags)
7619 {
7620   tree type = TREE_TYPE (decl);
7621
7622   /* If `start_decl' didn't like having an initialization, ignore it now.  */
7623   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7624     init = NULL_TREE;
7625
7626   /* If an initializer is present, DECL_INITIAL has been
7627      error_mark_node, to indicate that an as-of-yet unevaluated
7628      initialization will occur.  From now on, DECL_INITIAL reflects
7629      the static initialization -- if any -- of DECL.  */
7630   DECL_INITIAL (decl) = NULL_TREE;
7631
7632   /* Things that are going to be initialized need to have complete
7633      type.  */
7634   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7635
7636   if (type == error_mark_node)
7637     /* We will have already complained.  */
7638     init = NULL_TREE;
7639   else if (init && COMPLETE_TYPE_P (type) 
7640            && !TREE_CONSTANT (TYPE_SIZE (type)))
7641     {
7642       error ("variable-sized object `%D' may not be initialized", decl);
7643       init = NULL_TREE;
7644     }
7645   else if (TREE_CODE (type) == ARRAY_TYPE
7646            && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7647     {
7648       error ("elements of array `%#D' have incomplete type", decl);
7649       init = NULL_TREE;
7650     }
7651   else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7652     {
7653       error ("`%D' has incomplete type", decl);
7654       TREE_TYPE (decl) = error_mark_node;
7655       init = NULL_TREE;
7656     }
7657
7658   if (TREE_CODE (decl) == CONST_DECL)
7659     {
7660       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7661
7662       DECL_INITIAL (decl) = init;
7663
7664       my_friendly_assert (init != NULL_TREE, 149);
7665       init = NULL_TREE;
7666     }
7667   else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7668     init = grok_reference_init (decl, type, init);
7669   else if (init)
7670     {
7671       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7672         {
7673           /* [dcl.init] paragraph 13,
7674              If T is a scalar type, then a declaration of the form
7675              T x = { a };
7676              is equivalent to
7677              T x = a;
7678              
7679              reshape_init will complain about the extra braces,
7680              and doesn't do anything useful in the case where TYPE is
7681              scalar, so just don't call it.  */
7682           if (CP_AGGREGATE_TYPE_P (type))
7683             init = reshape_init (type, &init);
7684
7685           if ((*targetm.vector_opaque_p) (type))
7686             {
7687               error ("opaque vector types cannot be initialized");
7688               init = error_mark_node;
7689             }
7690         }
7691
7692       /* If DECL has an array type without a specific bound, deduce the
7693          array size from the initializer.  */
7694       maybe_deduce_size_from_array_init (decl, init);
7695       type = TREE_TYPE (decl);
7696       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7697         TREE_TYPE (init) = type;
7698
7699       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7700         {
7701           if (TREE_CODE (type) == ARRAY_TYPE)
7702             goto initialize_aggr;
7703           else if (TREE_CODE (init) == CONSTRUCTOR
7704                    && TREE_HAS_CONSTRUCTOR (init))
7705             {
7706               if (TYPE_NON_AGGREGATE_CLASS (type))
7707                 {
7708                   error ("`%D' must be initialized by constructor, not by `{...}'",
7709                          decl);
7710                   init = error_mark_node;
7711                 }
7712               else
7713                 goto dont_use_constructor;
7714             }
7715           else
7716             {
7717               int saved_stmts_are_full_exprs_p;
7718
7719             initialize_aggr:
7720               saved_stmts_are_full_exprs_p = 0;
7721               if (building_stmt_tree ())
7722                 {
7723                   saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7724                   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7725                 }
7726               init = build_aggr_init (decl, init, flags);
7727               if (building_stmt_tree ())
7728                 current_stmt_tree ()->stmts_are_full_exprs_p =
7729                   saved_stmts_are_full_exprs_p;
7730               return init;
7731             }
7732         }
7733       else
7734         {
7735         dont_use_constructor:
7736           if (TREE_CODE (init) != TREE_VEC)
7737             init = store_init_value (decl, init);
7738         }
7739     }
7740   else if (DECL_EXTERNAL (decl))
7741     ;
7742   else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7743     goto initialize_aggr;
7744   else if (IS_AGGR_TYPE (type))
7745     {
7746       tree core_type = strip_array_types (type);
7747
7748       if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7749         error ("structure `%D' with uninitialized const members", decl);
7750       if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7751         error ("structure `%D' with uninitialized reference members",
7752                decl);
7753
7754       check_for_uninitialized_const_var (decl);
7755     }
7756   else
7757     check_for_uninitialized_const_var (decl);
7758
7759   if (init && init != error_mark_node)
7760     init = build (INIT_EXPR, type, decl, init);
7761
7762   return init;
7763 }
7764
7765 /* If DECL is not a local variable, give it RTL.  */
7766
7767 static void
7768 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7769 {
7770   int toplev = toplevel_bindings_p ();
7771   int defer_p;
7772
7773   /* Handle non-variables up front.  */
7774   if (TREE_CODE (decl) != VAR_DECL)
7775     {
7776       rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7777       return;
7778     }
7779
7780   /* If we see a class member here, it should be a static data
7781      member.  */
7782   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7783     {
7784       my_friendly_assert (TREE_STATIC (decl), 19990828);
7785       /* An in-class declaration of a static data member should be
7786          external; it is only a declaration, and not a definition.  */
7787       if (init == NULL_TREE)
7788         my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7789     }
7790
7791   /* Set the DECL_ASSEMBLER_NAME for the variable.  */
7792   if (asmspec)
7793     {
7794       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7795       /* The `register' keyword, when used together with an
7796          asm-specification, indicates that the variable should be
7797          placed in a particular register.  */
7798       if (DECL_REGISTER (decl))
7799         DECL_C_HARD_REGISTER (decl) = 1;
7800     }
7801
7802   /* We don't create any RTL for local variables.  */
7803   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7804     return;
7805
7806   /* We defer emission of local statics until the corresponding
7807      DECL_STMT is expanded.  */
7808   defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7809
7810   /* We try to defer namespace-scope static constants so that they are
7811      not emitted into the object file unnecessarily.  */
7812   if (!DECL_VIRTUAL_P (decl)
7813       && TREE_READONLY (decl)
7814       && DECL_INITIAL (decl) != NULL_TREE
7815       && DECL_INITIAL (decl) != error_mark_node
7816       && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7817       && toplev
7818       && !TREE_PUBLIC (decl))
7819     {
7820       /* Fool with the linkage of static consts according to #pragma
7821          interface.  */
7822       if (!interface_unknown && !TREE_PUBLIC (decl))
7823         {
7824           TREE_PUBLIC (decl) = 1;
7825           DECL_EXTERNAL (decl) = interface_only;
7826         }
7827
7828       defer_p = 1;
7829     }
7830   /* Likewise for template instantiations.  */
7831   else if (DECL_COMDAT (decl))
7832     defer_p = 1;
7833
7834   /* If we're deferring the variable, we only need to make RTL if
7835      there's an ASMSPEC.  Otherwise, we'll lazily create it later when
7836      we need it.  (There's no way to lazily create RTL for things that
7837      have assembly specs because the information about the specifier
7838      isn't stored in the tree, yet)  */
7839   if (defer_p && asmspec)
7840     make_decl_rtl (decl, asmspec);
7841   /* If we're not deferring, go ahead and assemble the variable.  */
7842   else if (!defer_p)
7843     rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7844 }
7845
7846 /* The old ARM scoping rules injected variables declared in the
7847    initialization statement of a for-statement into the surrounding
7848    scope.  We support this usage, in order to be backward-compatible.
7849    DECL is a just-declared VAR_DECL; if necessary inject its
7850    declaration into the surrounding scope.  */
7851
7852 void
7853 maybe_inject_for_scope_var (tree decl)
7854 {
7855   timevar_push (TV_NAME_LOOKUP);
7856   if (!DECL_NAME (decl))
7857     {
7858       timevar_pop (TV_NAME_LOOKUP);
7859       return;
7860     }
7861   
7862   /* Declarations of __FUNCTION__ and its ilk appear magically when
7863      the variable is first used.  If that happens to be inside a
7864      for-loop, we don't want to do anything special.  */
7865   if (DECL_PRETTY_FUNCTION_P (decl))
7866     {
7867       timevar_pop (TV_NAME_LOOKUP);
7868       return;
7869     }
7870
7871   if (current_binding_level->is_for_scope)
7872     {
7873       struct cp_binding_level *outer
7874         = current_binding_level->level_chain;
7875
7876       /* Check to see if the same name is already bound at the outer
7877          level, either because it was directly declared, or because a
7878          dead for-decl got preserved.  In either case, the code would
7879          not have been valid under the ARM scope rules, so clear
7880          is_for_scope for the current_binding_level.
7881
7882          Otherwise, we need to preserve the temp slot for decl to last
7883          into the outer binding level.  */
7884
7885       cxx_binding *outer_binding
7886         = IDENTIFIER_BINDING (DECL_NAME (decl))->previous;
7887
7888       if (outer_binding && BINDING_SCOPE (outer_binding) == outer
7889           && (TREE_CODE (BINDING_VALUE (outer_binding)) == VAR_DECL)
7890           && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7891         {
7892           BINDING_VALUE (outer_binding)
7893             = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7894           current_binding_level->is_for_scope = 0;
7895         }
7896     }
7897   timevar_pop (TV_NAME_LOOKUP);
7898 }
7899
7900 /* Generate code to initialize DECL (a local variable).  */
7901
7902 static void
7903 initialize_local_var (tree decl, tree init)
7904 {
7905   tree type = TREE_TYPE (decl);
7906
7907   my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7908                       || TREE_CODE (decl) == RESULT_DECL, 
7909                       20021010);
7910   my_friendly_assert (!TREE_STATIC (decl), 20021010);
7911
7912   if (DECL_SIZE (decl) == NULL_TREE)
7913     {
7914       /* If we used it already as memory, it must stay in memory.  */
7915       DECL_INITIAL (decl) = NULL_TREE;
7916       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
7917     }
7918
7919   if (DECL_SIZE (decl) && type != error_mark_node)
7920     {
7921       int already_used;
7922
7923       /* Compute and store the initial value.  */
7924       already_used = TREE_USED (decl) || TREE_USED (type);
7925
7926       /* Perform the initialization.  */
7927       if (init)
7928         {
7929           int saved_stmts_are_full_exprs_p;
7930
7931           my_friendly_assert (building_stmt_tree (), 20000906);
7932           saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7933           current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7934           finish_expr_stmt (init);
7935           current_stmt_tree ()->stmts_are_full_exprs_p =
7936             saved_stmts_are_full_exprs_p;
7937         }
7938
7939       /* Set this to 0 so we can tell whether an aggregate which was
7940          initialized was ever used.  Don't do this if it has a
7941          destructor, so we don't complain about the 'resource
7942          allocation is initialization' idiom.  Now set
7943          attribute((unused)) on types so decls of that type will be
7944          marked used. (see TREE_USED, above.)  */
7945       if (TYPE_NEEDS_CONSTRUCTING (type)
7946           && ! already_used
7947           && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
7948           && DECL_NAME (decl))
7949         TREE_USED (decl) = 0;
7950       else if (already_used)
7951         TREE_USED (decl) = 1;
7952     }
7953
7954   /* Generate a cleanup, if necessary.  */
7955   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
7956     {
7957       tree cleanup;
7958
7959       /* Compute the cleanup.  */
7960       cleanup = cxx_maybe_build_cleanup (decl);
7961       
7962       /* Record the cleanup required for this declaration.  */
7963       if (DECL_SIZE (decl) && cleanup)
7964         finish_decl_cleanup (decl, cleanup);
7965     }
7966 }
7967
7968 /* Finish processing of a declaration;
7969    install its line number and initial value.
7970    If the length of an array type is not known before,
7971    it must be determined now, from the initial value, or it is an error.
7972
7973    INIT holds the value of an initializer that should be allowed to escape
7974    the normal rules.
7975
7976    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
7977    if the (init) syntax was used.  */
7978
7979 void
7980 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
7981 {
7982   register tree type;
7983   tree ttype = NULL_TREE;
7984   const char *asmspec = NULL;
7985   int was_readonly = 0;
7986
7987   if (! decl)
7988     {
7989       if (init)
7990         error ("assignment (not initialization) in declaration");
7991       return;
7992     }
7993
7994   /* If a name was specified, get the string.  */
7995   if (global_scope_p (current_binding_level))
7996     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
7997   if (asmspec_tree)
7998     asmspec = TREE_STRING_POINTER (asmspec_tree);
7999
8000   if (init && TREE_CODE (init) == NAMESPACE_DECL)
8001     {
8002       error ("cannot initialize `%D' to namespace `%D'",
8003                 decl, init);
8004       init = NULL_TREE;
8005     }
8006
8007   if (current_class_type
8008       && CP_DECL_CONTEXT (decl) == current_class_type
8009       && TYPE_BEING_DEFINED (current_class_type)
8010       && (DECL_INITIAL (decl) || init))
8011     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8012
8013   if (TREE_CODE (decl) == VAR_DECL
8014       && DECL_CONTEXT (decl)
8015       && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8016       && DECL_CONTEXT (decl) != current_namespace
8017       && init)
8018     {
8019       /* Leave the namespace of the object.  */
8020       pop_decl_namespace ();
8021     }
8022
8023   type = TREE_TYPE (decl);
8024
8025   if (type == error_mark_node)
8026     goto finish_end0;
8027
8028   if (TYPE_HAS_MUTABLE_P (type))
8029     TREE_READONLY (decl) = 0;
8030
8031   if (processing_template_decl)
8032     {
8033       /* Add this declaration to the statement-tree.  */
8034       if (at_function_scope_p ()
8035           && TREE_CODE (decl) != RESULT_DECL)
8036         add_decl_stmt (decl);
8037
8038       if (init && DECL_INITIAL (decl))
8039         DECL_INITIAL (decl) = init;
8040       goto finish_end0;
8041     }
8042
8043   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8044   my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8045
8046   /* Take care of TYPE_DECLs up front.  */
8047   if (TREE_CODE (decl) == TYPE_DECL)
8048     {
8049       if (type != error_mark_node
8050           && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8051         {
8052           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8053             warning ("shadowing previous type declaration of `%#D'", decl);
8054           set_identifier_type_value (DECL_NAME (decl), type);
8055           CLASSTYPE_GOT_SEMICOLON (type) = 1;
8056         }
8057
8058       /* If we have installed this as the canonical typedef for this
8059          type, and that type has not been defined yet, delay emitting
8060          the debug information for it, as we will emit it later.  */
8061       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8062           && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8063         TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8064
8065       rest_of_decl_compilation (decl, NULL,
8066                                 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8067       goto finish_end;
8068     }
8069
8070   if (TREE_CODE (decl) != FUNCTION_DECL)
8071     ttype = target_type (type);
8072
8073   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8074       && TYPE_NEEDS_CONSTRUCTING (type))
8075     {
8076       /* Currently, GNU C++ puts constants in text space, making them
8077          impossible to initialize.  In the future, one would hope for
8078          an operating system which understood the difference between
8079          initialization and the running of a program.  */
8080       was_readonly = 1;
8081       TREE_READONLY (decl) = 0;
8082     }
8083
8084   if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8085     {
8086       /* This must override the asm specifier which was placed by
8087          grokclassfn.  Lay this out fresh.  */
8088       SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8089       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8090       make_decl_rtl (decl, asmspec);
8091     }
8092   else if (TREE_CODE (decl) == RESULT_DECL)
8093     init = check_initializer (decl, init, flags);
8094   else if (TREE_CODE (decl) == VAR_DECL)
8095     {
8096       /* Only PODs can have thread-local storage.  Other types may require
8097          various kinds of non-trivial initialization.  */
8098       if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8099         error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8100                decl, TREE_TYPE (decl));
8101       /* Convert the initializer to the type of DECL, if we have not
8102          already initialized DECL.  */
8103       if (!DECL_INITIALIZED_P (decl)
8104           /* If !DECL_EXTERNAL then DECL is being defined.  In the
8105              case of a static data member initialized inside the
8106              class-specifier, there can be an initializer even if DECL
8107              is *not* defined.  */
8108           && (!DECL_EXTERNAL (decl) || init))
8109         {
8110           init = check_initializer (decl, init, flags);
8111           /* Thread-local storage cannot be dynamically initialized.  */
8112           if (DECL_THREAD_LOCAL (decl) && init)
8113             {
8114               error ("`%D' is thread-local and so cannot be dynamically "
8115                      "initialized", decl);
8116               init = NULL_TREE;
8117             }
8118           /* Handle:
8119              
8120              [dcl.init]
8121              
8122              The memory occupied by any object of static storage
8123              duration is zero-initialized at program startup before
8124              any other initialization takes place.
8125              
8126              We cannot create an appropriate initializer until after
8127              the type of DECL is finalized.  If DECL_INITIAL is set,
8128              then the DECL is statically initialized, and any
8129              necessary zero-initialization has already been performed.  */
8130           if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8131             DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8132                                                    /*nelts=*/NULL_TREE,
8133                                                    /*static_storage_p=*/true);
8134           /* Remember that the initialization for this variable has
8135              taken place.  */
8136           DECL_INITIALIZED_P (decl) = 1;
8137         }
8138       /* If the variable has an array type, lay out the type, even if
8139          there is no initializer.  It is valid to index through the
8140          array, and we must get TYPE_ALIGN set correctly on the array
8141          type.  */
8142       else if (TREE_CODE (type) == ARRAY_TYPE)
8143         layout_type (type);
8144     }
8145
8146   /* Add this declaration to the statement-tree.  This needs to happen
8147      after the call to check_initializer so that the DECL_STMT for a
8148      reference temp is added before the DECL_STMT for the reference itself.  */
8149   if (building_stmt_tree ()
8150       && at_function_scope_p ()
8151       && TREE_CODE (decl) != RESULT_DECL)
8152     add_decl_stmt (decl);
8153
8154   if (TREE_CODE (decl) == VAR_DECL)
8155     layout_var_decl (decl);
8156
8157   /* Output the assembler code and/or RTL code for variables and functions,
8158      unless the type is an undefined structure or union.
8159      If not, it will get done when the type is completed.  */
8160   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8161       || TREE_CODE (decl) == RESULT_DECL)
8162     {
8163       if (TREE_CODE (decl) == VAR_DECL)
8164         maybe_commonize_var (decl);
8165
8166       make_rtl_for_nonlocal_decl (decl, init, asmspec);
8167
8168       if (TREE_CODE (type) == FUNCTION_TYPE
8169           || TREE_CODE (type) == METHOD_TYPE)
8170         abstract_virtuals_error (decl,
8171                                  strip_array_types (TREE_TYPE (type)));
8172       else
8173         abstract_virtuals_error (decl, strip_array_types (type));
8174
8175       if (TREE_CODE (decl) == FUNCTION_DECL 
8176           || TREE_TYPE (decl) == error_mark_node)
8177         /* No initialization required.  */
8178         ;
8179       else if (DECL_EXTERNAL (decl)
8180                && ! (DECL_LANG_SPECIFIC (decl)
8181                      && DECL_NOT_REALLY_EXTERN (decl)))
8182         {
8183           if (init)
8184             DECL_INITIAL (decl) = init;
8185         }
8186       else
8187         {
8188           /* A variable definition.  */
8189           if (DECL_FUNCTION_SCOPE_P (decl))
8190             {
8191               /* This is a local declaration.  */
8192               if (doing_semantic_analysis_p ())
8193                 maybe_inject_for_scope_var (decl);
8194               /* Initialize the local variable.  */
8195               if (processing_template_decl)
8196                 {
8197                   if (init || DECL_INITIAL (decl) == error_mark_node)
8198                     DECL_INITIAL (decl) = init;
8199                 }
8200               else if (!TREE_STATIC (decl))
8201                 initialize_local_var (decl, init);
8202             }
8203
8204           if (TREE_STATIC (decl))
8205             expand_static_init (decl, init);
8206         }
8207     finish_end0:
8208
8209       /* Undo call to `pushclass' that was done in `start_decl'
8210          due to initialization of qualified member variable.
8211          I.e., Foo::x = 10;  */
8212       {
8213         tree context = CP_DECL_CONTEXT (decl);
8214         if (context
8215             && TYPE_P (context)
8216             && (TREE_CODE (decl) == VAR_DECL
8217                 /* We also have a pushclass done that we need to undo here
8218                    if we're at top level and declare a method.  */
8219                 || TREE_CODE (decl) == FUNCTION_DECL)
8220             /* If size hasn't been set, we're still defining it,
8221                and therefore inside the class body; don't pop
8222                the binding level..  */
8223             && COMPLETE_TYPE_P (context)
8224             && context == current_class_type)
8225           pop_nested_class ();
8226       }
8227     }
8228
8229  finish_end:
8230
8231   if (was_readonly)
8232     TREE_READONLY (decl) = 1;
8233 }
8234
8235 /* This is here for a midend callback from c-common.c */
8236
8237 void
8238 finish_decl (tree decl, tree init, tree asmspec_tree)
8239 {
8240   cp_finish_decl (decl, init, asmspec_tree, 0);
8241 }
8242
8243 /* Returns a declaration for a VAR_DECL as if:
8244
8245      extern "C" TYPE NAME;
8246
8247    had been seen.  Used to create compiler-generated global
8248    variables.  */
8249
8250 tree
8251 declare_global_var (tree name, tree type)
8252 {
8253   tree decl;
8254
8255   push_to_top_level ();
8256   decl = build_decl (VAR_DECL, name, type);
8257   TREE_PUBLIC (decl) = 1;
8258   DECL_EXTERNAL (decl) = 1;
8259   DECL_ARTIFICIAL (decl) = 1;
8260   pushdecl (decl);
8261   cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8262   pop_from_top_level ();
8263
8264   return decl;
8265 }
8266
8267 /* Returns a pointer to the `atexit' function.  Note that if
8268    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8269    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
8270
8271 static tree
8272 get_atexit_node (void)
8273 {
8274   tree atexit_fndecl;
8275   tree arg_types;
8276   tree fn_type;
8277   tree fn_ptr_type;
8278   const char *name;
8279
8280   if (atexit_node)
8281     return atexit_node;
8282
8283   if (flag_use_cxa_atexit)
8284     {
8285       /* The declaration for `__cxa_atexit' is:
8286
8287            int __cxa_atexit (void (*)(void *), void *, void *)
8288
8289          We build up the argument types and then then function type
8290          itself.  */
8291
8292       /* First, build the pointer-to-function type for the first
8293          argument.  */
8294       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8295       fn_type = build_function_type (void_type_node, arg_types);
8296       fn_ptr_type = build_pointer_type (fn_type);
8297       /* Then, build the rest of the argument types.  */
8298       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8299       arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8300       arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8301       /* And the final __cxa_atexit type.  */
8302       fn_type = build_function_type (integer_type_node, arg_types);
8303       fn_ptr_type = build_pointer_type (fn_type);
8304       name = "__cxa_atexit";
8305     }
8306   else
8307     {
8308       /* The declaration for `atexit' is:
8309
8310            int atexit (void (*)());
8311
8312          We build up the argument types and then then function type
8313          itself.  */
8314       fn_type = build_function_type (void_type_node, void_list_node);
8315       fn_ptr_type = build_pointer_type (fn_type);
8316       arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8317       /* Build the final atexit type.  */
8318       fn_type = build_function_type (integer_type_node, arg_types);
8319       name = "atexit";
8320     }
8321
8322   /* Now, build the function declaration.  */
8323   push_lang_context (lang_name_c);
8324   atexit_fndecl = build_library_fn_ptr (name, fn_type);
8325   mark_used (atexit_fndecl);
8326   pop_lang_context ();
8327   atexit_node = default_conversion (atexit_fndecl);
8328
8329   return atexit_node;
8330 }
8331
8332 /* Returns the __dso_handle VAR_DECL.  */
8333
8334 static tree
8335 get_dso_handle_node (void)
8336 {
8337   if (dso_handle_node)
8338     return dso_handle_node;
8339
8340   /* Declare the variable.  */
8341   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8342                                         ptr_type_node);
8343
8344   return dso_handle_node;
8345 }
8346
8347 /* Begin a new function with internal linkage whose job will be simply
8348    to destroy some particular variable.  */
8349
8350 static GTY(()) int start_cleanup_cnt;
8351
8352 static tree
8353 start_cleanup_fn (void)
8354 {
8355   int old_interface_only = interface_only;
8356   int old_interface_unknown = interface_unknown;
8357   char name[32];
8358   tree parmtypes;
8359   tree fntype;
8360   tree fndecl;
8361
8362   push_to_top_level ();
8363
8364   /* No need to mangle this.  */
8365   push_lang_context (lang_name_c);
8366
8367   interface_only = 0;
8368   interface_unknown = 1;
8369
8370   /* Build the parameter-types.  */
8371   parmtypes = void_list_node;
8372   /* Functions passed to __cxa_atexit take an additional parameter.
8373      We'll just ignore it.  After we implement the new calling
8374      convention for destructors, we can eliminate the use of
8375      additional cleanup functions entirely in the -fnew-abi case.  */
8376   if (flag_use_cxa_atexit)
8377     parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8378   /* Build the function type itself.  */
8379   fntype = build_function_type (void_type_node, parmtypes);
8380   /* Build the name of the function.  */
8381   sprintf (name, "__tcf_%d", start_cleanup_cnt++);
8382   /* Build the function declaration.  */
8383   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8384   /* It's a function with internal linkage, generated by the
8385      compiler.  */
8386   TREE_PUBLIC (fndecl) = 0;
8387   DECL_ARTIFICIAL (fndecl) = 1;
8388   /* Make the function `inline' so that it is only emitted if it is
8389      actually needed.  It is unlikely that it will be inlined, since
8390      it is only called via a function pointer, but we avoid unnecessary
8391      emissions this way.  */
8392   DECL_INLINE (fndecl) = 1;
8393   /* Build the parameter.  */
8394   if (flag_use_cxa_atexit)
8395     {
8396       tree parmdecl;
8397
8398       parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8399       DECL_CONTEXT (parmdecl) = fndecl;
8400       TREE_USED (parmdecl) = 1;
8401       DECL_ARGUMENTS (fndecl) = parmdecl;
8402     }
8403
8404   pushdecl (fndecl);
8405   start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8406
8407   interface_unknown = old_interface_unknown;
8408   interface_only = old_interface_only;
8409
8410   pop_lang_context ();
8411
8412   return current_function_decl;
8413 }
8414
8415 /* Finish the cleanup function begun by start_cleanup_fn.  */
8416
8417 static void
8418 end_cleanup_fn (void)
8419 {
8420   expand_body (finish_function (0));
8421
8422   pop_from_top_level ();
8423 }
8424
8425 /* Generate code to handle the destruction of DECL, an object with
8426    static storage duration.  */
8427
8428 void
8429 register_dtor_fn (tree decl)
8430 {
8431   tree cleanup;
8432   tree compound_stmt;
8433   tree args;
8434   tree fcall;
8435
8436   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8437     return;
8438
8439   /* Call build_cleanup before we enter the anonymous function so that
8440      any access checks will be done relative to the current scope,
8441      rather than the scope of the anonymous function.  */
8442   build_cleanup (decl);
8443
8444   /* Now start the function.  */
8445   cleanup = start_cleanup_fn ();
8446
8447   /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8448      to the original function, rather than the anonymous one.  That
8449      will make the back-end think that nested functions are in use,
8450      which causes confusion.  */
8451   
8452   push_deferring_access_checks (dk_no_check);
8453   fcall = build_cleanup (decl);
8454   pop_deferring_access_checks ();
8455
8456   /* Create the body of the anonymous function.  */
8457   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8458   finish_expr_stmt (fcall);
8459   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8460   end_cleanup_fn ();
8461
8462   /* Call atexit with the cleanup function.  */
8463   cxx_mark_addressable (cleanup);
8464   cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8465   if (flag_use_cxa_atexit)
8466     {
8467       args = tree_cons (NULL_TREE, 
8468                         build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8469                         NULL_TREE);
8470       args = tree_cons (NULL_TREE, null_pointer_node, args);
8471       args = tree_cons (NULL_TREE, cleanup, args);
8472     }
8473   else
8474     args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8475   finish_expr_stmt (build_function_call (get_atexit_node (), args));
8476 }
8477
8478 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8479    is its initializer.  Generate code to handle the construction
8480    and destruction of DECL.  */
8481
8482 static void
8483 expand_static_init (tree decl, tree init)
8484 {
8485   tree oldstatic;
8486
8487   my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8488   my_friendly_assert (TREE_STATIC (decl), 20021010);
8489
8490   /* Some variables require no initialization.  */
8491   if (!init 
8492       && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8493       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8494     return;
8495
8496   oldstatic = value_member (decl, static_aggregates);
8497
8498   if (oldstatic)
8499     {
8500       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8501         error ("multiple initializations given for `%D'", decl);
8502     }
8503   else if (! toplevel_bindings_p ())
8504     {
8505       /* Emit code to perform this initialization but once.  */
8506       tree if_stmt;
8507       tree then_clause;
8508       tree assignment;
8509       tree guard;
8510       tree guard_init;
8511
8512       /* Emit code to perform this initialization but once.  This code
8513          looks like:
8514
8515            static int guard = 0;
8516            if (!guard) {
8517              // Do initialization.
8518              guard = 1;
8519              // Register variable for destruction at end of program.
8520            }
8521
8522          Note that the `temp' variable is only set to 1 *after* the
8523          initialization is complete.  This ensures that an exception,
8524          thrown during the construction, will cause the variable to
8525          reinitialized when we pass through this code again, as per:
8526
8527            [stmt.dcl]
8528
8529            If the initialization exits by throwing an exception, the
8530            initialization is not complete, so it will be tried again
8531            the next time control enters the declaration.
8532
8533          In theory, this process should be thread-safe, too; multiple
8534          threads should not be able to initialize the variable more
8535          than once.  We don't yet attempt to ensure thread-safety.  */
8536
8537       /* Create the guard variable.  */
8538       guard = get_guard (decl);
8539
8540       /* Begin the conditional initialization.  */
8541       if_stmt = begin_if_stmt ();
8542       finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8543       then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8544
8545       /* Do the initialization itself.  */
8546       assignment = init ? init : NULL_TREE;
8547
8548       /* Once the assignment is complete, set TEMP to 1.  Since the
8549          construction of the static object is complete at this point,
8550          we want to make sure TEMP is set to 1 even if a temporary
8551          constructed during the initialization throws an exception
8552          when it is destroyed.  So, we combine the initialization and
8553          the assignment to TEMP into a single expression, ensuring
8554          that when we call finish_expr_stmt the cleanups will not be
8555          run until after TEMP is set to 1.  */
8556       guard_init = set_guard (guard);
8557       if (assignment)
8558         {
8559           assignment = tree_cons (NULL_TREE, assignment,
8560                                   build_tree_list (NULL_TREE,
8561                                                    guard_init));
8562           assignment = build_compound_expr (assignment);
8563         }
8564       else
8565         assignment = guard_init;
8566       finish_expr_stmt (assignment);
8567
8568       /* Use atexit to register a function for destroying this static
8569          variable.  */
8570       register_dtor_fn (decl);
8571
8572       finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8573       finish_then_clause (if_stmt);
8574       finish_if_stmt ();
8575     }
8576   else
8577     static_aggregates = tree_cons (init, decl, static_aggregates);
8578 }
8579
8580 /* Finish the declaration of a catch-parameter.  */
8581
8582 tree
8583 start_handler_parms (tree declspecs, tree declarator)
8584 {
8585   tree decl;
8586   if (declspecs)
8587     {
8588       decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8589                              1, NULL);
8590       if (decl == NULL_TREE)
8591         error ("invalid catch parameter");
8592     }
8593   else
8594     decl = NULL_TREE;
8595
8596   return decl;
8597 }
8598
8599 \f
8600 /* Make TYPE a complete type based on INITIAL_VALUE.
8601    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8602    2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8603
8604 int
8605 complete_array_type (tree type, tree initial_value, int do_default)
8606 {
8607   register tree maxindex = NULL_TREE;
8608   int value = 0;
8609
8610   if (initial_value)
8611     {
8612       /* An array of character type can be initialized from a
8613          brace-enclosed string constant.  */
8614       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8615           && TREE_CODE (initial_value) == CONSTRUCTOR
8616           && CONSTRUCTOR_ELTS (initial_value)
8617           && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8618               == STRING_CST)
8619           && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8620         initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8621
8622       /* Note MAXINDEX is really the maximum index, one less than the
8623          size.  */
8624       if (TREE_CODE (initial_value) == STRING_CST)
8625         {
8626           int eltsize
8627             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8628           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8629                                    / eltsize) - 1, 0);
8630         }
8631       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8632         {
8633           tree elts = CONSTRUCTOR_ELTS (initial_value);
8634
8635           maxindex = ssize_int (-1);
8636           for (; elts; elts = TREE_CHAIN (elts))
8637             {
8638               if (TREE_PURPOSE (elts))
8639                 maxindex = TREE_PURPOSE (elts);
8640               else
8641                 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8642             }
8643           maxindex = copy_node (maxindex);
8644         }
8645       else
8646         {
8647           /* Make an error message unless that happened already.  */
8648           if (initial_value != error_mark_node)
8649             value = 1;
8650           else
8651             initial_value = NULL_TREE;
8652
8653           /* Prevent further error messages.  */
8654           maxindex = build_int_2 (0, 0);
8655         }
8656     }
8657
8658   if (!maxindex)
8659     {
8660       if (do_default)
8661         maxindex = build_int_2 (0, 0);
8662       value = 2;
8663     }
8664
8665   if (maxindex)
8666     {
8667       tree itype;
8668       tree domain;
8669
8670       domain = build_index_type (maxindex);
8671       TYPE_DOMAIN (type) = domain;
8672
8673       if (! TREE_TYPE (maxindex))
8674         TREE_TYPE (maxindex) = domain;
8675       if (initial_value)
8676         itype = TREE_TYPE (initial_value);
8677       else
8678         itype = NULL;
8679       if (itype && !TYPE_DOMAIN (itype))
8680         TYPE_DOMAIN (itype) = domain;
8681       /* The type of the main variant should never be used for arrays
8682          of different sizes.  It should only ever be completed with the
8683          size of the array.  */
8684       if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8685         TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8686     }
8687
8688   /* Lay out the type now that we can get the real answer.  */
8689
8690   layout_type (type);
8691
8692   return value;
8693 }
8694 \f
8695 /* Return zero if something is declared to be a member of type
8696    CTYPE when in the context of CUR_TYPE.  STRING is the error
8697    message to print in that case.  Otherwise, quietly return 1.  */
8698
8699 static int
8700 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8701 {
8702   if (ctype && ctype != cur_type)
8703     {
8704       if (flags == DTOR_FLAG)
8705         error ("destructor for alien class `%T' cannot be a member",
8706                   ctype);
8707       else
8708         error ("constructor for alien class `%T' cannot be a member",
8709                   ctype);
8710       return 0;
8711     }
8712   return 1;
8713 }
8714 \f
8715 /* Subroutine of `grokdeclarator'.  */
8716
8717 /* Generate errors possibly applicable for a given set of specifiers.
8718    This is for ARM $7.1.2.  */
8719
8720 static void
8721 bad_specifiers (tree object,
8722                 const char* type,
8723                 int virtualp,
8724                 int quals,
8725                 int inlinep,
8726                 int friendp,
8727                 int raises)
8728 {
8729   if (virtualp)
8730     error ("`%D' declared as a `virtual' %s", object, type);
8731   if (inlinep)
8732     error ("`%D' declared as an `inline' %s", object, type);
8733   if (quals)
8734     error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8735               object, type);
8736   if (friendp)
8737     cp_error_at ("`%D' declared as a friend", object);
8738   if (raises
8739       && (TREE_CODE (object) == TYPE_DECL
8740           || (!TYPE_PTRFN_P (TREE_TYPE (object))
8741               && !TYPE_REFFN_P (TREE_TYPE (object))
8742               && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8743     cp_error_at ("`%D' declared with an exception specification", object);
8744 }
8745
8746 /* CTYPE is class type, or null if non-class.
8747    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8748    or METHOD_TYPE.
8749    DECLARATOR is the function's name.
8750    VIRTUALP is truthvalue of whether the function is virtual or not.
8751    FLAGS are to be passed through to `grokclassfn'.
8752    QUALS are qualifiers indicating whether the function is `const'
8753    or `volatile'.
8754    RAISES is a list of exceptions that this function can raise.
8755    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8756    not look, and -1 if we should not call `grokclassfn' at all.
8757
8758    Returns `NULL_TREE' if something goes wrong, after issuing
8759    applicable error messages.  */
8760
8761 static tree
8762 grokfndecl (tree ctype, 
8763             tree type,
8764             tree declarator,
8765             tree orig_declarator,
8766             int virtualp,
8767             enum overload_flags flags,
8768             tree quals, 
8769             tree raises,
8770             int check, 
8771             int friendp, 
8772             int publicp, 
8773             int inlinep, 
8774             int funcdef_flag, 
8775             int template_count,
8776             tree in_namespace)
8777 {
8778   tree decl;
8779   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8780   int has_default_arg = 0;
8781   tree t;
8782
8783   if (raises)
8784     type = build_exception_variant (type, raises);
8785
8786   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8787   /* Propagate volatile out from type to decl.  */
8788   if (TYPE_VOLATILE (type))
8789     TREE_THIS_VOLATILE (decl) = 1;
8790
8791   /* If this decl has namespace scope, set that up.  */
8792   if (in_namespace)
8793     set_decl_namespace (decl, in_namespace, friendp);
8794   else if (!ctype)
8795     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8796
8797   /* `main' and builtins have implicit 'C' linkage.  */
8798   if ((MAIN_NAME_P (declarator)
8799        || (IDENTIFIER_LENGTH (declarator) > 10
8800            && IDENTIFIER_POINTER (declarator)[0] == '_'
8801            && IDENTIFIER_POINTER (declarator)[1] == '_'
8802            && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8803       && current_lang_name == lang_name_cplusplus
8804       && ctype == NULL_TREE
8805       /* NULL_TREE means global namespace.  */
8806       && DECL_CONTEXT (decl) == NULL_TREE)
8807     SET_DECL_LANGUAGE (decl, lang_c);
8808
8809   /* Should probably propagate const out from type to decl I bet (mrs).  */
8810   if (staticp)
8811     {
8812       DECL_STATIC_FUNCTION_P (decl) = 1;
8813       DECL_CONTEXT (decl) = ctype;
8814     }
8815
8816   if (ctype)
8817     DECL_CONTEXT (decl) = ctype;
8818
8819   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8820     {
8821       if (processing_template_decl)
8822         error ("cannot declare `::main' to be a template");
8823       if (inlinep)
8824         error ("cannot declare `::main' to be inline");
8825       if (!publicp)
8826         error ("cannot declare `::main' to be static");
8827       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8828                         integer_type_node))
8829         error ("`main' must return `int'");
8830       inlinep = 0;
8831       publicp = 1;
8832     }
8833
8834   /* Members of anonymous types and local classes have no linkage; make
8835      them internal.  */
8836   /* FIXME what if it gets a name from typedef?  */
8837   if (ctype && (TYPE_ANONYMOUS_P (ctype)
8838                 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8839     publicp = 0;
8840
8841   if (publicp)
8842     {
8843       /* [basic.link]: A name with no linkage (notably, the name of a class
8844          or enumeration declared in a local scope) shall not be used to
8845          declare an entity with linkage.
8846
8847          Only check this for public decls for now.  See core 319, 389.  */
8848       t = no_linkage_check (TREE_TYPE (decl));
8849       if (t)
8850         {
8851           if (TYPE_ANONYMOUS_P (t))
8852             {
8853               if (DECL_EXTERN_C_P (decl))
8854                 /* Allow this; it's pretty common in C.  */;
8855               else
8856                 {
8857                   pedwarn ("non-local function `%#D' uses anonymous type",
8858                               decl);
8859                   if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8860                     cp_pedwarn_at ("\
8861 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8862                                 TYPE_NAME (t));
8863                 }
8864             }
8865           else
8866             pedwarn ("non-local function `%#D' uses local type `%T'",
8867                         decl, t);
8868         }
8869     }
8870
8871   TREE_PUBLIC (decl) = publicp;
8872   if (! publicp)
8873     {
8874       DECL_INTERFACE_KNOWN (decl) = 1;
8875       DECL_NOT_REALLY_EXTERN (decl) = 1;
8876     }
8877
8878   DID_INLINE_FUNC (decl) = 0;
8879   /* If the declaration was declared inline, mark it as such.  */
8880   if (inlinep)
8881     DECL_DECLARED_INLINE_P (decl) = 1;
8882   /* We inline functions that are explicitly declared inline, or, when
8883      the user explicitly asks us to, all functions.  */
8884   if (DECL_DECLARED_INLINE_P (decl))
8885     DECL_INLINE (decl) = 1;
8886   if (flag_inline_trees == 2 && !DECL_INLINE (decl))
8887     {
8888       DID_INLINE_FUNC (decl) = 1;
8889       DECL_INLINE (decl) = 1;
8890     }
8891
8892   DECL_EXTERNAL (decl) = 1;
8893   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8894     {
8895       error ("%smember function `%D' cannot have `%T' method qualifier",
8896                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8897       quals = NULL_TREE;
8898     }
8899
8900   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8901     grok_op_properties (decl, friendp);
8902
8903   if (ctype && decl_function_context (decl))
8904     DECL_NO_STATIC_CHAIN (decl) = 1;
8905
8906   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8907     if (TREE_PURPOSE (t)
8908         && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8909       {
8910         has_default_arg = 1;
8911         break;
8912       }
8913
8914   if (friendp
8915       && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
8916     {
8917       if (funcdef_flag)
8918         error
8919           ("defining explicit specialization `%D' in friend declaration",
8920            orig_declarator);
8921       else
8922         {
8923           tree fns = TREE_OPERAND (orig_declarator, 0);
8924           tree args = TREE_OPERAND (orig_declarator, 1);
8925
8926           if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8927             {
8928               /* Something like `template <class T> friend void f<T>()'.  */
8929               error ("invalid use of template-id `%D' in declaration of primary template",
8930                         orig_declarator);
8931               return NULL_TREE;
8932             }
8933
8934
8935           /* A friend declaration of the form friend void f<>().  Record
8936              the information in the TEMPLATE_ID_EXPR.  */
8937           SET_DECL_IMPLICIT_INSTANTIATION (decl);
8938
8939           if (TREE_CODE (fns) == COMPONENT_REF)
8940             {
8941               /* Due to bison parser ickiness, we will have already looked
8942                  up an operator_name or PFUNCNAME within the current class
8943                  (see template_id in parse.y). If the current class contains
8944                  such a name, we'll get a COMPONENT_REF here. Undo that.  */
8945
8946               my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
8947                                   == current_class_type, 20001120);
8948               fns = TREE_OPERAND (fns, 1);
8949             }
8950           my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
8951                               || TREE_CODE (fns) == LOOKUP_EXPR
8952                               || TREE_CODE (fns) == OVERLOAD, 20001120);
8953           DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
8954
8955           if (has_default_arg)
8956             {
8957               error ("default arguments are not allowed in declaration of friend template specialization `%D'",
8958                         decl);
8959               return NULL_TREE;
8960             }
8961
8962           if (inlinep)
8963             {
8964               error ("`inline' is not allowed in declaration of friend template specialization `%D'",
8965                         decl);
8966               return NULL_TREE;
8967             }
8968         }
8969     }
8970
8971   if (funcdef_flag)
8972     /* Make the init_value nonzero so pushdecl knows this is not
8973        tentative.  error_mark_node is replaced later with the BLOCK.  */
8974     DECL_INITIAL (decl) = error_mark_node;
8975
8976   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
8977     TREE_NOTHROW (decl) = 1;
8978
8979   /* Caller will do the rest of this.  */
8980   if (check < 0)
8981     return decl;
8982
8983   if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
8984     DECL_CONSTRUCTOR_P (decl) = 1;
8985
8986   /* Function gets the ugly name, field gets the nice one.  This call
8987      may change the type of the function (because of default
8988      parameters)!  */
8989   if (ctype != NULL_TREE)
8990     grokclassfn (ctype, decl, flags, quals);
8991
8992   decl = check_explicit_specialization (orig_declarator, decl,
8993                                         template_count,
8994                                         2 * (funcdef_flag != 0) +
8995                                         4 * (friendp != 0));
8996   if (decl == error_mark_node)
8997     return NULL_TREE;
8998
8999   if (ctype != NULL_TREE
9000       && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9001       && check)
9002     {
9003       tree old_decl;
9004
9005       old_decl = check_classfn (ctype, decl);
9006
9007       if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9008         /* Because grokfndecl is always supposed to return a
9009            FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9010            here.  We depend on our callers to figure out that its
9011            really a template that's being returned.  */
9012         old_decl = DECL_TEMPLATE_RESULT (old_decl);
9013
9014       if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9015           && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9016         {
9017           /* Remove the `this' parm added by grokclassfn.
9018              XXX Isn't this done in start_function, too?  */
9019           revert_static_member_fn (decl);
9020           last_function_parms = TREE_CHAIN (last_function_parms);
9021         }
9022       if (old_decl && DECL_ARTIFICIAL (old_decl))
9023         error ("definition of implicitly-declared `%D'", old_decl);
9024
9025       if (old_decl)
9026         {
9027           bool ok;
9028
9029           /* Since we've smashed OLD_DECL to its
9030              DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9031           if (TREE_CODE (decl) == TEMPLATE_DECL)
9032             decl = DECL_TEMPLATE_RESULT (decl);
9033
9034           /* Attempt to merge the declarations.  This can fail, in
9035              the case of some invalid specialization declarations.  */
9036           push_scope (ctype);
9037           ok = duplicate_decls (decl, old_decl);
9038           pop_scope (ctype);
9039           if (!ok)
9040             {
9041               error ("no `%#D' member function declared in class `%T'",
9042                      decl, ctype);
9043               return NULL_TREE;
9044             }
9045           return old_decl;
9046         }
9047     }
9048
9049   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9050     return NULL_TREE;
9051
9052   if (ctype == NULL_TREE || check)
9053     return decl;
9054
9055   if (virtualp)
9056     DECL_VIRTUAL_P (decl) = 1;
9057
9058   return decl;
9059 }
9060
9061 /* Create a VAR_DECL named NAME with the indicated TYPE.  
9062
9063    If SCOPE is non-NULL, it is the class type or namespace containing
9064    the variable.  If SCOPE is NULL, the variable should is created in
9065    the innermost enclosings scope.  */
9066
9067 static tree
9068 grokvardecl (tree type,
9069              tree name,
9070              RID_BIT_TYPE * specbits_in,
9071              int initialized,
9072              int constp,
9073              tree scope)
9074 {
9075   tree decl;
9076   RID_BIT_TYPE specbits;
9077
9078   my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, 
9079                       20020808);
9080
9081   specbits = *specbits_in;
9082
9083   /* Compute the scope in which to place the variable.  */
9084   if (!scope)
9085     {
9086       /* An explicit "extern" specifier indicates a namespace-scope
9087          variable.  */
9088       if (RIDBIT_SETP (RID_EXTERN, specbits))
9089         scope = current_namespace;
9090       else if (!at_function_scope_p ())
9091         {
9092           scope = current_scope ();
9093           if (!scope)
9094             scope = current_namespace;
9095         }
9096     }
9097
9098   if (scope
9099       && (/* If the variable is a namespace-scope variable declared in a
9100              template, we need DECL_LANG_SPECIFIC.  */
9101           (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9102           /* Similarly for namespace-scope variables with language linkage
9103              other than C++.  */
9104           || (TREE_CODE (scope) == NAMESPACE_DECL 
9105               && current_lang_name != lang_name_cplusplus)
9106           /* Similarly for static data members.  */
9107           || TYPE_P (scope)))
9108     decl = build_lang_decl (VAR_DECL, name, type);
9109   else
9110     decl = build_decl (VAR_DECL, name, type);
9111
9112   if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9113     set_decl_namespace (decl, scope, 0);
9114   else
9115     DECL_CONTEXT (decl) = scope;
9116
9117   if (name && scope && current_lang_name != lang_name_c)
9118     /* We can't mangle lazily here because we don't have any
9119        way to recover whether or not a variable was `extern
9120        "C"' later.  */
9121     mangle_decl (decl);
9122
9123   if (RIDBIT_SETP (RID_EXTERN, specbits))
9124     {
9125       DECL_THIS_EXTERN (decl) = 1;
9126       DECL_EXTERNAL (decl) = !initialized;
9127     }
9128
9129   /* In class context, static means one per class,
9130      public access, and static storage.  */
9131   if (DECL_CLASS_SCOPE_P (decl))
9132     {
9133       TREE_PUBLIC (decl) = 1;
9134       TREE_STATIC (decl) = 1;
9135       DECL_EXTERNAL (decl) = 0;
9136     }
9137   /* At top level, either `static' or no s.c. makes a definition
9138      (perhaps tentative), and absence of `static' makes it public.  */
9139   else if (toplevel_bindings_p ())
9140     {
9141       TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9142                             && (DECL_THIS_EXTERN (decl) || ! constp));
9143       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9144     }
9145   /* Not at top level, only `static' makes a static definition.  */
9146   else
9147     {
9148       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9149       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9150     }
9151
9152   if (RIDBIT_SETP (RID_THREAD, specbits))
9153     {
9154       if (targetm.have_tls)
9155         DECL_THREAD_LOCAL (decl) = 1;
9156       else
9157         /* A mere warning is sure to result in improper semantics
9158            at runtime.  Don't bother to allow this to compile.  */
9159         error ("thread-local storage not supported for this target");
9160     }
9161
9162   if (TREE_PUBLIC (decl))
9163     {
9164       /* [basic.link]: A name with no linkage (notably, the name of a class
9165          or enumeration declared in a local scope) shall not be used to
9166          declare an entity with linkage.
9167
9168          Only check this for public decls for now.  */
9169       tree t = no_linkage_check (TREE_TYPE (decl));
9170       if (t)
9171         {
9172           if (TYPE_ANONYMOUS_P (t))
9173             /* Ignore for now; `enum { foo } e' is pretty common.  */;
9174           else
9175             pedwarn ("non-local variable `%#D' uses local type `%T'",
9176                         decl, t);
9177         }
9178     }
9179
9180   return decl;
9181 }
9182
9183 /* Create and return a canonical pointer to member function type, for
9184    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9185
9186 tree
9187 build_ptrmemfunc_type (tree type)
9188 {
9189   tree field, fields;
9190   tree t;
9191   tree unqualified_variant = NULL_TREE;
9192
9193   if (type == error_mark_node)
9194     return type;
9195
9196   /* If a canonical type already exists for this type, use it.  We use
9197      this method instead of type_hash_canon, because it only does a
9198      simple equality check on the list of field members.  */
9199
9200   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9201     return t;
9202
9203   /* Make sure that we always have the unqualified pointer-to-member
9204      type first.  */
9205   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9206     unqualified_variant
9207       = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9208
9209   t = make_aggr_type (RECORD_TYPE);
9210   /* Let the front-end know this is a pointer to member function...  */
9211   TYPE_PTRMEMFUNC_FLAG (t) = 1;
9212   /* ... and not really an aggregate.  */
9213   SET_IS_AGGR_TYPE (t, 0);
9214
9215   field = build_decl (FIELD_DECL, pfn_identifier, type);
9216   fields = field;
9217   
9218   field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9219   TREE_CHAIN (field) = fields;
9220   fields = field;
9221   
9222   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9223
9224   /* Zap out the name so that the back-end will give us the debugging
9225      information for this anonymous RECORD_TYPE.  */
9226   TYPE_NAME (t) = NULL_TREE;
9227
9228   /* If this is not the unqualified form of this pointer-to-member
9229      type, set the TYPE_MAIN_VARIANT for this type to be the
9230      unqualified type.  Since they are actually RECORD_TYPEs that are
9231      not variants of each other, we must do this manually.  */
9232   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9233     {
9234       t = build_qualified_type (t, cp_type_quals (type));
9235       TYPE_MAIN_VARIANT (t) = unqualified_variant;
9236       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9237       TYPE_NEXT_VARIANT (unqualified_variant) = t;
9238     }
9239
9240   /* Cache this pointer-to-member type so that we can find it again
9241      later.  */
9242   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9243
9244   /* Seems to be wanted.  */
9245   CLASSTYPE_GOT_SEMICOLON (t) = 1;
9246
9247   return t;
9248 }
9249
9250 /* Create and return a pointer to data member type.  */
9251
9252 tree
9253 build_ptrmem_type (tree class_type, tree member_type)
9254 {
9255   return build_pointer_type (build_offset_type (class_type, member_type));
9256 }
9257
9258 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9259    Check to see that the definition is valid.  Issue appropriate error
9260    messages.  Return 1 if the definition is particularly bad, or 0
9261    otherwise.  */
9262
9263 int
9264 check_static_variable_definition (tree decl, tree type)
9265 {
9266   /* Motion 10 at San Diego: If a static const integral data member is
9267      initialized with an integral constant expression, the initializer
9268      may appear either in the declaration (within the class), or in
9269      the definition, but not both.  If it appears in the class, the
9270      member is a member constant.  The file-scope definition is always
9271      required.  */
9272   if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9273     {
9274       error ("invalid in-class initialization of static data member of non-integral type `%T'",
9275              type);
9276       /* If we just return the declaration, crashes will sometimes
9277          occur.  We therefore return void_type_node, as if this was a
9278          friend declaration, to cause callers to completely ignore
9279          this declaration.  */
9280       return 1;
9281     }
9282   else if (!CP_TYPE_CONST_P (type))
9283     error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9284               decl);
9285   else if (pedantic && !INTEGRAL_TYPE_P (type))
9286     pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9287
9288   return 0;
9289 }
9290
9291 /* Given the SIZE (i.e., number of elements) in an array, compute an
9292    appropriate index type for the array.  If non-NULL, NAME is the
9293    name of the thing being declared.  */
9294
9295 tree
9296 compute_array_index_type (tree name, tree size)
9297 {
9298   tree itype;
9299
9300   /* If this involves a template parameter, it will be a constant at
9301      instantiation time, but we don't know what the value is yet.
9302      Even if no template parameters are involved, we may an expression
9303      that is not a constant; we don't even simplify `1 + 2' when
9304      processing a template.  */
9305   if (processing_template_decl)
9306     {
9307       /* Resolve a qualified reference to an enumerator or static
9308          const data member of ours.  */
9309       if (TREE_CODE (size) == SCOPE_REF
9310           && TREE_OPERAND (size, 0) == current_class_type)
9311         {
9312           tree t = lookup_field (current_class_type,
9313                                  TREE_OPERAND (size, 1), 0, false);
9314           if (t)
9315             size = t;
9316         }
9317
9318       return build_index_type (build_min (MINUS_EXPR, sizetype,
9319                                           size, integer_one_node));
9320     }
9321
9322   /* The size might be the result of a cast.  */
9323   STRIP_TYPE_NOPS (size);
9324
9325   /* It might be a const variable or enumeration constant.  */
9326   size = decl_constant_value (size);
9327
9328   /* The array bound must be an integer type.  */
9329   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9330       && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9331       && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9332     {
9333       if (name)
9334         error ("size of array `%D' has non-integer type", name);
9335       else
9336         error ("size of array has non-integer type");
9337       size = integer_one_node;
9338     }
9339
9340   /* Normally, the array-bound will be a constant.  */
9341   if (TREE_CODE (size) == INTEGER_CST)
9342     {
9343       /* Check to see if the array bound overflowed.  Make that an
9344          error, no matter how generous we're being.  */
9345       int old_flag_pedantic_errors = flag_pedantic_errors;
9346       int old_pedantic = pedantic;
9347       pedantic = flag_pedantic_errors = 1;
9348       constant_expression_warning (size);
9349       pedantic = old_pedantic;
9350       flag_pedantic_errors = old_flag_pedantic_errors;
9351
9352       /* An array must have a positive number of elements.  */
9353       if (INT_CST_LT (size, integer_zero_node))
9354         {
9355           if (name)
9356             error ("size of array `%D' is negative", name);
9357           else
9358             error ("size of array is negative");
9359           size = integer_one_node;
9360         }
9361       /* Except that an extension we allow zero-sized arrays.  We
9362          always allow them in system headers because glibc uses
9363          them.  */
9364       else if (integer_zerop (size) && pedantic && !in_system_header)
9365         {
9366           if (name)
9367             pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9368           else
9369             pedwarn ("ISO C++ forbids zero-size array");
9370         }
9371     }
9372   else if (TREE_CONSTANT (size))
9373     {
9374       /* `(int) &fn' is not a valid array bound.  */
9375       if (name)
9376         error ("size of array `%D' is not an integral constant-expression",
9377                   name);
9378       else
9379         error ("size of array is not an integral constant-expression");
9380     }
9381
9382   /* Compute the index of the largest element in the array.  It is
9383      one less than the number of elements in the array.  */
9384   itype
9385     = fold (cp_build_binary_op (MINUS_EXPR,
9386                                 cp_convert (ssizetype, size),
9387                                 cp_convert (ssizetype,
9388                                             integer_one_node)));
9389
9390   /* Check for variable-sized arrays.  We allow such things as an
9391      extension, even though they are not allowed in ANSI/ISO C++.  */
9392   if (!TREE_CONSTANT (itype))
9393     {
9394       if (pedantic)
9395         {
9396           if (name)
9397             pedwarn ("ISO C++ forbids variable-size array `%D'",
9398                         name);
9399           else
9400             pedwarn ("ISO C++ forbids variable-size array");
9401         }
9402
9403       /* Create a variable-sized array index type.  */
9404       itype = variable_size (itype);
9405     }
9406   /* Make sure that there was no overflow when creating to a signed
9407      index type.  (For example, on a 32-bit machine, an array with
9408      size 2^32 - 1 is too big.)  */
9409   else if (TREE_OVERFLOW (itype))
9410     {
9411       error ("overflow in array dimension");
9412       TREE_OVERFLOW (itype) = 0;
9413     }
9414
9415   /* Create and return the appropriate index type.  */
9416   return build_index_type (itype);
9417 }
9418
9419 /* Returns the scope (if any) in which the entity declared by
9420    DECLARATOR will be located.  If the entity was declared with an
9421    unqualified name, NULL_TREE is returned.  */
9422
9423 tree
9424 get_scope_of_declarator (tree declarator)
9425 {
9426   if (!declarator)
9427     return NULL_TREE;
9428   
9429   switch (TREE_CODE (declarator))
9430     {
9431     case CALL_EXPR:
9432     case ARRAY_REF:
9433     case INDIRECT_REF:
9434     case ADDR_EXPR:
9435       /* For any of these, the main declarator is the first operand.  */
9436       return get_scope_of_declarator (TREE_OPERAND
9437                                       (declarator, 0));
9438
9439     case SCOPE_REF:
9440       /* For a pointer-to-member, continue descending.  */
9441       if (TREE_CODE (TREE_OPERAND (declarator, 1))
9442           == INDIRECT_REF)
9443         return get_scope_of_declarator (TREE_OPERAND
9444                                         (declarator, 1));
9445       /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9446          which the declaration occurs is the first operand.  */
9447       return TREE_OPERAND (declarator, 0);
9448
9449     case TREE_LIST:
9450       /* Attributes to be applied. The declarator is TREE_VALUE.  */
9451       return get_scope_of_declarator (TREE_VALUE (declarator));
9452       
9453     default:
9454       /* Otherwise, we have a declarator-id which is not a qualified
9455          name; the entity will be declared in the current scope.  */
9456       return NULL_TREE;
9457     }
9458 }
9459
9460 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9461    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
9462    with this type.  */
9463
9464 static tree
9465 create_array_type_for_decl (tree name, tree type, tree size)
9466 {
9467   tree itype = NULL_TREE;
9468   const char* error_msg;
9469
9470   /* If things have already gone awry, bail now.  */
9471   if (type == error_mark_node || size == error_mark_node)
9472     return error_mark_node;
9473
9474   /* Assume that everything will go OK.  */
9475   error_msg = NULL;
9476
9477   /* There are some types which cannot be array elements.  */
9478   switch (TREE_CODE (type))
9479     {
9480     case VOID_TYPE:
9481       error_msg = "array of void";
9482       break;
9483
9484     case FUNCTION_TYPE:
9485       error_msg = "array of functions";
9486       break;
9487
9488     case REFERENCE_TYPE:
9489       error_msg = "array of references";
9490       break;
9491
9492     case OFFSET_TYPE:
9493       error_msg = "array of data members";
9494       break;
9495
9496     case METHOD_TYPE:
9497       error_msg = "array of function members";
9498       break;
9499
9500     default:
9501       break;
9502     }
9503
9504   /* If something went wrong, issue an error-message and return.  */
9505   if (error_msg)
9506     {
9507       if (name)
9508         error ("declaration of `%D' as %s", name, error_msg);
9509       else
9510         error ("creating %s", error_msg);
9511
9512       return error_mark_node;
9513     }
9514
9515   /* [dcl.array]
9516
9517      The constant expressions that specify the bounds of the arrays
9518      can be omitted only for the first member of the sequence.  */
9519   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9520     {
9521       if (name)
9522         error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9523                   name);
9524       else
9525         error ("multidimensional array must have bounds for all dimensions except the first");
9526
9527       return error_mark_node;
9528     }
9529
9530   /* Figure out the index type for the array.  */
9531   if (size)
9532     itype = compute_array_index_type (name, size);
9533
9534   return build_cplus_array_type (type, itype);
9535 }
9536
9537 /* Check that it's OK to declare a function with the indicated TYPE.
9538    SFK indicates the kind of special function (if any) that this
9539    function is.  OPTYPE is the type given in a conversion operator
9540    declaration.  Returns the actual return type of the function; that
9541    may be different than TYPE if an error occurs, or for certain
9542    special functions.  */
9543
9544 static tree
9545 check_special_function_return_type (special_function_kind sfk,
9546                                     tree type,
9547                                     tree optype)
9548 {
9549   switch (sfk)
9550     {
9551     case sfk_constructor:
9552       if (type)
9553         error ("return type specification for constructor invalid");
9554
9555       type = void_type_node;
9556       break;
9557
9558     case sfk_destructor:
9559       if (type)
9560         error ("return type specification for destructor invalid");
9561       type = void_type_node;
9562       break;
9563
9564     case sfk_conversion:
9565       if (type && !same_type_p (type, optype))
9566         error ("operator `%T' declared to return `%T'", optype, type);
9567       else if (type)
9568         pedwarn ("return type specified for `operator %T'",  optype);
9569       type = optype;
9570       break;
9571
9572     default:
9573       abort ();
9574       break;
9575     }
9576
9577   return type;
9578 }
9579
9580 /* Given declspecs and a declarator (abstract or otherwise), determine
9581    the name and type of the object declared and construct a DECL node
9582    for it.
9583
9584    DECLSPECS is a chain of tree_list nodes whose value fields
9585     are the storage classes and type specifiers.
9586
9587    DECL_CONTEXT says which syntactic context this declaration is in:
9588      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9589      FUNCDEF for a function definition.  Like NORMAL but a few different
9590       error messages in each case.  Return value may be zero meaning
9591       this definition is too screwy to try to parse.
9592      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
9593       handle member functions (which have FIELD context).
9594       Return value may be zero meaning this definition is too screwy to
9595       try to parse.
9596      PARM for a parameter declaration (either within a function prototype
9597       or before a function body).  Make a PARM_DECL, or return void_type_node.
9598      CATCHPARM for a parameter declaration before a catch clause.
9599      TYPENAME if for a typename (in a cast or sizeof).
9600       Don't make a DECL node; just return the ..._TYPE node.
9601      FIELD for a struct or union field; make a FIELD_DECL.
9602      BITFIELD for a field with specified width.
9603    INITIALIZED is 1 if the decl has an initializer.
9604
9605    ATTRLIST is a pointer to the list of attributes, which may be NULL
9606    if there are none; *ATTRLIST may be modified if attributes from inside
9607    the declarator should be applied to the declaration.
9608
9609    When this function is called, scoping variables (such as
9610    CURRENT_CLASS_TYPE) should reflect the scope in which the
9611    declaration occurs, not the scope in which the new declaration will
9612    be placed.  For example, on:
9613
9614      void S::f() { ... }
9615
9616    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9617    should not be `S'.  */
9618
9619 tree
9620 grokdeclarator (tree declarator,
9621                 tree declspecs,
9622                 enum decl_context decl_context,
9623                 int initialized,
9624                 tree* attrlist)
9625 {
9626   RID_BIT_TYPE specbits;
9627   int nclasses = 0;
9628   tree spec;
9629   tree type = NULL_TREE;
9630   int longlong = 0;
9631   int type_quals;
9632   int virtualp, explicitp, friendp, inlinep, staticp;
9633   int explicit_int = 0;
9634   int explicit_char = 0;
9635   int defaulted_int = 0;
9636   int extern_langp = 0;
9637   tree dependant_name = NULL_TREE;
9638   
9639   tree typedef_decl = NULL_TREE;
9640   const char *name;
9641   tree typedef_type = NULL_TREE;
9642   int funcdef_flag = 0;
9643   enum tree_code innermost_code = ERROR_MARK;
9644   int bitfield = 0;
9645 #if 0
9646   /* See the code below that used this.  */
9647   tree decl_attr = NULL_TREE;
9648 #endif
9649
9650   /* Keep track of what sort of function is being processed
9651      so that we can warn about default return values, or explicit
9652      return values which do not match prescribed defaults.  */
9653   special_function_kind sfk = sfk_none;
9654
9655   tree dname = NULL_TREE;
9656   tree ctype = current_class_type;
9657   tree ctor_return_type = NULL_TREE;
9658   enum overload_flags flags = NO_SPECIAL;
9659   tree quals = NULL_TREE;
9660   tree raises = NULL_TREE;
9661   int template_count = 0;
9662   tree in_namespace = NULL_TREE;
9663   tree returned_attrs = NULL_TREE;
9664   tree scope = NULL_TREE;
9665
9666   RIDBIT_RESET_ALL (specbits);
9667   if (decl_context == FUNCDEF)
9668     funcdef_flag = 1, decl_context = NORMAL;
9669   else if (decl_context == MEMFUNCDEF)
9670     funcdef_flag = -1, decl_context = FIELD;
9671   else if (decl_context == BITFIELD)
9672     bitfield = 1, decl_context = FIELD;
9673
9674   /* Look inside a declarator for the name being declared
9675      and get it as a string, for an error message.  */
9676   {
9677     tree *next = &declarator;
9678     register tree decl;
9679     name = NULL;
9680
9681     while (next && *next)
9682       {
9683         decl = *next;
9684         switch (TREE_CODE (decl))
9685           {
9686           case TREE_LIST:
9687             /* For attributes.  */
9688             next = &TREE_VALUE (decl);
9689             break;
9690
9691           case COND_EXPR:
9692             ctype = NULL_TREE;
9693             next = &TREE_OPERAND (decl, 0);
9694             break;
9695
9696           case BIT_NOT_EXPR:    /* For C++ destructors!  */
9697             {
9698               tree name = TREE_OPERAND (decl, 0);
9699               tree rename = NULL_TREE;
9700
9701               my_friendly_assert (flags == NO_SPECIAL, 152);
9702               flags = DTOR_FLAG;
9703               sfk = sfk_destructor;
9704               if (TYPE_P (name))
9705                 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9706               my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9707               if (ctype == NULL_TREE)
9708                 {
9709                   if (current_class_type == NULL_TREE)
9710                     {
9711                       error ("destructors must be member functions");
9712                       flags = NO_SPECIAL;
9713                     }
9714                   else
9715                     {
9716                       tree t = constructor_name (current_class_type);
9717                       if (t != name)
9718                         rename = t;
9719                     }
9720                 }
9721               else
9722                 {
9723                   tree t = constructor_name (ctype);
9724                   if (t != name)
9725                     rename = t;
9726                 }
9727
9728               if (rename)
9729                 {
9730                   error ("destructor `%T' must match class name `%T'",
9731                             name, rename);
9732                   TREE_OPERAND (decl, 0) = rename;
9733                 }
9734               next = &name;
9735             }
9736             break;
9737
9738           case ADDR_EXPR:       /* C++ reference declaration */
9739             /* Fall through.  */
9740           case ARRAY_REF:
9741           case INDIRECT_REF:
9742             ctype = NULL_TREE;
9743             innermost_code = TREE_CODE (decl);
9744             next = &TREE_OPERAND (decl, 0);
9745             break;
9746
9747           case CALL_EXPR:
9748             innermost_code = TREE_CODE (decl);
9749             if (decl_context == FIELD && ctype == NULL_TREE)
9750               ctype = current_class_type;
9751             if (ctype
9752                 && TREE_OPERAND (decl, 0)
9753                 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9754                     && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9755                                            ctype)))
9756               TREE_OPERAND (decl, 0) = constructor_name (ctype);
9757             next = &TREE_OPERAND (decl, 0);
9758             decl = *next;
9759             if (ctype != NULL_TREE
9760                 && decl != NULL_TREE && flags != DTOR_FLAG
9761                 && constructor_name_p (decl, ctype))
9762               {
9763                 sfk = sfk_constructor;
9764                 ctor_return_type = ctype;
9765               }
9766             ctype = NULL_TREE;
9767             break;
9768
9769           case TEMPLATE_ID_EXPR:
9770               {
9771                 tree fns = TREE_OPERAND (decl, 0);
9772
9773                 if (TREE_CODE (fns) == LOOKUP_EXPR)
9774                   fns = TREE_OPERAND (fns, 0);
9775
9776                 dname = fns;
9777                 if (TREE_CODE (dname) == COMPONENT_REF)
9778                   dname = TREE_OPERAND (dname, 1);
9779                 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9780                   {
9781                     my_friendly_assert (is_overloaded_fn (dname),
9782                                         19990331);
9783                     dname = DECL_NAME (get_first_fn (dname));
9784                   }
9785               }
9786           /* Fall through.  */
9787
9788           case IDENTIFIER_NODE:
9789             if (TREE_CODE (decl) == IDENTIFIER_NODE)
9790               dname = decl;
9791
9792             next = 0;
9793
9794             if (C_IS_RESERVED_WORD (dname))
9795               {
9796                 error ("declarator-id missing; using reserved word `%D'",
9797                           dname);
9798                 name = IDENTIFIER_POINTER (dname);
9799               }
9800             else if (!IDENTIFIER_TYPENAME_P (dname))
9801               name = IDENTIFIER_POINTER (dname);
9802             else
9803               {
9804                 my_friendly_assert (flags == NO_SPECIAL, 154);
9805                 flags = TYPENAME_FLAG;
9806                 ctor_return_type = TREE_TYPE (dname);
9807                 sfk = sfk_conversion;
9808                 if (IDENTIFIER_GLOBAL_VALUE (dname)
9809                     && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9810                         == TYPE_DECL))
9811                   name = IDENTIFIER_POINTER (dname);
9812                 else
9813                   name = "<invalid operator>";
9814               }
9815             break;
9816
9817             /* C++ extension */
9818           case SCOPE_REF:
9819             {
9820               /* Perform error checking, and decide on a ctype.  */
9821               tree cname = TREE_OPERAND (decl, 0);
9822               if (cname == NULL_TREE)
9823                 ctype = NULL_TREE;
9824               else if (TREE_CODE (cname) == NAMESPACE_DECL)
9825                 {
9826                   ctype = NULL_TREE;
9827                   in_namespace = TREE_OPERAND (decl, 0);
9828                 }
9829               else if (! is_aggr_type (cname, 1))
9830                 ctype = NULL_TREE;
9831               /* Must test TREE_OPERAND (decl, 1), in case user gives
9832                  us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9833               else if (TREE_OPERAND (decl, 1)
9834                        && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9835                 ctype = cname;
9836               else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9837                        || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9838                 {
9839                   /* This might be declaring a member of a template
9840                      parm to be a friend.  */
9841                   ctype = cname;
9842                   dependant_name = TREE_OPERAND (decl, 1);
9843                 }
9844               else if (ctype == NULL_TREE)
9845                 ctype = cname;
9846               else if (TREE_COMPLEXITY (decl) == current_class_depth)
9847                 ;
9848               else
9849                 {
9850                   if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9851                     {
9852                       error ("type `%T' is not derived from type `%T'",
9853                                 cname, ctype);
9854                       ctype = NULL_TREE;
9855                     }
9856                   else
9857                     ctype = cname;
9858                 }
9859
9860               /* It is valid to write:
9861
9862                    class C { void f(); };
9863                    typedef C D;
9864                    void D::f();
9865
9866                  The standard is not clear about whether `typedef const C D' is
9867                  legal; as of 2002-09-15 the committee is considering
9868                  that question.  EDG 3.0 allows that syntax.
9869                  Therefore, we do as well.  */
9870               if (ctype)
9871                 ctype = TYPE_MAIN_VARIANT (ctype);
9872               /* Update the declarator so that when we process it
9873                  again the correct type is present.  */
9874               TREE_OPERAND (decl, 0) = ctype;
9875
9876               if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9877                   && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9878                                          ctype))
9879                 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9880               next = &TREE_OPERAND (decl, 1);
9881               decl = *next;
9882               if (ctype)
9883                 {
9884                   if (TREE_CODE (decl) == IDENTIFIER_NODE
9885                       && constructor_name_p (decl, ctype))
9886                     {
9887                       sfk = sfk_constructor;
9888                       ctor_return_type = ctype;
9889                     }
9890                   else if (TREE_CODE (decl) == BIT_NOT_EXPR
9891                            && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9892                            && constructor_name_p (TREE_OPERAND (decl, 0),
9893                                                   ctype))
9894                     {
9895                       sfk = sfk_destructor;
9896                       ctor_return_type = ctype;
9897                       flags = DTOR_FLAG;
9898                       TREE_OPERAND (decl, 0) = constructor_name (ctype);
9899                       next = &TREE_OPERAND (decl, 0);
9900                     }
9901                 }
9902             }
9903             break;
9904
9905           case ERROR_MARK:
9906             next = 0;
9907             break;
9908
9909           case TYPE_DECL:
9910             /* Parse error puts this typespec where
9911                a declarator should go.  */
9912             error ("`%T' specified as declarator-id", DECL_NAME (decl));
9913             if (TREE_TYPE (decl) == current_class_type)
9914               error ("  perhaps you want `%T' for a constructor",
9915                         current_class_name);
9916             dname = DECL_NAME (decl);
9917             name = IDENTIFIER_POINTER (dname);
9918
9919             /* Avoid giving two errors for this.  */
9920             IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
9921
9922             declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
9923             *next = dname;
9924             next = 0;
9925             break;
9926
9927           case BASELINK:
9928             next = &BASELINK_FUNCTIONS (decl);
9929             break;
9930
9931           case TEMPLATE_DECL:
9932             /* Sometimes, we see a template-name used as part of a 
9933                decl-specifier like in 
9934                   std::allocator alloc;
9935                Handle that gracefully.  */
9936             error ("invalid use of template-name '%E' in a declarator", decl);
9937             return error_mark_node;
9938             break;
9939             
9940           default:
9941             my_friendly_assert (0, 20020917);
9942           }
9943       }
9944   }
9945
9946   /* A function definition's declarator must have the form of
9947      a function declarator.  */
9948
9949   if (funcdef_flag && innermost_code != CALL_EXPR)
9950     return 0;
9951
9952   if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
9953       && innermost_code != CALL_EXPR
9954       && ! (ctype && declspecs == NULL_TREE))
9955     {
9956       error ("declaration of `%D' as non-function", dname);
9957       return void_type_node;
9958     }
9959
9960   /* Anything declared one level down from the top level
9961      must be one of the parameters of a function
9962      (because the body is at least two levels down).  */
9963
9964   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
9965      by not allowing C++ class definitions to specify their parameters
9966      with xdecls (must be spec.d in the parmlist).
9967
9968      Since we now wait to push a class scope until we are sure that
9969      we are in a legitimate method context, we must set oldcname
9970      explicitly (since current_class_name is not yet alive).
9971
9972      We also want to avoid calling this a PARM if it is in a namespace.  */
9973
9974   if (decl_context == NORMAL && !toplevel_bindings_p ())
9975     {
9976       struct cp_binding_level *b = current_binding_level;
9977       current_binding_level = b->level_chain;
9978       if (current_binding_level != 0 && toplevel_bindings_p ())
9979         decl_context = PARM;
9980       current_binding_level = b;
9981     }
9982
9983   if (name == NULL)
9984     name = decl_context == PARM ? "parameter" : "type name";
9985
9986   /* Look through the decl specs and record which ones appear.
9987      Some typespecs are defined as built-in typenames.
9988      Others, the ones that are modifiers of other types,
9989      are represented by bits in SPECBITS: set the bits for
9990      the modifiers that appear.  Storage class keywords are also in SPECBITS.
9991
9992      If there is a typedef name or a type, store the type in TYPE.
9993      This includes builtin typedefs such as `int'.
9994
9995      Set EXPLICIT_INT if the type is `int' or `char' and did not
9996      come from a user typedef.
9997
9998      Set LONGLONG if `long' is mentioned twice.
9999
10000      For C++, constructors and destructors have their own fast treatment.  */
10001
10002   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10003     {
10004       register int i;
10005       register tree id;
10006
10007       /* Certain parse errors slip through.  For example,
10008          `int class;' is not caught by the parser. Try
10009          weakly to recover here.  */
10010       if (TREE_CODE (spec) != TREE_LIST)
10011         return 0;
10012
10013       id = TREE_VALUE (spec);
10014
10015       /* If the entire declaration is itself tagged as deprecated then
10016          suppress reports of deprecated items.  */
10017       if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10018         {
10019           if (deprecated_state != DEPRECATED_SUPPRESS)
10020             warn_deprecated_use (id);
10021         }
10022
10023       if (TREE_CODE (id) == IDENTIFIER_NODE)
10024         {
10025           if (id == ridpointers[(int) RID_INT]
10026               || id == ridpointers[(int) RID_CHAR]
10027               || id == ridpointers[(int) RID_BOOL]
10028               || id == ridpointers[(int) RID_WCHAR])
10029             {
10030               if (type)
10031                 {
10032                   if (id == ridpointers[(int) RID_BOOL])
10033                     error ("`bool' is now a keyword");
10034                   else
10035                     error ("extraneous `%T' ignored", id);
10036                 }
10037               else
10038                 {
10039                   if (id == ridpointers[(int) RID_INT])
10040                     explicit_int = 1;
10041                   else if (id == ridpointers[(int) RID_CHAR])
10042                     explicit_char = 1;
10043                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10044                 }
10045               goto found;
10046             }
10047           /* C++ aggregate types.  */
10048           if (IDENTIFIER_HAS_TYPE_VALUE (id))
10049             {
10050               if (type)
10051                 error ("multiple declarations `%T' and `%T'", type, id);
10052               else
10053                 type = IDENTIFIER_TYPE_VALUE (id);
10054               goto found;
10055             }
10056
10057           for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10058             {
10059               if (ridpointers[i] == id)
10060                 {
10061                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10062                     {
10063                       if (pedantic && ! in_system_header && warn_long_long)
10064                         pedwarn ("ISO C++ does not support `long long'");
10065                       if (longlong)
10066                         error ("`long long long' is too long for GCC");
10067                       else
10068                         longlong = 1;
10069                     }
10070                   else if (RIDBIT_SETP (i, specbits))
10071                     pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10072
10073                   /* Diagnose "__thread extern" or "__thread static".  */
10074                   if (RIDBIT_SETP (RID_THREAD, specbits))
10075                     {
10076                       if (i == (int)RID_EXTERN)
10077                         error ("`__thread' before `extern'");
10078                       else if (i == (int)RID_STATIC)
10079                         error ("`__thread' before `static'");
10080                     }
10081
10082                   if (i == (int)RID_EXTERN
10083                       && TREE_PURPOSE (spec) == error_mark_node)
10084                     /* This extern was part of a language linkage.  */
10085                     extern_langp = 1;
10086
10087                   RIDBIT_SET (i, specbits);
10088                   goto found;
10089                 }
10090             }
10091         }
10092       else if (TREE_CODE (id) == TYPE_DECL)
10093         {
10094           if (type)
10095             error ("multiple declarations `%T' and `%T'", type,
10096                       TREE_TYPE (id));
10097           else
10098             {
10099               type = TREE_TYPE (id);
10100               TREE_VALUE (spec) = type;
10101               typedef_decl = id;
10102             }
10103           goto found;
10104         }
10105       if (type)
10106         error ("two or more data types in declaration of `%s'", name);
10107       else if (TREE_CODE (id) == IDENTIFIER_NODE)
10108         {
10109           register tree t = lookup_name (id, 1);
10110           if (!t || TREE_CODE (t) != TYPE_DECL)
10111             error ("`%s' fails to be a typedef or built in type",
10112                    IDENTIFIER_POINTER (id));
10113           else
10114             {
10115               type = TREE_TYPE (t);
10116               typedef_decl = t;
10117             }
10118         }
10119       else if (id != error_mark_node)
10120         /* Can't change CLASS nodes into RECORD nodes here!  */
10121         type = id;
10122
10123     found: ;
10124     }
10125
10126 #if 0
10127   /* See the code below that used this.  */
10128   if (typedef_decl)
10129     decl_attr = DECL_ATTRIBUTES (typedef_decl);
10130 #endif
10131   typedef_type = type;
10132
10133   /* No type at all: default to `int', and set DEFAULTED_INT
10134      because it was not a user-defined typedef.  */
10135
10136   if (type == NULL_TREE
10137       && (RIDBIT_SETP (RID_SIGNED, specbits)
10138           || RIDBIT_SETP (RID_UNSIGNED, specbits)
10139           || RIDBIT_SETP (RID_LONG, specbits)
10140           || RIDBIT_SETP (RID_SHORT, specbits)))
10141     {
10142       /* These imply 'int'.  */
10143       type = integer_type_node;
10144       defaulted_int = 1;
10145     }
10146
10147   if (sfk != sfk_none)
10148     type = check_special_function_return_type (sfk, type,
10149                                                ctor_return_type);
10150   else if (type == NULL_TREE)
10151     {
10152       int is_main;
10153
10154       explicit_int = -1;
10155
10156       /* We handle `main' specially here, because 'main () { }' is so
10157          common.  With no options, it is allowed.  With -Wreturn-type,
10158          it is a warning.  It is only an error with -pedantic-errors.  */
10159       is_main = (funcdef_flag
10160                  && dname && MAIN_NAME_P (dname)
10161                  && ctype == NULL_TREE
10162                  && in_namespace == NULL_TREE
10163                  && current_namespace == global_namespace);
10164
10165       if (in_system_header || flag_ms_extensions)
10166         /* Allow it, sigh.  */;
10167       else if (pedantic || ! is_main)
10168         pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10169                     name);
10170       else if (warn_return_type)
10171         warning ("ISO C++ forbids declaration of `%s' with no type",
10172                     name);
10173
10174       type = integer_type_node;
10175     }
10176   
10177   ctype = NULL_TREE;
10178
10179   /* Now process the modifiers that were specified
10180      and check for invalid combinations.  */
10181
10182   /* Long double is a special combination.  */
10183
10184   if (RIDBIT_SETP (RID_LONG, specbits)
10185       && TYPE_MAIN_VARIANT (type) == double_type_node)
10186     {
10187       RIDBIT_RESET (RID_LONG, specbits);
10188       type = build_qualified_type (long_double_type_node,
10189                                    cp_type_quals (type));
10190     }
10191
10192   /* Check all other uses of type modifiers.  */
10193
10194   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10195       || RIDBIT_SETP (RID_SIGNED, specbits)
10196       || RIDBIT_SETP (RID_LONG, specbits)
10197       || RIDBIT_SETP (RID_SHORT, specbits))
10198     {
10199       int ok = 0;
10200
10201       if (TREE_CODE (type) == REAL_TYPE)
10202         error ("short, signed or unsigned invalid for `%s'", name);
10203       else if (TREE_CODE (type) != INTEGER_TYPE)
10204         error ("long, short, signed or unsigned invalid for `%s'", name);
10205       else if (RIDBIT_SETP (RID_LONG, specbits)
10206                && RIDBIT_SETP (RID_SHORT, specbits))
10207         error ("long and short specified together for `%s'", name);
10208       else if ((RIDBIT_SETP (RID_LONG, specbits)
10209                 || RIDBIT_SETP (RID_SHORT, specbits))
10210                && explicit_char)
10211         error ("long or short specified with char for `%s'", name);
10212       else if ((RIDBIT_SETP (RID_LONG, specbits)
10213                 || RIDBIT_SETP (RID_SHORT, specbits))
10214                && TREE_CODE (type) == REAL_TYPE)
10215         error ("long or short specified with floating type for `%s'", name);
10216       else if (RIDBIT_SETP (RID_SIGNED, specbits)
10217                && RIDBIT_SETP (RID_UNSIGNED, specbits))
10218         error ("signed and unsigned given together for `%s'", name);
10219       else
10220         {
10221           ok = 1;
10222           if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10223             {
10224               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10225                        name);
10226               if (flag_pedantic_errors)
10227                 ok = 0;
10228             }
10229         }
10230
10231       /* Discard the type modifiers if they are invalid.  */
10232       if (! ok)
10233         {
10234           RIDBIT_RESET (RID_UNSIGNED, specbits);
10235           RIDBIT_RESET (RID_SIGNED, specbits);
10236           RIDBIT_RESET (RID_LONG, specbits);
10237           RIDBIT_RESET (RID_SHORT, specbits);
10238           longlong = 0;
10239         }
10240     }
10241
10242   if (RIDBIT_SETP (RID_COMPLEX, specbits)
10243       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10244     {
10245       error ("complex invalid for `%s'", name);
10246       RIDBIT_RESET (RID_COMPLEX, specbits);
10247     }
10248
10249   /* Decide whether an integer type is signed or not.
10250      Optionally treat bitfields as signed by default.  */
10251   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10252       /* [class.bit]
10253
10254          It is implementation-defined whether a plain (neither
10255          explicitly signed or unsigned) char, short, int, or long
10256          bit-field is signed or unsigned.
10257
10258          Naturally, we extend this to long long as well.  Note that
10259          this does not include wchar_t.  */
10260       || (bitfield && !flag_signed_bitfields
10261           && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10262           /* A typedef for plain `int' without `signed' can be
10263              controlled just like plain `int', but a typedef for
10264              `signed int' cannot be so controlled.  */
10265           && !(typedef_decl
10266                && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10267           && (TREE_CODE (type) == INTEGER_TYPE
10268               || TREE_CODE (type) == CHAR_TYPE)
10269           && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10270     {
10271       if (longlong)
10272         type = long_long_unsigned_type_node;
10273       else if (RIDBIT_SETP (RID_LONG, specbits))
10274         type = long_unsigned_type_node;
10275       else if (RIDBIT_SETP (RID_SHORT, specbits))
10276         type = short_unsigned_type_node;
10277       else if (type == char_type_node)
10278         type = unsigned_char_type_node;
10279       else if (typedef_decl)
10280         type = c_common_unsigned_type (type);
10281       else
10282         type = unsigned_type_node;
10283     }
10284   else if (RIDBIT_SETP (RID_SIGNED, specbits)
10285            && type == char_type_node)
10286     type = signed_char_type_node;
10287   else if (longlong)
10288     type = long_long_integer_type_node;
10289   else if (RIDBIT_SETP (RID_LONG, specbits))
10290     type = long_integer_type_node;
10291   else if (RIDBIT_SETP (RID_SHORT, specbits))
10292     type = short_integer_type_node;
10293
10294   if (RIDBIT_SETP (RID_COMPLEX, specbits))
10295     {
10296       /* If we just have "complex", it is equivalent to
10297          "complex double", but if any modifiers at all are specified it is
10298          the complex form of TYPE.  E.g, "complex short" is
10299          "complex short int".  */
10300
10301       if (defaulted_int && ! longlong
10302           && ! (RIDBIT_SETP (RID_LONG, specbits)
10303                 || RIDBIT_SETP (RID_SHORT, specbits)
10304                 || RIDBIT_SETP (RID_SIGNED, specbits)
10305                 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10306         type = complex_double_type_node;
10307       else if (type == integer_type_node)
10308         type = complex_integer_type_node;
10309       else if (type == float_type_node)
10310         type = complex_float_type_node;
10311       else if (type == double_type_node)
10312         type = complex_double_type_node;
10313       else if (type == long_double_type_node)
10314         type = complex_long_double_type_node;
10315       else
10316         type = build_complex_type (type);
10317     }
10318
10319   type_quals = TYPE_UNQUALIFIED;
10320   if (RIDBIT_SETP (RID_CONST, specbits))
10321     type_quals |= TYPE_QUAL_CONST;
10322   if (RIDBIT_SETP (RID_VOLATILE, specbits))
10323     type_quals |= TYPE_QUAL_VOLATILE;
10324   if (RIDBIT_SETP (RID_RESTRICT, specbits))
10325     type_quals |= TYPE_QUAL_RESTRICT;
10326   if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10327     error ("qualifiers are not allowed on declaration of `operator %T'",
10328               ctor_return_type);
10329
10330   type_quals |= cp_type_quals (type);
10331   type = cp_build_qualified_type_real
10332     (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10333                          ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10334   /* We might have ignored or rejected some of the qualifiers.  */
10335   type_quals = cp_type_quals (type);
10336   
10337   staticp = 0;
10338   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10339   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10340   RIDBIT_RESET (RID_VIRTUAL, specbits);
10341   explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10342   RIDBIT_RESET (RID_EXPLICIT, specbits);
10343
10344   if (RIDBIT_SETP (RID_STATIC, specbits))
10345     staticp = 1 + (decl_context == FIELD);
10346
10347   if (virtualp && staticp == 2)
10348     {
10349       error ("member `%D' cannot be declared both virtual and static",
10350                 dname);
10351       staticp = 0;
10352     }
10353   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10354   RIDBIT_RESET (RID_FRIEND, specbits);
10355
10356   if (dependant_name && !friendp)
10357     {
10358       error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10359       return void_type_node;
10360     }
10361   
10362   /* Warn if two storage classes are given. Default to `auto'.  */
10363
10364   if (RIDBIT_ANY_SET (specbits))
10365     {
10366       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10367       if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10368       if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10369       if (decl_context == PARM && nclasses > 0)
10370         error ("storage class specifiers invalid in parameter declarations");
10371       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10372         {
10373           if (decl_context == PARM)
10374             error ("typedef declaration invalid in parameter declaration");
10375           nclasses++;
10376         }
10377       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10378       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10379       if (!nclasses && !friendp && extern_langp)
10380         nclasses++;
10381     }
10382
10383   /* Give error if `virtual' is used outside of class declaration.  */
10384   if (virtualp
10385       && (current_class_name == NULL_TREE || decl_context != FIELD))
10386     {
10387       error ("virtual outside class declaration");
10388       virtualp = 0;
10389     }
10390
10391   /* Static anonymous unions are dealt with here.  */
10392   if (staticp && decl_context == TYPENAME
10393       && TREE_CODE (declspecs) == TREE_LIST
10394       && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10395     decl_context = FIELD;
10396
10397   /* Warn about storage classes that are invalid for certain
10398      kinds of declarations (parameters, typenames, etc.).  */
10399
10400   /* "static __thread" and "extern __thread" are allowed.  */
10401   if (nclasses == 2
10402       && RIDBIT_SETP (RID_THREAD, specbits)
10403       && (RIDBIT_SETP (RID_EXTERN, specbits)
10404           || RIDBIT_SETP (RID_STATIC, specbits)))
10405     nclasses = 1;
10406     
10407   if (nclasses > 1)
10408     error ("multiple storage classes in declaration of `%s'", name);
10409   else if (decl_context != NORMAL && nclasses > 0)
10410     {
10411       if ((decl_context == PARM || decl_context == CATCHPARM)
10412           && (RIDBIT_SETP (RID_REGISTER, specbits)
10413               || RIDBIT_SETP (RID_AUTO, specbits)))
10414         ;
10415       else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10416         ;
10417       else if (decl_context == FIELD
10418                /* C++ allows static class elements  */
10419                && RIDBIT_SETP (RID_STATIC, specbits))
10420         /* C++ also allows inlines and signed and unsigned elements,
10421            but in those cases we don't come in here.  */
10422         ;
10423       else
10424         {
10425           if (decl_context == FIELD)
10426             {
10427               tree tmp = NULL_TREE;
10428               register int op = 0;
10429
10430               if (declarator)
10431                 {
10432                   /* Avoid trying to get an operand off an identifier node.  */
10433                   if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10434                     tmp = declarator;
10435                   else
10436                     tmp = TREE_OPERAND (declarator, 0);
10437                   op = IDENTIFIER_OPNAME_P (tmp);
10438                   if (IDENTIFIER_TYPENAME_P (tmp))
10439                     {
10440                       if (IDENTIFIER_GLOBAL_VALUE (tmp)
10441                           && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10442                               == TYPE_DECL))
10443                         name = IDENTIFIER_POINTER (tmp);
10444                       else
10445                         name = "<invalid operator>";
10446                     }
10447                 }
10448               error ("storage class specified for %s `%s'",
10449                      op ? "member operator" : "field",
10450                      name);
10451             }
10452           else
10453             {
10454               if (decl_context == PARM || decl_context == CATCHPARM)
10455                 error ("storage class specified for parameter `%s'", name);
10456               else
10457                 error ("storage class specified for typename");
10458             }
10459           RIDBIT_RESET (RID_REGISTER, specbits);
10460           RIDBIT_RESET (RID_AUTO, specbits);
10461           RIDBIT_RESET (RID_EXTERN, specbits);
10462           RIDBIT_RESET (RID_THREAD, specbits);
10463         }
10464     }
10465   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10466     {
10467       if (toplevel_bindings_p ())
10468         {
10469           /* It's common practice (and completely valid) to have a const
10470              be initialized and declared extern.  */
10471           if (!(type_quals & TYPE_QUAL_CONST))
10472             warning ("`%s' initialized and declared `extern'", name);
10473         }
10474       else
10475         error ("`%s' has both `extern' and initializer", name);
10476     }
10477   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10478            && ! toplevel_bindings_p ())
10479     error ("nested function `%s' declared `extern'", name);
10480   else if (toplevel_bindings_p ())
10481     {
10482       if (RIDBIT_SETP (RID_AUTO, specbits))
10483         error ("top-level declaration of `%s' specifies `auto'", name);
10484     }
10485   else if (RIDBIT_SETP (RID_THREAD, specbits)
10486            && !RIDBIT_SETP (RID_EXTERN, specbits)
10487            && !RIDBIT_SETP (RID_STATIC, specbits))
10488     {
10489       error ("function-scope `%s' implicitly auto and declared `__thread'",
10490              name);
10491       RIDBIT_RESET (RID_THREAD, specbits);
10492     }
10493
10494   if (nclasses > 0 && friendp)
10495     error ("storage class specifiers invalid in friend function declarations");
10496
10497   scope = get_scope_of_declarator (declarator);
10498
10499   /* Now figure out the structure of the declarator proper.
10500      Descend through it, creating more complex types, until we reach
10501      the declared identifier (or NULL_TREE, in an abstract declarator).  */
10502
10503   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10504          && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10505     {
10506       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10507          an INDIRECT_REF (for *...),
10508          a CALL_EXPR (for ...(...)),
10509          an identifier (for the name being declared)
10510          or a null pointer (for the place in an absolute declarator
10511          where the name was omitted).
10512          For the last two cases, we have just exited the loop.
10513
10514          For C++ it could also be
10515          a SCOPE_REF (for class :: ...).  In this case, we have converted
10516          sensible names to types, and those are the values we use to
10517          qualify the member name.
10518          an ADDR_EXPR (for &...),
10519          a BIT_NOT_EXPR (for destructors)
10520
10521          At this point, TYPE is the type of elements of an array,
10522          or for a function to return, or for a pointer to point to.
10523          After this sequence of ifs, TYPE is the type of the
10524          array or function or pointer, and DECLARATOR has had its
10525          outermost layer removed.  */
10526
10527       if (type == error_mark_node)
10528         {
10529           if (declarator == error_mark_node)
10530             return error_mark_node;
10531           else if (TREE_CODE (declarator) == SCOPE_REF)
10532             declarator = TREE_OPERAND (declarator, 1);
10533           else
10534             declarator = TREE_OPERAND (declarator, 0);
10535           continue;
10536         }
10537       if (quals != NULL_TREE
10538           && (declarator == NULL_TREE
10539               || TREE_CODE (declarator) != SCOPE_REF))
10540         {
10541           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10542             ctype = TYPE_METHOD_BASETYPE (type);
10543           if (ctype != NULL_TREE)
10544             {
10545               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10546               grok_method_quals (ctype, dummy, quals);
10547               type = TREE_TYPE (dummy);
10548               quals = NULL_TREE;
10549             }
10550         }
10551
10552       switch (TREE_CODE (declarator))
10553         {
10554         case TREE_LIST:
10555           {
10556             /* We encode a declarator with embedded attributes using
10557                a TREE_LIST.  */
10558             tree attrs = TREE_PURPOSE (declarator);
10559             tree inner_decl;
10560             int attr_flags;
10561
10562             declarator = TREE_VALUE (declarator);
10563             inner_decl = declarator;
10564             while (inner_decl != NULL_TREE
10565                    && TREE_CODE (inner_decl) == TREE_LIST)
10566               inner_decl = TREE_VALUE (inner_decl);
10567             attr_flags = 0;
10568             if (inner_decl == NULL_TREE
10569                 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10570               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10571             if (TREE_CODE (inner_decl) == CALL_EXPR)
10572               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10573             if (TREE_CODE (inner_decl) == ARRAY_REF)
10574               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10575             returned_attrs = decl_attributes (&type,
10576                                               chainon (returned_attrs, attrs),
10577                                               attr_flags);
10578           }
10579           break;
10580
10581         case ARRAY_REF:
10582           {
10583             register tree size;
10584
10585             size = TREE_OPERAND (declarator, 1);
10586
10587             /* VC++ spells a zero-sized array with [].  */
10588             if (size == NULL_TREE && decl_context == FIELD && ! staticp
10589                 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10590               size = integer_zero_node;
10591
10592             declarator = TREE_OPERAND (declarator, 0);
10593
10594             type = create_array_type_for_decl (dname, type, size);
10595
10596             ctype = NULL_TREE;
10597           }
10598           break;
10599
10600         case CALL_EXPR:
10601           {
10602             tree arg_types;
10603             int funcdecl_p;
10604             tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10605             tree inner_decl = TREE_OPERAND (declarator, 0);
10606
10607             /* Declaring a function type.
10608                Make sure we have a valid type for the function to return.  */
10609
10610             /* We now know that the TYPE_QUALS don't apply to the
10611                decl, but to its return type.  */
10612             type_quals = TYPE_UNQUALIFIED;
10613
10614             /* Warn about some types functions can't return.  */
10615
10616             if (TREE_CODE (type) == FUNCTION_TYPE)
10617               {
10618                 error ("`%s' declared as function returning a function", name);
10619                 type = integer_type_node;
10620               }
10621             if (TREE_CODE (type) == ARRAY_TYPE)
10622               {
10623                 error ("`%s' declared as function returning an array", name);
10624                 type = integer_type_node;
10625               }
10626
10627             if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10628               inner_decl = TREE_OPERAND (inner_decl, 1);
10629
10630             if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10631               inner_decl = dname;
10632
10633             /* Pick up type qualifiers which should be applied to `this'.  */
10634             quals = CALL_DECLARATOR_QUALS (declarator);
10635
10636             /* Pick up the exception specifications.  */
10637             raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10638
10639             /* Say it's a definition only for the CALL_EXPR
10640                closest to the identifier.  */
10641             funcdecl_p
10642               = inner_decl
10643               && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10644                   || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10645                   || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10646
10647             if (ctype == NULL_TREE
10648                 && decl_context == FIELD
10649                 && funcdecl_p
10650                 && (friendp == 0 || dname == current_class_name))
10651               ctype = current_class_type;
10652
10653             if (ctype && sfk == sfk_conversion)
10654               TYPE_HAS_CONVERSION (ctype) = 1;
10655             if (ctype && constructor_name_p (dname, ctype))
10656               {
10657                 /* We are within a class's scope. If our declarator name
10658                    is the same as the class name, and we are defining
10659                    a function, then it is a constructor/destructor, and
10660                    therefore returns a void type.  */
10661
10662                 if (flags == DTOR_FLAG)
10663                   {
10664                     /* ISO C++ 12.4/2.  A destructor may not be
10665                        declared const or volatile.  A destructor may
10666                        not be static.  */
10667                     if (staticp == 2)
10668                       error ("destructor cannot be static member function");
10669                     if (quals)
10670                       {
10671                         error ("destructors may not be `%s'",
10672                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10673                         quals = NULL_TREE;
10674                       }
10675                     if (decl_context == FIELD)
10676                       {
10677                         if (! member_function_or_else (ctype,
10678                                                        current_class_type,
10679                                                        flags))
10680                           return void_type_node;
10681                       }
10682                   }
10683                 else            /* It's a constructor.  */
10684                   {
10685                     if (explicitp == 1)
10686                       explicitp = 2;
10687                     /* ISO C++ 12.1.  A constructor may not be
10688                        declared const or volatile.  A constructor may
10689                        not be virtual.  A constructor may not be
10690                        static.  */
10691                     if (staticp == 2)
10692                       error ("constructor cannot be static member function");
10693                     if (virtualp)
10694                       {
10695                         pedwarn ("constructors cannot be declared virtual");
10696                         virtualp = 0;
10697                       }
10698                     if (quals)
10699                       {
10700                         error ("constructors may not be `%s'",
10701                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10702                         quals = NULL_TREE;
10703                       }
10704                     {
10705                       RID_BIT_TYPE tmp_bits;
10706                       memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10707                       RIDBIT_RESET (RID_INLINE, tmp_bits);
10708                       RIDBIT_RESET (RID_STATIC, tmp_bits);
10709                       if (RIDBIT_ANY_SET (tmp_bits))
10710                         error ("return value type specifier for constructor ignored");
10711                     }
10712                     if (decl_context == FIELD)
10713                       {
10714                         if (! member_function_or_else (ctype,
10715                                                        current_class_type,
10716                                                        flags))
10717                           return void_type_node;
10718                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10719                         if (sfk != sfk_constructor)
10720                           return NULL_TREE;
10721                       }
10722                   }
10723                 if (decl_context == FIELD)
10724                   staticp = 0;
10725               }
10726             else if (friendp)
10727               {
10728                 if (initialized)
10729                   error ("can't initialize friend function `%s'", name);
10730                 if (virtualp)
10731                   {
10732                     /* Cannot be both friend and virtual.  */
10733                     error ("virtual functions cannot be friends");
10734                     RIDBIT_RESET (RID_FRIEND, specbits);
10735                     friendp = 0;
10736                   }
10737                 if (decl_context == NORMAL)
10738                   error ("friend declaration not in class definition");
10739                 if (current_function_decl && funcdef_flag)
10740                   error ("can't define friend function `%s' in a local class definition",
10741                             name);
10742               }
10743
10744             /* Construct the function type and go to the next
10745                inner layer of declarator.  */
10746
10747             declarator = TREE_OPERAND (declarator, 0);
10748
10749             /* FIXME: This is where default args should be fully
10750                processed.  */
10751
10752             arg_types = grokparms (inner_parms);
10753
10754             if (declarator && flags == DTOR_FLAG)
10755               {
10756                 /* A destructor declared in the body of a class will
10757                    be represented as a BIT_NOT_EXPR.  But, we just
10758                    want the underlying IDENTIFIER.  */
10759                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10760                   declarator = TREE_OPERAND (declarator, 0);
10761
10762                 if (arg_types != void_list_node)
10763                   {
10764                     error ("destructors may not have parameters");
10765                     arg_types = void_list_node;
10766                     last_function_parms = NULL_TREE;
10767                   }
10768               }
10769
10770             /* ANSI says that `const int foo ();'
10771                does not make the function foo const.  */
10772             type = build_function_type (type, arg_types);
10773           }
10774           break;
10775
10776         case ADDR_EXPR:
10777         case INDIRECT_REF:
10778           /* Filter out pointers-to-references and references-to-references.
10779              We can get these if a TYPE_DECL is used.  */
10780
10781           if (TREE_CODE (type) == REFERENCE_TYPE)
10782             {
10783               error (TREE_CODE (declarator) == ADDR_EXPR
10784                      ? "cannot declare reference to `%#T'"
10785                      : "cannot declare pointer to `%#T'", type);
10786               type = TREE_TYPE (type);
10787             }
10788           else if (VOID_TYPE_P (type)
10789                    && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10790             error (ctype ? "cannot declare pointer to `%#T' member"
10791                      : "cannot declare reference to `%#T'", type);
10792
10793           /* Merge any constancy or volatility into the target type
10794              for the pointer.  */
10795
10796           /* We now know that the TYPE_QUALS don't apply to the decl,
10797              but to the target of the pointer.  */
10798           type_quals = TYPE_UNQUALIFIED;
10799
10800           if (TREE_CODE (declarator) == ADDR_EXPR)
10801             {
10802               if (!VOID_TYPE_P (type))
10803                 type = build_reference_type (type);
10804             }
10805           else if (TREE_CODE (type) == METHOD_TYPE)
10806             type = build_ptrmemfunc_type (build_pointer_type (type));
10807           else if (ctype)
10808             type = build_ptrmem_type (ctype, type);
10809           else
10810             type = build_pointer_type (type);
10811
10812           /* Process a list of type modifier keywords (such as
10813              const or volatile) that were given inside the `*' or `&'.  */
10814
10815           if (TREE_TYPE (declarator))
10816             {
10817               register tree typemodlist;
10818               int erred = 0;
10819               int constp = 0;
10820               int volatilep = 0;
10821               int restrictp = 0;
10822               
10823               for (typemodlist = TREE_TYPE (declarator); typemodlist;
10824                    typemodlist = TREE_CHAIN (typemodlist))
10825                 {
10826                   tree qualifier = TREE_VALUE (typemodlist);
10827
10828                   if (qualifier == ridpointers[(int) RID_CONST])
10829                     {
10830                       constp++;
10831                       type_quals |= TYPE_QUAL_CONST;
10832                     }
10833                   else if (qualifier == ridpointers[(int) RID_VOLATILE])
10834                     {
10835                       volatilep++;
10836                       type_quals |= TYPE_QUAL_VOLATILE;
10837                     }
10838                   else if (qualifier == ridpointers[(int) RID_RESTRICT])
10839                     {
10840                       restrictp++;
10841                       type_quals |= TYPE_QUAL_RESTRICT;
10842                     }
10843                   else if (!erred)
10844                     {
10845                       erred = 1;
10846                       error ("invalid type modifier within pointer declarator");
10847                     }
10848                 }
10849               if (constp > 1)
10850                 pedwarn ("duplicate `const'");
10851               if (volatilep > 1)
10852                 pedwarn ("duplicate `volatile'");
10853               if (restrictp > 1)
10854                 pedwarn ("duplicate `restrict'");
10855               type = cp_build_qualified_type (type, type_quals);
10856               type_quals = cp_type_quals (type);
10857             }
10858           declarator = TREE_OPERAND (declarator, 0);
10859           ctype = NULL_TREE;
10860           break;
10861
10862         case SCOPE_REF:
10863           {
10864             /* We have converted type names to NULL_TREE if the
10865                name was bogus, or to a _TYPE node, if not.
10866
10867                The variable CTYPE holds the type we will ultimately
10868                resolve to.  The code here just needs to build
10869                up appropriate member types.  */
10870             tree sname = TREE_OPERAND (declarator, 1);
10871             tree t;
10872
10873             /* Destructors can have their visibilities changed as well.  */
10874             if (TREE_CODE (sname) == BIT_NOT_EXPR)
10875               sname = TREE_OPERAND (sname, 0);
10876
10877             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10878               {
10879                 /* We had a reference to a global decl, or
10880                    perhaps we were given a non-aggregate typedef,
10881                    in which case we cleared this out, and should just
10882                    keep going as though it wasn't there.  */
10883                 declarator = sname;
10884                 continue;
10885               }
10886             ctype = TREE_OPERAND (declarator, 0);
10887
10888             t = ctype;
10889             while (t != NULL_TREE && CLASS_TYPE_P (t))
10890               {
10891                 /* You're supposed to have one `template <...>'
10892                    for every template class, but you don't need one
10893                    for a full specialization.  For example:
10894
10895                      template <class T> struct S{};
10896                      template <> struct S<int> { void f(); };
10897                      void S<int>::f () {}
10898
10899                    is correct; there shouldn't be a `template <>' for
10900                    the definition of `S<int>::f'.  */
10901                 if (CLASSTYPE_TEMPLATE_INFO (t)
10902                     && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10903                         || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10904                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10905                   template_count += 1;
10906
10907                 t = TYPE_MAIN_DECL (t);
10908                 t = DECL_CONTEXT (t);
10909               }
10910
10911             if (sname == NULL_TREE)
10912               goto done_scoping;
10913
10914             if (TREE_CODE (sname) == IDENTIFIER_NODE)
10915               {
10916                 /* This is the `standard' use of the scoping operator:
10917                    basetype :: member .  */
10918
10919                 if (ctype == current_class_type)
10920                   {
10921                     /* class A {
10922                          void A::f ();
10923                        };
10924
10925                        Is this ill-formed?  */
10926
10927                     if (pedantic)
10928                       pedwarn ("extra qualification `%T::' on member `%s' ignored",
10929                                   ctype, name);
10930                   }
10931                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10932                   {
10933                     if (current_class_type == NULL_TREE || friendp)
10934                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
10935                                                       TYPE_ARG_TYPES (type));
10936                     else
10937                       {
10938                         error ("cannot declare member function `%T::%s' within `%T'",
10939                                   ctype, name, current_class_type);
10940                         return error_mark_node;
10941                       }
10942                   }
10943                 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
10944                          || COMPLETE_TYPE_P (complete_type (ctype)))
10945                   {
10946                     /* Have to move this code elsewhere in this function.
10947                        this code is used for i.e., typedef int A::M; M *pm;
10948
10949                        It is?  How? jason 10/2/94 */
10950
10951                     if (current_class_type)
10952                       {
10953                         error ("cannot declare member `%T::%s' within `%T'",
10954                                   ctype, name, current_class_type);
10955                         return void_type_node;
10956                       }
10957                   }
10958                 else
10959                   {
10960                     cxx_incomplete_type_error (NULL_TREE, ctype);
10961                     return error_mark_node;
10962                   }
10963
10964                 declarator = sname;
10965               }
10966             else if (TREE_CODE (sname) == SCOPE_REF)
10967               abort ();
10968             else
10969               {
10970               done_scoping:
10971                 declarator = TREE_OPERAND (declarator, 1);
10972                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
10973                   /* In this case, we will deal with it later.  */
10974                   ;
10975                 else if (TREE_CODE (type) == FUNCTION_TYPE)
10976                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
10977                                                   TYPE_ARG_TYPES (type));
10978               }
10979           }
10980           break;
10981
10982         case BIT_NOT_EXPR:
10983           declarator = TREE_OPERAND (declarator, 0);
10984           break;
10985
10986         case BASELINK:
10987           declarator = BASELINK_FUNCTIONS (declarator);
10988           break;
10989
10990         case RECORD_TYPE:
10991         case UNION_TYPE:
10992         case ENUMERAL_TYPE:
10993           declarator = NULL_TREE;
10994           break;
10995
10996         case ERROR_MARK:
10997           declarator = NULL_TREE;
10998           break;
10999
11000         default:
11001           abort ();
11002         }
11003     }
11004
11005   if (returned_attrs)
11006     {
11007       if (attrlist)
11008         *attrlist = chainon (returned_attrs, *attrlist);
11009       else
11010         attrlist = &returned_attrs;
11011     }
11012
11013   /* Now TYPE has the actual type.  */
11014
11015   /* Did array size calculations overflow?  */
11016
11017   if (TREE_CODE (type) == ARRAY_TYPE
11018       && COMPLETE_TYPE_P (type)
11019       && TREE_OVERFLOW (TYPE_SIZE (type)))
11020     {
11021       error ("size of array `%s' is too large", name);
11022       /* If we proceed with the array type as it is, we'll eventually
11023          crash in tree_low_cst().  */
11024       type = error_mark_node;
11025     }
11026
11027   if ((decl_context == FIELD || decl_context == PARM)
11028       && !processing_template_decl 
11029       && variably_modified_type_p (type))
11030     {
11031       if (decl_context == FIELD)
11032         error ("data member may not have variably modified type `%T'", type);
11033       else
11034         error ("parameter may not have variably modified type `%T'", type);
11035       type = error_mark_node;
11036     }
11037
11038   if (explicitp == 1 || (explicitp && friendp))
11039     {
11040       /* [dcl.fct.spec] The explicit specifier shall only be used in
11041          declarations of constructors within a class definition.  */
11042       error ("only declarations of constructors can be `explicit'");
11043       explicitp = 0;
11044     }
11045
11046   if (RIDBIT_SETP (RID_MUTABLE, specbits))
11047     {
11048       if (decl_context != FIELD || friendp)
11049         {
11050           error ("non-member `%s' cannot be declared `mutable'", name);
11051           RIDBIT_RESET (RID_MUTABLE, specbits);
11052         }
11053       else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11054         {
11055           error ("non-object member `%s' cannot be declared `mutable'", name);
11056           RIDBIT_RESET (RID_MUTABLE, specbits);
11057         }
11058       else if (TREE_CODE (type) == FUNCTION_TYPE
11059                || TREE_CODE (type) == METHOD_TYPE)
11060         {
11061           error ("function `%s' cannot be declared `mutable'", name);
11062           RIDBIT_RESET (RID_MUTABLE, specbits);
11063         }
11064       else if (staticp)
11065         {
11066           error ("static `%s' cannot be declared `mutable'", name);
11067           RIDBIT_RESET (RID_MUTABLE, specbits);
11068         }
11069       else if (type_quals & TYPE_QUAL_CONST)
11070         {
11071           error ("const `%s' cannot be declared `mutable'", name);
11072           RIDBIT_RESET (RID_MUTABLE, specbits);
11073         }
11074     }
11075
11076   if (declarator == NULL_TREE
11077       || TREE_CODE (declarator) == IDENTIFIER_NODE
11078       || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11079           && (TREE_CODE (type) == FUNCTION_TYPE
11080               || TREE_CODE (type) == METHOD_TYPE)))
11081     /* OK */;
11082   else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11083     {
11084       error ("template-id `%D' used as a declarator", declarator);
11085       declarator = dname;
11086     }
11087   else
11088     /* Unexpected declarator format.  */
11089     abort ();
11090
11091   /* If this is declaring a typedef name, return a TYPE_DECL.  */
11092
11093   if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11094     {
11095       tree decl;
11096
11097       /* Note that the grammar rejects storage classes
11098          in typenames, fields or parameters.  */
11099       if (current_lang_name == lang_name_java)
11100         TYPE_FOR_JAVA (type) = 1;
11101
11102       if (decl_context == FIELD)
11103         {
11104           if (constructor_name_p (declarator, current_class_type))
11105             pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11106                         declarator);
11107           decl = build_lang_decl (TYPE_DECL, declarator, type);
11108         }
11109       else
11110         {
11111           decl = build_decl (TYPE_DECL, declarator, type);
11112           if (in_namespace || ctype)
11113             cp_error_at ("typedef name may not be a nested-name-specifier",
11114                          decl);
11115           if (!current_function_decl)
11116             DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11117         }
11118       
11119       /* If the user declares "typedef struct {...} foo" then the
11120          struct will have an anonymous name.  Fill that name in now.
11121          Nothing can refer to it, so nothing needs know about the name
11122          change.  */
11123       if (type != error_mark_node
11124           && declarator
11125           && TYPE_NAME (type)
11126           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11127           && TYPE_ANONYMOUS_P (type)
11128           /* Don't do this if there are attributes.  */
11129           && (!attrlist || !*attrlist)
11130           && cp_type_quals (type) == TYPE_UNQUALIFIED)
11131         {
11132           tree oldname = TYPE_NAME (type);
11133           tree t;
11134
11135           /* Replace the anonymous name with the real name everywhere.  */
11136           lookup_tag_reverse (type, declarator);
11137           for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11138             if (TYPE_NAME (t) == oldname)
11139               TYPE_NAME (t) = decl;
11140
11141           if (TYPE_LANG_SPECIFIC (type))
11142             TYPE_WAS_ANONYMOUS (type) = 1;
11143
11144           /* If this is a typedef within a template class, the nested
11145              type is a (non-primary) template.  The name for the
11146              template needs updating as well.  */
11147           if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11148             DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11149               = TYPE_IDENTIFIER (type);
11150
11151           /* FIXME remangle member functions; member functions of a
11152              type with external linkage have external linkage.  */
11153         }
11154
11155       if (quals)
11156         {
11157           if (ctype == NULL_TREE)
11158             {
11159               if (TREE_CODE (type) != METHOD_TYPE)
11160                 cp_error_at ("invalid type qualifier for non-member function type", decl);
11161               else
11162                 ctype = TYPE_METHOD_BASETYPE (type);
11163             }
11164           if (ctype != NULL_TREE)
11165             grok_method_quals (ctype, decl, quals);
11166         }
11167
11168       if (RIDBIT_SETP (RID_SIGNED, specbits)
11169           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11170         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11171
11172       bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11173                       inlinep, friendp, raises != NULL_TREE);
11174
11175       return decl;
11176     }
11177
11178   /* Detect the case of an array type of unspecified size
11179      which came, as such, direct from a typedef name.
11180      We must copy the type, so that the array's domain can be
11181      individually set by the object's initializer.  */
11182
11183   if (type && typedef_type
11184       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11185       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11186     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11187
11188   /* Detect where we're using a typedef of function type to declare a
11189      function. last_function_parms will not be set, so we must create
11190      it now.  */
11191   
11192   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11193     {
11194       tree decls = NULL_TREE;
11195       tree args;
11196
11197       for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11198         {
11199           tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11200
11201           TREE_CHAIN (decl) = decls;
11202           decls = decl;
11203         }
11204       
11205       last_function_parms = nreverse (decls);
11206     }
11207
11208   /* If this is a type name (such as, in a cast or sizeof),
11209      compute the type and return it now.  */
11210
11211   if (decl_context == TYPENAME)
11212     {
11213       /* Note that the grammar rejects storage classes
11214          in typenames, fields or parameters.  */
11215       if (type_quals != TYPE_UNQUALIFIED)
11216         type_quals = TYPE_UNQUALIFIED;
11217
11218       /* Special case: "friend class foo" looks like a TYPENAME context.  */
11219       if (friendp)
11220         {
11221           if (type_quals != TYPE_UNQUALIFIED)
11222             {
11223               error ("type qualifiers specified for friend class declaration");
11224               type_quals = TYPE_UNQUALIFIED;
11225             }
11226           if (inlinep)
11227             {
11228               error ("`inline' specified for friend class declaration");
11229               inlinep = 0;
11230             }
11231
11232           if (!current_aggr)
11233             {
11234               /* Don't allow friend declaration without a class-key.  */
11235               if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11236                 pedwarn ("template parameters cannot be friends");
11237               else if (TREE_CODE (type) == TYPENAME_TYPE)
11238                 pedwarn ("friend declaration requires class-key, "
11239                          "i.e. `friend class %T::%D'",
11240                          TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11241               else
11242                 pedwarn ("friend declaration requires class-key, "
11243                          "i.e. `friend %#T'",
11244                          type);
11245             }
11246
11247           /* Only try to do this stuff if we didn't already give up.  */
11248           if (type != integer_type_node)
11249             {
11250               /* A friendly class?  */
11251               if (current_class_type)
11252                 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11253               else
11254                 error ("trying to make class `%T' a friend of global scope",
11255                           type);
11256
11257               type = void_type_node;
11258             }
11259         }
11260       else if (quals)
11261         {
11262           if (ctype == NULL_TREE)
11263             {
11264               if (TREE_CODE (type) != METHOD_TYPE)
11265                 error ("invalid qualifiers on non-member function type");
11266               else
11267                 ctype = TYPE_METHOD_BASETYPE (type);
11268             }
11269           if (ctype)
11270             {
11271               tree dummy = build_decl (TYPE_DECL, declarator, type);
11272               grok_method_quals (ctype, dummy, quals);
11273               type = TREE_TYPE (dummy);
11274             }
11275         }
11276
11277       return type;
11278     }
11279   else if (declarator == NULL_TREE && decl_context != PARM
11280            && decl_context != CATCHPARM
11281            && TREE_CODE (type) != UNION_TYPE
11282            && ! bitfield)
11283     {
11284       error ("abstract declarator `%T' used as declaration", type);
11285       declarator = make_anon_name ();
11286     }
11287
11288   /* `void' at top level (not within pointer)
11289      is allowed only in typedefs or type names.
11290      We don't complain about parms either, but that is because
11291      a better error message can be made later.  */
11292
11293   if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11294     {
11295       if (! declarator)
11296         error ("unnamed variable or field declared void");
11297       else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11298         {
11299           if (IDENTIFIER_OPNAME_P (declarator))
11300             abort ();
11301           else
11302             error ("variable or field `%s' declared void", name);
11303         }
11304       else
11305         error ("variable or field declared void");
11306       type = integer_type_node;
11307     }
11308
11309   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11310      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
11311
11312   if (decl_context == PARM || decl_context == CATCHPARM)
11313     {
11314       if (ctype || in_namespace)
11315         error ("cannot use `::' in parameter declaration");
11316
11317       /* A parameter declared as an array of T is really a pointer to T.
11318          One declared as a function is really a pointer to a function.
11319          One declared as a member is really a pointer to member.  */
11320
11321       if (TREE_CODE (type) == ARRAY_TYPE)
11322         {
11323           /* Transfer const-ness of array into that of type pointed to.  */
11324           type = build_pointer_type (TREE_TYPE (type));
11325           type_quals = TYPE_UNQUALIFIED;
11326         }
11327       else if (TREE_CODE (type) == FUNCTION_TYPE)
11328         type = build_pointer_type (type);
11329       else if (TREE_CODE (type) == OFFSET_TYPE)
11330         type = build_pointer_type (type);
11331     }
11332
11333   {
11334     register tree decl;
11335
11336     if (decl_context == PARM)
11337       {
11338         decl = cp_build_parm_decl (declarator, type);
11339
11340         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11341                         inlinep, friendp, raises != NULL_TREE);
11342       }
11343     else if (decl_context == FIELD)
11344       {
11345         if (type == error_mark_node)
11346           {
11347             /* Happens when declaring arrays of sizes which
11348                are error_mark_node, for example.  */
11349             decl = NULL_TREE;
11350           }
11351         else if (in_namespace && !friendp)
11352           {
11353             /* Something like struct S { int N::j; };  */
11354             error ("invalid use of `::'");
11355             decl = NULL_TREE;
11356           }
11357         else if (TREE_CODE (type) == FUNCTION_TYPE)
11358           {
11359             int publicp = 0;
11360             tree function_context;
11361
11362             /* We catch the others as conflicts with the builtin
11363                typedefs.  */
11364             if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11365               {
11366                 error ("function `%D' cannot be declared friend",
11367                           declarator);
11368                 friendp = 0;
11369               }
11370
11371             if (friendp == 0)
11372               {
11373                 if (ctype == NULL_TREE)
11374                   ctype = current_class_type;
11375
11376                 if (ctype == NULL_TREE)
11377                   {
11378                     error ("can't make `%D' into a method -- not in a class",
11379                               declarator);
11380                     return void_type_node;
11381                   }
11382
11383                 /* ``A union may [ ... ] not [ have ] virtual functions.''
11384                    ARM 9.5 */
11385                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11386                   {
11387                     error ("function `%D' declared virtual inside a union",
11388                               declarator);
11389                     return void_type_node;
11390                   }
11391
11392                 if (declarator == ansi_opname (NEW_EXPR)
11393                     || declarator == ansi_opname (VEC_NEW_EXPR)
11394                     || declarator == ansi_opname (DELETE_EXPR)
11395                     || declarator == ansi_opname (VEC_DELETE_EXPR))
11396                   {
11397                     if (virtualp)
11398                       {
11399                         error ("`%D' cannot be declared virtual, since it is always static",
11400                                   declarator);
11401                         virtualp = 0;
11402                       }
11403                   }
11404                 else if (staticp < 2)
11405                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11406                                                   TYPE_ARG_TYPES (type));
11407               }
11408
11409             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
11410             function_context = (ctype != NULL_TREE) ?
11411               decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11412             publicp = (! friendp || ! staticp)
11413               && function_context == NULL_TREE;
11414             decl = grokfndecl (ctype, type,
11415                                TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11416                                ? declarator : dname,
11417                                declarator,
11418                                virtualp, flags, quals, raises,
11419                                friendp ? -1 : 0, friendp, publicp, inlinep,
11420                                funcdef_flag, template_count, in_namespace);
11421             if (decl == NULL_TREE)
11422               return decl;
11423 #if 0
11424             /* This clobbers the attrs stored in `decl' from `attrlist'.  */
11425             /* The decl and setting of decl_attr is also turned off.  */
11426             decl = build_decl_attribute_variant (decl, decl_attr);
11427 #endif
11428
11429             /* [class.conv.ctor]
11430
11431                A constructor declared without the function-specifier
11432                explicit that can be called with a single parameter
11433                specifies a conversion from the type of its first
11434                parameter to the type of its class.  Such a constructor
11435                is called a converting constructor.  */
11436             if (explicitp == 2)
11437               DECL_NONCONVERTING_P (decl) = 1;
11438             else if (DECL_CONSTRUCTOR_P (decl))
11439               {
11440                 /* The constructor can be called with exactly one
11441                    parameter if there is at least one parameter, and
11442                    any subsequent parameters have default arguments.
11443                    Ignore any compiler-added parms.  */
11444                 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11445
11446                 if (arg_types == void_list_node
11447                     || (arg_types
11448                         && TREE_CHAIN (arg_types)
11449                         && TREE_CHAIN (arg_types) != void_list_node
11450                         && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11451                   DECL_NONCONVERTING_P (decl) = 1;
11452               }
11453           }
11454         else if (TREE_CODE (type) == METHOD_TYPE)
11455           {
11456             /* We only get here for friend declarations of
11457                members of other classes.  */
11458             /* All method decls are public, so tell grokfndecl to set
11459                TREE_PUBLIC, also.  */
11460             decl = grokfndecl (ctype, type, declarator, declarator,
11461                                virtualp, flags, quals, raises,
11462                                friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11463                                template_count, in_namespace);
11464             if (decl == NULL_TREE)
11465               return NULL_TREE;
11466           }
11467         else if (!staticp && ! processing_template_decl
11468                  && !COMPLETE_TYPE_P (complete_type (type))
11469                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11470           {
11471             if (declarator)
11472               error ("field `%D' has incomplete type", declarator);
11473             else
11474               error ("name `%T' has incomplete type", type);
11475
11476             /* If we're instantiating a template, tell them which
11477                instantiation made the field's type be incomplete.  */
11478             if (current_class_type
11479                 && TYPE_NAME (current_class_type)
11480                 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11481                 && declspecs && TREE_VALUE (declspecs)
11482                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11483               error ("  in instantiation of template `%T'",
11484                         current_class_type);
11485
11486             type = error_mark_node;
11487             decl = NULL_TREE;
11488           }
11489         else
11490           {
11491             if (friendp)
11492               {
11493                 error ("`%s' is neither function nor member function; cannot be declared friend",
11494                        IDENTIFIER_POINTER (declarator));
11495                 friendp = 0;
11496               }
11497             decl = NULL_TREE;
11498           }
11499
11500         if (friendp)
11501           {
11502             /* Friends are treated specially.  */
11503             if (ctype == current_class_type)
11504               warning ("member functions are implicitly friends of their class");
11505             else
11506               {
11507                 tree t = NULL_TREE;
11508                 if (decl && DECL_NAME (decl))
11509                   {
11510                     if (template_class_depth (current_class_type) == 0)
11511                       {
11512                         decl
11513                           = check_explicit_specialization
11514                           (declarator, decl,
11515                            template_count, 2 * (funcdef_flag != 0) + 4);
11516                         if (decl == error_mark_node)
11517                           return error_mark_node;
11518                       }
11519                     
11520                     t = do_friend (ctype, declarator, decl,
11521                                    last_function_parms, *attrlist,
11522                                    flags, quals, funcdef_flag);
11523                   }
11524                 if (t && funcdef_flag)
11525                   return t;
11526   
11527                 return void_type_node;
11528               }
11529           }
11530
11531         /* Structure field.  It may not be a function, except for C++ */
11532
11533         if (decl == NULL_TREE)
11534           {
11535             if (initialized)
11536               {
11537                 if (!staticp)
11538                   {
11539                     /* An attempt is being made to initialize a non-static
11540                        member.  But, from [class.mem]:
11541
11542                        4 A member-declarator can contain a
11543                        constant-initializer only if it declares a static
11544                        member (_class.static_) of integral or enumeration
11545                        type, see _class.static.data_.
11546
11547                        This used to be relatively common practice, but
11548                        the rest of the compiler does not correctly
11549                        handle the initialization unless the member is
11550                        static so we make it static below.  */
11551                     pedwarn ("ISO C++ forbids initialization of member `%D'",
11552                                 declarator);
11553                     pedwarn ("making `%D' static", declarator);
11554                     staticp = 1;
11555                   }
11556
11557                 if (uses_template_parms (type))
11558                   /* We'll check at instantiation time.  */
11559                   ;
11560                 else if (check_static_variable_definition (declarator,
11561                                                            type))
11562                   /* If we just return the declaration, crashes
11563                      will sometimes occur.  We therefore return
11564                      void_type_node, as if this was a friend
11565                      declaration, to cause callers to completely
11566                      ignore this declaration.  */
11567                   return void_type_node;
11568               }
11569
11570             if (staticp)
11571               {
11572                 /* [class.mem] forbids static data members with the
11573                    same name as the enclosing class.  Non-static data
11574                    members are checked in check_field_decls.  */
11575                 if (constructor_name_p (declarator, current_class_type))
11576                   pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11577                            declarator);
11578                   
11579                 /* C++ allows static class members.  All other work
11580                    for this is done by grokfield.  */
11581                 decl = build_lang_decl (VAR_DECL, declarator, type);
11582                 TREE_STATIC (decl) = 1;
11583                 /* In class context, 'static' means public access.  */
11584                 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11585               }
11586             else
11587               {
11588                 decl = build_decl (FIELD_DECL, declarator, type);
11589                 DECL_NONADDRESSABLE_P (decl) = bitfield;
11590                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11591                   {
11592                     DECL_MUTABLE_P (decl) = 1;
11593                     RIDBIT_RESET (RID_MUTABLE, specbits);
11594                   }
11595               }
11596
11597             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11598                             inlinep, friendp, raises != NULL_TREE);
11599           }
11600       }
11601     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11602       {
11603         tree original_name;
11604         int publicp = 0;
11605
11606         if (! declarator)
11607           return NULL_TREE;
11608
11609         if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11610           original_name = dname;
11611         else
11612           original_name = declarator;
11613
11614         if (RIDBIT_SETP (RID_AUTO, specbits))
11615           error ("storage class `auto' invalid for function `%s'", name);
11616         else if (RIDBIT_SETP (RID_REGISTER, specbits))
11617           error ("storage class `register' invalid for function `%s'", name);
11618         else if (RIDBIT_SETP (RID_THREAD, specbits))
11619           error ("storage class `__thread' invalid for function `%s'", name);
11620
11621         /* Function declaration not at top level.
11622            Storage classes other than `extern' are not allowed
11623            and `extern' makes no difference.  */
11624         if (! toplevel_bindings_p ()
11625             && (RIDBIT_SETP (RID_STATIC, specbits)
11626                 || RIDBIT_SETP (RID_INLINE, specbits))
11627             && pedantic)
11628           {
11629             if (RIDBIT_SETP (RID_STATIC, specbits))
11630               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11631             else
11632               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11633           }
11634
11635         if (ctype == NULL_TREE)
11636           {
11637             if (virtualp)
11638               {
11639                 error ("virtual non-class function `%s'", name);
11640                 virtualp = 0;
11641               }
11642           }
11643         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11644           type = build_cplus_method_type (ctype, TREE_TYPE (type),
11645                                           TYPE_ARG_TYPES (type));
11646
11647         /* Record presence of `static'.  */
11648         publicp = (ctype != NULL_TREE
11649                    || RIDBIT_SETP (RID_EXTERN, specbits)
11650                    || !RIDBIT_SETP (RID_STATIC, specbits));
11651
11652         decl = grokfndecl (ctype, type, original_name, declarator,
11653                            virtualp, flags, quals, raises,
11654                            1, friendp,
11655                            publicp, inlinep, funcdef_flag,
11656                            template_count, in_namespace);
11657         if (decl == NULL_TREE)
11658           return NULL_TREE;
11659
11660         if (staticp == 1)
11661           {
11662             int invalid_static = 0;
11663
11664             /* Don't allow a static member function in a class, and forbid
11665                declaring main to be static.  */
11666             if (TREE_CODE (type) == METHOD_TYPE)
11667               {
11668                 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11669                 invalid_static = 1;
11670               }
11671             else if (current_function_decl)
11672               {
11673                 /* FIXME need arm citation */
11674                 error ("cannot declare static function inside another function");
11675                 invalid_static = 1;
11676               }
11677
11678             if (invalid_static)
11679               {
11680                 staticp = 0;
11681                 RIDBIT_RESET (RID_STATIC, specbits);
11682               }
11683           }
11684       }
11685     else
11686       {
11687         /* It's a variable.  */
11688
11689         /* An uninitialized decl with `extern' is a reference.  */
11690         decl = grokvardecl (type, declarator, &specbits,
11691                             initialized,
11692                             (type_quals & TYPE_QUAL_CONST) != 0,
11693                             ctype ? ctype : in_namespace);
11694         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11695                         inlinep, friendp, raises != NULL_TREE);
11696
11697         if (ctype)
11698           {
11699             DECL_CONTEXT (decl) = ctype;
11700             if (staticp == 1)
11701               {
11702                 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11703                 staticp = 0;
11704                 RIDBIT_RESET (RID_STATIC, specbits);
11705               }
11706             if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11707               {
11708                 error ("static member `%D' declared `register'", decl);
11709                 RIDBIT_RESET (RID_REGISTER, specbits);
11710               }
11711             if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11712               {
11713                 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11714                             decl);
11715                 RIDBIT_RESET (RID_EXTERN, specbits);
11716               }
11717           }
11718       }
11719
11720     my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11721
11722     /* Record `register' declaration for warnings on &
11723        and in case doing stupid register allocation.  */
11724
11725     if (RIDBIT_SETP (RID_REGISTER, specbits))
11726       DECL_REGISTER (decl) = 1;
11727
11728     if (RIDBIT_SETP (RID_EXTERN, specbits))
11729       DECL_THIS_EXTERN (decl) = 1;
11730
11731     if (RIDBIT_SETP (RID_STATIC, specbits))
11732       DECL_THIS_STATIC (decl) = 1;
11733
11734     /* Record constancy and volatility.  There's no need to do this
11735        when processing a template; we'll do this for the instantiated
11736        declaration based on the type of DECL.  */
11737     if (!processing_template_decl)
11738       c_apply_type_quals_to_decl (type_quals, decl);
11739
11740     return decl;
11741   }
11742 }
11743 \f
11744 /* Subroutine of start_function.  Ensure that each of the parameter
11745    types (as listed in PARMS) is complete, as is required for a
11746    function definition.  */
11747
11748 static void
11749 require_complete_types_for_parms (tree parms)
11750 {
11751   for (; parms; parms = TREE_CHAIN (parms))
11752     {
11753       if (VOID_TYPE_P (TREE_TYPE (parms)))
11754         /* grokparms will have already issued an error */
11755         TREE_TYPE (parms) = error_mark_node;
11756       else if (complete_type_or_else (TREE_TYPE (parms), parms))
11757         {
11758           layout_decl (parms, 0);
11759           DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11760         }
11761       else
11762         TREE_TYPE (parms) = error_mark_node;
11763     }
11764 }
11765
11766 /* Returns nonzero if T is a local variable.  */
11767
11768 int
11769 local_variable_p (tree t)
11770 {
11771   if ((TREE_CODE (t) == VAR_DECL
11772        /* A VAR_DECL with a context that is a _TYPE is a static data
11773           member.  */
11774        && !TYPE_P (CP_DECL_CONTEXT (t))
11775        /* Any other non-local variable must be at namespace scope.  */
11776        && !DECL_NAMESPACE_SCOPE_P (t))
11777       || (TREE_CODE (t) == PARM_DECL))
11778     return 1;
11779
11780   return 0;
11781 }
11782
11783 /* Returns nonzero if T is an automatic local variable or a label.
11784    (These are the declarations that need to be remapped when the code
11785    containing them is duplicated.)  */
11786
11787 int
11788 nonstatic_local_decl_p (tree t)
11789 {
11790   return ((local_variable_p (t) && !TREE_STATIC (t))
11791           || TREE_CODE (t) == LABEL_DECL
11792           || TREE_CODE (t) == RESULT_DECL);
11793 }
11794
11795 /* Like local_variable_p, but suitable for use as a tree-walking
11796    function.  */
11797
11798 static tree
11799 local_variable_p_walkfn (tree* tp,
11800                          int* walk_subtrees ATTRIBUTE_UNUSED ,
11801                          void* data ATTRIBUTE_UNUSED )
11802 {
11803   return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11804           ? *tp : NULL_TREE);
11805 }
11806
11807 /* Check that ARG, which is a default-argument expression for a
11808    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
11809    something goes wrong.  DECL may also be a _TYPE node, rather than a
11810    DECL, if there is no DECL available.  */
11811
11812 tree
11813 check_default_argument (tree decl, tree arg)
11814 {
11815   tree var;
11816   tree decl_type;
11817
11818   if (TREE_CODE (arg) == DEFAULT_ARG)
11819     /* We get a DEFAULT_ARG when looking at an in-class declaration
11820        with a default argument.  Ignore the argument for now; we'll
11821        deal with it after the class is complete.  */
11822     return arg;
11823
11824   if (processing_template_decl || uses_template_parms (arg))
11825     /* We don't do anything checking until instantiation-time.  Note
11826        that there may be uninstantiated arguments even for an
11827        instantiated function, since default arguments are not
11828        instantiated until they are needed.  */
11829     return arg;
11830
11831   if (TYPE_P (decl))
11832     {
11833       decl_type = decl;
11834       decl = NULL_TREE;
11835     }
11836   else
11837     decl_type = TREE_TYPE (decl);
11838
11839   if (arg == error_mark_node
11840       || decl == error_mark_node
11841       || TREE_TYPE (arg) == error_mark_node
11842       || decl_type == error_mark_node)
11843     /* Something already went wrong.  There's no need to check
11844        further.  */
11845     return error_mark_node;
11846
11847   /* [dcl.fct.default]
11848
11849      A default argument expression is implicitly converted to the
11850      parameter type.  */
11851   if (!TREE_TYPE (arg)
11852       || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11853     {
11854       if (decl)
11855         error ("default argument for `%#D' has type `%T'",
11856                   decl, TREE_TYPE (arg));
11857       else
11858         error ("default argument for parameter of type `%T' has type `%T'",
11859                   decl_type, TREE_TYPE (arg));
11860
11861       return error_mark_node;
11862     }
11863
11864   /* [dcl.fct.default]
11865
11866      Local variables shall not be used in default argument
11867      expressions.
11868
11869      The keyword `this' shall not be used in a default argument of a
11870      member function.  */
11871   var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11872                                       NULL);
11873   if (var)
11874     {
11875       error ("default argument `%E' uses local variable `%D'",
11876                 arg, var);
11877       return error_mark_node;
11878     }
11879
11880   /* All is well.  */
11881   return arg;
11882 }
11883
11884 /* Decode the list of parameter types for a function type.
11885    Given the list of things declared inside the parens,
11886    return a list of types.
11887
11888    We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11889    flag. If unset, we append void_list_node. A parmlist declared
11890    as `(void)' is accepted as the empty parmlist.
11891
11892    Also set last_function_parms to the chain of PARM_DECLs.  */
11893
11894 static tree
11895 grokparms (tree first_parm)
11896 {
11897   tree result = NULL_TREE;
11898   tree decls = NULL_TREE;
11899   int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11900   tree parm, chain;
11901   int any_error = 0;
11902
11903   my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11904
11905   for (parm = first_parm; parm != NULL_TREE; parm = chain)
11906     {
11907       tree type = NULL_TREE;
11908       tree decl = TREE_VALUE (parm);
11909       tree init = TREE_PURPOSE (parm);
11910       tree specs, attrs;
11911
11912       chain = TREE_CHAIN (parm);
11913       /* @@ weak defense against parse errors.  */
11914       if (TREE_CODE (decl) != VOID_TYPE
11915           && TREE_CODE (decl) != TREE_LIST)
11916         {
11917           /* Give various messages as the need arises.  */
11918           if (TREE_CODE (decl) == STRING_CST)
11919             error ("invalid string constant `%E'", decl);
11920           else if (TREE_CODE (decl) == INTEGER_CST)
11921             error ("invalid integer constant in parameter list, did you forget to give parameter name?");
11922           continue;
11923         }
11924
11925       if (parm == void_list_node)
11926         break;
11927
11928       split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
11929       decl = grokdeclarator (TREE_VALUE (decl), specs,
11930                              PARM, init != NULL_TREE, &attrs);
11931       if (! decl || TREE_TYPE (decl) == error_mark_node)
11932         continue;
11933
11934       if (attrs)
11935         cplus_decl_attributes (&decl, attrs, 0);
11936
11937       type = TREE_TYPE (decl);
11938       if (VOID_TYPE_P (type))
11939         {
11940           if (same_type_p (type, void_type_node)
11941               && !DECL_NAME (decl) && !result && !chain && !ellipsis)
11942             /* this is a parmlist of `(void)', which is ok.  */
11943             break;
11944           cxx_incomplete_type_error (decl, type);
11945           /* It's not a good idea to actually create parameters of
11946              type `void'; other parts of the compiler assume that a
11947              void type terminates the parameter list.  */
11948           type = error_mark_node;
11949           TREE_TYPE (decl) = error_mark_node;
11950         }
11951
11952       if (type != error_mark_node)
11953         {
11954           /* Top-level qualifiers on the parameters are
11955              ignored for function types.  */
11956           type = TYPE_MAIN_VARIANT (type);
11957           if (TREE_CODE (type) == METHOD_TYPE)
11958             {
11959               error ("parameter `%D' invalidly declared method type", decl);
11960               type = build_pointer_type (type);
11961               TREE_TYPE (decl) = type;
11962             }
11963           else if (TREE_CODE (type) == OFFSET_TYPE)
11964             {
11965               error ("parameter `%D' invalidly declared offset type", decl);
11966               type = build_pointer_type (type);
11967               TREE_TYPE (decl) = type;
11968             }
11969           else if (abstract_virtuals_error (decl, type))
11970             any_error = 1;  /* Seems like a good idea.  */
11971           else if (POINTER_TYPE_P (type))
11972             {
11973               /* [dcl.fct]/6, parameter types cannot contain pointers
11974                  (references) to arrays of unknown bound.  */
11975               tree t = TREE_TYPE (type);
11976               int ptr = TYPE_PTR_P (type);
11977
11978               while (1)
11979                 {
11980                   if (TYPE_PTR_P (t))
11981                     ptr = 1;
11982                   else if (TREE_CODE (t) != ARRAY_TYPE)
11983                     break;
11984                   else if (!TYPE_DOMAIN (t))
11985                     break;
11986                   t = TREE_TYPE (t);
11987                 }
11988               if (TREE_CODE (t) == ARRAY_TYPE)
11989                 error ("parameter `%D' includes %s to array of unknown bound `%T'",
11990                           decl, ptr ? "pointer" : "reference", t);
11991             }
11992
11993           if (!any_error && init)
11994             init = check_default_argument (decl, init);
11995           else
11996             init = NULL_TREE;
11997         }
11998
11999       TREE_CHAIN (decl) = decls;
12000       decls = decl;
12001       result = tree_cons (init, type, result);
12002     }
12003   decls = nreverse (decls);
12004   result = nreverse (result);
12005   if (!ellipsis)
12006     result = chainon (result, void_list_node);
12007   last_function_parms = decls;
12008
12009   return result;
12010 }
12011
12012 \f
12013 /* D is a constructor or overloaded `operator='.
12014
12015    Let T be the class in which D is declared. Then, this function
12016    returns:
12017
12018    -1 if D's is an ill-formed constructor or copy assignment operator
12019       whose first parameter is of type `T'.
12020    0  if D is not a copy constructor or copy assignment
12021       operator.
12022    1  if D is a copy constructor or copy assignment operator whose
12023       first parameter is a reference to const qualified T.
12024    2  if D is a copy constructor or copy assignment operator whose
12025       first parameter is a reference to non-const qualified T.
12026
12027    This function can be used as a predicate. Positive values indicate
12028    a copy constructor and nonzero values indicate a copy assignment
12029    operator.  */
12030
12031 int
12032 copy_fn_p (tree d)
12033 {
12034   tree args;
12035   tree arg_type;
12036   int result = 1;
12037   
12038   my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12039
12040   if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12041     /* Instantiations of template member functions are never copy
12042        functions.  Note that member functions of templated classes are
12043        represented as template functions internally, and we must
12044        accept those as copy functions.  */
12045     return 0;
12046     
12047   args = FUNCTION_FIRST_USER_PARMTYPE (d);
12048   if (!args)
12049     return 0;
12050
12051   arg_type = TREE_VALUE (args);
12052
12053   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12054     {
12055       /* Pass by value copy assignment operator.  */
12056       result = -1;
12057     }
12058   else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12059            && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12060     {
12061       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12062         result = 2;
12063     }
12064   else
12065     return 0;
12066   
12067   args = TREE_CHAIN (args);
12068
12069   if (args && args != void_list_node && !TREE_PURPOSE (args))
12070     /* There are more non-optional args.  */
12071     return 0;
12072
12073   return result;
12074 }
12075
12076 /* Remember any special properties of member function DECL.  */
12077
12078 void grok_special_member_properties (tree decl)
12079 {
12080   if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12081     ; /* Not special.  */
12082   else if (DECL_CONSTRUCTOR_P (decl))
12083     {
12084       int ctor = copy_fn_p (decl);
12085       
12086       if (ctor > 0)
12087         {
12088           /* [class.copy]
12089               
12090              A non-template constructor for class X is a copy
12091              constructor if its first parameter is of type X&, const
12092              X&, volatile X& or const volatile X&, and either there
12093              are no other parameters or else all other parameters have
12094              default arguments.  */
12095           TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12096           if (ctor > 1)
12097             TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12098         }
12099       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12100         TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12101     }
12102   else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12103     {
12104       /* [class.copy]
12105           
12106          A non-template assignment operator for class X is a copy
12107          assignment operator if its parameter is of type X, X&, const
12108          X&, volatile X& or const volatile X&.  */
12109       
12110       int assop = copy_fn_p (decl);
12111       
12112       if (assop)
12113         {
12114           TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12115           if (assop != 1)
12116             TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12117           if (DECL_PURE_VIRTUAL_P (decl))
12118             TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12119         }
12120     }
12121 }
12122
12123 /* Check a constructor DECL has the correct form.  Complains
12124    if the class has a constructor of the form X(X).  */
12125
12126 int
12127 grok_ctor_properties (tree ctype, tree decl)
12128 {
12129   int ctor_parm = copy_fn_p (decl);
12130
12131   if (ctor_parm < 0)
12132     {
12133       /* [class.copy]
12134           
12135          A declaration of a constructor for a class X is ill-formed if
12136          its first parameter is of type (optionally cv-qualified) X
12137          and either there are no other parameters or else all other
12138          parameters have default arguments.
12139           
12140          We *don't* complain about member template instantiations that
12141          have this form, though; they can occur as we try to decide
12142          what constructor to use during overload resolution.  Since
12143          overload resolution will never prefer such a constructor to
12144          the non-template copy constructor (which is either explicitly
12145          or implicitly defined), there's no need to worry about their
12146          existence.  Theoretically, they should never even be
12147          instantiated, but that's hard to forestall.  */
12148       error ("invalid constructor; you probably meant `%T (const %T&)'",
12149                 ctype, ctype);
12150       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12151       return 0;
12152     }
12153   
12154   return 1;
12155 }
12156
12157 /* An operator with this code is unary, but can also be binary.  */
12158
12159 static int
12160 ambi_op_p (enum tree_code code)
12161 {
12162   return (code == INDIRECT_REF
12163           || code == ADDR_EXPR
12164           || code == CONVERT_EXPR
12165           || code == NEGATE_EXPR
12166           || code == PREINCREMENT_EXPR
12167           || code == PREDECREMENT_EXPR);
12168 }
12169
12170 /* An operator with this name can only be unary.  */
12171
12172 static int
12173 unary_op_p (enum tree_code code)
12174 {
12175   return (code == TRUTH_NOT_EXPR
12176           || code == BIT_NOT_EXPR
12177           || code == COMPONENT_REF
12178           || code == TYPE_EXPR);
12179 }
12180
12181 /* Do a little sanity-checking on how they declared their operator.  */
12182
12183 void
12184 grok_op_properties (tree decl, int friendp)
12185 {
12186   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12187   tree argtype;
12188   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12189   tree name = DECL_NAME (decl);
12190   enum tree_code operator_code;
12191   int arity;
12192
12193   /* Count the number of arguments.  */
12194   for (argtype = argtypes, arity = 0;
12195        argtype && argtype != void_list_node;
12196        argtype = TREE_CHAIN (argtype))
12197     ++arity;
12198
12199   if (current_class_type == NULL_TREE)
12200     friendp = 1;
12201
12202   if (DECL_CONV_FN_P (decl))
12203     operator_code = TYPE_EXPR;
12204   else
12205     do
12206       {
12207 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)       \
12208         if (ansi_opname (CODE) == name)                         \
12209           {                                                     \
12210             operator_code = (CODE);                             \
12211             break;                                              \
12212           }                                                     \
12213         else if (ansi_assopname (CODE) == name)                 \
12214           {                                                     \
12215             operator_code = (CODE);                             \
12216             DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;              \
12217             break;                                              \
12218           }
12219
12220 #include "operators.def"
12221 #undef DEF_OPERATOR
12222
12223         abort ();
12224       }
12225     while (0);
12226   my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12227   SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12228
12229   if (! friendp)
12230     {
12231       switch (operator_code)
12232         {
12233         case CALL_EXPR:
12234           TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12235           break;
12236
12237         case ARRAY_REF:
12238           TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12239           break;
12240
12241         case COMPONENT_REF:
12242         case MEMBER_REF:
12243           TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12244           break;
12245
12246         case NEW_EXPR:
12247           TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12248           break;
12249
12250         case DELETE_EXPR:
12251           TYPE_GETS_DELETE (current_class_type) |= 1;
12252           break;
12253
12254         case VEC_NEW_EXPR:
12255           TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12256           break;
12257
12258         case VEC_DELETE_EXPR:
12259           TYPE_GETS_DELETE (current_class_type) |= 2;
12260           break;
12261
12262         default:
12263           break;
12264         }
12265     }
12266
12267   if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12268     {
12269       /* When the compiler encounters the definition of A::operator new, it
12270          doesn't look at the class declaration to find out if it's static.  */
12271       if (methodp)
12272         revert_static_member_fn (decl);
12273
12274       TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12275     }
12276   else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12277     {
12278       if (methodp)
12279         revert_static_member_fn (decl);
12280
12281       TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12282     }
12283   else
12284     {
12285       /* An operator function must either be a non-static member function
12286          or have at least one parameter of a class, a reference to a class,
12287          an enumeration, or a reference to an enumeration.  13.4.0.6 */
12288       if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12289         {
12290           if (operator_code == TYPE_EXPR
12291               || operator_code == CALL_EXPR
12292               || operator_code == COMPONENT_REF
12293               || operator_code == ARRAY_REF
12294               || operator_code == NOP_EXPR)
12295             error ("`%D' must be a nonstatic member function", decl);
12296           else
12297             {
12298               tree p = argtypes;
12299
12300               if (DECL_STATIC_FUNCTION_P (decl))
12301                 error ("`%D' must be either a non-static member function or a non-member function", decl);
12302
12303               if (p)
12304                 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12305                   {
12306                     tree arg = TREE_VALUE (p);
12307                     if (TREE_CODE (arg) == REFERENCE_TYPE)
12308                       arg = TREE_TYPE (arg);
12309
12310                     /* This lets bad template code slip through.  */
12311                     if (IS_AGGR_TYPE (arg)
12312                         || TREE_CODE (arg) == ENUMERAL_TYPE
12313                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12314                         || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12315                       goto foundaggr;
12316                   }
12317               error
12318                 ("`%D' must have an argument of class or enumerated type",
12319                  decl);
12320             foundaggr:
12321               ;
12322             }
12323         }
12324
12325       if (operator_code == CALL_EXPR)
12326         return;                 /* No restrictions on args.  */
12327
12328       if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12329         {
12330           tree t = TREE_TYPE (name);
12331           if (! friendp)
12332             {
12333               int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12334               const char *what = 0;
12335
12336               if (ref)
12337                 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12338
12339               if (TREE_CODE (t) == VOID_TYPE)
12340                 what = "void";
12341               else if (t == current_class_type)
12342                 what = "the same type";
12343               /* Don't force t to be complete here.  */
12344               else if (IS_AGGR_TYPE (t)
12345                        && COMPLETE_TYPE_P (t)
12346                        && DERIVED_FROM_P (t, current_class_type))
12347                 what = "a base class";
12348
12349               if (what)
12350                 warning ("conversion to %s%s will never use a type conversion operator",
12351                          ref ? "a reference to " : "", what);
12352             }
12353         }
12354       if (operator_code == COND_EXPR)
12355         {
12356           /* 13.4.0.3 */
12357           error ("ISO C++ prohibits overloading operator ?:");
12358         }
12359       else if (ambi_op_p (operator_code))
12360         {
12361           if (arity == 1)
12362             /* We pick the one-argument operator codes by default, so
12363                we don't have to change anything.  */
12364             ;
12365           else if (arity == 2)
12366             {
12367               /* If we thought this was a unary operator, we now know
12368                  it to be a binary operator.  */
12369               switch (operator_code)
12370                 {
12371                 case INDIRECT_REF:
12372                   operator_code = MULT_EXPR;
12373                   break;
12374
12375                 case ADDR_EXPR:
12376                   operator_code = BIT_AND_EXPR;
12377                   break;
12378
12379                 case CONVERT_EXPR:
12380                   operator_code = PLUS_EXPR;
12381                   break;
12382
12383                 case NEGATE_EXPR:
12384                   operator_code = MINUS_EXPR;
12385                   break;
12386
12387                 case PREINCREMENT_EXPR:
12388                   operator_code = POSTINCREMENT_EXPR;
12389                   break;
12390
12391                 case PREDECREMENT_EXPR:
12392                   operator_code = POSTDECREMENT_EXPR;
12393                   break;
12394
12395                 default:
12396                   abort ();
12397                 }
12398
12399               SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12400
12401               if ((operator_code == POSTINCREMENT_EXPR
12402                    || operator_code == POSTDECREMENT_EXPR)
12403                   && ! processing_template_decl
12404                   && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12405                 {
12406                   if (methodp)
12407                     error ("postfix `%D' must take `int' as its argument",
12408                               decl);
12409                   else
12410                     error
12411                       ("postfix `%D' must take `int' as its second argument",
12412                        decl);
12413                 }
12414             }
12415           else
12416             {
12417               if (methodp)
12418                 error ("`%D' must take either zero or one argument", decl);
12419               else
12420                 error ("`%D' must take either one or two arguments", decl);
12421             }
12422
12423           /* More Effective C++ rule 6.  */
12424           if (warn_ecpp
12425               && (operator_code == POSTINCREMENT_EXPR
12426                   || operator_code == POSTDECREMENT_EXPR
12427                   || operator_code == PREINCREMENT_EXPR
12428                   || operator_code == PREDECREMENT_EXPR))
12429             {
12430               tree arg = TREE_VALUE (argtypes);
12431               tree ret = TREE_TYPE (TREE_TYPE (decl));
12432               if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12433                 arg = TREE_TYPE (arg);
12434               arg = TYPE_MAIN_VARIANT (arg);
12435               if (operator_code == PREINCREMENT_EXPR
12436                   || operator_code == PREDECREMENT_EXPR)
12437                 {
12438                   if (TREE_CODE (ret) != REFERENCE_TYPE
12439                       || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12440                                        arg))
12441                     warning ("prefix `%D' should return `%T'", decl,
12442                                 build_reference_type (arg));
12443                 }
12444               else
12445                 {
12446                   if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12447                     warning ("postfix `%D' should return `%T'", decl, arg);
12448                 }
12449             }
12450         }
12451       else if (unary_op_p (operator_code))
12452         {
12453           if (arity != 1)
12454             {
12455               if (methodp)
12456                 error ("`%D' must take `void'", decl);
12457               else
12458                 error ("`%D' must take exactly one argument", decl);
12459             }
12460         }
12461       else /* if (binary_op_p (operator_code)) */
12462         {
12463           if (arity != 2)
12464             {
12465               if (methodp)
12466                 error ("`%D' must take exactly one argument", decl);
12467               else
12468                 error ("`%D' must take exactly two arguments", decl);
12469             }
12470
12471           /* More Effective C++ rule 7.  */
12472           if (warn_ecpp
12473               && (operator_code == TRUTH_ANDIF_EXPR
12474                   || operator_code == TRUTH_ORIF_EXPR
12475                   || operator_code == COMPOUND_EXPR))
12476             warning ("user-defined `%D' always evaluates both arguments",
12477                         decl);
12478         }
12479
12480       /* Effective C++ rule 23.  */
12481       if (warn_ecpp
12482           && arity == 2
12483           && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12484           && (operator_code == PLUS_EXPR
12485               || operator_code == MINUS_EXPR
12486               || operator_code == TRUNC_DIV_EXPR
12487               || operator_code == MULT_EXPR
12488               || operator_code == TRUNC_MOD_EXPR)
12489           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12490         warning ("`%D' should return by value", decl);
12491
12492       /* [over.oper]/8 */
12493       for (; argtypes && argtypes != void_list_node;
12494           argtypes = TREE_CHAIN (argtypes))
12495         if (TREE_PURPOSE (argtypes))
12496           {
12497             TREE_PURPOSE (argtypes) = NULL_TREE;
12498             if (operator_code == POSTINCREMENT_EXPR
12499                 || operator_code == POSTDECREMENT_EXPR)
12500               {
12501                 if (pedantic)
12502                   pedwarn ("`%D' cannot have default arguments", decl);
12503               }
12504             else
12505               error ("`%D' cannot have default arguments", decl);
12506           }
12507
12508     }
12509 }
12510 \f
12511 static const char *
12512 tag_name (enum tag_types code)
12513 {
12514   switch (code)
12515     {
12516     case record_type:
12517       return "struct";
12518     case class_type:
12519       return "class";
12520     case union_type:
12521       return "union ";
12522     case enum_type:
12523       return "enum";
12524     default:
12525       abort ();
12526     }
12527 }
12528
12529 /* Name lookup in an elaborated-type-specifier (after the keyword
12530    indicated by TAG_CODE) has found TYPE.  If the
12531    elaborated-type-specifier is invalid, issue a diagnostic and return
12532    error_mark_node; otherwise, return TYPE itself.  */
12533
12534 static tree
12535 check_elaborated_type_specifier (enum tag_types tag_code,
12536                                  tree type)
12537 {
12538   tree t;
12539
12540   t = follow_tag_typedef (type);
12541
12542   /* [dcl.type.elab] If the identifier resolves to a typedef-name or a
12543      template type-parameter, the elaborated-type-specifier is
12544      ill-formed.  */
12545   if (!t)
12546     {
12547       error ("using typedef-name `%D' after `%s'",
12548              TYPE_NAME (type), tag_name (tag_code));
12549       t = error_mark_node;
12550     }
12551   else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
12552     {
12553       error ("using template type parameter `%T' after `%s'",
12554              type, tag_name (tag_code));
12555       t = error_mark_node;
12556     }
12557
12558   return t;
12559 }
12560
12561 /* Get the struct, enum or union (CODE says which) with tag NAME.
12562    Define the tag as a forward-reference if it is not defined.
12563
12564    C++: If a class derivation is given, process it here, and report
12565    an error if multiple derivation declarations are not identical.
12566
12567    If this is a definition, come in through xref_tag and only look in
12568    the current frame for the name (since C++ allows new names in any
12569    scope.)  */
12570
12571 tree
12572 xref_tag (enum tag_types tag_code, tree name, tree attributes, 
12573           bool globalize)
12574 {
12575   enum tree_code code;
12576   register tree ref, t;
12577   struct cp_binding_level *b = current_binding_level;
12578   tree context = NULL_TREE;
12579
12580   timevar_push (TV_NAME_LOOKUP);
12581   switch (tag_code)
12582     {
12583     case record_type:
12584     case class_type:
12585       code = RECORD_TYPE;
12586       break;
12587     case union_type:
12588       code = UNION_TYPE;
12589       break;
12590     case enum_type:
12591       code = ENUMERAL_TYPE;
12592       break;
12593     default:
12594       abort ();
12595     }
12596
12597   /* If a cross reference is requested, look up the type
12598      already defined for this tag and return it.  */
12599   if (TYPE_P (name))
12600     {
12601       t = name;
12602       name = TYPE_IDENTIFIER (t);
12603     }
12604   else
12605     t = IDENTIFIER_TYPE_VALUE (name);
12606
12607   /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
12608   if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12609     {
12610       static int explained;
12611       tree shadowed;
12612
12613       warning ("`%s %T' declares a new type at namespace scope",
12614                   tag_name (tag_code), name);
12615       if (!explained++)
12616         warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12617                     tag_name (tag_code),
12618                     constructor_name (current_class_type),
12619                     TYPE_IDENTIFIER (t));
12620
12621       /* We need to remove the class scope binding for the
12622          TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
12623       for (shadowed = b->class_shadowed;
12624            shadowed;
12625            shadowed = TREE_CHAIN (shadowed))
12626         if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12627           {
12628             TREE_PURPOSE (shadowed) = NULL_TREE;
12629             break;
12630           }
12631     }
12632
12633   if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12634       && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12635     t = NULL_TREE;
12636
12637   if (! globalize)
12638     {
12639       /* If we know we are defining this tag, only look it up in
12640          this scope and don't try to find it as a type.  */
12641       ref = lookup_tag (code, name, b, 1);
12642     }
12643   else
12644     {
12645       if (t)
12646         {
12647           ref = check_elaborated_type_specifier (tag_code, t);
12648           if (ref == error_mark_node)
12649             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12650         }
12651       else
12652         ref = lookup_tag (code, name, b, 0);
12653
12654       if (! ref)
12655         {
12656           /* Try finding it as a type declaration.  If that wins,
12657              use it.  */
12658           ref = lookup_name (name, 1);
12659
12660           if (ref != NULL_TREE
12661               && processing_template_decl
12662               && DECL_CLASS_TEMPLATE_P (ref)
12663               && template_class_depth (current_class_type) == 0)
12664             /* Since GLOBALIZE is true, we're declaring a global
12665                template, so we want this type.  */
12666             ref = DECL_TEMPLATE_RESULT (ref);
12667
12668           if (ref && TREE_CODE (ref) == TYPE_DECL)
12669             {
12670               ref = check_elaborated_type_specifier (tag_code, 
12671                                                      TREE_TYPE (ref));
12672               if (ref == error_mark_node)
12673                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
12674               if (ref && TREE_CODE (ref) != code)
12675                 ref = NULL_TREE;
12676             }
12677           else
12678             ref = NULL_TREE;
12679         }
12680
12681       if (ref && current_class_type
12682           && template_class_depth (current_class_type)
12683           && PROCESSING_REAL_TEMPLATE_DECL_P ())
12684         {
12685           /* Since GLOBALIZE is nonzero, we are not looking at a
12686              definition of this tag.  Since, in addition, we are currently
12687              processing a (member) template declaration of a template
12688              class, we must be very careful; consider:
12689
12690                template <class X>
12691                struct S1
12692
12693                template <class U>
12694                struct S2
12695                { template <class V>
12696                friend struct S1; };
12697
12698              Here, the S2::S1 declaration should not be confused with the
12699              outer declaration.  In particular, the inner version should
12700              have a template parameter of level 2, not level 1.  This
12701              would be particularly important if the member declaration
12702              were instead:
12703
12704                template <class V = U> friend struct S1;
12705
12706              say, when we should tsubst into `U' when instantiating
12707              S2.  On the other hand, when presented with:
12708
12709                  template <class T>
12710                  struct S1 {
12711                    template <class U>
12712                    struct S2 {};
12713                    template <class U>
12714                    friend struct S2;
12715                  };
12716
12717               we must find the inner binding eventually.  We
12718               accomplish this by making sure that the new type we
12719               create to represent this declaration has the right
12720               TYPE_CONTEXT.  */
12721           context = TYPE_CONTEXT (ref);
12722           ref = NULL_TREE;
12723         }
12724     }
12725
12726   if (! ref)
12727     {
12728       /* If no such tag is yet defined, create a forward-reference node
12729          and record it as the "definition".
12730          When a real declaration of this type is found,
12731          the forward-reference will be altered into a real type.  */
12732       if (code == ENUMERAL_TYPE)
12733         {
12734           error ("use of enum `%#D' without previous declaration", name);
12735
12736           ref = make_node (ENUMERAL_TYPE);
12737
12738           /* Give the type a default layout like unsigned int
12739              to avoid crashing if it does not get defined.  */
12740           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12741           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12742           TYPE_USER_ALIGN (ref) = 0;
12743           TREE_UNSIGNED (ref) = 1;
12744           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12745           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12746           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12747
12748           /* Enable us to recognize when a type is created in class context.
12749              To do nested classes correctly, this should probably be cleared
12750              out when we leave this classes scope.  Currently this in only
12751              done in `start_enum'.  */
12752
12753           pushtag (name, ref, globalize);
12754         }
12755       else
12756         {
12757           struct cp_binding_level *old_b = class_binding_level;
12758
12759           ref = make_aggr_type (code);
12760           TYPE_CONTEXT (ref) = context;
12761           pushtag (name, ref, globalize);
12762           class_binding_level = old_b;
12763         }
12764     }
12765   else
12766     {
12767       if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12768         redeclare_class_template (ref, current_template_parms);
12769     }
12770
12771   TYPE_ATTRIBUTES (ref) = attributes;
12772
12773   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12774 }
12775
12776 tree
12777 xref_tag_from_type (tree old, tree id, int globalize)
12778 {
12779   enum tag_types tag_kind;
12780
12781   if (TREE_CODE (old) == RECORD_TYPE)
12782     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12783   else
12784     tag_kind  = union_type;
12785
12786   if (id == NULL_TREE)
12787     id = TYPE_IDENTIFIER (old);
12788
12789   return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12790 }
12791
12792 /* REF is a type (named NAME), for which we have just seen some
12793    baseclasses.  BASE_LIST is a list of those baseclasses; the
12794    TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12795    the base-class.  TREE_VIA_VIRTUAL indicates virtual
12796    inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12797    struct, or union.  */
12798
12799 void
12800 xref_basetypes (tree ref, tree base_list)
12801 {
12802   /* In the declaration `A : X, Y, ... Z' we mark all the types
12803      (A, X, Y, ..., Z) so we can check for duplicates.  */
12804   tree *basep;
12805
12806   int i;
12807   enum tag_types tag_code;
12808
12809   if (TREE_CODE (ref) == UNION_TYPE)
12810     {
12811       error ("derived union `%T' invalid", ref);
12812       return;
12813     }
12814
12815   tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12816
12817   /* First, make sure that any templates in base-classes are
12818      instantiated.  This ensures that if we call ourselves recursively
12819      we do not get confused about which classes are marked and which
12820      are not.  */
12821   basep = &base_list; 
12822   while (*basep) 
12823     {
12824       tree basetype = TREE_VALUE (*basep);
12825       if (!(processing_template_decl && uses_template_parms (basetype))
12826           && !complete_type_or_else (basetype, NULL))
12827         /* An incomplete type.  Remove it from the list.  */
12828         *basep = TREE_CHAIN (*basep);
12829       else
12830         basep = &TREE_CHAIN (*basep);
12831     }
12832
12833   SET_CLASSTYPE_MARKED (ref);
12834   i = list_length (base_list);
12835   if (i)
12836     {
12837       tree binfo = TYPE_BINFO (ref);
12838       tree binfos = make_tree_vec (i);
12839       tree accesses = make_tree_vec (i);
12840       
12841       BINFO_BASETYPES (binfo) = binfos;
12842       BINFO_BASEACCESSES (binfo) = accesses;
12843   
12844       for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12845         {
12846           tree access = TREE_PURPOSE (base_list);
12847           int via_virtual = TREE_VIA_VIRTUAL (base_list);
12848           tree basetype = TREE_VALUE (base_list);
12849           tree base_binfo;
12850           
12851           if (access == access_default_node)
12852             /* The base of a derived struct is public by default.  */
12853             access = (tag_code == class_type
12854                       ? access_private_node : access_public_node);
12855           
12856           if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12857             basetype = TREE_TYPE (basetype);
12858           if (!basetype
12859               || (TREE_CODE (basetype) != RECORD_TYPE
12860                   && TREE_CODE (basetype) != TYPENAME_TYPE
12861                   && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12862                   && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12863             {
12864               error ("base type `%T' fails to be a struct or class type",
12865                      basetype);
12866               continue;
12867             }
12868           
12869           if (CLASSTYPE_MARKED (basetype))
12870             {
12871               if (basetype == ref)
12872                 error ("recursive type `%T' undefined", basetype);
12873               else
12874                 error ("duplicate base type `%T' invalid", basetype);
12875               continue;
12876             }
12877           
12878           if (TYPE_FOR_JAVA (basetype)
12879               && (current_lang_depth () == 0))
12880             TYPE_FOR_JAVA (ref) = 1;
12881           
12882           if (CLASS_TYPE_P (basetype))
12883             {
12884               base_binfo = TYPE_BINFO (basetype);
12885               /* This flag will be in the binfo of the base type, we must
12886                  clear it after copying the base binfos.  */
12887               BINFO_DEPENDENT_BASE_P (base_binfo)
12888                 = dependent_type_p (basetype);
12889             }
12890           else
12891             base_binfo = make_binfo (size_zero_node, basetype,
12892                                      NULL_TREE, NULL_TREE);
12893           
12894           TREE_VEC_ELT (binfos, i) = base_binfo;
12895           TREE_VEC_ELT (accesses, i) = access;
12896           /* This flag will be in the binfo of the base type, we must
12897              clear it after copying the base binfos.  */
12898           TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12899           
12900           SET_CLASSTYPE_MARKED (basetype);
12901           
12902           /* We are free to modify these bits because they are meaningless
12903              at top level, and BASETYPE is a top-level type.  */
12904           if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12905             {
12906               TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12907               /* Converting to a virtual base class requires looking
12908                  up the offset of the virtual base.  */
12909               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12910             }
12911           
12912           if (CLASS_TYPE_P (basetype))
12913             {
12914               TYPE_HAS_NEW_OPERATOR (ref)
12915                 |= TYPE_HAS_NEW_OPERATOR (basetype);
12916               TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12917                 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12918               TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12919               /* If the base-class uses multiple inheritance, so do we.  */
12920               TYPE_USES_MULTIPLE_INHERITANCE (ref)
12921                 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12922               /* Likewise, if converting to a base of the base may require
12923                  code, then we may need to generate code to convert to a
12924                  base as well.  */
12925               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12926                 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12927             }
12928           i++;
12929         }
12930       if (i)
12931         TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12932       else
12933         BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12934       
12935       if (i > 1)
12936         {
12937           TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12938           /* If there is more than one non-empty they cannot be at the same
12939              address.  */
12940           TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12941         }
12942     }
12943   
12944   /* Copy the base binfos, collect the virtual bases and set the
12945      inheritance order chain.  */
12946   copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
12947   CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
12948   
12949   /* Unmark all the types.  */
12950   while (i--)
12951     {
12952       tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
12953       
12954       CLEAR_CLASSTYPE_MARKED (basetype);
12955       if (CLASS_TYPE_P (basetype))
12956         {
12957           TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
12958           BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
12959         }
12960     }
12961   CLEAR_CLASSTYPE_MARKED (ref);
12962 }
12963
12964 \f
12965 /* Begin compiling the definition of an enumeration type.
12966    NAME is its name (or null if anonymous).
12967    Returns the type object, as yet incomplete.
12968    Also records info about it so that build_enumerator
12969    may be used to declare the individual values as they are read.  */
12970
12971 tree
12972 start_enum (tree name)
12973 {
12974   register tree enumtype = NULL_TREE;
12975   struct cp_binding_level *b = current_binding_level;
12976
12977   /* If this is the real definition for a previous forward reference,
12978      fill in the contents in the same object that used to be the
12979      forward reference.  */
12980
12981   if (name != NULL_TREE)
12982     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
12983
12984   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
12985     {
12986       error ("multiple definition of `%#T'", enumtype);
12987       cp_error_at ("previous definition here", enumtype);
12988       /* Clear out TYPE_VALUES, and start again.  */
12989       TYPE_VALUES (enumtype) = NULL_TREE;
12990     }
12991   else
12992     {
12993       enumtype = make_node (ENUMERAL_TYPE);
12994       pushtag (name, enumtype, 0);
12995     }
12996
12997   return enumtype;
12998 }
12999
13000 /* After processing and defining all the values of an enumeration type,
13001    install their decls in the enumeration type and finish it off.
13002    ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
13003
13004 void
13005 finish_enum (tree enumtype)
13006 {
13007   tree pair;
13008   tree minnode;
13009   tree maxnode;
13010   tree t;
13011   bool unsignedp;
13012   int lowprec;
13013   int highprec; 
13014   int precision;
13015
13016   /* We built up the VALUES in reverse order.  */
13017   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13018
13019   /* For an enum defined in a template, just set the type of the values;
13020      all further processing is postponed until the template is
13021      instantiated.  We need to set the type so that tsubst of a CONST_DECL
13022      works.  */
13023   if (processing_template_decl)
13024     {
13025       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13026         TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13027       if (at_function_scope_p ())
13028         add_stmt (build_min (TAG_DEFN, enumtype));
13029       return;
13030     }
13031
13032   if (TYPE_VALUES (enumtype))
13033     {
13034       minnode = maxnode = NULL_TREE;
13035
13036       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13037         {
13038           tree decl = TREE_VALUE (pair);
13039           tree value = DECL_INITIAL (decl);
13040
13041           /* [dcl.enum]: Following the closing brace of an enum-specifier,
13042              each enumerator has the type of its enumeration.  Prior to the
13043              closing brace, the type of each enumerator is the type of its
13044              initializing value.  */
13045           TREE_TYPE (decl) = enumtype;
13046
13047           /* Figure out what the minimum and maximum values of the
13048              enumerators are.  */
13049           if (!minnode)
13050             minnode = maxnode = value;
13051           else if (tree_int_cst_lt (maxnode, value))
13052             maxnode = value;
13053           else if (tree_int_cst_lt (value, minnode))
13054             minnode = value;
13055
13056           /* Set the TREE_TYPE for the values as well.  That's so that when
13057              we call decl_constant_value we get an entity of the right type
13058              (but with the constant value).  But first make a copy so we
13059              don't clobber shared INTEGER_CSTs.  */
13060           if (TREE_TYPE (value) != enumtype)
13061             {
13062               value = DECL_INITIAL (decl) = copy_node (value);
13063               TREE_TYPE (value) = enumtype;
13064             }
13065
13066           /* In addition, transform the TYPE_VALUES list to contain the
13067              values, rather than the CONST_DECLs for them.  */
13068           TREE_VALUE (pair) = value;
13069         }
13070     }
13071   else
13072     minnode = maxnode = integer_zero_node;
13073
13074   /* Compute the number of bits require to represent all values of the
13075      enumeration.  We must do this before the type of MINNODE and
13076      MAXNODE are transformed, since min_precision relies on the
13077      TREE_TYPE of the value it is passed.  */
13078   unsignedp = tree_int_cst_sgn (minnode) >= 0;
13079   lowprec = min_precision (minnode, unsignedp);
13080   highprec = min_precision (maxnode, unsignedp);
13081   precision = MAX (lowprec, highprec);
13082
13083   /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
13084   TYPE_SIZE (enumtype) = NULL_TREE;
13085   TYPE_PRECISION (enumtype) = precision;
13086   if (unsignedp)
13087     fixup_unsigned_type (enumtype);
13088   else
13089     fixup_signed_type (enumtype);
13090
13091   if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13092     /* Use the width of the narrowest normal C type which is wide
13093        enough.  */
13094     TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13095                                                 (precision, 1));
13096   else
13097     TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13098
13099   TYPE_SIZE (enumtype) = NULL_TREE;
13100   layout_type (enumtype);
13101
13102   /* Fix up all variant types of this enum type.  */
13103   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13104     {
13105       TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13106       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13107       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13108       TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13109       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13110       TYPE_MODE (t) = TYPE_MODE (enumtype);
13111       TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13112       TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13113       TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13114       TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13115     }
13116
13117   /* Finish debugging output for this type.  */
13118   rest_of_type_compilation (enumtype, namespace_bindings_p ());
13119 }
13120
13121 /* Build and install a CONST_DECL for an enumeration constant of the
13122    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13123    Assignment of sequential values by default is handled here.  */
13124
13125 void
13126 build_enumerator (tree name, tree value, tree enumtype)
13127 {
13128   tree decl;
13129   tree context;
13130   tree type;
13131
13132   /* Remove no-op casts from the value.  */
13133   if (value)
13134     STRIP_TYPE_NOPS (value);
13135
13136   if (! processing_template_decl)
13137     {
13138       /* Validate and default VALUE.  */
13139       if (value != NULL_TREE)
13140         {
13141           value = decl_constant_value (value);
13142
13143           if (TREE_CODE (value) == INTEGER_CST)
13144             {
13145               value = default_conversion (value);
13146               constant_expression_warning (value);
13147             }
13148           else
13149             {
13150               error ("enumerator value for `%D' not integer constant", name);
13151               value = NULL_TREE;
13152             }
13153         }
13154
13155       /* Default based on previous value.  */
13156       if (value == NULL_TREE)
13157         {
13158           tree prev_value;
13159
13160           if (TYPE_VALUES (enumtype))
13161             {
13162               /* The next value is the previous value ...  */
13163               prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13164               /* ... plus one.  */
13165               value = cp_build_binary_op (PLUS_EXPR,
13166                                           prev_value,
13167                                           integer_one_node);
13168
13169               if (tree_int_cst_lt (value, prev_value))
13170                 error ("overflow in enumeration values at `%D'", name);
13171             }
13172           else
13173             value = integer_zero_node;
13174         }
13175
13176       /* Remove no-op casts from the value.  */
13177       STRIP_TYPE_NOPS (value);
13178     }
13179
13180   /* C++ associates enums with global, function, or class declarations.  */
13181   context = current_scope ();
13182
13183   /* Build the actual enumeration constant.  Note that the enumeration
13184     constants have the type of their initializers until the
13185     enumeration is complete:
13186
13187       [ dcl.enum ]
13188
13189       Following the closing brace of an enum-specifier, each enumer-
13190       ator has the type of its enumeration.  Prior to the closing
13191       brace, the type of each enumerator is the type of its
13192       initializing value.
13193
13194     In finish_enum we will reset the type.  Of course, if we're
13195     processing a template, there may be no value.  */
13196   type = value ? TREE_TYPE (value) : NULL_TREE;
13197
13198   if (context && context == current_class_type)
13199     /* This enum declaration is local to the class.  We need the full
13200        lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
13201     decl = build_lang_decl (CONST_DECL, name, type);
13202   else
13203     /* It's a global enum, or it's local to a function.  (Note local to
13204       a function could mean local to a class method.  */
13205     decl = build_decl (CONST_DECL, name, type);
13206
13207   DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13208   TREE_CONSTANT (decl) = TREE_READONLY (decl) = 1;
13209   DECL_INITIAL (decl) = value;
13210
13211   if (context && context == current_class_type)
13212     /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13213       on the TYPE_FIELDS list for `S'.  (That's so that you can say
13214       things like `S::i' later.)  */
13215     finish_member_declaration (decl);
13216   else
13217     pushdecl (decl);
13218
13219   /* Add this enumeration constant to the list for this type.  */
13220   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13221 }
13222
13223 \f
13224 /* We're defining DECL.  Make sure that it's type is OK.  */
13225
13226 static void
13227 check_function_type (tree decl, tree current_function_parms)
13228 {
13229   tree fntype = TREE_TYPE (decl);
13230   tree return_type = complete_type (TREE_TYPE (fntype));
13231
13232   /* In a function definition, arg types must be complete.  */
13233   require_complete_types_for_parms (current_function_parms);
13234
13235   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13236     {
13237       error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13238
13239       /* Make it return void instead, but don't change the
13240          type of the DECL_RESULT, in case we have a named return value.  */
13241       if (TREE_CODE (fntype) == METHOD_TYPE)
13242         {
13243           tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13244           TREE_TYPE (decl)
13245             = build_cplus_method_type (ctype,
13246                                        void_type_node,
13247                                        FUNCTION_ARG_CHAIN (decl));
13248         }
13249       else
13250         TREE_TYPE (decl)
13251           = build_function_type (void_type_node,
13252                                  TYPE_ARG_TYPES (TREE_TYPE (decl)));
13253       TREE_TYPE (decl)
13254         = build_exception_variant (fntype,
13255                                    TYPE_RAISES_EXCEPTIONS (fntype));
13256     }
13257   else
13258     abstract_virtuals_error (decl, TREE_TYPE (fntype));
13259 }
13260
13261 /* Create the FUNCTION_DECL for a function definition.
13262    DECLSPECS and DECLARATOR are the parts of the declaration;
13263    they describe the function's name and the type it returns,
13264    but twisted together in a fashion that parallels the syntax of C.
13265
13266    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13267    DECLARATOR is really the DECL for the function we are about to
13268    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13269    indicating that the function is an inline defined in-class.
13270
13271    This function creates a binding context for the function body
13272    as well as setting up the FUNCTION_DECL in current_function_decl.
13273
13274    Returns 1 on success.  If the DECLARATOR is not suitable for a function
13275    (it defines a datum instead), we return 0, which tells
13276    yyparse to report a parse error.
13277
13278    For C++, we must first check whether that datum makes any sense.
13279    For example, "class A local_a(1,2);" means that variable local_a
13280    is an aggregate of type A, which should have a constructor
13281    applied to it with the argument list [1, 2].  */
13282
13283 int
13284 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13285 {
13286   tree decl1;
13287   tree ctype = NULL_TREE;
13288   tree fntype;
13289   tree restype;
13290   int doing_friend = 0;
13291   struct cp_binding_level *bl;
13292   tree current_function_parms;
13293
13294   /* Sanity check.  */
13295   my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13296   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13297
13298   /* This should only be done once on the top most decl.  */
13299   if (have_extern_spec)
13300     {
13301       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13302       have_extern_spec = false;
13303     }
13304
13305   if (flags & SF_PRE_PARSED)
13306     {
13307       decl1 = declarator;
13308
13309       fntype = TREE_TYPE (decl1);
13310       if (TREE_CODE (fntype) == METHOD_TYPE)
13311         ctype = TYPE_METHOD_BASETYPE (fntype);
13312
13313       /* ISO C++ 11.4/5.  A friend function defined in a class is in
13314          the (lexical) scope of the class in which it is defined.  */
13315       if (!ctype && DECL_FRIEND_P (decl1))
13316         {
13317           ctype = DECL_FRIEND_CONTEXT (decl1);
13318
13319           /* CTYPE could be null here if we're dealing with a template;
13320              for example, `inline friend float foo()' inside a template
13321              will have no CTYPE set.  */
13322           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13323             ctype = NULL_TREE;
13324           else
13325             doing_friend = 1;
13326         }
13327
13328       last_function_parms = DECL_ARGUMENTS (decl1);
13329     }
13330   else
13331     {
13332       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13333       /* If the declarator is not suitable for a function definition,
13334          cause a syntax error.  */
13335       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13336         return 0;
13337
13338       cplus_decl_attributes (&decl1, attrs, 0);
13339
13340       /* If #pragma weak was used, mark the decl weak now.  */
13341       if (global_scope_p (current_binding_level))
13342         maybe_apply_pragma_weak (decl1);
13343
13344       fntype = TREE_TYPE (decl1);
13345
13346       restype = TREE_TYPE (fntype);
13347       if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13348         {
13349           error ("semicolon missing after declaration of `%#T'", restype);
13350           shadow_tag (build_tree_list (NULL_TREE, restype));
13351           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13352           if (TREE_CODE (fntype) == FUNCTION_TYPE)
13353             fntype = build_function_type (integer_type_node,
13354                                           TYPE_ARG_TYPES (fntype));
13355           else
13356             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13357                                               integer_type_node,
13358                                               TYPE_ARG_TYPES (fntype));
13359           TREE_TYPE (decl1) = fntype;
13360         }
13361
13362       if (TREE_CODE (fntype) == METHOD_TYPE)
13363         ctype = TYPE_METHOD_BASETYPE (fntype);
13364       else if (DECL_MAIN_P (decl1))
13365         {
13366           /* If this doesn't return integer_type, or a typedef to
13367              integer_type, complain.  */
13368           if (!same_type_p (TREE_TYPE (TREE_TYPE (decl1)), integer_type_node))
13369             {
13370               if (pedantic || warn_return_type)
13371                 pedwarn ("return type for `main' changed to `int'");
13372               TREE_TYPE (decl1) = fntype = default_function_type;
13373             }
13374         }
13375     }
13376
13377   if (DECL_DECLARED_INLINE_P (decl1)
13378       && lookup_attribute ("noinline", attrs))
13379     warning ("%Hinline function '%D' given attribute noinline",
13380              &DECL_SOURCE_LOCATION (decl1), decl1);
13381
13382   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13383     /* This is a constructor, we must ensure that any default args
13384        introduced by this definition are propagated to the clones
13385        now. The clones are used directly in overload resolution.  */
13386     adjust_clone_args (decl1);
13387
13388   /* Sometimes we don't notice that a function is a static member, and
13389      build a METHOD_TYPE for it.  Fix that up now.  */
13390   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13391       && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13392     {
13393       revert_static_member_fn (decl1);
13394       last_function_parms = TREE_CHAIN (last_function_parms);
13395       ctype = NULL_TREE;
13396     }
13397
13398   /* Warn if function was previously implicitly declared
13399      (but not if we warned then).  */
13400   if (! warn_implicit
13401       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13402     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13403
13404   /* Set up current_class_type, and enter the scope of the class, if
13405      appropriate.  */
13406   if (ctype)
13407     push_nested_class (ctype);
13408   else if (DECL_STATIC_FUNCTION_P (decl1))
13409     push_nested_class (DECL_CONTEXT (decl1));
13410
13411   /* Now that we have entered the scope of the class, we must restore
13412      the bindings for any template parameters surrounding DECL1, if it
13413      is an inline member template.  (Order is important; consider the
13414      case where a template parameter has the same name as a field of
13415      the class.)  It is not until after this point that
13416      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
13417   if (flags & SF_INCLASS_INLINE)
13418     maybe_begin_member_template_processing (decl1);
13419
13420   /* Effective C++ rule 15.  */
13421   if (warn_ecpp
13422       && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13423       && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13424     warning ("`operator=' should return a reference to `*this'");
13425
13426   /* Make the init_value nonzero so pushdecl knows this is not tentative.
13427      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
13428   if (!DECL_INITIAL (decl1))
13429     DECL_INITIAL (decl1) = error_mark_node;
13430
13431   /* This function exists in static storage.
13432      (This does not mean `static' in the C sense!)  */
13433   TREE_STATIC (decl1) = 1;
13434
13435   /* We must call push_template_decl after current_class_type is set
13436      up.  (If we are processing inline definitions after exiting a
13437      class scope, current_class_type will be NULL_TREE until set above
13438      by push_nested_class.)  */
13439   if (processing_template_decl)
13440     decl1 = push_template_decl (decl1);
13441
13442   /* We are now in the scope of the function being defined.  */
13443   current_function_decl = decl1;
13444
13445   /* Save the parm names or decls from this function's declarator
13446      where store_parm_decls will find them.  */
13447   current_function_parms = last_function_parms;
13448
13449   /* Make sure the parameter and return types are reasonable.  When
13450      you declare a function, these types can be incomplete, but they
13451      must be complete when you define the function.  */
13452   if (! processing_template_decl)
13453     check_function_type (decl1, current_function_parms);
13454
13455   /* Build the return declaration for the function.  */
13456   restype = TREE_TYPE (fntype);
13457   /* Promote the value to int before returning it.  */
13458   if (c_promoting_integer_type_p (restype))
13459     restype = type_promotes_to (restype);
13460   if (DECL_RESULT (decl1) == NULL_TREE)
13461     {
13462       DECL_RESULT (decl1)
13463         = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13464       c_apply_type_quals_to_decl (cp_type_quals (restype),
13465                                   DECL_RESULT (decl1));
13466     }
13467
13468   /* Initialize RTL machinery.  We cannot do this until
13469      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
13470      even when processing a template; this is how we get
13471      CFUN set up, and our per-function variables initialized.
13472      FIXME factor out the non-RTL stuff.  */
13473   bl = current_binding_level;
13474   init_function_start (decl1, input_filename, input_line);
13475   current_binding_level = bl;
13476
13477   /* Even though we're inside a function body, we still don't want to
13478      call expand_expr to calculate the size of a variable-sized array.
13479      We haven't necessarily assigned RTL to all variables yet, so it's
13480      not safe to try to expand expressions involving them.  */
13481   immediate_size_expand = 0;
13482   cfun->x_dont_save_pending_sizes_p = 1;
13483
13484   /* Start the statement-tree, start the tree now.  */
13485   begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13486
13487   /* Don't double-count statements in templates.  */
13488   DECL_NUM_STMTS (decl1) = 0;
13489
13490   /* Let the user know we're compiling this function.  */
13491   announce_function (decl1);
13492
13493   /* Record the decl so that the function name is defined.
13494      If we already have a decl for this name, and it is a FUNCTION_DECL,
13495      use the old decl.  */
13496   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13497     {
13498       /* A specialization is not used to guide overload resolution.  */
13499       if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13500           && ! DECL_FUNCTION_MEMBER_P (decl1))
13501         decl1 = pushdecl (decl1);
13502       else
13503         {
13504           /* We need to set the DECL_CONTEXT.  */
13505           if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13506             DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13507           /* And make sure we have enough default args.  */
13508           check_default_args (decl1);
13509         }
13510       fntype = TREE_TYPE (decl1);
13511     }
13512
13513   /* Reset these in case the call to pushdecl changed them.  */
13514   current_function_decl = decl1;
13515   cfun->decl = decl1;
13516
13517   /* If we are (erroneously) defining a function that we have already
13518      defined before, wipe out what we knew before.  */
13519   if (!DECL_PENDING_INLINE_P (decl1))
13520     DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13521
13522   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13523     {
13524       /* We know that this was set up by `grokclassfn'.  We do not
13525          wait until `store_parm_decls', since evil parse errors may
13526          never get us to that point.  Here we keep the consistency
13527          between `current_class_type' and `current_class_ptr'.  */
13528       tree t = DECL_ARGUMENTS (decl1);
13529
13530       my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13531                           162);
13532       my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13533                           19990811);
13534
13535       cp_function_chain->x_current_class_ref
13536         = build_indirect_ref (t, NULL);
13537       cp_function_chain->x_current_class_ptr = t;
13538
13539       /* Constructors and destructors need to know whether they're "in
13540          charge" of initializing virtual base classes.  */
13541       t = TREE_CHAIN (t);
13542       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13543         {
13544           current_in_charge_parm = t;
13545           t = TREE_CHAIN (t);
13546         }
13547       if (DECL_HAS_VTT_PARM_P (decl1))
13548         {
13549           if (DECL_NAME (t) != vtt_parm_identifier)
13550             abort ();
13551           current_vtt_parm = t;
13552         }
13553     }
13554
13555   if (DECL_INTERFACE_KNOWN (decl1))
13556     {
13557       tree ctx = decl_function_context (decl1);
13558
13559       if (DECL_NOT_REALLY_EXTERN (decl1))
13560         DECL_EXTERNAL (decl1) = 0;
13561
13562       if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13563           && TREE_PUBLIC (ctx))
13564         /* This is a function in a local class in an extern inline
13565            function.  */
13566         comdat_linkage (decl1);
13567     }
13568   /* If this function belongs to an interface, it is public.
13569      If it belongs to someone else's interface, it is also external.
13570      This only affects inlines and template instantiations.  */
13571   else if (interface_unknown == 0
13572            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13573                || flag_alt_external_templates))
13574     {
13575       if (DECL_DECLARED_INLINE_P (decl1) 
13576           || DECL_TEMPLATE_INSTANTIATION (decl1)
13577           || processing_template_decl)
13578         {
13579           DECL_EXTERNAL (decl1)
13580             = (interface_only
13581                || (DECL_DECLARED_INLINE_P (decl1) 
13582                    && ! flag_implement_inlines
13583                    && !DECL_VINDEX (decl1)));
13584
13585           /* For WIN32 we also want to put these in linkonce sections.  */
13586           maybe_make_one_only (decl1);
13587         }
13588       else
13589         DECL_EXTERNAL (decl1) = 0;
13590       DECL_NOT_REALLY_EXTERN (decl1) = 0;
13591       DECL_INTERFACE_KNOWN (decl1) = 1;
13592     }
13593   else if (interface_unknown && interface_only
13594            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13595                || flag_alt_external_templates))
13596     {
13597       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13598          interface, we will have interface_only set but not
13599          interface_known.  In that case, we don't want to use the normal
13600          heuristics because someone will supply a #pragma implementation
13601          elsewhere, and deducing it here would produce a conflict.  */
13602       comdat_linkage (decl1);
13603       DECL_EXTERNAL (decl1) = 0;
13604       DECL_INTERFACE_KNOWN (decl1) = 1;
13605       DECL_DEFER_OUTPUT (decl1) = 1;
13606     }
13607   else
13608     {
13609       /* This is a definition, not a reference.
13610          So clear DECL_EXTERNAL.  */
13611       DECL_EXTERNAL (decl1) = 0;
13612
13613       if ((DECL_DECLARED_INLINE_P (decl1) 
13614            || DECL_TEMPLATE_INSTANTIATION (decl1))
13615           && ! DECL_INTERFACE_KNOWN (decl1)
13616           /* Don't try to defer nested functions for now.  */
13617           && ! decl_function_context (decl1))
13618         DECL_DEFER_OUTPUT (decl1) = 1;
13619       else
13620         DECL_INTERFACE_KNOWN (decl1) = 1;
13621     }
13622
13623   pushlevel (0);
13624   current_binding_level->parm_flag = 1;
13625
13626   ++function_depth;
13627
13628   if (DECL_DESTRUCTOR_P (decl1))
13629     {
13630       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13631       DECL_CONTEXT (dtor_label) = current_function_decl;
13632     }
13633
13634   start_fname_decls ();
13635   
13636   store_parm_decls (current_function_parms);
13637
13638   return 1;
13639 }
13640 \f
13641 /* Store the parameter declarations into the current function declaration.
13642    This is called after parsing the parameter declarations, before
13643    digesting the body of the function.
13644
13645    Also install to binding contour return value identifier, if any.  */
13646
13647 static void
13648 store_parm_decls (tree current_function_parms)
13649 {
13650   register tree fndecl = current_function_decl;
13651   register tree parm;
13652
13653   /* This is a chain of any other decls that came in among the parm
13654      declarations.  If a parm is declared with  enum {foo, bar} x;
13655      then CONST_DECLs for foo and bar are put here.  */
13656   tree nonparms = NULL_TREE;
13657
13658   if (current_function_parms)
13659     {
13660       /* This case is when the function was defined with an ANSI prototype.
13661          The parms already have decls, so we need not do anything here
13662          except record them as in effect
13663          and complain if any redundant old-style parm decls were written.  */
13664
13665       tree specparms = current_function_parms;
13666       tree next;
13667
13668       /* Must clear this because it might contain TYPE_DECLs declared
13669              at class level.  */
13670       storedecls (NULL_TREE);
13671
13672       /* If we're doing semantic analysis, then we'll call pushdecl
13673              for each of these.  We must do them in reverse order so that
13674              they end in the correct forward order.  */
13675       specparms = nreverse (specparms);
13676
13677       for (parm = specparms; parm; parm = next)
13678         {
13679           next = TREE_CHAIN (parm);
13680           if (TREE_CODE (parm) == PARM_DECL)
13681             {
13682               if (DECL_NAME (parm) == NULL_TREE
13683                   || TREE_CODE (parm) != VOID_TYPE)
13684                 pushdecl (parm);
13685               else
13686                 error ("parameter `%D' declared void", parm);
13687             }
13688           else
13689             {
13690               /* If we find an enum constant or a type tag,
13691                  put it aside for the moment.  */
13692               TREE_CHAIN (parm) = NULL_TREE;
13693               nonparms = chainon (nonparms, parm);
13694             }
13695         }
13696
13697       /* Get the decls in their original chain order and record in the
13698          function.  This is all and only the PARM_DECLs that were
13699          pushed into scope by the loop above.  */
13700       DECL_ARGUMENTS (fndecl) = getdecls ();
13701     }
13702   else
13703     DECL_ARGUMENTS (fndecl) = NULL_TREE;
13704
13705   /* Now store the final chain of decls for the arguments
13706      as the decl-chain of the current lexical scope.
13707      Put the enumerators in as well, at the front so that
13708      DECL_ARGUMENTS is not modified.  */
13709   storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13710
13711   /* Do the starting of the exception specifications, if we have any.  */
13712   if (flag_exceptions && !processing_template_decl
13713       && flag_enforce_eh_specs
13714       && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13715     current_eh_spec_block = begin_eh_spec_block ();
13716 }
13717
13718 \f
13719 /* We have finished doing semantic analysis on DECL, but have not yet
13720    generated RTL for its body.  Save away our current state, so that
13721    when we want to generate RTL later we know what to do.  */
13722
13723 static void
13724 save_function_data (tree decl)
13725 {
13726   struct language_function *f;
13727
13728   /* Save the language-specific per-function data so that we can
13729      get it back when we really expand this function.  */
13730   my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13731                       19990908);
13732
13733   /* Make a copy.  */
13734   f = ((struct language_function *)
13735        ggc_alloc (sizeof (struct language_function)));
13736   memcpy (f, cp_function_chain, sizeof (struct language_function));
13737   DECL_SAVED_FUNCTION_DATA (decl) = f;
13738
13739   /* Clear out the bits we don't need.  */
13740   f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13741   f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13742   f->x_named_label_uses = NULL;
13743   f->bindings = NULL;
13744   f->x_local_names = NULL;
13745
13746   /* When we get back here again, we will be expanding.  */
13747   f->x_expanding_p = 1;
13748
13749   /* If we've already decided that we cannot inline this function, we
13750      must remember that fact when we actually go to expand the
13751      function.  */
13752   if (current_function_cannot_inline)
13753     {
13754       f->cannot_inline = current_function_cannot_inline;
13755       DECL_INLINE (decl) = 0;
13756     }
13757 }
13758
13759 /* Add a note to mark the beginning of the main body of the constructor.
13760    This is used to set up the data structures for the cleanup regions for
13761    fully-constructed bases and members.  */
13762
13763 static void
13764 begin_constructor_body (void)
13765 {
13766 }
13767
13768 /* Add a note to mark the end of the main body of the constructor.  This is
13769    used to end the cleanup regions for fully-constructed bases and
13770    members.  */
13771
13772 static void
13773 finish_constructor_body (void)
13774 {
13775 }
13776
13777 /* Do all the processing for the beginning of a destructor; set up the
13778    vtable pointers and cleanups for bases and members.  */
13779
13780 static void
13781 begin_destructor_body (void)
13782 {
13783   tree if_stmt;
13784   tree compound_stmt;
13785
13786   /* If the dtor is empty, and we know there is not any possible
13787      way we could use any vtable entries, before they are possibly
13788      set by a base class dtor, we don't have to setup the vtables,
13789      as we know that any base class dtor will set up any vtables
13790      it needs.  We avoid MI, because one base class dtor can do a
13791      virtual dispatch to an overridden function that would need to
13792      have a non-related vtable set up, we cannot avoid setting up
13793      vtables in that case.  We could change this to see if there
13794      is just one vtable.
13795
13796      ??? In the destructor for a class, the vtables are set
13797      appropriately for that class.  There will be no non-related
13798      vtables.  jason 2001-12-11.  */
13799   if_stmt = begin_if_stmt ();
13800
13801   /* If it is not safe to avoid setting up the vtables, then
13802      someone will change the condition to be boolean_true_node.  
13803      (Actually, for now, we do not have code to set the condition
13804      appropriately, so we just assume that we always need to
13805      initialize the vtables.)  */
13806   finish_if_stmt_cond (boolean_true_node, if_stmt);
13807
13808   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13809
13810   /* Make all virtual function table pointers in non-virtual base
13811      classes point to CURRENT_CLASS_TYPE's virtual function
13812      tables.  */
13813   initialize_vtbl_ptrs (current_class_ptr);
13814
13815   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13816   finish_then_clause (if_stmt);
13817   finish_if_stmt ();
13818
13819   /* And insert cleanups for our bases and members so that they
13820      will be properly destroyed if we throw.  */
13821   push_base_cleanups ();
13822 }
13823
13824 /* At the end of every destructor we generate code to delete the object if
13825    necessary.  Do that now.  */
13826
13827 static void
13828 finish_destructor_body (void)
13829 {
13830   tree exprstmt;
13831
13832   /* Any return from a destructor will end up here; that way all base
13833      and member cleanups will be run when the function returns.  */
13834   add_stmt (build_stmt (LABEL_STMT, dtor_label));
13835
13836   /* In a virtual destructor, we must call delete.  */
13837   if (DECL_VIRTUAL_P (current_function_decl))
13838     {
13839       tree if_stmt;
13840       tree virtual_size = cxx_sizeof (current_class_type);
13841
13842       /* [class.dtor]
13843
13844       At the point of definition of a virtual destructor (including
13845       an implicit definition), non-placement operator delete shall
13846       be looked up in the scope of the destructor's class and if
13847       found shall be accessible and unambiguous.  */
13848       exprstmt = build_op_delete_call
13849         (DELETE_EXPR, current_class_ptr, virtual_size,
13850          LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13851
13852       if_stmt = begin_if_stmt ();
13853       finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13854                                   current_in_charge_parm,
13855                                   integer_one_node),
13856                            if_stmt);
13857       finish_expr_stmt (exprstmt);
13858       finish_then_clause (if_stmt);
13859       finish_if_stmt ();
13860     }
13861 }
13862
13863 /* Do the necessary processing for the beginning of a function body, which
13864    in this case includes member-initializers, but not the catch clauses of
13865    a function-try-block.  Currently, this means opening a binding level
13866    for the member-initializers (in a ctor) and member cleanups (in a dtor).
13867    In other functions, this isn't necessary, but it doesn't hurt.  */
13868
13869 tree
13870 begin_function_body (void)
13871 {
13872   tree stmt;
13873
13874   if (processing_template_decl)
13875     /* Do nothing now.  */;
13876   else
13877     /* Always keep the BLOCK node associated with the outermost pair of
13878        curly braces of a function.  These are needed for correct
13879        operation of dwarfout.c.  */
13880     keep_next_level (1);
13881
13882   stmt = begin_compound_stmt (0);
13883   COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13884
13885   if (processing_template_decl)
13886     /* Do nothing now.  */;
13887   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13888     begin_constructor_body ();
13889   else if (DECL_DESTRUCTOR_P (current_function_decl))
13890     begin_destructor_body ();
13891
13892   return stmt;
13893 }
13894
13895 /* Do the processing for the end of a function body.  Currently, this means
13896    closing out the cleanups for fully-constructed bases and members, and in
13897    the case of the destructor, deleting the object if desired.  Again, this
13898    is only meaningful for [cd]tors, since they are the only functions where
13899    there is a significant distinction between the main body and any
13900    function catch clauses.  Handling, say, main() return semantics here
13901    would be wrong, as flowing off the end of a function catch clause for
13902    main() would also need to return 0.  */
13903
13904 void
13905 finish_function_body (tree compstmt)
13906 {
13907   /* Close the block.  */
13908   finish_compound_stmt (0, compstmt);
13909
13910   if (processing_template_decl)
13911     /* Do nothing now.  */;
13912   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13913     finish_constructor_body ();
13914   else if (DECL_DESTRUCTOR_P (current_function_decl))
13915     finish_destructor_body ();
13916 }  
13917
13918 /* Finish up a function declaration and compile that function
13919    all the way to assembler language output.  The free the storage
13920    for the function definition.
13921
13922    FLAGS is a bitwise or of the following values:
13923      2 - INCLASS_INLINE
13924        We just finished processing the body of an in-class inline
13925        function definition.  (This processing will have taken place
13926        after the class definition is complete.)  */
13927
13928 tree
13929 finish_function (int flags)
13930 {
13931   register tree fndecl = current_function_decl;
13932   tree fntype, ctype = NULL_TREE;
13933   int inclass_inline = (flags & 2) != 0;
13934   int nested;
13935
13936   /* When we get some parse errors, we can end up without a
13937      current_function_decl, so cope.  */
13938   if (fndecl == NULL_TREE)
13939     return error_mark_node;
13940
13941   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
13942       && DECL_VIRTUAL_P (fndecl)
13943       && !processing_template_decl)
13944     {
13945       tree fnclass = DECL_CONTEXT (fndecl);
13946       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
13947         keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
13948     }
13949
13950   nested = function_depth > 1;
13951   fntype = TREE_TYPE (fndecl);
13952
13953   /*  TREE_READONLY (fndecl) = 1;
13954       This caused &foo to be of type ptr-to-const-function
13955       which then got a warning when stored in a ptr-to-function variable.  */
13956
13957   my_friendly_assert (building_stmt_tree (), 20000911);
13958
13959   finish_fname_decls ();
13960   
13961   /* For a cloned function, we've already got all the code we need;
13962      there's no need to add any extra bits.  */
13963   if (!DECL_CLONED_FUNCTION_P (fndecl))
13964     {
13965       if (DECL_MAIN_P (current_function_decl))
13966         {
13967           /* Make it so that `main' always returns 0 by default.  */
13968 #if VMS_TARGET
13969           finish_return_stmt (integer_one_node);
13970 #else
13971           finish_return_stmt (integer_zero_node);
13972 #endif
13973         }
13974
13975       /* Finish dealing with exception specifiers.  */
13976       if (flag_exceptions && !processing_template_decl
13977           && flag_enforce_eh_specs
13978           && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13979         finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
13980                               (TREE_TYPE (current_function_decl)),
13981                               current_eh_spec_block);
13982     }
13983
13984   /* If we're saving up tree structure, tie off the function now.  */
13985   finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
13986
13987   /* If this function can't throw any exceptions, remember that.  */
13988   if (!processing_template_decl
13989       && !cp_function_chain->can_throw
13990       && !flag_non_call_exceptions)
13991     TREE_NOTHROW (fndecl) = 1;
13992
13993   /* This must come after expand_function_end because cleanups might
13994      have declarations (from inline functions) that need to go into
13995      this function's blocks.  */
13996   
13997   /* If the current binding level isn't the outermost binding level
13998      for this function, either there is a bug, or we have experienced
13999      syntax errors and the statement tree is malformed.  */
14000   if (current_binding_level->parm_flag != 1)
14001     {
14002       /* Make sure we have already experienced errors.  */
14003       if (errorcount == 0)
14004         abort ();
14005
14006       /* Throw away the broken statement tree and extra binding
14007          levels.  */
14008       DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14009
14010       while (current_binding_level->parm_flag != 1)
14011         {
14012           if (current_binding_level->parm_flag == 2)
14013             pop_nested_class ();
14014           else
14015             poplevel (0, 0, 0);
14016         }
14017     }
14018   poplevel (1, 0, 1);
14019
14020   /* Set up the named return value optimization, if we can.  Here, we
14021      eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14022      for the nrv.  genrtl_start_function and declare_return_variable
14023      handle making the nrv and RESULT_DECL share space.  */
14024   if (current_function_return_value)
14025     {
14026       tree r = current_function_return_value;
14027       tree outer;
14028
14029       if (r != error_mark_node
14030           /* This is only worth doing for fns that return in memory--and
14031              simpler, since we don't have to worry about promoted modes.  */
14032           && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl)))
14033           /* Only allow this for variables declared in the outer scope of
14034              the function so we know that their lifetime always ends with a
14035              return; see g++.dg/opt/nrv6.C.  We could be more flexible if
14036              we were to do this optimization in tree-ssa.  */
14037           /* Skip the artificial function body block.  */
14038           && (outer = BLOCK_SUBBLOCKS (BLOCK_SUBBLOCKS (DECL_INITIAL (fndecl))),
14039               chain_member (r, BLOCK_VARS (outer))))
14040         {
14041           
14042           DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14043           walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14044                                         nullify_returns_r, r);
14045         }
14046       else
14047         /* Clear it so genrtl_start_function and declare_return_variable
14048            know we're not optimizing.  */
14049         current_function_return_value = NULL_TREE;
14050     }
14051
14052   /* Remember that we were in class scope.  */
14053   if (current_class_name)
14054     ctype = current_class_type;
14055
14056   /* Must mark the RESULT_DECL as being in this function.  */
14057   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14058
14059   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14060      to the FUNCTION_DECL node itself.  */
14061   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14062
14063   /* Save away current state, if appropriate.  */
14064   if (!processing_template_decl)
14065     save_function_data (fndecl);
14066
14067   /* If this function calls `setjmp' it cannot be inlined.  When
14068      `longjmp' is called it is not guaranteed to restore the value of
14069      local variables that have been modified since the call to
14070      `setjmp'.  So, if were to inline this function into some caller
14071      `c', then when we `longjmp', we might not restore all variables
14072      in `c'.  (It might seem, at first blush, that there's no way for
14073      this function to modify local variables in `c', but their
14074      addresses may have been stored somewhere accessible to this
14075      function.)  */
14076   if (!processing_template_decl && calls_setjmp_p (fndecl))
14077     DECL_UNINLINABLE (fndecl) = 1;
14078
14079   /* Complain if there's just no return statement.  */
14080   if (warn_return_type
14081       && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14082       && !current_function_returns_value && !current_function_returns_null
14083       /* Don't complain if we abort or throw.  */
14084       && !current_function_returns_abnormally
14085       && !DECL_NAME (DECL_RESULT (fndecl))
14086       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
14087          inline function, as we might never be compiled separately.  */
14088       && (DECL_INLINE (fndecl) || processing_template_decl))
14089     warning ("no return statement in function returning non-void");
14090     
14091   /* Clear out memory we no longer need.  */
14092   free_after_parsing (cfun);
14093   /* Since we never call rest_of_compilation, we never clear
14094      CFUN.  Do so explicitly.  */
14095   free_after_compilation (cfun);
14096   cfun = NULL;
14097
14098   /* If this is an in-class inline definition, we may have to pop the
14099      bindings for the template parameters that we added in
14100      maybe_begin_member_template_processing when start_function was
14101      called.  */
14102   if (inclass_inline)
14103     maybe_end_member_template_processing ();
14104
14105   /* Leave the scope of the class.  */
14106   if (ctype)
14107     pop_nested_class ();
14108
14109   --function_depth;
14110
14111   /* Clean up.  */
14112   if (! nested)
14113     /* Let the error reporting routines know that we're outside a
14114        function.  For a nested function, this value is used in
14115        cxx_pop_function_context and then reset via pop_function_context.  */
14116     current_function_decl = NULL_TREE;
14117
14118   return fndecl;
14119 }
14120 \f
14121 /* Create the FUNCTION_DECL for a function definition.
14122    DECLSPECS and DECLARATOR are the parts of the declaration;
14123    they describe the return type and the name of the function,
14124    but twisted together in a fashion that parallels the syntax of C.
14125
14126    This function creates a binding context for the function body
14127    as well as setting up the FUNCTION_DECL in current_function_decl.
14128
14129    Returns a FUNCTION_DECL on success.
14130
14131    If the DECLARATOR is not suitable for a function (it defines a datum
14132    instead), we return 0, which tells yyparse to report a parse error.
14133
14134    May return void_type_node indicating that this method is actually
14135    a friend.  See grokfield for more details.
14136
14137    Came here with a `.pushlevel' .
14138
14139    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14140    CHANGES TO CODE IN `grokfield'.  */
14141
14142 tree
14143 start_method (tree declspecs, tree declarator, tree attrlist)
14144 {
14145   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14146                                 &attrlist);
14147
14148   if (fndecl == error_mark_node)
14149     return error_mark_node;
14150
14151   if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14152     {
14153       error ("invalid member function declaration");
14154       return error_mark_node;
14155     }
14156
14157   if (attrlist)
14158     cplus_decl_attributes (&fndecl, attrlist, 0);
14159
14160   /* Pass friends other than inline friend functions back.  */
14161   if (fndecl == void_type_node)
14162     return fndecl;
14163
14164   if (DECL_IN_AGGR_P (fndecl))
14165     {
14166       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14167         {
14168           if (DECL_CONTEXT (fndecl)
14169               && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14170             error ("`%D' is already defined in class `%T'", fndecl,
14171                       DECL_CONTEXT (fndecl));
14172         }
14173       return void_type_node;
14174     }
14175
14176   check_template_shadow (fndecl);
14177
14178   DECL_DECLARED_INLINE_P (fndecl) = 1;
14179
14180   DID_INLINE_FUNC (fndecl) = 0;
14181   if (flag_default_inline)
14182     DECL_INLINE (fndecl) = 1;
14183
14184   /* We process method specializations in finish_struct_1.  */
14185   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14186     fndecl = push_template_decl (fndecl);
14187
14188   if (! DECL_FRIEND_P (fndecl))
14189     {
14190       if (TREE_CHAIN (fndecl))
14191         {
14192           fndecl = copy_node (fndecl);
14193           TREE_CHAIN (fndecl) = NULL_TREE;
14194         }
14195       grok_special_member_properties (fndecl);
14196     }
14197
14198   cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14199
14200   /* Make a place for the parms */
14201   pushlevel (0);
14202   current_binding_level->parm_flag = 1;
14203
14204   DECL_IN_AGGR_P (fndecl) = 1;
14205   return fndecl;
14206 }
14207
14208 /* Go through the motions of finishing a function definition.
14209    We don't compile this method until after the whole class has
14210    been processed.
14211
14212    FINISH_METHOD must return something that looks as though it
14213    came from GROKFIELD (since we are defining a method, after all).
14214
14215    This is called after parsing the body of the function definition.
14216    STMTS is the chain of statements that makes up the function body.
14217
14218    DECL is the ..._DECL that `start_method' provided.  */
14219
14220 tree
14221 finish_method (tree decl)
14222 {
14223   register tree fndecl = decl;
14224   tree old_initial;
14225
14226   register tree link;
14227
14228   if (decl == void_type_node)
14229     return decl;
14230
14231   old_initial = DECL_INITIAL (fndecl);
14232
14233   /* Undo the level for the parms (from start_method).
14234      This is like poplevel, but it causes nothing to be
14235      saved.  Saving information here confuses symbol-table
14236      output routines.  Besides, this information will
14237      be correctly output when this method is actually
14238      compiled.  */
14239
14240   /* Clear out the meanings of the local variables of this level;
14241      also record in each decl which block it belongs to.  */
14242
14243   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14244     {
14245       if (DECL_NAME (link) != NULL_TREE)
14246         pop_binding (DECL_NAME (link), link);
14247       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14248       DECL_CONTEXT (link) = NULL_TREE;
14249     }
14250
14251   poplevel (0, 0, 0);
14252
14253   DECL_INITIAL (fndecl) = old_initial;
14254
14255   /* We used to check if the context of FNDECL was different from
14256      current_class_type as another way to get inside here.  This didn't work
14257      for String.cc in libg++.  */
14258   if (DECL_FRIEND_P (fndecl))
14259     {
14260       CLASSTYPE_INLINE_FRIENDS (current_class_type)
14261         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14262       decl = void_type_node;
14263     }
14264
14265   return decl;
14266 }
14267 \f
14268
14269 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
14270    we can lay it out later, when and if its type becomes complete.  */
14271
14272 void
14273 maybe_register_incomplete_var (tree var)
14274 {
14275   my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14276
14277   /* Keep track of variables with incomplete types.  */
14278   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 
14279       && DECL_EXTERNAL (var))
14280     {
14281       tree inner_type = TREE_TYPE (var);
14282       
14283       while (TREE_CODE (inner_type) == ARRAY_TYPE)
14284         inner_type = TREE_TYPE (inner_type);
14285       inner_type = TYPE_MAIN_VARIANT (inner_type);
14286       
14287       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14288           /* RTTI TD entries are created while defining the type_info.  */
14289           || (TYPE_LANG_SPECIFIC (inner_type)
14290               && TYPE_BEING_DEFINED (inner_type)))
14291         incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14292     }
14293 }
14294
14295 /* Called when a class type (given by TYPE) is defined.  If there are
14296    any existing VAR_DECLs whose type hsa been completed by this
14297    declaration, update them now.  */
14298
14299 void
14300 complete_vars (tree type)
14301 {
14302   tree *list = &incomplete_vars;
14303
14304   my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14305   while (*list) 
14306     {
14307       if (same_type_p (type, TREE_PURPOSE (*list)))
14308         {
14309           tree var = TREE_VALUE (*list);
14310           /* Complete the type of the variable.  The VAR_DECL itself
14311              will be laid out in expand_expr.  */
14312           complete_type (TREE_TYPE (var));
14313           /* Remove this entry from the list.  */
14314           *list = TREE_CHAIN (*list);
14315         }
14316       else
14317         list = &TREE_CHAIN (*list);
14318     }
14319 }
14320
14321 /* If DECL is of a type which needs a cleanup, build that cleanup
14322    here.  */
14323
14324 tree
14325 cxx_maybe_build_cleanup (tree decl)
14326 {
14327   tree type = TREE_TYPE (decl);
14328
14329   if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14330     {
14331       int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14332       tree rval;
14333
14334       if (TREE_CODE (type) == ARRAY_TYPE)
14335         rval = decl;
14336       else
14337         {
14338           cxx_mark_addressable (decl);
14339           rval = build_unary_op (ADDR_EXPR, decl, 0);
14340         }
14341
14342       /* Optimize for space over speed here.  */
14343       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14344           || flag_expensive_optimizations)
14345         flags |= LOOKUP_NONVIRTUAL;
14346
14347       rval = build_delete (TREE_TYPE (rval), rval,
14348                            sfk_complete_destructor, flags, 0);
14349
14350       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14351           && ! TYPE_HAS_DESTRUCTOR (type))
14352         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14353                                                build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14354
14355       return rval;
14356     }
14357   return NULL_TREE;
14358 }
14359 \f
14360 /* When a stmt has been parsed, this function is called.  */
14361
14362 void
14363 finish_stmt (void)
14364 {
14365   /* Always assume this statement was not an expression statement.  If
14366      it actually was an expression statement, its our callers
14367      responsibility to fix this up.  */
14368   last_expr_type = NULL_TREE;
14369 }
14370
14371 /* DECL was originally constructed as a non-static member function,
14372    but turned out to be static.  Update it accordingly.  */
14373
14374 void
14375 revert_static_member_fn (tree decl)
14376 {
14377   tree tmp;
14378   tree function = TREE_TYPE (decl);
14379   tree args = TYPE_ARG_TYPES (function);
14380
14381   if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14382       != TYPE_UNQUALIFIED)
14383     error ("static member function `%#D' declared with type qualifiers",
14384               decl);
14385
14386   args = TREE_CHAIN (args);
14387   tmp = build_function_type (TREE_TYPE (function), args);
14388   tmp = build_qualified_type (tmp, cp_type_quals (function));
14389   tmp = build_exception_variant (tmp,
14390                                  TYPE_RAISES_EXCEPTIONS (function));
14391   TREE_TYPE (decl) = tmp;
14392   if (DECL_ARGUMENTS (decl))
14393     DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14394   DECL_STATIC_FUNCTION_P (decl) = 1;
14395 }
14396
14397 /* Initialize the variables used during compilation of a C++
14398    function.  */
14399
14400 void
14401 cxx_push_function_context (struct function * f)
14402 {
14403   struct language_function *p
14404     = ((struct language_function *)
14405        ggc_alloc_cleared (sizeof (struct language_function)));
14406   f->language = p;
14407
14408   /* It takes an explicit call to expand_body to generate RTL for a
14409      function.  */
14410   expanding_p = 0;
14411
14412   /* Whenever we start a new function, we destroy temporaries in the
14413      usual way.  */
14414   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14415 }
14416
14417 /* Free the language-specific parts of F, now that we've finished
14418    compiling the function.  */
14419
14420 void
14421 cxx_pop_function_context (struct function * f)
14422 {
14423   f->language = 0;
14424 }
14425
14426 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14427    one of the language-independent trees.  */
14428
14429 enum cp_tree_node_structure_enum
14430 cp_tree_node_structure (union lang_tree_node * t)
14431 {
14432   switch (TREE_CODE (&t->generic))
14433     {
14434     case DEFAULT_ARG:           return TS_CP_DEFAULT_ARG;
14435     case IDENTIFIER_NODE:       return TS_CP_IDENTIFIER;
14436     case OVERLOAD:              return TS_CP_OVERLOAD;
14437     case TEMPLATE_PARM_INDEX:   return TS_CP_TPI;
14438     case PTRMEM_CST:            return TS_CP_PTRMEM;
14439     case BASELINK:              return TS_CP_BASELINK;
14440     case WRAPPER:               return TS_CP_WRAPPER;
14441     default:                    return TS_CP_GENERIC;
14442     }
14443 }
14444
14445 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14446    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
14447
14448 tree
14449 identifier_global_value (tree t)
14450 {
14451   return IDENTIFIER_GLOBAL_VALUE (t);
14452 }
14453
14454 /* Build the void_list_node (void_type_node having been created).  */
14455 tree
14456 build_void_list_node (void)
14457 {
14458   tree t = build_tree_list (NULL_TREE, void_type_node);
14459   TREE_PARMLIST (t) = 1;
14460   return t;
14461 }
14462
14463 static int
14464 cp_missing_noreturn_ok_p (tree decl)
14465 {
14466   /* A missing noreturn is ok for the `main' function.  */
14467   return DECL_MAIN_P (decl);
14468 }
14469
14470 #include "gt-cp-decl.h"
14471 #include "gtype-cp.h"