OSDN Git Service

2003-03-02 Aldy Hernandez <aldyh@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / decl.c
1 /* Process declarations and variables for C++ compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003  Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* Process declarations and symbol lookup for C++ front end.
25    Also constructs types; the standard scalar types at initialization,
26    and structure, union, array and enum types when they are declared.  */
27
28 /* ??? not all decl nodes are given the most useful possible
29    line numbers.  For example, the CONST_DECLs for enum values.  */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "tree.h"
36 #include "rtl.h"
37 #include "expr.h"
38 #include "flags.h"
39 #include "cp-tree.h"
40 #include "tree-inline.h"
41 #include "decl.h"
42 #include "lex.h"
43 #include "output.h"
44 #include "except.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "tm_p.h"
48 #include "target.h"
49 #include "c-common.h"
50 #include "c-pragma.h"
51 #include "diagnostic.h"
52 #include "debug.h"
53 #include "timevar.h"
54
55 static tree grokparms (tree);
56 static const char *redeclaration_error_message (tree, tree);
57
58 static void push_binding_level (struct cp_binding_level *, int,
59                                 int);
60 static void pop_binding_level (void);
61 static void suspend_binding_level (void);
62 static void resume_binding_level (struct cp_binding_level *);
63 static struct cp_binding_level *make_binding_level (void);
64 static void declare_namespace_level (void);
65 static int decl_jump_unsafe (tree);
66 static void storedecls (tree);
67 static void require_complete_types_for_parms (tree);
68 static int ambi_op_p (enum tree_code);
69 static int unary_op_p (enum tree_code);
70 static tree store_bindings (tree, tree);
71 static tree lookup_tag_reverse (tree, tree);
72 static void push_local_name (tree);
73 static void warn_extern_redeclared_static (tree, tree);
74 static tree grok_reference_init (tree, tree, tree);
75 static tree grokfndecl (tree, tree, tree, tree, int,
76                         enum overload_flags, tree,
77                         tree, int, int, int, int, int, int, tree);
78 static tree grokvardecl (tree, tree, RID_BIT_TYPE *, int, int, tree);
79 static tree follow_tag_typedef (tree);
80 static tree lookup_tag (enum tree_code, tree,
81                         struct cp_binding_level *, int);
82 static void set_identifier_type_value_with_scope
83         (tree, tree, struct cp_binding_level *);
84 static void record_unknown_type (tree, const char *);
85 static tree builtin_function_1 (const char *, tree, tree, int,
86                                 enum built_in_class, const char *,
87                                 tree);
88 static tree build_library_fn_1 (tree, enum tree_code, tree);
89 static int member_function_or_else (tree, tree, enum overload_flags);
90 static void bad_specifiers (tree, const char *, int, int, int, int,
91                             int);
92 static tree maybe_process_template_type_declaration 
93         (tree, int, struct cp_binding_level*);
94 static void check_for_uninitialized_const_var (tree);
95 static hashval_t typename_hash (const void *);
96 static int typename_compare (const void *, const void *);
97 static void push_binding (tree, tree, struct cp_binding_level*);
98 static int add_binding (tree, tree);
99 static void pop_binding (tree, tree);
100 static tree local_variable_p_walkfn (tree *, int *, void *);
101 static tree find_binding (tree, tree);
102 static tree select_decl (tree, int);
103 static int lookup_flags (int, int);
104 static tree qualify_lookup (tree, int);
105 static tree record_builtin_java_type (const char *, int);
106 static const char *tag_name (enum tag_types code);
107 static void find_class_binding_level (void);
108 static struct cp_binding_level *innermost_nonclass_level (void);
109 static int walk_namespaces_r (tree, walk_namespaces_fn, void *);
110 static int walk_globals_r (tree, void*);
111 static int walk_vtables_r (tree, void*);
112 static void add_decl_to_level (tree, struct cp_binding_level *);
113 static tree make_label_decl (tree, int);
114 static void use_label (tree);
115 static void check_previous_goto_1 (tree, struct cp_binding_level *, tree,
116                                    const char *, int);
117 static void check_previous_goto (struct named_label_use_list *);
118 static void check_switch_goto (struct cp_binding_level *);
119 static void check_previous_gotos (tree);
120 static void pop_label (tree, tree);
121 static void pop_labels (tree);
122 static void maybe_deduce_size_from_array_init (tree, tree);
123 static void layout_var_decl (tree);
124 static void maybe_commonize_var (tree);
125 static tree check_initializer (tree, tree, int);
126 static void make_rtl_for_nonlocal_decl (tree, tree, const char *);
127 static void save_function_data (tree);
128 static void check_function_type (tree, tree);
129 static void begin_constructor_body (void);
130 static void finish_constructor_body (void);
131 static void begin_destructor_body (void);
132 static void finish_destructor_body (void);
133 static tree create_array_type_for_decl (tree, tree, tree);
134 static tree get_atexit_node (void);
135 static tree get_dso_handle_node (void);
136 static tree start_cleanup_fn (void);
137 static void end_cleanup_fn (void);
138 static tree cp_make_fname_decl (tree, int);
139 static void initialize_predefined_identifiers (void);
140 static tree check_special_function_return_type 
141         (special_function_kind, tree, tree);
142 static tree push_cp_library_fn (enum tree_code, tree);
143 static tree build_cp_library_fn (tree, enum tree_code, tree);
144 static void store_parm_decls (tree);
145 static int cp_missing_noreturn_ok_p (tree);
146 static void initialize_local_var (tree, tree);
147 static void expand_static_init (tree, tree);
148 static tree next_initializable_field (tree);
149 static tree reshape_init (tree, tree *);
150 static tree build_typename_type (tree, tree, tree);
151
152 #if defined (DEBUG_BINDING_LEVELS)
153 static void indent (void);
154 #endif
155
156 /* Erroneous argument lists can use this *IFF* they do not modify it.  */
157 tree error_mark_list;
158
159 /* The following symbols are subsumed in the cp_global_trees array, and
160    listed here individually for documentation purposes.
161
162    C++ extensions
163         tree wchar_decl_node;
164
165         tree vtable_entry_type;
166         tree delta_type_node;
167         tree __t_desc_type_node;
168         tree ti_desc_type_node;
169         tree bltn_desc_type_node, ptr_desc_type_node;
170         tree ary_desc_type_node, func_desc_type_node, enum_desc_type_node;
171         tree class_desc_type_node, si_class_desc_type_node, vmi_class_desc_type_node;
172         tree ptm_desc_type_node;
173         tree base_desc_type_node;
174
175         tree class_type_node, record_type_node, union_type_node, enum_type_node;
176         tree unknown_type_node;
177
178    Array type `vtable_entry_type[]'
179
180         tree vtbl_type_node;
181         tree vtbl_ptr_type_node;
182
183    Namespaces,
184
185         tree std_node;
186         tree abi_node;
187
188    A FUNCTION_DECL which can call `abort'.  Not necessarily the
189    one that the user will declare, but sufficient to be called
190    by routines that want to abort the program.
191
192         tree abort_fndecl;
193
194    The FUNCTION_DECL for the default `::operator delete'.
195
196         tree global_delete_fndecl;
197
198    Used by RTTI
199         tree type_info_type_node, tinfo_decl_id, tinfo_decl_type;
200         tree tinfo_var_id;
201
202 */
203
204 tree cp_global_trees[CPTI_MAX];
205
206 /* Indicates that there is a type value in some namespace, although
207    that is not necessarily in scope at the moment.  */
208
209 static GTY(()) tree global_type_node;
210
211 /* Used only for jumps to as-yet undefined labels, since jumps to
212    defined labels can have their validity checked immediately.  */
213
214 struct named_label_use_list GTY(())
215 {
216   struct cp_binding_level *binding_level;
217   tree names_in_scope;
218   tree label_decl;
219   const char *filename_o_goto;
220   int lineno_o_goto;
221   struct named_label_use_list *next;
222 };
223
224 #define named_label_uses cp_function_chain->x_named_label_uses
225
226 #define local_names cp_function_chain->x_local_names
227
228 /* A list of objects which have constructors or destructors
229    which reside in the global scope.  The decl is stored in
230    the TREE_VALUE slot and the initializer is stored
231    in the TREE_PURPOSE slot.  */
232 tree static_aggregates;
233
234 /* -- end of C++ */
235
236 /* A node for the integer constants 2, and 3.  */
237
238 tree integer_two_node, integer_three_node;
239
240 /* Similar, for last_function_parm_tags.  */
241 tree last_function_parms;
242
243 /* A list of all LABEL_DECLs in the function that have names.  Here so
244    we can clear out their names' definitions at the end of the
245    function, and so we can check the validity of jumps to these labels.  */
246
247 struct named_label_list GTY(())
248 {
249   struct cp_binding_level *binding_level;
250   tree names_in_scope;
251   tree old_value;
252   tree label_decl;
253   tree bad_decls;
254   struct named_label_list *next;
255   unsigned int in_try_scope : 1;
256   unsigned int in_catch_scope : 1;
257 };
258
259 #define named_labels cp_function_chain->x_named_labels
260 \f
261 /* The name of the anonymous namespace, throughout this translation
262    unit.  */
263 tree anonymous_namespace_name;
264
265 /* The number of function bodies which we are currently processing.
266    (Zero if we are at namespace scope, one inside the body of a
267    function, two inside the body of a function in a local class, etc.)  */
268 int function_depth;
269
270 /* States indicating how grokdeclarator() should handle declspecs marked
271    with __attribute__((deprecated)).  An object declared as
272    __attribute__((deprecated)) suppresses warnings of uses of other
273    deprecated items.  */
274    
275 enum deprecated_states {
276   DEPRECATED_NORMAL,
277   DEPRECATED_SUPPRESS
278 };
279
280 static enum deprecated_states deprecated_state = DEPRECATED_NORMAL;
281
282 /* Set by add_implicitly_declared_members() to keep those members from
283    being flagged as deprecated or reported as using deprecated
284    types.  */
285 int adding_implicit_members = 0;
286
287 /* True if a declaration with an `extern' linkage specifier is being
288    processed.  */
289 bool have_extern_spec;
290
291 \f
292 /* For each binding contour we allocate a binding_level structure
293    which records the names defined in that contour.
294    Contours include:
295     0) the global one
296     1) one for each function definition,
297        where internal declarations of the parameters appear.
298     2) one for each compound statement,
299        to record its declarations.
300
301    The current meaning of a name can be found by searching the levels
302    from the current one out to the global one.
303
304    Off to the side, may be the class_binding_level.  This exists only
305    to catch class-local declarations.  It is otherwise nonexistent.
306
307    Also there may be binding levels that catch cleanups that must be
308    run when exceptions occur.  Thus, to see whether a name is bound in
309    the current scope, it is not enough to look in the
310    CURRENT_BINDING_LEVEL.  You should use lookup_name_current_level
311    instead.  */
312
313 /* Note that the information in the `names' component of the global contour
314    is duplicated in the IDENTIFIER_GLOBAL_VALUEs of all identifiers.  */
315
316 struct cp_binding_level GTY(())
317   {
318     /* A chain of _DECL nodes for all variables, constants, functions,
319        and typedef types.  These are in the reverse of the order
320        supplied.  There may be OVERLOADs on this list, too, but they
321        are wrapped in TREE_LISTs; the TREE_VALUE is the OVERLOAD.  */
322     tree names;
323
324     /* Count of elements in names chain.  */
325     size_t names_size;
326
327     /* A chain of NAMESPACE_DECL nodes.  */
328     tree namespaces;
329
330     /* A chain of VTABLE_DECL nodes.  */
331     tree vtables; 
332
333     /* A list of structure, union and enum definitions, for looking up
334        tag names.
335        It is a chain of TREE_LIST nodes, each of whose TREE_PURPOSE is a name,
336        or NULL_TREE; and whose TREE_VALUE is a RECORD_TYPE, UNION_TYPE,
337        or ENUMERAL_TYPE node.
338
339        C++: the TREE_VALUE nodes can be simple types for
340        component_bindings.  */
341     tree tags;
342
343     /* A list of USING_DECL nodes.  */
344     tree usings;
345
346     /* A list of used namespaces. PURPOSE is the namespace,
347        VALUE the common ancestor with this binding_level's namespace.  */
348     tree using_directives;
349
350     /* If this binding level is the binding level for a class, then
351        class_shadowed is a TREE_LIST.  The TREE_PURPOSE of each node
352        is the name of an entity bound in the class.  The TREE_TYPE is
353        the DECL bound by this name in the class.  */
354     tree class_shadowed;
355
356     /* Similar to class_shadowed, but for IDENTIFIER_TYPE_VALUE, and
357        is used for all binding levels. In addition the TREE_VALUE is the
358        IDENTIFIER_TYPE_VALUE before we entered the class.  */
359     tree type_shadowed;
360
361     /* A TREE_LIST.  Each TREE_VALUE is the LABEL_DECL for a local
362        label in this scope.  The TREE_PURPOSE is the previous value of
363        the IDENTIFIER_LABEL VALUE.  */
364     tree shadowed_labels;
365
366     /* For each level (except not the global one),
367        a chain of BLOCK nodes for all the levels
368        that were entered and exited one level down.  */
369     tree blocks;
370
371     /* The _TYPE node for this level, if parm_flag == 2.  */
372     tree this_class;
373
374     /* The binding level which this one is contained in (inherits from).  */
375     struct cp_binding_level *level_chain;
376
377     /* List of VAR_DECLS saved from a previous for statement.
378        These would be dead in ISO-conforming code, but might
379        be referenced in ARM-era code.  These are stored in a
380        TREE_LIST; the TREE_VALUE is the actual declaration.  */
381     tree dead_vars_from_for;
382
383     /* 1 for the level that holds the parameters of a function.
384        2 for the level that holds a class declaration.  */
385     unsigned parm_flag : 2;
386
387     /* 1 means make a BLOCK for this level regardless of all else.
388        2 for temporary binding contours created by the compiler.  */
389     unsigned keep : 2;
390
391     /* Nonzero if this level "doesn't exist" for tags.  */
392     unsigned tag_transparent : 1;
393
394     /* Nonzero if this level can safely have additional
395        cleanup-needing variables added to it.  */
396     unsigned more_cleanups_ok : 1;
397     unsigned have_cleanups : 1;
398
399     /* Nonzero if this scope is for storing the decls for template
400        parameters and generic decls; these decls will be discarded and
401        replaced with a TEMPLATE_DECL.  */
402     unsigned template_parms_p : 1;
403
404     /* Nonzero if this scope corresponds to the `<>' in a
405        `template <>' clause.  Whenever this flag is set,
406        TEMPLATE_PARMS_P will be set as well.  */
407     unsigned template_spec_p : 1;
408
409     /* This is set for a namespace binding level.  */
410     unsigned namespace_p : 1;
411
412     /* True if this level is that of a for-statement where we need to
413        worry about ambiguous (ARM or ISO) scope rules.  */
414     unsigned is_for_scope : 1;
415
416     /* True if this level corresponds to a TRY block.  Currently this
417        information is only available while building the tree structure.  */
418     unsigned is_try_scope : 1;
419
420     /* True if this level corresponds to a CATCH block.  Currently this
421        information is only available while building the tree structure.  */
422     unsigned is_catch_scope : 1;
423
424     /* Three bits left for this word.  */
425
426     /* Binding depth at which this level began.  */
427     unsigned binding_depth;
428   };
429
430 #define NULL_BINDING_LEVEL ((struct cp_binding_level *) NULL)
431
432 /* The binding level currently in effect.  */
433
434 #define current_binding_level                   \
435   (cfun && cp_function_chain->bindings          \
436    ? cp_function_chain->bindings                \
437    : scope_chain->bindings)
438
439 /* The binding level of the current class, if any.  */
440
441 #define class_binding_level scope_chain->class_bindings
442
443 /* A chain of binding_level structures awaiting reuse.  */
444
445 static GTY((deletable (""))) struct cp_binding_level *free_binding_level;
446
447 /* The outermost binding level, for names of file scope.
448    This is created when the compiler is started and exists
449    through the entire run.  */
450
451 static GTY(()) struct cp_binding_level *global_binding_level;
452
453 /* Nonzero means unconditionally make a BLOCK for the next level pushed.  */
454
455 static int keep_next_level_flag;
456
457 /* A TREE_LIST of VAR_DECLs.  The TREE_PURPOSE is a RECORD_TYPE or
458    UNION_TYPE; the TREE_VALUE is a VAR_DECL with that type.  At the
459    time the VAR_DECL was declared, the type was incomplete.  */
460
461 static GTY(()) tree incomplete_vars;
462
463 #if defined(DEBUG_BINDING_LEVELS)
464 static int binding_depth = 0;
465 static int is_class_level = 0;
466
467 static void
468 indent (void)
469 {
470   register unsigned i;
471
472   for (i = 0; i < binding_depth*2; i++)
473     putc (' ', stderr);
474 }
475 #endif /* defined(DEBUG_BINDING_LEVELS) */
476
477 static tree pushdecl_with_scope (tree, struct cp_binding_level *);
478
479 static void
480 push_binding_level (struct cp_binding_level *newlevel, 
481                     int tag_transparent, 
482                     int keep)
483 {
484   /* Add this level to the front of the chain (stack) of levels that
485      are active.  */
486   memset ((char*) newlevel, 0, sizeof (struct cp_binding_level));
487   newlevel->level_chain = current_binding_level;
488   current_binding_level = newlevel;
489   newlevel->tag_transparent = tag_transparent;
490   newlevel->more_cleanups_ok = 1;
491
492   newlevel->keep = keep;
493 #if defined(DEBUG_BINDING_LEVELS)
494   newlevel->binding_depth = binding_depth;
495   indent ();
496   fprintf (stderr, "push %s level 0x%08x line %d\n",
497            (is_class_level) ? "class" : "block", newlevel, lineno);
498   is_class_level = 0;
499   binding_depth++;
500 #endif /* defined(DEBUG_BINDING_LEVELS) */
501 }
502
503 /* Find the innermost enclosing class scope, and reset
504    CLASS_BINDING_LEVEL appropriately.  */
505
506 static void
507 find_class_binding_level (void)
508 {
509   struct cp_binding_level *level = current_binding_level;
510
511   while (level && level->parm_flag != 2)
512     level = level->level_chain;
513   if (level && level->parm_flag == 2)
514     class_binding_level = level;
515   else
516     class_binding_level = 0;
517 }
518
519 static void
520 pop_binding_level (void)
521 {
522   if (global_binding_level)
523     {
524       /* Cannot pop a level, if there are none left to pop.  */
525       if (current_binding_level == global_binding_level)
526         abort ();
527     }
528   /* Pop the current level, and free the structure for reuse.  */
529 #if defined(DEBUG_BINDING_LEVELS)
530   binding_depth--;
531   indent ();
532   fprintf (stderr, "pop  %s level 0x%08x line %d\n",
533           (is_class_level) ? "class" : "block",
534           current_binding_level, lineno);
535   if (is_class_level != (current_binding_level == class_binding_level))
536     {
537       indent ();
538       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
539     }
540   is_class_level = 0;
541 #endif /* defined(DEBUG_BINDING_LEVELS) */
542   {
543     register struct cp_binding_level *level = current_binding_level;
544     current_binding_level = current_binding_level->level_chain;
545     level->level_chain = free_binding_level;
546 #if 0 /* defined(DEBUG_BINDING_LEVELS) */
547     if (level->binding_depth != binding_depth)
548       abort ();
549 #endif /* defined(DEBUG_BINDING_LEVELS) */
550     free_binding_level = level;
551     find_class_binding_level ();
552   }
553 }
554
555 static void
556 suspend_binding_level (void)
557 {
558   if (class_binding_level)
559     current_binding_level = class_binding_level;
560
561   if (global_binding_level)
562     {
563       /* Cannot suspend a level, if there are none left to suspend.  */
564       if (current_binding_level == global_binding_level)
565         abort ();
566     }
567   /* Suspend the current level.  */
568 #if defined(DEBUG_BINDING_LEVELS)
569   binding_depth--;
570   indent ();
571   fprintf (stderr, "suspend  %s level 0x%08x line %d\n",
572           (is_class_level) ? "class" : "block",
573           current_binding_level, lineno);
574   if (is_class_level != (current_binding_level == class_binding_level))
575     {
576       indent ();
577       fprintf (stderr, "XXX is_class_level != (current_binding_level == class_binding_level)\n");
578     }
579   is_class_level = 0;
580 #endif /* defined(DEBUG_BINDING_LEVELS) */
581   current_binding_level = current_binding_level->level_chain;
582   find_class_binding_level ();
583 }
584
585 static void
586 resume_binding_level (struct cp_binding_level* b)
587 {
588   /* Resuming binding levels is meant only for namespaces,
589      and those cannot nest into classes.  */
590   my_friendly_assert(!class_binding_level, 386);
591   /* Also, resuming a non-directly nested namespace is a no-no.  */
592   my_friendly_assert(b->level_chain == current_binding_level, 386);
593   current_binding_level = b;
594 #if defined(DEBUG_BINDING_LEVELS)
595   b->binding_depth = binding_depth;
596   indent ();
597   fprintf (stderr, "resume %s level 0x%08x line %d\n",
598            (is_class_level) ? "class" : "block", b, lineno);
599   is_class_level = 0;
600   binding_depth++;
601 #endif /* defined(DEBUG_BINDING_LEVELS) */
602 }
603 \f
604 /* Create a new `struct cp_binding_level'.  */
605
606 static
607 struct cp_binding_level *
608 make_binding_level (void)
609 {
610   /* NOSTRICT */
611   return (struct cp_binding_level *) ggc_alloc (sizeof (struct cp_binding_level));
612 }
613
614 /* Nonzero if we are currently in the global binding level.  */
615
616 int
617 global_bindings_p (void)
618 {
619   return current_binding_level == global_binding_level;
620 }
621
622 /* Return the innermost binding level that is not for a class scope.  */
623
624 static struct cp_binding_level *
625 innermost_nonclass_level (void)
626 {
627   struct cp_binding_level *b;
628
629   b = current_binding_level;
630   while (b->parm_flag == 2)
631     b = b->level_chain;
632
633   return b;
634 }
635
636 /* Nonzero if we are currently in a toplevel binding level.  This
637    means either the global binding level or a namespace in a toplevel
638    binding level.  Since there are no non-toplevel namespace levels,
639    this really means any namespace or template parameter level.  We
640    also include a class whose context is toplevel.  */
641
642 int
643 toplevel_bindings_p (void)
644 {
645   struct cp_binding_level *b = innermost_nonclass_level ();
646
647   return b->namespace_p || b->template_parms_p;
648 }
649
650 /* Nonzero if this is a namespace scope, or if we are defining a class
651    which is itself at namespace scope, or whose enclosing class is
652    such a class, etc.  */
653
654 int
655 namespace_bindings_p (void)
656 {
657   struct cp_binding_level *b = innermost_nonclass_level ();
658
659   return b->namespace_p;
660 }
661
662 /* If KEEP is nonzero, make a BLOCK node for the next binding level,
663    unconditionally.  Otherwise, use the normal logic to decide whether
664    or not to create a BLOCK.  */
665
666 void
667 keep_next_level (int keep)
668 {
669   keep_next_level_flag = keep;
670 }
671
672 /* Nonzero if the current level needs to have a BLOCK made.  */
673
674 int
675 kept_level_p (void)
676 {
677   return (current_binding_level->blocks != NULL_TREE
678           || current_binding_level->keep
679           || current_binding_level->names != NULL_TREE
680           || (current_binding_level->tags != NULL_TREE
681               && !current_binding_level->tag_transparent));
682 }
683
684 static void
685 declare_namespace_level (void)
686 {
687   current_binding_level->namespace_p = 1;
688 }
689
690 /* Returns nonzero if this scope was created to store template
691    parameters.  */
692
693 int
694 template_parm_scope_p (void)
695 {
696   return current_binding_level->template_parms_p;
697 }
698
699 /* Returns the kind of template specialization we are currently
700    processing, given that it's declaration contained N_CLASS_SCOPES
701    explicit scope qualifications.  */
702
703 tmpl_spec_kind
704 current_tmpl_spec_kind (int n_class_scopes)
705 {
706   int n_template_parm_scopes = 0;
707   int seen_specialization_p = 0;
708   int innermost_specialization_p = 0;
709   struct cp_binding_level *b;
710
711   /* Scan through the template parameter scopes.  */
712   for (b = current_binding_level; b->template_parms_p; b = b->level_chain)
713     {
714       /* If we see a specialization scope inside a parameter scope,
715          then something is wrong.  That corresponds to a declaration
716          like:
717
718             template <class T> template <> ...
719
720          which is always invalid since [temp.expl.spec] forbids the
721          specialization of a class member template if the enclosing
722          class templates are not explicitly specialized as well.  */
723       if (b->template_spec_p)
724         {
725           if (n_template_parm_scopes == 0)
726             innermost_specialization_p = 1;
727           else
728             seen_specialization_p = 1;
729         }
730       else if (seen_specialization_p == 1)
731         return tsk_invalid_member_spec;
732
733       ++n_template_parm_scopes;
734     }
735
736   /* Handle explicit instantiations.  */
737   if (processing_explicit_instantiation)
738     {
739       if (n_template_parm_scopes != 0)
740         /* We've seen a template parameter list during an explicit
741            instantiation.  For example:
742
743              template <class T> template void f(int);
744
745            This is erroneous.  */
746         return tsk_invalid_expl_inst;
747       else
748         return tsk_expl_inst;
749     }
750
751   if (n_template_parm_scopes < n_class_scopes)
752     /* We've not seen enough template headers to match all the
753        specialized classes present.  For example:
754
755          template <class T> void R<T>::S<T>::f(int);
756
757        This is invalid; there needs to be one set of template
758        parameters for each class.  */
759     return tsk_insufficient_parms;
760   else if (n_template_parm_scopes == n_class_scopes)
761     /* We're processing a non-template declaration (even though it may
762        be a member of a template class.)  For example:
763
764          template <class T> void S<T>::f(int);
765
766        The `class T' maches the `S<T>', leaving no template headers
767        corresponding to the `f'.  */
768     return tsk_none;
769   else if (n_template_parm_scopes > n_class_scopes + 1)
770     /* We've got too many template headers.  For example:
771
772          template <> template <class T> void f (T);
773
774        There need to be more enclosing classes.  */
775     return tsk_excessive_parms;
776   else
777     /* This must be a template.  It's of the form:
778
779          template <class T> template <class U> void S<T>::f(U);
780
781        This is a specialization if the innermost level was a
782        specialization; otherwise it's just a definition of the
783        template.  */
784     return innermost_specialization_p ? tsk_expl_spec : tsk_template;
785 }
786
787 void
788 set_class_shadows (tree shadows)
789 {
790   class_binding_level->class_shadowed = shadows;
791 }
792
793 /* Enter a new binding level.
794    If TAG_TRANSPARENT is nonzero, do so only for the name space of variables,
795    not for that of tags.  */
796
797 void
798 pushlevel (int tag_transparent)
799 {
800   struct cp_binding_level *newlevel;
801
802   if (cfun && !doing_semantic_analysis_p ())
803     return;
804
805   /* Reuse or create a struct for this binding level.  */
806 #if defined(DEBUG_BINDING_LEVELS)
807   if (0)
808 #else /* !defined(DEBUG_BINDING_LEVELS) */
809   if (free_binding_level)
810 #endif /* !defined(DEBUG_BINDING_LEVELS) */
811     {
812       newlevel = free_binding_level;
813       free_binding_level = free_binding_level->level_chain;
814     }
815   else
816     newlevel = make_binding_level ();
817
818   push_binding_level (newlevel, tag_transparent, keep_next_level_flag);
819   keep_next_level_flag = 0;
820 }
821
822 /* We're defining an object of type TYPE.  If it needs a cleanup, but
823    we're not allowed to add any more objects with cleanups to the current
824    scope, create a new binding level.  */
825
826 void
827 maybe_push_cleanup_level (tree type)
828 {
829   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
830       && current_binding_level->more_cleanups_ok == 0)
831     {
832       keep_next_level (2);
833       pushlevel (1);
834       clear_last_expr ();
835       add_scope_stmt (/*begin_p=*/1, /*partial_p=*/1);
836     }
837 }
838   
839 /* Enter a new scope.  The KIND indicates what kind of scope is being
840    created.  */
841
842 void
843 begin_scope (scope_kind sk)
844 {
845   pushlevel (0);
846
847   switch (sk)
848     {
849     case sk_block:
850       break;
851
852     case sk_try:
853       current_binding_level->is_try_scope = 1;
854       break;
855
856     case sk_catch:
857       current_binding_level->is_catch_scope = 1;
858       break;
859
860     case sk_for:
861       current_binding_level->is_for_scope = 1;
862       break;
863
864     case sk_template_spec:
865       current_binding_level->template_spec_p = 1;
866       /* Fall through.  */
867
868     case sk_template_parms:
869       current_binding_level->template_parms_p = 1;
870       break;
871
872     default:
873       abort ();
874     }
875 }
876
877 /* Exit the current scope.  */
878
879 void
880 finish_scope (void)
881 {
882   poplevel (0, 0, 0);
883 }
884
885 /* For a binding between a name and an entity at a block scope,
886    this is the `struct cp_binding_level' for the block.  */
887 #define BINDING_LEVEL(NODE) \
888   (((struct tree_binding*)(NODE))->scope.level)
889
890 /* A free list of CPLUS_BINDING nodes, connected by their
891    TREE_CHAINs.  */
892
893 static GTY((deletable (""))) tree free_bindings;
894
895 /* Make DECL the innermost binding for ID.  The LEVEL is the binding
896    level at which this declaration is being bound.  */
897
898 static void
899 push_binding (tree id, 
900               tree decl,
901               struct cp_binding_level* level)
902 {
903   tree binding;
904
905   if (free_bindings)
906     {
907       binding = free_bindings;
908       free_bindings = TREE_CHAIN (binding);
909     }
910   else
911     binding = make_node (CPLUS_BINDING);
912
913   /* Now, fill in the binding information.  */
914   BINDING_VALUE (binding) = decl;
915   BINDING_TYPE (binding) = NULL_TREE;
916   BINDING_LEVEL (binding) = level;
917   INHERITED_VALUE_BINDING_P (binding) = 0;
918   LOCAL_BINDING_P (binding) = (level != class_binding_level);
919   BINDING_HAS_LEVEL_P (binding) = 1;
920
921   /* And put it on the front of the list of bindings for ID.  */
922   TREE_CHAIN (binding) = IDENTIFIER_BINDING (id);
923   IDENTIFIER_BINDING (id) = binding;
924 }
925
926 /* ID is already bound in the current scope.  But, DECL is an
927    additional binding for ID in the same scope.  This is the `struct
928    stat' hack whereby a non-typedef class-name or enum-name can be
929    bound at the same level as some other kind of entity.  It's the
930    responsibility of the caller to check that inserting this name is
931    valid here.  Returns nonzero if the new binding was successful.  */
932 static int
933 add_binding (tree id, tree decl)
934 {
935   tree binding = IDENTIFIER_BINDING (id);
936   int ok = 1;
937
938   timevar_push (TV_NAME_LOOKUP);
939   if (TREE_CODE (decl) == TYPE_DECL && DECL_ARTIFICIAL (decl))
940     /* The new name is the type name.  */
941     BINDING_TYPE (binding) = decl;
942   else if (!BINDING_VALUE (binding))
943     /* This situation arises when push_class_level_binding moves an
944        inherited type-binding out of the way to make room for a new
945        value binding.  */
946     BINDING_VALUE (binding) = decl;
947   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
948            && DECL_ARTIFICIAL (BINDING_VALUE (binding)))
949     {
950       /* The old binding was a type name.  It was placed in
951          BINDING_VALUE because it was thought, at the point it was
952          declared, to be the only entity with such a name.  Move the
953          type name into the type slot; it is now hidden by the new
954          binding.  */
955       BINDING_TYPE (binding) = BINDING_VALUE (binding);
956       BINDING_VALUE (binding) = decl;
957       INHERITED_VALUE_BINDING_P (binding) = 0;
958     }
959   else if (TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
960            && TREE_CODE (decl) == TYPE_DECL
961            && DECL_NAME (decl) == DECL_NAME (BINDING_VALUE (binding))
962            && (same_type_p (TREE_TYPE (decl),
963                             TREE_TYPE (BINDING_VALUE (binding)))
964                /* If either type involves template parameters, we must
965                   wait until instantiation.  */
966                || uses_template_parms (TREE_TYPE (decl))
967                || uses_template_parms (TREE_TYPE (BINDING_VALUE (binding)))))
968     /* We have two typedef-names, both naming the same type to have
969        the same name.  This is OK because of:
970
971          [dcl.typedef]
972
973          In a given scope, a typedef specifier can be used to redefine
974          the name of any type declared in that scope to refer to the
975          type to which it already refers.  */
976     ok = 0;
977   /* There can be two block-scope declarations of the same variable,
978      so long as they are `extern' declarations.  */
979   else if (TREE_CODE (decl) == VAR_DECL
980            && TREE_CODE (BINDING_VALUE (binding)) == VAR_DECL
981            && DECL_EXTERNAL (decl)
982            && DECL_EXTERNAL (BINDING_VALUE (binding)))
983     {
984       duplicate_decls (decl, BINDING_VALUE (binding));
985       ok = 0;
986     }
987   else
988     {
989       error ("declaration of `%#D'", decl);
990       cp_error_at ("conflicts with previous declaration `%#D'",
991                    BINDING_VALUE (binding));
992       ok = 0;
993     }
994
995   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ok);
996 }
997
998 /* Add DECL to the list of things declared in B.  */
999
1000 static void
1001 add_decl_to_level (tree decl, 
1002                    struct cp_binding_level* b)
1003 {
1004   if (TREE_CODE (decl) == NAMESPACE_DECL 
1005       && !DECL_NAMESPACE_ALIAS (decl))
1006     {
1007       TREE_CHAIN (decl) = b->namespaces;
1008       b->namespaces = decl;
1009     }
1010   else if (TREE_CODE (decl) == VAR_DECL && DECL_VIRTUAL_P (decl))
1011     {
1012       TREE_CHAIN (decl) = b->vtables;
1013       b->vtables = decl;
1014     }
1015   else       
1016     {
1017       /* We build up the list in reverse order, and reverse it later if
1018          necessary.  */
1019       TREE_CHAIN (decl) = b->names;
1020       b->names = decl;
1021       b->names_size++;
1022     }
1023 }
1024
1025 /* Bind DECL to ID in the current_binding_level, assumed to be a local
1026    binding level.  If PUSH_USING is set in FLAGS, we know that DECL
1027    doesn't really belong to this binding level, that it got here
1028    through a using-declaration.  */
1029
1030 void
1031 push_local_binding (tree id, tree decl, int flags)
1032 {
1033   struct cp_binding_level *b;
1034
1035   /* Skip over any local classes.  This makes sense if we call
1036      push_local_binding with a friend decl of a local class.  */
1037   b = current_binding_level;
1038   while (b->parm_flag == 2)
1039     b = b->level_chain;
1040
1041   if (lookup_name_current_level (id))
1042     {
1043       /* Supplement the existing binding.  */
1044       if (!add_binding (id, decl))
1045         /* It didn't work.  Something else must be bound at this
1046            level.  Do not add DECL to the list of things to pop
1047            later.  */
1048         return;
1049     }
1050   else
1051     /* Create a new binding.  */
1052     push_binding (id, decl, b);
1053
1054   if (TREE_CODE (decl) == OVERLOAD || (flags & PUSH_USING))
1055     /* We must put the OVERLOAD into a TREE_LIST since the
1056        TREE_CHAIN of an OVERLOAD is already used.  Similarly for
1057        decls that got here through a using-declaration.  */
1058     decl = build_tree_list (NULL_TREE, decl);
1059
1060   /* And put DECL on the list of things declared by the current
1061      binding level.  */
1062   add_decl_to_level (decl, b);
1063 }
1064
1065 /* Bind DECL to ID in the class_binding_level.  Returns nonzero if the
1066    binding was successful.  */
1067
1068 int
1069 push_class_binding (tree id, tree decl)
1070 {
1071   int result = 1;
1072   tree binding = IDENTIFIER_BINDING (id);
1073   tree context;
1074
1075   timevar_push (TV_NAME_LOOKUP);
1076   /* Note that we declared this value so that we can issue an error if
1077      this is an invalid redeclaration of a name already used for some
1078      other purpose.  */
1079   note_name_declared_in_class (id, decl);
1080
1081   if (binding && BINDING_LEVEL (binding) == class_binding_level)
1082     /* Supplement the existing binding.  */
1083     result = add_binding (id, decl);
1084   else
1085     /* Create a new binding.  */
1086     push_binding (id, decl, class_binding_level);
1087
1088   /* Update the IDENTIFIER_CLASS_VALUE for this ID to be the
1089      class-level declaration.  Note that we do not use DECL here
1090      because of the possibility of the `struct stat' hack; if DECL is
1091      a class-name or enum-name we might prefer a field-name, or some
1092      such.  */
1093   IDENTIFIER_CLASS_VALUE (id) = BINDING_VALUE (IDENTIFIER_BINDING (id));
1094
1095   /* If this is a binding from a base class, mark it as such.  */
1096   binding = IDENTIFIER_BINDING (id);
1097   if (BINDING_VALUE (binding) == decl && TREE_CODE (decl) != TREE_LIST)
1098     {
1099       if (TREE_CODE (decl) == OVERLOAD)
1100         context = CP_DECL_CONTEXT (OVL_CURRENT (decl));
1101       else
1102         {
1103           my_friendly_assert (DECL_P (decl), 0);
1104           context = context_for_name_lookup (decl);
1105         }
1106
1107       if (is_properly_derived_from (current_class_type, context))
1108         INHERITED_VALUE_BINDING_P (binding) = 1;
1109       else
1110         INHERITED_VALUE_BINDING_P (binding) = 0;
1111     }
1112   else if (BINDING_VALUE (binding) == decl)
1113     /* We only encounter a TREE_LIST when push_class_decls detects an
1114        ambiguity.  Such an ambiguity can be overridden by a definition
1115        in this class.  */
1116     INHERITED_VALUE_BINDING_P (binding) = 1;
1117
1118   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, result);
1119 }
1120
1121 /* Remove the binding for DECL which should be the innermost binding
1122    for ID.  */
1123
1124 static void
1125 pop_binding (tree id, tree decl)
1126 {
1127   tree binding;
1128
1129   if (id == NULL_TREE)
1130     /* It's easiest to write the loops that call this function without
1131        checking whether or not the entities involved have names.  We
1132        get here for such an entity.  */
1133     return;
1134
1135   /* Get the innermost binding for ID.  */
1136   binding = IDENTIFIER_BINDING (id);
1137
1138   /* The name should be bound.  */
1139   my_friendly_assert (binding != NULL_TREE, 0);
1140
1141   /* The DECL will be either the ordinary binding or the type
1142      binding for this identifier.  Remove that binding.  */
1143   if (BINDING_VALUE (binding) == decl)
1144     BINDING_VALUE (binding) = NULL_TREE;
1145   else if (BINDING_TYPE (binding) == decl)
1146     BINDING_TYPE (binding) = NULL_TREE;
1147   else
1148     abort ();
1149
1150   if (!BINDING_VALUE (binding) && !BINDING_TYPE (binding))
1151     {
1152       /* We're completely done with the innermost binding for this
1153          identifier.  Unhook it from the list of bindings.  */
1154       IDENTIFIER_BINDING (id) = TREE_CHAIN (binding);
1155
1156       /* Add it to the free list.  */
1157       TREE_CHAIN (binding) = free_bindings;
1158       free_bindings = binding;
1159
1160       /* Clear the BINDING_LEVEL so the garbage collector doesn't walk
1161          it.  */
1162       BINDING_LEVEL (binding) = NULL;
1163     }
1164 }
1165
1166 /* When a label goes out of scope, check to see if that label was used
1167    in a valid manner, and issue any appropriate warnings or errors.  */
1168
1169 static void
1170 pop_label (tree label, tree old_value)
1171 {
1172   if (!processing_template_decl && doing_semantic_analysis_p ())
1173     {
1174       if (DECL_INITIAL (label) == NULL_TREE)
1175         {
1176           cp_error_at ("label `%D' used but not defined", label);
1177           /* Avoid crashing later.  */
1178           define_label (input_filename, 1, DECL_NAME (label));
1179         }
1180       else if (warn_unused_label && !TREE_USED (label))
1181         cp_warning_at ("label `%D' defined but not used", label);
1182     }
1183
1184   SET_IDENTIFIER_LABEL_VALUE (DECL_NAME (label), old_value);
1185 }
1186
1187 /* At the end of a function, all labels declared within the function
1188    go out of scope.  BLOCK is the top-level block for the
1189    function.  */
1190
1191 static void
1192 pop_labels (tree block)
1193 {
1194   struct named_label_list *link;
1195
1196   /* Clear out the definitions of all label names, since their scopes
1197      end here.  */
1198   for (link = named_labels; link; link = link->next)
1199     {
1200       pop_label (link->label_decl, link->old_value);
1201       /* Put the labels into the "variables" of the top-level block,
1202          so debugger can see them.  */
1203       TREE_CHAIN (link->label_decl) = BLOCK_VARS (block);
1204       BLOCK_VARS (block) = link->label_decl;
1205     }
1206
1207   named_labels = NULL;
1208 }
1209
1210 /* Exit a binding level.
1211    Pop the level off, and restore the state of the identifier-decl mappings
1212    that were in effect when this level was entered.
1213
1214    If KEEP == 1, this level had explicit declarations, so
1215    and create a "block" (a BLOCK node) for the level
1216    to record its declarations and subblocks for symbol table output.
1217
1218    If FUNCTIONBODY is nonzero, this level is the body of a function,
1219    so create a block as if KEEP were set and also clear out all
1220    label names.
1221
1222    If REVERSE is nonzero, reverse the order of decls before putting
1223    them into the BLOCK.  */
1224
1225 tree
1226 poplevel (int keep, int reverse, int functionbody)
1227 {
1228   register tree link;
1229   /* The chain of decls was accumulated in reverse order.
1230      Put it into forward order, just for cleanliness.  */
1231   tree decls;
1232   int tmp = functionbody;
1233   int real_functionbody;
1234   tree tags;
1235   tree subblocks;
1236   tree block = NULL_TREE;
1237   tree decl;
1238   int leaving_for_scope;
1239
1240   timevar_push (TV_NAME_LOOKUP);
1241   if (cfun && !doing_semantic_analysis_p ())
1242     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
1243
1244   my_friendly_assert (current_binding_level->parm_flag != 2,
1245                       19990916);
1246
1247   real_functionbody = (current_binding_level->keep == 2
1248                        ? ((functionbody = 0), tmp) : functionbody);
1249   tags = functionbody >= 0 ? current_binding_level->tags : 0;
1250   subblocks = functionbody >= 0 ? current_binding_level->blocks : 0;
1251
1252   my_friendly_assert (!current_binding_level->class_shadowed,
1253                       19990414);
1254
1255   /* We used to use KEEP == 2 to indicate that the new block should go
1256      at the beginning of the list of blocks at this binding level,
1257      rather than the end.  This hack is no longer used.  */
1258   my_friendly_assert (keep == 0 || keep == 1, 0);
1259
1260   if (current_binding_level->keep == 1)
1261     keep = 1;
1262
1263   /* Any uses of undefined labels, and any defined labels, now operate
1264      under constraints of next binding contour.  */
1265   if (cfun && !functionbody)
1266     {
1267       struct cp_binding_level *level_chain;
1268       level_chain = current_binding_level->level_chain;
1269       if (level_chain)
1270         {
1271           struct named_label_use_list *uses;
1272           struct named_label_list *labels;
1273           for (labels = named_labels; labels; labels = labels->next)
1274             if (labels->binding_level == current_binding_level)
1275               {
1276                 tree decl;
1277                 if (current_binding_level->is_try_scope)
1278                   labels->in_try_scope = 1;
1279                 if (current_binding_level->is_catch_scope)
1280                   labels->in_catch_scope = 1;
1281                 for (decl = labels->names_in_scope; decl;
1282                      decl = TREE_CHAIN (decl))
1283                   if (decl_jump_unsafe (decl))
1284                     labels->bad_decls = tree_cons (NULL_TREE, decl,
1285                                                    labels->bad_decls);
1286                 labels->binding_level = level_chain;
1287                 labels->names_in_scope = level_chain->names;
1288               }
1289
1290           for (uses = named_label_uses; uses; uses = uses->next)
1291             if (uses->binding_level == current_binding_level)
1292               {
1293                 uses->binding_level = level_chain;
1294                 uses->names_in_scope = level_chain->names;
1295               }
1296         }
1297     }
1298
1299   /* Get the decls in the order they were written.
1300      Usually current_binding_level->names is in reverse order.
1301      But parameter decls were previously put in forward order.  */
1302
1303   if (reverse)
1304     current_binding_level->names
1305       = decls = nreverse (current_binding_level->names);
1306   else
1307     decls = current_binding_level->names;
1308
1309   /* Output any nested inline functions within this block
1310      if they weren't already output.  */
1311   for (decl = decls; decl; decl = TREE_CHAIN (decl))
1312     if (TREE_CODE (decl) == FUNCTION_DECL
1313         && ! TREE_ASM_WRITTEN (decl)
1314         && DECL_INITIAL (decl) != NULL_TREE
1315         && TREE_ADDRESSABLE (decl)
1316         && decl_function_context (decl) == current_function_decl)
1317       {
1318         /* If this decl was copied from a file-scope decl
1319            on account of a block-scope extern decl,
1320            propagate TREE_ADDRESSABLE to the file-scope decl.  */
1321         if (DECL_ABSTRACT_ORIGIN (decl) != NULL_TREE)
1322           TREE_ADDRESSABLE (DECL_ABSTRACT_ORIGIN (decl)) = 1;
1323         else
1324           {
1325             push_function_context ();
1326             output_inline_function (decl);
1327             pop_function_context ();
1328           }
1329       }
1330
1331   /* When not in function-at-a-time mode, expand_end_bindings will
1332      warn about unused variables.  But, in function-at-a-time mode
1333      expand_end_bindings is not passed the list of variables in the
1334      current scope, and therefore no warning is emitted.  So, we
1335      explicitly warn here.  */
1336   if (!processing_template_decl)
1337     warn_about_unused_variables (getdecls ());
1338
1339   /* If there were any declarations or structure tags in that level,
1340      or if this level is a function body,
1341      create a BLOCK to record them for the life of this function.  */
1342   block = NULL_TREE;
1343   if (keep == 1 || functionbody)
1344     block = make_node (BLOCK);
1345   if (block != NULL_TREE)
1346     {
1347       BLOCK_VARS (block) = decls;
1348       BLOCK_SUBBLOCKS (block) = subblocks;
1349     }
1350
1351   /* In each subblock, record that this is its superior.  */
1352   if (keep >= 0)
1353     for (link = subblocks; link; link = TREE_CHAIN (link))
1354       BLOCK_SUPERCONTEXT (link) = block;
1355
1356   /* We still support the old for-scope rules, whereby the variables
1357      in a for-init statement were in scope after the for-statement
1358      ended.  We only use the new rules if flag_new_for_scope is
1359      nonzero.  */
1360   leaving_for_scope
1361     = current_binding_level->is_for_scope && flag_new_for_scope == 1;
1362
1363   /* Remove declarations for all the DECLs in this level.  */
1364   for (link = decls; link; link = TREE_CHAIN (link))
1365     {
1366       if (leaving_for_scope && TREE_CODE (link) == VAR_DECL
1367           && DECL_NAME (link))
1368         {
1369           tree outer_binding
1370             = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (link)));
1371           tree ns_binding;
1372
1373           if (!outer_binding)
1374             ns_binding = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (link));
1375           else
1376             ns_binding = NULL_TREE;
1377
1378           if (outer_binding
1379               && (BINDING_LEVEL (outer_binding)
1380                   == current_binding_level->level_chain))
1381             /* We have something like:
1382
1383                  int i;
1384                  for (int i; ;);
1385
1386                and we are leaving the `for' scope.  There's no reason to
1387                keep the binding of the inner `i' in this case.  */
1388             pop_binding (DECL_NAME (link), link);
1389           else if ((outer_binding
1390                     && (TREE_CODE (BINDING_VALUE (outer_binding))
1391                         == TYPE_DECL))
1392                    || (ns_binding
1393                        && TREE_CODE (ns_binding) == TYPE_DECL))
1394             /* Here, we have something like:
1395
1396                  typedef int I;
1397
1398                  void f () {
1399                    for (int I; ;);
1400                  }
1401
1402                We must pop the for-scope binding so we know what's a
1403                type and what isn't.  */
1404             pop_binding (DECL_NAME (link), link);
1405           else
1406             {
1407               /* Mark this VAR_DECL as dead so that we can tell we left it
1408                  there only for backward compatibility.  */
1409               DECL_DEAD_FOR_LOCAL (link) = 1;
1410
1411               /* Keep track of what should of have happenned when we
1412                  popped the binding.  */
1413               if (outer_binding && BINDING_VALUE (outer_binding))
1414                 DECL_SHADOWED_FOR_VAR (link)
1415                   = BINDING_VALUE (outer_binding);
1416
1417               /* Add it to the list of dead variables in the next
1418                  outermost binding to that we can remove these when we
1419                  leave that binding.  */
1420               current_binding_level->level_chain->dead_vars_from_for
1421                 = tree_cons (NULL_TREE, link,
1422                              current_binding_level->level_chain->
1423                              dead_vars_from_for);
1424
1425               /* Although we don't pop the CPLUS_BINDING, we do clear
1426                  its BINDING_LEVEL since the level is going away now.  */
1427               BINDING_LEVEL (IDENTIFIER_BINDING (DECL_NAME (link)))
1428                 = 0;
1429             }
1430         }
1431       else
1432         {
1433           /* Remove the binding.  */
1434           decl = link;
1435           if (TREE_CODE (decl) == TREE_LIST)
1436             decl = TREE_VALUE (decl);
1437           if (DECL_P (decl))
1438             pop_binding (DECL_NAME (decl), decl);
1439           else if (TREE_CODE (decl) == OVERLOAD)
1440             pop_binding (DECL_NAME (OVL_FUNCTION (decl)), decl);
1441           else
1442             abort ();
1443         }
1444     }
1445
1446   /* Remove declarations for any `for' variables from inner scopes
1447      that we kept around.  */
1448   for (link = current_binding_level->dead_vars_from_for;
1449        link; link = TREE_CHAIN (link))
1450     pop_binding (DECL_NAME (TREE_VALUE (link)), TREE_VALUE (link));
1451
1452   /* Restore the IDENTIFIER_TYPE_VALUEs.  */
1453   for (link = current_binding_level->type_shadowed;
1454        link; link = TREE_CHAIN (link))
1455     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (link), TREE_VALUE (link));
1456
1457   /* Restore the IDENTIFIER_LABEL_VALUEs for local labels.  */
1458   for (link = current_binding_level->shadowed_labels;
1459        link;
1460        link = TREE_CHAIN (link))
1461     pop_label (TREE_VALUE (link), TREE_PURPOSE (link));
1462
1463   /* There may be OVERLOADs (wrapped in TREE_LISTs) on the BLOCK_VARs
1464      list if a `using' declaration put them there.  The debugging
1465      back-ends won't understand OVERLOAD, so we remove them here.
1466      Because the BLOCK_VARS are (temporarily) shared with
1467      CURRENT_BINDING_LEVEL->NAMES we must do this fixup after we have
1468      popped all the bindings.  */
1469   if (block)
1470     {
1471       tree* d;
1472
1473       for (d = &BLOCK_VARS (block); *d; )
1474         {
1475           if (TREE_CODE (*d) == TREE_LIST)
1476             *d = TREE_CHAIN (*d);
1477           else
1478             d = &TREE_CHAIN (*d);
1479         }
1480     }
1481
1482   /* If the level being exited is the top level of a function,
1483      check over all the labels.  */
1484   if (functionbody)
1485     {
1486       /* Since this is the top level block of a function, the vars are
1487          the function's parameters.  Don't leave them in the BLOCK
1488          because they are found in the FUNCTION_DECL instead.  */
1489       BLOCK_VARS (block) = 0;
1490       pop_labels (block);
1491     }
1492
1493   tmp = current_binding_level->keep;
1494
1495   pop_binding_level ();
1496   if (functionbody)
1497     DECL_INITIAL (current_function_decl) = block;
1498   else if (block)
1499     current_binding_level->blocks
1500       = chainon (current_binding_level->blocks, block);
1501
1502   /* If we did not make a block for the level just exited,
1503      any blocks made for inner levels
1504      (since they cannot be recorded as subblocks in that level)
1505      must be carried forward so they will later become subblocks
1506      of something else.  */
1507   else if (subblocks)
1508     current_binding_level->blocks
1509       = chainon (current_binding_level->blocks, subblocks);
1510
1511   /* Each and every BLOCK node created here in `poplevel' is important
1512      (e.g. for proper debugging information) so if we created one
1513      earlier, mark it as "used".  */
1514   if (block)
1515     TREE_USED (block) = 1;
1516
1517   /* Take care of compiler's internal binding structures.  */
1518   if (tmp == 2)
1519     {
1520       tree scope_stmts;
1521
1522       scope_stmts
1523         = add_scope_stmt (/*begin_p=*/0, /*partial_p=*/1);
1524       if (block)
1525         {
1526           SCOPE_STMT_BLOCK (TREE_PURPOSE (scope_stmts)) = block;
1527           SCOPE_STMT_BLOCK (TREE_VALUE (scope_stmts)) = block;
1528         }
1529
1530       block = poplevel (keep, reverse, functionbody);
1531     }
1532
1533   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, block);
1534 }
1535
1536 /* Delete the node BLOCK from the current binding level.
1537    This is used for the block inside a stmt expr ({...})
1538    so that the block can be reinserted where appropriate.  */
1539
1540 void
1541 delete_block (tree block)
1542 {
1543   tree t;
1544   if (current_binding_level->blocks == block)
1545     current_binding_level->blocks = TREE_CHAIN (block);
1546   for (t = current_binding_level->blocks; t;)
1547     {
1548       if (TREE_CHAIN (t) == block)
1549         TREE_CHAIN (t) = TREE_CHAIN (block);
1550       else
1551         t = TREE_CHAIN (t);
1552     }
1553   TREE_CHAIN (block) = NULL_TREE;
1554   /* Clear TREE_USED which is always set by poplevel.
1555      The flag is set again if insert_block is called.  */
1556   TREE_USED (block) = 0;
1557 }
1558
1559 /* Insert BLOCK at the end of the list of subblocks of the
1560    current binding level.  This is used when a BIND_EXPR is expanded,
1561    to handle the BLOCK node inside the BIND_EXPR.  */
1562
1563 void
1564 insert_block (tree block)
1565 {
1566   TREE_USED (block) = 1;
1567   current_binding_level->blocks
1568     = chainon (current_binding_level->blocks, block);
1569 }
1570
1571 /* Set the BLOCK node for the innermost scope
1572    (the one we are currently in).  */
1573
1574 void
1575 set_block (tree block ATTRIBUTE_UNUSED )
1576 {
1577   /* The RTL expansion machinery requires us to provide this callback,
1578      but it is not applicable in function-at-a-time mode.  */
1579   my_friendly_assert (cfun && !doing_semantic_analysis_p (), 20000911);
1580 }
1581
1582 /* Do a pushlevel for class declarations.  */
1583
1584 void
1585 pushlevel_class (void)
1586 {
1587   register struct cp_binding_level *newlevel;
1588
1589   /* Reuse or create a struct for this binding level.  */
1590 #if defined(DEBUG_BINDING_LEVELS)
1591   if (0)
1592 #else /* !defined(DEBUG_BINDING_LEVELS) */
1593   if (free_binding_level)
1594 #endif /* !defined(DEBUG_BINDING_LEVELS) */
1595     {
1596       newlevel = free_binding_level;
1597       free_binding_level = free_binding_level->level_chain;
1598     }
1599   else
1600     newlevel = make_binding_level ();
1601
1602 #if defined(DEBUG_BINDING_LEVELS)
1603   is_class_level = 1;
1604 #endif /* defined(DEBUG_BINDING_LEVELS) */
1605
1606   push_binding_level (newlevel, 0, 0);
1607
1608   class_binding_level = current_binding_level;
1609   class_binding_level->parm_flag = 2;
1610   class_binding_level->this_class = current_class_type;
1611 }
1612
1613 /* ...and a poplevel for class declarations.  */
1614
1615 void
1616 poplevel_class (void)
1617 {
1618   register struct cp_binding_level *level = class_binding_level;
1619   tree shadowed;
1620
1621   timevar_push (TV_NAME_LOOKUP);
1622   my_friendly_assert (level != 0, 354);
1623
1624   /* If we're leaving a toplevel class, don't bother to do the setting
1625      of IDENTIFIER_CLASS_VALUE to NULL_TREE, since first of all this slot
1626      shouldn't even be used when current_class_type isn't set, and second,
1627      if we don't touch it here, we're able to use the cache effect if the
1628      next time we're entering a class scope, it is the same class.  */
1629   if (current_class_depth != 1)
1630     {
1631       struct cp_binding_level* b;
1632
1633       /* Clear out our IDENTIFIER_CLASS_VALUEs.  */
1634       for (shadowed = level->class_shadowed;
1635            shadowed;
1636            shadowed = TREE_CHAIN (shadowed))
1637         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed)) = NULL_TREE;
1638
1639       /* Find the next enclosing class, and recreate
1640          IDENTIFIER_CLASS_VALUEs appropriate for that class.  */
1641       b = level->level_chain;
1642       while (b && b->parm_flag != 2)
1643         b = b->level_chain;
1644
1645       if (b)
1646         for (shadowed = b->class_shadowed;
1647              shadowed;
1648              shadowed = TREE_CHAIN (shadowed))
1649           {
1650             tree t;
1651
1652             t = IDENTIFIER_BINDING (TREE_PURPOSE (shadowed));
1653             while (t && BINDING_LEVEL (t) != b)
1654               t = TREE_CHAIN (t);
1655
1656             if (t)
1657               IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (shadowed))
1658                 = BINDING_VALUE (t);
1659           }
1660     }
1661   else
1662     /* Remember to save what IDENTIFIER's were bound in this scope so we
1663        can recover from cache misses.  */
1664     {
1665       previous_class_type = current_class_type;
1666       previous_class_values = class_binding_level->class_shadowed;
1667     }
1668   for (shadowed = level->type_shadowed;
1669        shadowed;
1670        shadowed = TREE_CHAIN (shadowed))
1671     SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (shadowed), TREE_VALUE (shadowed));
1672
1673   /* Remove the bindings for all of the class-level declarations.  */
1674   for (shadowed = level->class_shadowed;
1675        shadowed;
1676        shadowed = TREE_CHAIN (shadowed))
1677     pop_binding (TREE_PURPOSE (shadowed), TREE_TYPE (shadowed));
1678
1679   /* Now, pop out of the binding level which we created up in the
1680      `pushlevel_class' routine.  */
1681 #if defined(DEBUG_BINDING_LEVELS)
1682   is_class_level = 1;
1683 #endif /* defined(DEBUG_BINDING_LEVELS) */
1684
1685   pop_binding_level ();
1686   timevar_pop (TV_NAME_LOOKUP);
1687 }
1688
1689 /* We are entering the scope of a class.  Clear IDENTIFIER_CLASS_VALUE
1690    for any names in enclosing classes.  */
1691
1692 void
1693 clear_identifier_class_values (void)
1694 {
1695   tree t;
1696
1697   if (!class_binding_level)
1698     return;
1699
1700   for (t = class_binding_level->class_shadowed;
1701        t;
1702        t = TREE_CHAIN (t))
1703     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
1704 }
1705
1706 /* Returns nonzero if T is a virtual function table.  */
1707
1708 int
1709 vtable_decl_p (tree t, void* data ATTRIBUTE_UNUSED )
1710 {
1711   return (TREE_CODE (t) == VAR_DECL && DECL_VIRTUAL_P (t));
1712 }
1713
1714 /* Returns nonzero if T is a TYPE_DECL for a type with virtual
1715    functions.  */
1716
1717 int
1718 vtype_decl_p (tree t, void *data ATTRIBUTE_UNUSED )
1719 {
1720   return (TREE_CODE (t) == TYPE_DECL
1721           && TREE_CODE (TREE_TYPE (t)) == RECORD_TYPE
1722           && TYPE_POLYMORPHIC_P (TREE_TYPE (t)));
1723 }
1724
1725 /* Return the declarations that are members of the namespace NS.  */
1726
1727 tree
1728 cp_namespace_decls (tree ns)
1729 {
1730   return NAMESPACE_LEVEL (ns)->names;
1731 }
1732
1733 struct walk_globals_data {
1734   walk_globals_pred p;
1735   walk_globals_fn f;
1736   void *data;
1737 };
1738
1739 /* Walk the vtable declarations in NAMESPACE.  Whenever one is found
1740    for which P returns nonzero, call F with its address.  If any call
1741    to F returns a nonzero value, return a nonzero value.  */
1742
1743 static int
1744 walk_vtables_r (tree namespace, void* data)
1745 {
1746   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1747   walk_globals_fn f = wgd->f;
1748   void *d = wgd->data;
1749   tree decl = NAMESPACE_LEVEL (namespace)->vtables;
1750   int result = 0;
1751
1752   for (; decl ; decl = TREE_CHAIN (decl))
1753     result |= (*f) (&decl, d);
1754
1755   return result;
1756 }
1757
1758 /* Walk the vtable declarations.  Whenever one is found for which P
1759    returns nonzero, call F with its address.  If any call to F
1760    returns a nonzero value, return a nonzero value.  */
1761 bool
1762 walk_vtables (walk_globals_pred p, walk_globals_fn f, void *data)
1763 {    
1764   struct walk_globals_data wgd;
1765   wgd.p = p;    
1766   wgd.f = f;
1767   wgd.data = data;
1768
1769   return walk_namespaces (walk_vtables_r, &wgd);
1770 }
1771
1772 /* Walk all the namespaces contained NAMESPACE, including NAMESPACE
1773    itself, calling F for each.  The DATA is passed to F as well.  */
1774
1775 static int
1776 walk_namespaces_r (tree namespace, walk_namespaces_fn f, void* data)
1777 {
1778   int result = 0;
1779   tree current = NAMESPACE_LEVEL (namespace)->namespaces;     
1780
1781   result |= (*f) (namespace, data);
1782
1783   for (; current; current = TREE_CHAIN (current))
1784     result |= walk_namespaces_r (current, f, data);
1785
1786   return result;
1787 }
1788
1789 /* Walk all the namespaces, calling F for each.  The DATA is passed to
1790    F as well.  */
1791
1792 int
1793 walk_namespaces (walk_namespaces_fn f, void* data)
1794 {
1795   return walk_namespaces_r (global_namespace, f, data);
1796 }
1797
1798 /* Walk the global declarations in NAMESPACE.  Whenever one is found
1799    for which P returns nonzero, call F with its address.  If any call
1800    to F returns a nonzero value, return a nonzero value.  */
1801
1802 static int
1803 walk_globals_r (tree namespace, void* data)
1804 {
1805   struct walk_globals_data* wgd = (struct walk_globals_data *) data;
1806   walk_globals_pred p = wgd->p;
1807   walk_globals_fn f = wgd->f;
1808   void *d = wgd->data;
1809   tree *t;
1810   int result = 0;
1811
1812   t = &NAMESPACE_LEVEL (namespace)->names;
1813
1814   while (*t)
1815     {
1816       tree glbl = *t;
1817
1818       if ((*p) (glbl, d))
1819         result |= (*f) (t, d);
1820
1821       /* If F changed *T, then *T still points at the next item to
1822          examine.  */
1823       if (*t == glbl)
1824         t = &TREE_CHAIN (*t);
1825     }
1826
1827   return result;
1828 }
1829
1830 /* Walk the global declarations.  Whenever one is found for which P
1831    returns true, call F with its address.  If any call to F
1832    returns true, return true.  */
1833
1834 bool
1835 walk_globals (walk_globals_pred p, walk_globals_fn f, void *data)
1836 {
1837   struct walk_globals_data wgd;
1838   wgd.p = p;
1839   wgd.f = f;
1840   wgd.data = data;
1841
1842   return walk_namespaces (walk_globals_r, &wgd);
1843 }
1844
1845 /* Call wrapup_globals_declarations for the globals in NAMESPACE.  If
1846    DATA is non-NULL, this is the last time we will call
1847    wrapup_global_declarations for this NAMESPACE.  */
1848
1849 int
1850 wrapup_globals_for_namespace (tree namespace, void* data)
1851 {
1852   tree globals = cp_namespace_decls (namespace);
1853   int len = NAMESPACE_LEVEL (namespace)->names_size;
1854   tree *vec = (tree *) alloca (sizeof (tree) * len);
1855   int i;
1856   int result;
1857   tree decl;
1858   int last_time = (data != 0);
1859
1860   /* Process the decls in reverse order--earliest first.
1861      Put them into VEC from back to front, then take out from front.  */       
1862   for (i = 0, decl = globals; i < len; i++, decl = TREE_CHAIN (decl))
1863     vec[len - i - 1] = decl;
1864
1865   if (last_time)
1866     {
1867       check_global_declarations (vec, len);
1868       return 0;
1869     }
1870
1871   /* Write out any globals that need to be output.  */
1872   result = wrapup_global_declarations (vec, len);
1873
1874   return result;
1875 }
1876
1877 \f
1878 /* For debugging.  */
1879 static int no_print_functions = 0;
1880 static int no_print_builtins = 0;
1881
1882 void
1883 print_binding_level (struct cp_binding_level* lvl)
1884 {
1885   tree t;
1886   int i = 0, len;
1887   fprintf (stderr, " blocks=");
1888   fprintf (stderr, HOST_PTR_PRINTF, lvl->blocks);
1889   if (lvl->tag_transparent)
1890     fprintf (stderr, " tag-transparent");
1891   if (lvl->more_cleanups_ok)
1892     fprintf (stderr, " more-cleanups-ok");
1893   if (lvl->have_cleanups)
1894     fprintf (stderr, " have-cleanups");
1895   fprintf (stderr, "\n");
1896   if (lvl->names)
1897     {
1898       fprintf (stderr, " names:\t");
1899       /* We can probably fit 3 names to a line?  */
1900       for (t = lvl->names; t; t = TREE_CHAIN (t))
1901         {
1902           if (no_print_functions && (TREE_CODE (t) == FUNCTION_DECL))
1903             continue;
1904           if (no_print_builtins
1905               && (TREE_CODE (t) == TYPE_DECL)
1906               && (!strcmp (DECL_SOURCE_FILE (t),"<built-in>")))
1907             continue;
1908
1909           /* Function decls tend to have longer names.  */
1910           if (TREE_CODE (t) == FUNCTION_DECL)
1911             len = 3;
1912           else
1913             len = 2;
1914           i += len;
1915           if (i > 6)
1916             {
1917               fprintf (stderr, "\n\t");
1918               i = len;
1919             }
1920           print_node_brief (stderr, "", t, 0);
1921           if (t == error_mark_node)
1922             break;
1923         }
1924       if (i)
1925         fprintf (stderr, "\n");
1926     }
1927   if (lvl->tags)
1928     {
1929       fprintf (stderr, " tags:\t");
1930       i = 0;
1931       for (t = lvl->tags; t; t = TREE_CHAIN (t))
1932         {
1933           if (TREE_PURPOSE (t) == NULL_TREE)
1934             len = 3;
1935           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1936             len = 2;
1937           else
1938             len = 4;
1939           i += len;
1940           if (i > 5)
1941             {
1942               fprintf (stderr, "\n\t");
1943               i = len;
1944             }
1945           if (TREE_PURPOSE (t) == NULL_TREE)
1946             {
1947               print_node_brief (stderr, "<unnamed-typedef", TREE_VALUE (t), 0);
1948               fprintf (stderr, ">");
1949             }
1950           else if (TREE_PURPOSE (t) == TYPE_IDENTIFIER (TREE_VALUE (t)))
1951             print_node_brief (stderr, "", TREE_VALUE (t), 0);
1952           else
1953             {
1954               print_node_brief (stderr, "<typedef", TREE_PURPOSE (t), 0);
1955               print_node_brief (stderr, "", TREE_VALUE (t), 0);
1956               fprintf (stderr, ">");
1957             }
1958         }
1959       if (i)
1960         fprintf (stderr, "\n");
1961     }
1962   if (lvl->class_shadowed)
1963     {
1964       fprintf (stderr, " class-shadowed:");
1965       for (t = lvl->class_shadowed; t; t = TREE_CHAIN (t))
1966         {
1967           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1968         }
1969       fprintf (stderr, "\n");
1970     }
1971   if (lvl->type_shadowed)
1972     {
1973       fprintf (stderr, " type-shadowed:");
1974       for (t = lvl->type_shadowed; t; t = TREE_CHAIN (t))
1975         {
1976           fprintf (stderr, " %s ", IDENTIFIER_POINTER (TREE_PURPOSE (t)));
1977         }
1978       fprintf (stderr, "\n");
1979     }
1980 }
1981
1982 void
1983 print_other_binding_stack (struct cp_binding_level *stack)
1984 {
1985   struct cp_binding_level *level;
1986   for (level = stack; level != global_binding_level; level = level->level_chain)
1987     {
1988       fprintf (stderr, "binding level ");
1989       fprintf (stderr, HOST_PTR_PRINTF, level);
1990       fprintf (stderr, "\n");
1991       print_binding_level (level);
1992     }
1993 }
1994
1995 void
1996 print_binding_stack (void)
1997 {
1998   struct cp_binding_level *b;
1999   fprintf (stderr, "current_binding_level=");
2000   fprintf (stderr, HOST_PTR_PRINTF, current_binding_level);
2001   fprintf (stderr, "\nclass_binding_level=");
2002   fprintf (stderr, HOST_PTR_PRINTF, class_binding_level);
2003   fprintf (stderr, "\nglobal_binding_level=");
2004   fprintf (stderr, HOST_PTR_PRINTF, global_binding_level);
2005   fprintf (stderr, "\n");
2006   if (class_binding_level)
2007     {
2008       for (b = class_binding_level; b; b = b->level_chain)
2009         if (b == current_binding_level)
2010           break;
2011       if (b)
2012         b = class_binding_level;
2013       else
2014         b = current_binding_level;
2015     }
2016   else
2017     b = current_binding_level;
2018   print_other_binding_stack (b);
2019   fprintf (stderr, "global:\n");
2020   print_binding_level (global_binding_level);
2021 }
2022
2023 /* Namespace binding access routines: The namespace_bindings field of
2024    the identifier is polymorphic, with three possible values:
2025    NULL_TREE, a list of CPLUS_BINDINGS, or any other tree_node
2026    indicating the BINDING_VALUE of global_namespace.  */
2027
2028 /* Check whether the a binding for the name to scope is known.
2029    Assumes that the bindings of the name are already a list
2030    of bindings. Returns the binding found, or NULL_TREE.  */
2031
2032 static tree
2033 find_binding (tree name, tree scope)
2034 {
2035   tree iter, prev = NULL_TREE;
2036
2037   timevar_push (TV_NAME_LOOKUP);
2038   scope = ORIGINAL_NAMESPACE (scope);
2039
2040   for (iter = IDENTIFIER_NAMESPACE_BINDINGS (name); iter;
2041        iter = TREE_CHAIN (iter))
2042     {
2043       my_friendly_assert (TREE_CODE (iter) == CPLUS_BINDING, 374);
2044       if (BINDING_SCOPE (iter) == scope)
2045         {
2046           /* Move binding found to the front of the list, so
2047              subsequent lookups will find it faster.  */
2048           if (prev)
2049             {
2050               TREE_CHAIN (prev) = TREE_CHAIN (iter);
2051               TREE_CHAIN (iter) = IDENTIFIER_NAMESPACE_BINDINGS (name);
2052               IDENTIFIER_NAMESPACE_BINDINGS (name) = iter;
2053             }
2054           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, iter);
2055         }
2056       prev = iter;
2057     }
2058   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2059 }
2060
2061 /* Always returns a binding for name in scope. If the
2062    namespace_bindings is not a list, convert it to one first.
2063    If no binding is found, make a new one.  */
2064
2065 tree
2066 binding_for_name (tree name, tree scope)
2067 {
2068   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2069   tree result;
2070
2071   scope = ORIGINAL_NAMESPACE (scope);
2072
2073   if (b && TREE_CODE (b) != CPLUS_BINDING)
2074     {
2075       /* Get rid of optimization for global scope.  */
2076       IDENTIFIER_NAMESPACE_BINDINGS (name) = NULL_TREE;
2077       BINDING_VALUE (binding_for_name (name, global_namespace)) = b;
2078       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2079     }
2080   if (b && (result = find_binding (name, scope)))
2081     return result;
2082   /* Not found, make a new one.  */
2083   result = make_node (CPLUS_BINDING);
2084   TREE_CHAIN (result) = b;
2085   IDENTIFIER_NAMESPACE_BINDINGS (name) = result;
2086   BINDING_SCOPE (result) = scope;
2087   BINDING_TYPE (result) = NULL_TREE;
2088   BINDING_VALUE (result) = NULL_TREE;
2089   return result;
2090 }
2091
2092 /* Return the binding value for name in scope, considering that
2093    namespace_binding may or may not be a list of CPLUS_BINDINGS.  */
2094
2095 tree
2096 namespace_binding (tree name, tree scope)
2097 {
2098   tree b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2099   if (b == NULL_TREE)
2100     return NULL_TREE;
2101   if (scope == NULL_TREE)
2102     scope = global_namespace;
2103   if (TREE_CODE (b) != CPLUS_BINDING)
2104     return (scope == global_namespace) ? b : NULL_TREE;
2105   name = find_binding (name,scope);
2106   if (name == NULL_TREE)
2107     return name;
2108   return BINDING_VALUE (name);
2109 }
2110
2111 /* Set the binding value for name in scope. If modifying the binding
2112    of global_namespace is attempted, try to optimize it.  */
2113
2114 void
2115 set_namespace_binding (tree name, tree scope, tree val)
2116 {
2117   tree b;
2118
2119   timevar_push (TV_NAME_LOOKUP);
2120   if (scope == NULL_TREE)
2121     scope = global_namespace;
2122
2123   if (scope == global_namespace)
2124     {
2125       b = IDENTIFIER_NAMESPACE_BINDINGS (name);
2126       if (b == NULL_TREE || TREE_CODE (b) != CPLUS_BINDING)
2127         {
2128           IDENTIFIER_NAMESPACE_BINDINGS (name) = val;
2129           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
2130         }
2131     }
2132   b = binding_for_name (name, scope);
2133   BINDING_VALUE (b) = val;
2134   timevar_pop (TV_NAME_LOOKUP);
2135 }
2136
2137 /* Push into the scope of the NAME namespace.  If NAME is NULL_TREE, then we
2138    select a name that is unique to this compilation unit.  */
2139
2140 void
2141 push_namespace (tree name)
2142 {
2143   tree d = NULL_TREE;
2144   int need_new = 1;
2145   int implicit_use = 0;
2146   int global = 0;
2147
2148   timevar_push (TV_NAME_LOOKUP);
2149   
2150   if (!global_namespace)
2151     {
2152       /* This must be ::.  */
2153       my_friendly_assert (name == get_identifier ("::"), 377);
2154       global = 1;
2155     }
2156   else if (!name)
2157     {
2158       /* The name of anonymous namespace is unique for the translation
2159          unit.  */
2160       if (!anonymous_namespace_name)
2161         anonymous_namespace_name = get_file_function_name ('N');
2162       name = anonymous_namespace_name;
2163       d = IDENTIFIER_NAMESPACE_VALUE (name);
2164       if (d)
2165         /* Reopening anonymous namespace.  */
2166         need_new = 0;
2167       implicit_use = 1;
2168     }
2169   else
2170     {
2171       /* Check whether this is an extended namespace definition.  */
2172       d = IDENTIFIER_NAMESPACE_VALUE (name);
2173       if (d != NULL_TREE && TREE_CODE (d) == NAMESPACE_DECL)
2174         {
2175           need_new = 0;
2176           if (DECL_NAMESPACE_ALIAS (d))
2177             {
2178               error ("namespace alias `%D' not allowed here, assuming `%D'",
2179                         d, DECL_NAMESPACE_ALIAS (d));
2180               d = DECL_NAMESPACE_ALIAS (d);
2181             }
2182         }
2183     }
2184
2185   if (need_new)
2186     {
2187       /* Make a new namespace, binding the name to it.  */
2188       d = build_lang_decl (NAMESPACE_DECL, name, void_type_node);
2189       /* The global namespace is not pushed, and the global binding
2190          level is set elsewhere.  */
2191       if (!global)
2192         {
2193           DECL_CONTEXT (d) = FROB_CONTEXT (current_namespace);
2194           d = pushdecl (d);
2195           pushlevel (0);
2196           declare_namespace_level ();
2197           NAMESPACE_LEVEL (d) = current_binding_level;
2198         }
2199     }
2200   else
2201     resume_binding_level (NAMESPACE_LEVEL (d));
2202
2203   if (implicit_use)
2204     do_using_directive (d);
2205   /* Enter the name space.  */
2206   current_namespace = d;
2207
2208   timevar_pop (TV_NAME_LOOKUP);
2209 }
2210
2211 /* Pop from the scope of the current namespace.  */
2212
2213 void
2214 pop_namespace (void)
2215 {
2216   my_friendly_assert (current_namespace != global_namespace, 20010801);
2217   current_namespace = CP_DECL_CONTEXT (current_namespace);
2218   /* The binding level is not popped, as it might be re-opened later.  */
2219   suspend_binding_level ();
2220 }
2221
2222 /* Push into the scope of the namespace NS, even if it is deeply
2223    nested within another namespace.  */
2224
2225 void
2226 push_nested_namespace (tree ns)
2227 {
2228   if (ns == global_namespace)
2229     push_to_top_level ();
2230   else
2231     {
2232       push_nested_namespace (CP_DECL_CONTEXT (ns));
2233       push_namespace (DECL_NAME (ns));
2234     }
2235 }
2236
2237 /* Pop back from the scope of the namespace NS, which was previously
2238    entered with push_nested_namespace.  */
2239
2240 void
2241 pop_nested_namespace (tree ns)
2242 {
2243   timevar_push (TV_NAME_LOOKUP);
2244   while (ns != global_namespace)
2245     {
2246       pop_namespace ();
2247       ns = CP_DECL_CONTEXT (ns);
2248     }
2249
2250   pop_from_top_level ();
2251   timevar_pop (TV_NAME_LOOKUP);
2252 }
2253
2254 \f
2255 /* Subroutines for reverting temporarily to top-level for instantiation
2256    of templates and such.  We actually need to clear out the class- and
2257    local-value slots of all identifiers, so that only the global values
2258    are at all visible.  Simply setting current_binding_level to the global
2259    scope isn't enough, because more binding levels may be pushed.  */
2260 struct saved_scope *scope_chain;
2261
2262 static tree
2263 store_bindings (tree names, tree old_bindings)
2264 {
2265   tree t;
2266   tree search_bindings = old_bindings;
2267
2268   timevar_push (TV_NAME_LOOKUP);
2269   for (t = names; t; t = TREE_CHAIN (t))
2270     {
2271       tree binding, t1, id;
2272
2273       if (TREE_CODE (t) == TREE_LIST)
2274         id = TREE_PURPOSE (t);
2275       else
2276         id = DECL_NAME (t);
2277
2278       if (!id
2279           /* Note that we may have an IDENTIFIER_CLASS_VALUE even when
2280              we have no IDENTIFIER_BINDING if we have left the class
2281              scope, but cached the class-level declarations.  */
2282           || !(IDENTIFIER_BINDING (id) || IDENTIFIER_CLASS_VALUE (id)))
2283         continue;
2284
2285       for (t1 = search_bindings; t1; t1 = TREE_CHAIN (t1))
2286         if (TREE_VEC_ELT (t1, 0) == id)
2287           goto skip_it;
2288
2289       my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 135);
2290       binding = make_tree_vec (4);
2291       TREE_VEC_ELT (binding, 0) = id;
2292       TREE_VEC_ELT (binding, 1) = REAL_IDENTIFIER_TYPE_VALUE (id);
2293       TREE_VEC_ELT (binding, 2) = IDENTIFIER_BINDING (id);
2294       TREE_VEC_ELT (binding, 3) = IDENTIFIER_CLASS_VALUE (id);
2295       IDENTIFIER_BINDING (id) = NULL_TREE;
2296       IDENTIFIER_CLASS_VALUE (id) = NULL_TREE;
2297       TREE_CHAIN (binding) = old_bindings;
2298       old_bindings = binding;
2299     skip_it:
2300       ;
2301     }
2302   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old_bindings);
2303 }
2304
2305 void
2306 maybe_push_to_top_level (int pseudo)
2307 {
2308   struct saved_scope *s;
2309   struct cp_binding_level *b;
2310   tree old_bindings;
2311   int need_pop;
2312
2313   timevar_push (TV_NAME_LOOKUP);
2314   s = (struct saved_scope *) ggc_alloc_cleared (sizeof (struct saved_scope));
2315
2316   b = scope_chain ? current_binding_level : 0;
2317
2318   /* If we're in the middle of some function, save our state.  */
2319   if (cfun)
2320     {
2321       need_pop = 1;
2322       push_function_context_to (NULL_TREE);
2323     }
2324   else
2325     need_pop = 0;
2326
2327   old_bindings = NULL_TREE;
2328   if (scope_chain && previous_class_type)
2329     old_bindings = store_bindings (previous_class_values, old_bindings);
2330
2331   /* Have to include global_binding_level, because class-level decls
2332      aren't listed anywhere useful.  */
2333   for (; b; b = b->level_chain)
2334     {
2335       tree t;
2336
2337       /* Template IDs are inserted into the global level. If they were
2338          inserted into namespace level, finish_file wouldn't find them
2339          when doing pending instantiations. Therefore, don't stop at
2340          namespace level, but continue until :: .  */
2341       if (b == global_binding_level || (pseudo && b->template_parms_p))
2342         break;
2343
2344       old_bindings = store_bindings (b->names, old_bindings);
2345       /* We also need to check class_shadowed to save class-level type
2346          bindings, since pushclass doesn't fill in b->names.  */
2347       if (b->parm_flag == 2)
2348         old_bindings = store_bindings (b->class_shadowed, old_bindings);
2349
2350       /* Unwind type-value slots back to top level.  */
2351       for (t = b->type_shadowed; t; t = TREE_CHAIN (t))
2352         SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (t), TREE_VALUE (t));
2353     }
2354   s->prev = scope_chain;
2355   s->old_bindings = old_bindings;
2356   s->bindings = b;
2357   s->need_pop_function_context = need_pop;
2358   s->function_decl = current_function_decl;
2359   s->last_parms = last_function_parms;
2360   s->check_access = flag_access_control;
2361
2362   scope_chain = s;
2363   current_function_decl = NULL_TREE;
2364   VARRAY_TREE_INIT (current_lang_base, 10, "current_lang_base");
2365   current_lang_name = lang_name_cplusplus;
2366   current_namespace = global_namespace;
2367   timevar_pop (TV_NAME_LOOKUP);
2368 }
2369
2370 void
2371 push_to_top_level (void)
2372 {
2373   maybe_push_to_top_level (0);
2374 }
2375
2376 void
2377 pop_from_top_level (void)
2378 {
2379   struct saved_scope *s = scope_chain;
2380   tree t;
2381
2382   timevar_push (TV_NAME_LOOKUP); 
2383   /* Clear out class-level bindings cache.  */
2384   if (previous_class_type)
2385     invalidate_class_lookup_cache ();
2386
2387   current_lang_base = 0;
2388
2389   scope_chain = s->prev;
2390   for (t = s->old_bindings; t; t = TREE_CHAIN (t))
2391     {
2392       tree id = TREE_VEC_ELT (t, 0);
2393
2394       SET_IDENTIFIER_TYPE_VALUE (id, TREE_VEC_ELT (t, 1));
2395       IDENTIFIER_BINDING (id) = TREE_VEC_ELT (t, 2);
2396       IDENTIFIER_CLASS_VALUE (id) = TREE_VEC_ELT (t, 3);
2397     }
2398
2399   /* If we were in the middle of compiling a function, restore our
2400      state.  */
2401   if (s->need_pop_function_context)
2402     pop_function_context_from (NULL_TREE);
2403   current_function_decl = s->function_decl;
2404   last_function_parms = s->last_parms;
2405   timevar_pop (TV_NAME_LOOKUP);
2406 }
2407 \f
2408 /* Push a definition of struct, union or enum tag "name".
2409    into binding_level "b".   "type" should be the type node,
2410    We assume that the tag "name" is not already defined.
2411
2412    Note that the definition may really be just a forward reference.
2413    In that case, the TYPE_SIZE will be a NULL_TREE.
2414
2415    C++ gratuitously puts all these tags in the name space.  */
2416
2417 /* When setting the IDENTIFIER_TYPE_VALUE field of an identifier ID,
2418    record the shadowed value for this binding contour.  TYPE is
2419    the type that ID maps to.  */
2420
2421 static void
2422 set_identifier_type_value_with_scope (tree id, 
2423                                       tree type, 
2424                                       struct cp_binding_level* b)
2425 {
2426   if (!b->namespace_p)
2427     {
2428       /* Shadow the marker, not the real thing, so that the marker
2429          gets restored later.  */
2430       tree old_type_value = REAL_IDENTIFIER_TYPE_VALUE (id);
2431       b->type_shadowed
2432         = tree_cons (id, old_type_value, b->type_shadowed);
2433     }
2434   else
2435     {
2436       tree binding = binding_for_name (id, current_namespace);
2437       BINDING_TYPE (binding) = type;
2438       /* Store marker instead of real type.  */
2439       type = global_type_node;
2440     }
2441   SET_IDENTIFIER_TYPE_VALUE (id, type);
2442 }
2443
2444 /* As set_identifier_type_value_with_scope, but using current_binding_level.  */
2445
2446 void
2447 set_identifier_type_value (tree id, tree type)
2448 {
2449   set_identifier_type_value_with_scope (id, type, current_binding_level);
2450 }
2451
2452 /* Return the type associated with id.  */
2453
2454 tree
2455 identifier_type_value (tree id)
2456 {
2457   timevar_push (TV_NAME_LOOKUP);
2458   /* There is no type with that name, anywhere.  */
2459   if (REAL_IDENTIFIER_TYPE_VALUE (id) == NULL_TREE)
2460     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2461   /* This is not the type marker, but the real thing.  */
2462   if (REAL_IDENTIFIER_TYPE_VALUE (id) != global_type_node)
2463     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, REAL_IDENTIFIER_TYPE_VALUE (id));
2464   /* Have to search for it. It must be on the global level, now.
2465      Ask lookup_name not to return non-types. */
2466   id = lookup_name_real (id, 2, 1, 0, LOOKUP_COMPLAIN);
2467   if (id)
2468     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_TYPE (id));
2469   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
2470 }
2471
2472 /* Pop off extraneous binding levels left over due to syntax errors.
2473
2474    We don't pop past namespaces, as they might be valid.  */
2475
2476 void
2477 pop_everything (void)
2478 {
2479 #ifdef DEBUG_BINDING_LEVELS
2480   fprintf (stderr, "XXX entering pop_everything ()\n");
2481 #endif
2482   while (!toplevel_bindings_p ())
2483     {
2484       if (current_binding_level->parm_flag == 2)
2485         pop_nested_class ();
2486       else
2487         poplevel (0, 0, 0);
2488     }
2489 #ifdef DEBUG_BINDING_LEVELS
2490   fprintf (stderr, "XXX leaving pop_everything ()\n");
2491 #endif
2492 }
2493
2494 /* The type TYPE is being declared.  If it is a class template, or a
2495    specialization of a class template, do any processing required and
2496    perform error-checking.  If IS_FRIEND is nonzero, this TYPE is
2497    being declared a friend.  B is the binding level at which this TYPE
2498    should be bound.
2499
2500    Returns the TYPE_DECL for TYPE, which may have been altered by this
2501    processing.  */
2502
2503 static tree
2504 maybe_process_template_type_declaration (tree type, 
2505                                          int globalize, 
2506                                          struct cp_binding_level* b)
2507 {
2508   tree decl = TYPE_NAME (type);
2509
2510   if (processing_template_parmlist)
2511     /* You can't declare a new template type in a template parameter
2512        list.  But, you can declare a non-template type:
2513
2514          template <class A*> struct S;
2515
2516        is a forward-declaration of `A'.  */
2517     ;
2518   else
2519     {
2520       maybe_check_template_type (type);
2521
2522       my_friendly_assert (IS_AGGR_TYPE (type)
2523                           || TREE_CODE (type) == ENUMERAL_TYPE, 0);
2524
2525
2526       if (processing_template_decl)
2527         {
2528           /* This may change after the call to
2529              push_template_decl_real, but we want the original value.  */
2530           tree name = DECL_NAME (decl);
2531
2532           decl = push_template_decl_real (decl, globalize);
2533           /* If the current binding level is the binding level for the
2534              template parameters (see the comment in
2535              begin_template_parm_list) and the enclosing level is a class
2536              scope, and we're not looking at a friend, push the
2537              declaration of the member class into the class scope.  In the
2538              friend case, push_template_decl will already have put the
2539              friend into global scope, if appropriate.  */
2540           if (TREE_CODE (type) != ENUMERAL_TYPE
2541               && !globalize && b->template_parms_p
2542               && b->level_chain->parm_flag == 2)
2543             {
2544               finish_member_declaration (CLASSTYPE_TI_TEMPLATE (type));
2545               /* Put this tag on the list of tags for the class, since
2546                  that won't happen below because B is not the class
2547                  binding level, but is instead the pseudo-global level.  */
2548               b->level_chain->tags =
2549                 tree_cons (name, type, b->level_chain->tags);
2550               if (!COMPLETE_TYPE_P (current_class_type))
2551                 {
2552                   maybe_add_class_template_decl_list (current_class_type,
2553                                                       type, /*friend_p=*/0);
2554                   CLASSTYPE_TAGS (current_class_type) = b->level_chain->tags;
2555                 }
2556             }
2557         }
2558     }
2559
2560   return decl;
2561 }
2562
2563 /* In C++, you don't have to write `struct S' to refer to `S'; you
2564    can just use `S'.  We accomplish this by creating a TYPE_DECL as
2565    if the user had written `typedef struct S S'.  Create and return
2566    the TYPE_DECL for TYPE.  */
2567
2568 tree
2569 create_implicit_typedef (tree name, tree type)
2570 {
2571   tree decl;
2572
2573   decl = build_decl (TYPE_DECL, name, type);
2574   DECL_ARTIFICIAL (decl) = 1;
2575   /* There are other implicit type declarations, like the one *within*
2576      a class that allows you to write `S::S'.  We must distinguish
2577      amongst these.  */
2578   SET_DECL_IMPLICIT_TYPEDEF_P (decl);
2579   TYPE_NAME (type) = decl;
2580
2581   return decl;
2582 }
2583
2584 /* Remember a local name for name-mangling purposes.  */
2585
2586 static void
2587 push_local_name (tree decl)
2588 {
2589   size_t i, nelts;
2590   tree t, name;
2591
2592   timevar_push (TV_NAME_LOOKUP);
2593   if (!local_names)
2594     VARRAY_TREE_INIT (local_names, 8, "local_names");
2595
2596   name = DECL_NAME (decl);
2597
2598   nelts = VARRAY_ACTIVE_SIZE (local_names);
2599   for (i = 0; i < nelts; i++)
2600     {
2601       t = VARRAY_TREE (local_names, i);
2602       if (DECL_NAME (t) == name)
2603         {
2604           if (!DECL_LANG_SPECIFIC (decl))
2605             retrofit_lang_decl (decl);
2606           DECL_LANG_SPECIFIC (decl)->decl_flags.u2sel = 1;
2607           if (DECL_LANG_SPECIFIC (t))
2608             DECL_DISCRIMINATOR (decl) = DECL_DISCRIMINATOR (t) + 1;
2609           else
2610             DECL_DISCRIMINATOR (decl) = 1;
2611
2612           VARRAY_TREE (local_names, i) = decl;
2613           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
2614         }
2615     }
2616
2617   VARRAY_PUSH_TREE (local_names, decl);
2618   timevar_pop (TV_NAME_LOOKUP);
2619 }
2620
2621 /* Push a tag name NAME for struct/class/union/enum type TYPE.
2622    Normally put it into the inner-most non-tag-transparent scope,
2623    but if GLOBALIZE is true, put it in the inner-most non-class scope.
2624    The latter is needed for implicit declarations.  */
2625
2626 void
2627 pushtag (tree name, tree type, int globalize)
2628 {
2629   register struct cp_binding_level *b;
2630
2631   timevar_push (TV_NAME_LOOKUP);
2632   b = current_binding_level;
2633   while (b->tag_transparent
2634          || (b->parm_flag == 2
2635              && (globalize
2636                  /* We may be defining a new type in the initializer
2637                     of a static member variable. We allow this when
2638                     not pedantic, and it is particularly useful for
2639                     type punning via an anonymous union.  */
2640                  || COMPLETE_TYPE_P (b->this_class))))
2641     b = b->level_chain;
2642
2643   b->tags = tree_cons (name, type, b->tags);
2644
2645   if (name)
2646     {
2647       /* Do C++ gratuitous typedefing.  */
2648       if (IDENTIFIER_TYPE_VALUE (name) != type)
2649         {
2650           register tree d = NULL_TREE;
2651           int in_class = 0;
2652           tree context = TYPE_CONTEXT (type);
2653
2654           if (! context)
2655             {
2656               tree cs = current_scope ();
2657
2658               if (! globalize)
2659                 context = cs;
2660               else if (cs != NULL_TREE && TYPE_P (cs))
2661                 /* When declaring a friend class of a local class, we want
2662                    to inject the newly named class into the scope
2663                    containing the local class, not the namespace scope.  */
2664                 context = decl_function_context (get_type_decl (cs));
2665             }
2666           if (!context)
2667             context = current_namespace;
2668
2669           if ((b->template_parms_p && b->level_chain->parm_flag == 2)
2670               || b->parm_flag == 2)
2671             in_class = 1;
2672
2673           if (current_lang_name == lang_name_java)
2674             TYPE_FOR_JAVA (type) = 1;
2675
2676           d = create_implicit_typedef (name, type);
2677           DECL_CONTEXT (d) = FROB_CONTEXT (context);
2678           if (! in_class)
2679             set_identifier_type_value_with_scope (name, type, b);
2680
2681           d = maybe_process_template_type_declaration (type,
2682                                                        globalize, b);
2683
2684           if (b->parm_flag == 2)
2685             {
2686               if (!PROCESSING_REAL_TEMPLATE_DECL_P ())
2687                 /* Put this TYPE_DECL on the TYPE_FIELDS list for the
2688                    class.  But if it's a member template class, we
2689                    want the TEMPLATE_DECL, not the TYPE_DECL, so this
2690                    is done later.  */
2691                 finish_member_declaration (d);
2692               else
2693                 pushdecl_class_level (d);
2694             }
2695           else
2696             d = pushdecl_with_scope (d, b);
2697
2698           /* FIXME what if it gets a name from typedef?  */
2699           if (ANON_AGGRNAME_P (name))
2700             DECL_IGNORED_P (d) = 1;
2701
2702           TYPE_CONTEXT (type) = DECL_CONTEXT (d);
2703
2704           /* If this is a local class, keep track of it.  We need this
2705              information for name-mangling, and so that it is possible to find
2706              all function definitions in a translation unit in a convenient
2707              way.  (It's otherwise tricky to find a member function definition
2708              it's only pointed to from within a local class.)  */
2709           if (TYPE_CONTEXT (type)
2710               && TREE_CODE (TYPE_CONTEXT (type)) == FUNCTION_DECL
2711               && !processing_template_decl)
2712             VARRAY_PUSH_TREE (local_classes, type);
2713         }
2714       if (b->parm_flag == 2)
2715         {
2716           if (!COMPLETE_TYPE_P (current_class_type))
2717             {
2718               maybe_add_class_template_decl_list (current_class_type,
2719                                                   type, /*friend_p=*/0);
2720               CLASSTYPE_TAGS (current_class_type) = b->tags;
2721             }
2722         }
2723     }
2724
2725   if (TREE_CODE (TYPE_NAME (type)) == TYPE_DECL)
2726     /* Use the canonical TYPE_DECL for this node.  */
2727     TYPE_STUB_DECL (type) = TYPE_NAME (type);
2728   else
2729     {
2730       /* Create a fake NULL-named TYPE_DECL node whose TREE_TYPE
2731          will be the tagged type we just added to the current
2732          binding level.  This fake NULL-named TYPE_DECL node helps
2733          dwarfout.c to know when it needs to output a
2734          representation of a tagged type, and it also gives us a
2735          convenient place to record the "scope start" address for
2736          the tagged type.  */
2737
2738       tree d = build_decl (TYPE_DECL, NULL_TREE, type);
2739       TYPE_STUB_DECL (type) = pushdecl_with_scope (d, b);
2740     }
2741   timevar_pop (TV_NAME_LOOKUP);
2742 }
2743
2744 /* Counter used to create anonymous type names.  */
2745
2746 static GTY(()) int anon_cnt;
2747
2748 /* Return an IDENTIFIER which can be used as a name for
2749    anonymous structs and unions.  */
2750
2751 tree
2752 make_anon_name (void)
2753 {
2754   char buf[32];
2755
2756   sprintf (buf, ANON_AGGRNAME_FORMAT, anon_cnt++);
2757   return get_identifier (buf);
2758 }
2759
2760 /* Clear the TREE_PURPOSE slot of tags which have anonymous typenames.
2761    This keeps dbxout from getting confused.  */
2762
2763 void
2764 clear_anon_tags (void)
2765 {
2766   register struct cp_binding_level *b;
2767   register tree tags;
2768   static int last_cnt = 0;
2769
2770   /* Fast out if no new anon names were declared.  */
2771   if (last_cnt == anon_cnt)
2772     return;
2773
2774   b = current_binding_level;
2775   while (b->tag_transparent)
2776     b = b->level_chain;
2777   tags = b->tags;
2778   while (tags)
2779     {
2780       /* A NULL purpose means we have already processed all tags
2781          from here to the end of the list.  */
2782       if (TREE_PURPOSE (tags) == NULL_TREE)
2783         break;
2784       if (ANON_AGGRNAME_P (TREE_PURPOSE (tags)))
2785         TREE_PURPOSE (tags) = NULL_TREE;
2786       tags = TREE_CHAIN (tags);
2787     }
2788   last_cnt = anon_cnt;
2789 }
2790 \f
2791 /* Subroutine of duplicate_decls: return truthvalue of whether
2792    or not types of these decls match.
2793
2794    For C++, we must compare the parameter list so that `int' can match
2795    `int&' in a parameter position, but `int&' is not confused with
2796    `const int&'.  */
2797
2798 int
2799 decls_match (tree newdecl, tree olddecl)
2800 {
2801   int types_match;
2802
2803   if (newdecl == olddecl)
2804     return 1;
2805
2806   if (TREE_CODE (newdecl) != TREE_CODE (olddecl))
2807     /* If the two DECLs are not even the same kind of thing, we're not
2808        interested in their types.  */
2809     return 0;
2810
2811   if (TREE_CODE (newdecl) == FUNCTION_DECL)
2812     {
2813       tree f1 = TREE_TYPE (newdecl);
2814       tree f2 = TREE_TYPE (olddecl);
2815       tree p1 = TYPE_ARG_TYPES (f1);
2816       tree p2 = TYPE_ARG_TYPES (f2);
2817
2818       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl)
2819           && ! (DECL_EXTERN_C_P (newdecl)
2820                 && DECL_EXTERN_C_P (olddecl)))
2821         return 0;
2822
2823       if (TREE_CODE (f1) != TREE_CODE (f2))
2824         return 0;
2825
2826       if (same_type_p (TREE_TYPE (f1), TREE_TYPE (f2)))
2827         {
2828           if (p2 == NULL_TREE && DECL_EXTERN_C_P (olddecl)
2829               && (DECL_BUILT_IN (olddecl)
2830 #ifndef NO_IMPLICIT_EXTERN_C
2831                   || (DECL_IN_SYSTEM_HEADER (newdecl) && !DECL_CLASS_SCOPE_P (newdecl))
2832                   || (DECL_IN_SYSTEM_HEADER (olddecl) && !DECL_CLASS_SCOPE_P (olddecl))
2833 #endif
2834               ))
2835             {
2836               types_match = self_promoting_args_p (p1);
2837               if (p1 == void_list_node)
2838                 TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2839             }
2840 #ifndef NO_IMPLICIT_EXTERN_C
2841           else if (p1 == NULL_TREE
2842                    && (DECL_EXTERN_C_P (olddecl)
2843                        && DECL_IN_SYSTEM_HEADER (olddecl)
2844                        && !DECL_CLASS_SCOPE_P (olddecl))
2845                    && (DECL_EXTERN_C_P (newdecl)
2846                        && DECL_IN_SYSTEM_HEADER (newdecl)
2847                        && !DECL_CLASS_SCOPE_P (newdecl)))
2848             {
2849               types_match = self_promoting_args_p (p2);
2850               TREE_TYPE (newdecl) = TREE_TYPE (olddecl);
2851             }
2852 #endif
2853           else
2854             types_match = compparms (p1, p2);
2855         }
2856       else
2857         types_match = 0;
2858     }
2859   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
2860     {
2861       if (!comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
2862                                 DECL_TEMPLATE_PARMS (olddecl)))
2863         return 0;
2864
2865       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl))
2866           != TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)))
2867         return 0;
2868
2869       if (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
2870         types_match = 1;
2871       else
2872         types_match = decls_match (DECL_TEMPLATE_RESULT (olddecl),
2873                                    DECL_TEMPLATE_RESULT (newdecl));
2874     }
2875   else
2876     {
2877       if (TREE_TYPE (newdecl) == error_mark_node)
2878         types_match = TREE_TYPE (olddecl) == error_mark_node;
2879       else if (TREE_TYPE (olddecl) == NULL_TREE)
2880         types_match = TREE_TYPE (newdecl) == NULL_TREE;
2881       else if (TREE_TYPE (newdecl) == NULL_TREE)
2882         types_match = 0;
2883       else
2884         types_match = comptypes (TREE_TYPE (newdecl),
2885                                  TREE_TYPE (olddecl),
2886                                  COMPARE_REDECLARATION);
2887     }
2888
2889   return types_match;
2890 }
2891
2892 /* If NEWDECL is `static' and an `extern' was seen previously,
2893    warn about it.  OLDDECL is the previous declaration.
2894
2895    Note that this does not apply to the C++ case of declaring
2896    a variable `extern const' and then later `const'.
2897
2898    Don't complain about built-in functions, since they are beyond
2899    the user's control.  */
2900
2901 static void
2902 warn_extern_redeclared_static (tree newdecl, tree olddecl)
2903 {
2904   static const char *const explicit_extern_static_warning
2905     = "`%D' was declared `extern' and later `static'";
2906   static const char *const implicit_extern_static_warning
2907     = "`%D' was declared implicitly `extern' and later `static'";
2908
2909   tree name;
2910
2911   if (TREE_CODE (newdecl) == TYPE_DECL
2912       || TREE_CODE (newdecl) == TEMPLATE_DECL
2913       || TREE_CODE (newdecl) == CONST_DECL)
2914     return;
2915
2916   /* Don't get confused by static member functions; that's a different
2917      use of `static'.  */
2918   if (TREE_CODE (newdecl) == FUNCTION_DECL
2919       && DECL_STATIC_FUNCTION_P (newdecl))
2920     return;
2921
2922   /* If the old declaration was `static', or the new one isn't, then
2923      then everything is OK.  */
2924   if (DECL_THIS_STATIC (olddecl) || !DECL_THIS_STATIC (newdecl))
2925     return;
2926
2927   /* It's OK to declare a builtin function as `static'.  */
2928   if (TREE_CODE (olddecl) == FUNCTION_DECL
2929       && DECL_ARTIFICIAL (olddecl))
2930     return;
2931
2932   name = DECL_ASSEMBLER_NAME (newdecl);
2933   pedwarn (IDENTIFIER_IMPLICIT_DECL (name)
2934               ? implicit_extern_static_warning
2935               : explicit_extern_static_warning, newdecl);
2936   cp_pedwarn_at ("previous declaration of `%D'", olddecl);
2937 }
2938
2939 /* Handle when a new declaration NEWDECL has the same name as an old
2940    one OLDDECL in the same binding contour.  Prints an error message
2941    if appropriate.
2942
2943    If safely possible, alter OLDDECL to look like NEWDECL, and return 1.
2944    Otherwise, return 0.  */
2945
2946 int
2947 duplicate_decls (tree newdecl, tree olddecl)
2948 {
2949   unsigned olddecl_uid = DECL_UID (olddecl);
2950   int olddecl_friend = 0, types_match = 0;
2951   int new_defines_function = 0;
2952
2953   if (newdecl == olddecl)
2954     return 1;
2955
2956   types_match = decls_match (newdecl, olddecl);
2957
2958   /* If either the type of the new decl or the type of the old decl is an
2959      error_mark_node, then that implies that we have already issued an
2960      error (earlier) for some bogus type specification, and in that case,
2961      it is rather pointless to harass the user with yet more error message
2962      about the same declaration, so just pretend the types match here.  */
2963   if (TREE_TYPE (newdecl) == error_mark_node
2964       || TREE_TYPE (olddecl) == error_mark_node)
2965     types_match = 1;
2966
2967   if (DECL_P (olddecl)
2968       && TREE_CODE (newdecl) == FUNCTION_DECL
2969       && TREE_CODE (olddecl) == FUNCTION_DECL
2970       && (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl)))
2971     {
2972       if (DECL_DECLARED_INLINE_P (newdecl)
2973           && DECL_UNINLINABLE (newdecl)
2974           && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2975         /* Already warned elsewhere.  */;
2976       else if (DECL_DECLARED_INLINE_P (olddecl)
2977                && DECL_UNINLINABLE (olddecl)
2978                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2979         /* Already warned.  */;
2980       else if (DECL_DECLARED_INLINE_P (newdecl)
2981                && DECL_UNINLINABLE (olddecl)
2982                && lookup_attribute ("noinline", DECL_ATTRIBUTES (olddecl)))
2983         {
2984           warning ("%Hfunction '%D' redeclared as inline",
2985                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2986           warning ("%Hprevious declaration of '%D' with attribute noinline",
2987                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2988         }
2989       else if (DECL_DECLARED_INLINE_P (olddecl)
2990                && DECL_UNINLINABLE (newdecl)
2991                && lookup_attribute ("noinline", DECL_ATTRIBUTES (newdecl)))
2992         {
2993           warning ("%Hfunction '%D' redeclared with attribute noinline",
2994                    &DECL_SOURCE_LOCATION (newdecl), newdecl);
2995           warning ("%Hprevious declaration of '%D' was inline",
2996                    &DECL_SOURCE_LOCATION (olddecl), olddecl);
2997         }
2998     }
2999
3000   /* Check for redeclaration and other discrepancies.  */
3001   if (TREE_CODE (olddecl) == FUNCTION_DECL
3002       && DECL_ARTIFICIAL (olddecl))
3003     {
3004       if (TREE_CODE (newdecl) != FUNCTION_DECL)
3005         {
3006           /* Avoid warnings redeclaring anticipated built-ins.  */
3007           if (DECL_ANTICIPATED (olddecl))
3008             return 0;
3009
3010           /* If you declare a built-in or predefined function name as static,
3011              the old definition is overridden, but optionally warn this was a
3012              bad choice of name.  */
3013           if (! TREE_PUBLIC (newdecl))
3014             {
3015               if (warn_shadow)
3016                 warning ("shadowing %s function `%#D'",
3017                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3018                             olddecl);
3019               /* Discard the old built-in function.  */
3020               return 0;
3021             }
3022           /* If the built-in is not ansi, then programs can override
3023              it even globally without an error.  */
3024           else if (! DECL_BUILT_IN (olddecl))
3025             warning ("library function `%#D' redeclared as non-function `%#D'",
3026                         olddecl, newdecl);
3027           else
3028             {
3029               error ("declaration of `%#D'", newdecl);
3030               error ("conflicts with built-in declaration `%#D'",
3031                         olddecl);
3032             }
3033           return 0;
3034         }
3035       else if (!types_match)
3036         {
3037           /* Avoid warnings redeclaring anticipated built-ins.  */
3038           if (DECL_ANTICIPATED (olddecl))
3039             ;  /* Do nothing yet.  */
3040           else if ((DECL_EXTERN_C_P (newdecl)
3041                && DECL_EXTERN_C_P (olddecl))
3042               || compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3043                             TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3044             {
3045               /* A near match; override the builtin.  */
3046
3047               if (TREE_PUBLIC (newdecl))
3048                 {
3049                   warning ("new declaration `%#D'", newdecl);
3050                   warning ("ambiguates built-in declaration `%#D'",
3051                               olddecl);
3052                 }
3053               else if (warn_shadow)
3054                 warning ("shadowing %s function `%#D'",
3055                             DECL_BUILT_IN (olddecl) ? "built-in" : "library",
3056                             olddecl);
3057             }
3058           else
3059             /* Discard the old built-in function.  */
3060             return 0;
3061
3062           /* Replace the old RTL to avoid problems with inlining.  */
3063           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3064         }
3065
3066       if (DECL_THIS_STATIC (newdecl) && !DECL_THIS_STATIC (olddecl))
3067         {
3068           /* If a builtin function is redeclared as `static', merge
3069              the declarations, but make the original one static.  */
3070           DECL_THIS_STATIC (olddecl) = 1;
3071           TREE_PUBLIC (olddecl) = 0;
3072
3073           /* Make the old declaration consistent with the new one so
3074              that all remnants of the builtin-ness of this function
3075              will be banished.  */
3076           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3077           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3078         }
3079     }
3080   else if (TREE_CODE (olddecl) != TREE_CODE (newdecl))
3081     {
3082       if ((TREE_CODE (olddecl) == TYPE_DECL && DECL_ARTIFICIAL (olddecl)
3083            && TREE_CODE (newdecl) != TYPE_DECL
3084            && ! (TREE_CODE (newdecl) == TEMPLATE_DECL
3085                  && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL))
3086           || (TREE_CODE (newdecl) == TYPE_DECL && DECL_ARTIFICIAL (newdecl)
3087               && TREE_CODE (olddecl) != TYPE_DECL
3088               && ! (TREE_CODE (olddecl) == TEMPLATE_DECL
3089                     && (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl))
3090                         == TYPE_DECL))))
3091         {
3092           /* We do nothing special here, because C++ does such nasty
3093              things with TYPE_DECLs.  Instead, just let the TYPE_DECL
3094              get shadowed, and know that if we need to find a TYPE_DECL
3095              for a given name, we can look in the IDENTIFIER_TYPE_VALUE
3096              slot of the identifier.  */
3097           return 0;
3098         }
3099
3100       if ((TREE_CODE (newdecl) == FUNCTION_DECL
3101            && DECL_FUNCTION_TEMPLATE_P (olddecl))
3102           || (TREE_CODE (olddecl) == FUNCTION_DECL
3103               && DECL_FUNCTION_TEMPLATE_P (newdecl)))
3104         return 0;
3105
3106       error ("`%#D' redeclared as different kind of symbol", newdecl);
3107       if (TREE_CODE (olddecl) == TREE_LIST)
3108         olddecl = TREE_VALUE (olddecl);
3109       cp_error_at ("previous declaration of `%#D'", olddecl);
3110
3111       /* New decl is completely inconsistent with the old one =>
3112          tell caller to replace the old one.  */
3113
3114       return 0;
3115     }
3116   else if (!types_match)
3117     {
3118       if (CP_DECL_CONTEXT (newdecl) != CP_DECL_CONTEXT (olddecl))
3119         /* These are certainly not duplicate declarations; they're
3120            from different scopes.  */
3121         return 0;
3122
3123       if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3124         {
3125           /* The name of a class template may not be declared to refer to
3126              any other template, class, function, object, namespace, value,
3127              or type in the same scope.  */
3128           if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == TYPE_DECL
3129               || TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL)
3130             {
3131               error ("declaration of template `%#D'", newdecl);
3132               cp_error_at ("conflicts with previous declaration `%#D'",
3133                            olddecl);
3134             }
3135           else if (TREE_CODE (DECL_TEMPLATE_RESULT (olddecl)) == FUNCTION_DECL
3136                    && TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
3137                    && compparms (TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl))),
3138                                  TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (newdecl))))
3139                    && comp_template_parms (DECL_TEMPLATE_PARMS (newdecl),
3140                                            DECL_TEMPLATE_PARMS (olddecl))
3141                    /* Template functions can be disambiguated by
3142                       return type.  */
3143                    && same_type_p (TREE_TYPE (TREE_TYPE (newdecl)),
3144                                    TREE_TYPE (TREE_TYPE (olddecl))))
3145             {
3146               error ("new declaration `%#D'", newdecl);
3147               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3148             }
3149           return 0;
3150         }
3151       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3152         {
3153           if (DECL_EXTERN_C_P (newdecl) && DECL_EXTERN_C_P (olddecl))
3154             {
3155               error ("declaration of C function `%#D' conflicts with",
3156                         newdecl);
3157               cp_error_at ("previous declaration `%#D' here", olddecl);
3158             }
3159           else if (compparms (TYPE_ARG_TYPES (TREE_TYPE (newdecl)),
3160                               TYPE_ARG_TYPES (TREE_TYPE (olddecl))))
3161             {
3162               error ("new declaration `%#D'", newdecl);
3163               cp_error_at ("ambiguates old declaration `%#D'", olddecl);
3164             }
3165           else
3166             return 0;
3167         }
3168
3169       /* Already complained about this, so don't do so again.  */
3170       else if (current_class_type == NULL_TREE
3171           || IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (newdecl)) != current_class_type)
3172         {
3173           error ("conflicting types for `%#D'", newdecl);
3174           cp_error_at ("previous declaration as `%#D'", olddecl);
3175         }
3176     }
3177   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3178             && ((DECL_TEMPLATE_SPECIALIZATION (olddecl)
3179                  && (!DECL_TEMPLATE_INFO (newdecl)
3180                      || (DECL_TI_TEMPLATE (newdecl)
3181                          != DECL_TI_TEMPLATE (olddecl))))
3182                 || (DECL_TEMPLATE_SPECIALIZATION (newdecl)
3183                     && (!DECL_TEMPLATE_INFO (olddecl)
3184                         || (DECL_TI_TEMPLATE (olddecl)
3185                             != DECL_TI_TEMPLATE (newdecl))))))
3186     /* It's OK to have a template specialization and a non-template
3187        with the same type, or to have specializations of two
3188        different templates with the same type.  Note that if one is a
3189        specialization, and the other is an instantiation of the same
3190        template, that we do not exit at this point.  That situation
3191        can occur if we instantiate a template class, and then
3192        specialize one of its methods.  This situation is valid, but
3193        the declarations must be merged in the usual way.  */
3194     return 0;
3195   else if (TREE_CODE (newdecl) == FUNCTION_DECL
3196            && ((DECL_TEMPLATE_INSTANTIATION (olddecl)
3197                 && !DECL_USE_TEMPLATE (newdecl))
3198                || (DECL_TEMPLATE_INSTANTIATION (newdecl)
3199                    && !DECL_USE_TEMPLATE (olddecl))))
3200     /* One of the declarations is a template instantiation, and the
3201        other is not a template at all.  That's OK.  */
3202     return 0;
3203   else if (TREE_CODE (newdecl) == NAMESPACE_DECL
3204            && DECL_NAMESPACE_ALIAS (newdecl)
3205            && DECL_NAMESPACE_ALIAS (newdecl) == DECL_NAMESPACE_ALIAS (olddecl))
3206     /* Redeclaration of namespace alias, ignore it.  */
3207     return 1;
3208   else
3209     {
3210       const char *errmsg = redeclaration_error_message (newdecl, olddecl);
3211       if (errmsg)
3212         {
3213           error (errmsg, newdecl);
3214           if (DECL_NAME (olddecl) != NULL_TREE)
3215             cp_error_at ((DECL_INITIAL (olddecl)
3216                           && namespace_bindings_p ())
3217                          ? "`%#D' previously defined here"
3218                          : "`%#D' previously declared here", olddecl);
3219           return 0;
3220         }
3221       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3222                && DECL_INITIAL (olddecl) != NULL_TREE
3223                && TYPE_ARG_TYPES (TREE_TYPE (olddecl)) == NULL_TREE
3224                && TYPE_ARG_TYPES (TREE_TYPE (newdecl)) != NULL_TREE)
3225         {
3226           /* Prototype decl follows defn w/o prototype.  */
3227           cp_warning_at ("prototype for `%#D'", newdecl);
3228           cp_warning_at ("follows non-prototype definition here", olddecl);
3229         }
3230       else if (TREE_CODE (olddecl) == FUNCTION_DECL
3231                && DECL_LANGUAGE (newdecl) != DECL_LANGUAGE (olddecl))
3232         {
3233           /* extern "C" int foo ();
3234              int foo () { bar (); }
3235              is OK.  */
3236           if (current_lang_depth () == 0)
3237             SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3238           else
3239             {
3240               cp_error_at ("previous declaration of `%#D' with %L linkage",
3241                            olddecl, DECL_LANGUAGE (olddecl));
3242               error ("conflicts with new declaration with %L linkage",
3243                         DECL_LANGUAGE (newdecl));
3244             }
3245         }
3246
3247       if (DECL_LANG_SPECIFIC (olddecl) && DECL_USE_TEMPLATE (olddecl))
3248         ;
3249       else if (TREE_CODE (olddecl) == FUNCTION_DECL)
3250         {
3251           tree t1 = TYPE_ARG_TYPES (TREE_TYPE (olddecl));
3252           tree t2 = TYPE_ARG_TYPES (TREE_TYPE (newdecl));
3253           int i = 1;
3254
3255           if (TREE_CODE (TREE_TYPE (newdecl)) == METHOD_TYPE)
3256             t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2);
3257
3258           for (; t1 && t1 != void_list_node;
3259                t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2), i++)
3260             if (TREE_PURPOSE (t1) && TREE_PURPOSE (t2))
3261               {
3262                 if (1 == simple_cst_equal (TREE_PURPOSE (t1),
3263                                            TREE_PURPOSE (t2)))
3264                   {
3265                     pedwarn ("default argument given for parameter %d of `%#D'",
3266                              i, newdecl);
3267                     cp_pedwarn_at ("after previous specification in `%#D'",
3268                                    olddecl);
3269                   }
3270                 else
3271                   {
3272                     error ("default argument given for parameter %d of `%#D'",
3273                               i, newdecl);
3274                     cp_error_at ("after previous specification in `%#D'",
3275                                  olddecl);
3276                   }
3277               }
3278
3279           if (DECL_DECLARED_INLINE_P (newdecl) 
3280               && ! DECL_DECLARED_INLINE_P (olddecl)
3281               && TREE_ADDRESSABLE (olddecl) && warn_inline)
3282             {
3283               warning ("`%#D' was used before it was declared inline",
3284                           newdecl);
3285               cp_warning_at ("previous non-inline declaration here",
3286                              olddecl);
3287             }
3288         }
3289     }
3290
3291   /* Do not merge an implicit typedef with an explicit one.  In:
3292
3293        class A;
3294        ...
3295        typedef class A A __attribute__ ((foo));
3296
3297      the attribute should apply only to the typedef.  */
3298   if (TREE_CODE (olddecl) == TYPE_DECL
3299       && (DECL_IMPLICIT_TYPEDEF_P (olddecl)
3300           || DECL_IMPLICIT_TYPEDEF_P (newdecl)))
3301     return 0;
3302
3303   /* If new decl is `static' and an `extern' was seen previously,
3304      warn about it.  */
3305   warn_extern_redeclared_static (newdecl, olddecl);
3306
3307   /* We have committed to returning 1 at this point.  */
3308   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3309     {
3310       /* Now that functions must hold information normally held
3311          by field decls, there is extra work to do so that
3312          declaration information does not get destroyed during
3313          definition.  */
3314       if (DECL_VINDEX (olddecl))
3315         DECL_VINDEX (newdecl) = DECL_VINDEX (olddecl);
3316       if (DECL_CONTEXT (olddecl))
3317         DECL_CONTEXT (newdecl) = DECL_CONTEXT (olddecl);
3318       DECL_STATIC_CONSTRUCTOR (newdecl) |= DECL_STATIC_CONSTRUCTOR (olddecl);
3319       DECL_STATIC_DESTRUCTOR (newdecl) |= DECL_STATIC_DESTRUCTOR (olddecl);
3320       DECL_PURE_VIRTUAL_P (newdecl) |= DECL_PURE_VIRTUAL_P (olddecl);
3321       DECL_VIRTUAL_P (newdecl) |= DECL_VIRTUAL_P (olddecl);
3322       DECL_NEEDS_FINAL_OVERRIDER_P (newdecl) |= DECL_NEEDS_FINAL_OVERRIDER_P (olddecl);
3323       DECL_THIS_STATIC (newdecl) |= DECL_THIS_STATIC (olddecl);
3324       if (DECL_OVERLOADED_OPERATOR_P (olddecl) != ERROR_MARK)
3325         SET_OVERLOADED_OPERATOR_CODE
3326           (newdecl, DECL_OVERLOADED_OPERATOR_P (olddecl));
3327       new_defines_function = DECL_INITIAL (newdecl) != NULL_TREE;
3328
3329       /* Optionally warn about more than one declaration for the same
3330          name, but don't warn about a function declaration followed by a
3331          definition.  */
3332       if (warn_redundant_decls && ! DECL_ARTIFICIAL (olddecl)
3333           && !(new_defines_function && DECL_INITIAL (olddecl) == NULL_TREE)
3334           /* Don't warn about extern decl followed by definition.  */
3335           && !(DECL_EXTERNAL (olddecl) && ! DECL_EXTERNAL (newdecl))
3336           /* Don't warn about friends, let add_friend take care of it.  */
3337           && ! (DECL_FRIEND_P (newdecl) || DECL_FRIEND_P (olddecl)))
3338         {
3339           warning ("redundant redeclaration of `%D' in same scope", newdecl);
3340           cp_warning_at ("previous declaration of `%D'", olddecl);
3341         }
3342     }
3343
3344   /* Deal with C++: must preserve virtual function table size.  */
3345   if (TREE_CODE (olddecl) == TYPE_DECL)
3346     {
3347       register tree newtype = TREE_TYPE (newdecl);
3348       register tree oldtype = TREE_TYPE (olddecl);
3349
3350       if (newtype != error_mark_node && oldtype != error_mark_node
3351           && TYPE_LANG_SPECIFIC (newtype) && TYPE_LANG_SPECIFIC (oldtype))
3352         CLASSTYPE_FRIEND_CLASSES (newtype)
3353           = CLASSTYPE_FRIEND_CLASSES (oldtype);
3354 \
3355       DECL_ORIGINAL_TYPE (newdecl) = DECL_ORIGINAL_TYPE (olddecl);
3356     }
3357
3358   /* Copy all the DECL_... slots specified in the new decl
3359      except for any that we copy here from the old type.  */
3360   DECL_ATTRIBUTES (newdecl)
3361     = (*targetm.merge_decl_attributes) (olddecl, newdecl);
3362
3363   if (TREE_CODE (newdecl) == TEMPLATE_DECL)
3364     {
3365       TREE_TYPE (olddecl) = TREE_TYPE (DECL_TEMPLATE_RESULT (olddecl));
3366       DECL_TEMPLATE_SPECIALIZATIONS (olddecl)
3367         = chainon (DECL_TEMPLATE_SPECIALIZATIONS (olddecl),
3368                    DECL_TEMPLATE_SPECIALIZATIONS (newdecl));
3369
3370       /* If the new declaration is a definition, update the file and
3371          line information on the declaration.  */
3372       if (DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)) == NULL_TREE
3373           && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl)) != NULL_TREE)
3374         {
3375           DECL_SOURCE_LOCATION (olddecl) 
3376             = DECL_SOURCE_LOCATION (DECL_TEMPLATE_RESULT (olddecl))
3377             = DECL_SOURCE_LOCATION (newdecl);
3378         }
3379
3380       return 1;
3381     }
3382
3383   if (types_match)
3384     {
3385       /* Automatically handles default parameters.  */
3386       tree oldtype = TREE_TYPE (olddecl);
3387       tree newtype;
3388
3389       /* Merge the data types specified in the two decls.  */
3390       newtype = merge_types (TREE_TYPE (newdecl), TREE_TYPE (olddecl));
3391
3392       /* If merge_types produces a non-typedef type, just use the old type.  */
3393       if (TREE_CODE (newdecl) == TYPE_DECL
3394           && newtype == DECL_ORIGINAL_TYPE (newdecl))
3395         newtype = oldtype;
3396
3397       if (TREE_CODE (newdecl) == VAR_DECL)
3398         {
3399           DECL_THIS_EXTERN (newdecl) |= DECL_THIS_EXTERN (olddecl);
3400           DECL_INITIALIZED_P (newdecl) |= DECL_INITIALIZED_P (olddecl);
3401         }
3402
3403       /* Do this after calling `merge_types' so that default
3404          parameters don't confuse us.  */
3405       else if (TREE_CODE (newdecl) == FUNCTION_DECL
3406           && (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl))
3407               != TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl))))
3408         {
3409           TREE_TYPE (newdecl) = build_exception_variant (newtype,
3410                                                          TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)));
3411           TREE_TYPE (olddecl) = build_exception_variant (newtype,
3412                                                          TYPE_RAISES_EXCEPTIONS (oldtype));
3413
3414           if ((pedantic || ! DECL_IN_SYSTEM_HEADER (olddecl))
3415               && DECL_SOURCE_LINE (olddecl) != 0
3416               && flag_exceptions
3417               && !comp_except_specs (TYPE_RAISES_EXCEPTIONS (TREE_TYPE (newdecl)),
3418                                      TYPE_RAISES_EXCEPTIONS (TREE_TYPE (olddecl)), 1))
3419             {
3420               error ("declaration of `%F' throws different exceptions",
3421                         newdecl);
3422               cp_error_at ("than previous declaration `%F'", olddecl);
3423             }
3424         }
3425       TREE_TYPE (newdecl) = TREE_TYPE (olddecl) = newtype;
3426
3427       /* Lay the type out, unless already done.  */
3428       if (! same_type_p (newtype, oldtype)
3429           && TREE_TYPE (newdecl) != error_mark_node
3430           && !(processing_template_decl && uses_template_parms (newdecl)))
3431         layout_type (TREE_TYPE (newdecl));
3432
3433       if ((TREE_CODE (newdecl) == VAR_DECL
3434            || TREE_CODE (newdecl) == PARM_DECL
3435            || TREE_CODE (newdecl) == RESULT_DECL
3436            || TREE_CODE (newdecl) == FIELD_DECL
3437            || TREE_CODE (newdecl) == TYPE_DECL)
3438           && !(processing_template_decl && uses_template_parms (newdecl)))
3439         layout_decl (newdecl, 0);
3440
3441       /* Merge the type qualifiers.  */
3442       if (TREE_READONLY (newdecl))
3443         TREE_READONLY (olddecl) = 1;
3444       if (TREE_THIS_VOLATILE (newdecl))
3445         TREE_THIS_VOLATILE (olddecl) = 1;
3446
3447       /* Merge the initialization information.  */
3448       if (DECL_INITIAL (newdecl) == NULL_TREE
3449           && DECL_INITIAL (olddecl) != NULL_TREE)
3450         {
3451           DECL_INITIAL (newdecl) = DECL_INITIAL (olddecl);
3452           DECL_SOURCE_LOCATION (newdecl) = DECL_SOURCE_LOCATION (olddecl);
3453           if (CAN_HAVE_FULL_LANG_DECL_P (newdecl)
3454               && DECL_LANG_SPECIFIC (newdecl)
3455               && DECL_LANG_SPECIFIC (olddecl))
3456             DECL_SAVED_TREE (newdecl) = DECL_SAVED_TREE (olddecl);
3457         }
3458
3459       /* Merge the section attribute.
3460          We want to issue an error if the sections conflict but that must be
3461          done later in decl_attributes since we are called before attributes
3462          are assigned.  */
3463       if (DECL_SECTION_NAME (newdecl) == NULL_TREE)
3464         DECL_SECTION_NAME (newdecl) = DECL_SECTION_NAME (olddecl);
3465
3466       if (TREE_CODE (newdecl) == FUNCTION_DECL)
3467         {
3468           DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (newdecl)
3469             |= DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (olddecl);
3470           DECL_NO_LIMIT_STACK (newdecl)
3471             |= DECL_NO_LIMIT_STACK (olddecl);
3472           /* Keep the old RTL.  */
3473           COPY_DECL_RTL (olddecl, newdecl);
3474         }
3475       else if (TREE_CODE (newdecl) == VAR_DECL 
3476                && (DECL_SIZE (olddecl) || !DECL_SIZE (newdecl)))
3477         {
3478           /* Keep the old RTL.  We cannot keep the old RTL if the old
3479              declaration was for an incomplete object and the new
3480              declaration is not since many attributes of the RTL will
3481              change.  */
3482           COPY_DECL_RTL (olddecl, newdecl);
3483         }
3484     }
3485   /* If cannot merge, then use the new type and qualifiers,
3486      and don't preserve the old rtl.  */
3487   else
3488     {
3489       /* Clean out any memory we had of the old declaration.  */
3490       tree oldstatic = value_member (olddecl, static_aggregates);
3491       if (oldstatic)
3492         TREE_VALUE (oldstatic) = error_mark_node;
3493
3494       TREE_TYPE (olddecl) = TREE_TYPE (newdecl);
3495       TREE_READONLY (olddecl) = TREE_READONLY (newdecl);
3496       TREE_THIS_VOLATILE (olddecl) = TREE_THIS_VOLATILE (newdecl);
3497       TREE_SIDE_EFFECTS (olddecl) = TREE_SIDE_EFFECTS (newdecl);
3498     }
3499
3500   /* Merge the storage class information.  */
3501   merge_weak (newdecl, olddecl);
3502
3503   DECL_ONE_ONLY (newdecl) |= DECL_ONE_ONLY (olddecl);
3504   DECL_DEFER_OUTPUT (newdecl) |= DECL_DEFER_OUTPUT (olddecl);
3505   TREE_PUBLIC (newdecl) = TREE_PUBLIC (olddecl);
3506   TREE_STATIC (olddecl) = TREE_STATIC (newdecl) |= TREE_STATIC (olddecl);
3507   if (! DECL_EXTERNAL (olddecl))
3508     DECL_EXTERNAL (newdecl) = 0;
3509
3510   if (DECL_LANG_SPECIFIC (newdecl) && DECL_LANG_SPECIFIC (olddecl))
3511     {
3512       DECL_INTERFACE_KNOWN (newdecl) |= DECL_INTERFACE_KNOWN (olddecl);
3513       DECL_NOT_REALLY_EXTERN (newdecl) |= DECL_NOT_REALLY_EXTERN (olddecl);
3514       DECL_COMDAT (newdecl) |= DECL_COMDAT (olddecl);
3515       DECL_TEMPLATE_INSTANTIATED (newdecl)
3516         |= DECL_TEMPLATE_INSTANTIATED (olddecl);
3517       /* Don't really know how much of the language-specific
3518          values we should copy from old to new.  */
3519       DECL_IN_AGGR_P (newdecl) = DECL_IN_AGGR_P (olddecl);
3520       DECL_LANG_SPECIFIC (newdecl)->decl_flags.u2 = 
3521         DECL_LANG_SPECIFIC (olddecl)->decl_flags.u2;
3522       DECL_NONCONVERTING_P (newdecl) = DECL_NONCONVERTING_P (olddecl);
3523       DECL_TEMPLATE_INFO (newdecl) = DECL_TEMPLATE_INFO (olddecl);
3524       DECL_INITIALIZED_IN_CLASS_P (newdecl)
3525         |= DECL_INITIALIZED_IN_CLASS_P (olddecl);
3526       olddecl_friend = DECL_FRIEND_P (olddecl);
3527
3528       /* Only functions have DECL_BEFRIENDING_CLASSES.  */
3529       if (TREE_CODE (newdecl) == FUNCTION_DECL
3530           || DECL_FUNCTION_TEMPLATE_P (newdecl))
3531         {
3532           DECL_BEFRIENDING_CLASSES (newdecl)
3533             = chainon (DECL_BEFRIENDING_CLASSES (newdecl),
3534                        DECL_BEFRIENDING_CLASSES (olddecl));
3535           DECL_THUNKS (newdecl) = DECL_THUNKS (olddecl);
3536         }
3537     }
3538
3539   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3540     {
3541       if (DECL_TEMPLATE_INSTANTIATION (olddecl)
3542           && !DECL_TEMPLATE_INSTANTIATION (newdecl))
3543         {
3544           /* If newdecl is not a specialization, then it is not a
3545              template-related function at all.  And that means that we
3546              should have exited above, returning 0.  */
3547           my_friendly_assert (DECL_TEMPLATE_SPECIALIZATION (newdecl),
3548                               0);
3549
3550           if (TREE_USED (olddecl))
3551             /* From [temp.expl.spec]:
3552
3553                If a template, a member template or the member of a class
3554                template is explicitly specialized then that
3555                specialization shall be declared before the first use of
3556                that specialization that would cause an implicit
3557                instantiation to take place, in every translation unit in
3558                which such a use occurs.  */
3559             error ("explicit specialization of %D after first use",
3560                       olddecl);
3561
3562           SET_DECL_TEMPLATE_SPECIALIZATION (olddecl);
3563
3564           /* [temp.expl.spec/14] We don't inline explicit specialization
3565              just because the primary template says so.  */
3566         }
3567       else
3568         {
3569           if (DECL_PENDING_INLINE_INFO (newdecl) == 0)
3570             DECL_PENDING_INLINE_INFO (newdecl) = DECL_PENDING_INLINE_INFO (olddecl);
3571
3572           DECL_DECLARED_INLINE_P (newdecl) |= DECL_DECLARED_INLINE_P (olddecl);
3573
3574           /* If either decl says `inline', this fn is inline, unless 
3575              its definition was passed already.  */
3576           if (DECL_INLINE (newdecl) && DECL_INITIAL (olddecl) == NULL_TREE)
3577             DECL_INLINE (olddecl) = 1;
3578           DECL_INLINE (newdecl) = DECL_INLINE (olddecl);
3579
3580           DECL_UNINLINABLE (newdecl) = DECL_UNINLINABLE (olddecl)
3581             = (DECL_UNINLINABLE (newdecl) || DECL_UNINLINABLE (olddecl));
3582         }
3583
3584       /* Preserve abstractness on cloned [cd]tors.  */
3585       DECL_ABSTRACT (newdecl) = DECL_ABSTRACT (olddecl);
3586
3587       if (! types_match)
3588         {
3589           SET_DECL_LANGUAGE (olddecl, DECL_LANGUAGE (newdecl));
3590           COPY_DECL_ASSEMBLER_NAME (newdecl, olddecl);
3591           SET_DECL_RTL (olddecl, DECL_RTL (newdecl));
3592         }
3593       if (! types_match || new_defines_function)
3594         {
3595           /* These need to be copied so that the names are available.
3596              Note that if the types do match, we'll preserve inline
3597              info and other bits, but if not, we won't.  */
3598           DECL_ARGUMENTS (olddecl) = DECL_ARGUMENTS (newdecl);
3599           DECL_RESULT (olddecl) = DECL_RESULT (newdecl);
3600         }
3601       if (new_defines_function)
3602         /* If defining a function declared with other language
3603            linkage, use the previously declared language linkage.  */
3604         SET_DECL_LANGUAGE (newdecl, DECL_LANGUAGE (olddecl));
3605       else if (types_match)
3606         {
3607           /* If redeclaring a builtin function, and not a definition,
3608              it stays built in.  */
3609           if (DECL_BUILT_IN (olddecl))
3610             {
3611               DECL_BUILT_IN_CLASS (newdecl) = DECL_BUILT_IN_CLASS (olddecl);
3612               DECL_FUNCTION_CODE (newdecl) = DECL_FUNCTION_CODE (olddecl);
3613               /* If we're keeping the built-in definition, keep the rtl,
3614                  regardless of declaration matches.  */
3615               SET_DECL_RTL (newdecl, DECL_RTL (olddecl));
3616             }
3617           else
3618             DECL_NUM_STMTS (newdecl) = DECL_NUM_STMTS (olddecl);
3619
3620           DECL_RESULT (newdecl) = DECL_RESULT (olddecl);
3621           /* Don't clear out the arguments if we're redefining a function.  */
3622           if (DECL_ARGUMENTS (olddecl))
3623             DECL_ARGUMENTS (newdecl) = DECL_ARGUMENTS (olddecl);
3624         }
3625     }
3626   else if (TREE_CODE (newdecl) == NAMESPACE_DECL)
3627     NAMESPACE_LEVEL (newdecl) = NAMESPACE_LEVEL (olddecl);
3628
3629   /* Now preserve various other info from the definition.  */
3630   TREE_ADDRESSABLE (newdecl) = TREE_ADDRESSABLE (olddecl);
3631   TREE_ASM_WRITTEN (newdecl) = TREE_ASM_WRITTEN (olddecl);
3632   DECL_COMMON (newdecl) = DECL_COMMON (olddecl);
3633   COPY_DECL_ASSEMBLER_NAME (olddecl, newdecl);
3634
3635   if (TREE_CODE (newdecl) == FUNCTION_DECL)
3636     {
3637       int function_size;
3638
3639       function_size = sizeof (struct tree_decl);
3640
3641       memcpy ((char *) olddecl + sizeof (struct tree_common),
3642               (char *) newdecl + sizeof (struct tree_common),
3643               function_size - sizeof (struct tree_common));
3644
3645       if (DECL_TEMPLATE_INSTANTIATION (newdecl))
3646         {
3647           /* If newdecl is a template instantiation, it is possible that
3648              the following sequence of events has occurred:
3649
3650              o A friend function was declared in a class template.  The
3651              class template was instantiated.
3652
3653              o The instantiation of the friend declaration was
3654              recorded on the instantiation list, and is newdecl.
3655
3656              o Later, however, instantiate_class_template called pushdecl
3657              on the newdecl to perform name injection.  But, pushdecl in
3658              turn called duplicate_decls when it discovered that another
3659              declaration of a global function with the same name already
3660              existed.
3661
3662              o Here, in duplicate_decls, we decided to clobber newdecl.
3663
3664              If we're going to do that, we'd better make sure that
3665              olddecl, and not newdecl, is on the list of
3666              instantiations so that if we try to do the instantiation
3667              again we won't get the clobbered declaration.  */
3668
3669           tree tmpl = DECL_TI_TEMPLATE (newdecl);
3670           tree decls = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
3671
3672           for (; decls; decls = TREE_CHAIN (decls))
3673             if (TREE_VALUE (decls) == newdecl)
3674               TREE_VALUE (decls) = olddecl;
3675         }
3676     }
3677   else
3678     {
3679       memcpy ((char *) olddecl + sizeof (struct tree_common),
3680               (char *) newdecl + sizeof (struct tree_common),
3681               sizeof (struct tree_decl) - sizeof (struct tree_common)
3682               + TREE_CODE_LENGTH (TREE_CODE (newdecl)) * sizeof (char *));
3683     }
3684
3685   DECL_UID (olddecl) = olddecl_uid;
3686   if (olddecl_friend)
3687     DECL_FRIEND_P (olddecl) = 1;
3688
3689   /* NEWDECL contains the merged attribute lists.
3690      Update OLDDECL to be the same.  */
3691   DECL_ATTRIBUTES (olddecl) = DECL_ATTRIBUTES (newdecl);
3692
3693   return 1;
3694 }
3695
3696 /* Record a decl-node X as belonging to the current lexical scope.
3697    Check for errors (such as an incompatible declaration for the same
3698    name already seen in the same scope).
3699
3700    Returns either X or an old decl for the same name.
3701    If an old decl is returned, it may have been smashed
3702    to agree with what X says.  */
3703
3704 tree
3705 pushdecl (tree x)
3706 {
3707   register tree t;
3708   register tree name;
3709   int need_new_binding;
3710
3711   timevar_push (TV_NAME_LOOKUP);
3712   /* We shouldn't be calling pushdecl when we're generating RTL for a
3713      function that we already did semantic analysis on previously.  */
3714   my_friendly_assert (!cfun || doing_semantic_analysis_p (),
3715                       19990913);
3716
3717   need_new_binding = 1;
3718
3719   if (DECL_TEMPLATE_PARM_P (x))
3720     /* Template parameters have no context; they are not X::T even
3721        when declared within a class or namespace.  */
3722     ;
3723   else
3724     {
3725       if (current_function_decl && x != current_function_decl
3726           /* A local declaration for a function doesn't constitute
3727              nesting.  */
3728           && !(TREE_CODE (x) == FUNCTION_DECL && !DECL_INITIAL (x))
3729           /* A local declaration for an `extern' variable is in the
3730              scope of the current namespace, not the current
3731              function.  */
3732           && !(TREE_CODE (x) == VAR_DECL && DECL_EXTERNAL (x))
3733           && !DECL_CONTEXT (x))
3734         DECL_CONTEXT (x) = current_function_decl;
3735
3736       /* If this is the declaration for a namespace-scope function,
3737          but the declaration itself is in a local scope, mark the
3738          declaration.  */
3739       if (TREE_CODE (x) == FUNCTION_DECL
3740           && DECL_NAMESPACE_SCOPE_P (x)
3741           && current_function_decl
3742           && x != current_function_decl)
3743         DECL_LOCAL_FUNCTION_P (x) = 1;
3744     }
3745
3746   name = DECL_NAME (x);
3747   if (name)
3748     {
3749       int different_binding_level = 0;
3750
3751       if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
3752         name = TREE_OPERAND (name, 0);
3753
3754       /* In case this decl was explicitly namespace-qualified, look it
3755          up in its namespace context.  */
3756       if (TREE_CODE (x) == VAR_DECL && DECL_NAMESPACE_SCOPE_P (x)
3757           && namespace_bindings_p ())
3758         t = namespace_binding (name, DECL_CONTEXT (x));
3759       else
3760         t = lookup_name_current_level (name);
3761
3762       /* [basic.link] If there is a visible declaration of an entity
3763          with linkage having the same name and type, ignoring entities
3764          declared outside the innermost enclosing namespace scope, the
3765          block scope declaration declares that same entity and
3766          receives the linkage of the previous declaration.  */
3767       if (! t && current_function_decl && x != current_function_decl
3768           && (TREE_CODE (x) == FUNCTION_DECL || TREE_CODE (x) == VAR_DECL)
3769           && DECL_EXTERNAL (x))
3770         {
3771           /* Look in block scope.  */
3772           t = IDENTIFIER_VALUE (name);
3773           /* Or in the innermost namespace.  */
3774           if (! t)
3775             t = namespace_binding (name, DECL_CONTEXT (x));
3776           /* Does it have linkage?  Note that if this isn't a DECL, it's an
3777              OVERLOAD, which is OK.  */
3778           if (t && DECL_P (t) && ! (TREE_STATIC (t) || DECL_EXTERNAL (t)))
3779             t = NULL_TREE;
3780           if (t)
3781             different_binding_level = 1;
3782         }
3783
3784       /* If we are declaring a function, and the result of name-lookup
3785          was an OVERLOAD, look for an overloaded instance that is
3786          actually the same as the function we are declaring.  (If
3787          there is one, we have to merge our declaration with the
3788          previous declaration.)  */
3789       if (t && TREE_CODE (t) == OVERLOAD)
3790         {
3791           tree match;
3792
3793           if (TREE_CODE (x) == FUNCTION_DECL)
3794             for (match = t; match; match = OVL_NEXT (match))
3795               {
3796                 if (decls_match (OVL_CURRENT (match), x))
3797                   break;
3798               }
3799           else
3800             /* Just choose one.  */
3801             match = t;
3802
3803           if (match)
3804             t = OVL_CURRENT (match);
3805           else
3806             t = NULL_TREE;
3807         }
3808
3809       if (t == error_mark_node)
3810         {
3811           /* error_mark_node is 0 for a while during initialization!  */
3812           t = NULL_TREE;
3813           cp_error_at ("`%#D' used prior to declaration", x);
3814         }
3815       else if (t != NULL_TREE)
3816         {
3817           if (different_binding_level)
3818             {
3819               if (decls_match (x, t))
3820                 /* The standard only says that the local extern
3821                    inherits linkage from the previous decl; in
3822                    particular, default args are not shared.  It would
3823                    be nice to propagate inlining info, though.  FIXME.  */
3824                 TREE_PUBLIC (x) = TREE_PUBLIC (t);
3825             }
3826           else if (TREE_CODE (t) == PARM_DECL)
3827             {
3828               if (DECL_CONTEXT (t) == NULL_TREE)
3829                 /* This is probaby caused by too many errors, but calling
3830                    abort will say that if errors have occurred.  */
3831                 abort ();
3832
3833               /* Check for duplicate params.  */
3834               if (duplicate_decls (x, t))
3835                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3836             }
3837           else if ((DECL_EXTERN_C_FUNCTION_P (x)
3838                     || DECL_FUNCTION_TEMPLATE_P (x))
3839                    && is_overloaded_fn (t))
3840             /* Don't do anything just yet.  */;
3841           else if (t == wchar_decl_node)
3842             {
3843               if (pedantic && ! DECL_IN_SYSTEM_HEADER (x))
3844                 pedwarn ("redeclaration of `wchar_t' as `%T'",
3845                             TREE_TYPE (x));
3846
3847               /* Throw away the redeclaration.  */
3848               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3849             }
3850           else if (TREE_CODE (t) != TREE_CODE (x))
3851             {
3852               if (duplicate_decls (x, t))
3853                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3854             }
3855           else if (duplicate_decls (x, t))
3856             {
3857               if (TREE_CODE (t) == TYPE_DECL)
3858                 SET_IDENTIFIER_TYPE_VALUE (name, TREE_TYPE (t));
3859               else if (TREE_CODE (t) == FUNCTION_DECL)
3860                 check_default_args (t);
3861
3862               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3863             }
3864           else if (DECL_MAIN_P (x))
3865             {
3866               /* A redeclaration of main, but not a duplicate of the
3867                  previous one.
3868
3869                  [basic.start.main]
3870
3871                  This function shall not be overloaded.  */
3872               cp_error_at ("invalid redeclaration of `%D'", t);
3873               error ("as `%D'", x);
3874               /* We don't try to push this declaration since that
3875                  causes a crash.  */
3876               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
3877             }
3878         }
3879
3880       check_template_shadow (x);
3881
3882       /* If this is a function conjured up by the backend, massage it
3883          so it looks friendly.  */
3884       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_LANG_SPECIFIC (x))
3885         {
3886           retrofit_lang_decl (x);
3887           SET_DECL_LANGUAGE (x, lang_c);
3888         }
3889
3890       if (DECL_NON_THUNK_FUNCTION_P (x) && ! DECL_FUNCTION_MEMBER_P (x))
3891         {
3892           t = push_overloaded_decl (x, PUSH_LOCAL);
3893           if (t != x)
3894             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3895           if (!namespace_bindings_p ())
3896             /* We do not need to create a binding for this name;
3897                push_overloaded_decl will have already done so if
3898                necessary.  */
3899             need_new_binding = 0;
3900         }
3901       else if (DECL_FUNCTION_TEMPLATE_P (x) && DECL_NAMESPACE_SCOPE_P (x))
3902         {
3903           t = push_overloaded_decl (x, PUSH_GLOBAL);
3904           if (t == x)
3905             add_decl_to_level (x, NAMESPACE_LEVEL (CP_DECL_CONTEXT (t)));
3906           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
3907         }
3908
3909       /* If declaring a type as a typedef, copy the type (unless we're
3910          at line 0), and install this TYPE_DECL as the new type's typedef
3911          name.  See the extensive comment in ../c-decl.c (pushdecl).  */
3912       if (TREE_CODE (x) == TYPE_DECL)
3913         {
3914           tree type = TREE_TYPE (x);
3915           if (DECL_SOURCE_LINE (x) == 0)
3916             {
3917               if (TYPE_NAME (type) == 0)
3918                 TYPE_NAME (type) = x;
3919             }
3920           else if (type != error_mark_node && TYPE_NAME (type) != x
3921                    /* We don't want to copy the type when all we're
3922                       doing is making a TYPE_DECL for the purposes of
3923                       inlining.  */
3924                    && (!TYPE_NAME (type)
3925                        || TYPE_NAME (type) != DECL_ABSTRACT_ORIGIN (x)))
3926             {
3927               DECL_ORIGINAL_TYPE (x) = type;
3928               type = build_type_copy (type);
3929               TYPE_STUB_DECL (type) = TYPE_STUB_DECL (DECL_ORIGINAL_TYPE (x));
3930               TYPE_NAME (type) = x;
3931               TREE_TYPE (x) = type;
3932             }
3933
3934           if (type != error_mark_node
3935               && TYPE_NAME (type)
3936               && TYPE_IDENTIFIER (type))
3937             set_identifier_type_value_with_scope (DECL_NAME (x), type,
3938                                                   current_binding_level);
3939
3940         }
3941
3942       /* Multiple external decls of the same identifier ought to match.
3943
3944          We get warnings about inline functions where they are defined.
3945          We get warnings about other functions from push_overloaded_decl.
3946
3947          Avoid duplicate warnings where they are used.  */
3948       if (TREE_PUBLIC (x) && TREE_CODE (x) != FUNCTION_DECL)
3949         {
3950           tree decl;
3951
3952           decl = IDENTIFIER_NAMESPACE_VALUE (name);
3953           if (decl && TREE_CODE (decl) == OVERLOAD)
3954             decl = OVL_FUNCTION (decl);
3955
3956           if (decl && decl != error_mark_node
3957               && (DECL_EXTERNAL (decl) || TREE_PUBLIC (decl))
3958               /* If different sort of thing, we already gave an error.  */
3959               && TREE_CODE (decl) == TREE_CODE (x)
3960               && !same_type_p (TREE_TYPE (x), TREE_TYPE (decl)))
3961             {
3962               pedwarn ("type mismatch with previous external decl", x);
3963               cp_pedwarn_at ("previous external decl of `%#D'", decl);
3964             }
3965         }
3966
3967       /* This name is new in its binding level.
3968          Install the new declaration and return it.  */
3969       if (namespace_bindings_p ())
3970         {
3971           /* Install a global value.  */
3972
3973           /* If the first global decl has external linkage,
3974              warn if we later see static one.  */
3975           if (IDENTIFIER_GLOBAL_VALUE (name) == NULL_TREE && TREE_PUBLIC (x))
3976             TREE_PUBLIC (name) = 1;
3977
3978           /* Bind the name for the entity.  */
3979           if (!(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)
3980                 && t != NULL_TREE)
3981               && (TREE_CODE (x) == TYPE_DECL
3982                   || TREE_CODE (x) == VAR_DECL
3983                   || TREE_CODE (x) == ALIAS_DECL
3984                   || TREE_CODE (x) == NAMESPACE_DECL
3985                   || TREE_CODE (x) == CONST_DECL
3986                   || TREE_CODE (x) == TEMPLATE_DECL))
3987             SET_IDENTIFIER_NAMESPACE_VALUE (name, x);
3988
3989           /* Don't forget if the function was used via an implicit decl.  */
3990           if (IDENTIFIER_IMPLICIT_DECL (name)
3991               && TREE_USED (IDENTIFIER_IMPLICIT_DECL (name)))
3992             TREE_USED (x) = 1;
3993
3994           /* Don't forget if its address was taken in that way.  */
3995           if (IDENTIFIER_IMPLICIT_DECL (name)
3996               && TREE_ADDRESSABLE (IDENTIFIER_IMPLICIT_DECL (name)))
3997             TREE_ADDRESSABLE (x) = 1;
3998
3999           /* Warn about mismatches against previous implicit decl.  */
4000           if (IDENTIFIER_IMPLICIT_DECL (name) != NULL_TREE
4001               /* If this real decl matches the implicit, don't complain.  */
4002               && ! (TREE_CODE (x) == FUNCTION_DECL
4003                     && TREE_TYPE (TREE_TYPE (x)) == integer_type_node))
4004             warning
4005               ("`%D' was previously implicitly declared to return `int'", x);
4006
4007           /* If new decl is `static' and an `extern' was seen previously,
4008              warn about it.  */
4009           if (x != NULL_TREE && t != NULL_TREE && decls_match (x, t))
4010             warn_extern_redeclared_static (x, t);
4011         }
4012       else
4013         {
4014           /* Here to install a non-global value.  */
4015           tree oldlocal = IDENTIFIER_VALUE (name);
4016           tree oldglobal = IDENTIFIER_NAMESPACE_VALUE (name);
4017
4018           if (need_new_binding)
4019             {
4020               push_local_binding (name, x, 0);
4021               /* Because push_local_binding will hook X on to the
4022                  current_binding_level's name list, we don't want to
4023                  do that again below.  */
4024               need_new_binding = 0;
4025             }
4026
4027           /* If this is a TYPE_DECL, push it into the type value slot.  */
4028           if (TREE_CODE (x) == TYPE_DECL)
4029             set_identifier_type_value_with_scope (name, TREE_TYPE (x),
4030                                                   current_binding_level);
4031
4032           /* Clear out any TYPE_DECL shadowed by a namespace so that
4033              we won't think this is a type.  The C struct hack doesn't
4034              go through namespaces.  */
4035           if (TREE_CODE (x) == NAMESPACE_DECL)
4036             set_identifier_type_value_with_scope (name, NULL_TREE,
4037                                                   current_binding_level);
4038
4039           if (oldlocal)
4040             {
4041               tree d = oldlocal;
4042
4043               while (oldlocal
4044                      && TREE_CODE (oldlocal) == VAR_DECL
4045                      && DECL_DEAD_FOR_LOCAL (oldlocal))
4046                 oldlocal = DECL_SHADOWED_FOR_VAR (oldlocal);
4047
4048               if (oldlocal == NULL_TREE)
4049                 oldlocal = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (d));
4050             }
4051
4052           /* If this is an extern function declaration, see if we
4053              have a global definition or declaration for the function.  */
4054           if (oldlocal == NULL_TREE
4055               && DECL_EXTERNAL (x)
4056               && oldglobal != NULL_TREE
4057               && TREE_CODE (x) == FUNCTION_DECL
4058               && TREE_CODE (oldglobal) == FUNCTION_DECL)
4059             {
4060               /* We have one.  Their types must agree.  */
4061               if (decls_match (x, oldglobal))
4062                 /* OK */;
4063               else
4064                 {
4065                   warning ("extern declaration of `%#D' doesn't match", x);
4066                   cp_warning_at ("global declaration `%#D'", oldglobal);
4067                 }
4068             }
4069           /* If we have a local external declaration,
4070              and no file-scope declaration has yet been seen,
4071              then if we later have a file-scope decl it must not be static.  */
4072           if (oldlocal == NULL_TREE
4073               && oldglobal == NULL_TREE
4074               && DECL_EXTERNAL (x)
4075               && TREE_PUBLIC (x))
4076             TREE_PUBLIC (name) = 1;
4077
4078           /* Warn if shadowing an argument at the top level of the body.  */
4079           if (oldlocal != NULL_TREE && !DECL_EXTERNAL (x)
4080               /* Inline decls shadow nothing.  */
4081               && !DECL_FROM_INLINE (x)
4082               && TREE_CODE (oldlocal) == PARM_DECL)
4083             {
4084               bool err = false;
4085
4086               /* Don't complain if it's from an enclosing function.  */
4087               if (DECL_CONTEXT (oldlocal) == current_function_decl
4088                   && TREE_CODE (x) != PARM_DECL)
4089                 {
4090                   /* Go to where the parms should be and see if we find
4091                      them there.  */
4092                   struct cp_binding_level *b = current_binding_level->level_chain;
4093
4094                   /* Skip the ctor/dtor cleanup level.  */
4095                   b = b->level_chain;
4096
4097                   /* ARM $8.3 */
4098                   if (b->parm_flag == 1)
4099                     {
4100                       error ("declaration of `%#D' shadows a parameter",
4101                                 name);
4102                       err = true;
4103                     }
4104                 }
4105
4106               if (warn_shadow && !err)
4107                 shadow_warning ("a parameter", name, oldlocal);
4108             }
4109
4110           /* Maybe warn if shadowing something else.  */
4111           else if (warn_shadow && !DECL_EXTERNAL (x)
4112               /* No shadow warnings for internally generated vars.  */
4113               && ! DECL_ARTIFICIAL (x)
4114               /* No shadow warnings for vars made for inlining.  */
4115               && ! DECL_FROM_INLINE (x))
4116             {
4117               if (IDENTIFIER_CLASS_VALUE (name) != NULL_TREE
4118                        && current_class_ptr
4119                        && !TREE_STATIC (name))
4120                 warning ("declaration of `%s' shadows a member of `this'",
4121                             IDENTIFIER_POINTER (name));
4122               else if (oldlocal != NULL_TREE
4123                        && TREE_CODE (oldlocal) == VAR_DECL)
4124                 shadow_warning ("a previous local", name, oldlocal);
4125               else if (oldglobal != NULL_TREE
4126                        && TREE_CODE (oldglobal) == VAR_DECL)
4127                 /* XXX shadow warnings in outer-more namespaces */
4128                 shadow_warning ("a global declaration", name, oldglobal);
4129             }
4130         }
4131
4132       if (TREE_CODE (x) == FUNCTION_DECL)
4133         check_default_args (x);
4134
4135       if (TREE_CODE (x) == VAR_DECL)
4136         maybe_register_incomplete_var (x);
4137     }
4138
4139   if (need_new_binding)
4140     add_decl_to_level (x,
4141                        DECL_NAMESPACE_SCOPE_P (x)
4142                        ? NAMESPACE_LEVEL (CP_DECL_CONTEXT (x))
4143                        : current_binding_level);
4144
4145   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4146 }
4147
4148 /* Same as pushdecl, but define X in binding-level LEVEL.  We rely on the
4149    caller to set DECL_CONTEXT properly.  */
4150
4151 static tree
4152 pushdecl_with_scope (tree x, struct cp_binding_level* level)
4153 {
4154   register struct cp_binding_level *b;
4155   tree function_decl = current_function_decl;
4156
4157   timevar_push (TV_NAME_LOOKUP);
4158   current_function_decl = NULL_TREE;
4159   if (level->parm_flag == 2)
4160     {
4161       b = class_binding_level;
4162       class_binding_level = level;
4163       pushdecl_class_level (x);
4164       class_binding_level = b;
4165     }
4166   else
4167     {
4168       b = current_binding_level;
4169       current_binding_level = level;
4170       x = pushdecl (x);
4171       current_binding_level = b;
4172     }
4173   current_function_decl = function_decl;
4174   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4175 }
4176
4177 /* Like pushdecl, only it places X in the current namespace,
4178    if appropriate.  */
4179
4180 tree
4181 pushdecl_namespace_level (tree x)
4182 {
4183   register struct cp_binding_level *b = current_binding_level;
4184   register tree t;
4185
4186   timevar_push (TV_NAME_LOOKUP);
4187   t = pushdecl_with_scope (x, NAMESPACE_LEVEL (current_namespace));
4188
4189   /* Now, the type_shadowed stack may screw us.  Munge it so it does
4190      what we want.  */
4191   if (TREE_CODE (x) == TYPE_DECL)
4192     {
4193       tree name = DECL_NAME (x);
4194       tree newval;
4195       tree *ptr = (tree *)0;
4196       for (; b != global_binding_level; b = b->level_chain)
4197         {
4198           tree shadowed = b->type_shadowed;
4199           for (; shadowed; shadowed = TREE_CHAIN (shadowed))
4200             if (TREE_PURPOSE (shadowed) == name)
4201               {
4202                 ptr = &TREE_VALUE (shadowed);
4203                 /* Can't break out of the loop here because sometimes
4204                    a binding level will have duplicate bindings for
4205                    PT names.  It's gross, but I haven't time to fix it.  */
4206               }
4207         }
4208       newval = TREE_TYPE (x);
4209       if (ptr == (tree *)0)
4210         {
4211           /* @@ This shouldn't be needed.  My test case "zstring.cc" trips
4212              up here if this is changed to an assertion.  --KR  */
4213           SET_IDENTIFIER_TYPE_VALUE (name, newval);
4214         }
4215       else
4216         {
4217           *ptr = newval;
4218         }
4219     }
4220   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
4221 }
4222
4223 /* Like pushdecl, only it places X in GLOBAL_BINDING_LEVEL,
4224    if appropriate.  */
4225
4226 tree
4227 pushdecl_top_level (tree x)
4228 {
4229   timevar_push (TV_NAME_LOOKUP);
4230   push_to_top_level ();
4231   x = pushdecl_namespace_level (x);
4232   pop_from_top_level ();
4233   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, x);
4234 }
4235
4236 /* Make the declaration of X appear in CLASS scope.  */
4237
4238 void
4239 pushdecl_class_level (tree x)
4240 {
4241   tree name;
4242
4243   timevar_push (TV_NAME_LOOKUP);
4244   /* Get the name of X.  */
4245   if (TREE_CODE (x) == OVERLOAD)
4246     name = DECL_NAME (get_first_fn (x));
4247   else
4248     name = DECL_NAME (x);
4249
4250   if (name)
4251     {
4252       push_class_level_binding (name, x);
4253       if (TREE_CODE (x) == TYPE_DECL)
4254         set_identifier_type_value (name, TREE_TYPE (x));
4255     }
4256   else if (ANON_AGGR_TYPE_P (TREE_TYPE (x)))
4257     {
4258       /* If X is an anonymous aggregate, all of its members are
4259          treated as if they were members of the class containing the
4260          aggregate, for naming purposes.  */
4261       tree f;
4262
4263       for (f = TYPE_FIELDS (TREE_TYPE (x)); f; f = TREE_CHAIN (f))
4264         pushdecl_class_level (f);
4265     }
4266   timevar_pop (TV_NAME_LOOKUP);
4267 }
4268
4269 /* Enter DECL into the symbol table, if that's appropriate.  Returns
4270    DECL, or a modified version thereof.  */
4271
4272 tree
4273 maybe_push_decl (tree decl)
4274 {
4275   tree type = TREE_TYPE (decl);
4276
4277   /* Add this decl to the current binding level, but not if it comes
4278      from another scope, e.g. a static member variable.  TEM may equal
4279      DECL or it may be a previous decl of the same name.  */
4280   if (decl == error_mark_node
4281       || (TREE_CODE (decl) != PARM_DECL
4282           && DECL_CONTEXT (decl) != NULL_TREE
4283           /* Definitions of namespace members outside their namespace are
4284              possible.  */
4285           && TREE_CODE (DECL_CONTEXT (decl)) != NAMESPACE_DECL)
4286       || (TREE_CODE (decl) == TEMPLATE_DECL && !namespace_bindings_p ())
4287       || TREE_CODE (type) == UNKNOWN_TYPE
4288       /* The declaration of a template specialization does not affect
4289          the functions available for overload resolution, so we do not
4290          call pushdecl.  */
4291       || (TREE_CODE (decl) == FUNCTION_DECL
4292           && DECL_TEMPLATE_SPECIALIZATION (decl)))
4293     return decl;
4294   else
4295     return pushdecl (decl);
4296 }
4297
4298 /* Make the declaration(s) of X appear in CLASS scope
4299    under the name NAME.  */
4300
4301 void
4302 push_class_level_binding (tree name, tree x)
4303 {
4304   tree binding;
4305   timevar_push (TV_NAME_LOOKUP);
4306   /* The class_binding_level will be NULL if x is a template
4307      parameter name in a member template.  */
4308   if (!class_binding_level)
4309     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
4310
4311   /* Make sure that this new member does not have the same name
4312      as a template parameter.  */
4313   if (TYPE_BEING_DEFINED (current_class_type))
4314     check_template_shadow (x);
4315
4316   /* If this declaration shadows a declaration from an enclosing
4317      class, then we will need to restore IDENTIFIER_CLASS_VALUE when
4318      we leave this class.  Record the shadowed declaration here.  */
4319   binding = IDENTIFIER_BINDING (name);
4320   if (binding
4321       && ((TREE_CODE (x) == OVERLOAD
4322            && BINDING_VALUE (binding)
4323            && is_overloaded_fn (BINDING_VALUE (binding)))
4324           || INHERITED_VALUE_BINDING_P (binding)))
4325     {
4326       tree shadow;
4327       tree old_decl;
4328
4329       /* If the old binding was from a base class, and was for a tag
4330          name, slide it over to make room for the new binding.  The
4331          old binding is still visible if explicitly qualified with a
4332          class-key.  */
4333       if (INHERITED_VALUE_BINDING_P (binding)
4334           && BINDING_VALUE (binding)
4335           && TREE_CODE (BINDING_VALUE (binding)) == TYPE_DECL
4336           && DECL_ARTIFICIAL (BINDING_VALUE (binding))
4337           && !(TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x)))
4338         {
4339           old_decl = BINDING_TYPE (binding);
4340           BINDING_TYPE (binding) = BINDING_VALUE (binding);
4341           BINDING_VALUE (binding) = NULL_TREE;
4342           INHERITED_VALUE_BINDING_P (binding) = 0;
4343         }
4344       else
4345         old_decl = BINDING_VALUE (binding);
4346
4347       /* Find the previous binding of name on the class-shadowed
4348          list, and update it.  */
4349       for (shadow = class_binding_level->class_shadowed;
4350            shadow;
4351            shadow = TREE_CHAIN (shadow))
4352         if (TREE_PURPOSE (shadow) == name
4353             && TREE_TYPE (shadow) == old_decl)
4354           {
4355             BINDING_VALUE (binding) = x;
4356             INHERITED_VALUE_BINDING_P (binding) = 0;
4357             TREE_TYPE (shadow) = x;
4358             IDENTIFIER_CLASS_VALUE (name) = x;
4359             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
4360           }
4361     }
4362
4363   /* If we didn't replace an existing binding, put the binding on the
4364      stack of bindings for the identifier, and update the shadowed list.  */
4365   if (push_class_binding (name, x))
4366     {
4367       class_binding_level->class_shadowed
4368         = tree_cons (name, NULL,
4369                      class_binding_level->class_shadowed);
4370       /* Record the value we are binding NAME to so that we can know
4371          what to pop later.  */
4372       TREE_TYPE (class_binding_level->class_shadowed) = x;
4373     }
4374   timevar_pop (TV_NAME_LOOKUP);
4375 }
4376
4377 /* Insert another USING_DECL into the current binding level, returning
4378    this declaration. If this is a redeclaration, do nothing, and
4379    return NULL_TREE if this not in namespace scope (in namespace
4380    scope, a using decl might extend any previous bindings).  */
4381
4382 tree
4383 push_using_decl (tree scope, tree name)
4384 {
4385   tree decl;
4386
4387   timevar_push (TV_NAME_LOOKUP);
4388   my_friendly_assert (TREE_CODE (scope) == NAMESPACE_DECL, 383);
4389   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 384);
4390   for (decl = current_binding_level->usings; decl; decl = TREE_CHAIN (decl))
4391     if (DECL_INITIAL (decl) == scope && DECL_NAME (decl) == name)
4392       break;
4393   if (decl)
4394     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
4395                             namespace_bindings_p () ? decl : NULL_TREE);
4396   decl = build_lang_decl (USING_DECL, name, void_type_node);
4397   DECL_INITIAL (decl) = scope;
4398   TREE_CHAIN (decl) = current_binding_level->usings;
4399   current_binding_level->usings = decl;
4400   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4401 }
4402
4403 /* Add namespace to using_directives. Return NULL_TREE if nothing was
4404    changed (i.e. there was already a directive), or the fresh
4405    TREE_LIST otherwise.  */
4406
4407 tree
4408 push_using_directive (tree used)
4409 {
4410   tree ud = current_binding_level->using_directives;
4411   tree iter, ancestor;
4412
4413   timevar_push (TV_NAME_LOOKUP);
4414   /* Check if we already have this.  */
4415   if (purpose_member (used, ud) != NULL_TREE)
4416     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4417
4418   /* Recursively add all namespaces used.  */
4419   for (iter = DECL_NAMESPACE_USING (used); iter; iter = TREE_CHAIN (iter))
4420     push_using_directive (TREE_PURPOSE (iter));
4421
4422   ancestor = namespace_ancestor (current_decl_namespace (), used);
4423   ud = current_binding_level->using_directives;
4424   ud = tree_cons (used, ancestor, ud);
4425   current_binding_level->using_directives = ud;
4426   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ud);
4427 }
4428
4429 /* DECL is a FUNCTION_DECL for a non-member function, which may have
4430    other definitions already in place.  We get around this by making
4431    the value of the identifier point to a list of all the things that
4432    want to be referenced by that name.  It is then up to the users of
4433    that name to decide what to do with that list.
4434
4435    DECL may also be a TEMPLATE_DECL, with a FUNCTION_DECL in its
4436    DECL_TEMPLATE_RESULT.  It is dealt with the same way.
4437
4438    FLAGS is a bitwise-or of the following values:
4439      PUSH_LOCAL: Bind DECL in the current scope, rather than at
4440                  namespace scope.
4441      PUSH_USING: DECL is being pushed as the result of a using
4442                  declaration.
4443
4444    The value returned may be a previous declaration if we guessed wrong
4445    about what language DECL should belong to (C or C++).  Otherwise,
4446    it's always DECL (and never something that's not a _DECL).  */
4447
4448 tree
4449 push_overloaded_decl (tree decl, int flags)
4450 {
4451   tree name = DECL_NAME (decl);
4452   tree old;
4453   tree new_binding;
4454   int doing_global = (namespace_bindings_p () || !(flags & PUSH_LOCAL));
4455
4456   timevar_push (TV_NAME_LOOKUP);
4457   if (doing_global)
4458     old = namespace_binding (name, DECL_CONTEXT (decl));
4459   else
4460     old = lookup_name_current_level (name);
4461
4462   if (old)
4463     {
4464       if (TREE_CODE (old) == TYPE_DECL && DECL_ARTIFICIAL (old))
4465         {
4466           tree t = TREE_TYPE (old);
4467           if (IS_AGGR_TYPE (t) && warn_shadow
4468               && (! DECL_IN_SYSTEM_HEADER (decl)
4469                   || ! DECL_IN_SYSTEM_HEADER (old)))
4470             warning ("`%#D' hides constructor for `%#T'", decl, t);
4471           old = NULL_TREE;
4472         }
4473       else if (is_overloaded_fn (old))
4474         {
4475           tree tmp;
4476
4477           for (tmp = old; tmp; tmp = OVL_NEXT (tmp))
4478             {
4479               tree fn = OVL_CURRENT (tmp);
4480
4481               if (TREE_CODE (tmp) == OVERLOAD && OVL_USED (tmp)
4482                   && !(flags & PUSH_USING)
4483                   && compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
4484                                 TYPE_ARG_TYPES (TREE_TYPE (decl))))
4485                 error ("`%#D' conflicts with previous using declaration `%#D'",
4486                           decl, fn);
4487
4488               if (duplicate_decls (decl, fn))
4489                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, fn);
4490             }
4491         }
4492       else if (old == error_mark_node)
4493         /* Ignore the undefined symbol marker.  */
4494         old = NULL_TREE;
4495       else
4496         {
4497           cp_error_at ("previous non-function declaration `%#D'", old);
4498           error ("conflicts with function declaration `%#D'", decl);
4499           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4500         }
4501     }
4502
4503   if (old || TREE_CODE (decl) == TEMPLATE_DECL)
4504     {
4505       if (old && TREE_CODE (old) != OVERLOAD)
4506         new_binding = ovl_cons (decl, ovl_cons (old, NULL_TREE));
4507       else
4508         new_binding = ovl_cons (decl, old);
4509       if (flags & PUSH_USING)
4510         OVL_USED (new_binding) = 1;
4511     }
4512   else
4513     /* NAME is not ambiguous.  */
4514     new_binding = decl;
4515
4516   if (doing_global)
4517     set_namespace_binding (name, current_namespace, new_binding);
4518   else
4519     {
4520       /* We only create an OVERLOAD if there was a previous binding at
4521          this level, or if decl is a template. In the former case, we
4522          need to remove the old binding and replace it with the new
4523          binding.  We must also run through the NAMES on the binding
4524          level where the name was bound to update the chain.  */
4525
4526       if (TREE_CODE (new_binding) == OVERLOAD && old)
4527         {
4528           tree *d;
4529
4530           for (d = &BINDING_LEVEL (IDENTIFIER_BINDING (name))->names;
4531                *d;
4532                d = &TREE_CHAIN (*d))
4533             if (*d == old
4534                 || (TREE_CODE (*d) == TREE_LIST
4535                     && TREE_VALUE (*d) == old))
4536               {
4537                 if (TREE_CODE (*d) == TREE_LIST)
4538                   /* Just replace the old binding with the new.  */
4539                   TREE_VALUE (*d) = new_binding;
4540                 else
4541                   /* Build a TREE_LIST to wrap the OVERLOAD.  */
4542                   *d = tree_cons (NULL_TREE, new_binding,
4543                                   TREE_CHAIN (*d));
4544
4545                 /* And update the CPLUS_BINDING node.  */
4546                 BINDING_VALUE (IDENTIFIER_BINDING (name))
4547                   = new_binding;
4548                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4549               }
4550
4551           /* We should always find a previous binding in this case.  */
4552           abort ();
4553         }
4554
4555       /* Install the new binding.  */
4556       push_local_binding (name, new_binding, flags);
4557     }
4558
4559   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4560 }
4561 \f
4562 /* Generate an implicit declaration for identifier FUNCTIONID
4563    as a function of type int ().  Print a warning if appropriate.  */
4564
4565 tree
4566 implicitly_declare (tree functionid)
4567 {
4568   register tree decl;
4569
4570   /* We used to reuse an old implicit decl here,
4571      but this loses with inline functions because it can clobber
4572      the saved decl chains.  */
4573   decl = build_lang_decl (FUNCTION_DECL, functionid, default_function_type);
4574
4575   DECL_EXTERNAL (decl) = 1;
4576   TREE_PUBLIC (decl) = 1;
4577
4578   /* ISO standard says implicit declarations are in the innermost block.
4579      So we record the decl in the standard fashion.  */
4580   pushdecl (decl);
4581   rest_of_decl_compilation (decl, NULL, 0, 0);
4582
4583   if (warn_implicit
4584       /* Only one warning per identifier.  */
4585       && IDENTIFIER_IMPLICIT_DECL (functionid) == NULL_TREE)
4586     {
4587       pedwarn ("implicit declaration of function `%#D'", decl);
4588     }
4589
4590   SET_IDENTIFIER_IMPLICIT_DECL (functionid, decl);
4591
4592   return decl;
4593 }
4594
4595 /* Return zero if the declaration NEWDECL is valid
4596    when the declaration OLDDECL (assumed to be for the same name)
4597    has already been seen.
4598    Otherwise return an error message format string with a %s
4599    where the identifier should go.  */
4600
4601 static const char *
4602 redeclaration_error_message (tree newdecl, tree olddecl)
4603 {
4604   if (TREE_CODE (newdecl) == TYPE_DECL)
4605     {
4606       /* Because C++ can put things into name space for free,
4607          constructs like "typedef struct foo { ... } foo"
4608          would look like an erroneous redeclaration.  */
4609       if (same_type_p (TREE_TYPE (newdecl), TREE_TYPE (olddecl)))
4610         return 0;
4611       else
4612         return "redefinition of `%#D'";
4613     }
4614   else if (TREE_CODE (newdecl) == FUNCTION_DECL)
4615     {
4616       /* If this is a pure function, its olddecl will actually be
4617          the original initialization to `0' (which we force to call
4618          abort()).  Don't complain about redefinition in this case.  */
4619       if (DECL_LANG_SPECIFIC (olddecl) && DECL_PURE_VIRTUAL_P (olddecl))
4620         return 0;
4621
4622       /* If both functions come from different namespaces, this is not
4623          a redeclaration - this is a conflict with a used function.  */
4624       if (DECL_NAMESPACE_SCOPE_P (olddecl)
4625           && DECL_CONTEXT (olddecl) != DECL_CONTEXT (newdecl))
4626         return "`%D' conflicts with used function";
4627
4628       /* We'll complain about linkage mismatches in
4629          warn_extern_redeclared_static.  */
4630
4631       /* Defining the same name twice is no good.  */
4632       if (DECL_INITIAL (olddecl) != NULL_TREE
4633           && DECL_INITIAL (newdecl) != NULL_TREE)
4634         {
4635           if (DECL_NAME (olddecl) == NULL_TREE)
4636             return "`%#D' not declared in class";
4637           else
4638             return "redefinition of `%#D'";
4639         }
4640       return 0;
4641     }
4642   else if (TREE_CODE (newdecl) == TEMPLATE_DECL)
4643     {
4644       if ((TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == FUNCTION_DECL
4645            && (DECL_TEMPLATE_RESULT (newdecl)
4646                != DECL_TEMPLATE_RESULT (olddecl))
4647            && DECL_INITIAL (DECL_TEMPLATE_RESULT (newdecl))
4648            && DECL_INITIAL (DECL_TEMPLATE_RESULT (olddecl)))
4649           || (TREE_CODE (DECL_TEMPLATE_RESULT (newdecl)) == TYPE_DECL
4650               && COMPLETE_TYPE_P (TREE_TYPE (newdecl))
4651               && COMPLETE_TYPE_P (TREE_TYPE (olddecl))))
4652         return "redefinition of `%#D'";
4653       return 0;
4654     }
4655   else if (toplevel_bindings_p () || DECL_NAMESPACE_SCOPE_P (newdecl))
4656     {
4657       /* Objects declared at top level:  */
4658       /* If at least one is a reference, it's ok.  */
4659       if (DECL_EXTERNAL (newdecl) || DECL_EXTERNAL (olddecl))
4660         return 0;
4661       /* Reject two definitions.  */
4662       return "redefinition of `%#D'";
4663     }
4664   else
4665     {
4666       /* Objects declared with block scope:  */
4667       /* Reject two definitions, and reject a definition
4668          together with an external reference.  */
4669       if (!(DECL_EXTERNAL (newdecl) && DECL_EXTERNAL (olddecl)))
4670         return "redeclaration of `%#D'";
4671       return 0;
4672     }
4673 }
4674 \f
4675 /* Create a new label, named ID.  */
4676
4677 static tree
4678 make_label_decl (tree id, int local_p)
4679 {
4680   tree decl;
4681
4682   decl = build_decl (LABEL_DECL, id, void_type_node);
4683   if (expanding_p)
4684     /* Make sure every label has an rtx.  */
4685     label_rtx (decl);
4686
4687   DECL_CONTEXT (decl) = current_function_decl;
4688   DECL_MODE (decl) = VOIDmode;
4689   C_DECLARED_LABEL_FLAG (decl) = local_p;
4690
4691   /* Say where one reference is to the label, for the sake of the
4692      error if it is not defined.  */
4693   DECL_SOURCE_LINE (decl) = lineno;
4694   DECL_SOURCE_FILE (decl) = input_filename;
4695
4696   /* Record the fact that this identifier is bound to this label.  */
4697   SET_IDENTIFIER_LABEL_VALUE (id, decl);
4698
4699   return decl;
4700 }
4701
4702 /* Record this label on the list of used labels so that we can check
4703    at the end of the function to see whether or not the label was
4704    actually defined, and so we can check when the label is defined whether
4705    this use is valid.  */
4706
4707 static void
4708 use_label (tree decl)
4709 {
4710   if (named_label_uses == NULL
4711       || named_label_uses->names_in_scope != current_binding_level->names
4712       || named_label_uses->label_decl != decl)
4713     {
4714       struct named_label_use_list *new_ent;
4715       new_ent = ((struct named_label_use_list *)
4716                  ggc_alloc (sizeof (struct named_label_use_list)));
4717       new_ent->label_decl = decl;
4718       new_ent->names_in_scope = current_binding_level->names;
4719       new_ent->binding_level = current_binding_level;
4720       new_ent->lineno_o_goto = lineno;
4721       new_ent->filename_o_goto = input_filename;
4722       new_ent->next = named_label_uses;
4723       named_label_uses = new_ent;
4724     }
4725 }
4726
4727 /* Look for a label named ID in the current function.  If one cannot
4728    be found, create one.  (We keep track of used, but undefined,
4729    labels, and complain about them at the end of a function.)  */
4730
4731 tree
4732 lookup_label (tree id)
4733 {
4734   tree decl;
4735   struct named_label_list *ent;
4736
4737   timevar_push (TV_NAME_LOOKUP);
4738   /* You can't use labels at global scope.  */
4739   if (current_function_decl == NULL_TREE)
4740     {
4741       error ("label `%s' referenced outside of any function",
4742              IDENTIFIER_POINTER (id));
4743       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
4744     }
4745
4746   /* See if we've already got this label.  */
4747   decl = IDENTIFIER_LABEL_VALUE (id);
4748   if (decl != NULL_TREE && DECL_CONTEXT (decl) == current_function_decl)
4749     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4750
4751   /* Record this label on the list of labels used in this function.
4752      We do this before calling make_label_decl so that we get the
4753      IDENTIFIER_LABEL_VALUE before the new label is declared.  */
4754   ent = ((struct named_label_list *)
4755          ggc_alloc_cleared (sizeof (struct named_label_list)));
4756   ent->old_value = IDENTIFIER_LABEL_VALUE (id);
4757   ent->next = named_labels;
4758   named_labels = ent;
4759
4760   /* We need a new label.  */
4761   decl = make_label_decl (id, /*local_p=*/0);
4762
4763   /* Now fill in the information we didn't have before.  */
4764   ent->label_decl = decl;
4765
4766   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
4767 }
4768
4769 /* Declare a local label named ID.  */
4770
4771 tree
4772 declare_local_label (tree id)
4773 {
4774   tree decl;
4775
4776   /* Add a new entry to the SHADOWED_LABELS list so that when we leave
4777      this scope we can restore the old value of
4778      IDENTIFIER_TYPE_VALUE.  */
4779   current_binding_level->shadowed_labels
4780     = tree_cons (IDENTIFIER_LABEL_VALUE (id), NULL_TREE,
4781                  current_binding_level->shadowed_labels);
4782   /* Look for the label.  */
4783   decl = make_label_decl (id, /*local_p=*/1);
4784   /* Now fill in the information we didn't have before.  */
4785   TREE_VALUE (current_binding_level->shadowed_labels) = decl;
4786
4787   return decl;
4788 }
4789
4790 /* Returns nonzero if it is ill-formed to jump past the declaration of
4791    DECL.  Returns 2 if it's also a real problem.  */
4792
4793 static int
4794 decl_jump_unsafe (tree decl)
4795 {
4796   if (TREE_CODE (decl) != VAR_DECL || TREE_STATIC (decl))
4797     return 0;
4798
4799   if (DECL_INITIAL (decl) == NULL_TREE
4800       && pod_type_p (TREE_TYPE (decl)))
4801     return 0;
4802
4803   /* This is really only important if we're crossing an initialization.
4804      The POD stuff is just pedantry; why should it matter if the class
4805      contains a field of pointer to member type?  */
4806   if (DECL_INITIAL (decl)
4807       || (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))))
4808     return 2;
4809   return 1;
4810 }
4811
4812 /* Check that a single previously seen jump to a newly defined label
4813    is OK.  DECL is the LABEL_DECL or 0; LEVEL is the binding_level for
4814    the jump context; NAMES are the names in scope in LEVEL at the jump
4815    context; FILE and LINE are the source position of the jump or 0.  */
4816
4817 static void
4818 check_previous_goto_1 (tree decl,
4819                        struct cp_binding_level* level,
4820                        tree names,
4821                        const char* file,
4822                        int line)
4823 {
4824   int identified = 0;
4825   int saw_eh = 0;
4826   struct cp_binding_level *b = current_binding_level;
4827   for (; b; b = b->level_chain)
4828     {
4829       tree new_decls = b->names;
4830       tree old_decls = (b == level ? names : NULL_TREE);
4831       for (; new_decls != old_decls;
4832            new_decls = TREE_CHAIN (new_decls))
4833         {
4834           int problem = decl_jump_unsafe (new_decls);
4835           if (! problem)
4836             continue;
4837
4838           if (! identified)
4839             {
4840               if (decl)
4841                 pedwarn ("jump to label `%D'", decl);
4842               else
4843                 pedwarn ("jump to case label");
4844
4845               if (file)
4846                 pedwarn_with_file_and_line (file, line, "  from here");
4847               identified = 1;
4848             }
4849
4850           if (problem > 1)
4851             cp_error_at ("  crosses initialization of `%#D'",
4852                          new_decls);
4853           else
4854             cp_pedwarn_at ("  enters scope of non-POD `%#D'",
4855                            new_decls);
4856         }
4857
4858       if (b == level)
4859         break;
4860       if ((b->is_try_scope || b->is_catch_scope) && ! saw_eh)
4861         {
4862           if (! identified)
4863             {
4864               if (decl)
4865                 pedwarn ("jump to label `%D'", decl);
4866               else
4867                 pedwarn ("jump to case label");
4868
4869               if (file)
4870                 pedwarn_with_file_and_line (file, line, "  from here");
4871               identified = 1;
4872             }
4873           if (b->is_try_scope)
4874             error ("  enters try block");
4875           else
4876             error ("  enters catch block");
4877           saw_eh = 1;
4878         }
4879     }
4880 }
4881
4882 static void
4883 check_previous_goto (struct named_label_use_list* use)
4884 {
4885   check_previous_goto_1 (use->label_decl, use->binding_level,
4886                          use->names_in_scope, use->filename_o_goto,
4887                          use->lineno_o_goto);
4888 }
4889
4890 static void
4891 check_switch_goto (struct cp_binding_level* level)
4892 {
4893   check_previous_goto_1 (NULL_TREE, level, level->names, NULL, 0);
4894 }
4895
4896 /* Check that any previously seen jumps to a newly defined label DECL
4897    are OK.  Called by define_label.  */
4898
4899 static void
4900 check_previous_gotos (tree decl)
4901 {
4902   struct named_label_use_list **usep;
4903
4904   if (! TREE_USED (decl))
4905     return;
4906
4907   for (usep = &named_label_uses; *usep; )
4908     {
4909       struct named_label_use_list *use = *usep;
4910       if (use->label_decl == decl)
4911         {
4912           check_previous_goto (use);
4913           *usep = use->next;
4914         }
4915       else
4916         usep = &(use->next);
4917     }
4918 }
4919
4920 /* Check that a new jump to a label DECL is OK.  Called by
4921    finish_goto_stmt.  */
4922
4923 void
4924 check_goto (tree decl)
4925 {
4926   int identified = 0;
4927   tree bad;
4928   struct named_label_list *lab;
4929
4930   /* We can't know where a computed goto is jumping.  So we assume
4931      that it's OK.  */
4932   if (! DECL_P (decl))
4933     return;
4934
4935   /* If the label hasn't been defined yet, defer checking.  */
4936   if (! DECL_INITIAL (decl))
4937     {
4938       use_label (decl);
4939       return;
4940     }
4941
4942   for (lab = named_labels; lab; lab = lab->next)
4943     if (decl == lab->label_decl)
4944       break;
4945
4946   /* If the label is not on named_labels it's a gcc local label, so
4947      it must be in an outer scope, so jumping to it is always OK.  */
4948   if (lab == 0)
4949     return;
4950
4951   if ((lab->in_try_scope || lab->in_catch_scope || lab->bad_decls)
4952       && !identified)
4953     {
4954       cp_pedwarn_at ("jump to label `%D'", decl);
4955       pedwarn ("  from here");
4956       identified = 1;
4957     }
4958
4959   for (bad = lab->bad_decls; bad; bad = TREE_CHAIN (bad))
4960     {
4961       tree b = TREE_VALUE (bad);
4962       int u = decl_jump_unsafe (b);
4963
4964       if (u > 1 && DECL_ARTIFICIAL (b))
4965         /* Can't skip init of __exception_info.  */
4966         cp_error_at ("  enters catch block", b);
4967       else if (u > 1)
4968         cp_error_at ("  skips initialization of `%#D'", b);
4969       else
4970         cp_pedwarn_at ("  enters scope of non-POD `%#D'", b);
4971     }
4972
4973   if (lab->in_try_scope)
4974     error ("  enters try block");
4975   else if (lab->in_catch_scope)
4976     error ("  enters catch block");
4977 }
4978
4979 /* Define a label, specifying the location in the source file.
4980    Return the LABEL_DECL node for the label, if the definition is valid.
4981    Otherwise return 0.  */
4982
4983 tree
4984 define_label (const char* filename, int line, tree name)
4985 {
4986   tree decl = lookup_label (name);
4987   struct named_label_list *ent;
4988   register struct cp_binding_level *p;
4989
4990   timevar_push (TV_NAME_LOOKUP);
4991   for (ent = named_labels; ent; ent = ent->next)
4992     if (ent->label_decl == decl)
4993       break;
4994
4995   /* After labels, make any new cleanups in the function go into their
4996      own new (temporary) binding contour.  */
4997   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
4998     p->more_cleanups_ok = 0;
4999
5000   if (name == get_identifier ("wchar_t"))
5001     pedwarn ("label named wchar_t");
5002
5003   if (DECL_INITIAL (decl) != NULL_TREE)
5004     {
5005       error ("duplicate label `%D'", decl);
5006       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5007     }
5008   else
5009     {
5010       /* Mark label as having been defined.  */
5011       DECL_INITIAL (decl) = error_mark_node;
5012       /* Say where in the source.  */
5013       DECL_SOURCE_FILE (decl) = filename;
5014       DECL_SOURCE_LINE (decl) = line;
5015       if (ent)
5016         {
5017           ent->names_in_scope = current_binding_level->names;
5018           ent->binding_level = current_binding_level;
5019         }
5020       check_previous_gotos (decl);
5021       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, decl);
5022     }
5023   timevar_pop (TV_NAME_LOOKUP);
5024 }
5025
5026 struct cp_switch
5027 {
5028   struct cp_binding_level *level;
5029   struct cp_switch *next;
5030   /* The SWITCH_STMT being built.  */
5031   tree switch_stmt;
5032   /* A splay-tree mapping the low element of a case range to the high
5033      element, or NULL_TREE if there is no high element.  Used to
5034      determine whether or not a new case label duplicates an old case
5035      label.  We need a tree, rather than simply a hash table, because
5036      of the GNU case range extension.  */
5037   splay_tree cases;
5038 };
5039
5040 /* A stack of the currently active switch statements.  The innermost
5041    switch statement is on the top of the stack.  There is no need to
5042    mark the stack for garbage collection because it is only active
5043    during the processing of the body of a function, and we never
5044    collect at that point.  */
5045
5046 static struct cp_switch *switch_stack;
5047
5048 /* Called right after a switch-statement condition is parsed.
5049    SWITCH_STMT is the switch statement being parsed.  */
5050
5051 void
5052 push_switch (tree switch_stmt)
5053 {
5054   struct cp_switch *p
5055     = (struct cp_switch *) xmalloc (sizeof (struct cp_switch));
5056   p->level = current_binding_level;
5057   p->next = switch_stack;
5058   p->switch_stmt = switch_stmt;
5059   p->cases = splay_tree_new (case_compare, NULL, NULL);
5060   switch_stack = p;
5061 }
5062
5063 void
5064 pop_switch (void)
5065 {
5066   struct cp_switch *cs;
5067
5068   cs = switch_stack;
5069   splay_tree_delete (cs->cases);
5070   switch_stack = switch_stack->next;
5071   free (cs);
5072 }
5073
5074 /* Note that we've seen a definition of a case label, and complain if this
5075    is a bad place for one.  */
5076
5077 tree
5078 finish_case_label (tree low_value, tree high_value)
5079 {
5080   tree cond, r;
5081   register struct cp_binding_level *p;
5082
5083   if (! switch_stack)
5084     {
5085       if (high_value)
5086         error ("case label not within a switch statement");
5087       else if (low_value)
5088         error ("case label `%E' not within a switch statement",
5089                   low_value);
5090       else
5091         error ("`default' label not within a switch statement");
5092       return NULL_TREE;
5093     }
5094
5095   if (processing_template_decl)
5096     {
5097       tree label;
5098
5099       /* For templates, just add the case label; we'll do semantic
5100          analysis at instantiation-time.  */
5101       label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
5102       return add_stmt (build_case_label (low_value, high_value, label));
5103     }
5104
5105   /* Find the condition on which this switch statement depends.  */
5106   cond = SWITCH_COND (switch_stack->switch_stmt);
5107   if (cond && TREE_CODE (cond) == TREE_LIST)
5108     cond = TREE_VALUE (cond);
5109
5110   r = c_add_case_label (switch_stack->cases, cond, low_value, high_value);
5111
5112   check_switch_goto (switch_stack->level);
5113
5114   /* After labels, make any new cleanups in the function go into their
5115      own new (temporary) binding contour.  */
5116   for (p = current_binding_level; !(p->parm_flag); p = p->level_chain)
5117     p->more_cleanups_ok = 0;
5118
5119   return r;
5120 }
5121 \f
5122 /* Return the list of declarations of the current level.
5123    Note that this list is in reverse order unless/until
5124    you nreverse it; and when you do nreverse it, you must
5125    store the result back using `storedecls' or you will lose.  */
5126
5127 tree
5128 getdecls (void)
5129 {
5130   return current_binding_level->names;
5131 }
5132
5133 /* Return the list of type-tags (for structs, etc) of the current level.  */
5134
5135 tree
5136 gettags (void)
5137 {
5138   return current_binding_level->tags;
5139 }
5140
5141 /* Store the list of declarations of the current level.
5142    This is done for the parameter declarations of a function being defined,
5143    after they are modified in the light of any missing parameters.  */
5144
5145 static void
5146 storedecls (tree decls)
5147 {
5148   current_binding_level->names = decls;
5149 }
5150
5151 /* Similarly, store the list of tags of the current level.  */
5152
5153 void
5154 storetags (tree tags)
5155 {
5156   current_binding_level->tags = tags;
5157 }
5158 \f
5159 /* Return the type that should be used when TYPE's name is preceded
5160    by a tag such as 'struct' or 'union', or null if the name cannot
5161    be used in this way.
5162
5163    For example, when processing the third line of:
5164
5165         struct A;
5166         typedef struct A A;
5167         struct A;
5168
5169    lookup of A will find the typedef.  Given A's typedef, this function
5170    will return the type associated with "struct A".  For the tag to be
5171    anything other than TYPE, TYPE must be a typedef whose original type
5172    has the same name and context as TYPE itself.
5173
5174    It is not valid for a typedef of an anonymous type to be used with
5175    an explicit tag:
5176
5177        typedef struct { ... } B;
5178        struct B;
5179
5180    Return null for this case.  */
5181
5182 static tree
5183 follow_tag_typedef (tree type)
5184 {
5185   tree original;
5186
5187   original = original_type (type);
5188   if (! TYPE_NAME (original))
5189     return NULL_TREE;
5190   if (TYPE_IDENTIFIER (original) == TYPE_IDENTIFIER (type)
5191       && (CP_DECL_CONTEXT (TYPE_NAME (original))
5192           == CP_DECL_CONTEXT (TYPE_NAME (type)))
5193       && !(CLASS_TYPE_P (original) && TYPE_WAS_ANONYMOUS (original)))
5194     return original;
5195   else
5196     return NULL_TREE;
5197 }
5198
5199 /* Given NAME, an IDENTIFIER_NODE,
5200    return the structure (or union or enum) definition for that name.
5201    Searches binding levels from BINDING_LEVEL up to the global level.
5202    If THISLEVEL_ONLY is nonzero, searches only the specified context
5203    (but skips any tag-transparent contexts to find one that is
5204    meaningful for tags).
5205    FORM says which kind of type the caller wants;
5206    it is RECORD_TYPE or UNION_TYPE or ENUMERAL_TYPE.
5207    If the wrong kind of type is found, and it's not a template, an error is
5208    reported.  */
5209
5210 static tree
5211 lookup_tag (enum tree_code form,
5212             tree name,
5213             struct cp_binding_level* binding_level,
5214             int thislevel_only)
5215 {
5216   register struct cp_binding_level *level;
5217   /* Nonzero if, we should look past a template parameter level, even
5218      if THISLEVEL_ONLY.  */
5219   int allow_template_parms_p = 1;
5220
5221   timevar_push (TV_NAME_LOOKUP);
5222   for (level = binding_level; level; level = level->level_chain)
5223     {
5224       register tree tail;
5225       if (ANON_AGGRNAME_P (name))
5226         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5227           {
5228             /* There's no need for error checking here, because
5229                anon names are unique throughout the compilation.  */
5230             if (TYPE_IDENTIFIER (TREE_VALUE (tail)) == name)
5231               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5232           }
5233       else if (level->namespace_p)
5234         /* Do namespace lookup.  */
5235         for (tail = current_namespace; 1; tail = CP_DECL_CONTEXT (tail))
5236           {
5237             tree old = binding_for_name (name, tail);
5238
5239             /* If we just skipped past a template parameter level,
5240                even though THISLEVEL_ONLY, and we find a template
5241                class declaration, then we use the _TYPE node for the
5242                template.  See the example below.  */
5243             if (thislevel_only && !allow_template_parms_p
5244                 && old && BINDING_VALUE (old)
5245                 && DECL_CLASS_TEMPLATE_P (BINDING_VALUE (old)))
5246               old = TREE_TYPE (BINDING_VALUE (old));
5247             else
5248               old = BINDING_TYPE (old);
5249
5250             if (old)
5251               {
5252                 /* We've found something at this binding level.  If it is
5253                    a typedef, extract the tag it refers to.  Lookup fails
5254                    if the typedef doesn't refer to a taggable type.  */
5255                 old = follow_tag_typedef (old);
5256                 if (!old)
5257                   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5258                 if (TREE_CODE (old) != form
5259                     && (form == ENUMERAL_TYPE
5260                         || TREE_CODE (old) == ENUMERAL_TYPE))
5261                   {
5262                     error ("`%#D' redeclared as %C", old, form);
5263                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5264                   }
5265                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, old);
5266               }
5267             if (thislevel_only || tail == global_namespace)
5268               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5269           }
5270       else
5271         for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5272           {
5273             if (TREE_PURPOSE (tail) == name)
5274               {
5275                 enum tree_code code = TREE_CODE (TREE_VALUE (tail));
5276                 
5277                 if (code != form
5278                     && (form == ENUMERAL_TYPE || code == ENUMERAL_TYPE))
5279                   {
5280                     /* Definition isn't the kind we were looking for.  */
5281                     error ("`%#D' redeclared as %C", TREE_VALUE (tail), form);
5282                     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5283                   }
5284                 POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_VALUE (tail));
5285               }
5286           }
5287       if (thislevel_only && ! level->tag_transparent)
5288         {
5289           if (level->template_parms_p && allow_template_parms_p)
5290             {
5291               /* We must deal with cases like this:
5292
5293                    template <class T> struct S;
5294                    template <class T> struct S {};
5295
5296                  When looking up `S', for the second declaration, we
5297                  would like to find the first declaration.  But, we
5298                  are in the pseudo-global level created for the
5299                  template parameters, rather than the (surrounding)
5300                  namespace level.  Thus, we keep going one more level,
5301                  even though THISLEVEL_ONLY is nonzero.  */
5302               allow_template_parms_p = 0;
5303               continue;
5304             }
5305           else
5306             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5307         }
5308     }
5309   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5310 }
5311
5312 /* Given a type, find the tag that was defined for it and return the tag name.
5313    Otherwise return 0.  However, the value can never be 0
5314    in the cases in which this is used.
5315
5316    C++: If NAME is nonzero, this is the new name to install.  This is
5317    done when replacing anonymous tags with real tag names.  */
5318
5319 static tree
5320 lookup_tag_reverse (tree type, tree name)
5321 {
5322   register struct cp_binding_level *level;
5323
5324   timevar_push (TV_NAME_LOOKUP);
5325   for (level = current_binding_level; level; level = level->level_chain)
5326     {
5327       register tree tail;
5328       for (tail = level->tags; tail; tail = TREE_CHAIN (tail))
5329         {
5330           if (TREE_VALUE (tail) == type)
5331             {
5332               if (name)
5333                 TREE_PURPOSE (tail) = name;
5334               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, TREE_PURPOSE (tail));
5335             }
5336         }
5337     }
5338   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5339 }
5340 \f
5341 /* Look up NAME in the NAMESPACE.  */
5342
5343 tree
5344 lookup_namespace_name (tree namespace, tree name)
5345 {
5346   tree val;
5347   tree template_id = NULL_TREE;
5348
5349   timevar_push (TV_NAME_LOOKUP);
5350   my_friendly_assert (TREE_CODE (namespace) == NAMESPACE_DECL, 370);
5351
5352   if (TREE_CODE (name) == NAMESPACE_DECL)
5353     /* This happens for A::B<int> when B is a namespace.  */
5354     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, name);
5355   else if (TREE_CODE (name) == TEMPLATE_DECL)
5356     {
5357       /* This happens for A::B where B is a template, and there are no
5358          template arguments.  */
5359       error ("invalid use of `%D'", name);
5360       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5361     }
5362
5363   namespace = ORIGINAL_NAMESPACE (namespace);
5364
5365   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5366     {
5367       template_id = name;
5368       name = TREE_OPERAND (name, 0);
5369       if (TREE_CODE (name) == OVERLOAD)
5370         name = DECL_NAME (OVL_CURRENT (name));
5371       else if (DECL_P (name))
5372         name = DECL_NAME (name);
5373     }
5374
5375   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 373);
5376
5377   val = make_node (CPLUS_BINDING);
5378   if (!qualified_lookup_using_namespace (name, namespace, val, 0))
5379     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5380
5381   if (BINDING_VALUE (val))
5382     {
5383       val = BINDING_VALUE (val);
5384
5385       if (template_id)
5386         {
5387           if (DECL_CLASS_TEMPLATE_P (val))
5388             val = lookup_template_class (val,
5389                                          TREE_OPERAND (template_id, 1),
5390                                          /*in_decl=*/NULL_TREE,
5391                                          /*context=*/NULL_TREE,
5392                                          /*entering_scope=*/0,
5393                                          tf_error | tf_warning);
5394           else if (DECL_FUNCTION_TEMPLATE_P (val)
5395                    || TREE_CODE (val) == OVERLOAD)
5396             val = lookup_template_function (val,
5397                                             TREE_OPERAND (template_id, 1));
5398           else
5399             {
5400               error ("`%D::%D' is not a template",
5401                         namespace, name);
5402               POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5403             }
5404         }
5405
5406       /* If we have a single function from a using decl, pull it out.  */
5407       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
5408         val = OVL_FUNCTION (val);
5409
5410       /* Ignore built-in functions that haven't been prototyped yet.  */
5411       if (!val || !DECL_P(val)
5412           || !DECL_LANG_SPECIFIC(val)
5413           || !DECL_ANTICIPATED (val))
5414         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5415     }
5416
5417   error ("`%D' undeclared in namespace `%D'", name, namespace);
5418   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5419 }
5420
5421 /* Hash a TYPENAME_TYPE.  K is really of type `tree'.  */
5422
5423 static hashval_t
5424 typename_hash (const void* k)
5425 {
5426   hashval_t hash;
5427   tree t = (tree) k;
5428
5429   hash = (htab_hash_pointer (TYPE_CONTEXT (t))
5430           ^ htab_hash_pointer (DECL_NAME (TYPE_NAME (t))));
5431
5432   return hash;
5433 }
5434
5435 /* Compare two TYPENAME_TYPEs.  K1 and K2 are really of type `tree'.  */
5436
5437 static int
5438 typename_compare (const void * k1, const void * k2)
5439 {
5440   tree t1;
5441   tree t2;
5442   tree d1;
5443   tree d2;
5444
5445   t1 = (tree) k1;
5446   t2 = (tree) k2;
5447   d1 = TYPE_NAME (t1);
5448   d2 = TYPE_NAME (t2);
5449
5450   return (DECL_NAME (d1) == DECL_NAME (d2)
5451           && TYPE_CONTEXT (t1) == TYPE_CONTEXT (t2)
5452           && ((TREE_TYPE (t1) != NULL_TREE)
5453               == (TREE_TYPE (t2) != NULL_TREE))
5454           && same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
5455           && TYPENAME_TYPE_FULLNAME (t1) == TYPENAME_TYPE_FULLNAME (t2));
5456 }
5457
5458 /* Build a TYPENAME_TYPE.  If the type is `typename T::t', CONTEXT is
5459    the type of `T', NAME is the IDENTIFIER_NODE for `t'.  If BASE_TYPE
5460    is non-NULL, this type is being created by the implicit typename
5461    extension, and BASE_TYPE is a type named `t' in some base class of
5462    `T' which depends on template parameters.
5463
5464    Returns the new TYPENAME_TYPE.  */
5465
5466 static GTY ((param_is (union tree_node))) htab_t typename_htab;
5467
5468 tree
5469 build_typename_type (tree context, tree name, tree fullname)
5470 {
5471   tree t;
5472   tree d;
5473   PTR *e;
5474
5475   if (typename_htab == NULL)
5476     {
5477       typename_htab = htab_create_ggc (61, &typename_hash, 
5478                                        &typename_compare, NULL);
5479     }
5480
5481   /* Build the TYPENAME_TYPE.  */
5482   t = make_aggr_type (TYPENAME_TYPE);
5483   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5484   TYPENAME_TYPE_FULLNAME (t) = fullname;
5485
5486   /* Build the corresponding TYPE_DECL.  */
5487   d = build_decl (TYPE_DECL, name, t);
5488   TYPE_NAME (TREE_TYPE (d)) = d;
5489   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5490   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5491   DECL_ARTIFICIAL (d) = 1;
5492
5493   /* See if we already have this type.  */
5494   e = htab_find_slot (typename_htab, t, INSERT);
5495   if (*e)
5496     t = (tree) *e;
5497   else
5498     *e = t;
5499
5500   return t;
5501 }
5502
5503 /* Resolve `typename CONTEXT::NAME'.  Returns an appropriate type,
5504    unless an error occurs, in which case error_mark_node is returned.
5505    If we locate a non-artificial TYPE_DECL and TF_KEEP_TYPE_DECL is
5506    set, we return that, rather than the _TYPE it corresponds to, in
5507    other cases we look through the type decl.  If TF_ERROR is set,
5508    complain about errors, otherwise be quiet.  */
5509
5510 tree
5511 make_typename_type (tree context, tree name, tsubst_flags_t complain)
5512 {
5513   tree fullname;
5514
5515   if (TYPE_P (name))
5516     {
5517       if (!(TYPE_LANG_SPECIFIC (name)
5518             && (CLASSTYPE_IS_TEMPLATE (name)
5519                 || CLASSTYPE_USE_TEMPLATE (name))))
5520         name = TYPE_IDENTIFIER (name);
5521       else
5522         /* Create a TEMPLATE_ID_EXPR for the type.  */
5523         name = build_nt (TEMPLATE_ID_EXPR,
5524                          CLASSTYPE_TI_TEMPLATE (name),
5525                          CLASSTYPE_TI_ARGS (name));
5526     }
5527   else if (TREE_CODE (name) == TYPE_DECL)
5528     name = DECL_NAME (name);
5529
5530   fullname = name;
5531
5532   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
5533     {
5534       name = TREE_OPERAND (name, 0);
5535       if (TREE_CODE (name) == TEMPLATE_DECL)
5536         name = TREE_OPERAND (fullname, 0) = DECL_NAME (name);
5537     }
5538   if (TREE_CODE (name) == TEMPLATE_DECL)
5539     {
5540       error ("`%D' used without template parameters", name);
5541       return error_mark_node;
5542     }
5543   if (TREE_CODE (name) != IDENTIFIER_NODE)
5544     abort ();
5545
5546   if (TREE_CODE (context) == NAMESPACE_DECL)
5547     {
5548       /* We can get here from typename_sub0 in the explicit_template_type
5549          expansion.  Just fail.  */
5550       if (complain & tf_error)
5551         error ("no class template named `%#T' in `%#T'",
5552                   name, context);
5553       return error_mark_node;
5554     }
5555
5556   if (! uses_template_parms (context)
5557       || currently_open_class (context))
5558     {
5559       if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR)
5560         {
5561           tree tmpl = NULL_TREE;
5562           if (IS_AGGR_TYPE (context))
5563             tmpl = lookup_field (context, name, 0, false);
5564           if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5565             {
5566               if (complain & tf_error)
5567                 error ("no class template named `%#T' in `%#T'",
5568                           name, context);
5569               return error_mark_node;
5570             }
5571
5572           if (complain & tf_error)
5573             {
5574               if (complain & tf_parsing)
5575                 perform_or_defer_access_check (context, tmpl);
5576               else
5577                 enforce_access (context, tmpl);
5578             }
5579
5580           return lookup_template_class (tmpl,
5581                                         TREE_OPERAND (fullname, 1),
5582                                         NULL_TREE, context,
5583                                         /*entering_scope=*/0,
5584                                         tf_error | tf_warning);
5585         }
5586       else
5587         {
5588           tree t;
5589
5590           if (!IS_AGGR_TYPE (context))
5591             {
5592               if (complain & tf_error)
5593                 error ("no type named `%#T' in `%#T'", name, context);
5594               return error_mark_node;
5595             }
5596
5597           t = lookup_field (context, name, 0, true);
5598           if (t)
5599             {
5600               if (TREE_CODE (t) != TYPE_DECL)
5601                 {
5602                   if (complain & tf_error)
5603                     error ("no type named `%#T' in `%#T'", name, context);
5604                   return error_mark_node;
5605                 }
5606
5607               if (complain & tf_error)
5608                 {
5609                   if (complain & tf_parsing)
5610                     perform_or_defer_access_check (context, t);
5611                   else
5612                     enforce_access (context, t);
5613                 }
5614
5615               if (DECL_ARTIFICIAL (t) || !(complain & tf_keep_type_decl))
5616                 t = TREE_TYPE (t);
5617               
5618               return t;
5619             }
5620         }
5621     }
5622
5623   /* If the CONTEXT is not a template type, then either the field is
5624      there now or its never going to be.  */
5625   if (!uses_template_parms (context))
5626     {
5627       if (complain & tf_error)
5628         error ("no type named `%#T' in `%#T'", name, context);
5629       return error_mark_node;
5630     }
5631
5632   return build_typename_type (context, name, fullname);
5633 }
5634
5635 /* Resolve `CONTEXT::template NAME'.  Returns an appropriate type,
5636    unless an error occurs, in which case error_mark_node is returned.
5637    If we locate a TYPE_DECL, we return that, rather than the _TYPE it
5638    corresponds to.  If COMPLAIN zero, don't complain about any errors
5639    that occur.  */
5640
5641 tree
5642 make_unbound_class_template (tree context, tree name, tsubst_flags_t complain)
5643 {
5644   tree t;
5645   tree d;
5646
5647   if (TYPE_P (name))
5648     name = TYPE_IDENTIFIER (name);
5649   else if (DECL_P (name))
5650     name = DECL_NAME (name);
5651   if (TREE_CODE (name) != IDENTIFIER_NODE)
5652     abort ();
5653
5654   if (!uses_template_parms (context)
5655       || currently_open_class (context))
5656     {
5657       tree tmpl = NULL_TREE;
5658
5659       if (IS_AGGR_TYPE (context))
5660         tmpl = lookup_field (context, name, 0, false);
5661
5662       if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
5663         {
5664           if (complain & tf_error)
5665             error ("no class template named `%#T' in `%#T'", name, context);
5666           return error_mark_node;
5667         }
5668       
5669       if (complain & tf_error)
5670         {
5671           if (complain & tf_parsing)
5672             perform_or_defer_access_check (context, tmpl);
5673           else
5674             enforce_access (context, tmpl);
5675         }
5676
5677       return tmpl;
5678     }
5679
5680   /* Build the UNBOUND_CLASS_TEMPLATE.  */
5681   t = make_aggr_type (UNBOUND_CLASS_TEMPLATE);
5682   TYPE_CONTEXT (t) = FROB_CONTEXT (context);
5683   TREE_TYPE (t) = NULL_TREE;
5684
5685   /* Build the corresponding TEMPLATE_DECL.  */
5686   d = build_decl (TEMPLATE_DECL, name, t);
5687   TYPE_NAME (TREE_TYPE (d)) = d;
5688   TYPE_STUB_DECL (TREE_TYPE (d)) = d;
5689   DECL_CONTEXT (d) = FROB_CONTEXT (context);
5690   DECL_ARTIFICIAL (d) = 1;
5691
5692   return t;
5693 }
5694
5695 /* Select the right _DECL from multiple choices.  */
5696
5697 static tree
5698 select_decl (tree binding, int flags)
5699 {
5700   tree val;
5701   val = BINDING_VALUE (binding);
5702
5703   timevar_push (TV_NAME_LOOKUP);
5704   if (LOOKUP_NAMESPACES_ONLY (flags))
5705     {
5706       /* We are not interested in types.  */
5707       if (val && TREE_CODE (val) == NAMESPACE_DECL)
5708         POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5709       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5710     }
5711
5712   /* If we could have a type and
5713      we have nothing or we need a type and have none.  */
5714   if (BINDING_TYPE (binding)
5715       && (!val || ((flags & LOOKUP_PREFER_TYPES)
5716                    && TREE_CODE (val) != TYPE_DECL)))
5717     val = TYPE_STUB_DECL (BINDING_TYPE (binding));
5718   /* Don't return non-types if we really prefer types.  */
5719   else if (val && LOOKUP_TYPES_ONLY (flags)  && TREE_CODE (val) != TYPE_DECL
5720            && (TREE_CODE (val) != TEMPLATE_DECL
5721                || !DECL_CLASS_TEMPLATE_P (val)))
5722     val = NULL_TREE;
5723
5724   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5725 }
5726
5727 /* Unscoped lookup of a global: iterate over current namespaces,
5728    considering using-directives.  If SPACESP is non-NULL, store a list
5729    of the namespaces we've considered in it.  */
5730
5731 tree
5732 unqualified_namespace_lookup (tree name, int flags, tree* spacesp)
5733 {
5734   tree b = make_node (CPLUS_BINDING);
5735   tree initial = current_decl_namespace ();
5736   tree scope = initial;
5737   tree siter;
5738   struct cp_binding_level *level;
5739   tree val = NULL_TREE;
5740
5741   timevar_push (TV_NAME_LOOKUP);
5742   if (spacesp)
5743     *spacesp = NULL_TREE;
5744
5745   for (; !val; scope = CP_DECL_CONTEXT (scope))
5746     {
5747       if (spacesp)
5748         *spacesp = tree_cons (scope, NULL_TREE, *spacesp);
5749       val = binding_for_name (name, scope);
5750
5751       /* Ignore anticipated built-in functions.  */
5752       if (val && BINDING_VALUE (val)
5753           && DECL_P (BINDING_VALUE (val))
5754           && DECL_LANG_SPECIFIC (BINDING_VALUE (val))
5755           && DECL_ANTICIPATED (BINDING_VALUE (val)))
5756         {
5757           BINDING_VALUE (b) = NULL_TREE;
5758           BINDING_TYPE (b) = NULL_TREE;
5759         }
5760       else
5761         {
5762           /* Initialize binding for this context.  */
5763           BINDING_VALUE (b) = BINDING_VALUE (val);
5764           BINDING_TYPE (b) = BINDING_TYPE (val);
5765         }
5766
5767       /* Add all _DECLs seen through local using-directives.  */
5768       for (level = current_binding_level;
5769            !level->namespace_p;
5770            level = level->level_chain)
5771         if (!lookup_using_namespace (name, b, level->using_directives,
5772                                      scope, flags, spacesp))
5773           /* Give up because of error.  */
5774           POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5775
5776       /* Add all _DECLs seen through global using-directives.  */
5777       /* XXX local and global using lists should work equally.  */
5778       siter = initial;
5779       while (1)
5780         {
5781           if (!lookup_using_namespace (name, b, DECL_NAMESPACE_USING (siter),
5782                                        scope, flags, spacesp))
5783             /* Give up because of error.  */
5784             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, error_mark_node);
5785           if (siter == scope) break;
5786           siter = CP_DECL_CONTEXT (siter);
5787         }
5788
5789       val = select_decl (b, flags);
5790       if (scope == global_namespace)
5791         break;
5792     }
5793   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
5794 }
5795
5796 /* Combine prefer_type and namespaces_only into flags.  */
5797
5798 static int
5799 lookup_flags (int prefer_type, int namespaces_only)
5800 {
5801   if (namespaces_only)
5802     return LOOKUP_PREFER_NAMESPACES;
5803   if (prefer_type > 1)
5804     return LOOKUP_PREFER_TYPES;
5805   if (prefer_type > 0)
5806     return LOOKUP_PREFER_BOTH;
5807   return 0;
5808 }
5809
5810 /* Given a lookup that returned VAL, use FLAGS to decide if we want to
5811    ignore it or not.  Subroutine of lookup_name_real.  */
5812
5813 static tree
5814 qualify_lookup (tree val, int flags)
5815 {
5816   if (val == NULL_TREE)
5817     return val;
5818   if ((flags & LOOKUP_PREFER_NAMESPACES) && TREE_CODE (val) == NAMESPACE_DECL)
5819     return val;
5820   if ((flags & LOOKUP_PREFER_TYPES) && TREE_CODE (val) == TYPE_DECL)
5821     return val;
5822   if (flags & (LOOKUP_PREFER_NAMESPACES | LOOKUP_PREFER_TYPES))
5823     return NULL_TREE;
5824   return val;
5825 }
5826
5827 /* Look up NAME (an IDENTIFIER_NODE) in SCOPE (either a NAMESPACE_DECL
5828    or a class TYPE).  If IS_TYPE_P is TRUE, then ignore non-type
5829    bindings.  
5830
5831    Returns a DECL (or OVERLOAD, or BASELINK) representing the
5832    declaration found.  */
5833
5834 tree
5835 lookup_qualified_name (tree scope, tree name, bool is_type_p, int flags)
5836 {
5837   if (TREE_CODE (scope) == NAMESPACE_DECL)
5838     {
5839       tree val;
5840
5841       val = make_node (CPLUS_BINDING);
5842       flags |= LOOKUP_COMPLAIN;
5843       if (is_type_p)
5844         flags |= LOOKUP_PREFER_TYPES;
5845       if (!qualified_lookup_using_namespace (name, scope, val, flags))
5846         return NULL_TREE;
5847       return select_decl (val, flags);
5848     }
5849   else
5850     return lookup_member (scope, name, 0, is_type_p);
5851 }
5852
5853 /* Check to see whether or not DECL is a variable that would have been
5854    in scope under the ARM, but is not in scope under the ANSI/ISO
5855    standard.  If so, issue an error message.  If name lookup would
5856    work in both cases, but return a different result, this function
5857    returns the result of ANSI/ISO lookup.  Otherwise, it returns
5858    DECL.  */
5859
5860 tree
5861 check_for_out_of_scope_variable (tree decl)
5862 {
5863   tree shadowed;
5864
5865   /* We only care about out of scope variables.  */
5866   if (!(TREE_CODE (decl) == VAR_DECL && DECL_DEAD_FOR_LOCAL (decl)))
5867     return decl;
5868
5869   shadowed = DECL_SHADOWED_FOR_VAR (decl);
5870   while (shadowed != NULL_TREE && TREE_CODE (shadowed) == VAR_DECL
5871          && DECL_DEAD_FOR_LOCAL (shadowed))
5872     shadowed = DECL_SHADOWED_FOR_VAR (shadowed);
5873   if (!shadowed)
5874     shadowed = IDENTIFIER_NAMESPACE_VALUE (DECL_NAME (decl));
5875   if (shadowed)
5876     {
5877       if (!DECL_ERROR_REPORTED (decl))
5878         {
5879           warning ("name lookup of `%D' changed",
5880                       DECL_NAME (decl));
5881           cp_warning_at ("  matches this `%D' under ISO standard rules",
5882                          shadowed);
5883           cp_warning_at ("  matches this `%D' under old rules", decl);
5884           DECL_ERROR_REPORTED (decl) = 1;
5885         }
5886       return shadowed;
5887     }
5888
5889   /* If we have already complained about this declaration, there's no
5890      need to do it again.  */
5891   if (DECL_ERROR_REPORTED (decl))
5892     return decl;
5893
5894   DECL_ERROR_REPORTED (decl) = 1;
5895   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
5896     {
5897       error ("name lookup of `%D' changed for new ISO `for' scoping",
5898              DECL_NAME (decl));
5899       cp_error_at ("  cannot use obsolete binding at `%D' because it has a destructor", decl);
5900       return error_mark_node;
5901     }
5902   else
5903     {
5904       pedwarn ("name lookup of `%D' changed for new ISO `for' scoping",
5905                DECL_NAME (decl));
5906       cp_pedwarn_at ("  using obsolete binding at `%D'", decl);
5907     }
5908
5909   return decl;
5910 }
5911
5912 /* Look up NAME in the current binding level and its superiors in the
5913    namespace of variables, functions and typedefs.  Return a ..._DECL
5914    node of some kind representing its definition if there is only one
5915    such declaration, or return a TREE_LIST with all the overloaded
5916    definitions if there are many, or return 0 if it is undefined.
5917
5918    If PREFER_TYPE is > 0, we prefer TYPE_DECLs or namespaces.
5919    If PREFER_TYPE is > 1, we reject non-type decls (e.g. namespaces).
5920    Otherwise we prefer non-TYPE_DECLs.
5921
5922    If NONCLASS is nonzero, we don't look for the NAME in class scope,
5923    using IDENTIFIER_CLASS_VALUE.  */
5924
5925 tree
5926 lookup_name_real (tree name, 
5927                   int prefer_type, 
5928                   int nonclass, 
5929                   int namespaces_only,
5930                   int flags)
5931 {
5932   tree t;
5933   tree val = NULL_TREE;
5934
5935   timevar_push (TV_NAME_LOOKUP);
5936   /* Conversion operators are handled specially because ordinary
5937      unqualified name lookup will not find template conversion
5938      operators.  */
5939   if (IDENTIFIER_TYPENAME_P (name)) 
5940     {
5941       struct cp_binding_level *level;
5942
5943       for (level = current_binding_level; 
5944            level && !level->namespace_p; 
5945            level = level->level_chain)
5946         {
5947           tree class_type;
5948           tree operators;
5949           
5950           /* A conversion operator can only be declared in a class 
5951              scope.  */
5952           if (level->parm_flag != 2)
5953             continue;
5954           
5955           /* Lookup the conversion operator in the class.  */
5956           class_type = level->this_class;
5957           operators = lookup_fnfields (class_type, name, /*protect=*/0);
5958           if (operators)
5959             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, operators);
5960         }
5961
5962       POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, NULL_TREE);
5963     }
5964
5965   flags |= lookup_flags (prefer_type, namespaces_only);
5966
5967   /* First, look in non-namespace scopes.  */
5968
5969   if (current_class_type == NULL_TREE)
5970     nonclass = 1;
5971
5972   for (t = IDENTIFIER_BINDING (name); t; t = TREE_CHAIN (t))
5973     {
5974       tree binding;
5975
5976       if (!LOCAL_BINDING_P (t) && nonclass)
5977         /* We're not looking for class-scoped bindings, so keep going.  */
5978         continue;
5979
5980       /* If this is the kind of thing we're looking for, we're done.  */
5981       if (qualify_lookup (BINDING_VALUE (t), flags))
5982         binding = BINDING_VALUE (t);
5983       else if ((flags & LOOKUP_PREFER_TYPES)
5984                && qualify_lookup (BINDING_TYPE (t), flags))
5985         binding = BINDING_TYPE (t);
5986       else
5987         binding = NULL_TREE;
5988
5989       if (binding)
5990         {
5991           val = binding;
5992           break;
5993         }
5994     }
5995
5996   /* Now lookup in namespace scopes.  */
5997   if (!val)
5998     {
5999       t = unqualified_namespace_lookup (name, flags, 0);
6000       if (t)
6001         val = t;
6002     }
6003
6004   if (val)
6005     {
6006       /* If we have a single function from a using decl, pull it out.  */
6007       if (TREE_CODE (val) == OVERLOAD && ! really_overloaded_fn (val))
6008         val = OVL_FUNCTION (val);
6009     }
6010
6011   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, val);
6012 }
6013
6014 tree
6015 lookup_name_nonclass (tree name)
6016 {
6017   return lookup_name_real (name, 0, 1, 0, LOOKUP_COMPLAIN);
6018 }
6019
6020 tree
6021 lookup_function_nonclass (tree name, tree args)
6022 {
6023   return lookup_arg_dependent (name, lookup_name_nonclass (name), args);
6024 }
6025
6026 tree
6027 lookup_name (tree name, int prefer_type)
6028 {
6029   return lookup_name_real (name, prefer_type, 0, 0, LOOKUP_COMPLAIN);
6030 }
6031
6032 /* Similar to `lookup_name' but look only in the innermost non-class
6033    binding level.  */
6034
6035 tree
6036 lookup_name_current_level (tree name)
6037 {
6038   struct cp_binding_level *b;
6039   tree t = NULL_TREE;
6040
6041   timevar_push (TV_NAME_LOOKUP);
6042   b = current_binding_level;
6043   while (b->parm_flag == 2)
6044     b = b->level_chain;
6045
6046   if (b->namespace_p)
6047     {
6048       t = IDENTIFIER_NAMESPACE_VALUE (name);
6049
6050       /* extern "C" function() */
6051       if (t != NULL_TREE && TREE_CODE (t) == TREE_LIST)
6052         t = TREE_VALUE (t);
6053     }
6054   else if (IDENTIFIER_BINDING (name)
6055            && LOCAL_BINDING_P (IDENTIFIER_BINDING (name)))
6056     {
6057       while (1)
6058         {
6059           if (BINDING_LEVEL (IDENTIFIER_BINDING (name)) == b)
6060             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, IDENTIFIER_VALUE (name));
6061
6062           if (b->keep == 2)
6063             b = b->level_chain;
6064           else
6065             break;
6066         }
6067     }
6068
6069   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6070 }
6071
6072 /* Like lookup_name_current_level, but for types.  */
6073
6074 tree
6075 lookup_type_current_level (tree name)
6076 {
6077   register tree t = NULL_TREE;
6078
6079   timevar_push (TV_NAME_LOOKUP);
6080   my_friendly_assert (! current_binding_level->namespace_p, 980716);
6081
6082   if (REAL_IDENTIFIER_TYPE_VALUE (name) != NULL_TREE
6083       && REAL_IDENTIFIER_TYPE_VALUE (name) != global_type_node)
6084     {
6085       struct cp_binding_level *b = current_binding_level;
6086       while (1)
6087         {
6088           if (purpose_member (name, b->type_shadowed))
6089             POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP,
6090                                     REAL_IDENTIFIER_TYPE_VALUE (name));
6091           if (b->keep == 2)
6092             b = b->level_chain;
6093           else
6094             break;
6095         }
6096     }
6097
6098   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, t);
6099 }
6100
6101 \f
6102 /* Push the declarations of builtin types into the namespace.
6103    RID_INDEX is the index of the builtin type
6104    in the array RID_POINTERS.  NAME is the name used when looking
6105    up the builtin type.  TYPE is the _TYPE node for the builtin type.  */
6106
6107 void
6108 record_builtin_type (enum rid rid_index, 
6109                      const char* name, 
6110                      tree type)
6111 {
6112   tree rname = NULL_TREE, tname = NULL_TREE;
6113   tree tdecl = NULL_TREE;
6114
6115   if ((int) rid_index < (int) RID_MAX)
6116     rname = ridpointers[(int) rid_index];
6117   if (name)
6118     tname = get_identifier (name);
6119
6120   if (tname)
6121     {
6122       tdecl = pushdecl (build_decl (TYPE_DECL, tname, type));
6123       set_identifier_type_value (tname, NULL_TREE);
6124       if ((int) rid_index < (int) RID_MAX)
6125         /* Built-in types live in the global namespace.  */
6126         SET_IDENTIFIER_GLOBAL_VALUE (tname, tdecl);
6127     }
6128   if (rname != NULL_TREE)
6129     {
6130       if (tname != NULL_TREE)
6131         {
6132           set_identifier_type_value (rname, NULL_TREE);
6133           SET_IDENTIFIER_GLOBAL_VALUE (rname, tdecl);
6134         }
6135       else
6136         {
6137           tdecl = pushdecl (build_decl (TYPE_DECL, rname, type));
6138           set_identifier_type_value (rname, NULL_TREE);
6139         }
6140     }
6141 }
6142
6143 /* Record one of the standard Java types.
6144  * Declare it as having the given NAME.
6145  * If SIZE > 0, it is the size of one of the integral types;
6146  * otherwise it is the negative of the size of one of the other types.  */
6147
6148 static tree
6149 record_builtin_java_type (const char* name, int size)
6150 {
6151   tree type, decl;
6152   if (size > 0)
6153     type = make_signed_type (size);
6154   else if (size > -32)
6155     { /* "__java_char" or ""__java_boolean".  */
6156       type = make_unsigned_type (-size);
6157       /*if (size == -1) TREE_SET_CODE (type, BOOLEAN_TYPE);*/
6158     }
6159   else
6160     { /* "__java_float" or ""__java_double".  */
6161       type = make_node (REAL_TYPE);
6162       TYPE_PRECISION (type) = - size;
6163       layout_type (type);
6164     }
6165   record_builtin_type (RID_MAX, name, type);
6166   decl = TYPE_NAME (type);
6167
6168   /* Suppress generate debug symbol entries for these types,
6169      since for normal C++ they are just clutter.
6170      However, push_lang_context undoes this if extern "Java" is seen.  */
6171   DECL_IGNORED_P (decl) = 1;
6172
6173   TYPE_FOR_JAVA (type) = 1;
6174   return type;
6175 }
6176
6177 /* Push a type into the namespace so that the back-ends ignore it.  */
6178
6179 static void
6180 record_unknown_type (tree type, const char* name)
6181 {
6182   tree decl = pushdecl (build_decl (TYPE_DECL, get_identifier (name), type));
6183   /* Make sure the "unknown type" typedecl gets ignored for debug info.  */
6184   DECL_IGNORED_P (decl) = 1;
6185   TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
6186   TYPE_SIZE (type) = TYPE_SIZE (void_type_node);
6187   TYPE_ALIGN (type) = 1;
6188   TYPE_USER_ALIGN (type) = 0;
6189   TYPE_MODE (type) = TYPE_MODE (void_type_node);
6190 }
6191
6192 /* An string for which we should create an IDENTIFIER_NODE at
6193    startup.  */
6194
6195 typedef struct predefined_identifier
6196 {
6197   /* The name of the identifier.  */
6198   const char *const name;
6199   /* The place where the IDENTIFIER_NODE should be stored.  */
6200   tree *const node;
6201   /* Nonzero if this is the name of a constructor or destructor.  */
6202   const int ctor_or_dtor_p;
6203 } predefined_identifier;
6204
6205 /* Create all the predefined identifiers.  */
6206
6207 static void
6208 initialize_predefined_identifiers (void)
6209 {
6210   const predefined_identifier *pid;
6211
6212   /* A table of identifiers to create at startup.  */
6213   static const predefined_identifier predefined_identifiers[] = {
6214     { "C++", &lang_name_cplusplus, 0 },
6215     { "C", &lang_name_c, 0 },
6216     { "Java", &lang_name_java, 0 },
6217     { CTOR_NAME, &ctor_identifier, 1 },
6218     { "__base_ctor", &base_ctor_identifier, 1 },
6219     { "__comp_ctor", &complete_ctor_identifier, 1 },
6220     { DTOR_NAME, &dtor_identifier, 1 },
6221     { "__comp_dtor", &complete_dtor_identifier, 1 },
6222     { "__base_dtor", &base_dtor_identifier, 1 },
6223     { "__deleting_dtor", &deleting_dtor_identifier, 1 },
6224     { IN_CHARGE_NAME, &in_charge_identifier, 0 },
6225     { "nelts", &nelts_identifier, 0 },
6226     { THIS_NAME, &this_identifier, 0 },
6227     { VTABLE_DELTA_NAME, &delta_identifier, 0 },
6228     { VTABLE_PFN_NAME, &pfn_identifier, 0 },
6229     { "_vptr", &vptr_identifier, 0 },
6230     { "__vtt_parm", &vtt_parm_identifier, 0 },
6231     { "std", &std_identifier, 0 },
6232     { NULL, NULL, 0 }
6233   };
6234
6235   for (pid = predefined_identifiers; pid->name; ++pid)
6236     {
6237       *pid->node = get_identifier (pid->name);
6238       if (pid->ctor_or_dtor_p)
6239         IDENTIFIER_CTOR_OR_DTOR_P (*pid->node) = 1;
6240     }
6241 }
6242
6243 /* Create the predefined scalar types of C,
6244    and some nodes representing standard constants (0, 1, (void *)0).
6245    Initialize the global binding level.
6246    Make definitions for built-in primitive functions.  */
6247
6248 void
6249 cxx_init_decl_processing (void)
6250 {
6251   tree void_ftype;
6252   tree void_ftype_ptr;
6253
6254   /* Create all the identifiers we need.  */
6255   initialize_predefined_identifiers ();
6256
6257   /* Fill in back-end hooks.  */
6258   lang_missing_noreturn_ok_p = &cp_missing_noreturn_ok_p;
6259
6260   /* Create the global variables.  */
6261   push_to_top_level ();
6262
6263   /* Enter the global namespace.  */
6264   my_friendly_assert (global_namespace == NULL_TREE, 375);
6265   push_namespace (get_identifier ("::"));
6266   global_namespace = current_namespace;
6267   current_lang_name = NULL_TREE;
6268
6269   /* Adjust various flags based on command-line settings.  */
6270   if (! flag_permissive && ! pedantic)
6271     flag_pedantic_errors = 1;
6272   if (!flag_no_inline)
6273     {
6274       flag_inline_trees = 1;
6275       flag_no_inline = 1;
6276     }
6277   if (flag_inline_functions)
6278     {
6279       flag_inline_trees = 2;
6280       flag_inline_functions = 0;
6281     }
6282
6283   /* Force minimum function alignment if using the least significant
6284      bit of function pointers to store the virtual bit.  */
6285   if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_pfn
6286       && force_align_functions_log < 1)
6287     force_align_functions_log = 1;
6288
6289   /* Initially, C.  */
6290   current_lang_name = lang_name_c;
6291
6292   current_function_decl = NULL_TREE;
6293   current_binding_level = NULL_BINDING_LEVEL;
6294   free_binding_level = NULL_BINDING_LEVEL;
6295
6296   build_common_tree_nodes (flag_signed_char);
6297
6298   error_mark_list = build_tree_list (error_mark_node, error_mark_node);
6299   TREE_TYPE (error_mark_list) = error_mark_node;
6300
6301   /* Make the binding_level structure for global names.  */
6302   pushlevel (0);
6303   global_binding_level = current_binding_level;
6304   /* The global level is the namespace level of ::.  */
6305   NAMESPACE_LEVEL (global_namespace) = global_binding_level;
6306   declare_namespace_level ();
6307
6308   /* Create the `std' namespace.  */
6309   push_namespace (std_identifier);
6310   std_node = current_namespace;
6311   pop_namespace ();
6312
6313   c_common_nodes_and_builtins ();
6314
6315   java_byte_type_node = record_builtin_java_type ("__java_byte", 8);
6316   java_short_type_node = record_builtin_java_type ("__java_short", 16);
6317   java_int_type_node = record_builtin_java_type ("__java_int", 32);
6318   java_long_type_node = record_builtin_java_type ("__java_long", 64);
6319   java_float_type_node = record_builtin_java_type ("__java_float", -32);
6320   java_double_type_node = record_builtin_java_type ("__java_double", -64);
6321   java_char_type_node = record_builtin_java_type ("__java_char", -16);
6322   java_boolean_type_node = record_builtin_java_type ("__java_boolean", -1);
6323
6324   integer_two_node = build_int_2 (2, 0);
6325   TREE_TYPE (integer_two_node) = integer_type_node;
6326   integer_three_node = build_int_2 (3, 0);
6327   TREE_TYPE (integer_three_node) = integer_type_node;
6328
6329   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
6330   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
6331   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
6332   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
6333   TYPE_PRECISION (boolean_type_node) = 1;
6334   record_builtin_type (RID_BOOL, "bool", boolean_type_node);
6335   boolean_false_node = build_int_2 (0, 0);
6336   TREE_TYPE (boolean_false_node) = boolean_type_node;
6337   boolean_true_node = build_int_2 (1, 0);
6338   TREE_TYPE (boolean_true_node) = boolean_type_node;
6339
6340   empty_except_spec = build_tree_list (NULL_TREE, NULL_TREE);
6341
6342 #if 0
6343   record_builtin_type (RID_MAX, NULL, string_type_node);
6344 #endif
6345
6346   delta_type_node = ptrdiff_type_node;
6347   vtable_index_type = ptrdiff_type_node;
6348
6349   vtt_parm_type = build_pointer_type (const_ptr_type_node);
6350   void_ftype = build_function_type (void_type_node, void_list_node);
6351   void_ftype_ptr = build_function_type (void_type_node,
6352                                         tree_cons (NULL_TREE,
6353                                                    ptr_type_node, 
6354                                                    void_list_node));
6355   void_ftype_ptr
6356     = build_exception_variant (void_ftype_ptr, empty_except_spec);
6357
6358   /* C++ extensions */
6359
6360   unknown_type_node = make_node (UNKNOWN_TYPE);
6361   record_unknown_type (unknown_type_node, "unknown type");
6362
6363   /* Indirecting an UNKNOWN_TYPE node yields an UNKNOWN_TYPE node.  */
6364   TREE_TYPE (unknown_type_node) = unknown_type_node;
6365
6366   /* Looking up TYPE_POINTER_TO and TYPE_REFERENCE_TO yield the same
6367      result.  */
6368   TYPE_POINTER_TO (unknown_type_node) = unknown_type_node;
6369   TYPE_REFERENCE_TO (unknown_type_node) = unknown_type_node;
6370
6371   {
6372     /* Make sure we get a unique function type, so we can give
6373        its pointer type a name.  (This wins for gdb.) */
6374     tree vfunc_type = make_node (FUNCTION_TYPE);
6375     TREE_TYPE (vfunc_type) = integer_type_node;
6376     TYPE_ARG_TYPES (vfunc_type) = NULL_TREE;
6377     layout_type (vfunc_type);
6378
6379     vtable_entry_type = build_pointer_type (vfunc_type);
6380   }
6381   record_builtin_type (RID_MAX, VTBL_PTR_TYPE, vtable_entry_type);
6382
6383   vtbl_type_node
6384     = build_cplus_array_type (vtable_entry_type, NULL_TREE);
6385   layout_type (vtbl_type_node);
6386   vtbl_type_node = build_qualified_type (vtbl_type_node, TYPE_QUAL_CONST);
6387   record_builtin_type (RID_MAX, NULL, vtbl_type_node);
6388   vtbl_ptr_type_node = build_pointer_type (vtable_entry_type);
6389   layout_type (vtbl_ptr_type_node);
6390   record_builtin_type (RID_MAX, NULL, vtbl_ptr_type_node);
6391
6392   push_namespace (get_identifier ("__cxxabiv1"));
6393   abi_node = current_namespace;
6394   pop_namespace ();
6395
6396   global_type_node = make_node (LANG_TYPE);
6397   record_unknown_type (global_type_node, "global type");
6398
6399   /* Now, C++.  */
6400   current_lang_name = lang_name_cplusplus;
6401
6402   {
6403     tree bad_alloc_type_node, newtype, deltype;
6404     tree ptr_ftype_sizetype;
6405
6406     push_namespace (std_identifier);
6407     bad_alloc_type_node 
6408       = xref_tag (class_type, get_identifier ("bad_alloc"), 
6409                   /*attributes=*/NULL_TREE, 1);
6410     pop_namespace ();
6411     ptr_ftype_sizetype 
6412       = build_function_type (ptr_type_node,
6413                              tree_cons (NULL_TREE,
6414                                         size_type_node,
6415                                         void_list_node));
6416     newtype = build_exception_variant
6417       (ptr_ftype_sizetype, add_exception_specifier
6418        (NULL_TREE, bad_alloc_type_node, -1));
6419     deltype = build_exception_variant (void_ftype_ptr, empty_except_spec);
6420     push_cp_library_fn (NEW_EXPR, newtype);
6421     push_cp_library_fn (VEC_NEW_EXPR, newtype);
6422     global_delete_fndecl = push_cp_library_fn (DELETE_EXPR, deltype);
6423     push_cp_library_fn (VEC_DELETE_EXPR, deltype);
6424   }
6425
6426   abort_fndecl
6427     = build_library_fn_ptr ("__cxa_pure_virtual", void_ftype);
6428
6429   /* Perform other language dependent initializations.  */
6430   init_class_processing ();
6431   init_search_processing ();
6432   init_rtti_processing ();
6433
6434   if (flag_exceptions)
6435     init_exception_processing ();
6436
6437   if (! supports_one_only ())
6438     flag_weak = 0;
6439
6440   make_fname_decl = cp_make_fname_decl;
6441   start_fname_decls ();
6442
6443   /* Show we use EH for cleanups.  */
6444   using_eh_for_cleanups ();
6445
6446   /* Maintain consistency.  Perhaps we should just complain if they
6447      say -fwritable-strings?  */
6448   if (flag_writable_strings)
6449     flag_const_strings = 0;
6450 }
6451
6452 /* Generate an initializer for a function naming variable from
6453    NAME. NAME may be NULL, in which case we generate a special
6454    ERROR_MARK node which should be replaced later.  */
6455
6456 tree
6457 cp_fname_init (const char* name)
6458 {
6459   tree domain = NULL_TREE;
6460   tree type;
6461   tree init = NULL_TREE;
6462   size_t length = 0;
6463
6464   if (name)
6465     {
6466       length = strlen (name);
6467       domain = build_index_type (size_int (length));
6468       init = build_string (length + 1, name);
6469     }
6470   
6471   type = build_qualified_type (char_type_node, TYPE_QUAL_CONST);
6472   type = build_cplus_array_type (type, domain);
6473
6474   if (init)
6475     TREE_TYPE (init) = type;
6476   else
6477     /* We don't know the value until instantiation time. Make
6478        something which will be digested now, but replaced later.  */
6479     init = build (ERROR_MARK, type);
6480   
6481   return init;
6482 }
6483
6484 /* Create the VAR_DECL for __FUNCTION__ etc. ID is the name to give the
6485    decl, NAME is the initialization string and TYPE_DEP indicates whether
6486    NAME depended on the type of the function. We make use of that to detect
6487    __PRETTY_FUNCTION__ inside a template fn. This is being done
6488    lazily at the point of first use, so we musn't push the decl now.  */
6489
6490 static tree
6491 cp_make_fname_decl (tree id, int type_dep)
6492 {
6493   const char *const name = (type_dep && processing_template_decl
6494                       ? NULL : fname_as_string (type_dep));
6495   tree init = cp_fname_init (name);
6496   tree decl = build_decl (VAR_DECL, id, TREE_TYPE (init));
6497
6498   /* As we're using pushdecl_with_scope, we must set the context.  */
6499   DECL_CONTEXT (decl) = current_function_decl;
6500   DECL_PRETTY_FUNCTION_P (decl) = type_dep;
6501       
6502   TREE_STATIC (decl) = 1;
6503   TREE_READONLY (decl) = 1;
6504   DECL_ARTIFICIAL (decl) = 1;
6505   DECL_INITIAL (decl) = init;
6506   
6507   TREE_USED (decl) = 1;
6508
6509   if (current_function_decl)
6510     {
6511       struct cp_binding_level *b = current_binding_level;
6512       while (b->level_chain->parm_flag == 0)
6513         b = b->level_chain;
6514       pushdecl_with_scope (decl, b);
6515     }   
6516
6517   cp_finish_decl (decl, init, NULL_TREE, LOOKUP_ONLYCONVERTING);
6518       
6519   return decl;
6520 }
6521
6522 /* Make a definition for a builtin function named NAME in the current
6523    namespace, whose data type is TYPE and whose context is CONTEXT.
6524    TYPE should be a function type with argument types.
6525
6526    CLASS and CODE tell later passes how to compile calls to this function.
6527    See tree.h for possible values.
6528
6529    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6530    the name to be called if we can't opencode the function.
6531    If ATTRS is nonzero, use that for the function's attribute
6532    list.  */
6533
6534 static tree
6535 builtin_function_1 (const char* name,
6536                     tree type,
6537                     tree context,
6538                     int code,
6539                     enum built_in_class class,
6540                     const char* libname,
6541                     tree attrs)
6542 {
6543   tree decl = build_library_fn_1 (get_identifier (name), ERROR_MARK, type);
6544   DECL_BUILT_IN_CLASS (decl) = class;
6545   DECL_FUNCTION_CODE (decl) = code;
6546   DECL_CONTEXT (decl) = context;
6547
6548   pushdecl (decl);
6549
6550   /* Since `pushdecl' relies on DECL_ASSEMBLER_NAME instead of DECL_NAME,
6551      we cannot change DECL_ASSEMBLER_NAME until we have installed this
6552      function in the namespace.  */
6553   if (libname)
6554     SET_DECL_ASSEMBLER_NAME (decl, get_identifier (libname));
6555   make_decl_rtl (decl, NULL);
6556
6557   /* Warn if a function in the namespace for users
6558      is used without an occasion to consider it declared.  */
6559   if (name[0] != '_' || name[1] != '_')
6560     DECL_ANTICIPATED (decl) = 1;
6561
6562   /* Possibly apply some default attributes to this built-in function.  */
6563   if (attrs)
6564     decl_attributes (&decl, attrs, ATTR_FLAG_BUILT_IN);
6565   else
6566     decl_attributes (&decl, NULL_TREE, 0);
6567
6568   return decl;
6569 }
6570
6571 /* Entry point for the benefit of c_common_nodes_and_builtins.
6572
6573    Make a defintion for a builtin function named NAME and whose data type
6574    is TYPE.  TYPE should be a function type with argument types.  This
6575    function places the anticipated declaration in the global namespace
6576    and additionally in the std namespace if appropriate.
6577
6578    CLASS and CODE tell later passes how to compile calls to this function.
6579    See tree.h for possible values.
6580
6581    If LIBNAME is nonzero, use that for DECL_ASSEMBLER_NAME,
6582    the name to be called if we can't opencode the function.
6583
6584    If ATTRS is nonzero, use that for the function's attribute
6585    list.  */
6586
6587 tree
6588 builtin_function (const char* name,
6589                   tree type,
6590                   int code,
6591                   enum built_in_class class,
6592                   const char* libname,
6593                   tree attrs)
6594 {
6595   /* All builtins that don't begin with an '_' should additionally
6596      go in the 'std' namespace.  */
6597   if (name[0] != '_')
6598     {
6599       push_namespace (std_identifier);
6600       builtin_function_1 (name, type, std_node, code, class, libname, attrs);
6601       pop_namespace ();
6602     }
6603
6604   return builtin_function_1 (name, type, NULL_TREE, code,
6605                              class, libname, attrs);
6606 }
6607
6608 /* Generate a FUNCTION_DECL with the typical flags for a runtime library
6609    function.  Not called directly.  */
6610
6611 static tree
6612 build_library_fn_1 (tree name, enum tree_code operator_code, tree type)
6613 {
6614   tree fn = build_lang_decl (FUNCTION_DECL, name, type);
6615   DECL_EXTERNAL (fn) = 1;
6616   TREE_PUBLIC (fn) = 1;
6617   DECL_ARTIFICIAL (fn) = 1;
6618   TREE_NOTHROW (fn) = 1;
6619   SET_OVERLOADED_OPERATOR_CODE (fn, operator_code);
6620   SET_DECL_LANGUAGE (fn, lang_c);
6621   return fn;
6622 }
6623
6624 /* Returns the _DECL for a library function with C linkage.
6625    We assume that such functions never throw; if this is incorrect,
6626    callers should unset TREE_NOTHROW.  */
6627
6628 tree
6629 build_library_fn (tree name, tree type)
6630 {
6631   return build_library_fn_1 (name, ERROR_MARK, type);
6632 }
6633
6634 /* Returns the _DECL for a library function with C++ linkage.  */
6635
6636 static tree
6637 build_cp_library_fn (tree name, enum tree_code operator_code, tree type)
6638 {
6639   tree fn = build_library_fn_1 (name, operator_code, type);
6640   TREE_NOTHROW (fn) = TYPE_NOTHROW_P (type);
6641   DECL_CONTEXT (fn) = FROB_CONTEXT (current_namespace);
6642   SET_DECL_LANGUAGE (fn, lang_cplusplus);
6643   set_mangled_name_for_decl (fn);
6644   return fn;
6645 }
6646
6647 /* Like build_library_fn, but takes a C string instead of an
6648    IDENTIFIER_NODE.  */
6649
6650 tree
6651 build_library_fn_ptr (const char* name, tree type)
6652 {
6653   return build_library_fn (get_identifier (name), type);
6654 }
6655
6656 /* Like build_cp_library_fn, but takes a C string instead of an
6657    IDENTIFIER_NODE.  */
6658
6659 tree
6660 build_cp_library_fn_ptr (const char* name, tree type)
6661 {
6662   return build_cp_library_fn (get_identifier (name), ERROR_MARK, type);
6663 }
6664
6665 /* Like build_library_fn, but also pushes the function so that we will
6666    be able to find it via IDENTIFIER_GLOBAL_VALUE.  */
6667
6668 tree
6669 push_library_fn (tree name, tree type)
6670 {
6671   tree fn = build_library_fn (name, type);
6672   pushdecl_top_level (fn);
6673   return fn;
6674 }
6675
6676 /* Like build_cp_library_fn, but also pushes the function so that it
6677    will be found by normal lookup.  */
6678
6679 static tree
6680 push_cp_library_fn (enum tree_code operator_code, tree type)
6681 {
6682   tree fn = build_cp_library_fn (ansi_opname (operator_code),
6683                                  operator_code,
6684                                  type);
6685   pushdecl (fn);
6686   return fn;
6687 }
6688
6689 /* Like push_library_fn, but takes a TREE_LIST of parm types rather than
6690    a FUNCTION_TYPE.  */
6691
6692 tree
6693 push_void_library_fn (tree name, tree parmtypes)
6694 {
6695   tree type = build_function_type (void_type_node, parmtypes);
6696   return push_library_fn (name, type);
6697 }
6698
6699 /* Like push_library_fn, but also note that this function throws
6700    and does not return.  Used for __throw_foo and the like.  */
6701
6702 tree
6703 push_throw_library_fn (tree name, tree type)
6704 {
6705   tree fn = push_library_fn (name, type);
6706   TREE_THIS_VOLATILE (fn) = 1;
6707   TREE_NOTHROW (fn) = 0;
6708   return fn;
6709 }
6710
6711 /* Apply default attributes to a function, if a system function with default
6712    attributes.  */
6713
6714 void
6715 cxx_insert_default_attributes (tree decl)
6716 {
6717   if (!DECL_EXTERN_C_FUNCTION_P (decl))
6718     return;
6719   if (!TREE_PUBLIC (decl))
6720     return;
6721   c_common_insert_default_attributes (decl);
6722 }
6723 \f
6724 /* When we call finish_struct for an anonymous union, we create
6725    default copy constructors and such.  But, an anonymous union
6726    shouldn't have such things; this function undoes the damage to the
6727    anonymous union type T.
6728
6729    (The reason that we create the synthesized methods is that we don't
6730    distinguish `union { int i; }' from `typedef union { int i; } U'.
6731    The first is an anonymous union; the second is just an ordinary
6732    union type.)  */
6733
6734 void
6735 fixup_anonymous_aggr (tree t)
6736 {
6737   tree *q;
6738
6739   /* Wipe out memory of synthesized methods */
6740   TYPE_HAS_CONSTRUCTOR (t) = 0;
6741   TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 0;
6742   TYPE_HAS_INIT_REF (t) = 0;
6743   TYPE_HAS_CONST_INIT_REF (t) = 0;
6744   TYPE_HAS_ASSIGN_REF (t) = 0;
6745   TYPE_HAS_CONST_ASSIGN_REF (t) = 0;
6746
6747   /* Splice the implicitly generated functions out of the TYPE_METHODS
6748      list.  */
6749   q = &TYPE_METHODS (t);
6750   while (*q)
6751     {
6752       if (DECL_ARTIFICIAL (*q))
6753         *q = TREE_CHAIN (*q);
6754       else
6755         q = &TREE_CHAIN (*q);
6756     }
6757
6758   /* ISO C++ 9.5.3.  Anonymous unions may not have function members.  */
6759   if (TYPE_METHODS (t))
6760     cp_error_at ("an anonymous union cannot have function members", t);
6761
6762   /* Anonymous aggregates cannot have fields with ctors, dtors or complex
6763      assignment operators (because they cannot have these methods themselves).
6764      For anonymous unions this is already checked because they are not allowed
6765      in any union, otherwise we have to check it.  */
6766   if (TREE_CODE (t) != UNION_TYPE)
6767     {
6768       tree field, type;
6769
6770       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
6771         if (TREE_CODE (field) == FIELD_DECL)
6772           {
6773             type = TREE_TYPE (field);
6774             if (CLASS_TYPE_P (type))
6775               {
6776                 if (TYPE_NEEDS_CONSTRUCTING (type))
6777                   cp_error_at ("member %#D' with constructor not allowed in anonymous aggregate",
6778                                field);
6779                 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
6780                   cp_error_at ("member %#D' with destructor not allowed in anonymous aggregate",
6781                                field);
6782                 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
6783                   cp_error_at ("member %#D' with copy assignment operator not allowed in anonymous aggregate",
6784                                field);
6785               }
6786           }
6787     }
6788 }
6789
6790 /* Make sure that a declaration with no declarator is well-formed, i.e.
6791    just declares a tagged type or anonymous union.
6792
6793    Returns the type declared; or NULL_TREE if none.  */
6794
6795 tree
6796 check_tag_decl (tree declspecs)
6797 {
6798   int found_type = 0;
6799   int saw_friend = 0;
6800   int saw_typedef = 0;
6801   tree ob_modifier = NULL_TREE;
6802   register tree link;
6803   /* If a class, struct, or enum type is declared by the DECLSPECS
6804      (i.e, if a class-specifier, enum-specifier, or non-typename
6805      elaborated-type-specifier appears in the DECLSPECS),
6806      DECLARED_TYPE is set to the corresponding type.  */
6807   tree declared_type = NULL_TREE;
6808   bool error_p = false;
6809
6810   for (link = declspecs; link; link = TREE_CHAIN (link))
6811     {
6812       tree value = TREE_VALUE (link);
6813
6814       if (TYPE_P (value)
6815           || TREE_CODE (value) == TYPE_DECL
6816           || (TREE_CODE (value) == IDENTIFIER_NODE
6817               && IDENTIFIER_GLOBAL_VALUE (value)
6818               && TREE_CODE (IDENTIFIER_GLOBAL_VALUE (value)) == TYPE_DECL))
6819         {
6820           ++found_type;
6821
6822           if (found_type == 2 && TREE_CODE (value) == IDENTIFIER_NODE)
6823             {
6824               if (! in_system_header)
6825                 pedwarn ("redeclaration of C++ built-in type `%T'", value);
6826               return NULL_TREE;
6827             }
6828
6829           if (TYPE_P (value)
6830               && ((TREE_CODE (value) != TYPENAME_TYPE && IS_AGGR_TYPE (value))
6831                   || TREE_CODE (value) == ENUMERAL_TYPE))
6832             {
6833               my_friendly_assert (TYPE_MAIN_DECL (value) != NULL_TREE, 261);
6834               declared_type = value;
6835             }
6836         }
6837       else if (value == ridpointers[(int) RID_TYPEDEF])
6838         saw_typedef = 1;
6839       else if (value == ridpointers[(int) RID_FRIEND])
6840         {
6841           if (current_class_type == NULL_TREE
6842               || current_scope () != current_class_type)
6843             ob_modifier = value;
6844           else
6845             saw_friend = 1;
6846         }
6847       else if (value == ridpointers[(int) RID_STATIC]
6848                || value == ridpointers[(int) RID_EXTERN]
6849                || value == ridpointers[(int) RID_AUTO]
6850                || value == ridpointers[(int) RID_REGISTER]
6851                || value == ridpointers[(int) RID_INLINE]
6852                || value == ridpointers[(int) RID_VIRTUAL]
6853                || value == ridpointers[(int) RID_CONST]
6854                || value == ridpointers[(int) RID_VOLATILE]
6855                || value == ridpointers[(int) RID_EXPLICIT]
6856                || value == ridpointers[(int) RID_THREAD])
6857         ob_modifier = value;
6858       else if (value == error_mark_node)
6859         error_p = true;
6860     }
6861
6862   if (found_type > 1)
6863     error ("multiple types in one declaration");
6864
6865   if (declared_type == NULL_TREE && ! saw_friend && !error_p)
6866     pedwarn ("declaration does not declare anything");
6867   /* Check for an anonymous union.  */
6868   else if (declared_type && IS_AGGR_TYPE_CODE (TREE_CODE (declared_type))
6869            && TYPE_ANONYMOUS_P (declared_type))
6870     {
6871       /* 7/3 In a simple-declaration, the optional init-declarator-list
6872          can be omitted only when declaring a class (clause 9) or
6873          enumeration (7.2), that is, when the decl-specifier-seq contains
6874          either a class-specifier, an elaborated-type-specifier with
6875          a class-key (9.1), or an enum-specifier.  In these cases and
6876          whenever a class-specifier or enum-specifier is present in the
6877          decl-specifier-seq, the identifiers in these specifiers are among
6878          the names being declared by the declaration (as class-name,
6879          enum-names, or enumerators, depending on the syntax).  In such
6880          cases, and except for the declaration of an unnamed bit-field (9.6),
6881          the decl-specifier-seq shall introduce one or more names into the
6882          program, or shall redeclare a name introduced by a previous
6883          declaration.  [Example:
6884              enum { };            // ill-formed
6885              typedef class { };   // ill-formed
6886          --end example]  */
6887       if (saw_typedef)
6888         {
6889           error ("missing type-name in typedef-declaration");
6890           return NULL_TREE;
6891         }
6892       /* Anonymous unions are objects, so they can have specifiers.  */;
6893       SET_ANON_AGGR_TYPE_P (declared_type);
6894
6895       if (TREE_CODE (declared_type) != UNION_TYPE && pedantic 
6896           && !in_system_header)
6897         pedwarn ("ISO C++ prohibits anonymous structs");
6898     }
6899
6900   else if (ob_modifier)
6901     {
6902       if (ob_modifier == ridpointers[(int) RID_INLINE]
6903           || ob_modifier == ridpointers[(int) RID_VIRTUAL])
6904         error ("`%D' can only be specified for functions", ob_modifier);
6905       else if (ob_modifier == ridpointers[(int) RID_FRIEND])
6906         error ("`%D' can only be specified inside a class", ob_modifier);
6907       else if (ob_modifier == ridpointers[(int) RID_EXPLICIT])
6908         error ("`%D' can only be specified for constructors",
6909                   ob_modifier);
6910       else
6911         error ("`%D' can only be specified for objects and functions",
6912                   ob_modifier);
6913     }
6914
6915   return declared_type;
6916 }
6917
6918 /* Called when a declaration is seen that contains no names to declare.
6919    If its type is a reference to a structure, union or enum inherited
6920    from a containing scope, shadow that tag name for the current scope
6921    with a forward reference.
6922    If its type defines a new named structure or union
6923    or defines an enum, it is valid but we need not do anything here.
6924    Otherwise, it is an error.
6925
6926    C++: may have to grok the declspecs to learn about static,
6927    complain for anonymous unions.  
6928
6929    Returns the TYPE declared -- or NULL_TREE if none.  */
6930
6931 tree
6932 shadow_tag (tree declspecs)
6933 {
6934   tree t = check_tag_decl (declspecs);
6935
6936   if (!t)
6937     return NULL_TREE;
6938
6939   maybe_process_partial_specialization (t);
6940
6941   /* This is where the variables in an anonymous union are
6942      declared.  An anonymous union declaration looks like:
6943      union { ... } ;
6944      because there is no declarator after the union, the parser
6945      sends that declaration here.  */
6946   if (ANON_AGGR_TYPE_P (t))
6947     {
6948       fixup_anonymous_aggr (t);
6949
6950       if (TYPE_FIELDS (t))
6951         {
6952           tree decl = grokdeclarator (NULL_TREE, declspecs, NORMAL, 0,
6953                                       NULL);
6954           finish_anon_union (decl);
6955         }
6956     }
6957
6958   return t;
6959 }
6960 \f
6961 /* Decode a "typename", such as "int **", returning a ..._TYPE node.  */
6962
6963 tree
6964 groktypename (tree typename)
6965 {
6966   tree specs, attrs;
6967   tree type;
6968   if (TREE_CODE (typename) != TREE_LIST)
6969     return typename;
6970   split_specs_attrs (TREE_PURPOSE (typename), &specs, &attrs);
6971   type = grokdeclarator (TREE_VALUE (typename), specs,
6972                          TYPENAME, 0, &attrs);
6973   if (attrs)
6974     cplus_decl_attributes (&type, attrs, 0);
6975   return type;
6976 }
6977
6978 /* Decode a declarator in an ordinary declaration or data definition.
6979    This is called as soon as the type information and variable name
6980    have been parsed, before parsing the initializer if any.
6981    Here we create the ..._DECL node, fill in its type,
6982    and put it on the list of decls for the current context.
6983    The ..._DECL node is returned as the value.
6984
6985    Exception: for arrays where the length is not specified,
6986    the type is left null, to be filled in by `cp_finish_decl'.
6987
6988    Function definitions do not come here; they go to start_function
6989    instead.  However, external and forward declarations of functions
6990    do go through here.  Structure field declarations are done by
6991    grokfield and not through here.  */
6992
6993 tree
6994 start_decl (tree declarator, 
6995             tree declspecs, 
6996             int initialized, 
6997             tree attributes, 
6998             tree prefix_attributes)
6999 {
7000   tree decl;
7001   register tree type, tem;
7002   tree context;
7003
7004   /* This should only be done once on the top most decl.  */
7005   if (have_extern_spec)
7006     {
7007       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"),
7008                              declspecs);
7009       have_extern_spec = false;
7010     }
7011
7012   /* An object declared as __attribute__((deprecated)) suppresses
7013      warnings of uses of other deprecated items.  */
7014   if (lookup_attribute ("deprecated", attributes))
7015     deprecated_state = DEPRECATED_SUPPRESS;
7016
7017   attributes = chainon (attributes, prefix_attributes);
7018
7019   decl = grokdeclarator (declarator, declspecs, NORMAL, initialized,
7020                          &attributes);
7021
7022   deprecated_state = DEPRECATED_NORMAL;
7023
7024   if (decl == NULL_TREE || TREE_CODE (decl) == VOID_TYPE)
7025     return NULL_TREE;
7026
7027   type = TREE_TYPE (decl);
7028
7029   if (type == error_mark_node)
7030     return NULL_TREE;
7031
7032   context = DECL_CONTEXT (decl);
7033
7034   if (initialized && context && TREE_CODE (context) == NAMESPACE_DECL
7035       && context != current_namespace && TREE_CODE (decl) == VAR_DECL)
7036     {
7037       /* When parsing the initializer, lookup should use the object's
7038          namespace.  */
7039       push_decl_namespace (context);
7040     }
7041
7042   /* We are only interested in class contexts, later.  */
7043   if (context && TREE_CODE (context) == NAMESPACE_DECL)
7044     context = NULL_TREE;
7045
7046   if (initialized)
7047     /* Is it valid for this decl to have an initializer at all?
7048        If not, set INITIALIZED to zero, which will indirectly
7049        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7050     switch (TREE_CODE (decl))
7051       {
7052       case TYPE_DECL:
7053         error ("typedef `%D' is initialized (use __typeof__ instead)", decl);
7054         initialized = 0;
7055         break;
7056
7057       case FUNCTION_DECL:
7058         error ("function `%#D' is initialized like a variable", decl);
7059         initialized = 0;
7060         break;
7061
7062       default:
7063         break;
7064       }
7065
7066   if (initialized)
7067     {
7068       if (! toplevel_bindings_p ()
7069           && DECL_EXTERNAL (decl))
7070         warning ("declaration of `%#D' has `extern' and is initialized",
7071                     decl);
7072       DECL_EXTERNAL (decl) = 0;
7073       if (toplevel_bindings_p ())
7074         TREE_STATIC (decl) = 1;
7075
7076       /* Tell `pushdecl' this is an initialized decl
7077          even though we don't yet have the initializer expression.
7078          Also tell `cp_finish_decl' it may store the real initializer.  */
7079       DECL_INITIAL (decl) = error_mark_node;
7080     }
7081
7082   /* Set attributes here so if duplicate decl, will have proper attributes.  */
7083   cplus_decl_attributes (&decl, attributes, 0);
7084
7085   /* If #pragma weak was used, mark the decl weak now.  */
7086   if (current_binding_level == global_binding_level)
7087     maybe_apply_pragma_weak (decl);
7088
7089   if (TREE_CODE (decl) == FUNCTION_DECL
7090       && DECL_DECLARED_INLINE_P (decl)
7091       && DECL_UNINLINABLE (decl)
7092       && lookup_attribute ("noinline", DECL_ATTRIBUTES (decl)))
7093     warning ("%Hinline function '%D' given attribute noinline",
7094              &DECL_SOURCE_LOCATION (decl), decl);
7095
7096   if (context && COMPLETE_TYPE_P (complete_type (context)))
7097     {
7098       push_nested_class (context);
7099
7100       if (TREE_CODE (decl) == VAR_DECL)
7101         {
7102           tree field = lookup_field (context, DECL_NAME (decl), 0, false);
7103           if (field == NULL_TREE || TREE_CODE (field) != VAR_DECL)
7104             error ("`%#D' is not a static member of `%#T'", decl, context);
7105           else
7106             {
7107               if (DECL_CONTEXT (field) != context)
7108                 {
7109                   if (!same_type_p (DECL_CONTEXT (field), context))
7110                     pedwarn ("ISO C++ does not permit `%T::%D' to be defined as `%T::%D'",
7111                              DECL_CONTEXT (field), DECL_NAME (decl),
7112                              context, DECL_NAME (decl));
7113                   DECL_CONTEXT (decl) = DECL_CONTEXT (field);
7114                 }
7115               /* Static data member are tricky; an in-class initialization
7116                  still doesn't provide a definition, so the in-class
7117                  declaration will have DECL_EXTERNAL set, but will have an
7118                  initialization.  Thus, duplicate_decls won't warn
7119                  about this situation, and so we check here.  */
7120               if (DECL_INITIAL (decl) && DECL_INITIAL (field))
7121                 error ("duplicate initialization of %D", decl);
7122               if (duplicate_decls (decl, field))
7123                 decl = field;
7124             }
7125         }
7126       else
7127         {
7128           tree field = check_classfn (context, decl);
7129           if (field && duplicate_decls (decl, field))
7130             decl = field;
7131         }
7132
7133       /* cp_finish_decl sets DECL_EXTERNAL if DECL_IN_AGGR_P is set.  */
7134       DECL_IN_AGGR_P (decl) = 0;
7135       if ((DECL_LANG_SPECIFIC (decl) && DECL_USE_TEMPLATE (decl))
7136           || CLASSTYPE_TEMPLATE_INSTANTIATION (context))
7137         {
7138           SET_DECL_TEMPLATE_SPECIALIZATION (decl);
7139           /* [temp.expl.spec] An explicit specialization of a static data
7140              member of a template is a definition if the declaration
7141              includes an initializer; otherwise, it is a declaration.
7142
7143              We check for processing_specialization so this only applies
7144              to the new specialization syntax.  */
7145           if (DECL_INITIAL (decl) == NULL_TREE && processing_specialization)
7146             DECL_EXTERNAL (decl) = 1;
7147         }
7148
7149       if (DECL_EXTERNAL (decl) && ! DECL_TEMPLATE_SPECIALIZATION (decl))
7150         pedwarn ("declaration of `%#D' outside of class is not definition",
7151                     decl);
7152     }
7153
7154   /* Enter this declaration into the symbol table.  */
7155   tem = maybe_push_decl (decl);
7156
7157   if (processing_template_decl)
7158     tem = push_template_decl (tem);
7159
7160 #if ! defined (ASM_OUTPUT_BSS) && ! defined (ASM_OUTPUT_ALIGNED_BSS)
7161   /* Tell the back-end to use or not use .common as appropriate.  If we say
7162      -fconserve-space, we want this to save .data space, at the expense of
7163      wrong semantics.  If we say -fno-conserve-space, we want this to
7164      produce errors about redefs; to do this we force variables into the
7165      data segment.  */
7166   DECL_COMMON (tem) = ((TREE_CODE (tem) != VAR_DECL
7167                         || !DECL_THREAD_LOCAL (tem))
7168                        && (flag_conserve_space || ! TREE_PUBLIC (tem)));
7169 #endif
7170
7171   if (! processing_template_decl)
7172     start_decl_1 (tem);
7173
7174   return tem;
7175 }
7176
7177 void
7178 start_decl_1 (tree decl)
7179 {
7180   tree type = TREE_TYPE (decl);
7181   int initialized = (DECL_INITIAL (decl) != NULL_TREE);
7182
7183   if (type == error_mark_node)
7184     return;
7185
7186   maybe_push_cleanup_level (type);
7187
7188   if (initialized)
7189     /* Is it valid for this decl to have an initializer at all?
7190        If not, set INITIALIZED to zero, which will indirectly
7191        tell `cp_finish_decl' to ignore the initializer once it is parsed.  */
7192     {
7193       /* Don't allow initializations for incomplete types except for
7194          arrays which might be completed by the initialization.  */
7195       if (COMPLETE_TYPE_P (complete_type (type)))
7196         ;                       /* A complete type is ok.  */
7197       else if (TREE_CODE (type) != ARRAY_TYPE)
7198         {
7199           error ("variable `%#D' has initializer but incomplete type",
7200                     decl);
7201           initialized = 0;
7202           type = TREE_TYPE (decl) = error_mark_node;
7203         }
7204       else if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7205         {
7206           if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
7207             error ("elements of array `%#D' have incomplete type", decl);
7208           /* else we already gave an error in start_decl.  */
7209           initialized = 0;
7210         }
7211     }
7212
7213   if (!initialized
7214       && TREE_CODE (decl) != TYPE_DECL
7215       && TREE_CODE (decl) != TEMPLATE_DECL
7216       && type != error_mark_node
7217       && IS_AGGR_TYPE (type)
7218       && ! DECL_EXTERNAL (decl))
7219     {
7220       if ((! processing_template_decl || ! uses_template_parms (type))
7221           && !COMPLETE_TYPE_P (complete_type (type)))
7222         {
7223           error ("aggregate `%#D' has incomplete type and cannot be defined",
7224                  decl);
7225           /* Change the type so that assemble_variable will give
7226              DECL an rtl we can live with: (mem (const_int 0)).  */
7227           type = TREE_TYPE (decl) = error_mark_node;
7228         }
7229       else
7230         {
7231           /* If any base type in the hierarchy of TYPE needs a constructor,
7232              then we set initialized to 1.  This way any nodes which are
7233              created for the purposes of initializing this aggregate
7234              will live as long as it does.  This is necessary for global
7235              aggregates which do not have their initializers processed until
7236              the end of the file.  */
7237           initialized = TYPE_NEEDS_CONSTRUCTING (type);
7238         }
7239     }
7240
7241   if (! initialized)
7242     DECL_INITIAL (decl) = NULL_TREE;
7243 }
7244
7245 /* Handle initialization of references.
7246    These three arguments are from `cp_finish_decl', and have the
7247    same meaning here that they do there.
7248
7249    Quotes on semantics can be found in ARM 8.4.3.  */
7250
7251 static tree
7252 grok_reference_init (tree decl, tree type, tree init)
7253 {
7254   tree tmp;
7255
7256   if (init == NULL_TREE)
7257     {
7258       if ((DECL_LANG_SPECIFIC (decl) == 0
7259            || DECL_IN_AGGR_P (decl) == 0)
7260           && ! DECL_THIS_EXTERN (decl))
7261         error ("`%D' declared as reference but not initialized", decl);
7262       return NULL_TREE;
7263     }
7264
7265   if (TREE_CODE (init) == CONSTRUCTOR)
7266     {
7267       error ("ISO C++ forbids use of initializer list to initialize reference `%D'", decl);
7268       return NULL_TREE;
7269     }
7270
7271   if (TREE_CODE (init) == TREE_LIST)
7272     init = build_compound_expr (init);
7273
7274   if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
7275     init = convert_from_reference (init);
7276
7277   if (TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE
7278       && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
7279     {
7280       /* Note: default conversion is only called in very special cases.  */
7281       init = default_conversion (init);
7282     }
7283
7284   /* Convert INIT to the reference type TYPE.  This may involve the
7285      creation of a temporary, whose lifetime must be the same as that
7286      of the reference.  If so, a DECL_STMT for the temporary will be
7287      added just after the DECL_STMT for DECL.  That's why we don't set
7288      DECL_INITIAL for local references (instead assigning to them
7289      explicitly); we need to allow the temporary to be initialized
7290      first.  */
7291   tmp = convert_to_reference
7292     (type, init, CONV_IMPLICIT,
7293      LOOKUP_ONLYCONVERTING|LOOKUP_SPECULATIVELY|LOOKUP_NORMAL|DIRECT_BIND,
7294      decl);
7295
7296   if (tmp == error_mark_node)
7297     return NULL_TREE;
7298   else if (tmp == NULL_TREE)
7299     {
7300       error ("cannot initialize `%T' from `%T'", type, TREE_TYPE (init));
7301       return NULL_TREE;
7302     }
7303
7304   if (TREE_STATIC (decl) && !TREE_CONSTANT (tmp))
7305     return tmp;
7306
7307   DECL_INITIAL (decl) = tmp;
7308
7309   return NULL_TREE;
7310 }
7311
7312 /* When parsing `int a[] = {1, 2};' we don't know the size of the
7313    array until we finish parsing the initializer.  If that's the
7314    situation we're in, update DECL accordingly.  */
7315
7316 static void
7317 maybe_deduce_size_from_array_init (tree decl, tree init)
7318 {
7319   tree type = TREE_TYPE (decl);
7320
7321   if (TREE_CODE (type) == ARRAY_TYPE
7322       && TYPE_DOMAIN (type) == NULL_TREE
7323       && TREE_CODE (decl) != TYPE_DECL)
7324     {
7325       /* do_default is really a C-ism to deal with tentative definitions.
7326          But let's leave it here to ease the eventual merge.  */
7327       int do_default = !DECL_EXTERNAL (decl);
7328       tree initializer = init ? init : DECL_INITIAL (decl);
7329       int failure = complete_array_type (type, initializer, do_default);
7330
7331       if (failure == 1)
7332         error ("initializer fails to determine size of `%D'", decl);
7333
7334       if (failure == 2)
7335         {
7336           if (do_default)
7337             error ("array size missing in `%D'", decl);
7338           /* If a `static' var's size isn't known, make it extern as
7339              well as static, so it does not get allocated.  If it's not
7340              `static', then don't mark it extern; finish_incomplete_decl
7341              will give it a default size and it will get allocated.  */
7342           else if (!pedantic && TREE_STATIC (decl) && !TREE_PUBLIC (decl))
7343             DECL_EXTERNAL (decl) = 1;
7344         }
7345
7346       if (pedantic && TYPE_DOMAIN (type) != NULL_TREE
7347           && tree_int_cst_lt (TYPE_MAX_VALUE (TYPE_DOMAIN (type)),
7348                               integer_zero_node))
7349         error ("zero-size array `%D'", decl);
7350
7351       layout_decl (decl, 0);
7352     }
7353 }
7354
7355 /* Set DECL_SIZE, DECL_ALIGN, etc. for DECL (a VAR_DECL), and issue
7356    any appropriate error messages regarding the layout.  */
7357
7358 static void
7359 layout_var_decl (tree decl)
7360 {
7361   tree type = TREE_TYPE (decl);
7362 #if 0
7363   tree ttype = target_type (type);
7364 #endif
7365
7366   /* If we haven't already layed out this declaration, do so now.
7367      Note that we must not call complete type for an external object
7368      because it's type might involve templates that we are not
7369      supposed to isntantiate yet.  (And it's perfectly valid to say
7370      `extern X x' for some incomplete type `X'.)  */
7371   if (!DECL_EXTERNAL (decl))
7372     complete_type (type);
7373   if (!DECL_SIZE (decl) 
7374       && TREE_TYPE (decl) != error_mark_node
7375       && (COMPLETE_TYPE_P (type)
7376           || (TREE_CODE (type) == ARRAY_TYPE 
7377               && !TYPE_DOMAIN (type)
7378               && COMPLETE_TYPE_P (TREE_TYPE (type)))))
7379     layout_decl (decl, 0);
7380
7381   if (!DECL_EXTERNAL (decl) && DECL_SIZE (decl) == NULL_TREE)
7382     {
7383       /* An automatic variable with an incomplete type: that is an error.
7384          Don't talk about array types here, since we took care of that
7385          message in grokdeclarator.  */
7386       error ("storage size of `%D' isn't known", decl);
7387       TREE_TYPE (decl) = error_mark_node;
7388     }
7389 #if 0
7390   /* Keep this code around in case we later want to control debug info
7391      based on whether a type is "used".  (jason 1999-11-11) */
7392
7393   else if (!DECL_EXTERNAL (decl) && IS_AGGR_TYPE (ttype))
7394     /* Let debugger know it should output info for this type.  */
7395     note_debug_info_needed (ttype);
7396
7397   if (TREE_STATIC (decl) && DECL_CLASS_SCOPE_P (decl))
7398     note_debug_info_needed (DECL_CONTEXT (decl));
7399 #endif
7400
7401   if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
7402       && DECL_SIZE (decl) != NULL_TREE
7403       && ! TREE_CONSTANT (DECL_SIZE (decl)))
7404     {
7405       if (TREE_CODE (DECL_SIZE (decl)) == INTEGER_CST)
7406         constant_expression_warning (DECL_SIZE (decl));
7407       else
7408         error ("storage size of `%D' isn't constant", decl);
7409     }
7410
7411   if (TREE_STATIC (decl)
7412       && !DECL_ARTIFICIAL (decl)
7413       && current_function_decl
7414       && DECL_CONTEXT (decl) == current_function_decl)
7415     push_local_name (decl);
7416 }
7417
7418 /* If a local static variable is declared in an inline function, or if
7419    we have a weak definition, we must endeavor to create only one
7420    instance of the variable at link-time.  */
7421
7422 static void
7423 maybe_commonize_var (tree decl)
7424 {
7425   /* Static data in a function with comdat linkage also has comdat
7426      linkage.  */
7427   if (TREE_STATIC (decl)
7428       /* Don't mess with __FUNCTION__.  */
7429       && ! DECL_ARTIFICIAL (decl)
7430       && current_function_decl
7431       && DECL_CONTEXT (decl) == current_function_decl
7432       && (DECL_DECLARED_INLINE_P (current_function_decl)
7433           || DECL_TEMPLATE_INSTANTIATION (current_function_decl))
7434       && TREE_PUBLIC (current_function_decl))
7435     {
7436       /* If flag_weak, we don't need to mess with this, as we can just
7437          make the function weak, and let it refer to its unique local
7438          copy.  This works because we don't allow the function to be
7439          inlined.  */
7440       if (! flag_weak)
7441         {
7442           if (DECL_INTERFACE_KNOWN (current_function_decl))
7443             {
7444               TREE_PUBLIC (decl) = 1;
7445               DECL_EXTERNAL (decl) = DECL_EXTERNAL (current_function_decl);
7446             }
7447           else if (DECL_INITIAL (decl) == NULL_TREE
7448                    || DECL_INITIAL (decl) == error_mark_node)
7449             {
7450               TREE_PUBLIC (decl) = 1;
7451               DECL_COMMON (decl) = 1;
7452             }
7453           /* else we lose. We can only do this if we can use common,
7454              which we can't if it has been initialized.  */
7455
7456           if (!TREE_PUBLIC (decl))
7457             {
7458               cp_warning_at ("sorry: semantics of inline function static data `%#D' are wrong (you'll wind up with multiple copies)", decl);
7459               cp_warning_at ("  you can work around this by removing the initializer", decl);
7460             }
7461         }
7462       else
7463         comdat_linkage (decl);
7464     }
7465   else if (DECL_LANG_SPECIFIC (decl) && DECL_COMDAT (decl))
7466     /* Set it up again; we might have set DECL_INITIAL since the last
7467        time.  */
7468     comdat_linkage (decl);
7469 }
7470
7471 /* Issue an error message if DECL is an uninitialized const variable.  */
7472
7473 static void
7474 check_for_uninitialized_const_var (tree decl)
7475 {
7476   tree type = TREE_TYPE (decl);
7477
7478   /* ``Unless explicitly declared extern, a const object does not have
7479      external linkage and must be initialized. ($8.4; $12.1)'' ARM
7480      7.1.6 */
7481   if (TREE_CODE (decl) == VAR_DECL
7482       && TREE_CODE (type) != REFERENCE_TYPE
7483       && CP_TYPE_CONST_P (type)
7484       && !TYPE_NEEDS_CONSTRUCTING (type)
7485       && !DECL_INITIAL (decl))
7486     error ("uninitialized const `%D'", decl);
7487 }
7488
7489 /* FIELD is a FIELD_DECL or NULL.  In the former case, the value
7490    returned is the next FIELD_DECL (possibly FIELD itself) that can be
7491    initialized.  If there are no more such fields, the return value
7492    will be NULL.  */
7493
7494 static tree
7495 next_initializable_field (tree field)
7496 {
7497   while (field
7498          && (TREE_CODE (field) != FIELD_DECL
7499              || (DECL_C_BIT_FIELD (field) && !DECL_NAME (field))
7500              || DECL_ARTIFICIAL (field)))
7501     field = TREE_CHAIN (field);
7502
7503   return field;
7504 }
7505
7506 /* Undo the brace-elision allowed by [dcl.init.aggr] in a
7507    brace-enclosed aggregate initializer.
7508
7509    *INITP is one of a list of initializers describing a brace-enclosed
7510    initializer for an entity of the indicated aggregate TYPE.  It may
7511    not presently match the shape of the TYPE; for example:
7512    
7513      struct S { int a; int b; };
7514      struct S a[] = { 1, 2, 3, 4 };
7515
7516    Here *INITP will point to TREE_LIST of four elements, rather than a
7517    list of two elements, each itself a list of two elements.  This
7518    routine transforms INIT from the former form into the latter.  The
7519    revised initializer is returned.  */
7520
7521 static tree
7522 reshape_init (tree type, tree *initp)
7523 {
7524   tree inits;
7525   tree old_init;
7526   tree old_init_value;
7527   tree new_init;
7528   bool brace_enclosed_p;
7529
7530   old_init = *initp;
7531   old_init_value = (TREE_CODE (*initp) == TREE_LIST
7532                     ? TREE_VALUE (*initp) : old_init);
7533
7534   /* For some parse errors, OLD_INIT_VALUE may be NULL.  */
7535   if (!old_init_value)
7536     {
7537       my_friendly_assert (TREE_CODE (old_init) == TREE_LIST, 20021202);
7538       TREE_VALUE (old_init) = error_mark_node;
7539       return old_init;
7540     }
7541
7542   /* If the initializer is brace-enclosed, pull initializers from the
7543      enclosed elements.  Advance past the brace-enclosed initializer
7544      now.  */
7545   if (TREE_CODE (old_init_value) == CONSTRUCTOR 
7546       && TREE_HAS_CONSTRUCTOR (old_init_value))
7547     {
7548       *initp = TREE_CHAIN (old_init);
7549       TREE_CHAIN (old_init) = NULL_TREE;
7550       inits = CONSTRUCTOR_ELTS (old_init_value);
7551       initp = &inits;
7552       brace_enclosed_p = true;
7553     }
7554   else
7555     {
7556       inits = NULL_TREE;
7557       brace_enclosed_p = false;
7558     }
7559
7560   /* A non-aggregate type is always initialized with a single
7561      initializer.  */
7562   if (!CP_AGGREGATE_TYPE_P (type))
7563       {
7564         *initp = TREE_CHAIN (old_init);
7565         TREE_CHAIN (old_init) = NULL_TREE;
7566         /* It is invalid to initialize a non-aggregate type with a
7567            brace-enclosed initializer.  */
7568         if (brace_enclosed_p)
7569           {
7570             error ("brace-enclosed initializer used to initialize `%T'",
7571                    type);
7572             if (TREE_CODE (old_init) == TREE_LIST)
7573               TREE_VALUE (old_init) = error_mark_node;
7574             else
7575               old_init = error_mark_node;
7576           }
7577         
7578         return old_init;
7579       }
7580
7581   /* [dcl.init.aggr]
7582
7583      All implicit type conversions (clause _conv_) are considered when
7584      initializing the aggregate member with an initializer from an
7585      initializer-list.  If the initializer can initialize a member,
7586      the member is initialized.  Otherwise, if the member is itself a
7587      non-empty subaggregate, brace elision is assumed and the
7588      initializer is considered for the initialization of the first
7589      member of the subaggregate.  */
7590   if (CLASS_TYPE_P (type) 
7591       && !brace_enclosed_p
7592       && can_convert_arg (type, TREE_TYPE (old_init_value), old_init_value))
7593     {
7594       *initp = TREE_CHAIN (old_init);
7595       TREE_CHAIN (old_init) = NULL_TREE;
7596       return old_init;
7597     }
7598
7599   if (TREE_CODE (old_init_value) == STRING_CST
7600       && TREE_CODE (type) == ARRAY_TYPE
7601       && char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type))))
7602     {
7603       /* [dcl.init.string]
7604
7605          A char array (whether plain char, signed char, or unsigned char)
7606          can be initialized by a string-literal (optionally enclosed in
7607          braces); a wchar_t array can be initialized by a wide
7608          string-literal (optionally enclosed in braces).  */
7609       new_init = old_init;
7610       /* Move past the initializer.  */
7611       *initp = TREE_CHAIN (old_init);
7612       TREE_CHAIN (old_init) = NULL_TREE;
7613     }
7614   else
7615     {
7616       /* Build a CONSTRUCTOR to hold the contents of the aggregate.  */  
7617       new_init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
7618       TREE_HAS_CONSTRUCTOR (new_init) = 1;
7619
7620       if (CLASS_TYPE_P (type))
7621         {
7622           tree field;
7623
7624           field = next_initializable_field (TYPE_FIELDS (type));
7625
7626           if (!field)
7627             {
7628               /* [dcl.init.aggr]
7629               
7630                  An initializer for an aggregate member that is an
7631                  empty class shall have the form of an empty
7632                  initializer-list {}.  */
7633               if (!brace_enclosed_p)
7634                 error ("initializer for `%T' must be brace-enclosed",
7635                        type);
7636             }
7637           else
7638             {
7639               /* Loop through the initializable fields, gathering
7640                  initializers.  */
7641               /* FIXME support non-trivial labeled initializers.  */
7642               while (*initp && field)
7643                 {
7644                   tree field_init;
7645
7646                   field_init = reshape_init (TREE_TYPE (field), initp);
7647                   TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
7648                   CONSTRUCTOR_ELTS (new_init) = field_init;
7649                   /* [dcl.init.aggr] 
7650
7651                      When a union  is  initialized with a brace-enclosed
7652                      initializer, the braces shall only contain an
7653                      initializer for the first member of the union.  */
7654                   if (TREE_CODE (type) == UNION_TYPE)
7655                     break;
7656                   field = next_initializable_field (TREE_CHAIN (field));
7657                 }
7658             }
7659         }
7660       else if (TREE_CODE (type) == ARRAY_TYPE)
7661         {
7662           tree index;
7663           tree max_index;
7664
7665           /* If the bound of the array is known, take no more initializers
7666              than are allowed.  */
7667           max_index = (TYPE_DOMAIN (type) 
7668                        ? array_type_nelts (type) : NULL_TREE);
7669           /* Loop through the array elements, gathering initializers.  */
7670           for (index = size_zero_node;
7671                *initp && (!max_index || !tree_int_cst_lt (max_index, index));
7672                index = size_binop (PLUS_EXPR, index, size_one_node))
7673             {
7674               tree element_init;
7675
7676               element_init = reshape_init (TREE_TYPE (type), initp);
7677               TREE_CHAIN (element_init) = CONSTRUCTOR_ELTS (new_init);
7678               CONSTRUCTOR_ELTS (new_init) = element_init;
7679               if (TREE_PURPOSE (element_init))
7680                 index = TREE_PURPOSE (element_init);
7681             }
7682         }
7683       else
7684         abort ();
7685
7686       /* The initializers were placed in reverse order in the
7687          CONSTRUCTOR.  */
7688       CONSTRUCTOR_ELTS (new_init) = nreverse (CONSTRUCTOR_ELTS (new_init));
7689
7690       if (TREE_CODE (old_init) == TREE_LIST)
7691         new_init = build_tree_list (TREE_PURPOSE (old_init), new_init);
7692     }
7693
7694   /* If this was a brace-enclosed initializer and all of the
7695      initializers were not used up, there is a problem.  */
7696   if (brace_enclosed_p && *initp)
7697     error ("too many initializers for `%T'", type);
7698
7699   return new_init;
7700 }
7701
7702 /* Verify INIT (the initializer for DECL), and record the
7703    initialization in DECL_INITIAL, if appropriate.  
7704
7705    If the return value is non-NULL, it is an expression that must be
7706    evaluated dynamically to initialize DECL.  */
7707
7708 static tree
7709 check_initializer (tree decl, tree init, int flags)
7710 {
7711   tree type = TREE_TYPE (decl);
7712
7713   /* If `start_decl' didn't like having an initialization, ignore it now.  */
7714   if (init != NULL_TREE && DECL_INITIAL (decl) == NULL_TREE)
7715     init = NULL_TREE;
7716
7717   /* If an initializer is present, DECL_INITIAL has been
7718      error_mark_node, to indicate that an as-of-yet unevaluated
7719      initialization will occur.  From now on, DECL_INITIAL reflects
7720      the static initialization -- if any -- of DECL.  */
7721   DECL_INITIAL (decl) = NULL_TREE;
7722
7723   /* Things that are going to be initialized need to have complete
7724      type.  */
7725   TREE_TYPE (decl) = type = complete_type (TREE_TYPE (decl));
7726
7727   if (type == error_mark_node)
7728     /* We will have already complained.  */
7729     init = NULL_TREE;
7730   else if (init && COMPLETE_TYPE_P (type) 
7731            && !TREE_CONSTANT (TYPE_SIZE (type)))
7732     {
7733       error ("variable-sized object `%D' may not be initialized", decl);
7734       init = NULL_TREE;
7735     }
7736   else if (TREE_CODE (type) == ARRAY_TYPE
7737            && !COMPLETE_TYPE_P (complete_type (TREE_TYPE (type))))
7738     {
7739       error ("elements of array `%#D' have incomplete type", decl);
7740       init = NULL_TREE;
7741     }
7742   else if (TREE_CODE (type) != ARRAY_TYPE && !COMPLETE_TYPE_P (type))
7743     {
7744       error ("`%D' has incomplete type", decl);
7745       TREE_TYPE (decl) = error_mark_node;
7746       init = NULL_TREE;
7747     }
7748
7749   if (TREE_CODE (decl) == CONST_DECL)
7750     {
7751       my_friendly_assert (TREE_CODE (decl) != REFERENCE_TYPE, 148);
7752
7753       DECL_INITIAL (decl) = init;
7754
7755       my_friendly_assert (init != NULL_TREE, 149);
7756       init = NULL_TREE;
7757     }
7758   else if (!DECL_EXTERNAL (decl) && TREE_CODE (type) == REFERENCE_TYPE)
7759     init = grok_reference_init (decl, type, init);
7760   else if (init)
7761     {
7762       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7763         {
7764           /* [dcl.init] paragraph 13,
7765              If T is a scalar type, then a declaration of the form
7766              T x = { a };
7767              is equivalent to
7768              T x = a;
7769              
7770              reshape_init will complain about the extra braces,
7771              and doesn't do anything useful in the case where TYPE is
7772              scalar, so just don't call it.  */
7773           if (CP_AGGREGATE_TYPE_P (type))
7774             init = reshape_init (type, &init);
7775
7776           if ((*targetm.vector_opaque_p) (type))
7777             {
7778               error ("opaque vector types cannot be initialized");
7779               init = error_mark_node;
7780             }
7781         }
7782
7783       /* If DECL has an array type without a specific bound, deduce the
7784          array size from the initializer.  */
7785       maybe_deduce_size_from_array_init (decl, init);
7786       type = TREE_TYPE (decl);
7787       if (TREE_CODE (init) == CONSTRUCTOR && TREE_HAS_CONSTRUCTOR (init))
7788         TREE_TYPE (init) = type;
7789
7790       if (TYPE_HAS_CONSTRUCTOR (type) || TYPE_NEEDS_CONSTRUCTING (type))
7791         {
7792           if (TREE_CODE (type) == ARRAY_TYPE)
7793             goto initialize_aggr;
7794           else if (TREE_CODE (init) == CONSTRUCTOR
7795                    && TREE_HAS_CONSTRUCTOR (init))
7796             {
7797               if (TYPE_NON_AGGREGATE_CLASS (type))
7798                 {
7799                   error ("`%D' must be initialized by constructor, not by `{...}'",
7800                          decl);
7801                   init = error_mark_node;
7802                 }
7803               else
7804                 goto dont_use_constructor;
7805             }
7806           else
7807             {
7808               int saved_stmts_are_full_exprs_p;
7809
7810             initialize_aggr:
7811               saved_stmts_are_full_exprs_p = 0;
7812               if (building_stmt_tree ())
7813                 {
7814                   saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
7815                   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
7816                 }
7817               init = build_aggr_init (decl, init, flags);
7818               if (building_stmt_tree ())
7819                 current_stmt_tree ()->stmts_are_full_exprs_p =
7820                   saved_stmts_are_full_exprs_p;
7821               return init;
7822             }
7823         }
7824       else
7825         {
7826         dont_use_constructor:
7827           if (TREE_CODE (init) != TREE_VEC)
7828             init = store_init_value (decl, init);
7829         }
7830     }
7831   else if (DECL_EXTERNAL (decl))
7832     ;
7833   else if (TYPE_P (type) && TYPE_NEEDS_CONSTRUCTING (type))
7834     goto initialize_aggr;
7835   else if (IS_AGGR_TYPE (type))
7836     {
7837       tree core_type = strip_array_types (type);
7838
7839       if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (core_type))
7840         error ("structure `%D' with uninitialized const members", decl);
7841       if (CLASSTYPE_REF_FIELDS_NEED_INIT (core_type))
7842         error ("structure `%D' with uninitialized reference members",
7843                decl);
7844
7845       check_for_uninitialized_const_var (decl);
7846     }
7847   else
7848     check_for_uninitialized_const_var (decl);
7849
7850   if (init && init != error_mark_node)
7851     init = build (INIT_EXPR, type, decl, init);
7852
7853   return init;
7854 }
7855
7856 /* If DECL is not a local variable, give it RTL.  */
7857
7858 static void
7859 make_rtl_for_nonlocal_decl (tree decl, tree init, const char* asmspec)
7860 {
7861   int toplev = toplevel_bindings_p ();
7862   int defer_p;
7863
7864   /* Handle non-variables up front.  */
7865   if (TREE_CODE (decl) != VAR_DECL)
7866     {
7867       rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7868       return;
7869     }
7870
7871   /* If we see a class member here, it should be a static data
7872      member.  */
7873   if (DECL_LANG_SPECIFIC (decl) && DECL_IN_AGGR_P (decl))
7874     {
7875       my_friendly_assert (TREE_STATIC (decl), 19990828);
7876       /* An in-class declaration of a static data member should be
7877          external; it is only a declaration, and not a definition.  */
7878       if (init == NULL_TREE)
7879         my_friendly_assert (DECL_EXTERNAL (decl), 20000723);
7880     }
7881
7882   /* Set the DECL_ASSEMBLER_NAME for the variable.  */
7883   if (asmspec)
7884     {
7885       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
7886       /* The `register' keyword, when used together with an
7887          asm-specification, indicates that the variable should be
7888          placed in a particular register.  */
7889       if (DECL_REGISTER (decl))
7890         DECL_C_HARD_REGISTER (decl) = 1;
7891     }
7892
7893   /* We don't create any RTL for local variables.  */
7894   if (DECL_FUNCTION_SCOPE_P (decl) && !TREE_STATIC (decl))
7895     return;
7896
7897   /* We defer emission of local statics until the corresponding
7898      DECL_STMT is expanded.  */
7899   defer_p = DECL_FUNCTION_SCOPE_P (decl) || DECL_VIRTUAL_P (decl);
7900
7901   /* We try to defer namespace-scope static constants so that they are
7902      not emitted into the object file unnecessarily.  */
7903   if (!DECL_VIRTUAL_P (decl)
7904       && TREE_READONLY (decl)
7905       && DECL_INITIAL (decl) != NULL_TREE
7906       && DECL_INITIAL (decl) != error_mark_node
7907       && ! EMPTY_CONSTRUCTOR_P (DECL_INITIAL (decl))
7908       && toplev
7909       && !TREE_PUBLIC (decl))
7910     {
7911       /* Fool with the linkage of static consts according to #pragma
7912          interface.  */
7913       if (!interface_unknown && !TREE_PUBLIC (decl))
7914         {
7915           TREE_PUBLIC (decl) = 1;
7916           DECL_EXTERNAL (decl) = interface_only;
7917         }
7918
7919       defer_p = 1;
7920     }
7921   /* Likewise for template instantiations.  */
7922   else if (DECL_COMDAT (decl))
7923     defer_p = 1;
7924
7925   /* If we're deferring the variable, we only need to make RTL if
7926      there's an ASMSPEC.  Otherwise, we'll lazily create it later when
7927      we need it.  (There's no way to lazily create RTL for things that
7928      have assembly specs because the information about the specifier
7929      isn't stored in the tree, yet)  */
7930   if (defer_p && asmspec)
7931     make_decl_rtl (decl, asmspec);
7932   /* If we're not deferring, go ahead and assemble the variable.  */
7933   else if (!defer_p)
7934     rest_of_decl_compilation (decl, asmspec, toplev, at_eof);
7935 }
7936
7937 /* The old ARM scoping rules injected variables declared in the
7938    initialization statement of a for-statement into the surrounding
7939    scope.  We support this usage, in order to be backward-compatible.
7940    DECL is a just-declared VAR_DECL; if necessary inject its
7941    declaration into the surrounding scope.  */
7942
7943 void
7944 maybe_inject_for_scope_var (tree decl)
7945 {
7946   timevar_push (TV_NAME_LOOKUP);
7947   if (!DECL_NAME (decl))
7948     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
7949   
7950   /* Declarations of __FUNCTION__ and its ilk appear magically when
7951      the variable is first used.  If that happens to be inside a
7952      for-loop, we don't want to do anything special.  */
7953   if (DECL_PRETTY_FUNCTION_P (decl))
7954     POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, (void)0);
7955
7956   if (current_binding_level->is_for_scope)
7957     {
7958       struct cp_binding_level *outer
7959         = current_binding_level->level_chain;
7960
7961       /* Check to see if the same name is already bound at the outer
7962          level, either because it was directly declared, or because a
7963          dead for-decl got preserved.  In either case, the code would
7964          not have been valid under the ARM scope rules, so clear
7965          is_for_scope for the current_binding_level.
7966
7967          Otherwise, we need to preserve the temp slot for decl to last
7968          into the outer binding level.  */
7969
7970       tree outer_binding
7971         = TREE_CHAIN (IDENTIFIER_BINDING (DECL_NAME (decl)));
7972
7973       if (outer_binding && BINDING_LEVEL (outer_binding) == outer
7974           && (TREE_CODE (BINDING_VALUE (outer_binding))
7975               == VAR_DECL)
7976           && DECL_DEAD_FOR_LOCAL (BINDING_VALUE (outer_binding)))
7977         {
7978           BINDING_VALUE (outer_binding)
7979             = DECL_SHADOWED_FOR_VAR (BINDING_VALUE (outer_binding));
7980           current_binding_level->is_for_scope = 0;
7981         }
7982     }
7983   timevar_pop (TV_NAME_LOOKUP);
7984 }
7985
7986 /* Generate code to initialize DECL (a local variable).  */
7987
7988 static void
7989 initialize_local_var (tree decl, tree init)
7990 {
7991   tree type = TREE_TYPE (decl);
7992
7993   my_friendly_assert (TREE_CODE (decl) == VAR_DECL
7994                       || TREE_CODE (decl) == RESULT_DECL, 
7995                       20021010);
7996   my_friendly_assert (!TREE_STATIC (decl), 20021010);
7997
7998   if (DECL_SIZE (decl) == NULL_TREE)
7999     {
8000       /* If we used it already as memory, it must stay in memory.  */
8001       DECL_INITIAL (decl) = NULL_TREE;
8002       TREE_ADDRESSABLE (decl) = TREE_USED (decl);
8003     }
8004
8005   if (DECL_SIZE (decl) && type != error_mark_node)
8006     {
8007       int already_used;
8008
8009       /* Compute and store the initial value.  */
8010       already_used = TREE_USED (decl) || TREE_USED (type);
8011
8012       /* Perform the initialization.  */
8013       if (init)
8014         {
8015           int saved_stmts_are_full_exprs_p;
8016
8017           my_friendly_assert (building_stmt_tree (), 20000906);
8018           saved_stmts_are_full_exprs_p = stmts_are_full_exprs_p ();
8019           current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8020           finish_expr_stmt (init);
8021           current_stmt_tree ()->stmts_are_full_exprs_p =
8022             saved_stmts_are_full_exprs_p;
8023         }
8024
8025       /* Set this to 0 so we can tell whether an aggregate which was
8026          initialized was ever used.  Don't do this if it has a
8027          destructor, so we don't complain about the 'resource
8028          allocation is initialization' idiom.  Now set
8029          attribute((unused)) on types so decls of that type will be
8030          marked used. (see TREE_USED, above.)  */
8031       if (TYPE_NEEDS_CONSTRUCTING (type)
8032           && ! already_used
8033           && TYPE_HAS_TRIVIAL_DESTRUCTOR (type)
8034           && DECL_NAME (decl))
8035         TREE_USED (decl) = 0;
8036       else if (already_used)
8037         TREE_USED (decl) = 1;
8038     }
8039
8040   /* Generate a cleanup, if necessary.  */
8041   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
8042     {
8043       tree cleanup;
8044
8045       /* Compute the cleanup.  */
8046       cleanup = cxx_maybe_build_cleanup (decl);
8047       
8048       /* Record the cleanup required for this declaration.  */
8049       if (DECL_SIZE (decl) && cleanup)
8050         finish_decl_cleanup (decl, cleanup);
8051     }
8052 }
8053
8054 /* Finish processing of a declaration;
8055    install its line number and initial value.
8056    If the length of an array type is not known before,
8057    it must be determined now, from the initial value, or it is an error.
8058
8059    INIT holds the value of an initializer that should be allowed to escape
8060    the normal rules.
8061
8062    FLAGS is LOOKUP_ONLYCONVERTING if the = init syntax was used, else 0
8063    if the (init) syntax was used.  */
8064
8065 void
8066 cp_finish_decl (tree decl, tree init, tree asmspec_tree, int flags)
8067 {
8068   register tree type;
8069   tree ttype = NULL_TREE;
8070   const char *asmspec = NULL;
8071   int was_readonly = 0;
8072
8073   if (! decl)
8074     {
8075       if (init)
8076         error ("assignment (not initialization) in declaration");
8077       return;
8078     }
8079
8080   /* If a name was specified, get the string.  */
8081   if (current_binding_level == global_binding_level)
8082     asmspec_tree = maybe_apply_renaming_pragma (decl, asmspec_tree);
8083   if (asmspec_tree)
8084     asmspec = TREE_STRING_POINTER (asmspec_tree);
8085
8086   if (init && TREE_CODE (init) == NAMESPACE_DECL)
8087     {
8088       error ("cannot initialize `%D' to namespace `%D'",
8089                 decl, init);
8090       init = NULL_TREE;
8091     }
8092
8093   if (current_class_type
8094       && CP_DECL_CONTEXT (decl) == current_class_type
8095       && TYPE_BEING_DEFINED (current_class_type)
8096       && (DECL_INITIAL (decl) || init))
8097     DECL_INITIALIZED_IN_CLASS_P (decl) = 1;
8098
8099   if (TREE_CODE (decl) == VAR_DECL
8100       && DECL_CONTEXT (decl)
8101       && TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL
8102       && DECL_CONTEXT (decl) != current_namespace
8103       && init)
8104     {
8105       /* Leave the namespace of the object.  */
8106       pop_decl_namespace ();
8107     }
8108
8109   type = TREE_TYPE (decl);
8110
8111   if (type == error_mark_node)
8112     return;
8113
8114   if (TYPE_HAS_MUTABLE_P (type))
8115     TREE_READONLY (decl) = 0;
8116
8117   if (processing_template_decl)
8118     {
8119       /* Add this declaration to the statement-tree.  */
8120       if (at_function_scope_p ()
8121           && TREE_CODE (decl) != RESULT_DECL)
8122         add_decl_stmt (decl);
8123
8124       if (init && DECL_INITIAL (decl))
8125         DECL_INITIAL (decl) = init;
8126       goto finish_end0;
8127     }
8128
8129   /* Parameters are handled by store_parm_decls, not cp_finish_decl.  */
8130   my_friendly_assert (TREE_CODE (decl) != PARM_DECL, 19990828);
8131
8132   /* Take care of TYPE_DECLs up front.  */
8133   if (TREE_CODE (decl) == TYPE_DECL)
8134     {
8135       if (type != error_mark_node
8136           && IS_AGGR_TYPE (type) && DECL_NAME (decl))
8137         {
8138           if (TREE_TYPE (DECL_NAME (decl)) && TREE_TYPE (decl) != type)
8139             warning ("shadowing previous type declaration of `%#D'", decl);
8140           set_identifier_type_value (DECL_NAME (decl), type);
8141           CLASSTYPE_GOT_SEMICOLON (type) = 1;
8142         }
8143
8144       /* If we have installed this as the canonical typedef for this
8145          type, and that type has not been defined yet, delay emitting
8146          the debug information for it, as we will emit it later.  */
8147       if (TYPE_MAIN_DECL (TREE_TYPE (decl)) == decl
8148           && !COMPLETE_TYPE_P (TREE_TYPE (decl)))
8149         TYPE_DECL_SUPPRESS_DEBUG (decl) = 1;
8150
8151       rest_of_decl_compilation (decl, NULL,
8152                                 DECL_CONTEXT (decl) == NULL_TREE, at_eof);
8153       goto finish_end;
8154     }
8155
8156   if (TREE_CODE (decl) != FUNCTION_DECL)
8157     ttype = target_type (type);
8158
8159   if (! DECL_EXTERNAL (decl) && TREE_READONLY (decl)
8160       && TYPE_NEEDS_CONSTRUCTING (type))
8161     {
8162       /* Currently, GNU C++ puts constants in text space, making them
8163          impossible to initialize.  In the future, one would hope for
8164          an operating system which understood the difference between
8165          initialization and the running of a program.  */
8166       was_readonly = 1;
8167       TREE_READONLY (decl) = 0;
8168     }
8169
8170   if (TREE_CODE (decl) == FIELD_DECL && asmspec)
8171     {
8172       /* This must override the asm specifier which was placed by
8173          grokclassfn.  Lay this out fresh.  */
8174       SET_DECL_RTL (TREE_TYPE (decl), NULL_RTX);
8175       SET_DECL_ASSEMBLER_NAME (decl, get_identifier (asmspec));
8176       make_decl_rtl (decl, asmspec);
8177     }
8178   else if (TREE_CODE (decl) == RESULT_DECL)
8179     init = check_initializer (decl, init, flags);
8180   else if (TREE_CODE (decl) == VAR_DECL)
8181     {
8182       /* Only PODs can have thread-local storage.  Other types may require
8183          various kinds of non-trivial initialization.  */
8184       if (DECL_THREAD_LOCAL (decl) && !pod_type_p (TREE_TYPE (decl)))
8185         error ("`%D' cannot be thread-local because it has non-POD type `%T'",
8186                decl, TREE_TYPE (decl));
8187       /* Convert the initializer to the type of DECL, if we have not
8188          already initialized DECL.  */
8189       if (!DECL_INITIALIZED_P (decl)
8190           /* If !DECL_EXTERNAL then DECL is being defined.  In the
8191              case of a static data member initialized inside the
8192              class-specifier, there can be an initializer even if DECL
8193              is *not* defined.  */
8194           && (!DECL_EXTERNAL (decl) || init))
8195         {
8196           init = check_initializer (decl, init, flags);
8197           /* Thread-local storage cannot be dynamically initialized.  */
8198           if (DECL_THREAD_LOCAL (decl) && init)
8199             {
8200               error ("`%D' is thread-local and so cannot be dynamically "
8201                      "initialized", decl);
8202               init = NULL_TREE;
8203             }
8204           /* Handle:
8205              
8206              [dcl.init]
8207              
8208              The memory occupied by any object of static storage
8209              duration is zero-initialized at program startup before
8210              any other initialization takes place.
8211              
8212              We cannot create an appropriate initializer until after
8213              the type of DECL is finalized.  If DECL_INITIAL is set,
8214              then the DECL is statically initialized, and any
8215              necessary zero-initialization has already been performed.  */
8216           if (TREE_STATIC (decl) && !DECL_INITIAL (decl))
8217             DECL_INITIAL (decl) = build_zero_init (TREE_TYPE (decl),
8218                                                    /*nelts=*/NULL_TREE,
8219                                                    /*static_storage_p=*/true);
8220           /* Remember that the initialization for this variable has
8221              taken place.  */
8222           DECL_INITIALIZED_P (decl) = 1;
8223         }
8224       /* If the variable has an array type, lay out the type, even if
8225          there is no initializer.  It is valid to index through the
8226          array, and we must get TYPE_ALIGN set correctly on the array
8227          type.  */
8228       else if (TREE_CODE (type) == ARRAY_TYPE)
8229         layout_type (type);
8230     }
8231
8232   /* Add this declaration to the statement-tree.  This needs to happen
8233      after the call to check_initializer so that the DECL_STMT for a
8234      reference temp is added before the DECL_STMT for the reference itself.  */
8235   if (building_stmt_tree ()
8236       && at_function_scope_p ()
8237       && TREE_CODE (decl) != RESULT_DECL)
8238     add_decl_stmt (decl);
8239
8240   if (TREE_CODE (decl) == VAR_DECL)
8241     layout_var_decl (decl);
8242
8243   /* Output the assembler code and/or RTL code for variables and functions,
8244      unless the type is an undefined structure or union.
8245      If not, it will get done when the type is completed.  */
8246   if (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == FUNCTION_DECL
8247       || TREE_CODE (decl) == RESULT_DECL)
8248     {
8249       if (TREE_CODE (decl) == VAR_DECL)
8250         maybe_commonize_var (decl);
8251
8252       make_rtl_for_nonlocal_decl (decl, init, asmspec);
8253
8254       if (TREE_CODE (type) == FUNCTION_TYPE
8255           || TREE_CODE (type) == METHOD_TYPE)
8256         abstract_virtuals_error (decl,
8257                                  strip_array_types (TREE_TYPE (type)));
8258       else
8259         abstract_virtuals_error (decl, strip_array_types (type));
8260
8261       if (TREE_CODE (decl) == FUNCTION_DECL 
8262           || TREE_TYPE (decl) == error_mark_node)
8263         /* No initialization required.  */
8264         ;
8265       else if (DECL_EXTERNAL (decl)
8266                && ! (DECL_LANG_SPECIFIC (decl)
8267                      && DECL_NOT_REALLY_EXTERN (decl)))
8268         {
8269           if (init)
8270             DECL_INITIAL (decl) = init;
8271         }
8272       else
8273         {
8274           /* A variable definition.  */
8275           if (DECL_FUNCTION_SCOPE_P (decl))
8276             {
8277               /* This is a local declaration.  */
8278               if (doing_semantic_analysis_p ())
8279                 maybe_inject_for_scope_var (decl);
8280               /* Initialize the local variable.  */
8281               if (processing_template_decl)
8282                 {
8283                   if (init || DECL_INITIAL (decl) == error_mark_node)
8284                     DECL_INITIAL (decl) = init;
8285                 }
8286               else if (!TREE_STATIC (decl))
8287                 initialize_local_var (decl, init);
8288             }
8289
8290           if (TREE_STATIC (decl))
8291             expand_static_init (decl, init);
8292         }
8293     finish_end0:
8294
8295       /* Undo call to `pushclass' that was done in `start_decl'
8296          due to initialization of qualified member variable.
8297          I.e., Foo::x = 10;  */
8298       {
8299         tree context = CP_DECL_CONTEXT (decl);
8300         if (context
8301             && TYPE_P (context)
8302             && (TREE_CODE (decl) == VAR_DECL
8303                 /* We also have a pushclass done that we need to undo here
8304                    if we're at top level and declare a method.  */
8305                 || TREE_CODE (decl) == FUNCTION_DECL)
8306             /* If size hasn't been set, we're still defining it,
8307                and therefore inside the class body; don't pop
8308                the binding level..  */
8309             && COMPLETE_TYPE_P (context)
8310             && context == current_class_type)
8311           pop_nested_class ();
8312       }
8313     }
8314
8315  finish_end:
8316
8317   if (was_readonly)
8318     TREE_READONLY (decl) = 1;
8319 }
8320
8321 /* This is here for a midend callback from c-common.c */
8322
8323 void
8324 finish_decl (tree decl, tree init, tree asmspec_tree)
8325 {
8326   cp_finish_decl (decl, init, asmspec_tree, 0);
8327 }
8328
8329 /* Returns a declaration for a VAR_DECL as if:
8330
8331      extern "C" TYPE NAME;
8332
8333    had been seen.  Used to create compiler-generated global
8334    variables.  */
8335
8336 tree
8337 declare_global_var (tree name, tree type)
8338 {
8339   tree decl;
8340
8341   push_to_top_level ();
8342   decl = build_decl (VAR_DECL, name, type);
8343   TREE_PUBLIC (decl) = 1;
8344   DECL_EXTERNAL (decl) = 1;
8345   DECL_ARTIFICIAL (decl) = 1;
8346   pushdecl (decl);
8347   cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
8348   pop_from_top_level ();
8349
8350   return decl;
8351 }
8352
8353 /* Returns a pointer to the `atexit' function.  Note that if
8354    FLAG_USE_CXA_ATEXIT is nonzero, then this will actually be the new
8355    `__cxa_atexit' function specified in the IA64 C++ ABI.  */
8356
8357 static tree
8358 get_atexit_node (void)
8359 {
8360   tree atexit_fndecl;
8361   tree arg_types;
8362   tree fn_type;
8363   tree fn_ptr_type;
8364   const char *name;
8365
8366   if (atexit_node)
8367     return atexit_node;
8368
8369   if (flag_use_cxa_atexit)
8370     {
8371       /* The declaration for `__cxa_atexit' is:
8372
8373            int __cxa_atexit (void (*)(void *), void *, void *)
8374
8375          We build up the argument types and then then function type
8376          itself.  */
8377
8378       /* First, build the pointer-to-function type for the first
8379          argument.  */
8380       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8381       fn_type = build_function_type (void_type_node, arg_types);
8382       fn_ptr_type = build_pointer_type (fn_type);
8383       /* Then, build the rest of the argument types.  */
8384       arg_types = tree_cons (NULL_TREE, ptr_type_node, void_list_node);
8385       arg_types = tree_cons (NULL_TREE, ptr_type_node, arg_types);
8386       arg_types = tree_cons (NULL_TREE, fn_ptr_type, arg_types);
8387       /* And the final __cxa_atexit type.  */
8388       fn_type = build_function_type (integer_type_node, arg_types);
8389       fn_ptr_type = build_pointer_type (fn_type);
8390       name = "__cxa_atexit";
8391     }
8392   else
8393     {
8394       /* The declaration for `atexit' is:
8395
8396            int atexit (void (*)());
8397
8398          We build up the argument types and then then function type
8399          itself.  */
8400       fn_type = build_function_type (void_type_node, void_list_node);
8401       fn_ptr_type = build_pointer_type (fn_type);
8402       arg_types = tree_cons (NULL_TREE, fn_ptr_type, void_list_node);
8403       /* Build the final atexit type.  */
8404       fn_type = build_function_type (integer_type_node, arg_types);
8405       name = "atexit";
8406     }
8407
8408   /* Now, build the function declaration.  */
8409   push_lang_context (lang_name_c);
8410   atexit_fndecl = build_library_fn_ptr (name, fn_type);
8411   mark_used (atexit_fndecl);
8412   pop_lang_context ();
8413   atexit_node = default_conversion (atexit_fndecl);
8414
8415   return atexit_node;
8416 }
8417
8418 /* Returns the __dso_handle VAR_DECL.  */
8419
8420 static tree
8421 get_dso_handle_node (void)
8422 {
8423   if (dso_handle_node)
8424     return dso_handle_node;
8425
8426   /* Declare the variable.  */
8427   dso_handle_node = declare_global_var (get_identifier ("__dso_handle"),
8428                                         ptr_type_node);
8429
8430   return dso_handle_node;
8431 }
8432
8433 /* Begin a new function with internal linkage whose job will be simply
8434    to destroy some particular variable.  */
8435
8436 static tree
8437 start_cleanup_fn (void)
8438 {
8439   static int counter = 0;
8440   int old_interface_only = interface_only;
8441   int old_interface_unknown = interface_unknown;
8442   char name[32];
8443   tree parmtypes;
8444   tree fntype;
8445   tree fndecl;
8446
8447   push_to_top_level ();
8448
8449   /* No need to mangle this.  */
8450   push_lang_context (lang_name_c);
8451
8452   interface_only = 0;
8453   interface_unknown = 1;
8454
8455   /* Build the parameter-types.  */
8456   parmtypes = void_list_node;
8457   /* Functions passed to __cxa_atexit take an additional parameter.
8458      We'll just ignore it.  After we implement the new calling
8459      convention for destructors, we can eliminate the use of
8460      additional cleanup functions entirely in the -fnew-abi case.  */
8461   if (flag_use_cxa_atexit)
8462     parmtypes = tree_cons (NULL_TREE, ptr_type_node, parmtypes);
8463   /* Build the function type itself.  */
8464   fntype = build_function_type (void_type_node, parmtypes);
8465   /* Build the name of the function.  */
8466   sprintf (name, "__tcf_%d", counter++);
8467   /* Build the function declaration.  */
8468   fndecl = build_lang_decl (FUNCTION_DECL, get_identifier (name), fntype);
8469   /* It's a function with internal linkage, generated by the
8470      compiler.  */
8471   TREE_PUBLIC (fndecl) = 0;
8472   DECL_ARTIFICIAL (fndecl) = 1;
8473   /* Make the function `inline' so that it is only emitted if it is
8474      actually needed.  It is unlikely that it will be inlined, since
8475      it is only called via a function pointer, but we avoid unnecessary
8476      emissions this way.  */
8477   DECL_INLINE (fndecl) = 1;
8478   /* Build the parameter.  */
8479   if (flag_use_cxa_atexit)
8480     {
8481       tree parmdecl;
8482
8483       parmdecl = cp_build_parm_decl (NULL_TREE, ptr_type_node);
8484       DECL_CONTEXT (parmdecl) = fndecl;
8485       TREE_USED (parmdecl) = 1;
8486       DECL_ARGUMENTS (fndecl) = parmdecl;
8487     }
8488
8489   pushdecl (fndecl);
8490   start_function (/*specs=*/NULL_TREE, fndecl, NULL_TREE, SF_PRE_PARSED);
8491
8492   interface_unknown = old_interface_unknown;
8493   interface_only = old_interface_only;
8494
8495   pop_lang_context ();
8496
8497   return current_function_decl;
8498 }
8499
8500 /* Finish the cleanup function begun by start_cleanup_fn.  */
8501
8502 static void
8503 end_cleanup_fn (void)
8504 {
8505   expand_body (finish_function (0));
8506
8507   pop_from_top_level ();
8508 }
8509
8510 /* Generate code to handle the destruction of DECL, an object with
8511    static storage duration.  */
8512
8513 void
8514 register_dtor_fn (tree decl)
8515 {
8516   tree cleanup;
8517   tree compound_stmt;
8518   tree args;
8519   tree fcall;
8520   int saved_flag_access_control;
8521
8522   if (TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8523     return;
8524
8525   /* Call build_cleanup before we enter the anonymous function so that
8526      any access checks will be done relative to the current scope,
8527      rather than the scope of the anonymous function.  */
8528   build_cleanup (decl);
8529
8530   /* Now start the function.  */
8531   cleanup = start_cleanup_fn ();
8532
8533   /* Now, recompute the cleanup.  It may contain SAVE_EXPRs that refer
8534      to the original function, rather than the anonymous one.  That
8535      will make the back-end think that nested functions are in use,
8536      which causes confusion.  */
8537   saved_flag_access_control = flag_access_control;
8538   scope_chain->check_access = flag_access_control = 0;
8539   fcall = build_cleanup (decl);
8540   scope_chain->check_access = flag_access_control = saved_flag_access_control;
8541
8542   /* Create the body of the anonymous function.  */
8543   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
8544   finish_expr_stmt (fcall);
8545   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
8546   end_cleanup_fn ();
8547
8548   /* Call atexit with the cleanup function.  */
8549   cxx_mark_addressable (cleanup);
8550   cleanup = build_unary_op (ADDR_EXPR, cleanup, 0);
8551   if (flag_use_cxa_atexit)
8552     {
8553       args = tree_cons (NULL_TREE, 
8554                         build_unary_op (ADDR_EXPR, get_dso_handle_node (), 0),
8555                         NULL_TREE);
8556       args = tree_cons (NULL_TREE, null_pointer_node, args);
8557       args = tree_cons (NULL_TREE, cleanup, args);
8558     }
8559   else
8560     args = tree_cons (NULL_TREE, cleanup, NULL_TREE);
8561   finish_expr_stmt (build_function_call (get_atexit_node (), args));
8562 }
8563
8564 /* DECL is a VAR_DECL with static storage duration.  INIT, if present,
8565    is its initializer.  Generate code to handle the construction
8566    and destruction of DECL.  */
8567
8568 static void
8569 expand_static_init (tree decl, tree init)
8570 {
8571   tree oldstatic;
8572
8573   my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20021010);
8574   my_friendly_assert (TREE_STATIC (decl), 20021010);
8575
8576   /* Some variables require no initialization.  */
8577   if (!init 
8578       && !TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (decl))
8579       && TYPE_HAS_TRIVIAL_DESTRUCTOR (TREE_TYPE (decl)))
8580     return;
8581
8582   oldstatic = value_member (decl, static_aggregates);
8583
8584   if (oldstatic)
8585     {
8586       if (TREE_PURPOSE (oldstatic) && init != NULL_TREE)
8587         error ("multiple initializations given for `%D'", decl);
8588     }
8589   else if (! toplevel_bindings_p ())
8590     {
8591       /* Emit code to perform this initialization but once.  */
8592       tree if_stmt;
8593       tree then_clause;
8594       tree assignment;
8595       tree guard;
8596       tree guard_init;
8597
8598       /* Emit code to perform this initialization but once.  This code
8599          looks like:
8600
8601            static int guard = 0;
8602            if (!guard) {
8603              // Do initialization.
8604              guard = 1;
8605              // Register variable for destruction at end of program.
8606            }
8607
8608          Note that the `temp' variable is only set to 1 *after* the
8609          initialization is complete.  This ensures that an exception,
8610          thrown during the construction, will cause the variable to
8611          reinitialized when we pass through this code again, as per:
8612
8613            [stmt.dcl]
8614
8615            If the initialization exits by throwing an exception, the
8616            initialization is not complete, so it will be tried again
8617            the next time control enters the declaration.
8618
8619          In theory, this process should be thread-safe, too; multiple
8620          threads should not be able to initialize the variable more
8621          than once.  We don't yet attempt to ensure thread-safety.  */
8622
8623       /* Create the guard variable.  */
8624       guard = get_guard (decl);
8625
8626       /* Begin the conditional initialization.  */
8627       if_stmt = begin_if_stmt ();
8628       finish_if_stmt_cond (get_guard_cond (guard), if_stmt);
8629       then_clause = begin_compound_stmt (/*has_no_scope=*/0);
8630
8631       /* Do the initialization itself.  */
8632       assignment = init ? init : NULL_TREE;
8633
8634       /* Once the assignment is complete, set TEMP to 1.  Since the
8635          construction of the static object is complete at this point,
8636          we want to make sure TEMP is set to 1 even if a temporary
8637          constructed during the initialization throws an exception
8638          when it is destroyed.  So, we combine the initialization and
8639          the assignment to TEMP into a single expression, ensuring
8640          that when we call finish_expr_stmt the cleanups will not be
8641          run until after TEMP is set to 1.  */
8642       guard_init = set_guard (guard);
8643       if (assignment)
8644         {
8645           assignment = tree_cons (NULL_TREE, assignment,
8646                                   build_tree_list (NULL_TREE,
8647                                                    guard_init));
8648           assignment = build_compound_expr (assignment);
8649         }
8650       else
8651         assignment = guard_init;
8652       finish_expr_stmt (assignment);
8653
8654       /* Use atexit to register a function for destroying this static
8655          variable.  */
8656       register_dtor_fn (decl);
8657
8658       finish_compound_stmt (/*has_no_scope=*/0, then_clause);
8659       finish_then_clause (if_stmt);
8660       finish_if_stmt ();
8661     }
8662   else
8663     static_aggregates = tree_cons (init, decl, static_aggregates);
8664 }
8665
8666 /* Finish the declaration of a catch-parameter.  */
8667
8668 tree
8669 start_handler_parms (tree declspecs, tree declarator)
8670 {
8671   tree decl;
8672   if (declspecs)
8673     {
8674       decl = grokdeclarator (declarator, declspecs, CATCHPARM,
8675                              1, NULL);
8676       if (decl == NULL_TREE)
8677         error ("invalid catch parameter");
8678     }
8679   else
8680     decl = NULL_TREE;
8681
8682   return decl;
8683 }
8684
8685 \f
8686 /* Make TYPE a complete type based on INITIAL_VALUE.
8687    Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
8688    2 if there was no information (in which case assume 0 if DO_DEFAULT).  */
8689
8690 int
8691 complete_array_type (tree type, tree initial_value, int do_default)
8692 {
8693   register tree maxindex = NULL_TREE;
8694   int value = 0;
8695
8696   if (initial_value)
8697     {
8698       /* An array of character type can be initialized from a
8699          brace-enclosed string constant.  */
8700       if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type)))
8701           && TREE_CODE (initial_value) == CONSTRUCTOR
8702           && CONSTRUCTOR_ELTS (initial_value)
8703           && (TREE_CODE (TREE_VALUE (CONSTRUCTOR_ELTS (initial_value)))
8704               == STRING_CST)
8705           && TREE_CHAIN (CONSTRUCTOR_ELTS (initial_value)) == NULL_TREE)
8706         initial_value = TREE_VALUE (CONSTRUCTOR_ELTS (initial_value));
8707
8708       /* Note MAXINDEX is really the maximum index, one less than the
8709          size.  */
8710       if (TREE_CODE (initial_value) == STRING_CST)
8711         {
8712           int eltsize
8713             = int_size_in_bytes (TREE_TYPE (TREE_TYPE (initial_value)));
8714           maxindex = build_int_2 ((TREE_STRING_LENGTH (initial_value)
8715                                    / eltsize) - 1, 0);
8716         }
8717       else if (TREE_CODE (initial_value) == CONSTRUCTOR)
8718         {
8719           tree elts = CONSTRUCTOR_ELTS (initial_value);
8720
8721           maxindex = ssize_int (-1);
8722           for (; elts; elts = TREE_CHAIN (elts))
8723             {
8724               if (TREE_PURPOSE (elts))
8725                 maxindex = TREE_PURPOSE (elts);
8726               else
8727                 maxindex = size_binop (PLUS_EXPR, maxindex, ssize_int (1));
8728             }
8729           maxindex = copy_node (maxindex);
8730         }
8731       else
8732         {
8733           /* Make an error message unless that happened already.  */
8734           if (initial_value != error_mark_node)
8735             value = 1;
8736           else
8737             initial_value = NULL_TREE;
8738
8739           /* Prevent further error messages.  */
8740           maxindex = build_int_2 (0, 0);
8741         }
8742     }
8743
8744   if (!maxindex)
8745     {
8746       if (do_default)
8747         maxindex = build_int_2 (0, 0);
8748       value = 2;
8749     }
8750
8751   if (maxindex)
8752     {
8753       tree itype;
8754       tree domain;
8755
8756       domain = build_index_type (maxindex);
8757       TYPE_DOMAIN (type) = domain;
8758
8759       if (! TREE_TYPE (maxindex))
8760         TREE_TYPE (maxindex) = domain;
8761       if (initial_value)
8762         itype = TREE_TYPE (initial_value);
8763       else
8764         itype = NULL;
8765       if (itype && !TYPE_DOMAIN (itype))
8766         TYPE_DOMAIN (itype) = domain;
8767       /* The type of the main variant should never be used for arrays
8768          of different sizes.  It should only ever be completed with the
8769          size of the array.  */
8770       if (! TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)))
8771         TYPE_DOMAIN (TYPE_MAIN_VARIANT (type)) = domain;
8772     }
8773
8774   /* Lay out the type now that we can get the real answer.  */
8775
8776   layout_type (type);
8777
8778   return value;
8779 }
8780 \f
8781 /* Return zero if something is declared to be a member of type
8782    CTYPE when in the context of CUR_TYPE.  STRING is the error
8783    message to print in that case.  Otherwise, quietly return 1.  */
8784
8785 static int
8786 member_function_or_else (tree ctype, tree cur_type, enum overload_flags flags)
8787 {
8788   if (ctype && ctype != cur_type)
8789     {
8790       if (flags == DTOR_FLAG)
8791         error ("destructor for alien class `%T' cannot be a member",
8792                   ctype);
8793       else
8794         error ("constructor for alien class `%T' cannot be a member",
8795                   ctype);
8796       return 0;
8797     }
8798   return 1;
8799 }
8800 \f
8801 /* Subroutine of `grokdeclarator'.  */
8802
8803 /* Generate errors possibly applicable for a given set of specifiers.
8804    This is for ARM $7.1.2.  */
8805
8806 static void
8807 bad_specifiers (tree object,
8808                 const char* type,
8809                 int virtualp,
8810                 int quals,
8811                 int inlinep,
8812                 int friendp,
8813                 int raises)
8814 {
8815   if (virtualp)
8816     error ("`%D' declared as a `virtual' %s", object, type);
8817   if (inlinep)
8818     error ("`%D' declared as an `inline' %s", object, type);
8819   if (quals)
8820     error ("`const' and `volatile' function specifiers on `%D' invalid in %s declaration",
8821               object, type);
8822   if (friendp)
8823     cp_error_at ("`%D' declared as a friend", object);
8824   if (raises
8825       && (TREE_CODE (object) == TYPE_DECL
8826           || (!TYPE_PTRFN_P (TREE_TYPE (object))
8827               && !TYPE_REFFN_P (TREE_TYPE (object))
8828               && !TYPE_PTRMEMFUNC_P (TREE_TYPE (object)))))
8829     cp_error_at ("`%D' declared with an exception specification", object);
8830 }
8831
8832 /* CTYPE is class type, or null if non-class.
8833    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
8834    or METHOD_TYPE.
8835    DECLARATOR is the function's name.
8836    VIRTUALP is truthvalue of whether the function is virtual or not.
8837    FLAGS are to be passed through to `grokclassfn'.
8838    QUALS are qualifiers indicating whether the function is `const'
8839    or `volatile'.
8840    RAISES is a list of exceptions that this function can raise.
8841    CHECK is 1 if we must find this method in CTYPE, 0 if we should
8842    not look, and -1 if we should not call `grokclassfn' at all.
8843
8844    Returns `NULL_TREE' if something goes wrong, after issuing
8845    applicable error messages.  */
8846
8847 static tree
8848 grokfndecl (tree ctype, 
8849             tree type,
8850             tree declarator,
8851             tree orig_declarator,
8852             int virtualp,
8853             enum overload_flags flags,
8854             tree quals, 
8855             tree raises,
8856             int check, 
8857             int friendp, 
8858             int publicp, 
8859             int inlinep, 
8860             int funcdef_flag, 
8861             int template_count,
8862             tree in_namespace)
8863 {
8864   tree decl;
8865   int staticp = ctype && TREE_CODE (type) == FUNCTION_TYPE;
8866   int has_default_arg = 0;
8867   tree t;
8868
8869   if (raises)
8870     type = build_exception_variant (type, raises);
8871
8872   decl = build_lang_decl (FUNCTION_DECL, declarator, type);
8873   /* Propagate volatile out from type to decl.  */
8874   if (TYPE_VOLATILE (type))
8875     TREE_THIS_VOLATILE (decl) = 1;
8876
8877   /* If this decl has namespace scope, set that up.  */
8878   if (in_namespace)
8879     set_decl_namespace (decl, in_namespace, friendp);
8880   else if (!ctype)
8881     DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
8882
8883   /* `main' and builtins have implicit 'C' linkage.  */
8884   if ((MAIN_NAME_P (declarator)
8885        || (IDENTIFIER_LENGTH (declarator) > 10
8886            && IDENTIFIER_POINTER (declarator)[0] == '_'
8887            && IDENTIFIER_POINTER (declarator)[1] == '_'
8888            && strncmp (IDENTIFIER_POINTER (declarator)+2, "builtin_", 8) == 0))
8889       && current_lang_name == lang_name_cplusplus
8890       && ctype == NULL_TREE
8891       /* NULL_TREE means global namespace.  */
8892       && DECL_CONTEXT (decl) == NULL_TREE)
8893     SET_DECL_LANGUAGE (decl, lang_c);
8894
8895   /* Should probably propagate const out from type to decl I bet (mrs).  */
8896   if (staticp)
8897     {
8898       DECL_STATIC_FUNCTION_P (decl) = 1;
8899       DECL_CONTEXT (decl) = ctype;
8900     }
8901
8902   if (ctype)
8903     DECL_CONTEXT (decl) = ctype;
8904
8905   if (ctype == NULL_TREE && DECL_MAIN_P (decl))
8906     {
8907       if (processing_template_decl)
8908         error ("cannot declare `::main' to be a template");
8909       if (inlinep)
8910         error ("cannot declare `::main' to be inline");
8911       if (!publicp)
8912         error ("cannot declare `::main' to be static");
8913       if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
8914                         integer_type_node))
8915         error ("`main' must return `int'");
8916       inlinep = 0;
8917       publicp = 1;
8918     }
8919
8920   /* Members of anonymous types and local classes have no linkage; make
8921      them internal.  */
8922   /* FIXME what if it gets a name from typedef?  */
8923   if (ctype && (TYPE_ANONYMOUS_P (ctype)
8924                 || decl_function_context (TYPE_MAIN_DECL (ctype))))
8925     publicp = 0;
8926
8927   if (publicp)
8928     {
8929       /* [basic.link]: A name with no linkage (notably, the name of a class
8930          or enumeration declared in a local scope) shall not be used to
8931          declare an entity with linkage.
8932
8933          Only check this for public decls for now.  */
8934       t = no_linkage_check (TREE_TYPE (decl));
8935       if (t)
8936         {
8937           if (TYPE_ANONYMOUS_P (t))
8938             {
8939               if (DECL_EXTERN_C_P (decl))
8940                 /* Allow this; it's pretty common in C.  */;
8941               else
8942                 {
8943                   pedwarn ("non-local function `%#D' uses anonymous type",
8944                               decl);
8945                   if (DECL_ORIGINAL_TYPE (TYPE_NAME (t)))
8946                     cp_pedwarn_at ("\
8947 `%#D' does not refer to the unqualified type, so it is not used for linkage",
8948                                 TYPE_NAME (t));
8949                 }
8950             }
8951           else
8952             pedwarn ("non-local function `%#D' uses local type `%T'",
8953                         decl, t);
8954         }
8955     }
8956
8957   TREE_PUBLIC (decl) = publicp;
8958   if (! publicp)
8959     {
8960       DECL_INTERFACE_KNOWN (decl) = 1;
8961       DECL_NOT_REALLY_EXTERN (decl) = 1;
8962     }
8963
8964   DID_INLINE_FUNC (decl) = 0;
8965   /* If the declaration was declared inline, mark it as such.  */
8966   if (inlinep)
8967     DECL_DECLARED_INLINE_P (decl) = 1;
8968   /* We inline functions that are explicitly declared inline, or, when
8969      the user explicitly asks us to, all functions.  */
8970   if (DECL_DECLARED_INLINE_P (decl))
8971     DECL_INLINE (decl) = 1;
8972   if (flag_inline_trees == 2 && !DECL_INLINE (decl))
8973     {
8974       DID_INLINE_FUNC (decl) = 1;
8975       DECL_INLINE (decl) = 1;
8976     }
8977
8978   DECL_EXTERNAL (decl) = 1;
8979   if (quals != NULL_TREE && TREE_CODE (type) == FUNCTION_TYPE)
8980     {
8981       error ("%smember function `%D' cannot have `%T' method qualifier",
8982                 (ctype ? "static " : "non-"), decl, TREE_VALUE (quals));
8983       quals = NULL_TREE;
8984     }
8985
8986   if (IDENTIFIER_OPNAME_P (DECL_NAME (decl)))
8987     grok_op_properties (decl, friendp);
8988
8989   if (ctype && decl_function_context (decl))
8990     DECL_NO_STATIC_CHAIN (decl) = 1;
8991
8992   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
8993     if (TREE_PURPOSE (t)
8994         && TREE_CODE (TREE_PURPOSE (t)) == DEFAULT_ARG)
8995       {
8996         has_default_arg = 1;
8997         break;
8998       }
8999
9000   if (friendp
9001       && TREE_CODE (orig_declarator) == TEMPLATE_ID_EXPR)
9002     {
9003       if (funcdef_flag)
9004         error
9005           ("defining explicit specialization `%D' in friend declaration",
9006            orig_declarator);
9007       else
9008         {
9009           tree fns = TREE_OPERAND (orig_declarator, 0);
9010           tree args = TREE_OPERAND (orig_declarator, 1);
9011
9012           if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9013             {
9014               /* Something like `template <class T> friend void f<T>()'.  */
9015               error ("invalid use of template-id `%D' in declaration of primary template",
9016                         orig_declarator);
9017               return NULL_TREE;
9018             }
9019
9020
9021           /* A friend declaration of the form friend void f<>().  Record
9022              the information in the TEMPLATE_ID_EXPR.  */
9023           SET_DECL_IMPLICIT_INSTANTIATION (decl);
9024
9025           if (TREE_CODE (fns) == COMPONENT_REF)
9026             {
9027               /* Due to bison parser ickiness, we will have already looked
9028                  up an operator_name or PFUNCNAME within the current class
9029                  (see template_id in parse.y). If the current class contains
9030                  such a name, we'll get a COMPONENT_REF here. Undo that.  */
9031
9032               my_friendly_assert (TREE_TYPE (TREE_OPERAND (fns, 0))
9033                                   == current_class_type, 20001120);
9034               fns = TREE_OPERAND (fns, 1);
9035             }
9036           my_friendly_assert (TREE_CODE (fns) == IDENTIFIER_NODE
9037                               || TREE_CODE (fns) == LOOKUP_EXPR
9038                               || TREE_CODE (fns) == OVERLOAD, 20001120);
9039           DECL_TEMPLATE_INFO (decl) = tree_cons (fns, args, NULL_TREE);
9040
9041           if (has_default_arg)
9042             {
9043               error ("default arguments are not allowed in declaration of friend template specialization `%D'",
9044                         decl);
9045               return NULL_TREE;
9046             }
9047
9048           if (inlinep)
9049             {
9050               error ("`inline' is not allowed in declaration of friend template specialization `%D'",
9051                         decl);
9052               return NULL_TREE;
9053             }
9054         }
9055     }
9056
9057   if (funcdef_flag)
9058     /* Make the init_value nonzero so pushdecl knows this is not
9059        tentative.  error_mark_node is replaced later with the BLOCK.  */
9060     DECL_INITIAL (decl) = error_mark_node;
9061
9062   if (TYPE_NOTHROW_P (type) || nothrow_libfn_p (decl))
9063     TREE_NOTHROW (decl) = 1;
9064
9065   /* Caller will do the rest of this.  */
9066   if (check < 0)
9067     return decl;
9068
9069   if (flags == NO_SPECIAL && ctype && constructor_name_p (declarator, ctype))
9070     DECL_CONSTRUCTOR_P (decl) = 1;
9071
9072   /* Function gets the ugly name, field gets the nice one.  This call
9073      may change the type of the function (because of default
9074      parameters)!  */
9075   if (ctype != NULL_TREE)
9076     grokclassfn (ctype, decl, flags, quals);
9077
9078   decl = check_explicit_specialization (orig_declarator, decl,
9079                                         template_count,
9080                                         2 * (funcdef_flag != 0) +
9081                                         4 * (friendp != 0));
9082   if (decl == error_mark_node)
9083     return NULL_TREE;
9084
9085   if (ctype != NULL_TREE
9086       && (! TYPE_FOR_JAVA (ctype) || check_java_method (decl))
9087       && check)
9088     {
9089       tree old_decl;
9090
9091       old_decl = check_classfn (ctype, decl);
9092
9093       if (old_decl && TREE_CODE (old_decl) == TEMPLATE_DECL)
9094         /* Because grokfndecl is always supposed to return a
9095            FUNCTION_DECL, we pull out the DECL_TEMPLATE_RESULT
9096            here.  We depend on our callers to figure out that its
9097            really a template that's being returned.  */
9098         old_decl = DECL_TEMPLATE_RESULT (old_decl);
9099
9100       if (old_decl && DECL_STATIC_FUNCTION_P (old_decl)
9101           && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
9102         {
9103           /* Remove the `this' parm added by grokclassfn.
9104              XXX Isn't this done in start_function, too?  */
9105           revert_static_member_fn (decl);
9106           last_function_parms = TREE_CHAIN (last_function_parms);
9107         }
9108       if (old_decl && DECL_ARTIFICIAL (old_decl))
9109         error ("definition of implicitly-declared `%D'", old_decl);
9110
9111       if (old_decl)
9112         {
9113           /* Since we've smashed OLD_DECL to its
9114              DECL_TEMPLATE_RESULT, we must do the same to DECL.  */
9115           if (TREE_CODE (decl) == TEMPLATE_DECL)
9116             decl = DECL_TEMPLATE_RESULT (decl);
9117
9118           /* Attempt to merge the declarations.  This can fail, in
9119              the case of some invalid specialization declarations.  */
9120           push_scope (ctype);
9121           if (!duplicate_decls (decl, old_decl))
9122             error ("no `%#D' member function declared in class `%T'",
9123                       decl, ctype);
9124           pop_scope (ctype);
9125           return old_decl;
9126         }
9127     }
9128
9129   if (DECL_CONSTRUCTOR_P (decl) && !grok_ctor_properties (ctype, decl))
9130     return NULL_TREE;
9131
9132   if (ctype == NULL_TREE || check)
9133     return decl;
9134
9135   if (virtualp)
9136     DECL_VIRTUAL_P (decl) = 1;
9137
9138   return decl;
9139 }
9140
9141 /* Create a VAR_DECL named NAME with the indicated TYPE.  
9142
9143    If SCOPE is non-NULL, it is the class type or namespace containing
9144    the variable.  If SCOPE is NULL, the variable should is created in
9145    the innermost enclosings scope.  */
9146
9147 static tree
9148 grokvardecl (tree type,
9149              tree name,
9150              RID_BIT_TYPE * specbits_in,
9151              int initialized,
9152              int constp,
9153              tree scope)
9154 {
9155   tree decl;
9156   RID_BIT_TYPE specbits;
9157
9158   my_friendly_assert (!name || TREE_CODE (name) == IDENTIFIER_NODE, 
9159                       20020808);
9160
9161   specbits = *specbits_in;
9162
9163   /* Compute the scope in which to place the variable.  */
9164   if (!scope)
9165     {
9166       /* An explicit "extern" specifier indicates a namespace-scope
9167          variable.  */
9168       if (RIDBIT_SETP (RID_EXTERN, specbits))
9169         scope = current_namespace;
9170       else if (!at_function_scope_p ())
9171         {
9172           scope = current_scope ();
9173           if (!scope)
9174             scope = current_namespace;
9175         }
9176     }
9177
9178   if (scope
9179       && (/* If the variable is a namespace-scope variable declared in a
9180              template, we need DECL_LANG_SPECIFIC.  */
9181           (TREE_CODE (scope) == NAMESPACE_DECL && processing_template_decl)
9182           /* Similarly for namespace-scope variables with language linkage
9183              other than C++.  */
9184           || (TREE_CODE (scope) == NAMESPACE_DECL 
9185               && current_lang_name != lang_name_cplusplus)
9186           /* Similarly for static data members.  */
9187           || TYPE_P (scope)))
9188     decl = build_lang_decl (VAR_DECL, name, type);
9189   else
9190     decl = build_decl (VAR_DECL, name, type);
9191
9192   if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
9193     set_decl_namespace (decl, scope, 0);
9194   else
9195     DECL_CONTEXT (decl) = scope;
9196
9197   if (name && scope && current_lang_name != lang_name_c)
9198     /* We can't mangle lazily here because we don't have any
9199        way to recover whether or not a variable was `extern
9200        "C"' later.  */
9201     mangle_decl (decl);
9202
9203   if (RIDBIT_SETP (RID_EXTERN, specbits))
9204     {
9205       DECL_THIS_EXTERN (decl) = 1;
9206       DECL_EXTERNAL (decl) = !initialized;
9207     }
9208
9209   /* In class context, static means one per class,
9210      public access, and static storage.  */
9211   if (DECL_CLASS_SCOPE_P (decl))
9212     {
9213       TREE_PUBLIC (decl) = 1;
9214       TREE_STATIC (decl) = 1;
9215       DECL_EXTERNAL (decl) = 0;
9216     }
9217   /* At top level, either `static' or no s.c. makes a definition
9218      (perhaps tentative), and absence of `static' makes it public.  */
9219   else if (toplevel_bindings_p ())
9220     {
9221       TREE_PUBLIC (decl) = (RIDBIT_NOTSETP (RID_STATIC, specbits)
9222                             && (DECL_THIS_EXTERN (decl) || ! constp));
9223       TREE_STATIC (decl) = ! DECL_EXTERNAL (decl);
9224     }
9225   /* Not at top level, only `static' makes a static definition.  */
9226   else
9227     {
9228       TREE_STATIC (decl) = !! RIDBIT_SETP (RID_STATIC, specbits);
9229       TREE_PUBLIC (decl) = DECL_EXTERNAL (decl);
9230     }
9231
9232   if (RIDBIT_SETP (RID_THREAD, specbits))
9233     {
9234       if (targetm.have_tls)
9235         DECL_THREAD_LOCAL (decl) = 1;
9236       else
9237         /* A mere warning is sure to result in improper semantics
9238            at runtime.  Don't bother to allow this to compile.  */
9239         error ("thread-local storage not supported for this target");
9240     }
9241
9242   if (TREE_PUBLIC (decl))
9243     {
9244       /* [basic.link]: A name with no linkage (notably, the name of a class
9245          or enumeration declared in a local scope) shall not be used to
9246          declare an entity with linkage.
9247
9248          Only check this for public decls for now.  */
9249       tree t = no_linkage_check (TREE_TYPE (decl));
9250       if (t)
9251         {
9252           if (TYPE_ANONYMOUS_P (t))
9253             /* Ignore for now; `enum { foo } e' is pretty common.  */;
9254           else
9255             pedwarn ("non-local variable `%#D' uses local type `%T'",
9256                         decl, t);
9257         }
9258     }
9259
9260   return decl;
9261 }
9262
9263 /* Create and return a canonical pointer to member function type, for
9264    TYPE, which is a POINTER_TYPE to a METHOD_TYPE.  */
9265
9266 tree
9267 build_ptrmemfunc_type (tree type)
9268 {
9269   tree field, fields;
9270   tree t;
9271   tree unqualified_variant = NULL_TREE;
9272
9273   if (type == error_mark_node)
9274     return type;
9275
9276   /* If a canonical type already exists for this type, use it.  We use
9277      this method instead of type_hash_canon, because it only does a
9278      simple equality check on the list of field members.  */
9279
9280   if ((t = TYPE_GET_PTRMEMFUNC_TYPE (type)))
9281     return t;
9282
9283   /* Make sure that we always have the unqualified pointer-to-member
9284      type first.  */
9285   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9286     unqualified_variant
9287       = build_ptrmemfunc_type (TYPE_MAIN_VARIANT (type));
9288
9289   t = make_aggr_type (RECORD_TYPE);
9290   /* Let the front-end know this is a pointer to member function...  */
9291   TYPE_PTRMEMFUNC_FLAG (t) = 1;
9292   /* ... and not really an aggregate.  */
9293   SET_IS_AGGR_TYPE (t, 0);
9294
9295   field = build_decl (FIELD_DECL, pfn_identifier, type);
9296   fields = field;
9297   
9298   field = build_decl (FIELD_DECL, delta_identifier, delta_type_node);
9299   TREE_CHAIN (field) = fields;
9300   fields = field;
9301   
9302   finish_builtin_struct (t, "__ptrmemfunc_type", fields, ptr_type_node);
9303
9304   /* Zap out the name so that the back-end will give us the debugging
9305      information for this anonymous RECORD_TYPE.  */
9306   TYPE_NAME (t) = NULL_TREE;
9307
9308   /* If this is not the unqualified form of this pointer-to-member
9309      type, set the TYPE_MAIN_VARIANT for this type to be the
9310      unqualified type.  Since they are actually RECORD_TYPEs that are
9311      not variants of each other, we must do this manually.  */
9312   if (cp_type_quals (type) != TYPE_UNQUALIFIED)
9313     {
9314       t = build_qualified_type (t, cp_type_quals (type));
9315       TYPE_MAIN_VARIANT (t) = unqualified_variant;
9316       TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (unqualified_variant);
9317       TYPE_NEXT_VARIANT (unqualified_variant) = t;
9318     }
9319
9320   /* Cache this pointer-to-member type so that we can find it again
9321      later.  */
9322   TYPE_SET_PTRMEMFUNC_TYPE (type, t);
9323
9324   /* Seems to be wanted.  */
9325   CLASSTYPE_GOT_SEMICOLON (t) = 1;
9326
9327   return t;
9328 }
9329
9330 /* Create and return a pointer to data member type.  */
9331
9332 tree
9333 build_ptrmem_type (tree class_type, tree member_type)
9334 {
9335   return build_pointer_type (build_offset_type (class_type, member_type));
9336 }
9337
9338 /* DECL is a VAR_DECL defined in-class, whose TYPE is also given.
9339    Check to see that the definition is valid.  Issue appropriate error
9340    messages.  Return 1 if the definition is particularly bad, or 0
9341    otherwise.  */
9342
9343 int
9344 check_static_variable_definition (tree decl, tree type)
9345 {
9346   /* Motion 10 at San Diego: If a static const integral data member is
9347      initialized with an integral constant expression, the initializer
9348      may appear either in the declaration (within the class), or in
9349      the definition, but not both.  If it appears in the class, the
9350      member is a member constant.  The file-scope definition is always
9351      required.  */
9352   if (!ARITHMETIC_TYPE_P (type) && TREE_CODE (type) != ENUMERAL_TYPE)
9353     {
9354       error ("invalid in-class initialization of static data member of non-integral type `%T'",
9355              type);
9356       /* If we just return the declaration, crashes will sometimes
9357          occur.  We therefore return void_type_node, as if this was a
9358          friend declaration, to cause callers to completely ignore
9359          this declaration.  */
9360       return 1;
9361     }
9362   else if (!CP_TYPE_CONST_P (type))
9363     error ("ISO C++ forbids in-class initialization of non-const static member `%D'",
9364               decl);
9365   else if (pedantic && !INTEGRAL_TYPE_P (type))
9366     pedwarn ("ISO C++ forbids initialization of member constant `%D' of non-integral type `%T'", decl, type);
9367
9368   return 0;
9369 }
9370
9371 /* Given the SIZE (i.e., number of elements) in an array, compute an
9372    appropriate index type for the array.  If non-NULL, NAME is the
9373    name of the thing being declared.  */
9374
9375 tree
9376 compute_array_index_type (tree name, tree size)
9377 {
9378   tree itype;
9379
9380   /* If this involves a template parameter, it will be a constant at
9381      instantiation time, but we don't know what the value is yet.
9382      Even if no template parameters are involved, we may an expression
9383      that is not a constant; we don't even simplify `1 + 2' when
9384      processing a template.  */
9385   if (processing_template_decl)
9386     {
9387       /* Resolve a qualified reference to an enumerator or static
9388          const data member of ours.  */
9389       if (TREE_CODE (size) == SCOPE_REF
9390           && TREE_OPERAND (size, 0) == current_class_type)
9391         {
9392           tree t = lookup_field (current_class_type,
9393                                  TREE_OPERAND (size, 1), 0, false);
9394           if (t)
9395             size = t;
9396         }
9397
9398       return build_index_type (build_min (MINUS_EXPR, sizetype,
9399                                           size, integer_one_node));
9400     }
9401
9402   /* The size might be the result of a cast.  */
9403   STRIP_TYPE_NOPS (size);
9404
9405   /* It might be a const variable or enumeration constant.  */
9406   size = decl_constant_value (size);
9407
9408   /* The array bound must be an integer type.  */
9409   if (TREE_CODE (TREE_TYPE (size)) != INTEGER_TYPE
9410       && TREE_CODE (TREE_TYPE (size)) != ENUMERAL_TYPE
9411       && TREE_CODE (TREE_TYPE (size)) != BOOLEAN_TYPE)
9412     {
9413       if (name)
9414         error ("size of array `%D' has non-integer type", name);
9415       else
9416         error ("size of array has non-integer type");
9417       size = integer_one_node;
9418     }
9419
9420   /* Normally, the array-bound will be a constant.  */
9421   if (TREE_CODE (size) == INTEGER_CST)
9422     {
9423       /* Check to see if the array bound overflowed.  Make that an
9424          error, no matter how generous we're being.  */
9425       int old_flag_pedantic_errors = flag_pedantic_errors;
9426       int old_pedantic = pedantic;
9427       pedantic = flag_pedantic_errors = 1;
9428       constant_expression_warning (size);
9429       pedantic = old_pedantic;
9430       flag_pedantic_errors = old_flag_pedantic_errors;
9431
9432       /* An array must have a positive number of elements.  */
9433       if (INT_CST_LT (size, integer_zero_node))
9434         {
9435           if (name)
9436             error ("size of array `%D' is negative", name);
9437           else
9438             error ("size of array is negative");
9439           size = integer_one_node;
9440         }
9441       /* Except that an extension we allow zero-sized arrays.  We
9442          always allow them in system headers because glibc uses
9443          them.  */
9444       else if (integer_zerop (size) && pedantic && !in_system_header)
9445         {
9446           if (name)
9447             pedwarn ("ISO C++ forbids zero-size array `%D'", name);
9448           else
9449             pedwarn ("ISO C++ forbids zero-size array");
9450         }
9451     }
9452   else if (TREE_CONSTANT (size))
9453     {
9454       /* `(int) &fn' is not a valid array bound.  */
9455       if (name)
9456         error ("size of array `%D' is not an integral constant-expression",
9457                   name);
9458       else
9459         error ("size of array is not an integral constant-expression");
9460     }
9461
9462   /* Compute the index of the largest element in the array.  It is
9463      one less than the number of elements in the array.  */
9464   itype
9465     = fold (cp_build_binary_op (MINUS_EXPR,
9466                                 cp_convert (ssizetype, size),
9467                                 cp_convert (ssizetype,
9468                                             integer_one_node)));
9469
9470   /* Check for variable-sized arrays.  We allow such things as an
9471      extension, even though they are not allowed in ANSI/ISO C++.  */
9472   if (!TREE_CONSTANT (itype))
9473     {
9474       if (pedantic)
9475         {
9476           if (name)
9477             pedwarn ("ISO C++ forbids variable-size array `%D'",
9478                         name);
9479           else
9480             pedwarn ("ISO C++ forbids variable-size array");
9481         }
9482
9483       /* Create a variable-sized array index type.  */
9484       itype = variable_size (itype);
9485     }
9486   /* Make sure that there was no overflow when creating to a signed
9487      index type.  (For example, on a 32-bit machine, an array with
9488      size 2^32 - 1 is too big.)  */
9489   else if (TREE_OVERFLOW (itype))
9490     {
9491       error ("overflow in array dimension");
9492       TREE_OVERFLOW (itype) = 0;
9493     }
9494
9495   /* Create and return the appropriate index type.  */
9496   return build_index_type (itype);
9497 }
9498
9499 /* Returns the scope (if any) in which the entity declared by
9500    DECLARATOR will be located.  If the entity was declared with an
9501    unqualified name, NULL_TREE is returned.  */
9502
9503 tree
9504 get_scope_of_declarator (tree declarator)
9505 {
9506   if (!declarator)
9507     return NULL_TREE;
9508   
9509   switch (TREE_CODE (declarator))
9510     {
9511     case CALL_EXPR:
9512     case ARRAY_REF:
9513     case INDIRECT_REF:
9514     case ADDR_EXPR:
9515       /* For any of these, the main declarator is the first operand.  */
9516       return get_scope_of_declarator (TREE_OPERAND
9517                                       (declarator, 0));
9518
9519     case SCOPE_REF:
9520       /* For a pointer-to-member, continue descending.  */
9521       if (TREE_CODE (TREE_OPERAND (declarator, 1))
9522           == INDIRECT_REF)
9523         return get_scope_of_declarator (TREE_OPERAND
9524                                         (declarator, 1));
9525       /* Otherwise, if the declarator-id is a SCOPE_REF, the scope in
9526          which the declaration occurs is the first operand.  */
9527       return TREE_OPERAND (declarator, 0);
9528
9529     case TREE_LIST:
9530       /* Attributes to be applied. The declarator is TREE_VALUE.  */
9531       return get_scope_of_declarator (TREE_VALUE (declarator));
9532       
9533     default:
9534       /* Otherwise, we have a declarator-id which is not a qualified
9535          name; the entity will be declared in the current scope.  */
9536       return NULL_TREE;
9537     }
9538 }
9539
9540 /* Returns an ARRAY_TYPE for an array with SIZE elements of the
9541    indicated TYPE.  If non-NULL, NAME is the NAME of the declaration
9542    with this type.  */
9543
9544 static tree
9545 create_array_type_for_decl (tree name, tree type, tree size)
9546 {
9547   tree itype = NULL_TREE;
9548   const char* error_msg;
9549
9550   /* If things have already gone awry, bail now.  */
9551   if (type == error_mark_node || size == error_mark_node)
9552     return error_mark_node;
9553
9554   /* Assume that everything will go OK.  */
9555   error_msg = NULL;
9556
9557   /* There are some types which cannot be array elements.  */
9558   switch (TREE_CODE (type))
9559     {
9560     case VOID_TYPE:
9561       error_msg = "array of void";
9562       break;
9563
9564     case FUNCTION_TYPE:
9565       error_msg = "array of functions";
9566       break;
9567
9568     case REFERENCE_TYPE:
9569       error_msg = "array of references";
9570       break;
9571
9572     case OFFSET_TYPE:
9573       error_msg = "array of data members";
9574       break;
9575
9576     case METHOD_TYPE:
9577       error_msg = "array of function members";
9578       break;
9579
9580     default:
9581       break;
9582     }
9583
9584   /* If something went wrong, issue an error-message and return.  */
9585   if (error_msg)
9586     {
9587       if (name)
9588         error ("declaration of `%D' as %s", name, error_msg);
9589       else
9590         error ("creating %s", error_msg);
9591
9592       return error_mark_node;
9593     }
9594
9595   /* [dcl.array]
9596
9597      The constant expressions that specify the bounds of the arrays
9598      can be omitted only for the first member of the sequence.  */
9599   if (TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type))
9600     {
9601       if (name)
9602         error ("declaration of `%D' as multidimensional array must have bounds for all dimensions except the first",
9603                   name);
9604       else
9605         error ("multidimensional array must have bounds for all dimensions except the first");
9606
9607       return error_mark_node;
9608     }
9609
9610   /* Figure out the index type for the array.  */
9611   if (size)
9612     itype = compute_array_index_type (name, size);
9613
9614   return build_cplus_array_type (type, itype);
9615 }
9616
9617 /* Check that it's OK to declare a function with the indicated TYPE.
9618    SFK indicates the kind of special function (if any) that this
9619    function is.  OPTYPE is the type given in a conversion operator
9620    declaration.  Returns the actual return type of the function; that
9621    may be different than TYPE if an error occurs, or for certain
9622    special functions.  */
9623
9624 static tree
9625 check_special_function_return_type (special_function_kind sfk,
9626                                     tree type,
9627                                     tree optype)
9628 {
9629   switch (sfk)
9630     {
9631     case sfk_constructor:
9632       if (type)
9633         error ("return type specification for constructor invalid");
9634
9635       type = void_type_node;
9636       break;
9637
9638     case sfk_destructor:
9639       if (type)
9640         error ("return type specification for destructor invalid");
9641       type = void_type_node;
9642       break;
9643
9644     case sfk_conversion:
9645       if (type && !same_type_p (type, optype))
9646         error ("operator `%T' declared to return `%T'", optype, type);
9647       else if (type)
9648         pedwarn ("return type specified for `operator %T'",  optype);
9649       type = optype;
9650       break;
9651
9652     default:
9653       abort ();
9654       break;
9655     }
9656
9657   return type;
9658 }
9659
9660 /* Given declspecs and a declarator (abstract or otherwise), determine
9661    the name and type of the object declared and construct a DECL node
9662    for it.
9663
9664    DECLSPECS is a chain of tree_list nodes whose value fields
9665     are the storage classes and type specifiers.
9666
9667    DECL_CONTEXT says which syntactic context this declaration is in:
9668      NORMAL for most contexts.  Make a VAR_DECL or FUNCTION_DECL or TYPE_DECL.
9669      FUNCDEF for a function definition.  Like NORMAL but a few different
9670       error messages in each case.  Return value may be zero meaning
9671       this definition is too screwy to try to parse.
9672      MEMFUNCDEF for a function definition.  Like FUNCDEF but prepares to
9673       handle member functions (which have FIELD context).
9674       Return value may be zero meaning this definition is too screwy to
9675       try to parse.
9676      PARM for a parameter declaration (either within a function prototype
9677       or before a function body).  Make a PARM_DECL, or return void_type_node.
9678      CATCHPARM for a parameter declaration before a catch clause.
9679      TYPENAME if for a typename (in a cast or sizeof).
9680       Don't make a DECL node; just return the ..._TYPE node.
9681      FIELD for a struct or union field; make a FIELD_DECL.
9682      BITFIELD for a field with specified width.
9683    INITIALIZED is 1 if the decl has an initializer.
9684
9685    ATTRLIST is a pointer to the list of attributes, which may be NULL
9686    if there are none; *ATTRLIST may be modified if attributes from inside
9687    the declarator should be applied to the declaration.
9688
9689    When this function is called, scoping variables (such as
9690    CURRENT_CLASS_TYPE) should reflect the scope in which the
9691    declaration occurs, not the scope in which the new declaration will
9692    be placed.  For example, on:
9693
9694      void S::f() { ... }
9695
9696    when grokdeclarator is called for `S::f', the CURRENT_CLASS_TYPE
9697    should not be `S'.  */
9698
9699 tree
9700 grokdeclarator (tree declarator,
9701                 tree declspecs,
9702                 enum decl_context decl_context,
9703                 int initialized,
9704                 tree* attrlist)
9705 {
9706   RID_BIT_TYPE specbits;
9707   int nclasses = 0;
9708   tree spec;
9709   tree type = NULL_TREE;
9710   int longlong = 0;
9711   int type_quals;
9712   int virtualp, explicitp, friendp, inlinep, staticp;
9713   int explicit_int = 0;
9714   int explicit_char = 0;
9715   int defaulted_int = 0;
9716   int extern_langp = 0;
9717   tree dependant_name = NULL_TREE;
9718   
9719   tree typedef_decl = NULL_TREE;
9720   const char *name;
9721   tree typedef_type = NULL_TREE;
9722   int funcdef_flag = 0;
9723   enum tree_code innermost_code = ERROR_MARK;
9724   int bitfield = 0;
9725 #if 0
9726   /* See the code below that used this.  */
9727   tree decl_attr = NULL_TREE;
9728 #endif
9729
9730   /* Keep track of what sort of function is being processed
9731      so that we can warn about default return values, or explicit
9732      return values which do not match prescribed defaults.  */
9733   special_function_kind sfk = sfk_none;
9734
9735   tree dname = NULL_TREE;
9736   tree ctype = current_class_type;
9737   tree ctor_return_type = NULL_TREE;
9738   enum overload_flags flags = NO_SPECIAL;
9739   tree quals = NULL_TREE;
9740   tree raises = NULL_TREE;
9741   int template_count = 0;
9742   tree in_namespace = NULL_TREE;
9743   tree returned_attrs = NULL_TREE;
9744   tree scope = NULL_TREE;
9745
9746   RIDBIT_RESET_ALL (specbits);
9747   if (decl_context == FUNCDEF)
9748     funcdef_flag = 1, decl_context = NORMAL;
9749   else if (decl_context == MEMFUNCDEF)
9750     funcdef_flag = -1, decl_context = FIELD;
9751   else if (decl_context == BITFIELD)
9752     bitfield = 1, decl_context = FIELD;
9753
9754   /* Look inside a declarator for the name being declared
9755      and get it as a string, for an error message.  */
9756   {
9757     tree *next = &declarator;
9758     register tree decl;
9759     name = NULL;
9760
9761     while (next && *next)
9762       {
9763         decl = *next;
9764         switch (TREE_CODE (decl))
9765           {
9766           case TREE_LIST:
9767             /* For attributes.  */
9768             next = &TREE_VALUE (decl);
9769             break;
9770
9771           case COND_EXPR:
9772             ctype = NULL_TREE;
9773             next = &TREE_OPERAND (decl, 0);
9774             break;
9775
9776           case BIT_NOT_EXPR:    /* For C++ destructors!  */
9777             {
9778               tree name = TREE_OPERAND (decl, 0);
9779               tree rename = NULL_TREE;
9780
9781               my_friendly_assert (flags == NO_SPECIAL, 152);
9782               flags = DTOR_FLAG;
9783               sfk = sfk_destructor;
9784               if (TYPE_P (name))
9785                 TREE_OPERAND (decl, 0) = name = constructor_name (name);
9786               my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 153);
9787               if (ctype == NULL_TREE)
9788                 {
9789                   if (current_class_type == NULL_TREE)
9790                     {
9791                       error ("destructors must be member functions");
9792                       flags = NO_SPECIAL;
9793                     }
9794                   else
9795                     {
9796                       tree t = constructor_name (current_class_type);
9797                       if (t != name)
9798                         rename = t;
9799                     }
9800                 }
9801               else
9802                 {
9803                   tree t = constructor_name (ctype);
9804                   if (t != name)
9805                     rename = t;
9806                 }
9807
9808               if (rename)
9809                 {
9810                   error ("destructor `%T' must match class name `%T'",
9811                             name, rename);
9812                   TREE_OPERAND (decl, 0) = rename;
9813                 }
9814               next = &name;
9815             }
9816             break;
9817
9818           case ADDR_EXPR:       /* C++ reference declaration */
9819             /* Fall through.  */
9820           case ARRAY_REF:
9821           case INDIRECT_REF:
9822             ctype = NULL_TREE;
9823             innermost_code = TREE_CODE (decl);
9824             next = &TREE_OPERAND (decl, 0);
9825             break;
9826
9827           case CALL_EXPR:
9828             innermost_code = TREE_CODE (decl);
9829             if (decl_context == FIELD && ctype == NULL_TREE)
9830               ctype = current_class_type;
9831             if (ctype
9832                 && TREE_OPERAND (decl, 0)
9833                 && (TREE_CODE (TREE_OPERAND (decl, 0)) == TYPE_DECL
9834                     && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 0)),
9835                                            ctype)))
9836               TREE_OPERAND (decl, 0) = constructor_name (ctype);
9837             next = &TREE_OPERAND (decl, 0);
9838             decl = *next;
9839             if (ctype != NULL_TREE
9840                 && decl != NULL_TREE && flags != DTOR_FLAG
9841                 && constructor_name_p (decl, ctype))
9842               {
9843                 sfk = sfk_constructor;
9844                 ctor_return_type = ctype;
9845               }
9846             ctype = NULL_TREE;
9847             break;
9848
9849           case TEMPLATE_ID_EXPR:
9850               {
9851                 tree fns = TREE_OPERAND (decl, 0);
9852
9853                 if (TREE_CODE (fns) == LOOKUP_EXPR)
9854                   fns = TREE_OPERAND (fns, 0);
9855
9856                 dname = fns;
9857                 if (TREE_CODE (dname) == COMPONENT_REF)
9858                   dname = TREE_OPERAND (dname, 1);
9859                 if (TREE_CODE (dname) != IDENTIFIER_NODE)
9860                   {
9861                     my_friendly_assert (is_overloaded_fn (dname),
9862                                         19990331);
9863                     dname = DECL_NAME (get_first_fn (dname));
9864                   }
9865               }
9866           /* Fall through.  */
9867
9868           case IDENTIFIER_NODE:
9869             if (TREE_CODE (decl) == IDENTIFIER_NODE)
9870               dname = decl;
9871
9872             next = 0;
9873
9874             if (C_IS_RESERVED_WORD (dname))
9875               {
9876                 error ("declarator-id missing; using reserved word `%D'",
9877                           dname);
9878                 name = IDENTIFIER_POINTER (dname);
9879               }
9880             else if (!IDENTIFIER_TYPENAME_P (dname))
9881               name = IDENTIFIER_POINTER (dname);
9882             else
9883               {
9884                 my_friendly_assert (flags == NO_SPECIAL, 154);
9885                 flags = TYPENAME_FLAG;
9886                 ctor_return_type = TREE_TYPE (dname);
9887                 sfk = sfk_conversion;
9888                 if (IDENTIFIER_GLOBAL_VALUE (dname)
9889                     && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (dname))
9890                         == TYPE_DECL))
9891                   name = IDENTIFIER_POINTER (dname);
9892                 else
9893                   name = "<invalid operator>";
9894               }
9895             break;
9896
9897             /* C++ extension */
9898           case SCOPE_REF:
9899             {
9900               /* Perform error checking, and decide on a ctype.  */
9901               tree cname = TREE_OPERAND (decl, 0);
9902               if (cname == NULL_TREE)
9903                 ctype = NULL_TREE;
9904               else if (TREE_CODE (cname) == NAMESPACE_DECL)
9905                 {
9906                   ctype = NULL_TREE;
9907                   in_namespace = TREE_OPERAND (decl, 0);
9908                 }
9909               else if (! is_aggr_type (cname, 1))
9910                 ctype = NULL_TREE;
9911               /* Must test TREE_OPERAND (decl, 1), in case user gives
9912                  us `typedef (class::memfunc)(int); memfunc *memfuncptr;'  */
9913               else if (TREE_OPERAND (decl, 1)
9914                        && TREE_CODE (TREE_OPERAND (decl, 1)) == INDIRECT_REF)
9915                 ctype = cname;
9916               else if (TREE_CODE (cname) == TEMPLATE_TYPE_PARM
9917                        || TREE_CODE (cname) == BOUND_TEMPLATE_TEMPLATE_PARM)
9918                 {
9919                   /* This might be declaring a member of a template
9920                      parm to be a friend.  */
9921                   ctype = cname;
9922                   dependant_name = TREE_OPERAND (decl, 1);
9923                 }
9924               else if (ctype == NULL_TREE)
9925                 ctype = cname;
9926               else if (TREE_COMPLEXITY (decl) == current_class_depth)
9927                 ;
9928               else
9929                 {
9930                   if (! UNIQUELY_DERIVED_FROM_P (cname, ctype))
9931                     {
9932                       error ("type `%T' is not derived from type `%T'",
9933                                 cname, ctype);
9934                       ctype = NULL_TREE;
9935                     }
9936                   else
9937                     ctype = cname;
9938                 }
9939
9940               /* It is valid to write:
9941
9942                    class C { void f(); };
9943                    typedef C D;
9944                    void D::f();
9945
9946                  The standard is not clear about whether `typedef const C D' is
9947                  legal; as of 2002-09-15 the committee is considering
9948                  that question.  EDG 3.0 allows that syntax.
9949                  Therefore, we do as well.  */
9950               if (ctype)
9951                 ctype = TYPE_MAIN_VARIANT (ctype);
9952               /* Update the declarator so that when we process it
9953                  again the correct type is present.  */
9954               TREE_OPERAND (decl, 0) = ctype;
9955
9956               if (ctype && TREE_CODE (TREE_OPERAND (decl, 1)) == TYPE_DECL
9957                   && constructor_name_p (DECL_NAME (TREE_OPERAND (decl, 1)),
9958                                          ctype))
9959                 TREE_OPERAND (decl, 1) = constructor_name (ctype);
9960               next = &TREE_OPERAND (decl, 1);
9961               decl = *next;
9962               if (ctype)
9963                 {
9964                   if (TREE_CODE (decl) == IDENTIFIER_NODE
9965                       && constructor_name_p (decl, ctype))
9966                     {
9967                       sfk = sfk_constructor;
9968                       ctor_return_type = ctype;
9969                     }
9970                   else if (TREE_CODE (decl) == BIT_NOT_EXPR
9971                            && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
9972                            && constructor_name_p (TREE_OPERAND (decl, 0),
9973                                                   ctype))
9974                     {
9975                       sfk = sfk_destructor;
9976                       ctor_return_type = ctype;
9977                       flags = DTOR_FLAG;
9978                       TREE_OPERAND (decl, 0) = constructor_name (ctype);
9979                       next = &TREE_OPERAND (decl, 0);
9980                     }
9981                 }
9982             }
9983             break;
9984
9985           case ERROR_MARK:
9986             next = 0;
9987             break;
9988
9989           case TYPE_DECL:
9990             /* Parse error puts this typespec where
9991                a declarator should go.  */
9992             error ("`%T' specified as declarator-id", DECL_NAME (decl));
9993             if (TREE_TYPE (decl) == current_class_type)
9994               error ("  perhaps you want `%T' for a constructor",
9995                         current_class_name);
9996             dname = DECL_NAME (decl);
9997             name = IDENTIFIER_POINTER (dname);
9998
9999             /* Avoid giving two errors for this.  */
10000             IDENTIFIER_CLASS_VALUE (dname) = NULL_TREE;
10001
10002             declspecs = tree_cons (NULL_TREE, integer_type_node, declspecs);
10003             *next = dname;
10004             next = 0;
10005             break;
10006
10007           case BASELINK:
10008             next = &BASELINK_FUNCTIONS (decl);
10009             break;
10010
10011           case TEMPLATE_DECL:
10012             /* Sometimes, we see a template-name used as part of a 
10013                decl-specifier like in 
10014                   std::allocator alloc;
10015                Handle that gracefully.  */
10016             error ("invalid use of template-name '%E' in a declarator", decl);
10017             return error_mark_node;
10018             break;
10019             
10020           default:
10021             my_friendly_assert (0, 20020917);
10022           }
10023       }
10024   }
10025
10026   /* A function definition's declarator must have the form of
10027      a function declarator.  */
10028
10029   if (funcdef_flag && innermost_code != CALL_EXPR)
10030     return 0;
10031
10032   if (((dname && IDENTIFIER_OPNAME_P (dname)) || flags == TYPENAME_FLAG)
10033       && innermost_code != CALL_EXPR
10034       && ! (ctype && declspecs == NULL_TREE))
10035     {
10036       error ("declaration of `%D' as non-function", dname);
10037       return void_type_node;
10038     }
10039
10040   /* Anything declared one level down from the top level
10041      must be one of the parameters of a function
10042      (because the body is at least two levels down).  */
10043
10044   /* This heuristic cannot be applied to C++ nodes! Fixed, however,
10045      by not allowing C++ class definitions to specify their parameters
10046      with xdecls (must be spec.d in the parmlist).
10047
10048      Since we now wait to push a class scope until we are sure that
10049      we are in a legitimate method context, we must set oldcname
10050      explicitly (since current_class_name is not yet alive).
10051
10052      We also want to avoid calling this a PARM if it is in a namespace.  */
10053
10054   if (decl_context == NORMAL && !toplevel_bindings_p ())
10055     {
10056       struct cp_binding_level *b = current_binding_level;
10057       current_binding_level = b->level_chain;
10058       if (current_binding_level != 0 && toplevel_bindings_p ())
10059         decl_context = PARM;
10060       current_binding_level = b;
10061     }
10062
10063   if (name == NULL)
10064     name = decl_context == PARM ? "parameter" : "type name";
10065
10066   /* Look through the decl specs and record which ones appear.
10067      Some typespecs are defined as built-in typenames.
10068      Others, the ones that are modifiers of other types,
10069      are represented by bits in SPECBITS: set the bits for
10070      the modifiers that appear.  Storage class keywords are also in SPECBITS.
10071
10072      If there is a typedef name or a type, store the type in TYPE.
10073      This includes builtin typedefs such as `int'.
10074
10075      Set EXPLICIT_INT if the type is `int' or `char' and did not
10076      come from a user typedef.
10077
10078      Set LONGLONG if `long' is mentioned twice.
10079
10080      For C++, constructors and destructors have their own fast treatment.  */
10081
10082   for (spec = declspecs; spec; spec = TREE_CHAIN (spec))
10083     {
10084       register int i;
10085       register tree id;
10086
10087       /* Certain parse errors slip through.  For example,
10088          `int class;' is not caught by the parser. Try
10089          weakly to recover here.  */
10090       if (TREE_CODE (spec) != TREE_LIST)
10091         return 0;
10092
10093       id = TREE_VALUE (spec);
10094
10095       /* If the entire declaration is itself tagged as deprecated then
10096          suppress reports of deprecated items.  */
10097       if (!adding_implicit_members && id && TREE_DEPRECATED (id))
10098         {
10099           if (deprecated_state != DEPRECATED_SUPPRESS)
10100             warn_deprecated_use (id);
10101         }
10102
10103       if (TREE_CODE (id) == IDENTIFIER_NODE)
10104         {
10105           if (id == ridpointers[(int) RID_INT]
10106               || id == ridpointers[(int) RID_CHAR]
10107               || id == ridpointers[(int) RID_BOOL]
10108               || id == ridpointers[(int) RID_WCHAR])
10109             {
10110               if (type)
10111                 {
10112                   if (id == ridpointers[(int) RID_BOOL])
10113                     error ("`bool' is now a keyword");
10114                   else
10115                     error ("extraneous `%T' ignored", id);
10116                 }
10117               else
10118                 {
10119                   if (id == ridpointers[(int) RID_INT])
10120                     explicit_int = 1;
10121                   else if (id == ridpointers[(int) RID_CHAR])
10122                     explicit_char = 1;
10123                   type = TREE_TYPE (IDENTIFIER_GLOBAL_VALUE (id));
10124                 }
10125               goto found;
10126             }
10127           /* C++ aggregate types.  */
10128           if (IDENTIFIER_HAS_TYPE_VALUE (id))
10129             {
10130               if (type)
10131                 error ("multiple declarations `%T' and `%T'", type, id);
10132               else
10133                 type = IDENTIFIER_TYPE_VALUE (id);
10134               goto found;
10135             }
10136
10137           for (i = (int) RID_FIRST_MODIFIER; i <= (int) RID_LAST_MODIFIER; i++)
10138             {
10139               if (ridpointers[i] == id)
10140                 {
10141                   if (i == (int) RID_LONG && RIDBIT_SETP (i, specbits))
10142                     {
10143                       if (pedantic && ! in_system_header && warn_long_long)
10144                         pedwarn ("ISO C++ does not support `long long'");
10145                       if (longlong)
10146                         error ("`long long long' is too long for GCC");
10147                       else
10148                         longlong = 1;
10149                     }
10150                   else if (RIDBIT_SETP (i, specbits))
10151                     pedwarn ("duplicate `%s'", IDENTIFIER_POINTER (id));
10152
10153                   /* Diagnose "__thread extern" or "__thread static".  */
10154                   if (RIDBIT_SETP (RID_THREAD, specbits))
10155                     {
10156                       if (i == (int)RID_EXTERN)
10157                         error ("`__thread' before `extern'");
10158                       else if (i == (int)RID_STATIC)
10159                         error ("`__thread' before `static'");
10160                     }
10161
10162                   if (i == (int)RID_EXTERN
10163                       && TREE_PURPOSE (spec) == error_mark_node)
10164                     /* This extern was part of a language linkage.  */
10165                     extern_langp = 1;
10166
10167                   RIDBIT_SET (i, specbits);
10168                   goto found;
10169                 }
10170             }
10171         }
10172       else if (TREE_CODE (id) == TYPE_DECL)
10173         {
10174           if (type)
10175             error ("multiple declarations `%T' and `%T'", type,
10176                       TREE_TYPE (id));
10177           else
10178             {
10179               type = TREE_TYPE (id);
10180               TREE_VALUE (spec) = type;
10181               typedef_decl = id;
10182             }
10183           goto found;
10184         }
10185       if (type)
10186         error ("two or more data types in declaration of `%s'", name);
10187       else if (TREE_CODE (id) == IDENTIFIER_NODE)
10188         {
10189           register tree t = lookup_name (id, 1);
10190           if (!t || TREE_CODE (t) != TYPE_DECL)
10191             error ("`%s' fails to be a typedef or built in type",
10192                    IDENTIFIER_POINTER (id));
10193           else
10194             {
10195               type = TREE_TYPE (t);
10196               typedef_decl = t;
10197             }
10198         }
10199       else if (id != error_mark_node)
10200         /* Can't change CLASS nodes into RECORD nodes here!  */
10201         type = id;
10202
10203     found: ;
10204     }
10205
10206 #if 0
10207   /* See the code below that used this.  */
10208   if (typedef_decl)
10209     decl_attr = DECL_ATTRIBUTES (typedef_decl);
10210 #endif
10211   typedef_type = type;
10212
10213   /* No type at all: default to `int', and set DEFAULTED_INT
10214      because it was not a user-defined typedef.  */
10215
10216   if (type == NULL_TREE
10217       && (RIDBIT_SETP (RID_SIGNED, specbits)
10218           || RIDBIT_SETP (RID_UNSIGNED, specbits)
10219           || RIDBIT_SETP (RID_LONG, specbits)
10220           || RIDBIT_SETP (RID_SHORT, specbits)))
10221     {
10222       /* These imply 'int'.  */
10223       type = integer_type_node;
10224       defaulted_int = 1;
10225     }
10226
10227   if (sfk != sfk_none)
10228     type = check_special_function_return_type (sfk, type,
10229                                                ctor_return_type);
10230   else if (type == NULL_TREE)
10231     {
10232       int is_main;
10233
10234       explicit_int = -1;
10235
10236       /* We handle `main' specially here, because 'main () { }' is so
10237          common.  With no options, it is allowed.  With -Wreturn-type,
10238          it is a warning.  It is only an error with -pedantic-errors.  */
10239       is_main = (funcdef_flag
10240                  && dname && MAIN_NAME_P (dname)
10241                  && ctype == NULL_TREE
10242                  && in_namespace == NULL_TREE
10243                  && current_namespace == global_namespace);
10244
10245       if (in_system_header || flag_ms_extensions)
10246         /* Allow it, sigh.  */;
10247       else if (pedantic || ! is_main)
10248         pedwarn ("ISO C++ forbids declaration of `%s' with no type",
10249                     name);
10250       else if (warn_return_type)
10251         warning ("ISO C++ forbids declaration of `%s' with no type",
10252                     name);
10253
10254       type = integer_type_node;
10255     }
10256   
10257   ctype = NULL_TREE;
10258
10259   /* Now process the modifiers that were specified
10260      and check for invalid combinations.  */
10261
10262   /* Long double is a special combination.  */
10263
10264   if (RIDBIT_SETP (RID_LONG, specbits)
10265       && TYPE_MAIN_VARIANT (type) == double_type_node)
10266     {
10267       RIDBIT_RESET (RID_LONG, specbits);
10268       type = build_qualified_type (long_double_type_node,
10269                                    cp_type_quals (type));
10270     }
10271
10272   /* Check all other uses of type modifiers.  */
10273
10274   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10275       || RIDBIT_SETP (RID_SIGNED, specbits)
10276       || RIDBIT_SETP (RID_LONG, specbits)
10277       || RIDBIT_SETP (RID_SHORT, specbits))
10278     {
10279       int ok = 0;
10280
10281       if (TREE_CODE (type) == REAL_TYPE)
10282         error ("short, signed or unsigned invalid for `%s'", name);
10283       else if (TREE_CODE (type) != INTEGER_TYPE)
10284         error ("long, short, signed or unsigned invalid for `%s'", name);
10285       else if (RIDBIT_SETP (RID_LONG, specbits)
10286                && RIDBIT_SETP (RID_SHORT, specbits))
10287         error ("long and short specified together for `%s'", name);
10288       else if ((RIDBIT_SETP (RID_LONG, specbits)
10289                 || RIDBIT_SETP (RID_SHORT, specbits))
10290                && explicit_char)
10291         error ("long or short specified with char for `%s'", name);
10292       else if ((RIDBIT_SETP (RID_LONG, specbits)
10293                 || RIDBIT_SETP (RID_SHORT, specbits))
10294                && TREE_CODE (type) == REAL_TYPE)
10295         error ("long or short specified with floating type for `%s'", name);
10296       else if (RIDBIT_SETP (RID_SIGNED, specbits)
10297                && RIDBIT_SETP (RID_UNSIGNED, specbits))
10298         error ("signed and unsigned given together for `%s'", name);
10299       else
10300         {
10301           ok = 1;
10302           if (!explicit_int && !defaulted_int && !explicit_char && pedantic)
10303             {
10304               pedwarn ("long, short, signed or unsigned used invalidly for `%s'",
10305                        name);
10306               if (flag_pedantic_errors)
10307                 ok = 0;
10308             }
10309         }
10310
10311       /* Discard the type modifiers if they are invalid.  */
10312       if (! ok)
10313         {
10314           RIDBIT_RESET (RID_UNSIGNED, specbits);
10315           RIDBIT_RESET (RID_SIGNED, specbits);
10316           RIDBIT_RESET (RID_LONG, specbits);
10317           RIDBIT_RESET (RID_SHORT, specbits);
10318           longlong = 0;
10319         }
10320     }
10321
10322   if (RIDBIT_SETP (RID_COMPLEX, specbits)
10323       && TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != REAL_TYPE)
10324     {
10325       error ("complex invalid for `%s'", name);
10326       RIDBIT_RESET (RID_COMPLEX, specbits);
10327     }
10328
10329   /* Decide whether an integer type is signed or not.
10330      Optionally treat bitfields as signed by default.  */
10331   if (RIDBIT_SETP (RID_UNSIGNED, specbits)
10332       /* [class.bit]
10333
10334          It is implementation-defined whether a plain (neither
10335          explicitly signed or unsigned) char, short, int, or long
10336          bit-field is signed or unsigned.
10337
10338          Naturally, we extend this to long long as well.  Note that
10339          this does not include wchar_t.  */
10340       || (bitfield && !flag_signed_bitfields
10341           && RIDBIT_NOTSETP (RID_SIGNED, specbits)
10342           /* A typedef for plain `int' without `signed' can be
10343              controlled just like plain `int', but a typedef for
10344              `signed int' cannot be so controlled.  */
10345           && !(typedef_decl
10346                && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl))
10347           && (TREE_CODE (type) == INTEGER_TYPE
10348               || TREE_CODE (type) == CHAR_TYPE)
10349           && !same_type_p (TYPE_MAIN_VARIANT (type), wchar_type_node)))
10350     {
10351       if (longlong)
10352         type = long_long_unsigned_type_node;
10353       else if (RIDBIT_SETP (RID_LONG, specbits))
10354         type = long_unsigned_type_node;
10355       else if (RIDBIT_SETP (RID_SHORT, specbits))
10356         type = short_unsigned_type_node;
10357       else if (type == char_type_node)
10358         type = unsigned_char_type_node;
10359       else if (typedef_decl)
10360         type = c_common_unsigned_type (type);
10361       else
10362         type = unsigned_type_node;
10363     }
10364   else if (RIDBIT_SETP (RID_SIGNED, specbits)
10365            && type == char_type_node)
10366     type = signed_char_type_node;
10367   else if (longlong)
10368     type = long_long_integer_type_node;
10369   else if (RIDBIT_SETP (RID_LONG, specbits))
10370     type = long_integer_type_node;
10371   else if (RIDBIT_SETP (RID_SHORT, specbits))
10372     type = short_integer_type_node;
10373
10374   if (RIDBIT_SETP (RID_COMPLEX, specbits))
10375     {
10376       /* If we just have "complex", it is equivalent to
10377          "complex double", but if any modifiers at all are specified it is
10378          the complex form of TYPE.  E.g, "complex short" is
10379          "complex short int".  */
10380
10381       if (defaulted_int && ! longlong
10382           && ! (RIDBIT_SETP (RID_LONG, specbits)
10383                 || RIDBIT_SETP (RID_SHORT, specbits)
10384                 || RIDBIT_SETP (RID_SIGNED, specbits)
10385                 || RIDBIT_SETP (RID_UNSIGNED, specbits)))
10386         type = complex_double_type_node;
10387       else if (type == integer_type_node)
10388         type = complex_integer_type_node;
10389       else if (type == float_type_node)
10390         type = complex_float_type_node;
10391       else if (type == double_type_node)
10392         type = complex_double_type_node;
10393       else if (type == long_double_type_node)
10394         type = complex_long_double_type_node;
10395       else
10396         type = build_complex_type (type);
10397     }
10398
10399   type_quals = TYPE_UNQUALIFIED;
10400   if (RIDBIT_SETP (RID_CONST, specbits))
10401     type_quals |= TYPE_QUAL_CONST;
10402   if (RIDBIT_SETP (RID_VOLATILE, specbits))
10403     type_quals |= TYPE_QUAL_VOLATILE;
10404   if (RIDBIT_SETP (RID_RESTRICT, specbits))
10405     type_quals |= TYPE_QUAL_RESTRICT;
10406   if (sfk == sfk_conversion && type_quals != TYPE_UNQUALIFIED)
10407     error ("qualifiers are not allowed on declaration of `operator %T'",
10408               ctor_return_type);
10409
10410   type_quals |= cp_type_quals (type);
10411   type = cp_build_qualified_type_real
10412     (type, type_quals, ((typedef_decl && !DECL_ARTIFICIAL (typedef_decl)
10413                          ? tf_ignore_bad_quals : 0) | tf_error | tf_warning));
10414   /* We might have ignored or rejected some of the qualifiers.  */
10415   type_quals = cp_type_quals (type);
10416   
10417   staticp = 0;
10418   inlinep = !! RIDBIT_SETP (RID_INLINE, specbits);
10419   virtualp = RIDBIT_SETP (RID_VIRTUAL, specbits);
10420   RIDBIT_RESET (RID_VIRTUAL, specbits);
10421   explicitp = RIDBIT_SETP (RID_EXPLICIT, specbits) != 0;
10422   RIDBIT_RESET (RID_EXPLICIT, specbits);
10423
10424   if (RIDBIT_SETP (RID_STATIC, specbits))
10425     staticp = 1 + (decl_context == FIELD);
10426
10427   if (virtualp && staticp == 2)
10428     {
10429       error ("member `%D' cannot be declared both virtual and static",
10430                 dname);
10431       staticp = 0;
10432     }
10433   friendp = RIDBIT_SETP (RID_FRIEND, specbits);
10434   RIDBIT_RESET (RID_FRIEND, specbits);
10435
10436   if (dependant_name && !friendp)
10437     {
10438       error ("`%T::%D' is not a valid declarator", ctype, dependant_name);
10439       return void_type_node;
10440     }
10441   
10442   /* Warn if two storage classes are given. Default to `auto'.  */
10443
10444   if (RIDBIT_ANY_SET (specbits))
10445     {
10446       if (RIDBIT_SETP (RID_STATIC, specbits)) nclasses++;
10447       if (RIDBIT_SETP (RID_EXTERN, specbits) && !extern_langp) nclasses++;
10448       if (RIDBIT_SETP (RID_THREAD, specbits)) nclasses++;
10449       if (decl_context == PARM && nclasses > 0)
10450         error ("storage class specifiers invalid in parameter declarations");
10451       if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10452         {
10453           if (decl_context == PARM)
10454             error ("typedef declaration invalid in parameter declaration");
10455           nclasses++;
10456         }
10457       if (RIDBIT_SETP (RID_AUTO, specbits)) nclasses++;
10458       if (RIDBIT_SETP (RID_REGISTER, specbits)) nclasses++;
10459       if (!nclasses && !friendp && extern_langp)
10460         nclasses++;
10461     }
10462
10463   /* Give error if `virtual' is used outside of class declaration.  */
10464   if (virtualp
10465       && (current_class_name == NULL_TREE || decl_context != FIELD))
10466     {
10467       error ("virtual outside class declaration");
10468       virtualp = 0;
10469     }
10470
10471   /* Static anonymous unions are dealt with here.  */
10472   if (staticp && decl_context == TYPENAME
10473       && TREE_CODE (declspecs) == TREE_LIST
10474       && ANON_AGGR_TYPE_P (TREE_VALUE (declspecs)))
10475     decl_context = FIELD;
10476
10477   /* Warn about storage classes that are invalid for certain
10478      kinds of declarations (parameters, typenames, etc.).  */
10479
10480   /* "static __thread" and "extern __thread" are allowed.  */
10481   if (nclasses == 2
10482       && RIDBIT_SETP (RID_THREAD, specbits)
10483       && (RIDBIT_SETP (RID_EXTERN, specbits)
10484           || RIDBIT_SETP (RID_STATIC, specbits)))
10485     nclasses = 1;
10486     
10487   if (nclasses > 1)
10488     error ("multiple storage classes in declaration of `%s'", name);
10489   else if (decl_context != NORMAL && nclasses > 0)
10490     {
10491       if ((decl_context == PARM || decl_context == CATCHPARM)
10492           && (RIDBIT_SETP (RID_REGISTER, specbits)
10493               || RIDBIT_SETP (RID_AUTO, specbits)))
10494         ;
10495       else if (RIDBIT_SETP (RID_TYPEDEF, specbits))
10496         ;
10497       else if (decl_context == FIELD
10498                /* C++ allows static class elements  */
10499                && RIDBIT_SETP (RID_STATIC, specbits))
10500         /* C++ also allows inlines and signed and unsigned elements,
10501            but in those cases we don't come in here.  */
10502         ;
10503       else
10504         {
10505           if (decl_context == FIELD)
10506             {
10507               tree tmp = NULL_TREE;
10508               register int op = 0;
10509
10510               if (declarator)
10511                 {
10512                   /* Avoid trying to get an operand off an identifier node.  */
10513                   if (TREE_CODE (declarator) == IDENTIFIER_NODE)
10514                     tmp = declarator;
10515                   else
10516                     tmp = TREE_OPERAND (declarator, 0);
10517                   op = IDENTIFIER_OPNAME_P (tmp);
10518                   if (IDENTIFIER_TYPENAME_P (tmp))
10519                     {
10520                       if (IDENTIFIER_GLOBAL_VALUE (tmp)
10521                           && (TREE_CODE (IDENTIFIER_GLOBAL_VALUE (tmp))
10522                               == TYPE_DECL))
10523                         name = IDENTIFIER_POINTER (tmp);
10524                       else
10525                         name = "<invalid operator>";
10526                     }
10527                 }
10528               error ("storage class specified for %s `%s'",
10529                      op ? "member operator" : "field",
10530                      name);
10531             }
10532           else
10533             {
10534               if (decl_context == PARM || decl_context == CATCHPARM)
10535                 error ("storage class specified for parameter `%s'", name);
10536               else
10537                 error ("storage class specified for typename");
10538             }
10539           RIDBIT_RESET (RID_REGISTER, specbits);
10540           RIDBIT_RESET (RID_AUTO, specbits);
10541           RIDBIT_RESET (RID_EXTERN, specbits);
10542           RIDBIT_RESET (RID_THREAD, specbits);
10543         }
10544     }
10545   else if (RIDBIT_SETP (RID_EXTERN, specbits) && initialized && !funcdef_flag)
10546     {
10547       if (toplevel_bindings_p ())
10548         {
10549           /* It's common practice (and completely valid) to have a const
10550              be initialized and declared extern.  */
10551           if (!(type_quals & TYPE_QUAL_CONST))
10552             warning ("`%s' initialized and declared `extern'", name);
10553         }
10554       else
10555         error ("`%s' has both `extern' and initializer", name);
10556     }
10557   else if (RIDBIT_SETP (RID_EXTERN, specbits) && funcdef_flag
10558            && ! toplevel_bindings_p ())
10559     error ("nested function `%s' declared `extern'", name);
10560   else if (toplevel_bindings_p ())
10561     {
10562       if (RIDBIT_SETP (RID_AUTO, specbits))
10563         error ("top-level declaration of `%s' specifies `auto'", name);
10564     }
10565   else if (RIDBIT_SETP (RID_THREAD, specbits)
10566            && !RIDBIT_SETP (RID_EXTERN, specbits)
10567            && !RIDBIT_SETP (RID_STATIC, specbits))
10568     {
10569       error ("function-scope `%s' implicitly auto and declared `__thread'",
10570              name);
10571       RIDBIT_RESET (RID_THREAD, specbits);
10572     }
10573
10574   if (nclasses > 0 && friendp)
10575     error ("storage class specifiers invalid in friend function declarations");
10576
10577   scope = get_scope_of_declarator (declarator);
10578
10579   /* Now figure out the structure of the declarator proper.
10580      Descend through it, creating more complex types, until we reach
10581      the declared identifier (or NULL_TREE, in an abstract declarator).  */
10582
10583   while (declarator && TREE_CODE (declarator) != IDENTIFIER_NODE
10584          && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
10585     {
10586       /* Each level of DECLARATOR is either an ARRAY_REF (for ...[..]),
10587          an INDIRECT_REF (for *...),
10588          a CALL_EXPR (for ...(...)),
10589          an identifier (for the name being declared)
10590          or a null pointer (for the place in an absolute declarator
10591          where the name was omitted).
10592          For the last two cases, we have just exited the loop.
10593
10594          For C++ it could also be
10595          a SCOPE_REF (for class :: ...).  In this case, we have converted
10596          sensible names to types, and those are the values we use to
10597          qualify the member name.
10598          an ADDR_EXPR (for &...),
10599          a BIT_NOT_EXPR (for destructors)
10600
10601          At this point, TYPE is the type of elements of an array,
10602          or for a function to return, or for a pointer to point to.
10603          After this sequence of ifs, TYPE is the type of the
10604          array or function or pointer, and DECLARATOR has had its
10605          outermost layer removed.  */
10606
10607       if (type == error_mark_node)
10608         {
10609           if (TREE_CODE (declarator) == SCOPE_REF)
10610             declarator = TREE_OPERAND (declarator, 1);
10611           else
10612             declarator = TREE_OPERAND (declarator, 0);
10613           continue;
10614         }
10615       if (quals != NULL_TREE
10616           && (declarator == NULL_TREE
10617               || TREE_CODE (declarator) != SCOPE_REF))
10618         {
10619           if (ctype == NULL_TREE && TREE_CODE (type) == METHOD_TYPE)
10620             ctype = TYPE_METHOD_BASETYPE (type);
10621           if (ctype != NULL_TREE)
10622             {
10623               tree dummy = build_decl (TYPE_DECL, NULL_TREE, type);
10624               grok_method_quals (ctype, dummy, quals);
10625               type = TREE_TYPE (dummy);
10626               quals = NULL_TREE;
10627             }
10628         }
10629
10630       switch (TREE_CODE (declarator))
10631         {
10632         case TREE_LIST:
10633           {
10634             /* We encode a declarator with embedded attributes using
10635                a TREE_LIST.  */
10636             tree attrs = TREE_PURPOSE (declarator);
10637             tree inner_decl;
10638             int attr_flags;
10639
10640             declarator = TREE_VALUE (declarator);
10641             inner_decl = declarator;
10642             while (inner_decl != NULL_TREE
10643                    && TREE_CODE (inner_decl) == TREE_LIST)
10644               inner_decl = TREE_VALUE (inner_decl);
10645             attr_flags = 0;
10646             if (inner_decl == NULL_TREE
10647                 || TREE_CODE (inner_decl) == IDENTIFIER_NODE)
10648               attr_flags |= (int) ATTR_FLAG_DECL_NEXT;
10649             if (TREE_CODE (inner_decl) == CALL_EXPR)
10650               attr_flags |= (int) ATTR_FLAG_FUNCTION_NEXT;
10651             if (TREE_CODE (inner_decl) == ARRAY_REF)
10652               attr_flags |= (int) ATTR_FLAG_ARRAY_NEXT;
10653             returned_attrs = decl_attributes (&type,
10654                                               chainon (returned_attrs, attrs),
10655                                               attr_flags);
10656           }
10657           break;
10658
10659         case ARRAY_REF:
10660           {
10661             register tree size;
10662
10663             size = TREE_OPERAND (declarator, 1);
10664
10665             /* VC++ spells a zero-sized array with [].  */
10666             if (size == NULL_TREE && decl_context == FIELD && ! staticp
10667                 && ! RIDBIT_SETP (RID_TYPEDEF, specbits))
10668               size = integer_zero_node;
10669
10670             declarator = TREE_OPERAND (declarator, 0);
10671
10672             type = create_array_type_for_decl (dname, type, size);
10673
10674             ctype = NULL_TREE;
10675           }
10676           break;
10677
10678         case CALL_EXPR:
10679           {
10680             tree arg_types;
10681             int funcdecl_p;
10682             tree inner_parms = CALL_DECLARATOR_PARMS (declarator);
10683             tree inner_decl = TREE_OPERAND (declarator, 0);
10684
10685             /* Declaring a function type.
10686                Make sure we have a valid type for the function to return.  */
10687
10688             /* We now know that the TYPE_QUALS don't apply to the
10689                decl, but to its return type.  */
10690             type_quals = TYPE_UNQUALIFIED;
10691
10692             /* Warn about some types functions can't return.  */
10693
10694             if (TREE_CODE (type) == FUNCTION_TYPE)
10695               {
10696                 error ("`%s' declared as function returning a function", name);
10697                 type = integer_type_node;
10698               }
10699             if (TREE_CODE (type) == ARRAY_TYPE)
10700               {
10701                 error ("`%s' declared as function returning an array", name);
10702                 type = integer_type_node;
10703               }
10704
10705             if (inner_decl && TREE_CODE (inner_decl) == SCOPE_REF)
10706               inner_decl = TREE_OPERAND (inner_decl, 1);
10707
10708             if (inner_decl && TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR)
10709               inner_decl = dname;
10710
10711             /* Pick up type qualifiers which should be applied to `this'.  */
10712             quals = CALL_DECLARATOR_QUALS (declarator);
10713
10714             /* Pick up the exception specifications.  */
10715             raises = CALL_DECLARATOR_EXCEPTION_SPEC (declarator);
10716
10717             /* Say it's a definition only for the CALL_EXPR
10718                closest to the identifier.  */
10719             funcdecl_p
10720               = inner_decl
10721               && (TREE_CODE (inner_decl) == IDENTIFIER_NODE
10722                   || TREE_CODE (inner_decl) == TEMPLATE_ID_EXPR
10723                   || TREE_CODE (inner_decl) == BIT_NOT_EXPR);
10724
10725             if (ctype == NULL_TREE
10726                 && decl_context == FIELD
10727                 && funcdecl_p
10728                 && (friendp == 0 || dname == current_class_name))
10729               ctype = current_class_type;
10730
10731             if (ctype && sfk == sfk_conversion)
10732               TYPE_HAS_CONVERSION (ctype) = 1;
10733             if (ctype && constructor_name_p (dname, ctype))
10734               {
10735                 /* We are within a class's scope. If our declarator name
10736                    is the same as the class name, and we are defining
10737                    a function, then it is a constructor/destructor, and
10738                    therefore returns a void type.  */
10739
10740                 if (flags == DTOR_FLAG)
10741                   {
10742                     /* ISO C++ 12.4/2.  A destructor may not be
10743                        declared const or volatile.  A destructor may
10744                        not be static.  */
10745                     if (staticp == 2)
10746                       error ("destructor cannot be static member function");
10747                     if (quals)
10748                       {
10749                         error ("destructors may not be `%s'",
10750                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10751                         quals = NULL_TREE;
10752                       }
10753                     if (decl_context == FIELD)
10754                       {
10755                         if (! member_function_or_else (ctype,
10756                                                        current_class_type,
10757                                                        flags))
10758                           return void_type_node;
10759                       }
10760                   }
10761                 else            /* It's a constructor.  */
10762                   {
10763                     if (explicitp == 1)
10764                       explicitp = 2;
10765                     /* ISO C++ 12.1.  A constructor may not be
10766                        declared const or volatile.  A constructor may
10767                        not be virtual.  A constructor may not be
10768                        static.  */
10769                     if (staticp == 2)
10770                       error ("constructor cannot be static member function");
10771                     if (virtualp)
10772                       {
10773                         pedwarn ("constructors cannot be declared virtual");
10774                         virtualp = 0;
10775                       }
10776                     if (quals)
10777                       {
10778                         error ("constructors may not be `%s'",
10779                                   IDENTIFIER_POINTER (TREE_VALUE (quals)));
10780                         quals = NULL_TREE;
10781                       }
10782                     {
10783                       RID_BIT_TYPE tmp_bits;
10784                       memcpy (&tmp_bits, &specbits, sizeof (RID_BIT_TYPE));
10785                       RIDBIT_RESET (RID_INLINE, tmp_bits);
10786                       RIDBIT_RESET (RID_STATIC, tmp_bits);
10787                       if (RIDBIT_ANY_SET (tmp_bits))
10788                         error ("return value type specifier for constructor ignored");
10789                     }
10790                     if (decl_context == FIELD)
10791                       {
10792                         if (! member_function_or_else (ctype,
10793                                                        current_class_type,
10794                                                        flags))
10795                           return void_type_node;
10796                         TYPE_HAS_CONSTRUCTOR (ctype) = 1;
10797                         if (sfk != sfk_constructor)
10798                           return NULL_TREE;
10799                       }
10800                   }
10801                 if (decl_context == FIELD)
10802                   staticp = 0;
10803               }
10804             else if (friendp)
10805               {
10806                 if (initialized)
10807                   error ("can't initialize friend function `%s'", name);
10808                 if (virtualp)
10809                   {
10810                     /* Cannot be both friend and virtual.  */
10811                     error ("virtual functions cannot be friends");
10812                     RIDBIT_RESET (RID_FRIEND, specbits);
10813                     friendp = 0;
10814                   }
10815                 if (decl_context == NORMAL)
10816                   error ("friend declaration not in class definition");
10817                 if (current_function_decl && funcdef_flag)
10818                   error ("can't define friend function `%s' in a local class definition",
10819                             name);
10820               }
10821
10822             /* Construct the function type and go to the next
10823                inner layer of declarator.  */
10824
10825             declarator = TREE_OPERAND (declarator, 0);
10826
10827             /* FIXME: This is where default args should be fully
10828                processed.  */
10829
10830             arg_types = grokparms (inner_parms);
10831
10832             if (declarator && flags == DTOR_FLAG)
10833               {
10834                 /* A destructor declared in the body of a class will
10835                    be represented as a BIT_NOT_EXPR.  But, we just
10836                    want the underlying IDENTIFIER.  */
10837                 if (TREE_CODE (declarator) == BIT_NOT_EXPR)
10838                   declarator = TREE_OPERAND (declarator, 0);
10839
10840                 if (arg_types != void_list_node)
10841                   {
10842                     error ("destructors may not have parameters");
10843                     arg_types = void_list_node;
10844                     last_function_parms = NULL_TREE;
10845                   }
10846               }
10847
10848             /* ANSI says that `const int foo ();'
10849                does not make the function foo const.  */
10850             type = build_function_type (type, arg_types);
10851           }
10852           break;
10853
10854         case ADDR_EXPR:
10855         case INDIRECT_REF:
10856           /* Filter out pointers-to-references and references-to-references.
10857              We can get these if a TYPE_DECL is used.  */
10858
10859           if (TREE_CODE (type) == REFERENCE_TYPE)
10860             {
10861               error (TREE_CODE (declarator) == ADDR_EXPR
10862                      ? "cannot declare reference to `%#T'"
10863                      : "cannot declare pointer to `%#T'", type);
10864               type = TREE_TYPE (type);
10865             }
10866           else if (VOID_TYPE_P (type)
10867                    && (ctype || TREE_CODE (declarator) == ADDR_EXPR))
10868             error (ctype ? "cannot declare pointer to `%#T' member"
10869                      : "cannot declare reference to `%#T'", type);
10870
10871           /* Merge any constancy or volatility into the target type
10872              for the pointer.  */
10873
10874           /* We now know that the TYPE_QUALS don't apply to the decl,
10875              but to the target of the pointer.  */
10876           type_quals = TYPE_UNQUALIFIED;
10877
10878           if (TREE_CODE (declarator) == ADDR_EXPR)
10879             {
10880               if (!VOID_TYPE_P (type))
10881                 type = build_reference_type (type);
10882             }
10883           else if (TREE_CODE (type) == METHOD_TYPE)
10884             type = build_ptrmemfunc_type (build_pointer_type (type));
10885           else if (ctype)
10886             type = build_ptrmem_type (ctype, type);
10887           else
10888             type = build_pointer_type (type);
10889
10890           /* Process a list of type modifier keywords (such as
10891              const or volatile) that were given inside the `*' or `&'.  */
10892
10893           if (TREE_TYPE (declarator))
10894             {
10895               register tree typemodlist;
10896               int erred = 0;
10897               int constp = 0;
10898               int volatilep = 0;
10899               int restrictp = 0;
10900               
10901               for (typemodlist = TREE_TYPE (declarator); typemodlist;
10902                    typemodlist = TREE_CHAIN (typemodlist))
10903                 {
10904                   tree qualifier = TREE_VALUE (typemodlist);
10905
10906                   if (qualifier == ridpointers[(int) RID_CONST])
10907                     {
10908                       constp++;
10909                       type_quals |= TYPE_QUAL_CONST;
10910                     }
10911                   else if (qualifier == ridpointers[(int) RID_VOLATILE])
10912                     {
10913                       volatilep++;
10914                       type_quals |= TYPE_QUAL_VOLATILE;
10915                     }
10916                   else if (qualifier == ridpointers[(int) RID_RESTRICT])
10917                     {
10918                       restrictp++;
10919                       type_quals |= TYPE_QUAL_RESTRICT;
10920                     }
10921                   else if (!erred)
10922                     {
10923                       erred = 1;
10924                       error ("invalid type modifier within pointer declarator");
10925                     }
10926                 }
10927               if (constp > 1)
10928                 pedwarn ("duplicate `const'");
10929               if (volatilep > 1)
10930                 pedwarn ("duplicate `volatile'");
10931               if (restrictp > 1)
10932                 pedwarn ("duplicate `restrict'");
10933               type = cp_build_qualified_type (type, type_quals);
10934               type_quals = cp_type_quals (type);
10935             }
10936           declarator = TREE_OPERAND (declarator, 0);
10937           ctype = NULL_TREE;
10938           break;
10939
10940         case SCOPE_REF:
10941           {
10942             /* We have converted type names to NULL_TREE if the
10943                name was bogus, or to a _TYPE node, if not.
10944
10945                The variable CTYPE holds the type we will ultimately
10946                resolve to.  The code here just needs to build
10947                up appropriate member types.  */
10948             tree sname = TREE_OPERAND (declarator, 1);
10949             tree t;
10950
10951             /* Destructors can have their visibilities changed as well.  */
10952             if (TREE_CODE (sname) == BIT_NOT_EXPR)
10953               sname = TREE_OPERAND (sname, 0);
10954
10955             if (TREE_OPERAND (declarator, 0) == NULL_TREE)
10956               {
10957                 /* We had a reference to a global decl, or
10958                    perhaps we were given a non-aggregate typedef,
10959                    in which case we cleared this out, and should just
10960                    keep going as though it wasn't there.  */
10961                 declarator = sname;
10962                 continue;
10963               }
10964             ctype = TREE_OPERAND (declarator, 0);
10965
10966             t = ctype;
10967             while (t != NULL_TREE && CLASS_TYPE_P (t))
10968               {
10969                 /* You're supposed to have one `template <...>'
10970                    for every template class, but you don't need one
10971                    for a full specialization.  For example:
10972
10973                      template <class T> struct S{};
10974                      template <> struct S<int> { void f(); };
10975                      void S<int>::f () {}
10976
10977                    is correct; there shouldn't be a `template <>' for
10978                    the definition of `S<int>::f'.  */
10979                 if (CLASSTYPE_TEMPLATE_INFO (t)
10980                     && (CLASSTYPE_TEMPLATE_INSTANTIATION (t)
10981                         || uses_template_parms (CLASSTYPE_TI_ARGS (t)))
10982                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)))
10983                   template_count += 1;
10984
10985                 t = TYPE_MAIN_DECL (t);
10986                 t = DECL_CONTEXT (t);
10987               }
10988
10989             if (sname == NULL_TREE)
10990               goto done_scoping;
10991
10992             if (TREE_CODE (sname) == IDENTIFIER_NODE)
10993               {
10994                 /* This is the `standard' use of the scoping operator:
10995                    basetype :: member .  */
10996
10997                 if (ctype == current_class_type)
10998                   {
10999                     /* class A {
11000                          void A::f ();
11001                        };
11002
11003                        Is this ill-formed?  */
11004
11005                     if (pedantic)
11006                       pedwarn ("extra qualification `%T::' on member `%s' ignored",
11007                                   ctype, name);
11008                   }
11009                 else if (TREE_CODE (type) == FUNCTION_TYPE)
11010                   {
11011                     if (current_class_type == NULL_TREE || friendp)
11012                       type = build_cplus_method_type (ctype, TREE_TYPE (type),
11013                                                       TYPE_ARG_TYPES (type));
11014                     else
11015                       {
11016                         error ("cannot declare member function `%T::%s' within `%T'",
11017                                   ctype, name, current_class_type);
11018                         return error_mark_node;
11019                       }
11020                   }
11021                 else if (RIDBIT_SETP (RID_TYPEDEF, specbits)
11022                          || COMPLETE_TYPE_P (complete_type (ctype)))
11023                   {
11024                     /* Have to move this code elsewhere in this function.
11025                        this code is used for i.e., typedef int A::M; M *pm;
11026
11027                        It is?  How? jason 10/2/94 */
11028
11029                     if (current_class_type)
11030                       {
11031                         error ("cannot declare member `%T::%s' within `%T'",
11032                                   ctype, name, current_class_type);
11033                         return void_type_node;
11034                       }
11035                   }
11036                 else
11037                   {
11038                     cxx_incomplete_type_error (NULL_TREE, ctype);
11039                     return error_mark_node;
11040                   }
11041
11042                 declarator = sname;
11043               }
11044             else if (TREE_CODE (sname) == SCOPE_REF)
11045               abort ();
11046             else
11047               {
11048               done_scoping:
11049                 declarator = TREE_OPERAND (declarator, 1);
11050                 if (declarator && TREE_CODE (declarator) == CALL_EXPR)
11051                   /* In this case, we will deal with it later.  */
11052                   ;
11053                 else if (TREE_CODE (type) == FUNCTION_TYPE)
11054                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11055                                                   TYPE_ARG_TYPES (type));
11056               }
11057           }
11058           break;
11059
11060         case BIT_NOT_EXPR:
11061           declarator = TREE_OPERAND (declarator, 0);
11062           break;
11063
11064         case BASELINK:
11065           declarator = BASELINK_FUNCTIONS (declarator);
11066           break;
11067
11068         case RECORD_TYPE:
11069         case UNION_TYPE:
11070         case ENUMERAL_TYPE:
11071           declarator = NULL_TREE;
11072           break;
11073
11074         case ERROR_MARK:
11075           declarator = NULL_TREE;
11076           break;
11077
11078         default:
11079           abort ();
11080         }
11081     }
11082
11083   if (returned_attrs)
11084     {
11085       if (attrlist)
11086         *attrlist = chainon (returned_attrs, *attrlist);
11087       else
11088         attrlist = &returned_attrs;
11089     }
11090
11091   /* Now TYPE has the actual type.  */
11092
11093   /* Did array size calculations overflow?  */
11094
11095   if (TREE_CODE (type) == ARRAY_TYPE
11096       && COMPLETE_TYPE_P (type)
11097       && TREE_OVERFLOW (TYPE_SIZE (type)))
11098     {
11099       error ("size of array `%s' is too large", name);
11100       /* If we proceed with the array type as it is, we'll eventually
11101          crash in tree_low_cst().  */
11102       type = error_mark_node;
11103     }
11104
11105   if ((decl_context == FIELD || decl_context == PARM)
11106       && !processing_template_decl 
11107       && variably_modified_type_p (type))
11108     {
11109       if (decl_context == FIELD)
11110         error ("data member may not have variably modified type `%T'", type);
11111       else
11112         error ("parameter may not have variably modified type `%T'", type);
11113       type = error_mark_node;
11114     }
11115
11116   if (explicitp == 1 || (explicitp && friendp))
11117     {
11118       /* [dcl.fct.spec] The explicit specifier shall only be used in
11119          declarations of constructors within a class definition.  */
11120       error ("only declarations of constructors can be `explicit'");
11121       explicitp = 0;
11122     }
11123
11124   if (RIDBIT_SETP (RID_MUTABLE, specbits))
11125     {
11126       if (current_class_name == NULL_TREE || decl_context == PARM || friendp)
11127         {
11128           error ("non-member `%s' cannot be declared `mutable'", name);
11129           RIDBIT_RESET (RID_MUTABLE, specbits);
11130         }
11131       else if (decl_context == TYPENAME || RIDBIT_SETP (RID_TYPEDEF, specbits))
11132         {
11133           error ("non-object member `%s' cannot be declared `mutable'", name);
11134           RIDBIT_RESET (RID_MUTABLE, specbits);
11135         }
11136       else if (TREE_CODE (type) == FUNCTION_TYPE
11137                || TREE_CODE (type) == METHOD_TYPE)
11138         {
11139           error ("function `%s' cannot be declared `mutable'", name);
11140           RIDBIT_RESET (RID_MUTABLE, specbits);
11141         }
11142       else if (staticp)
11143         {
11144           error ("static `%s' cannot be declared `mutable'", name);
11145           RIDBIT_RESET (RID_MUTABLE, specbits);
11146         }
11147       else if (type_quals & TYPE_QUAL_CONST)
11148         {
11149           error ("const `%s' cannot be declared `mutable'", name);
11150           RIDBIT_RESET (RID_MUTABLE, specbits);
11151         }
11152     }
11153
11154   if (declarator == NULL_TREE
11155       || TREE_CODE (declarator) == IDENTIFIER_NODE
11156       || (TREE_CODE (declarator) == TEMPLATE_ID_EXPR
11157           && (TREE_CODE (type) == FUNCTION_TYPE
11158               || TREE_CODE (type) == METHOD_TYPE)))
11159     /* OK */;
11160   else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11161     {
11162       error ("template-id `%D' used as a declarator", declarator);
11163       declarator = dname;
11164     }
11165   else
11166     /* Unexpected declarator format.  */
11167     abort ();
11168
11169   /* If this is declaring a typedef name, return a TYPE_DECL.  */
11170
11171   if (RIDBIT_SETP (RID_TYPEDEF, specbits) && decl_context != TYPENAME)
11172     {
11173       tree decl;
11174
11175       /* Note that the grammar rejects storage classes
11176          in typenames, fields or parameters.  */
11177       if (current_lang_name == lang_name_java)
11178         TYPE_FOR_JAVA (type) = 1;
11179
11180       if (decl_context == FIELD)
11181         {
11182           if (constructor_name_p (declarator, current_class_type))
11183             pedwarn ("ISO C++ forbids nested type `%D' with same name as enclosing class",
11184                         declarator);
11185           decl = build_lang_decl (TYPE_DECL, declarator, type);
11186         }
11187       else
11188         {
11189           decl = build_decl (TYPE_DECL, declarator, type);
11190           if (in_namespace || ctype)
11191             cp_error_at ("typedef name may not be a nested-name-specifier",
11192                          decl);
11193           if (!current_function_decl)
11194             DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
11195         }
11196       
11197       /* If the user declares "typedef struct {...} foo" then the
11198          struct will have an anonymous name.  Fill that name in now.
11199          Nothing can refer to it, so nothing needs know about the name
11200          change.  */
11201       if (type != error_mark_node
11202           && declarator
11203           && TYPE_NAME (type)
11204           && TREE_CODE (TYPE_NAME (type)) == TYPE_DECL
11205           && TYPE_ANONYMOUS_P (type)
11206           /* Don't do this if there are attributes.  */
11207           && (!attrlist || !*attrlist)
11208           && cp_type_quals (type) == TYPE_UNQUALIFIED)
11209         {
11210           tree oldname = TYPE_NAME (type);
11211           tree t;
11212
11213           /* Replace the anonymous name with the real name everywhere.  */
11214           lookup_tag_reverse (type, declarator);
11215           for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
11216             if (TYPE_NAME (t) == oldname)
11217               TYPE_NAME (t) = decl;
11218
11219           if (TYPE_LANG_SPECIFIC (type))
11220             TYPE_WAS_ANONYMOUS (type) = 1;
11221
11222           /* If this is a typedef within a template class, the nested
11223              type is a (non-primary) template.  The name for the
11224              template needs updating as well.  */
11225           if (TYPE_LANG_SPECIFIC (type) && CLASSTYPE_TEMPLATE_INFO (type))
11226             DECL_NAME (CLASSTYPE_TI_TEMPLATE (type))
11227               = TYPE_IDENTIFIER (type);
11228
11229           /* FIXME remangle member functions; member functions of a
11230              type with external linkage have external linkage.  */
11231         }
11232
11233       if (quals)
11234         {
11235           if (ctype == NULL_TREE)
11236             {
11237               if (TREE_CODE (type) != METHOD_TYPE)
11238                 cp_error_at ("invalid type qualifier for non-member function type", decl);
11239               else
11240                 ctype = TYPE_METHOD_BASETYPE (type);
11241             }
11242           if (ctype != NULL_TREE)
11243             grok_method_quals (ctype, decl, quals);
11244         }
11245
11246       if (RIDBIT_SETP (RID_SIGNED, specbits)
11247           || (typedef_decl && C_TYPEDEF_EXPLICITLY_SIGNED (typedef_decl)))
11248         C_TYPEDEF_EXPLICITLY_SIGNED (decl) = 1;
11249
11250       bad_specifiers (decl, "type", virtualp, quals != NULL_TREE,
11251                       inlinep, friendp, raises != NULL_TREE);
11252
11253       return decl;
11254     }
11255
11256   /* Detect the case of an array type of unspecified size
11257      which came, as such, direct from a typedef name.
11258      We must copy the type, so that the array's domain can be
11259      individually set by the object's initializer.  */
11260
11261   if (type && typedef_type
11262       && TREE_CODE (type) == ARRAY_TYPE && !TYPE_DOMAIN (type)
11263       && TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (typedef_type))
11264     type = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
11265
11266   /* Detect where we're using a typedef of function type to declare a
11267      function. last_function_parms will not be set, so we must create
11268      it now.  */
11269   
11270   if (type == typedef_type && TREE_CODE (type) == FUNCTION_TYPE)
11271     {
11272       tree decls = NULL_TREE;
11273       tree args;
11274
11275       for (args = TYPE_ARG_TYPES (type); args; args = TREE_CHAIN (args))
11276         {
11277           tree decl = cp_build_parm_decl (NULL_TREE, TREE_VALUE (args));
11278
11279           TREE_CHAIN (decl) = decls;
11280           decls = decl;
11281         }
11282       
11283       last_function_parms = nreverse (decls);
11284     }
11285
11286   /* If this is a type name (such as, in a cast or sizeof),
11287      compute the type and return it now.  */
11288
11289   if (decl_context == TYPENAME)
11290     {
11291       /* Note that the grammar rejects storage classes
11292          in typenames, fields or parameters.  */
11293       if (type_quals != TYPE_UNQUALIFIED)
11294         type_quals = TYPE_UNQUALIFIED;
11295
11296       /* Special case: "friend class foo" looks like a TYPENAME context.  */
11297       if (friendp)
11298         {
11299           if (type_quals != TYPE_UNQUALIFIED)
11300             {
11301               error ("type qualifiers specified for friend class declaration");
11302               type_quals = TYPE_UNQUALIFIED;
11303             }
11304           if (inlinep)
11305             {
11306               error ("`inline' specified for friend class declaration");
11307               inlinep = 0;
11308             }
11309
11310           if (!current_aggr)
11311             {
11312               /* Don't allow friend declaration without a class-key.  */
11313               if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
11314                 pedwarn ("template parameters cannot be friends");
11315               else if (TREE_CODE (type) == TYPENAME_TYPE)
11316                 pedwarn ("friend declaration requires class-key, "
11317                          "i.e. `friend class %T::%D'",
11318                          TYPE_CONTEXT (type), TYPENAME_TYPE_FULLNAME (type));
11319               else
11320                 pedwarn ("friend declaration requires class-key, "
11321                          "i.e. `friend %#T'",
11322                          type);
11323             }
11324
11325           /* Only try to do this stuff if we didn't already give up.  */
11326           if (type != integer_type_node)
11327             {
11328               /* A friendly class?  */
11329               if (current_class_type)
11330                 make_friend_class (current_class_type, TYPE_MAIN_VARIANT (type));
11331               else
11332                 error ("trying to make class `%T' a friend of global scope",
11333                           type);
11334
11335               type = void_type_node;
11336             }
11337         }
11338       else if (quals)
11339         {
11340           if (ctype == NULL_TREE)
11341             {
11342               if (TREE_CODE (type) != METHOD_TYPE)
11343                 error ("invalid qualifiers on non-member function type");
11344               else
11345                 ctype = TYPE_METHOD_BASETYPE (type);
11346             }
11347           if (ctype)
11348             {
11349               tree dummy = build_decl (TYPE_DECL, declarator, type);
11350               grok_method_quals (ctype, dummy, quals);
11351               type = TREE_TYPE (dummy);
11352             }
11353         }
11354
11355       return type;
11356     }
11357   else if (declarator == NULL_TREE && decl_context != PARM
11358            && decl_context != CATCHPARM
11359            && TREE_CODE (type) != UNION_TYPE
11360            && ! bitfield)
11361     {
11362       error ("abstract declarator `%T' used as declaration", type);
11363       declarator = make_anon_name ();
11364     }
11365
11366   /* `void' at top level (not within pointer)
11367      is allowed only in typedefs or type names.
11368      We don't complain about parms either, but that is because
11369      a better error message can be made later.  */
11370
11371   if (TREE_CODE (type) == VOID_TYPE && decl_context != PARM)
11372     {
11373       if (! declarator)
11374         error ("unnamed variable or field declared void");
11375       else if (TREE_CODE (declarator) == IDENTIFIER_NODE)
11376         {
11377           if (IDENTIFIER_OPNAME_P (declarator))
11378             abort ();
11379           else
11380             error ("variable or field `%s' declared void", name);
11381         }
11382       else
11383         error ("variable or field declared void");
11384       type = integer_type_node;
11385     }
11386
11387   /* Now create the decl, which may be a VAR_DECL, a PARM_DECL
11388      or a FUNCTION_DECL, depending on DECL_CONTEXT and TYPE.  */
11389
11390   if (decl_context == PARM || decl_context == CATCHPARM)
11391     {
11392       if (ctype || in_namespace)
11393         error ("cannot use `::' in parameter declaration");
11394
11395       /* A parameter declared as an array of T is really a pointer to T.
11396          One declared as a function is really a pointer to a function.
11397          One declared as a member is really a pointer to member.  */
11398
11399       if (TREE_CODE (type) == ARRAY_TYPE)
11400         {
11401           /* Transfer const-ness of array into that of type pointed to.  */
11402           type = build_pointer_type (TREE_TYPE (type));
11403           type_quals = TYPE_UNQUALIFIED;
11404         }
11405       else if (TREE_CODE (type) == FUNCTION_TYPE)
11406         type = build_pointer_type (type);
11407       else if (TREE_CODE (type) == OFFSET_TYPE)
11408         type = build_pointer_type (type);
11409     }
11410
11411   {
11412     register tree decl;
11413
11414     if (decl_context == PARM)
11415       {
11416         decl = cp_build_parm_decl (declarator, type);
11417
11418         bad_specifiers (decl, "parameter", virtualp, quals != NULL_TREE,
11419                         inlinep, friendp, raises != NULL_TREE);
11420       }
11421     else if (decl_context == FIELD)
11422       {
11423         if (type == error_mark_node)
11424           {
11425             /* Happens when declaring arrays of sizes which
11426                are error_mark_node, for example.  */
11427             decl = NULL_TREE;
11428           }
11429         else if (in_namespace && !friendp)
11430           {
11431             /* Something like struct S { int N::j; };  */
11432             error ("invalid use of `::'");
11433             decl = NULL_TREE;
11434           }
11435         else if (TREE_CODE (type) == FUNCTION_TYPE)
11436           {
11437             int publicp = 0;
11438             tree function_context;
11439
11440             /* We catch the others as conflicts with the builtin
11441                typedefs.  */
11442             if (friendp && declarator == ridpointers[(int) RID_SIGNED])
11443               {
11444                 error ("function `%D' cannot be declared friend",
11445                           declarator);
11446                 friendp = 0;
11447               }
11448
11449             if (friendp == 0)
11450               {
11451                 if (ctype == NULL_TREE)
11452                   ctype = current_class_type;
11453
11454                 if (ctype == NULL_TREE)
11455                   {
11456                     error ("can't make `%D' into a method -- not in a class",
11457                               declarator);
11458                     return void_type_node;
11459                   }
11460
11461                 /* ``A union may [ ... ] not [ have ] virtual functions.''
11462                    ARM 9.5 */
11463                 if (virtualp && TREE_CODE (ctype) == UNION_TYPE)
11464                   {
11465                     error ("function `%D' declared virtual inside a union",
11466                               declarator);
11467                     return void_type_node;
11468                   }
11469
11470                 if (declarator == ansi_opname (NEW_EXPR)
11471                     || declarator == ansi_opname (VEC_NEW_EXPR)
11472                     || declarator == ansi_opname (DELETE_EXPR)
11473                     || declarator == ansi_opname (VEC_DELETE_EXPR))
11474                   {
11475                     if (virtualp)
11476                       {
11477                         error ("`%D' cannot be declared virtual, since it is always static",
11478                                   declarator);
11479                         virtualp = 0;
11480                       }
11481                   }
11482                 else if (staticp < 2)
11483                   type = build_cplus_method_type (ctype, TREE_TYPE (type),
11484                                                   TYPE_ARG_TYPES (type));
11485               }
11486
11487             /* Tell grokfndecl if it needs to set TREE_PUBLIC on the node.  */
11488             function_context = (ctype != NULL_TREE) ?
11489               decl_function_context (TYPE_MAIN_DECL (ctype)) : NULL_TREE;
11490             publicp = (! friendp || ! staticp)
11491               && function_context == NULL_TREE;
11492             decl = grokfndecl (ctype, type,
11493                                TREE_CODE (declarator) != TEMPLATE_ID_EXPR
11494                                ? declarator : dname,
11495                                declarator,
11496                                virtualp, flags, quals, raises,
11497                                friendp ? -1 : 0, friendp, publicp, inlinep,
11498                                funcdef_flag, template_count, in_namespace);
11499             if (decl == NULL_TREE)
11500               return decl;
11501 #if 0
11502             /* This clobbers the attrs stored in `decl' from `attrlist'.  */
11503             /* The decl and setting of decl_attr is also turned off.  */
11504             decl = build_decl_attribute_variant (decl, decl_attr);
11505 #endif
11506
11507             /* [class.conv.ctor]
11508
11509                A constructor declared without the function-specifier
11510                explicit that can be called with a single parameter
11511                specifies a conversion from the type of its first
11512                parameter to the type of its class.  Such a constructor
11513                is called a converting constructor.  */
11514             if (explicitp == 2)
11515               DECL_NONCONVERTING_P (decl) = 1;
11516             else if (DECL_CONSTRUCTOR_P (decl))
11517               {
11518                 /* The constructor can be called with exactly one
11519                    parameter if there is at least one parameter, and
11520                    any subsequent parameters have default arguments.
11521                    Ignore any compiler-added parms.  */
11522                 tree arg_types = FUNCTION_FIRST_USER_PARMTYPE (decl);
11523
11524                 if (arg_types == void_list_node
11525                     || (arg_types
11526                         && TREE_CHAIN (arg_types)
11527                         && TREE_CHAIN (arg_types) != void_list_node
11528                         && !TREE_PURPOSE (TREE_CHAIN (arg_types))))
11529                   DECL_NONCONVERTING_P (decl) = 1;
11530               }
11531           }
11532         else if (TREE_CODE (type) == METHOD_TYPE)
11533           {
11534             /* We only get here for friend declarations of
11535                members of other classes.  */
11536             /* All method decls are public, so tell grokfndecl to set
11537                TREE_PUBLIC, also.  */
11538             decl = grokfndecl (ctype, type, declarator, declarator,
11539                                virtualp, flags, quals, raises,
11540                                friendp ? -1 : 0, friendp, 1, 0, funcdef_flag,
11541                                template_count, in_namespace);
11542             if (decl == NULL_TREE)
11543               return NULL_TREE;
11544           }
11545         else if (!staticp && ! processing_template_decl
11546                  && !COMPLETE_TYPE_P (complete_type (type))
11547                  && (TREE_CODE (type) != ARRAY_TYPE || initialized == 0))
11548           {
11549             if (declarator)
11550               error ("field `%D' has incomplete type", declarator);
11551             else
11552               error ("name `%T' has incomplete type", type);
11553
11554             /* If we're instantiating a template, tell them which
11555                instantiation made the field's type be incomplete.  */
11556             if (current_class_type
11557                 && TYPE_NAME (current_class_type)
11558                 && IDENTIFIER_TEMPLATE (TYPE_IDENTIFIER (current_class_type))
11559                 && declspecs && TREE_VALUE (declspecs)
11560                 && TREE_TYPE (TREE_VALUE (declspecs)) == type)
11561               error ("  in instantiation of template `%T'",
11562                         current_class_type);
11563
11564             type = error_mark_node;
11565             decl = NULL_TREE;
11566           }
11567         else
11568           {
11569             if (friendp)
11570               {
11571                 error ("`%s' is neither function nor member function; cannot be declared friend",
11572                        IDENTIFIER_POINTER (declarator));
11573                 friendp = 0;
11574               }
11575             decl = NULL_TREE;
11576           }
11577
11578         if (friendp)
11579           {
11580             /* Friends are treated specially.  */
11581             if (ctype == current_class_type)
11582               warning ("member functions are implicitly friends of their class");
11583             else
11584               {
11585                 tree t = NULL_TREE;
11586                 if (decl && DECL_NAME (decl))
11587                   {
11588                     if (template_class_depth (current_class_type) == 0)
11589                       {
11590                         decl
11591                           = check_explicit_specialization
11592                           (declarator, decl,
11593                            template_count, 2 * (funcdef_flag != 0) + 4);
11594                         if (decl == error_mark_node)
11595                           return error_mark_node;
11596                       }
11597                     
11598                     t = do_friend (ctype, declarator, decl,
11599                                    last_function_parms, *attrlist,
11600                                    flags, quals, funcdef_flag);
11601                   }
11602                 if (t && funcdef_flag)
11603                   return t;
11604   
11605                 return void_type_node;
11606               }
11607           }
11608
11609         /* Structure field.  It may not be a function, except for C++ */
11610
11611         if (decl == NULL_TREE)
11612           {
11613             if (initialized)
11614               {
11615                 if (!staticp)
11616                   {
11617                     /* An attempt is being made to initialize a non-static
11618                        member.  But, from [class.mem]:
11619
11620                        4 A member-declarator can contain a
11621                        constant-initializer only if it declares a static
11622                        member (_class.static_) of integral or enumeration
11623                        type, see _class.static.data_.
11624
11625                        This used to be relatively common practice, but
11626                        the rest of the compiler does not correctly
11627                        handle the initialization unless the member is
11628                        static so we make it static below.  */
11629                     pedwarn ("ISO C++ forbids initialization of member `%D'",
11630                                 declarator);
11631                     pedwarn ("making `%D' static", declarator);
11632                     staticp = 1;
11633                   }
11634
11635                 if (uses_template_parms (type))
11636                   /* We'll check at instantiation time.  */
11637                   ;
11638                 else if (check_static_variable_definition (declarator,
11639                                                            type))
11640                   /* If we just return the declaration, crashes
11641                      will sometimes occur.  We therefore return
11642                      void_type_node, as if this was a friend
11643                      declaration, to cause callers to completely
11644                      ignore this declaration.  */
11645                   return void_type_node;
11646               }
11647
11648             /* 9.2p13 [class.mem] */
11649             if (constructor_name_p (declarator, current_class_type)
11650                 /* The standard does not allow non-static data members
11651                    here either, but we agreed at the 10/99 meeting
11652                    to change that in TC 1 so that they are allowed in
11653                    classes with no user-defined constructors.  */
11654                 && staticp)
11655               pedwarn ("ISO C++ forbids static data member `%D' with same name as enclosing class",
11656                           declarator);
11657
11658             if (staticp)
11659               {
11660                 /* C++ allows static class members.  All other work
11661                    for this is done by grokfield.  */
11662                 decl = build_lang_decl (VAR_DECL, declarator, type);
11663                 TREE_STATIC (decl) = 1;
11664                 /* In class context, 'static' means public access.  */
11665                 TREE_PUBLIC (decl) = DECL_EXTERNAL (decl) = 1;
11666               }
11667             else
11668               {
11669                 decl = build_decl (FIELD_DECL, declarator, type);
11670                 DECL_NONADDRESSABLE_P (decl) = bitfield;
11671                 if (RIDBIT_SETP (RID_MUTABLE, specbits))
11672                   {
11673                     DECL_MUTABLE_P (decl) = 1;
11674                     RIDBIT_RESET (RID_MUTABLE, specbits);
11675                   }
11676               }
11677
11678             bad_specifiers (decl, "field", virtualp, quals != NULL_TREE,
11679                             inlinep, friendp, raises != NULL_TREE);
11680           }
11681       }
11682     else if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
11683       {
11684         tree original_name;
11685         int publicp = 0;
11686
11687         if (! declarator)
11688           return NULL_TREE;
11689
11690         if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
11691           original_name = dname;
11692         else
11693           original_name = declarator;
11694
11695         if (RIDBIT_SETP (RID_AUTO, specbits))
11696           error ("storage class `auto' invalid for function `%s'", name);
11697         else if (RIDBIT_SETP (RID_REGISTER, specbits))
11698           error ("storage class `register' invalid for function `%s'", name);
11699         else if (RIDBIT_SETP (RID_THREAD, specbits))
11700           error ("storage class `__thread' invalid for function `%s'", name);
11701
11702         /* Function declaration not at top level.
11703            Storage classes other than `extern' are not allowed
11704            and `extern' makes no difference.  */
11705         if (! toplevel_bindings_p ()
11706             && (RIDBIT_SETP (RID_STATIC, specbits)
11707                 || RIDBIT_SETP (RID_INLINE, specbits))
11708             && pedantic)
11709           {
11710             if (RIDBIT_SETP (RID_STATIC, specbits))
11711               pedwarn ("storage class `static' invalid for function `%s' declared out of global scope", name);
11712             else
11713               pedwarn ("storage class `inline' invalid for function `%s' declared out of global scope", name);
11714           }
11715
11716         if (ctype == NULL_TREE)
11717           {
11718             if (virtualp)
11719               {
11720                 error ("virtual non-class function `%s'", name);
11721                 virtualp = 0;
11722               }
11723           }
11724         else if (TREE_CODE (type) == FUNCTION_TYPE && staticp < 2)
11725           type = build_cplus_method_type (ctype, TREE_TYPE (type),
11726                                           TYPE_ARG_TYPES (type));
11727
11728         /* Record presence of `static'.  */
11729         publicp = (ctype != NULL_TREE
11730                    || RIDBIT_SETP (RID_EXTERN, specbits)
11731                    || !RIDBIT_SETP (RID_STATIC, specbits));
11732
11733         decl = grokfndecl (ctype, type, original_name, declarator,
11734                            virtualp, flags, quals, raises,
11735                            1, friendp,
11736                            publicp, inlinep, funcdef_flag,
11737                            template_count, in_namespace);
11738         if (decl == NULL_TREE)
11739           return NULL_TREE;
11740
11741         if (staticp == 1)
11742           {
11743             int invalid_static = 0;
11744
11745             /* Don't allow a static member function in a class, and forbid
11746                declaring main to be static.  */
11747             if (TREE_CODE (type) == METHOD_TYPE)
11748               {
11749                 pedwarn ("cannot declare member function `%D' to have static linkage", decl);
11750                 invalid_static = 1;
11751               }
11752             else if (current_function_decl)
11753               {
11754                 /* FIXME need arm citation */
11755                 error ("cannot declare static function inside another function");
11756                 invalid_static = 1;
11757               }
11758
11759             if (invalid_static)
11760               {
11761                 staticp = 0;
11762                 RIDBIT_RESET (RID_STATIC, specbits);
11763               }
11764           }
11765       }
11766     else
11767       {
11768         /* It's a variable.  */
11769
11770         /* An uninitialized decl with `extern' is a reference.  */
11771         decl = grokvardecl (type, declarator, &specbits,
11772                             initialized,
11773                             (type_quals & TYPE_QUAL_CONST) != 0,
11774                             ctype ? ctype : in_namespace);
11775         bad_specifiers (decl, "variable", virtualp, quals != NULL_TREE,
11776                         inlinep, friendp, raises != NULL_TREE);
11777
11778         if (ctype)
11779           {
11780             DECL_CONTEXT (decl) = ctype;
11781             if (staticp == 1)
11782               {
11783                 pedwarn ("`static' may not be used when defining (as opposed to declaring) a static data member");
11784                 staticp = 0;
11785                 RIDBIT_RESET (RID_STATIC, specbits);
11786               }
11787             if (RIDBIT_SETP (RID_REGISTER, specbits) && TREE_STATIC (decl))
11788               {
11789                 error ("static member `%D' declared `register'", decl);
11790                 RIDBIT_RESET (RID_REGISTER, specbits);
11791               }
11792             if (RIDBIT_SETP (RID_EXTERN, specbits) && pedantic)
11793               {
11794                 pedwarn ("cannot explicitly declare member `%#D' to have extern linkage",
11795                             decl);
11796                 RIDBIT_RESET (RID_EXTERN, specbits);
11797               }
11798           }
11799       }
11800
11801     my_friendly_assert (!RIDBIT_SETP (RID_MUTABLE, specbits), 19990927);
11802
11803     /* Record `register' declaration for warnings on &
11804        and in case doing stupid register allocation.  */
11805
11806     if (RIDBIT_SETP (RID_REGISTER, specbits))
11807       DECL_REGISTER (decl) = 1;
11808
11809     if (RIDBIT_SETP (RID_EXTERN, specbits))
11810       DECL_THIS_EXTERN (decl) = 1;
11811
11812     if (RIDBIT_SETP (RID_STATIC, specbits))
11813       DECL_THIS_STATIC (decl) = 1;
11814
11815     /* Record constancy and volatility.  There's no need to do this
11816        when processing a template; we'll do this for the instantiated
11817        declaration based on the type of DECL.  */
11818     if (!processing_template_decl)
11819       c_apply_type_quals_to_decl (type_quals, decl);
11820
11821     return decl;
11822   }
11823 }
11824 \f
11825 /* Subroutine of start_function.  Ensure that each of the parameter
11826    types (as listed in PARMS) is complete, as is required for a
11827    function definition.  */
11828
11829 static void
11830 require_complete_types_for_parms (tree parms)
11831 {
11832   for (; parms; parms = TREE_CHAIN (parms))
11833     {
11834       if (VOID_TYPE_P (TREE_TYPE (parms)))
11835         /* grokparms will have already issued an error */
11836         TREE_TYPE (parms) = error_mark_node;
11837       else if (complete_type_or_else (TREE_TYPE (parms), parms))
11838         {
11839           layout_decl (parms, 0);
11840           DECL_ARG_TYPE (parms) = type_passed_as (TREE_TYPE (parms));
11841         }
11842       else
11843         TREE_TYPE (parms) = error_mark_node;
11844     }
11845 }
11846
11847 /* Returns nonzero if T is a local variable.  */
11848
11849 int
11850 local_variable_p (tree t)
11851 {
11852   if ((TREE_CODE (t) == VAR_DECL
11853        /* A VAR_DECL with a context that is a _TYPE is a static data
11854           member.  */
11855        && !TYPE_P (CP_DECL_CONTEXT (t))
11856        /* Any other non-local variable must be at namespace scope.  */
11857        && !DECL_NAMESPACE_SCOPE_P (t))
11858       || (TREE_CODE (t) == PARM_DECL))
11859     return 1;
11860
11861   return 0;
11862 }
11863
11864 /* Returns nonzero if T is an automatic local variable or a label.
11865    (These are the declarations that need to be remapped when the code
11866    containing them is duplicated.)  */
11867
11868 int
11869 nonstatic_local_decl_p (tree t)
11870 {
11871   return ((local_variable_p (t) && !TREE_STATIC (t))
11872           || TREE_CODE (t) == LABEL_DECL
11873           || TREE_CODE (t) == RESULT_DECL);
11874 }
11875
11876 /* Like local_variable_p, but suitable for use as a tree-walking
11877    function.  */
11878
11879 static tree
11880 local_variable_p_walkfn (tree* tp,
11881                          int* walk_subtrees ATTRIBUTE_UNUSED ,
11882                          void* data ATTRIBUTE_UNUSED )
11883 {
11884   return ((local_variable_p (*tp) && !DECL_ARTIFICIAL (*tp))
11885           ? *tp : NULL_TREE);
11886 }
11887
11888 /* Check that ARG, which is a default-argument expression for a
11889    parameter DECL, is valid.  Returns ARG, or ERROR_MARK_NODE, if
11890    something goes wrong.  DECL may also be a _TYPE node, rather than a
11891    DECL, if there is no DECL available.  */
11892
11893 tree
11894 check_default_argument (tree decl, tree arg)
11895 {
11896   tree var;
11897   tree decl_type;
11898
11899   if (TREE_CODE (arg) == DEFAULT_ARG)
11900     /* We get a DEFAULT_ARG when looking at an in-class declaration
11901        with a default argument.  Ignore the argument for now; we'll
11902        deal with it after the class is complete.  */
11903     return arg;
11904
11905   if (processing_template_decl || uses_template_parms (arg))
11906     /* We don't do anything checking until instantiation-time.  Note
11907        that there may be uninstantiated arguments even for an
11908        instantiated function, since default arguments are not
11909        instantiated until they are needed.  */
11910     return arg;
11911
11912   if (TYPE_P (decl))
11913     {
11914       decl_type = decl;
11915       decl = NULL_TREE;
11916     }
11917   else
11918     decl_type = TREE_TYPE (decl);
11919
11920   if (arg == error_mark_node
11921       || decl == error_mark_node
11922       || TREE_TYPE (arg) == error_mark_node
11923       || decl_type == error_mark_node)
11924     /* Something already went wrong.  There's no need to check
11925        further.  */
11926     return error_mark_node;
11927
11928   /* [dcl.fct.default]
11929
11930      A default argument expression is implicitly converted to the
11931      parameter type.  */
11932   if (!TREE_TYPE (arg)
11933       || !can_convert_arg (decl_type, TREE_TYPE (arg), arg))
11934     {
11935       if (decl)
11936         error ("default argument for `%#D' has type `%T'",
11937                   decl, TREE_TYPE (arg));
11938       else
11939         error ("default argument for parameter of type `%T' has type `%T'",
11940                   decl_type, TREE_TYPE (arg));
11941
11942       return error_mark_node;
11943     }
11944
11945   /* [dcl.fct.default]
11946
11947      Local variables shall not be used in default argument
11948      expressions.
11949
11950      The keyword `this' shall not be used in a default argument of a
11951      member function.  */
11952   var = walk_tree_without_duplicates (&arg, local_variable_p_walkfn,
11953                                       NULL);
11954   if (var)
11955     {
11956       error ("default argument `%E' uses local variable `%D'",
11957                 arg, var);
11958       return error_mark_node;
11959     }
11960
11961   /* All is well.  */
11962   return arg;
11963 }
11964
11965 /* Decode the list of parameter types for a function type.
11966    Given the list of things declared inside the parens,
11967    return a list of types.
11968
11969    We determine whether ellipsis parms are used by PARMLIST_ELLIPSIS_P
11970    flag. If unset, we append void_list_node. A parmlist declared
11971    as `(void)' is accepted as the empty parmlist.
11972
11973    Also set last_function_parms to the chain of PARM_DECLs.  */
11974
11975 static tree
11976 grokparms (tree first_parm)
11977 {
11978   tree result = NULL_TREE;
11979   tree decls = NULL_TREE;
11980   int ellipsis = !first_parm || PARMLIST_ELLIPSIS_P (first_parm);
11981   tree parm, chain;
11982   int any_error = 0;
11983
11984   my_friendly_assert (!first_parm || TREE_PARMLIST (first_parm), 20001115);
11985
11986   for (parm = first_parm; parm != NULL_TREE; parm = chain)
11987     {
11988       tree type = NULL_TREE;
11989       tree decl = TREE_VALUE (parm);
11990       tree init = TREE_PURPOSE (parm);
11991       tree specs, attrs;
11992
11993       chain = TREE_CHAIN (parm);
11994       /* @@ weak defense against parse errors.  */
11995       if (TREE_CODE (decl) != VOID_TYPE
11996           && TREE_CODE (decl) != TREE_LIST)
11997         {
11998           /* Give various messages as the need arises.  */
11999           if (TREE_CODE (decl) == STRING_CST)
12000             error ("invalid string constant `%E'", decl);
12001           else if (TREE_CODE (decl) == INTEGER_CST)
12002             error ("invalid integer constant in parameter list, did you forget to give parameter name?");
12003           continue;
12004         }
12005
12006       if (parm == void_list_node)
12007         break;
12008
12009       split_specs_attrs (TREE_PURPOSE (decl), &specs, &attrs);
12010       decl = grokdeclarator (TREE_VALUE (decl), specs,
12011                              PARM, init != NULL_TREE, &attrs);
12012       if (! decl || TREE_TYPE (decl) == error_mark_node)
12013         continue;
12014
12015       if (attrs)
12016         cplus_decl_attributes (&decl, attrs, 0);
12017
12018       type = TREE_TYPE (decl);
12019       if (VOID_TYPE_P (type))
12020         {
12021           if (same_type_p (type, void_type_node)
12022               && !DECL_NAME (decl) && !result && !chain && !ellipsis)
12023             /* this is a parmlist of `(void)', which is ok.  */
12024             break;
12025           cxx_incomplete_type_error (decl, type);
12026           /* It's not a good idea to actually create parameters of
12027              type `void'; other parts of the compiler assume that a
12028              void type terminates the parameter list.  */
12029           type = error_mark_node;
12030           TREE_TYPE (decl) = error_mark_node;
12031         }
12032
12033       if (type != error_mark_node)
12034         {
12035           /* Top-level qualifiers on the parameters are
12036              ignored for function types.  */
12037           type = TYPE_MAIN_VARIANT (type);
12038           if (TREE_CODE (type) == METHOD_TYPE)
12039             {
12040               error ("parameter `%D' invalidly declared method type", decl);
12041               type = build_pointer_type (type);
12042               TREE_TYPE (decl) = type;
12043             }
12044           else if (TREE_CODE (type) == OFFSET_TYPE)
12045             {
12046               error ("parameter `%D' invalidly declared offset type", decl);
12047               type = build_pointer_type (type);
12048               TREE_TYPE (decl) = type;
12049             }
12050           else if (abstract_virtuals_error (decl, type))
12051             any_error = 1;  /* Seems like a good idea.  */
12052           else if (POINTER_TYPE_P (type))
12053             {
12054               /* [dcl.fct]/6, parameter types cannot contain pointers
12055                  (references) to arrays of unknown bound.  */
12056               tree t = TREE_TYPE (type);
12057               int ptr = TYPE_PTR_P (type);
12058
12059               while (1)
12060                 {
12061                   if (TYPE_PTR_P (t))
12062                     ptr = 1;
12063                   else if (TREE_CODE (t) != ARRAY_TYPE)
12064                     break;
12065                   else if (!TYPE_DOMAIN (t))
12066                     break;
12067                   t = TREE_TYPE (t);
12068                 }
12069               if (TREE_CODE (t) == ARRAY_TYPE)
12070                 error ("parameter `%D' includes %s to array of unknown bound `%T'",
12071                           decl, ptr ? "pointer" : "reference", t);
12072             }
12073
12074           if (!any_error && init)
12075             init = check_default_argument (decl, init);
12076           else
12077             init = NULL_TREE;
12078         }
12079
12080       TREE_CHAIN (decl) = decls;
12081       decls = decl;
12082       result = tree_cons (init, type, result);
12083     }
12084   decls = nreverse (decls);
12085   result = nreverse (result);
12086   if (!ellipsis)
12087     result = chainon (result, void_list_node);
12088   last_function_parms = decls;
12089
12090   return result;
12091 }
12092
12093 \f
12094 /* D is a constructor or overloaded `operator='.
12095
12096    Let T be the class in which D is declared. Then, this function
12097    returns:
12098
12099    -1 if D's is an ill-formed constructor or copy assignment operator
12100       whose first parameter is of type `T'.
12101    0  if D is not a copy constructor or copy assignment
12102       operator.
12103    1  if D is a copy constructor or copy assignment operator whose
12104       first parameter is a reference to const qualified T.
12105    2  if D is a copy constructor or copy assignment operator whose
12106       first parameter is a reference to non-const qualified T.
12107
12108    This function can be used as a predicate. Positive values indicate
12109    a copy constructor and nonzero values indicate a copy assignment
12110    operator.  */
12111
12112 int
12113 copy_fn_p (tree d)
12114 {
12115   tree args;
12116   tree arg_type;
12117   int result = 1;
12118   
12119   my_friendly_assert (DECL_FUNCTION_MEMBER_P (d), 20011208);
12120
12121   if (DECL_TEMPLATE_INFO (d) && is_member_template (DECL_TI_TEMPLATE (d)))
12122     /* Instantiations of template member functions are never copy
12123        functions.  Note that member functions of templated classes are
12124        represented as template functions internally, and we must
12125        accept those as copy functions.  */
12126     return 0;
12127     
12128   args = FUNCTION_FIRST_USER_PARMTYPE (d);
12129   if (!args)
12130     return 0;
12131
12132   arg_type = TREE_VALUE (args);
12133
12134   if (TYPE_MAIN_VARIANT (arg_type) == DECL_CONTEXT (d))
12135     {
12136       /* Pass by value copy assignment operator.  */
12137       result = -1;
12138     }
12139   else if (TREE_CODE (arg_type) == REFERENCE_TYPE
12140            && TYPE_MAIN_VARIANT (TREE_TYPE (arg_type)) == DECL_CONTEXT (d))
12141     {
12142       if (CP_TYPE_CONST_P (TREE_TYPE (arg_type)))
12143         result = 2;
12144     }
12145   else
12146     return 0;
12147   
12148   args = TREE_CHAIN (args);
12149
12150   if (args && args != void_list_node && !TREE_PURPOSE (args))
12151     /* There are more non-optional args.  */
12152     return 0;
12153
12154   return result;
12155 }
12156
12157 /* Remember any special properties of member function DECL.  */
12158
12159 void grok_special_member_properties (tree decl)
12160 {
12161   if (!DECL_NONSTATIC_MEMBER_FUNCTION_P(decl))
12162     ; /* Not special.  */
12163   else if (DECL_CONSTRUCTOR_P (decl))
12164     {
12165       int ctor = copy_fn_p (decl);
12166       
12167       if (ctor > 0)
12168         {
12169           /* [class.copy]
12170               
12171              A non-template constructor for class X is a copy
12172              constructor if its first parameter is of type X&, const
12173              X&, volatile X& or const volatile X&, and either there
12174              are no other parameters or else all other parameters have
12175              default arguments.  */
12176           TYPE_HAS_INIT_REF (DECL_CONTEXT (decl)) = 1;
12177           if (ctor > 1)
12178             TYPE_HAS_CONST_INIT_REF (DECL_CONTEXT (decl)) = 1;
12179         }
12180       else if (sufficient_parms_p (FUNCTION_FIRST_USER_PARMTYPE (decl)))
12181         TYPE_HAS_DEFAULT_CONSTRUCTOR (DECL_CONTEXT (decl)) = 1;
12182     }
12183   else if (DECL_OVERLOADED_OPERATOR_P (decl) == NOP_EXPR)
12184     {
12185       /* [class.copy]
12186           
12187          A non-template assignment operator for class X is a copy
12188          assignment operator if its parameter is of type X, X&, const
12189          X&, volatile X& or const volatile X&.  */
12190       
12191       int assop = copy_fn_p (decl);
12192       
12193       if (assop)
12194         {
12195           TYPE_HAS_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12196           if (assop != 1)
12197             TYPE_HAS_CONST_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12198           if (DECL_PURE_VIRTUAL_P (decl))
12199             TYPE_HAS_ABSTRACT_ASSIGN_REF (DECL_CONTEXT (decl)) = 1;
12200         }
12201     }
12202 }
12203
12204 /* Check a constructor DECL has the correct form.  Complains
12205    if the class has a constructor of the form X(X).  */
12206
12207 int
12208 grok_ctor_properties (tree ctype, tree decl)
12209 {
12210   int ctor_parm = copy_fn_p (decl);
12211
12212   if (ctor_parm < 0)
12213     {
12214       /* [class.copy]
12215           
12216          A declaration of a constructor for a class X is ill-formed if
12217          its first parameter is of type (optionally cv-qualified) X
12218          and either there are no other parameters or else all other
12219          parameters have default arguments.
12220           
12221          We *don't* complain about member template instantiations that
12222          have this form, though; they can occur as we try to decide
12223          what constructor to use during overload resolution.  Since
12224          overload resolution will never prefer such a constructor to
12225          the non-template copy constructor (which is either explicitly
12226          or implicitly defined), there's no need to worry about their
12227          existence.  Theoretically, they should never even be
12228          instantiated, but that's hard to forestall.  */
12229       error ("invalid constructor; you probably meant `%T (const %T&)'",
12230                 ctype, ctype);
12231       SET_IDENTIFIER_ERROR_LOCUS (DECL_NAME (decl), ctype);
12232       return 0;
12233     }
12234   
12235   return 1;
12236 }
12237
12238 /* An operator with this code is unary, but can also be binary.  */
12239
12240 static int
12241 ambi_op_p (enum tree_code code)
12242 {
12243   return (code == INDIRECT_REF
12244           || code == ADDR_EXPR
12245           || code == CONVERT_EXPR
12246           || code == NEGATE_EXPR
12247           || code == PREINCREMENT_EXPR
12248           || code == PREDECREMENT_EXPR);
12249 }
12250
12251 /* An operator with this name can only be unary.  */
12252
12253 static int
12254 unary_op_p (enum tree_code code)
12255 {
12256   return (code == TRUTH_NOT_EXPR
12257           || code == BIT_NOT_EXPR
12258           || code == COMPONENT_REF
12259           || code == TYPE_EXPR);
12260 }
12261
12262 /* Do a little sanity-checking on how they declared their operator.  */
12263
12264 void
12265 grok_op_properties (tree decl, int friendp)
12266 {
12267   tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
12268   tree argtype;
12269   int methodp = (TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
12270   tree name = DECL_NAME (decl);
12271   enum tree_code operator_code;
12272   int arity;
12273
12274   /* Count the number of arguments.  */
12275   for (argtype = argtypes, arity = 0;
12276        argtype && argtype != void_list_node;
12277        argtype = TREE_CHAIN (argtype))
12278     ++arity;
12279
12280   if (current_class_type == NULL_TREE)
12281     friendp = 1;
12282
12283   if (DECL_CONV_FN_P (decl))
12284     operator_code = TYPE_EXPR;
12285   else
12286     do
12287       {
12288 #define DEF_OPERATOR(NAME, CODE, MANGLING, ARITY, ASSN_P)       \
12289         if (ansi_opname (CODE) == name)                         \
12290           {                                                     \
12291             operator_code = (CODE);                             \
12292             break;                                              \
12293           }                                                     \
12294         else if (ansi_assopname (CODE) == name)                 \
12295           {                                                     \
12296             operator_code = (CODE);                             \
12297             DECL_ASSIGNMENT_OPERATOR_P (decl) = 1;              \
12298             break;                                              \
12299           }
12300
12301 #include "operators.def"
12302 #undef DEF_OPERATOR
12303
12304         abort ();
12305       }
12306     while (0);
12307   my_friendly_assert (operator_code != LAST_CPLUS_TREE_CODE, 20000526);
12308   SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12309
12310   if (! friendp)
12311     {
12312       switch (operator_code)
12313         {
12314         case CALL_EXPR:
12315           TYPE_OVERLOADS_CALL_EXPR (current_class_type) = 1;
12316           break;
12317
12318         case ARRAY_REF:
12319           TYPE_OVERLOADS_ARRAY_REF (current_class_type) = 1;
12320           break;
12321
12322         case COMPONENT_REF:
12323         case MEMBER_REF:
12324           TYPE_OVERLOADS_ARROW (current_class_type) = 1;
12325           break;
12326
12327         case NEW_EXPR:
12328           TYPE_HAS_NEW_OPERATOR (current_class_type) = 1;
12329           break;
12330
12331         case DELETE_EXPR:
12332           TYPE_GETS_DELETE (current_class_type) |= 1;
12333           break;
12334
12335         case VEC_NEW_EXPR:
12336           TYPE_HAS_ARRAY_NEW_OPERATOR (current_class_type) = 1;
12337           break;
12338
12339         case VEC_DELETE_EXPR:
12340           TYPE_GETS_DELETE (current_class_type) |= 2;
12341           break;
12342
12343         default:
12344           break;
12345         }
12346     }
12347
12348   if (operator_code == NEW_EXPR || operator_code == VEC_NEW_EXPR)
12349     {
12350       /* When the compiler encounters the definition of A::operator new, it
12351          doesn't look at the class declaration to find out if it's static.  */
12352       if (methodp)
12353         revert_static_member_fn (decl);
12354
12355       TREE_TYPE (decl) = coerce_new_type (TREE_TYPE (decl));
12356     }
12357   else if (operator_code == DELETE_EXPR || operator_code == VEC_DELETE_EXPR)
12358     {
12359       if (methodp)
12360         revert_static_member_fn (decl);
12361
12362       TREE_TYPE (decl) = coerce_delete_type (TREE_TYPE (decl));
12363     }
12364   else
12365     {
12366       /* An operator function must either be a non-static member function
12367          or have at least one parameter of a class, a reference to a class,
12368          an enumeration, or a reference to an enumeration.  13.4.0.6 */
12369       if (! methodp || DECL_STATIC_FUNCTION_P (decl))
12370         {
12371           if (operator_code == TYPE_EXPR
12372               || operator_code == CALL_EXPR
12373               || operator_code == COMPONENT_REF
12374               || operator_code == ARRAY_REF
12375               || operator_code == NOP_EXPR)
12376             error ("`%D' must be a nonstatic member function", decl);
12377           else
12378             {
12379               tree p = argtypes;
12380
12381               if (DECL_STATIC_FUNCTION_P (decl))
12382                 error ("`%D' must be either a non-static member function or a non-member function", decl);
12383
12384               if (p)
12385                 for (; TREE_CODE (TREE_VALUE (p)) != VOID_TYPE ; p = TREE_CHAIN (p))
12386                   {
12387                     tree arg = TREE_VALUE (p);
12388                     if (TREE_CODE (arg) == REFERENCE_TYPE)
12389                       arg = TREE_TYPE (arg);
12390
12391                     /* This lets bad template code slip through.  */
12392                     if (IS_AGGR_TYPE (arg)
12393                         || TREE_CODE (arg) == ENUMERAL_TYPE
12394                         || TREE_CODE (arg) == TEMPLATE_TYPE_PARM
12395                         || TREE_CODE (arg) == BOUND_TEMPLATE_TEMPLATE_PARM)
12396                       goto foundaggr;
12397                   }
12398               error
12399                 ("`%D' must have an argument of class or enumerated type",
12400                  decl);
12401             foundaggr:
12402               ;
12403             }
12404         }
12405
12406       if (operator_code == CALL_EXPR)
12407         return;                 /* No restrictions on args.  */
12408
12409       if (IDENTIFIER_TYPENAME_P (name) && ! DECL_TEMPLATE_INFO (decl))
12410         {
12411           tree t = TREE_TYPE (name);
12412           if (! friendp)
12413             {
12414               int ref = (TREE_CODE (t) == REFERENCE_TYPE);
12415               const char *what = 0;
12416
12417               if (ref)
12418                 t = TYPE_MAIN_VARIANT (TREE_TYPE (t));
12419
12420               if (TREE_CODE (t) == VOID_TYPE)
12421                 what = "void";
12422               else if (t == current_class_type)
12423                 what = "the same type";
12424               /* Don't force t to be complete here.  */
12425               else if (IS_AGGR_TYPE (t)
12426                        && COMPLETE_TYPE_P (t)
12427                        && DERIVED_FROM_P (t, current_class_type))
12428                 what = "a base class";
12429
12430               if (what)
12431                 warning ("conversion to %s%s will never use a type conversion operator",
12432                          ref ? "a reference to " : "", what);
12433             }
12434         }
12435       if (operator_code == COND_EXPR)
12436         {
12437           /* 13.4.0.3 */
12438           error ("ISO C++ prohibits overloading operator ?:");
12439         }
12440       else if (ambi_op_p (operator_code))
12441         {
12442           if (arity == 1)
12443             /* We pick the one-argument operator codes by default, so
12444                we don't have to change anything.  */
12445             ;
12446           else if (arity == 2)
12447             {
12448               /* If we thought this was a unary operator, we now know
12449                  it to be a binary operator.  */
12450               switch (operator_code)
12451                 {
12452                 case INDIRECT_REF:
12453                   operator_code = MULT_EXPR;
12454                   break;
12455
12456                 case ADDR_EXPR:
12457                   operator_code = BIT_AND_EXPR;
12458                   break;
12459
12460                 case CONVERT_EXPR:
12461                   operator_code = PLUS_EXPR;
12462                   break;
12463
12464                 case NEGATE_EXPR:
12465                   operator_code = MINUS_EXPR;
12466                   break;
12467
12468                 case PREINCREMENT_EXPR:
12469                   operator_code = POSTINCREMENT_EXPR;
12470                   break;
12471
12472                 case PREDECREMENT_EXPR:
12473                   operator_code = POSTDECREMENT_EXPR;
12474                   break;
12475
12476                 default:
12477                   abort ();
12478                 }
12479
12480               SET_OVERLOADED_OPERATOR_CODE (decl, operator_code);
12481
12482               if ((operator_code == POSTINCREMENT_EXPR
12483                    || operator_code == POSTDECREMENT_EXPR)
12484                   && ! processing_template_decl
12485                   && ! same_type_p (TREE_VALUE (TREE_CHAIN (argtypes)), integer_type_node))
12486                 {
12487                   if (methodp)
12488                     error ("postfix `%D' must take `int' as its argument",
12489                               decl);
12490                   else
12491                     error
12492                       ("postfix `%D' must take `int' as its second argument",
12493                        decl);
12494                 }
12495             }
12496           else
12497             {
12498               if (methodp)
12499                 error ("`%D' must take either zero or one argument", decl);
12500               else
12501                 error ("`%D' must take either one or two arguments", decl);
12502             }
12503
12504           /* More Effective C++ rule 6.  */
12505           if (warn_ecpp
12506               && (operator_code == POSTINCREMENT_EXPR
12507                   || operator_code == POSTDECREMENT_EXPR
12508                   || operator_code == PREINCREMENT_EXPR
12509                   || operator_code == PREDECREMENT_EXPR))
12510             {
12511               tree arg = TREE_VALUE (argtypes);
12512               tree ret = TREE_TYPE (TREE_TYPE (decl));
12513               if (methodp || TREE_CODE (arg) == REFERENCE_TYPE)
12514                 arg = TREE_TYPE (arg);
12515               arg = TYPE_MAIN_VARIANT (arg);
12516               if (operator_code == PREINCREMENT_EXPR
12517                   || operator_code == PREDECREMENT_EXPR)
12518                 {
12519                   if (TREE_CODE (ret) != REFERENCE_TYPE
12520                       || !same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (ret)),
12521                                        arg))
12522                     warning ("prefix `%D' should return `%T'", decl,
12523                                 build_reference_type (arg));
12524                 }
12525               else
12526                 {
12527                   if (!same_type_p (TYPE_MAIN_VARIANT (ret), arg))
12528                     warning ("postfix `%D' should return `%T'", decl, arg);
12529                 }
12530             }
12531         }
12532       else if (unary_op_p (operator_code))
12533         {
12534           if (arity != 1)
12535             {
12536               if (methodp)
12537                 error ("`%D' must take `void'", decl);
12538               else
12539                 error ("`%D' must take exactly one argument", decl);
12540             }
12541         }
12542       else /* if (binary_op_p (operator_code)) */
12543         {
12544           if (arity != 2)
12545             {
12546               if (methodp)
12547                 error ("`%D' must take exactly one argument", decl);
12548               else
12549                 error ("`%D' must take exactly two arguments", decl);
12550             }
12551
12552           /* More Effective C++ rule 7.  */
12553           if (warn_ecpp
12554               && (operator_code == TRUTH_ANDIF_EXPR
12555                   || operator_code == TRUTH_ORIF_EXPR
12556                   || operator_code == COMPOUND_EXPR))
12557             warning ("user-defined `%D' always evaluates both arguments",
12558                         decl);
12559         }
12560
12561       /* Effective C++ rule 23.  */
12562       if (warn_ecpp
12563           && arity == 2
12564           && !DECL_ASSIGNMENT_OPERATOR_P (decl)
12565           && (operator_code == PLUS_EXPR
12566               || operator_code == MINUS_EXPR
12567               || operator_code == TRUNC_DIV_EXPR
12568               || operator_code == MULT_EXPR
12569               || operator_code == TRUNC_MOD_EXPR)
12570           && TREE_CODE (TREE_TYPE (TREE_TYPE (decl))) == REFERENCE_TYPE)
12571         warning ("`%D' should return by value", decl);
12572
12573       /* [over.oper]/8 */
12574       for (; argtypes && argtypes != void_list_node;
12575           argtypes = TREE_CHAIN (argtypes))
12576         if (TREE_PURPOSE (argtypes))
12577           {
12578             TREE_PURPOSE (argtypes) = NULL_TREE;
12579             if (operator_code == POSTINCREMENT_EXPR
12580                 || operator_code == POSTDECREMENT_EXPR)
12581               {
12582                 if (pedantic)
12583                   pedwarn ("`%D' cannot have default arguments", decl);
12584               }
12585             else
12586               error ("`%D' cannot have default arguments", decl);
12587           }
12588
12589     }
12590 }
12591 \f
12592 static const char *
12593 tag_name (enum tag_types code)
12594 {
12595   switch (code)
12596     {
12597     case record_type:
12598       return "struct";
12599     case class_type:
12600       return "class";
12601     case union_type:
12602       return "union ";
12603     case enum_type:
12604       return "enum";
12605     default:
12606       abort ();
12607     }
12608 }
12609
12610 /* Get the struct, enum or union (CODE says which) with tag NAME.
12611    Define the tag as a forward-reference if it is not defined.
12612
12613    C++: If a class derivation is given, process it here, and report
12614    an error if multiple derivation declarations are not identical.
12615
12616    If this is a definition, come in through xref_tag and only look in
12617    the current frame for the name (since C++ allows new names in any
12618    scope.)  */
12619
12620 tree
12621 xref_tag (enum tag_types tag_code, tree name, tree attributes, 
12622           bool globalize)
12623 {
12624   enum tree_code code;
12625   register tree ref, t;
12626   struct cp_binding_level *b = current_binding_level;
12627   tree context = NULL_TREE;
12628
12629   timevar_push (TV_NAME_LOOKUP);
12630   switch (tag_code)
12631     {
12632     case record_type:
12633     case class_type:
12634       code = RECORD_TYPE;
12635       break;
12636     case union_type:
12637       code = UNION_TYPE;
12638       break;
12639     case enum_type:
12640       code = ENUMERAL_TYPE;
12641       break;
12642     default:
12643       abort ();
12644     }
12645
12646   /* If a cross reference is requested, look up the type
12647      already defined for this tag and return it.  */
12648   if (TYPE_P (name))
12649     {
12650       t = name;
12651       name = TYPE_IDENTIFIER (t);
12652     }
12653   else
12654     t = IDENTIFIER_TYPE_VALUE (name);
12655
12656   /* Warn about 'friend struct Inherited;' doing the wrong thing.  */
12657   if (t && globalize && TREE_CODE (t) == TYPENAME_TYPE)
12658     {
12659       static int explained;
12660       tree shadowed;
12661
12662       warning ("`%s %T' declares a new type at namespace scope",
12663                   tag_name (tag_code), name);
12664       if (!explained++)
12665         warning ("  names from dependent base classes are not visible to unqualified name lookup - to refer to the inherited type, say `%s %T::%T'",
12666                     tag_name (tag_code),
12667                     constructor_name (current_class_type),
12668                     TYPE_IDENTIFIER (t));
12669
12670       /* We need to remove the class scope binding for the
12671          TYPENAME_TYPE as otherwise poplevel_class gets confused.  */
12672       for (shadowed = b->class_shadowed;
12673            shadowed;
12674            shadowed = TREE_CHAIN (shadowed))
12675         if (TREE_TYPE (shadowed) == TYPE_NAME (t))
12676           {
12677             TREE_PURPOSE (shadowed) = NULL_TREE;
12678             break;
12679           }
12680     }
12681
12682   if (t && TREE_CODE (t) != code && TREE_CODE (t) != TEMPLATE_TYPE_PARM
12683       && TREE_CODE (t) != BOUND_TEMPLATE_TEMPLATE_PARM)
12684     t = NULL_TREE;
12685
12686   if (! globalize)
12687     {
12688       /* If we know we are defining this tag, only look it up in
12689          this scope and don't try to find it as a type.  */
12690       ref = lookup_tag (code, name, b, 1);
12691     }
12692   else
12693     {
12694       if (t)
12695         {
12696           ref = follow_tag_typedef (t);
12697
12698           /* [dcl.type.elab] If the identifier resolves to a
12699              typedef-name or a template type-parameter, the
12700              elaborated-type-specifier is ill-formed.  */
12701           if (!ref)
12702             {
12703               pedwarn ("using typedef-name `%D' after `%s'",
12704                        TYPE_NAME (t), tag_name (tag_code));
12705               ref = t;
12706             }
12707           else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
12708             error ("using template type parameter `%T' after `%s'",
12709                    t, tag_name (tag_code));
12710         }
12711       else
12712         ref = lookup_tag (code, name, b, 0);
12713
12714       if (! ref)
12715         {
12716           /* Try finding it as a type declaration.  If that wins,
12717              use it.  */
12718           ref = lookup_name (name, 1);
12719
12720           if (ref != NULL_TREE
12721               && processing_template_decl
12722               && DECL_CLASS_TEMPLATE_P (ref)
12723               && template_class_depth (current_class_type) == 0)
12724             /* Since GLOBALIZE is true, we're declaring a global
12725                template, so we want this type.  */
12726             ref = DECL_TEMPLATE_RESULT (ref);
12727
12728           if (ref && TREE_CODE (ref) == TYPE_DECL
12729               && TREE_CODE (TREE_TYPE (ref)) == code)
12730             ref = TREE_TYPE (ref);
12731           else
12732             ref = NULL_TREE;
12733         }
12734
12735       if (ref && current_class_type
12736           && template_class_depth (current_class_type)
12737           && PROCESSING_REAL_TEMPLATE_DECL_P ())
12738         {
12739           /* Since GLOBALIZE is nonzero, we are not looking at a
12740              definition of this tag.  Since, in addition, we are currently
12741              processing a (member) template declaration of a template
12742              class, we must be very careful; consider:
12743
12744                template <class X>
12745                struct S1
12746
12747                template <class U>
12748                struct S2
12749                { template <class V>
12750                friend struct S1; };
12751
12752              Here, the S2::S1 declaration should not be confused with the
12753              outer declaration.  In particular, the inner version should
12754              have a template parameter of level 2, not level 1.  This
12755              would be particularly important if the member declaration
12756              were instead:
12757
12758                template <class V = U> friend struct S1;
12759
12760              say, when we should tsubst into `U' when instantiating
12761              S2.  On the other hand, when presented with:
12762
12763                  template <class T>
12764                  struct S1 {
12765                    template <class U>
12766                    struct S2 {};
12767                    template <class U>
12768                    friend struct S2;
12769                  };
12770
12771               we must find the inner binding eventually.  We
12772               accomplish this by making sure that the new type we
12773               create to represent this declaration has the right
12774               TYPE_CONTEXT.  */
12775           context = TYPE_CONTEXT (ref);
12776           ref = NULL_TREE;
12777         }
12778     }
12779
12780   if (! ref)
12781     {
12782       /* If no such tag is yet defined, create a forward-reference node
12783          and record it as the "definition".
12784          When a real declaration of this type is found,
12785          the forward-reference will be altered into a real type.  */
12786       if (code == ENUMERAL_TYPE)
12787         {
12788           error ("use of enum `%#D' without previous declaration", name);
12789
12790           ref = make_node (ENUMERAL_TYPE);
12791
12792           /* Give the type a default layout like unsigned int
12793              to avoid crashing if it does not get defined.  */
12794           TYPE_MODE (ref) = TYPE_MODE (unsigned_type_node);
12795           TYPE_ALIGN (ref) = TYPE_ALIGN (unsigned_type_node);
12796           TYPE_USER_ALIGN (ref) = 0;
12797           TREE_UNSIGNED (ref) = 1;
12798           TYPE_PRECISION (ref) = TYPE_PRECISION (unsigned_type_node);
12799           TYPE_MIN_VALUE (ref) = TYPE_MIN_VALUE (unsigned_type_node);
12800           TYPE_MAX_VALUE (ref) = TYPE_MAX_VALUE (unsigned_type_node);
12801
12802           /* Enable us to recognize when a type is created in class context.
12803              To do nested classes correctly, this should probably be cleared
12804              out when we leave this classes scope.  Currently this in only
12805              done in `start_enum'.  */
12806
12807           pushtag (name, ref, globalize);
12808         }
12809       else
12810         {
12811           struct cp_binding_level *old_b = class_binding_level;
12812
12813           ref = make_aggr_type (code);
12814           TYPE_CONTEXT (ref) = context;
12815
12816 #ifdef NONNESTED_CLASSES
12817           /* Class types don't nest the way enums do.  */
12818           class_binding_level = (struct cp_binding_level *)0;
12819 #endif
12820           pushtag (name, ref, globalize);
12821           class_binding_level = old_b;
12822         }
12823     }
12824   else
12825     {
12826       if (!globalize && processing_template_decl && IS_AGGR_TYPE (ref))
12827         redeclare_class_template (ref, current_template_parms);
12828     }
12829
12830   TYPE_ATTRIBUTES (ref) = attributes;
12831
12832   POP_TIMEVAR_AND_RETURN (TV_NAME_LOOKUP, ref);
12833 }
12834
12835 tree
12836 xref_tag_from_type (tree old, tree id, int globalize)
12837 {
12838   enum tag_types tag_kind;
12839
12840   if (TREE_CODE (old) == RECORD_TYPE)
12841     tag_kind = (CLASSTYPE_DECLARED_CLASS (old) ? class_type : record_type);
12842   else
12843     tag_kind  = union_type;
12844
12845   if (id == NULL_TREE)
12846     id = TYPE_IDENTIFIER (old);
12847
12848   return xref_tag (tag_kind, id, /*attributes=*/NULL_TREE, globalize);
12849 }
12850
12851 /* REF is a type (named NAME), for which we have just seen some
12852    baseclasses.  BASE_LIST is a list of those baseclasses; the
12853    TREE_PURPOSE is an access_* node, and the TREE_VALUE is the type of
12854    the base-class.  TREE_VIA_VIRTUAL indicates virtual
12855    inheritance. CODE_TYPE_NODE indicates whether REF is a class,
12856    struct, or union.  */
12857
12858 void
12859 xref_basetypes (tree ref, tree base_list)
12860 {
12861   /* In the declaration `A : X, Y, ... Z' we mark all the types
12862      (A, X, Y, ..., Z) so we can check for duplicates.  */
12863   tree *basep;
12864
12865   int i;
12866   enum tag_types tag_code;
12867
12868   if (TREE_CODE (ref) == UNION_TYPE)
12869     {
12870       error ("derived union `%T' invalid", ref);
12871       return;
12872     }
12873
12874   tag_code = (CLASSTYPE_DECLARED_CLASS (ref) ? class_type : record_type);
12875
12876   /* First, make sure that any templates in base-classes are
12877      instantiated.  This ensures that if we call ourselves recursively
12878      we do not get confused about which classes are marked and which
12879      are not.  */
12880   basep = &base_list; 
12881   while (*basep) 
12882     {
12883       tree basetype = TREE_VALUE (*basep);
12884       if (!(processing_template_decl && uses_template_parms (basetype))
12885           && !complete_type_or_else (basetype, NULL))
12886         /* An incomplete type.  Remove it from the list.  */
12887         *basep = TREE_CHAIN (*basep);
12888       else
12889         basep = &TREE_CHAIN (*basep);
12890     }
12891
12892   SET_CLASSTYPE_MARKED (ref);
12893   i = list_length (base_list);
12894   if (i)
12895     {
12896       tree binfo = TYPE_BINFO (ref);
12897       tree binfos = make_tree_vec (i);
12898       tree accesses = make_tree_vec (i);
12899       
12900       BINFO_BASETYPES (binfo) = binfos;
12901       BINFO_BASEACCESSES (binfo) = accesses;
12902   
12903       for (i = 0; base_list; base_list = TREE_CHAIN (base_list))
12904         {
12905           tree access = TREE_PURPOSE (base_list);
12906           int via_virtual = TREE_VIA_VIRTUAL (base_list);
12907           tree basetype = TREE_VALUE (base_list);
12908           tree base_binfo;
12909           
12910           if (access == access_default_node)
12911             /* The base of a derived struct is public by default.  */
12912             access = (tag_code == class_type
12913                       ? access_private_node : access_public_node);
12914           
12915           if (basetype && TREE_CODE (basetype) == TYPE_DECL)
12916             basetype = TREE_TYPE (basetype);
12917           if (!basetype
12918               || (TREE_CODE (basetype) != RECORD_TYPE
12919                   && TREE_CODE (basetype) != TYPENAME_TYPE
12920                   && TREE_CODE (basetype) != TEMPLATE_TYPE_PARM
12921                   && TREE_CODE (basetype) != BOUND_TEMPLATE_TEMPLATE_PARM))
12922             {
12923               error ("base type `%T' fails to be a struct or class type",
12924                      basetype);
12925               continue;
12926             }
12927           
12928           if (CLASSTYPE_MARKED (basetype))
12929             {
12930               if (basetype == ref)
12931                 error ("recursive type `%T' undefined", basetype);
12932               else
12933                 error ("duplicate base type `%T' invalid", basetype);
12934               continue;
12935             }
12936           
12937           if (TYPE_FOR_JAVA (basetype)
12938               && (current_lang_depth () == 0))
12939             TYPE_FOR_JAVA (ref) = 1;
12940           
12941           if (CLASS_TYPE_P (basetype))
12942             {
12943               base_binfo = TYPE_BINFO (basetype);
12944               /* This flag will be in the binfo of the base type, we must
12945                  clear it after copying the base binfos.  */
12946               BINFO_DEPENDENT_BASE_P (base_binfo)
12947                 = dependent_type_p (basetype);
12948             }
12949           else
12950             base_binfo = make_binfo (size_zero_node, basetype,
12951                                      NULL_TREE, NULL_TREE);
12952           
12953           TREE_VEC_ELT (binfos, i) = base_binfo;
12954           TREE_VEC_ELT (accesses, i) = access;
12955           /* This flag will be in the binfo of the base type, we must
12956              clear it after copying the base binfos.  */
12957           TREE_VIA_VIRTUAL (base_binfo) = via_virtual;
12958           
12959           SET_CLASSTYPE_MARKED (basetype);
12960           
12961           /* We are free to modify these bits because they are meaningless
12962              at top level, and BASETYPE is a top-level type.  */
12963           if (via_virtual || TYPE_USES_VIRTUAL_BASECLASSES (basetype))
12964             {
12965               TYPE_USES_VIRTUAL_BASECLASSES (ref) = 1;
12966               /* Converting to a virtual base class requires looking
12967                  up the offset of the virtual base.  */
12968               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
12969             }
12970           
12971           if (CLASS_TYPE_P (basetype))
12972             {
12973               TYPE_HAS_NEW_OPERATOR (ref)
12974                 |= TYPE_HAS_NEW_OPERATOR (basetype);
12975               TYPE_HAS_ARRAY_NEW_OPERATOR (ref)
12976                 |= TYPE_HAS_ARRAY_NEW_OPERATOR (basetype);
12977               TYPE_GETS_DELETE (ref) |= TYPE_GETS_DELETE (basetype);
12978               /* If the base-class uses multiple inheritance, so do we.  */
12979               TYPE_USES_MULTIPLE_INHERITANCE (ref)
12980                 |= TYPE_USES_MULTIPLE_INHERITANCE (basetype);
12981               /* Likewise, if converting to a base of the base may require
12982                  code, then we may need to generate code to convert to a
12983                  base as well.  */
12984               TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref)
12985                 |= TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (basetype);
12986             }
12987           i++;
12988         }
12989       if (i)
12990         TREE_VEC_LENGTH (accesses) = TREE_VEC_LENGTH (binfos) = i;
12991       else
12992         BINFO_BASEACCESSES (binfo) = BINFO_BASETYPES (binfo) = NULL_TREE;
12993       
12994       if (i > 1)
12995         {
12996           TYPE_USES_MULTIPLE_INHERITANCE (ref) = 1;
12997           /* If there is more than one non-empty they cannot be at the same
12998              address.  */
12999           TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (ref) = 1;
13000         }
13001     }
13002   
13003   /* Copy the base binfos, collect the virtual bases and set the
13004      inheritance order chain.  */
13005   copy_base_binfos (TYPE_BINFO (ref), ref, NULL_TREE);
13006   CLASSTYPE_VBASECLASSES (ref) = nreverse (CLASSTYPE_VBASECLASSES (ref));
13007   
13008   /* Unmark all the types.  */
13009   while (i--)
13010     {
13011       tree basetype = BINFO_TYPE (BINFO_BASETYPE (TYPE_BINFO (ref), i));
13012       
13013       CLEAR_CLASSTYPE_MARKED (basetype);
13014       if (CLASS_TYPE_P (basetype))
13015         {
13016           TREE_VIA_VIRTUAL (TYPE_BINFO (basetype)) = 0;
13017           BINFO_DEPENDENT_BASE_P (TYPE_BINFO (basetype)) = 0;
13018         }
13019     }
13020   CLEAR_CLASSTYPE_MARKED (ref);
13021 }
13022
13023 \f
13024 /* Begin compiling the definition of an enumeration type.
13025    NAME is its name (or null if anonymous).
13026    Returns the type object, as yet incomplete.
13027    Also records info about it so that build_enumerator
13028    may be used to declare the individual values as they are read.  */
13029
13030 tree
13031 start_enum (tree name)
13032 {
13033   register tree enumtype = NULL_TREE;
13034   struct cp_binding_level *b = current_binding_level;
13035
13036   /* If this is the real definition for a previous forward reference,
13037      fill in the contents in the same object that used to be the
13038      forward reference.  */
13039
13040   if (name != NULL_TREE)
13041     enumtype = lookup_tag (ENUMERAL_TYPE, name, b, 1);
13042
13043   if (enumtype != NULL_TREE && TREE_CODE (enumtype) == ENUMERAL_TYPE)
13044     {
13045       error ("multiple definition of `%#T'", enumtype);
13046       cp_error_at ("previous definition here", enumtype);
13047       /* Clear out TYPE_VALUES, and start again.  */
13048       TYPE_VALUES (enumtype) = NULL_TREE;
13049     }
13050   else
13051     {
13052       enumtype = make_node (ENUMERAL_TYPE);
13053       pushtag (name, enumtype, 0);
13054     }
13055
13056   return enumtype;
13057 }
13058
13059 /* After processing and defining all the values of an enumeration type,
13060    install their decls in the enumeration type and finish it off.
13061    ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
13062
13063 void
13064 finish_enum (tree enumtype)
13065 {
13066   tree pair;
13067   tree minnode;
13068   tree maxnode;
13069   tree t;
13070   bool unsignedp;
13071   int lowprec;
13072   int highprec; 
13073   int precision;
13074
13075   /* We built up the VALUES in reverse order.  */
13076   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
13077
13078   /* For an enum defined in a template, all further processing is
13079      postponed until the template is instantiated.  */
13080   if (processing_template_decl)
13081     {
13082       if (at_function_scope_p ())
13083         add_stmt (build_min (TAG_DEFN, enumtype));
13084       return;
13085     }
13086
13087   if (TYPE_VALUES (enumtype))
13088     {
13089       /* Initialize min and max values and figure out actual values in
13090          following 'for' loop.  */
13091       minnode = maxnode = NULL_TREE;
13092
13093       /* [dcl.enum]
13094          
13095       Following the closing brace of an enum-specifier, each
13096       enumerator has the type of its enumeration.  Prior to the
13097       closing brace, the type of each enumerator is the type of
13098       its initializing value.  */
13099       for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13100         {
13101
13102           tree value;
13103
13104           /* If we are going to reset type then copy node first.
13105              It cannot be shared now.  */
13106           if (TREE_TYPE (TREE_VALUE (pair)) != enumtype)
13107             {
13108               if (DECL_INITIAL (TREE_VALUE (pair)))
13109                 DECL_INITIAL (TREE_VALUE (pair)) = 
13110                   copy_node (DECL_INITIAL (TREE_VALUE (pair)));
13111               TREE_TYPE (TREE_VALUE (pair)) = enumtype;
13112             }
13113
13114           if (!processing_template_decl)
13115             {
13116               /* Adjust min and max value.  */
13117               value = DECL_INITIAL (TREE_VALUE (pair));
13118
13119               if (!minnode)
13120                 minnode = maxnode = value;
13121               else if (tree_int_cst_lt (maxnode, value))
13122                 maxnode = value;
13123               else if (tree_int_cst_lt (value, minnode))
13124                 minnode = value;
13125             }
13126         }
13127     }
13128   else
13129     minnode = maxnode = integer_zero_node;
13130
13131
13132   /* Compute the number of bits require to represent all values of the
13133      enumeration.  We must do this before the type of MINNODE and
13134      MAXNODE are transformed, since min_precision relies on the
13135      TREE_TYPE of the value it is passed.  */
13136   unsignedp = tree_int_cst_sgn (minnode) >= 0;
13137   lowprec = min_precision (minnode, unsignedp);
13138   highprec = min_precision (maxnode, unsignedp);
13139   precision = MAX (lowprec, highprec);
13140
13141   /* Set the TREE_TYPE for the values as well.  That's so that when we
13142      call decl_constant_value we get an entity of the right type (but
13143      with the constant value).  In addition, transform the TYPE_VALUES
13144      list to contain the values, rather than the CONST_DECLs for them.  */
13145   for (pair = TYPE_VALUES (enumtype); pair; pair = TREE_CHAIN (pair))
13146     {
13147       tree value = DECL_INITIAL (TREE_VALUE (pair));
13148
13149       TREE_TYPE (value) = enumtype;
13150       TREE_VALUE (pair) = value;
13151     }
13152
13153   /* Set TYPE_MIN_VALUE and TYPE_MAX_VALUE according to `precision'.  */
13154   TYPE_SIZE (enumtype) = NULL_TREE;
13155   TYPE_PRECISION (enumtype) = precision;
13156   if (unsignedp)
13157     fixup_unsigned_type (enumtype);
13158   else
13159     fixup_signed_type (enumtype);
13160
13161   if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
13162     /* Use the width of the narrowest normal C type which is wide
13163        enough.  */
13164     TYPE_PRECISION (enumtype) = TYPE_PRECISION (c_common_type_for_size
13165                                                 (precision, 1));
13166   else
13167     TYPE_PRECISION (enumtype) = TYPE_PRECISION (integer_type_node);
13168
13169   TYPE_SIZE (enumtype) = NULL_TREE;
13170   layout_type (enumtype);
13171
13172   /* Fix up all variant types of this enum type.  */
13173   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
13174     {
13175       TYPE_VALUES (t) = TYPE_VALUES (enumtype);
13176       TYPE_MIN_VALUE (t) = TYPE_MIN_VALUE (enumtype);
13177       TYPE_MAX_VALUE (t) = TYPE_MAX_VALUE (enumtype);
13178       TYPE_SIZE (t) = TYPE_SIZE (enumtype);
13179       TYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (enumtype);
13180       TYPE_MODE (t) = TYPE_MODE (enumtype);
13181       TYPE_PRECISION (t) = TYPE_PRECISION (enumtype);
13182       TYPE_ALIGN (t) = TYPE_ALIGN (enumtype);
13183       TYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (enumtype);
13184       TREE_UNSIGNED (t) = TREE_UNSIGNED (enumtype);
13185     }
13186
13187   /* Finish debugging output for this type.  */
13188   rest_of_type_compilation (enumtype, namespace_bindings_p ());
13189 }
13190
13191 /* Build and install a CONST_DECL for an enumeration constant of the
13192    enumeration type ENUMTYPE whose NAME and VALUE (if any) are provided.
13193    Assignment of sequential values by default is handled here.  */
13194
13195 void
13196 build_enumerator (tree name, tree value, tree enumtype)
13197 {
13198   tree decl;
13199   tree context;
13200   tree type;
13201
13202   /* Remove no-op casts from the value.  */
13203   if (value)
13204     STRIP_TYPE_NOPS (value);
13205
13206   if (! processing_template_decl)
13207     {
13208       /* Validate and default VALUE.  */
13209       if (value != NULL_TREE)
13210         {
13211           value = decl_constant_value (value);
13212
13213           if (TREE_CODE (value) == INTEGER_CST)
13214             {
13215               value = default_conversion (value);
13216               constant_expression_warning (value);
13217             }
13218           else
13219             {
13220               error ("enumerator value for `%D' not integer constant", name);
13221               value = NULL_TREE;
13222             }
13223         }
13224
13225       /* Default based on previous value.  */
13226       if (value == NULL_TREE)
13227         {
13228           tree prev_value;
13229
13230           if (TYPE_VALUES (enumtype))
13231             {
13232               /* The next value is the previous value ...  */
13233               prev_value = DECL_INITIAL (TREE_VALUE (TYPE_VALUES (enumtype)));
13234               /* ... plus one.  */
13235               value = cp_build_binary_op (PLUS_EXPR,
13236                                           prev_value,
13237                                           integer_one_node);
13238
13239               if (tree_int_cst_lt (value, prev_value))
13240                 error ("overflow in enumeration values at `%D'", name);
13241             }
13242           else
13243             value = integer_zero_node;
13244         }
13245
13246       /* Remove no-op casts from the value.  */
13247       if (value)
13248         STRIP_TYPE_NOPS (value);
13249     }
13250
13251   /* C++ associates enums with global, function, or class declarations.  */
13252   context = current_scope ();
13253
13254   /* Build the actual enumeration constant.  Note that the enumeration
13255     constants have the type of their initializers until the
13256     enumeration is complete:
13257
13258       [ dcl.enum ]
13259
13260       Following the closing brace of an enum-specifier, each enumer-
13261       ator has the type of its enumeration.  Prior to the closing
13262       brace, the type of each enumerator is the type of its
13263       initializing value.
13264
13265     In finish_enum we will reset the type.  Of course, if we're
13266     processing a template, there may be no value.  */
13267   type = value ? TREE_TYPE (value) : NULL_TREE;
13268
13269   if (context && context == current_class_type)
13270     /* This enum declaration is local to the class.  We need the full
13271        lang_decl so that we can record DECL_CLASS_CONTEXT, for example.  */
13272     decl = build_lang_decl (CONST_DECL, name, type);
13273   else
13274     /* It's a global enum, or it's local to a function.  (Note local to
13275       a function could mean local to a class method.  */
13276     decl = build_decl (CONST_DECL, name, type);
13277
13278   DECL_CONTEXT (decl) = FROB_CONTEXT (context);
13279   DECL_INITIAL (decl) = value;
13280   TREE_READONLY (decl) = 1;
13281
13282   if (context && context == current_class_type)
13283     /* In something like `struct S { enum E { i = 7 }; };' we put `i'
13284       on the TYPE_FIELDS list for `S'.  (That's so that you can say
13285       things like `S::i' later.)  */
13286     finish_member_declaration (decl);
13287   else
13288     pushdecl (decl);
13289
13290   /* Add this enumeration constant to the list for this type.  */
13291   TYPE_VALUES (enumtype) = tree_cons (name, decl, TYPE_VALUES (enumtype));
13292 }
13293
13294 \f
13295 /* We're defining DECL.  Make sure that it's type is OK.  */
13296
13297 static void
13298 check_function_type (tree decl, tree current_function_parms)
13299 {
13300   tree fntype = TREE_TYPE (decl);
13301   tree return_type = complete_type (TREE_TYPE (fntype));
13302
13303   /* In a function definition, arg types must be complete.  */
13304   require_complete_types_for_parms (current_function_parms);
13305
13306   if (!COMPLETE_OR_VOID_TYPE_P (return_type))
13307     {
13308       error ("return type `%#T' is incomplete", TREE_TYPE (fntype));
13309
13310       /* Make it return void instead, but don't change the
13311          type of the DECL_RESULT, in case we have a named return value.  */
13312       if (TREE_CODE (fntype) == METHOD_TYPE)
13313         {
13314           tree ctype = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (fntype)));
13315           TREE_TYPE (decl)
13316             = build_cplus_method_type (ctype,
13317                                        void_type_node,
13318                                        FUNCTION_ARG_CHAIN (decl));
13319         }
13320       else
13321         TREE_TYPE (decl)
13322           = build_function_type (void_type_node,
13323                                  TYPE_ARG_TYPES (TREE_TYPE (decl)));
13324       TREE_TYPE (decl)
13325         = build_exception_variant (fntype,
13326                                    TYPE_RAISES_EXCEPTIONS (fntype));
13327     }
13328   else
13329     abstract_virtuals_error (decl, TREE_TYPE (fntype));
13330 }
13331
13332 /* Create the FUNCTION_DECL for a function definition.
13333    DECLSPECS and DECLARATOR are the parts of the declaration;
13334    they describe the function's name and the type it returns,
13335    but twisted together in a fashion that parallels the syntax of C.
13336
13337    FLAGS is a bitwise or of SF_PRE_PARSED (indicating that the
13338    DECLARATOR is really the DECL for the function we are about to
13339    process and that DECLSPECS should be ignored), SF_INCLASS_INLINE
13340    indicating that the function is an inline defined in-class.
13341
13342    This function creates a binding context for the function body
13343    as well as setting up the FUNCTION_DECL in current_function_decl.
13344
13345    Returns 1 on success.  If the DECLARATOR is not suitable for a function
13346    (it defines a datum instead), we return 0, which tells
13347    yyparse to report a parse error.
13348
13349    For C++, we must first check whether that datum makes any sense.
13350    For example, "class A local_a(1,2);" means that variable local_a
13351    is an aggregate of type A, which should have a constructor
13352    applied to it with the argument list [1, 2].  */
13353
13354 int
13355 start_function (tree declspecs, tree declarator, tree attrs, int flags)
13356 {
13357   tree decl1;
13358   tree ctype = NULL_TREE;
13359   tree fntype;
13360   tree restype;
13361   int doing_friend = 0;
13362   struct cp_binding_level *bl;
13363   tree current_function_parms;
13364
13365   /* Sanity check.  */
13366   my_friendly_assert (TREE_CODE (TREE_VALUE (void_list_node)) == VOID_TYPE, 160);
13367   my_friendly_assert (TREE_CHAIN (void_list_node) == NULL_TREE, 161);
13368
13369   /* This should only be done once on the top most decl.  */
13370   if (have_extern_spec)
13371     {
13372       declspecs = tree_cons (NULL_TREE, get_identifier ("extern"), declspecs);
13373       have_extern_spec = false;
13374     }
13375
13376   if (flags & SF_PRE_PARSED)
13377     {
13378       decl1 = declarator;
13379
13380       fntype = TREE_TYPE (decl1);
13381       if (TREE_CODE (fntype) == METHOD_TYPE)
13382         ctype = TYPE_METHOD_BASETYPE (fntype);
13383
13384       /* ISO C++ 11.4/5.  A friend function defined in a class is in
13385          the (lexical) scope of the class in which it is defined.  */
13386       if (!ctype && DECL_FRIEND_P (decl1))
13387         {
13388           ctype = DECL_FRIEND_CONTEXT (decl1);
13389
13390           /* CTYPE could be null here if we're dealing with a template;
13391              for example, `inline friend float foo()' inside a template
13392              will have no CTYPE set.  */
13393           if (ctype && TREE_CODE (ctype) != RECORD_TYPE)
13394             ctype = NULL_TREE;
13395           else
13396             doing_friend = 1;
13397         }
13398
13399       last_function_parms = DECL_ARGUMENTS (decl1);
13400     }
13401   else
13402     {
13403       decl1 = grokdeclarator (declarator, declspecs, FUNCDEF, 1, NULL);
13404       /* If the declarator is not suitable for a function definition,
13405          cause a syntax error.  */
13406       if (decl1 == NULL_TREE || TREE_CODE (decl1) != FUNCTION_DECL)
13407         return 0;
13408
13409       cplus_decl_attributes (&decl1, attrs, 0);
13410
13411       /* If #pragma weak was used, mark the decl weak now.  */
13412       if (current_binding_level == global_binding_level)
13413         maybe_apply_pragma_weak (decl1);
13414
13415       fntype = TREE_TYPE (decl1);
13416
13417       restype = TREE_TYPE (fntype);
13418       if (CLASS_TYPE_P (restype) && !CLASSTYPE_GOT_SEMICOLON (restype))
13419         {
13420           error ("semicolon missing after declaration of `%#T'", restype);
13421           shadow_tag (build_tree_list (NULL_TREE, restype));
13422           CLASSTYPE_GOT_SEMICOLON (restype) = 1;
13423           if (TREE_CODE (fntype) == FUNCTION_TYPE)
13424             fntype = build_function_type (integer_type_node,
13425                                           TYPE_ARG_TYPES (fntype));
13426           else
13427             fntype = build_cplus_method_type (build_type_variant (TYPE_METHOD_BASETYPE (fntype), TREE_READONLY (decl1), TREE_SIDE_EFFECTS (decl1)),
13428                                               integer_type_node,
13429                                               TYPE_ARG_TYPES (fntype));
13430           TREE_TYPE (decl1) = fntype;
13431         }
13432
13433       if (TREE_CODE (fntype) == METHOD_TYPE)
13434         ctype = TYPE_METHOD_BASETYPE (fntype);
13435       else if (DECL_MAIN_P (decl1))
13436         {
13437           /* If this doesn't return integer_type, complain.  */
13438           if (TREE_TYPE (TREE_TYPE (decl1)) != integer_type_node)
13439             {
13440               if (pedantic || warn_return_type)
13441                 pedwarn ("return type for `main' changed to `int'");
13442               TREE_TYPE (decl1) = fntype = default_function_type;
13443             }
13444         }
13445     }
13446
13447   if (DECL_DECLARED_INLINE_P (decl1)
13448       && lookup_attribute ("noinline", attrs))
13449     warning ("%Hinline function '%D' given attribute noinline",
13450              &DECL_SOURCE_LOCATION (decl1), decl1);
13451
13452   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl1))
13453     /* This is a constructor, we must ensure that any default args
13454        introduced by this definition are propagated to the clones
13455        now. The clones are used directly in overload resolution.  */
13456     adjust_clone_args (decl1);
13457
13458   /* Sometimes we don't notice that a function is a static member, and
13459      build a METHOD_TYPE for it.  Fix that up now.  */
13460   if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
13461       && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
13462     {
13463       revert_static_member_fn (decl1);
13464       last_function_parms = TREE_CHAIN (last_function_parms);
13465       ctype = NULL_TREE;
13466     }
13467
13468   /* Warn if function was previously implicitly declared
13469      (but not if we warned then).  */
13470   if (! warn_implicit
13471       && IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)) != NULL_TREE)
13472     cp_warning_at ("`%D' implicitly declared before its definition", IDENTIFIER_IMPLICIT_DECL (DECL_NAME (decl1)));
13473
13474   /* Set up current_class_type, and enter the scope of the class, if
13475      appropriate.  */
13476   if (ctype)
13477     push_nested_class (ctype);
13478   else if (DECL_STATIC_FUNCTION_P (decl1))
13479     push_nested_class (DECL_CONTEXT (decl1));
13480
13481   /* Now that we have entered the scope of the class, we must restore
13482      the bindings for any template parameters surrounding DECL1, if it
13483      is an inline member template.  (Order is important; consider the
13484      case where a template parameter has the same name as a field of
13485      the class.)  It is not until after this point that
13486      PROCESSING_TEMPLATE_DECL is guaranteed to be set up correctly.  */
13487   if (flags & SF_INCLASS_INLINE)
13488     maybe_begin_member_template_processing (decl1);
13489
13490   /* Effective C++ rule 15.  */
13491   if (warn_ecpp
13492       && DECL_OVERLOADED_OPERATOR_P (decl1) == NOP_EXPR
13493       && TREE_CODE (TREE_TYPE (fntype)) == VOID_TYPE)
13494     warning ("`operator=' should return a reference to `*this'");
13495
13496   /* Make the init_value nonzero so pushdecl knows this is not tentative.
13497      error_mark_node is replaced below (in poplevel) with the BLOCK.  */
13498   if (!DECL_INITIAL (decl1))
13499     DECL_INITIAL (decl1) = error_mark_node;
13500
13501   /* This function exists in static storage.
13502      (This does not mean `static' in the C sense!)  */
13503   TREE_STATIC (decl1) = 1;
13504
13505   /* We must call push_template_decl after current_class_type is set
13506      up.  (If we are processing inline definitions after exiting a
13507      class scope, current_class_type will be NULL_TREE until set above
13508      by push_nested_class.)  */
13509   if (processing_template_decl)
13510     decl1 = push_template_decl (decl1);
13511
13512   /* We are now in the scope of the function being defined.  */
13513   current_function_decl = decl1;
13514
13515   /* Save the parm names or decls from this function's declarator
13516      where store_parm_decls will find them.  */
13517   current_function_parms = last_function_parms;
13518
13519   /* Make sure the parameter and return types are reasonable.  When
13520      you declare a function, these types can be incomplete, but they
13521      must be complete when you define the function.  */
13522   if (! processing_template_decl)
13523     check_function_type (decl1, current_function_parms);
13524
13525   /* Build the return declaration for the function.  */
13526   restype = TREE_TYPE (fntype);
13527   /* Promote the value to int before returning it.  */
13528   if (c_promoting_integer_type_p (restype))
13529     restype = type_promotes_to (restype);
13530   if (DECL_RESULT (decl1) == NULL_TREE)
13531     {
13532       DECL_RESULT (decl1)
13533         = build_decl (RESULT_DECL, 0, TYPE_MAIN_VARIANT (restype));
13534       c_apply_type_quals_to_decl (cp_type_quals (restype),
13535                                   DECL_RESULT (decl1));
13536     }
13537
13538   /* Initialize RTL machinery.  We cannot do this until
13539      CURRENT_FUNCTION_DECL and DECL_RESULT are set up.  We do this
13540      even when processing a template; this is how we get
13541      CFUN set up, and our per-function variables initialized.
13542      FIXME factor out the non-RTL stuff.  */
13543   bl = current_binding_level;
13544   init_function_start (decl1, input_filename, lineno);
13545   current_binding_level = bl;
13546
13547   /* Even though we're inside a function body, we still don't want to
13548      call expand_expr to calculate the size of a variable-sized array.
13549      We haven't necessarily assigned RTL to all variables yet, so it's
13550      not safe to try to expand expressions involving them.  */
13551   immediate_size_expand = 0;
13552   cfun->x_dont_save_pending_sizes_p = 1;
13553
13554   /* Start the statement-tree, start the tree now.  */
13555   begin_stmt_tree (&DECL_SAVED_TREE (decl1));
13556
13557   /* Let the user know we're compiling this function.  */
13558   announce_function (decl1);
13559
13560   /* Record the decl so that the function name is defined.
13561      If we already have a decl for this name, and it is a FUNCTION_DECL,
13562      use the old decl.  */
13563   if (!processing_template_decl && !(flags & SF_PRE_PARSED))
13564     {
13565       /* A specialization is not used to guide overload resolution.  */
13566       if (!DECL_TEMPLATE_SPECIALIZATION (decl1)
13567           && ! DECL_FUNCTION_MEMBER_P (decl1))
13568         decl1 = pushdecl (decl1);
13569       else
13570         {
13571           /* We need to set the DECL_CONTEXT.  */
13572           if (!DECL_CONTEXT (decl1) && DECL_TEMPLATE_INFO (decl1))
13573             DECL_CONTEXT (decl1) = DECL_CONTEXT (DECL_TI_TEMPLATE (decl1));
13574           /* And make sure we have enough default args.  */
13575           check_default_args (decl1);
13576         }
13577       fntype = TREE_TYPE (decl1);
13578     }
13579
13580   /* Reset these in case the call to pushdecl changed them.  */
13581   current_function_decl = decl1;
13582   cfun->decl = decl1;
13583
13584   /* If we are (erroneously) defining a function that we have already
13585      defined before, wipe out what we knew before.  */
13586   if (!DECL_PENDING_INLINE_P (decl1))
13587     DECL_SAVED_FUNCTION_DATA (decl1) = NULL;
13588
13589   if (ctype && !doing_friend && !DECL_STATIC_FUNCTION_P (decl1))
13590     {
13591       /* We know that this was set up by `grokclassfn'.  We do not
13592          wait until `store_parm_decls', since evil parse errors may
13593          never get us to that point.  Here we keep the consistency
13594          between `current_class_type' and `current_class_ptr'.  */
13595       tree t = DECL_ARGUMENTS (decl1);
13596
13597       my_friendly_assert (t != NULL_TREE && TREE_CODE (t) == PARM_DECL,
13598                           162);
13599       my_friendly_assert (TREE_CODE (TREE_TYPE (t)) == POINTER_TYPE,
13600                           19990811);
13601
13602       cp_function_chain->x_current_class_ref
13603         = build_indirect_ref (t, NULL);
13604       cp_function_chain->x_current_class_ptr = t;
13605
13606       /* Constructors and destructors need to know whether they're "in
13607          charge" of initializing virtual base classes.  */
13608       t = TREE_CHAIN (t);
13609       if (DECL_HAS_IN_CHARGE_PARM_P (decl1))
13610         {
13611           current_in_charge_parm = t;
13612           t = TREE_CHAIN (t);
13613         }
13614       if (DECL_HAS_VTT_PARM_P (decl1))
13615         {
13616           if (DECL_NAME (t) != vtt_parm_identifier)
13617             abort ();
13618           current_vtt_parm = t;
13619         }
13620     }
13621
13622   if (DECL_INTERFACE_KNOWN (decl1))
13623     {
13624       tree ctx = decl_function_context (decl1);
13625
13626       if (DECL_NOT_REALLY_EXTERN (decl1))
13627         DECL_EXTERNAL (decl1) = 0;
13628
13629       if (ctx != NULL_TREE && DECL_DECLARED_INLINE_P (ctx)
13630           && TREE_PUBLIC (ctx))
13631         /* This is a function in a local class in an extern inline
13632            function.  */
13633         comdat_linkage (decl1);
13634     }
13635   /* If this function belongs to an interface, it is public.
13636      If it belongs to someone else's interface, it is also external.
13637      This only affects inlines and template instantiations.  */
13638   else if (interface_unknown == 0
13639            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13640                || flag_alt_external_templates))
13641     {
13642       if (DECL_DECLARED_INLINE_P (decl1) 
13643           || DECL_TEMPLATE_INSTANTIATION (decl1)
13644           || processing_template_decl)
13645         {
13646           DECL_EXTERNAL (decl1)
13647             = (interface_only
13648                || (DECL_DECLARED_INLINE_P (decl1) 
13649                    && ! flag_implement_inlines
13650                    && !DECL_VINDEX (decl1)));
13651
13652           /* For WIN32 we also want to put these in linkonce sections.  */
13653           maybe_make_one_only (decl1);
13654         }
13655       else
13656         DECL_EXTERNAL (decl1) = 0;
13657       DECL_NOT_REALLY_EXTERN (decl1) = 0;
13658       DECL_INTERFACE_KNOWN (decl1) = 1;
13659     }
13660   else if (interface_unknown && interface_only
13661            && (! DECL_TEMPLATE_INSTANTIATION (decl1)
13662                || flag_alt_external_templates))
13663     {
13664       /* If MULTIPLE_SYMBOL_SPACES is defined and we saw a #pragma
13665          interface, we will have interface_only set but not
13666          interface_known.  In that case, we don't want to use the normal
13667          heuristics because someone will supply a #pragma implementation
13668          elsewhere, and deducing it here would produce a conflict.  */
13669       comdat_linkage (decl1);
13670       DECL_EXTERNAL (decl1) = 0;
13671       DECL_INTERFACE_KNOWN (decl1) = 1;
13672       DECL_DEFER_OUTPUT (decl1) = 1;
13673     }
13674   else
13675     {
13676       /* This is a definition, not a reference.
13677          So clear DECL_EXTERNAL.  */
13678       DECL_EXTERNAL (decl1) = 0;
13679
13680       if ((DECL_DECLARED_INLINE_P (decl1) 
13681            || DECL_TEMPLATE_INSTANTIATION (decl1))
13682           && ! DECL_INTERFACE_KNOWN (decl1)
13683           /* Don't try to defer nested functions for now.  */
13684           && ! decl_function_context (decl1))
13685         DECL_DEFER_OUTPUT (decl1) = 1;
13686       else
13687         DECL_INTERFACE_KNOWN (decl1) = 1;
13688     }
13689
13690   pushlevel (0);
13691   current_binding_level->parm_flag = 1;
13692
13693   ++function_depth;
13694
13695   if (DECL_DESTRUCTOR_P (decl1))
13696     {
13697       dtor_label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
13698       DECL_CONTEXT (dtor_label) = current_function_decl;
13699     }
13700
13701   start_fname_decls ();
13702   
13703   store_parm_decls (current_function_parms);
13704
13705   return 1;
13706 }
13707 \f
13708 /* Store the parameter declarations into the current function declaration.
13709    This is called after parsing the parameter declarations, before
13710    digesting the body of the function.
13711
13712    Also install to binding contour return value identifier, if any.  */
13713
13714 static void
13715 store_parm_decls (tree current_function_parms)
13716 {
13717   register tree fndecl = current_function_decl;
13718   register tree parm;
13719
13720   /* This is a chain of any other decls that came in among the parm
13721      declarations.  If a parm is declared with  enum {foo, bar} x;
13722      then CONST_DECLs for foo and bar are put here.  */
13723   tree nonparms = NULL_TREE;
13724
13725   if (current_function_parms)
13726     {
13727       /* This case is when the function was defined with an ANSI prototype.
13728          The parms already have decls, so we need not do anything here
13729          except record them as in effect
13730          and complain if any redundant old-style parm decls were written.  */
13731
13732       tree specparms = current_function_parms;
13733       tree next;
13734
13735       /* Must clear this because it might contain TYPE_DECLs declared
13736              at class level.  */
13737       storedecls (NULL_TREE);
13738
13739       /* If we're doing semantic analysis, then we'll call pushdecl
13740              for each of these.  We must do them in reverse order so that
13741              they end in the correct forward order.  */
13742       specparms = nreverse (specparms);
13743
13744       for (parm = specparms; parm; parm = next)
13745         {
13746           next = TREE_CHAIN (parm);
13747           if (TREE_CODE (parm) == PARM_DECL)
13748             {
13749               if (DECL_NAME (parm) == NULL_TREE
13750                   || TREE_CODE (parm) != VOID_TYPE)
13751                 pushdecl (parm);
13752               else
13753                 error ("parameter `%D' declared void", parm);
13754             }
13755           else
13756             {
13757               /* If we find an enum constant or a type tag,
13758                  put it aside for the moment.  */
13759               TREE_CHAIN (parm) = NULL_TREE;
13760               nonparms = chainon (nonparms, parm);
13761             }
13762         }
13763
13764       /* Get the decls in their original chain order and record in the
13765          function.  This is all and only the PARM_DECLs that were
13766          pushed into scope by the loop above.  */
13767       DECL_ARGUMENTS (fndecl) = getdecls ();
13768       storetags (gettags ());
13769     }
13770   else
13771     DECL_ARGUMENTS (fndecl) = NULL_TREE;
13772
13773   /* Now store the final chain of decls for the arguments
13774      as the decl-chain of the current lexical scope.
13775      Put the enumerators in as well, at the front so that
13776      DECL_ARGUMENTS is not modified.  */
13777   storedecls (chainon (nonparms, DECL_ARGUMENTS (fndecl)));
13778
13779   /* Do the starting of the exception specifications, if we have any.  */
13780   if (flag_exceptions && !processing_template_decl
13781       && flag_enforce_eh_specs
13782       && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
13783     current_eh_spec_block = begin_eh_spec_block ();
13784 }
13785
13786 \f
13787 /* We have finished doing semantic analysis on DECL, but have not yet
13788    generated RTL for its body.  Save away our current state, so that
13789    when we want to generate RTL later we know what to do.  */
13790
13791 static void
13792 save_function_data (tree decl)
13793 {
13794   struct language_function *f;
13795
13796   /* Save the language-specific per-function data so that we can
13797      get it back when we really expand this function.  */
13798   my_friendly_assert (!DECL_PENDING_INLINE_P (decl),
13799                       19990908);
13800
13801   /* Make a copy.  */
13802   f = ((struct language_function *)
13803        ggc_alloc (sizeof (struct language_function)));
13804   memcpy (f, cp_function_chain, sizeof (struct language_function));
13805   DECL_SAVED_FUNCTION_DATA (decl) = f;
13806
13807   /* Clear out the bits we don't need.  */
13808   f->base.x_stmt_tree.x_last_stmt = NULL_TREE;
13809   f->base.x_stmt_tree.x_last_expr_type = NULL_TREE;
13810   f->x_named_label_uses = NULL;
13811   f->bindings = NULL;
13812   f->x_local_names = NULL;
13813
13814   /* When we get back here again, we will be expanding.  */
13815   f->x_expanding_p = 1;
13816
13817   /* If we've already decided that we cannot inline this function, we
13818      must remember that fact when we actually go to expand the
13819      function.  */
13820   if (current_function_cannot_inline)
13821     {
13822       f->cannot_inline = current_function_cannot_inline;
13823       DECL_INLINE (decl) = 0;
13824     }
13825 }
13826
13827 /* Add a note to mark the beginning of the main body of the constructor.
13828    This is used to set up the data structures for the cleanup regions for
13829    fully-constructed bases and members.  */
13830
13831 static void
13832 begin_constructor_body (void)
13833 {
13834 }
13835
13836 /* Add a note to mark the end of the main body of the constructor.  This is
13837    used to end the cleanup regions for fully-constructed bases and
13838    members.  */
13839
13840 static void
13841 finish_constructor_body (void)
13842 {
13843 }
13844
13845 /* Do all the processing for the beginning of a destructor; set up the
13846    vtable pointers and cleanups for bases and members.  */
13847
13848 static void
13849 begin_destructor_body (void)
13850 {
13851   tree if_stmt;
13852   tree compound_stmt;
13853
13854   /* If the dtor is empty, and we know there is not any possible
13855      way we could use any vtable entries, before they are possibly
13856      set by a base class dtor, we don't have to setup the vtables,
13857      as we know that any base class dtor will set up any vtables
13858      it needs.  We avoid MI, because one base class dtor can do a
13859      virtual dispatch to an overridden function that would need to
13860      have a non-related vtable set up, we cannot avoid setting up
13861      vtables in that case.  We could change this to see if there
13862      is just one vtable.
13863
13864      ??? In the destructor for a class, the vtables are set
13865      appropriately for that class.  There will be no non-related
13866      vtables.  jason 2001-12-11.  */
13867   if_stmt = begin_if_stmt ();
13868
13869   /* If it is not safe to avoid setting up the vtables, then
13870      someone will change the condition to be boolean_true_node.  
13871      (Actually, for now, we do not have code to set the condition
13872      appropriately, so we just assume that we always need to
13873      initialize the vtables.)  */
13874   finish_if_stmt_cond (boolean_true_node, if_stmt);
13875
13876   compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
13877
13878   /* Make all virtual function table pointers in non-virtual base
13879      classes point to CURRENT_CLASS_TYPE's virtual function
13880      tables.  */
13881   initialize_vtbl_ptrs (current_class_ptr);
13882
13883   finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
13884   finish_then_clause (if_stmt);
13885   finish_if_stmt ();
13886
13887   /* And insert cleanups for our bases and members so that they
13888      will be properly destroyed if we throw.  */
13889   push_base_cleanups ();
13890 }
13891
13892 /* At the end of every destructor we generate code to delete the object if
13893    necessary.  Do that now.  */
13894
13895 static void
13896 finish_destructor_body (void)
13897 {
13898   tree exprstmt;
13899
13900   /* Any return from a destructor will end up here; that way all base
13901      and member cleanups will be run when the function returns.  */
13902   add_stmt (build_stmt (LABEL_STMT, dtor_label));
13903
13904   /* In a virtual destructor, we must call delete.  */
13905   if (DECL_VIRTUAL_P (current_function_decl))
13906     {
13907       tree if_stmt;
13908       tree virtual_size = cxx_sizeof (current_class_type);
13909
13910       /* [class.dtor]
13911
13912       At the point of definition of a virtual destructor (including
13913       an implicit definition), non-placement operator delete shall
13914       be looked up in the scope of the destructor's class and if
13915       found shall be accessible and unambiguous.  */
13916       exprstmt = build_op_delete_call
13917         (DELETE_EXPR, current_class_ptr, virtual_size,
13918          LOOKUP_NORMAL | LOOKUP_SPECULATIVELY, NULL_TREE);
13919
13920       if_stmt = begin_if_stmt ();
13921       finish_if_stmt_cond (build (BIT_AND_EXPR, integer_type_node,
13922                                   current_in_charge_parm,
13923                                   integer_one_node),
13924                            if_stmt);
13925       finish_expr_stmt (exprstmt);
13926       finish_then_clause (if_stmt);
13927       finish_if_stmt ();
13928     }
13929 }
13930
13931 /* Do the necessary processing for the beginning of a function body, which
13932    in this case includes member-initializers, but not the catch clauses of
13933    a function-try-block.  Currently, this means opening a binding level
13934    for the member-initializers (in a ctor) and member cleanups (in a dtor).
13935    In other functions, this isn't necessary, but it doesn't hurt.  */
13936
13937 tree
13938 begin_function_body (void)
13939 {
13940   tree stmt;
13941
13942   if (processing_template_decl)
13943     /* Do nothing now.  */;
13944   else
13945     /* Always keep the BLOCK node associated with the outermost pair of
13946        curly braces of a function.  These are needed for correct
13947        operation of dwarfout.c.  */
13948     keep_next_level (1);
13949
13950   stmt = begin_compound_stmt (0);
13951   COMPOUND_STMT_BODY_BLOCK (stmt) = 1;
13952
13953   if (processing_template_decl)
13954     /* Do nothing now.  */;
13955   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13956     begin_constructor_body ();
13957   else if (DECL_DESTRUCTOR_P (current_function_decl))
13958     begin_destructor_body ();
13959
13960   return stmt;
13961 }
13962
13963 /* Do the processing for the end of a function body.  Currently, this means
13964    closing out the cleanups for fully-constructed bases and members, and in
13965    the case of the destructor, deleting the object if desired.  Again, this
13966    is only meaningful for [cd]tors, since they are the only functions where
13967    there is a significant distinction between the main body and any
13968    function catch clauses.  Handling, say, main() return semantics here
13969    would be wrong, as flowing off the end of a function catch clause for
13970    main() would also need to return 0.  */
13971
13972 void
13973 finish_function_body (tree compstmt)
13974 {
13975   /* Close the block.  */
13976   finish_compound_stmt (0, compstmt);
13977
13978   if (processing_template_decl)
13979     /* Do nothing now.  */;
13980   else if (DECL_CONSTRUCTOR_P (current_function_decl))
13981     finish_constructor_body ();
13982   else if (DECL_DESTRUCTOR_P (current_function_decl))
13983     finish_destructor_body ();
13984 }  
13985
13986 /* Finish up a function declaration and compile that function
13987    all the way to assembler language output.  The free the storage
13988    for the function definition.
13989
13990    FLAGS is a bitwise or of the following values:
13991      2 - INCLASS_INLINE
13992        We just finished processing the body of an in-class inline
13993        function definition.  (This processing will have taken place
13994        after the class definition is complete.)  */
13995
13996 tree
13997 finish_function (int flags)
13998 {
13999   register tree fndecl = current_function_decl;
14000   tree fntype, ctype = NULL_TREE;
14001   int inclass_inline = (flags & 2) != 0;
14002   int nested;
14003
14004   /* When we get some parse errors, we can end up without a
14005      current_function_decl, so cope.  */
14006   if (fndecl == NULL_TREE)
14007     return error_mark_node;
14008
14009   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fndecl)
14010       && DECL_VIRTUAL_P (fndecl)
14011       && !processing_template_decl)
14012     {
14013       tree fnclass = DECL_CONTEXT (fndecl);
14014       if (fndecl == CLASSTYPE_KEY_METHOD (fnclass))
14015         keyed_classes = tree_cons (NULL_TREE, fnclass, keyed_classes);
14016     }
14017
14018   nested = function_depth > 1;
14019   fntype = TREE_TYPE (fndecl);
14020
14021   /*  TREE_READONLY (fndecl) = 1;
14022       This caused &foo to be of type ptr-to-const-function
14023       which then got a warning when stored in a ptr-to-function variable.  */
14024
14025   my_friendly_assert (building_stmt_tree (), 20000911);
14026
14027   finish_fname_decls ();
14028   
14029   /* For a cloned function, we've already got all the code we need;
14030      there's no need to add any extra bits.  */
14031   if (!DECL_CLONED_FUNCTION_P (fndecl))
14032     {
14033       if (DECL_MAIN_P (current_function_decl))
14034         {
14035           /* Make it so that `main' always returns 0 by default.  */
14036 #if VMS_TARGET
14037           finish_return_stmt (integer_one_node);
14038 #else
14039           finish_return_stmt (integer_zero_node);
14040 #endif
14041         }
14042
14043       /* Finish dealing with exception specifiers.  */
14044       if (flag_exceptions && !processing_template_decl
14045           && flag_enforce_eh_specs
14046           && TYPE_RAISES_EXCEPTIONS (TREE_TYPE (current_function_decl)))
14047         finish_eh_spec_block (TYPE_RAISES_EXCEPTIONS
14048                               (TREE_TYPE (current_function_decl)),
14049                               current_eh_spec_block);
14050     }
14051
14052   /* If we're saving up tree structure, tie off the function now.  */
14053   finish_stmt_tree (&DECL_SAVED_TREE (fndecl));
14054
14055   /* This must come after expand_function_end because cleanups might
14056      have declarations (from inline functions) that need to go into
14057      this function's blocks.  */
14058   
14059   /* If the current binding level isn't the outermost binding level
14060      for this function, either there is a bug, or we have experienced
14061      syntax errors and the statement tree is malformed.  */
14062   if (current_binding_level->parm_flag != 1)
14063     {
14064       /* Make sure we have already experienced errors.  */
14065       if (errorcount == 0)
14066         abort ();
14067
14068       /* Throw away the broken statement tree and extra binding
14069          levels.  */
14070       DECL_SAVED_TREE (fndecl) = build_stmt (COMPOUND_STMT, NULL_TREE);
14071
14072       while (current_binding_level->parm_flag != 1)
14073         {
14074           if (current_binding_level->parm_flag == 2)
14075             pop_nested_class ();
14076           else
14077             poplevel (0, 0, 0);
14078         }
14079     }
14080   poplevel (1, 0, 1);
14081
14082   /* Set up the named return value optimization, if we can.  Here, we
14083      eliminate the copy from the nrv into the RESULT_DECL and any cleanup
14084      for the nrv.  genrtl_start_function and declare_return_variable
14085      handle making the nrv and RESULT_DECL share space.  */
14086   if (current_function_return_value)
14087     {
14088       tree r = current_function_return_value;
14089       /* This is only worth doing for fns that return in memory--and
14090          simpler, since we don't have to worry about promoted modes.  */
14091       if (r != error_mark_node
14092           && aggregate_value_p (TREE_TYPE (TREE_TYPE (fndecl))))
14093         {
14094           DECL_ALIGN (r) = DECL_ALIGN (DECL_RESULT (fndecl));
14095           walk_tree_without_duplicates (&DECL_SAVED_TREE (fndecl),
14096                                         nullify_returns_r, r);
14097         }
14098       else
14099         /* Clear it so genrtl_start_function and declare_return_variable
14100            know we're not optimizing.  */
14101         current_function_return_value = NULL_TREE;
14102     }
14103
14104   /* Remember that we were in class scope.  */
14105   if (current_class_name)
14106     ctype = current_class_type;
14107
14108   /* Must mark the RESULT_DECL as being in this function.  */
14109   DECL_CONTEXT (DECL_RESULT (fndecl)) = fndecl;
14110
14111   /* Set the BLOCK_SUPERCONTEXT of the outermost function scope to point
14112      to the FUNCTION_DECL node itself.  */
14113   BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
14114
14115   /* Save away current state, if appropriate.  */
14116   if (!processing_template_decl)
14117     save_function_data (fndecl);
14118
14119   /* If this function calls `setjmp' it cannot be inlined.  When
14120      `longjmp' is called it is not guaranteed to restore the value of
14121      local variables that have been modified since the call to
14122      `setjmp'.  So, if were to inline this function into some caller
14123      `c', then when we `longjmp', we might not restore all variables
14124      in `c'.  (It might seem, at first blush, that there's no way for
14125      this function to modify local variables in `c', but their
14126      addresses may have been stored somewhere accessible to this
14127      function.)  */
14128   if (!processing_template_decl && calls_setjmp_p (fndecl))
14129     DECL_UNINLINABLE (fndecl) = 1;
14130
14131   /* Complain if there's just no return statement.  */
14132   if (warn_return_type
14133       && TREE_CODE (TREE_TYPE (fntype)) != VOID_TYPE
14134       && !current_function_returns_value && !current_function_returns_null
14135       /* Don't complain if we abort or throw.  */
14136       && !current_function_returns_abnormally
14137       && !DECL_NAME (DECL_RESULT (fndecl))
14138       /* Normally, with -Wreturn-type, flow will complain.  Unless we're an
14139          inline function, as we might never be compiled separately.  */
14140       && (DECL_INLINE (fndecl) || processing_template_decl))
14141     warning ("no return statement in function returning non-void");
14142     
14143   /* Clear out memory we no longer need.  */
14144   free_after_parsing (cfun);
14145   /* Since we never call rest_of_compilation, we never clear
14146      CFUN.  Do so explicitly.  */
14147   free_after_compilation (cfun);
14148   cfun = NULL;
14149
14150   /* If this is an in-class inline definition, we may have to pop the
14151      bindings for the template parameters that we added in
14152      maybe_begin_member_template_processing when start_function was
14153      called.  */
14154   if (inclass_inline)
14155     maybe_end_member_template_processing ();
14156
14157   /* Leave the scope of the class.  */
14158   if (ctype)
14159     pop_nested_class ();
14160
14161   --function_depth;
14162
14163   /* Clean up.  */
14164   if (! nested)
14165     /* Let the error reporting routines know that we're outside a
14166        function.  For a nested function, this value is used in
14167        cxx_pop_function_context and then reset via pop_function_context.  */
14168     current_function_decl = NULL_TREE;
14169
14170   return fndecl;
14171 }
14172 \f
14173 /* Create the FUNCTION_DECL for a function definition.
14174    DECLSPECS and DECLARATOR are the parts of the declaration;
14175    they describe the return type and the name of the function,
14176    but twisted together in a fashion that parallels the syntax of C.
14177
14178    This function creates a binding context for the function body
14179    as well as setting up the FUNCTION_DECL in current_function_decl.
14180
14181    Returns a FUNCTION_DECL on success.
14182
14183    If the DECLARATOR is not suitable for a function (it defines a datum
14184    instead), we return 0, which tells yyparse to report a parse error.
14185
14186    May return void_type_node indicating that this method is actually
14187    a friend.  See grokfield for more details.
14188
14189    Came here with a `.pushlevel' .
14190
14191    DO NOT MAKE ANY CHANGES TO THIS CODE WITHOUT MAKING CORRESPONDING
14192    CHANGES TO CODE IN `grokfield'.  */
14193
14194 tree
14195 start_method (tree declspecs, tree declarator, tree attrlist)
14196 {
14197   tree fndecl = grokdeclarator (declarator, declspecs, MEMFUNCDEF, 0,
14198                                 &attrlist);
14199
14200   if (fndecl == error_mark_node)
14201     return error_mark_node;
14202
14203   if (fndecl == NULL || TREE_CODE (fndecl) != FUNCTION_DECL)
14204     {
14205       error ("invalid member function declaration");
14206       return error_mark_node;
14207     }
14208
14209   if (attrlist)
14210     cplus_decl_attributes (&fndecl, attrlist, 0);
14211
14212   /* Pass friends other than inline friend functions back.  */
14213   if (fndecl == void_type_node)
14214     return fndecl;
14215
14216   if (DECL_IN_AGGR_P (fndecl))
14217     {
14218       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (fndecl)) != current_class_type)
14219         {
14220           if (DECL_CONTEXT (fndecl)
14221               && TREE_CODE( DECL_CONTEXT (fndecl)) != NAMESPACE_DECL)
14222             error ("`%D' is already defined in class `%T'", fndecl,
14223                       DECL_CONTEXT (fndecl));
14224         }
14225       return void_type_node;
14226     }
14227
14228   check_template_shadow (fndecl);
14229
14230   DECL_DECLARED_INLINE_P (fndecl) = 1;
14231
14232   DID_INLINE_FUNC (fndecl) = 0;
14233   if (flag_default_inline)
14234     DECL_INLINE (fndecl) = 1;
14235
14236   /* We process method specializations in finish_struct_1.  */
14237   if (processing_template_decl && !DECL_TEMPLATE_SPECIALIZATION (fndecl))
14238     fndecl = push_template_decl (fndecl);
14239
14240   if (! DECL_FRIEND_P (fndecl))
14241     {
14242       if (TREE_CHAIN (fndecl))
14243         {
14244           fndecl = copy_node (fndecl);
14245           TREE_CHAIN (fndecl) = NULL_TREE;
14246         }
14247       grok_special_member_properties (fndecl);
14248     }
14249
14250   cp_finish_decl (fndecl, NULL_TREE, NULL_TREE, 0);
14251
14252   /* Make a place for the parms */
14253   pushlevel (0);
14254   current_binding_level->parm_flag = 1;
14255
14256   DECL_IN_AGGR_P (fndecl) = 1;
14257   return fndecl;
14258 }
14259
14260 /* Go through the motions of finishing a function definition.
14261    We don't compile this method until after the whole class has
14262    been processed.
14263
14264    FINISH_METHOD must return something that looks as though it
14265    came from GROKFIELD (since we are defining a method, after all).
14266
14267    This is called after parsing the body of the function definition.
14268    STMTS is the chain of statements that makes up the function body.
14269
14270    DECL is the ..._DECL that `start_method' provided.  */
14271
14272 tree
14273 finish_method (tree decl)
14274 {
14275   register tree fndecl = decl;
14276   tree old_initial;
14277
14278   register tree link;
14279
14280   if (decl == void_type_node)
14281     return decl;
14282
14283   old_initial = DECL_INITIAL (fndecl);
14284
14285   /* Undo the level for the parms (from start_method).
14286      This is like poplevel, but it causes nothing to be
14287      saved.  Saving information here confuses symbol-table
14288      output routines.  Besides, this information will
14289      be correctly output when this method is actually
14290      compiled.  */
14291
14292   /* Clear out the meanings of the local variables of this level;
14293      also record in each decl which block it belongs to.  */
14294
14295   for (link = current_binding_level->names; link; link = TREE_CHAIN (link))
14296     {
14297       if (DECL_NAME (link) != NULL_TREE)
14298         pop_binding (DECL_NAME (link), link);
14299       my_friendly_assert (TREE_CODE (link) != FUNCTION_DECL, 163);
14300       DECL_CONTEXT (link) = NULL_TREE;
14301     }
14302
14303   poplevel (0, 0, 0);
14304
14305   DECL_INITIAL (fndecl) = old_initial;
14306
14307   /* We used to check if the context of FNDECL was different from
14308      current_class_type as another way to get inside here.  This didn't work
14309      for String.cc in libg++.  */
14310   if (DECL_FRIEND_P (fndecl))
14311     {
14312       CLASSTYPE_INLINE_FRIENDS (current_class_type)
14313         = tree_cons (NULL_TREE, fndecl, CLASSTYPE_INLINE_FRIENDS (current_class_type));
14314       decl = void_type_node;
14315     }
14316
14317   return decl;
14318 }
14319 \f
14320
14321 /* VAR is a VAR_DECL.  If its type is incomplete, remember VAR so that
14322    we can lay it out later, when and if its type becomes complete.  */
14323
14324 void
14325 maybe_register_incomplete_var (tree var)
14326 {
14327   my_friendly_assert (TREE_CODE (var) == VAR_DECL, 20020406);
14328
14329   /* Keep track of variables with incomplete types.  */
14330   if (!processing_template_decl && TREE_TYPE (var) != error_mark_node 
14331       && DECL_EXTERNAL (var))
14332     {
14333       tree inner_type = TREE_TYPE (var);
14334       
14335       while (TREE_CODE (inner_type) == ARRAY_TYPE)
14336         inner_type = TREE_TYPE (inner_type);
14337       inner_type = TYPE_MAIN_VARIANT (inner_type);
14338       
14339       if ((!COMPLETE_TYPE_P (inner_type) && CLASS_TYPE_P (inner_type))
14340           /* RTTI TD entries are created while defining the type_info.  */
14341           || (TYPE_LANG_SPECIFIC (inner_type)
14342               && TYPE_BEING_DEFINED (inner_type)))
14343         incomplete_vars = tree_cons (inner_type, var, incomplete_vars);
14344     }
14345 }
14346
14347 /* Called when a class type (given by TYPE) is defined.  If there are
14348    any existing VAR_DECLs whose type hsa been completed by this
14349    declaration, update them now.  */
14350
14351 void
14352 complete_vars (tree type)
14353 {
14354   tree *list = &incomplete_vars;
14355
14356   my_friendly_assert (CLASS_TYPE_P (type), 20020406);
14357   while (*list) 
14358     {
14359       if (same_type_p (type, TREE_PURPOSE (*list)))
14360         {
14361           tree var = TREE_VALUE (*list);
14362           /* Complete the type of the variable.  The VAR_DECL itself
14363              will be laid out in expand_expr.  */
14364           complete_type (TREE_TYPE (var));
14365           /* Remove this entry from the list.  */
14366           *list = TREE_CHAIN (*list);
14367         }
14368       else
14369         list = &TREE_CHAIN (*list);
14370     }
14371 }
14372
14373 /* If DECL is of a type which needs a cleanup, build that cleanup
14374    here.  */
14375
14376 tree
14377 cxx_maybe_build_cleanup (tree decl)
14378 {
14379   tree type = TREE_TYPE (decl);
14380
14381   if (type != error_mark_node && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
14382     {
14383       int flags = LOOKUP_NORMAL|LOOKUP_DESTRUCTOR;
14384       tree rval;
14385
14386       if (TREE_CODE (type) == ARRAY_TYPE)
14387         rval = decl;
14388       else
14389         {
14390           cxx_mark_addressable (decl);
14391           rval = build_unary_op (ADDR_EXPR, decl, 0);
14392         }
14393
14394       /* Optimize for space over speed here.  */
14395       if (! TYPE_USES_VIRTUAL_BASECLASSES (type)
14396           || flag_expensive_optimizations)
14397         flags |= LOOKUP_NONVIRTUAL;
14398
14399       rval = build_delete (TREE_TYPE (rval), rval,
14400                            sfk_complete_destructor, flags, 0);
14401
14402       if (TYPE_USES_VIRTUAL_BASECLASSES (type)
14403           && ! TYPE_HAS_DESTRUCTOR (type))
14404         rval = build_compound_expr (tree_cons (NULL_TREE, rval,
14405                                                build_tree_list (NULL_TREE, build_vbase_delete (type, decl))));
14406
14407       return rval;
14408     }
14409   return NULL_TREE;
14410 }
14411 \f
14412 /* When a stmt has been parsed, this function is called.  */
14413
14414 void
14415 finish_stmt (void)
14416 {
14417   /* Always assume this statement was not an expression statement.  If
14418      it actually was an expression statement, its our callers
14419      responsibility to fix this up.  */
14420   last_expr_type = NULL_TREE;
14421 }
14422
14423 /* DECL was originally constructed as a non-static member function,
14424    but turned out to be static.  Update it accordingly.  */
14425
14426 void
14427 revert_static_member_fn (tree decl)
14428 {
14429   tree tmp;
14430   tree function = TREE_TYPE (decl);
14431   tree args = TYPE_ARG_TYPES (function);
14432
14433   if (cp_type_quals (TREE_TYPE (TREE_VALUE (args)))
14434       != TYPE_UNQUALIFIED)
14435     error ("static member function `%#D' declared with type qualifiers",
14436               decl);
14437
14438   args = TREE_CHAIN (args);
14439   tmp = build_function_type (TREE_TYPE (function), args);
14440   tmp = build_qualified_type (tmp, cp_type_quals (function));
14441   tmp = build_exception_variant (tmp,
14442                                  TYPE_RAISES_EXCEPTIONS (function));
14443   TREE_TYPE (decl) = tmp;
14444   if (DECL_ARGUMENTS (decl))
14445     DECL_ARGUMENTS (decl) = TREE_CHAIN (DECL_ARGUMENTS (decl));
14446   DECL_STATIC_FUNCTION_P (decl) = 1;
14447 }
14448
14449 /* Initialize the variables used during compilation of a C++
14450    function.  */
14451
14452 void
14453 cxx_push_function_context (struct function * f)
14454 {
14455   struct language_function *p
14456     = ((struct language_function *)
14457        ggc_alloc_cleared (sizeof (struct language_function)));
14458   f->language = p;
14459
14460   /* It takes an explicit call to expand_body to generate RTL for a
14461      function.  */
14462   expanding_p = 0;
14463
14464   /* Whenever we start a new function, we destroy temporaries in the
14465      usual way.  */
14466   current_stmt_tree ()->stmts_are_full_exprs_p = 1;
14467 }
14468
14469 /* Free the language-specific parts of F, now that we've finished
14470    compiling the function.  */
14471
14472 void
14473 cxx_pop_function_context (struct function * f)
14474 {
14475   f->language = 0;
14476 }
14477
14478 /* Return which tree structure is used by T, or TS_CP_GENERIC if T is
14479    one of the language-independent trees.  */
14480
14481 enum cp_tree_node_structure_enum
14482 cp_tree_node_structure (union lang_tree_node * t)
14483 {
14484   switch (TREE_CODE (&t->generic))
14485     {
14486     case DEFAULT_ARG:           return TS_CP_DEFAULT_ARG;
14487     case IDENTIFIER_NODE:       return TS_CP_IDENTIFIER;
14488     case CPLUS_BINDING:         return TS_CP_BINDING;
14489     case OVERLOAD:              return TS_CP_OVERLOAD;
14490     case TEMPLATE_PARM_INDEX:   return TS_CP_TPI;
14491     case PTRMEM_CST:            return TS_CP_PTRMEM;
14492     case BASELINK:              return TS_CP_BASELINK;
14493     case WRAPPER:               return TS_CP_WRAPPER;
14494     case SRCLOC:                return TS_CP_SRCLOC;
14495     default:                    return TS_CP_GENERIC;
14496     }
14497 }
14498
14499 /* Return the IDENTIFIER_GLOBAL_VALUE of T, for use in common code, since
14500    the definition of IDENTIFIER_GLOBAL_VALUE is different for C and C++.  */
14501
14502 tree
14503 identifier_global_value (tree t)
14504 {
14505   return IDENTIFIER_GLOBAL_VALUE (t);
14506 }
14507
14508 /* Build the void_list_node (void_type_node having been created).  */
14509 tree
14510 build_void_list_node (void)
14511 {
14512   tree t = build_tree_list (NULL_TREE, void_type_node);
14513   TREE_PARMLIST (t) = 1;
14514   return t;
14515 }
14516
14517 static int
14518 cp_missing_noreturn_ok_p (tree decl)
14519 {
14520   /* A missing noreturn is ok for the `main' function.  */
14521   return DECL_MAIN_P (decl);
14522 }
14523
14524 #include "gt-cp-decl.h"
14525 #include "gtype-cp.h"